From dacb774ae01102f756e92449ef395fb938a2acf0 Mon Sep 17 00:00:00 2001 From: Simon Robertshaw Date: Mon, 25 Jun 2012 16:10:40 +0100 Subject: [PATCH] Resizable window when using OpenGL interface --- src/Graphics.h | 1 + src/OpenGLGraphics.cpp | 30 +++++++++++++++----------- src/PowderToySDL.cpp | 38 +++++++++++++++++++++++++++------ src/simulation/SaveRenderer.cpp | 3 ++- 4 files changed, 51 insertions(+), 21 deletions(-) diff --git a/src/Graphics.h b/src/Graphics.h index e4c2ad8d2..ef72bf050 100644 --- a/src/Graphics.h +++ b/src/Graphics.h @@ -107,6 +107,7 @@ public: #ifdef OGLI //OpenGL specific instance variables GLuint vidBuf, textTexture; + void Reset(); #endif //Common graphics methods in Graphics.cpp diff --git a/src/OpenGLGraphics.cpp b/src/OpenGLGraphics.cpp index eb242ce3b..d688639ff 100644 --- a/src/OpenGLGraphics.cpp +++ b/src/OpenGLGraphics.cpp @@ -6,19 +6,7 @@ Graphics::Graphics(): sdl_scale(1) { - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - - //glOrtho(0, (XRES+BARSIZE)*sdl_scale, 0, (YRES+MENUSIZE)*sdl_scale, -1, 1); - glOrtho(0, (XRES+BARSIZE)*sdl_scale, (YRES+MENUSIZE)*sdl_scale, 0, -1, 1); - - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - - - //glRasterPos2i(0, (YRES+MENUSIZE)); - glRasterPos2i(0, 0); - glPixelZoom(1, 1); + Reset(); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); @@ -51,6 +39,22 @@ Graphics::~Graphics() { } +void Graphics::Reset() +{ + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + + //glOrtho(0, (XRES+BARSIZE)*sdl_scale, 0, (YRES+MENUSIZE)*sdl_scale, -1, 1); + glOrtho(0, (XRES+BARSIZE)*sdl_scale, (YRES+MENUSIZE)*sdl_scale, 0, -1, 1); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + + //glRasterPos2i(0, (YRES+MENUSIZE)); + glRasterPos2i(0, 0); + glPixelZoom(1, 1); +} + void Graphics::Clear() { glClearColor(0.0f, 0.0f, 0.0f, 1.0f); diff --git a/src/PowderToySDL.cpp b/src/PowderToySDL.cpp index 079986a67..afc385dad 100644 --- a/src/PowderToySDL.cpp +++ b/src/PowderToySDL.cpp @@ -113,7 +113,7 @@ SDL_Surface * SDLOpen() #ifndef OGLI surface = SDL_SetVideoMode(XRES + BARSIZE, YRES + MENUSIZE, 32, SDL_SWSURFACE); #else - surface = SDL_SetVideoMode(XRES + BARSIZE, YRES + MENUSIZE, 32, SDL_OPENGL); + surface = SDL_SetVideoMode((XRES + BARSIZE), (YRES + MENUSIZE), 32, SDL_OPENGL | SDL_RESIZABLE); #endif #if defined(OGLI) @@ -132,11 +132,14 @@ int main(int argc, char * argv[]) { int elapsedTime = 0, currentTime = 0, lastTime = 0, currentFrame = 0; unsigned int lastTick = 0; - float fps = 0, delta = 1.0f; + float fps = 0, delta = 1.0f, inputScale = 1.0f; + float currentWidth = XRES+BARSIZE, currentHeight = YRES+MENUSIZE; sdl_scrn = SDLOpen(); #ifdef OGLI SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1); + //glScaled(2.0f, 2.0f, 1.0f); + #endif ui::Engine::Ref().g = new Graphics(); @@ -168,26 +171,47 @@ int main(int argc, char * argv[]) engine->onKeyRelease(event.key.keysym.sym, event.key.keysym.unicode, event.key.keysym.mod&KEY_MOD_SHIFT, event.key.keysym.mod&KEY_MOD_CONTROL, event.key.keysym.mod&KEY_MOD_ALT); break; case SDL_MOUSEMOTION: - engine->onMouseMove(event.motion.x, event.motion.y); + engine->onMouseMove(event.motion.x*inputScale, event.motion.y*inputScale); break; case SDL_MOUSEBUTTONDOWN: if(event.button.button == SDL_BUTTON_WHEELUP) { - engine->onMouseWheel(event.motion.x, event.motion.y, 1); + engine->onMouseWheel(event.motion.x*inputScale, event.motion.y*inputScale, 1); } else if (event.button.button == SDL_BUTTON_WHEELDOWN) { - engine->onMouseWheel(event.motion.x, event.motion.y, -1); + engine->onMouseWheel(event.motion.x*inputScale, event.motion.y*inputScale, -1); } else { - engine->onMouseClick(event.motion.x, event.motion.y, event.button.button); + engine->onMouseClick(event.motion.x*inputScale, event.motion.y*inputScale, event.button.button); } break; case SDL_MOUSEBUTTONUP: if(event.button.button != SDL_BUTTON_WHEELUP && event.button.button != SDL_BUTTON_WHEELDOWN) - engine->onMouseUnclick(event.motion.x, event.motion.y, event.button.button); + engine->onMouseUnclick(event.motion.x*inputScale, event.motion.y*inputScale, event.button.button); break; +#ifdef OGLI + case SDL_VIDEORESIZE: + float ratio = float(XRES+BARSIZE) / float(YRES+MENUSIZE); + float width = event.resize.w; + float height = width/ratio; + + sdl_scrn = SDL_SetVideoMode(event.resize.w, height, 32, SDL_OPENGL | SDL_RESIZABLE); + + glViewport(0, 0, width, height); + engine->g->Reset(); + //glScaled(width/currentWidth, height/currentHeight, 1.0f); + + currentWidth = width; + currentHeight = height; + inputScale = float(XRES+BARSIZE)/currentWidth; + if(sdl_scrn == NULL) + { + std::cerr << "Oh bugger" << std::endl; + } + break; +#endif } event.type = 0; //Clear last event } diff --git a/src/simulation/SaveRenderer.cpp b/src/simulation/SaveRenderer.cpp index 79dd4217b..197d4cf76 100644 --- a/src/simulation/SaveRenderer.cpp +++ b/src/simulation/SaveRenderer.cpp @@ -12,13 +12,14 @@ SaveRenderer::SaveRenderer(){ - g = new Graphics(); + //g = new Graphics(); sim = new Simulation(); ren = new Renderer(g, sim); } Thumbnail * SaveRenderer::Render(GameSave * save) { + return NULL; int width, height; Thumbnail * tempThumb; #ifdef OGLR