diff options
| author | pixel <pixel> | 2002-12-07 00:42:14 +0000 | 
|---|---|---|
| committer | pixel <pixel> | 2002-12-07 00:42:14 +0000 | 
| commit | 11f44e36d5f02f8309fc463f9a2c6cd81b42c3ce (patch) | |
| tree | e0daba1124a46dd6dc30150066324f62c6c9eedc /lib | |
| parent | 70ab3da408f5939fbfec4237e87f503ae82b2179 (diff) | |
Whoups
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/engine.cc | 23 | ||||
| -rw-r--r-- | lib/glbase.cc | 142 | ||||
| -rw-r--r-- | lib/glfont.cc | 110 | ||||
| -rw-r--r-- | lib/gltexture.cc | 85 | 
4 files changed, 360 insertions, 0 deletions
| 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 <SDL/SDL.h> +#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 <SDL.h> +#include <SDL_opengl.h> + +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 <GL/gl.h> +#include <GL/glu.h> +#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); +} | 
