From 5cc874802b0b8c4462e7e873654e6daa54be00de Mon Sep 17 00:00:00 2001 From: pixel Date: Sun, 4 Jul 2004 11:56:18 +0000 Subject: SOL Demo --- lib/base.cc | 51 ++++++++++++++++++++++++++++++++-- lib/engine.cc | 15 ++++++++++ lib/font.cc | 2 +- lib/glbase.cc | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- lib/texture.cc | 50 ++++++++++++++++++++++++++++++---- 5 files changed, 194 insertions(+), 10 deletions(-) (limited to 'lib') diff --git a/lib/base.cc b/lib/base.cc index acfc654..fd5c1da 100644 --- a/lib/base.cc +++ b/lib/base.cc @@ -46,10 +46,11 @@ int mogltk::base::GetHeight(void) { return height; } -void mogltk::base::Flip() { +void mogltk::base::Flip(bool clear) { mogltk::engine::pollevents(); SDL_Flip(surface); - SDL_FillRect(surface, NULL, 0); + if (clear) + SDL_FillRect(surface, NULL, 0); } mogltk::base::base(int w, int h, int flags, int) : surface(0) { @@ -108,3 +109,49 @@ void mogltk::base::ToggleFullscreen() { surface = SDL_SetVideoMode(surface->w, surface->h, surface->format->BitsPerPixel, SDL_HWSURFACE | newflags); } + +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::base::GrabTexture() { + int w = nextpower(GetWidth()); + int h = nextpower(GetHeight()); + texture * r = new texture(w, h); + + SDL_BlitSurface(getsurface(), NULL, r->GetSurface(), NULL); + + return r; +} + +SDL_Surface * mogltk::base::GrabSurface() { + SDL_Surface * r = SDL_CreateRGBSurface(SDL_SWSURFACE, GetWidth(), GetHeight(), 24, +#if SDL_BYTEORDER == SDL_BIG_ENDIAN + 0xff000000, + 0x00ff0000, + 0x0000ff00, + 0x00000000 +#else + 0x000000ff, + 0x0000ff00, + 0x00ff0000, + 0x00000000 +#endif + ); + + SDL_BlitSurface(getsurface(), NULL, r, NULL); + + return r; +} diff --git a/lib/engine.cc b/lib/engine.cc index 3a2aaa0..34317b1 100644 --- a/lib/engine.cc +++ b/lib/engine.cc @@ -49,6 +49,21 @@ void mogltk::engine::keyevent::down(SDL_keysym) { printm(M_INFO, "Generic keyevent::down called\n"); } +class keyhandler_t : public mogltk::engine::keyevent { + virtual void down(SDL_keysym keysym) { + if (keysym.sym == SDLK_ESCAPE) + mogltk::engine::quit(); + else if ((keysym.sym == SDLK_RETURN) && (keysym.mod & KMOD_ALT)) + mogltk::engine::base_o->ToggleFullscreen(); + else if (old_handler) + old_handler->down(keysym); + } + virtual void up(SDL_keysym keysym) { + if (old_handler) + old_handler->up(keysym); + } +} basic_keyhandler; + mogltk::engine::mouseevent::mouseevent() { new_handler = 0; old_handler = getmouseevent(); diff --git a/lib/font.cc b/lib/font.cc index 4062c37..5534cfc 100644 --- a/lib/font.cc +++ b/lib/font.cc @@ -516,7 +516,7 @@ mogltk::rect mogltk::font::printtotex(texture * t, const char * p, ...) { return r; } -inline unsigned int nextpower(unsigned int n) { +inline static unsigned int nextpower(unsigned int n) { unsigned int i; if (!n) 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; +} diff --git a/lib/texture.cc b/lib/texture.cc index a8036bc..42c8f3f 100644 --- a/lib/texture.cc +++ b/lib/texture.cc @@ -17,7 +17,7 @@ mogltk::texture * mogltk::texture::footer = 0; mogltk::texture * mogltk::texture::active = 0; mogltk::texture::texture(int w, int h, bool plane) throw (GeneralException) : width(w), height(h), - texture_allocated(false), planar(plane), tainted(true) { + texture_allocated(false), planar(plane), tainted(true), taintable(true) { if ((!ISPOT(w)) || (!ISPOT(h))) throw GeneralException(_("Size of the texture not a power of 2!")); @@ -51,7 +51,7 @@ mogltk::texture::texture(int w, int h, bool plane) throw (GeneralException) : wi } mogltk::texture::texture(Handle * h, bool plane) throw (GeneralException) : - texture_allocated(false), planar(plane), tainted(true) { + texture_allocated(false), planar(plane), tainted(true), taintable(true) { SDL_Surface * temp; @@ -114,6 +114,34 @@ mogltk::texture::texture(Handle * h, bool plane) throw (GeneralException) : } } +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::texture(int x, int y, int w, int h) throw (GeneralException) : width(nextpower(w)), height(nextpower(h)), + texture_allocated(true), planar(false), tainted(false), taintable(false) { + glGenTextures(1, &tex); + glBindTexture(GL_TEXTURE_2D, tex); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + 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); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); + glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, x, y, w, h); +} + mogltk::texture::~texture() { if (surface) { SDL_FreeSurface(surface); @@ -141,7 +169,10 @@ mogltk::texture::~texture() { } Uint32 * mogltk::texture::GetPixels() { - return (Uint32 *) surface->pixels; + if (surface) + return (Uint32 *) surface->pixels; + else + return 0; } SDL_Surface * mogltk::texture::GetSurface() { @@ -149,7 +180,10 @@ SDL_Surface * mogltk::texture::GetSurface() { } SDL_PixelFormat * mogltk::texture::GetFormat() { - return surface->format; + if (surface) + return surface->format; + else + return 0; } void mogltk::texture::Generate() { @@ -245,7 +279,8 @@ void mogltk::texture::Unbind(void) { } void mogltk::texture::Taint(void) { - tainted = true; + if (taintable) + tainted = true; } void mogltk::texture::Taintall(void) { @@ -298,3 +333,8 @@ SDL_Surface * mogltk::texture::LoadNTEX(Handle * h) throw (GeneralException) { return r; } + +void mogltk::texture::DumpBMP(const String & n) { + if (surface) + SDL_SaveBMP(surface, n.to_charp()); +} -- cgit v1.2.3