From 5cc874802b0b8c4462e7e873654e6daa54be00de Mon Sep 17 00:00:00 2001 From: pixel Date: Sun, 4 Jul 2004 11:56:18 +0000 Subject: SOL Demo --- lib/glbase.cc | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 84 insertions(+), 2 deletions(-) (limited to 'lib/glbase.cc') diff --git a/lib/glbase.cc b/lib/glbase.cc index 3d89644..9c33c54 100644 --- a/lib/glbase.cc +++ b/lib/glbase.cc @@ -1,6 +1,8 @@ #include #include #include +#include +#include #include "glbase.h" #include "engine.h" #include "generic.h" @@ -10,12 +12,24 @@ mogltk::glbase::glbase(int w, int h, int flags) throw (GeneralException) : mogltk::base(w, h, flags, 0), twoD(0), fovy(60.0) { SDL_Surface * surface; + GLint bits; mogltk::engine::setup(); if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) { throw GeneralException(String("Couldn't initialise Video SubSystem: ") + SDL_GetError()); } +#if 1 + SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); +// SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 32); + SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE, 8); +#endif if (!(surface = SDL_SetVideoMode(w, h, 0, flags | SDL_OPENGL | SDL_HWSURFACE))) { throw GeneralException(String("Couldn't set GL mode: ") + SDL_GetError()); } @@ -25,6 +39,8 @@ mogltk::glbase::glbase(int w, int h, int flags) throw (GeneralException) : moglt ratio = (GLdouble) surface->w / surface->h; + glGetIntegerv(GL_STENCIL_BITS, &bits); + 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"); @@ -34,6 +50,9 @@ mogltk::glbase::glbase(int w, int h, int flags) throw (GeneralException) : moglt printm(M_INFO, String("Version : ") + (char *) glGetString(GL_VERSION) + "\n"); printm(M_INFO, String("Extensions: ") + (char *) glGetString(GL_EXTENSIONS) + "\n"); + Output e("ext.txt"); + e.write(glGetString(GL_EXTENSIONS), strlen((char *) glGetString(GL_EXTENSIONS))); + glViewport(0, 0, surface->w, surface->h); glCullFace(GL_BACK); @@ -106,10 +125,11 @@ void mogltk::glbase::Leave2DMode(void) { twoD = 0; } -void mogltk::glbase::Flip() { +void mogltk::glbase::Flip(bool clear) { mogltk::engine::pollevents(); SDL_GL_SwapBuffers(); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + if (clear) + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); } bool mogltk::glbase::is2D() { @@ -161,3 +181,65 @@ void mogltk::glbase::changefovy(GLdouble nfoyv) { if (!engine::base_o->is2D()) gluPerspective(fovy, ratio, 1.0, 1024.0); } + +inline static unsigned int nextpower(unsigned int n) { + unsigned int i; + + if (!n) + return n; + + if (ISPOT(n)) + return n; + + for (i = 31; i >= 0; i--) { + if ((n >> i) & 1) { + return 1 << (i + 1); + } + } +} + +mogltk::texture * mogltk::glbase::GrabTexture() { + int w = nextpower(GetWidth()); + int h = nextpower(GetHeight()); + texture * r = new texture(w, h); + SDL_Surface * t; + + t = GrabSurface(); + SDL_BlitSurface(t, NULL, r->GetSurface(), NULL); + SDL_FreeSurface(t); + + return r; +} + +SDL_Surface * mogltk::glbase::GrabSurface() { + int i; + SDL_Surface * r; + Uint8 * pixels = (Uint8 *) malloc(GetWidth() * GetHeight() * 3); + Uint8 * s, * d; + + glReadPixels(0, 0, GetWidth(), GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, pixels); + + r = SDL_CreateRGBSurface(SDL_SWSURFACE, GetWidth(), GetHeight(), 24, +#if SDL_BYTEORDER == SDL_BIG_ENDIAN + 0xff000000, + 0x00ff0000, + 0x0000ff00, + 0x00000000 +#else + 0x000000ff, + 0x0000ff00, + 0x00ff0000, + 0x00000000 +#endif + ); + + for (i = 0; i < GetHeight(); i++) { + s = pixels + i * GetWidth() * 3; + d = ((Uint8 *) r->pixels) + (GetHeight() - i - 1) * GetWidth() * 3; + memcpy(d, s, GetWidth() * 3); + } + + free(pixels); + + return r; +} -- cgit v1.2.3