From 11f44e36d5f02f8309fc463f9a2c6cd81b42c3ce Mon Sep 17 00:00:00 2001 From: pixel Date: Sat, 7 Dec 2002 00:42:14 +0000 Subject: Whoups --- include/engine.h | 16 ++++++ include/glbase.h | 29 +++++++++++ include/glfont.h | 25 +++++++++ include/gltexture.h | 26 ++++++++++ lib/engine.cc | 23 +++++++++ lib/glbase.cc | 142 ++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/glfont.cc | 110 ++++++++++++++++++++++++++++++++++++++++ lib/gltexture.cc | 85 +++++++++++++++++++++++++++++++ 8 files changed, 456 insertions(+) create mode 100644 include/engine.h create mode 100644 include/glbase.h create mode 100644 include/glfont.h create mode 100644 include/gltexture.h create mode 100644 lib/engine.cc create mode 100644 lib/glbase.cc create mode 100644 lib/glfont.cc create mode 100644 lib/gltexture.cc diff --git a/include/engine.h b/include/engine.h new file mode 100644 index 0000000..2b8af3c --- /dev/null +++ b/include/engine.h @@ -0,0 +1,16 @@ +#ifndef __ENGINE_H__ +#define __ENGINE_H__ + +#include + +namespace mogltk { + class engine : public Base { + public: + static int setup() throw(GeneralException); + static int GetInited(); + private: + static int inited; + }; +}; + +#endif diff --git a/include/glbase.h b/include/glbase.h new file mode 100644 index 0000000..1852250 --- /dev/null +++ b/include/glbase.h @@ -0,0 +1,29 @@ +#ifndef __GLBASE_H__ +#define __GLBASE_H__ + +#include +#include +#include + +namespace mogltk { + class glbase : public Base { + public: + static int setup(int w = 640, int h = 480, int flags = 0) throw(GeneralException); + static int GetWidth(void); + static int GetHeight(void); + static int GetInited(void); + static void Enter2DMode(void); + static void Leave2DMode(void); + static void Flip(void); + static bool is2D(void); + static void glVertex(GLdouble, GLdouble, GLdouble = 0.0, GLdouble = 1.0); + static void glVertex(GLfloat, GLfloat, GLfloat = 0.0, GLfloat = 1.0); + static void glVertex(GLint, GLint, GLint = 0, GLint = 1); + static void glVertex(GLshort, GLshort, GLshort = 0, GLshort = 1); + private: + static int width, height, inited, twoD; + static SDL_Surface * surface; + }; +}; + +#endif diff --git a/include/glfont.h b/include/glfont.h new file mode 100644 index 0000000..de4ef63 --- /dev/null +++ b/include/glfont.h @@ -0,0 +1,25 @@ +#ifndef __GLFONT_H__ +#define __GLFONT_H__ + +#include +#include +#include +#include "gltexture.h" + +namespace mogltk { + class font : public Base { + public: + font(const String & = "font.bin"); + virtual ~font(); + void drawentry(Uint16, Color = Color(255, 255, 255), int = -1, int = -1); + + private: + Uint8 * sizes; + Uint16 nbentries, nbcT, nbT; + Uint8 flags, maxX, maxY, nbcU, nbcV; + texture ** fonttex; + Uint16 * corresp; + }; +}; + +#endif diff --git a/include/gltexture.h b/include/gltexture.h new file mode 100644 index 0000000..c18e6ec --- /dev/null +++ b/include/gltexture.h @@ -0,0 +1,26 @@ +#ifndef __GLTEXTURE_H__ +#define __GLTEXTURE_H__ + +#include +#include +#include + +namespace mogltk { + class texture : public Base { + public: + texture(int = 256, int = 256, bool = false) throw (GeneralException); + virtual ~texture(); + SDL_Surface * GetSurface() throw (GeneralException); + void Generate() throw (GeneralException); + void Bind(bool = true) throw (GeneralException); + GLuint GetWidth(); + GLuint GetHeight(); + static void Unbind(void); + private: + GLuint width, height, tex; + SDL_Surface * surface; + bool planar; + }; +}; + +#endif diff --git a/lib/engine.cc b/lib/engine.cc new file mode 100644 index 0000000..919fcb4 --- /dev/null +++ b/lib/engine.cc @@ -0,0 +1,23 @@ +#include +#include "engine.h" + +int mogltk::engine::inited = 0; + +int mogltk::engine::setup() throw(GeneralException) { + if (inited) { + printm(M_WARNING, "mogltk::engine::startup() called twice, ignoring second call.\n"); + return -1; + } + if (SDL_Init(0) < 0) { + throw GeneralException(_("Unable to start SDL base system")); + } + atexit(SDL_Quit); + + inited = 1; + + return 0; +} + +int mogltk::engine::GetInited() { + return inited; +} diff --git a/lib/glbase.cc b/lib/glbase.cc new file mode 100644 index 0000000..1fb62a5 --- /dev/null +++ b/lib/glbase.cc @@ -0,0 +1,142 @@ +#include "glbase.h" +#include "engine.h" +#include "generic.h" +#include +#include + +int mogltk::glbase::width, mogltk::glbase::height, mogltk::glbase::inited = 0, mogltk::glbase::twoD = 0; +SDL_Surface * mogltk::glbase::surface = 0; + +int mogltk::glbase::setup(int w, int h, int flags) throw(GeneralException) { + if (inited) { + printm(M_WARNING, "mogltk::glbase::setup called twice, ignoring second call...\n"); + return -1; + } + + width = w; + height = h; + + mogltk::engine::setup(); + if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) { + throw GeneralException(String("Couldn't initialise Video SubSystem: ") + SDL_GetError()); + } + + if (!(surface = SDL_SetVideoMode(width, height, 0, flags | SDL_OPENGL))) { + throw GeneralException(String("Couldn't set GL mode: ") + SDL_GetError()); + } + + float ratio = surface->w; + ratio /= surface->h; + + printm(M_INFO, "Video resolution: %dx%dx%d (ratio = %3.2f)\n", surface->w, surface->h, surface->format->BitsPerPixel, ratio); + printm(M_INFO, "\n"); + printm(M_INFO, "OpenGL infos\n"); + printm(M_INFO, "------------\n"); + printm(M_INFO, String("Vendor : ") + (char *) glGetString(GL_VENDOR) + "\n"); + printm(M_INFO, String("Renderer : ") + (char *) glGetString(GL_RENDERER) + "\n"); + printm(M_INFO, String("Version : ") + (char *) glGetString(GL_VERSION) + "\n"); + printm(M_INFO, String("Extensions: ") + (char *) glGetString(GL_EXTENSIONS) + "\n"); + + inited = 1; + + glViewport(0, 0, surface->w, surface->h); + + glCullFace(GL_BACK); + glFrontFace(GL_CCW); + glEnable(GL_CULL_FACE); + + glClearColor(0, 0, 0, 0); + glShadeModel(GL_SMOOTH); + + glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_LEQUAL); + glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluPerspective(60.0, ratio, 1.0, 1024.0); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + + glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + Flip(); + + return 0; +} + +int mogltk::glbase::GetWidth(void) { + return width; +} + +int mogltk::glbase::GetHeight(void) { + return height; +} + +int mogltk::glbase::GetInited(void) { + return inited; +} + +void mogltk::glbase::Enter2DMode(void) { + if (twoD) + return; + + glPushAttrib(GL_ENABLE_BIT); + glDisable(GL_DEPTH_TEST); + glDisable(GL_CULL_FACE); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + glMatrixMode(GL_PROJECTION); + glPushMatrix(); + glLoadIdentity(); + glOrtho(0.0, width, height, 0.0, 0.0, 1.0); + + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glLoadIdentity(); + + twoD = 1; +} + +void mogltk::glbase::Leave2DMode(void) { + if (!twoD) + return; + + glMatrixMode(GL_MODELVIEW); + glPopMatrix(); + glMatrixMode(GL_PROJECTION); + glPopMatrix(); + + glPopAttrib(); + + twoD = 0; +} + +void mogltk::glbase::Flip() { + SDL_GL_SwapBuffers(); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); +} + +bool mogltk::glbase::is2D() { + return twoD; +} + +void mogltk::glbase::glVertex(GLdouble x, GLdouble y, GLdouble z, GLdouble w) { + glVertex4d(x, y, z, w); +} + +void mogltk::glbase::glVertex(GLfloat x, GLfloat y, GLfloat z, GLfloat w) { + glVertex4f(x, y, z, w); +} + +void mogltk::glbase::glVertex(GLint x, GLint y, GLint z, GLint w) { + glVertex4i(x, y, z, w); +} + +void mogltk::glbase::glVertex(GLshort x, GLshort y, GLshort z, GLshot w) { + glVertex4i(x, y, z, w); +} + diff --git a/lib/glfont.cc b/lib/glfont.cc new file mode 100644 index 0000000..82058c8 --- /dev/null +++ b/lib/glfont.cc @@ -0,0 +1,110 @@ +#include "glbase.h" +#include "glfont.h" +#include "Input.h" + +Uint8 prescale2[4] = { 0, 85, 170, 255 }, prescale3[8] = { 0, 36, 72, 109, 145, 182, 218, 255 }; + +mogltk::font::font(const String & file) { + Input ffont(file); + int i; + +#ifdef HAVE_ZLIB + ffont.SetZ(); +#endif + + ffont.read(&nbentries, 2); + ffont.read(&flags, 1); + ffont.read(&maxX, 1); + ffont.read(&maxY, 1); + + nbcU = 256 / maxX; + nbcV = 256 / maxY; + + nbcT = nbcU * nbcV; + + nbT = nbentries / nbcT; + + if (nbentries % nbcT) { + nbT++; + } + + fonttex = (texture **) malloc(nbT * sizeof(texture *)); + + for (i = 0; i < nbT; i++) { + fonttex[i] = new texture(256, 256, true); + } + + sizes = (Uint8 *) malloc(nbentries * sizeof(Uint8)); + + Uint8 * curtex = (Uint8 *) fonttex[0]->GetSurface()->pixels; + Uint8 curU = 0, curV = 0, curT = 0; + for (int i = 0; i < nbentries; i++) { + ffont.read(&sizes[i], 1); + for (int v = 0; v < maxY; v++) { + for (int u = 0; u < maxX; u++) { + Uint8 f; + ffont.read(&f, 1); + if (flags) { + Uint8 r, g, b, a; + r = f & 3; + g = (f >> 2) & 7; + b = (f >> 5) & 3; + a = (f >> 7) & 1; + curtex[(curU + u + (curV + v) * 256) * 4 + 0] = prescale2[r]; + curtex[(curU + u + (curV + v) * 256) * 4 + 1] = prescale3[g]; + curtex[(curU + u + (curV + v) * 256) * 4 + 2] = prescale2[b]; + curtex[(curU + u + (curV + v) * 256) * 4 + 3] = a ? 255 : 0; + } else { + curtex[(curU + u + (curV + v) * 256) * 4 + 0] = 255; + curtex[(curU + u + (curV + v) * 256) * 4 + 1] = 255; + curtex[(curU + u + (curV + v) * 256) * 4 + 2] = 255; + curtex[(curU + u + (curV + v) * 256) * 4 + 3] = f; + } + } + } + curU += maxX; + if (curU >= 256) { + curU = 0; + curV += maxY; + if (curV >= 256) { + curV = 0; + curT++; + curtex = (Uint8 *) fonttex[curT]->GetSurface()->pixels; + } + } + } + + for (int i = 0; i < nbT; i++) { + fonttex[i]->Generate(); + } + + corresp = (Uint16 *) malloc(nbentries * 2 * sizeof(Uint16)); + + ffont.read(corresp, 2 * sizeof(Uint16) * nbentries); +} + +mogltk::font::~font() { + int i; + for (i = 0; i < nbT; i++) { + delete fonttex[i]; + } + + free((void *) fonttex); + free(sizes); +} + +void mogltk::font::drawentry(Uint16 entry, Color c, int x, int y) { + bool was2D; + + was2D = mogltk::glbase::is2D(); + + if (!was2D) { + mogltk::glbase::Enter2DMode(); + } + + + + if (!was2D) { + mogltk::glbase::Leave2DMode(); + } +} diff --git a/lib/gltexture.cc b/lib/gltexture.cc new file mode 100644 index 0000000..7949cb9 --- /dev/null +++ b/lib/gltexture.cc @@ -0,0 +1,85 @@ +#include +#include +#include "gltexture.h" +#include "General.h" + +mogltk::texture::texture(int w, int h, bool plane) throw (GeneralException) : width(w), height(h), planar(plane) { + if ((BITCOUNT(w) != 1) || (BITCOUNT(h) != 1)) + throw GeneralException("Size of the texture not a power of 2!"); + + if (!(surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32, +#if SDL_BYTEORDER == SDL_BIG_ENDIAN + 0xff000000, + 0x00ff0000, + 0x0000ff00, + 0x000000ff +#else + 0x000000ff, + 0x0000ff00, + 0x00ff0000, + 0xff000000 +#endif + ))) { + throw GeneralException("Can't create RGB Surface"); + } +} + +mogltk::texture::~texture() { + if (surface) { + SDL_FreeSurface(surface); + } else { + glDeleteTextures(1, &tex); + } +} + +SDL_Surface * mogltk::texture::GetSurface() throw (GeneralException) { + if (!surface) + throw GeneralException("Texture already generated"); + return surface; +} + +void mogltk::texture::Generate() throw (GeneralException) { + if (!surface) + throw GeneralException("Texture already generated"); + + if (planar) { + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels); + } else { + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, width, height, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels); +// glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels); + } + + SDL_FreeSurface(surface); + surface = 0; +} + +void mogltk::texture::Bind(bool expand) throw (GeneralException) { + if (surface) + throw GeneralException("Texture is not yet generated"); + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, tex); + if (expand) { + glMatrixMode(GL_TEXTURE); + glLoadIdentity(); + glScaled(1 / (double) width, 1 / (double) height, 1); + glMatrixMode(GL_MODELVIEW); + } +} + +GLuint mogltk::texture::GetWidth() { + return width; +} + +GLuint mogltk::texture::GetHeight() { + return height; +} + +void mogltk::texture::Unbind(void) { + glDisable(GL_TEXTURE_2D); +} -- cgit v1.2.3