Window Resizeable
authorIvan Hernanez <iturtleman128@gmail.com>
Mon, 28 May 2012 04:57:10 +0000 (23:57 -0500)
committerIvan Hernanez <iturtleman128@gmail.com>
Mon, 28 May 2012 04:57:10 +0000 (23:57 -0500)
Game.cpp
Level.cpp

index d793d46..2d30670 100644 (file)
--- a/Game.cpp
+++ b/Game.cpp
@@ -2,7 +2,7 @@
 #include "fns.h"
 #include <string>
 #include <iostream>
-#include <GL/gl.h>
+#include <SDL/SDL_opengl.h>
 #include "SDL_ttf.h"
 
 const Uint32 Game::waitTime = 1000/60; /* ms */
@@ -28,13 +28,26 @@ void init_GL(int w = 640, int h = 480)
        glLoadIdentity();
 }
 
+void Reshape( int w, int h )
+{
+       if( h <= 0 ) h = 1 ;
+
+       glViewport( 0, 0, w, h ) ;
+
+       glMatrixMode( GL_PROJECTION ) ;
+       glLoadIdentity() ;
+       gluPerspective( 45.0, (GLfloat)w / (GLfloat)h, 0.1, 1000.0 ) ;
+
+       glMatrixMode( GL_MODELVIEW ) ;
+       glLoadIdentity() ;
+}
 
 /** Global static pointer used to ensure a single instance of the class. */
 Game* Game::m_instance = NULL;
 
 /** This function is called to create an instance of the class.
-       Calling the constructor publicly is not allowed.
-       The constructor is private and is only called by this Instance function.
+Calling the constructor publicly is not allowed.
+The constructor is private and is only called by this Instance function.
 */
 Game* Game::game(){
        if(!m_instance)
@@ -54,15 +67,21 @@ Game::Game() : mCurrentLevel(0), mScreen(0), ShowCollisions(false), ShowFPS(fals
        }
        atexit (SDL_Quit);
        // Initialize SDL_ttf library
-   if (TTF_Init() != 0)
-   {
-      cerr << "TTF_Init() Failed: " << TTF_GetError() << endl;
-      SDL_Quit();
-      exit(1);
-   }
+       if (TTF_Init() != 0)
+       {
+               cerr << "TTF_Init() Failed: " << TTF_GetError() << endl;
+               SDL_Quit();
+               exit(1);
+       }
        atexit (TTF_Quit);
+
+       /* Turn on double buffering with a 24bit Z buffer.
+       * You may need to change this to 16 or 32 for your system */
+       SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
+       SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
+
        /** Set 800x600 video mode */
-       mScreen = SDL_SetVideoMode(screenwidth, screenheight, 32, SDL_OPENGL);
+       mScreen = SDL_SetVideoMode(screenwidth, screenheight, 32, SDL_OPENGL | SDL_RESIZABLE);
        if (!mScreen) {
                printf ("Couldn't set video mode: %s\n", SDL_GetError ());
                exit (2);
@@ -72,12 +91,12 @@ Game::Game() : mCurrentLevel(0), mScreen(0), ShowCollisions(false), ShowFPS(fals
        SDL_WM_SetCaption("Ivan's Game", NULL);
 
        SDL_ShowCursor(0);
-       
+
        init_GL(screenwidth, screenheight);
 
        /** Load Resources */
        LoadResources();
-       
+
        // set start time for fps calc
        startclock = SDL_GetTicks();
 }
@@ -150,6 +169,10 @@ void Game::run()
                        done = 1;
                        break;
 
+               case SDL_VIDEORESIZE :
+                       Reshape( event.resize.w, event.resize.h ) ;
+                       break ;
+
                case SDL_USEREVENT:
                        getCurrentLevel()->drawScene();
                        break;
index 3fe9e7e..5d7eca3 100644 (file)
--- a/Level.cpp
+++ b/Level.cpp
@@ -48,6 +48,8 @@ void Level::DrawIMG(SDL_Surface *img, int x, int y, int w, int h, int x2, int y2
 
 void Level::drawScene()
 {
+
+       glLoadIdentity();
        Uint32 color;
 
        /** Create a black mBackgroundground using the mScreen pixel format (32 bpp) */