C:/Documents and Settings/jegan/Desktop/projectX/Screen.cpp

Go to the documentation of this file.
00001 #include "Screen.h"
00002 #include "Object.h"
00003 #include "CommandManager.h"
00004 #include "Parser.h"
00005 
00006 #include <iostream>
00007 #include <sstream>
00008 
00009 using namespace std;
00010 
00011 int Screen::HEIGHT =768;
00012 int Screen::WIDTH = 1024;
00013 
00014 Screen* Screen::_instance = NULL;
00015 
00016 Screen::Screen()
00017 :_followMe(NULL)
00018 {
00019 
00020         
00021         
00022         _screen.h = HEIGHT;
00023         _screen.w = WIDTH;
00024         _screen.x = 0;
00025         _screen.y = 0;
00026 
00027 
00028         _center.h = HEIGHT/3.0;
00029         _center.w = WIDTH/5.0;
00030         _center.x = 2.0*WIDTH/5.0;
00031         _center.y = HEIGHT/3.0;
00032 
00033         #if defined(_WIN32)
00034         char *gl_library = "OpenGL32.DLL";
00035 #elif defined(__QNXNTO__)
00036         char *gl_library = "libGL.so.3";
00037 #else
00038         char *gl_library = "libGL.so.1";
00039 #endif
00040         
00041         
00042         if (SDL_Init(SDL_INIT_VIDEO)<0)
00043         {
00044                 printf("Unable to init SDL : %s\n",SDL_GetError());
00045                 exit(1);
00046         }
00047 
00048         atexit(SDL_Quit);
00049         
00050         if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1)<0)
00051         {
00052                 printf("Unable to set GL attribute : %s\n",SDL_GetError());
00053                 exit(1);
00054         }
00055         
00056         if (SDL_GL_LoadLibrary(gl_library)<0)
00057         {
00058                 printf("Unable to dynamically open GL lib : %s\n",SDL_GetError());
00059                 exit(1);
00060         }
00061 
00062         SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
00063         SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
00064         SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
00065         SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
00066         SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 32);
00067 
00068         if (SDL_SetVideoMode(_screen.w,_screen.h,32,SDL_OPENGL)==NULL)
00069         {
00070                 printf("Unable to open video mode : %s\n",SDL_GetError());
00071                 exit(1);
00072         }
00073 
00074         /* Set the window manager title bar */
00075         SDL_WM_SetCaption( "SDL Dynamic OpenGL Loading Test", "testdyngl" );
00076 
00077         glfuncs::instance()->glViewport(0,0,_screen.w,_screen.h);
00078         glfuncs::instance()->glMatrixMode(GL_PROJECTION);
00079         glfuncs::instance()->glLoadIdentity();
00080 
00081         glfuncs::instance()->glOrtho(0.0f, _screen.w, _screen.h, 0.0f, -1.0f, 1.0f);
00082                 
00083         glfuncs::instance()->glMatrixMode(GL_MODELVIEW);
00084         glfuncs::instance()->glLoadIdentity();
00085 
00086         glfuncs::instance()->glClearColor(0.0f,0.0f,0.0f,0.5f);
00087         glfuncs::instance()->glClearDepth(1.0f);
00088         glfuncs::instance()->glDepthFunc(GL_LEQUAL);    
00089         glfuncs::instance()->glEnable(GL_DEPTH_TEST);
00090         glfuncs::instance()->glShadeModel(GL_SMOOTH);
00091         glfuncs::instance()->glDisable(GL_CULL_FACE);
00092         glfuncs::instance()->glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
00093 
00094         glfuncs::instance()->glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
00095         glfuncs::instance()->glEnable( GL_BLEND );
00096 
00097         glfuncs::instance()->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00098         glfuncs::instance()->glEnable(GL_TEXTURE_2D);
00099 
00100         //team scramble logo
00101         SDL_Surface* scramSurface = glfuncs::instance()->load_image("scramble.png");
00102         _scram = glfuncs::instance()->SDL_GL_LoadTexture(scramSurface, _scramCoords);
00103         
00104 
00105         //pause screen
00106         SDL_Surface* pauseSurface = glfuncs::instance()->load_image("menus/pause.png");
00107         _pause = glfuncs::instance()->SDL_GL_LoadTexture(pauseSurface, _pauseCoords);
00108 
00109         //pause screen
00110         SDL_Surface* loadingSurface = glfuncs::instance()->load_image("menus/loading.png");
00111         _loading = glfuncs::instance()->SDL_GL_LoadTexture(loadingSurface, _loadingCoords);
00112 
00113         SDL_FreeSurface(scramSurface);
00114         SDL_FreeSurface(pauseSurface);
00115         SDL_FreeSurface(loadingSurface);
00116 }
00117 
00118 Screen* Screen::instance()
00119 {
00120         if(!_instance)
00121                 _instance = new Screen();
00122         return _instance;
00123 }
00124 
00125 void Screen::drawLevel(int texture, GLfloat texCoords[4], SDL_Rect quad)
00126 {
00127         if(glfuncs::intersect(quad,_screen)) {
00128                 int x1 = quad.x - _screen.x;
00129                 int x2 = x1 + quad.w;
00130                 int y1 = quad.y - _screen.y;
00131                 int y2 = y1 + quad.h;
00132 
00133                 glfuncs::instance()->glBindTexture(GL_TEXTURE_2D, texture);
00134                 glfuncs::instance()->glBegin(GL_QUADS);
00135                 glfuncs::instance()->glTexCoord2f(texCoords[0],texCoords[1]);   glfuncs::instance()->glVertex2f(x1,y1);
00136                 glfuncs::instance()->glTexCoord2f(texCoords[2],texCoords[1]);   glfuncs::instance()->glVertex2f(x2,y1);
00137                 glfuncs::instance()->glTexCoord2f(texCoords[2],texCoords[3]);   glfuncs::instance()->glVertex2f(x2,y2);
00138                 glfuncs::instance()->glTexCoord2f(texCoords[0],texCoords[3]);   glfuncs::instance()->glVertex2f(x1,y2);
00139                 glfuncs::instance()->glEnd();
00140         }
00141 }
00142 
00143 void Screen::drawScreen(int texture, GLfloat texCoords[4], SDL_Rect quad)
00144 {
00145         int x1 = quad.x;
00146         int x2 = x1 + quad.w;
00147         int y1 = quad.y;
00148         int y2 = y1 + quad.h;
00149 
00150         glfuncs::instance()->glBindTexture(GL_TEXTURE_2D, texture);
00151         glfuncs::instance()->glBegin(GL_QUADS);
00152         glfuncs::instance()->glTexCoord2f(texCoords[0],texCoords[1]);   glfuncs::instance()->glVertex2f(x1,y1);
00153         glfuncs::instance()->glTexCoord2f(texCoords[2],texCoords[1]);   glfuncs::instance()->glVertex2f(x2,y1);
00154         glfuncs::instance()->glTexCoord2f(texCoords[2],texCoords[3]);   glfuncs::instance()->glVertex2f(x2,y2);
00155         glfuncs::instance()->glTexCoord2f(texCoords[0],texCoords[3]);   glfuncs::instance()->glVertex2f(x1,y2);
00156         glfuncs::instance()->glEnd();
00157 }
00158 
00159 void Screen::drawScramble()
00160 {
00161         // Draw Team Scramble logo
00162                 SDL_Rect scramRect;
00163                 scramRect.w = 51;
00164                 scramRect.h = 81;
00165                 scramRect.x = 5;
00166                 scramRect.y = _instance->getScreenRect().h - 80;
00167                 _instance->drawScreen(_instance->_scram,_instance->_scramCoords,scramRect);
00168 }
00169 
00170 void Screen::drawPause()
00171 {
00172         // Draw Pause screen
00173                 SDL_Rect pauseRect;
00174                 pauseRect.w = WIDTH;
00175                 pauseRect.h = HEIGHT;
00176                 pauseRect.x = 0;
00177                 pauseRect.y = 0;
00178                 _instance->drawScreen(_instance->_pause,_instance->_pauseCoords,pauseRect);
00179 }
00180 
00181 void Screen::drawLoading()
00182 {
00183         glfuncs::instance()->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00184         
00185         // Draw Loading screen
00186                 SDL_Rect loadingRect;
00187                 loadingRect.w = WIDTH;
00188                 loadingRect.h = HEIGHT;
00189                 loadingRect.x = 0;
00190                 loadingRect.y = 0;
00191                 _instance->drawScreen(_instance->_loading,_instance->_loadingCoords,loadingRect);
00192 
00193                 drawScramble();
00194                 glfuncs::instance()->glFinish();
00195         SDL_GL_SwapBuffers();
00196 }
00197 
00198 void Screen::drawCutScene(std::string cutSceneText)
00199 {
00200         glfuncs::instance()->glDisable(GL_TEXTURE_2D);
00201 
00202 
00203         glfuncs::instance()->glColor4f(0,0,0,1);
00204         
00205         glfuncs::instance()->glBegin(GL_QUADS);
00206         glfuncs::instance()->glVertex2f(0,0);
00207         glfuncs::instance()->glVertex2f(_screen.w,0);
00208         glfuncs::instance()->glVertex2f(_screen.w,_screen.h/CSFRAC);
00209         glfuncs::instance()->glVertex2f(0,_screen.h/CSFRAC);
00210         glfuncs::instance()->glEnd();
00211 
00212         glfuncs::instance()->glBegin(GL_QUADS);
00213         glfuncs::instance()->glVertex2f(0,_screen.h-_screen.h/CSFRAC);
00214         glfuncs::instance()->glVertex2f(_screen.w,_screen.h-_screen.h/CSFRAC);
00215         glfuncs::instance()->glVertex2f(_screen.w,_screen.h);
00216         glfuncs::instance()->glVertex2f(0,_screen.h);
00217         glfuncs::instance()->glEnd();
00218 
00219         glfuncs::instance()->glEnable(GL_TEXTURE_2D);
00220 
00221         glfuncs::instance()->glColor4f(1,1,1,1);
00222 
00223         //glfuncs::instance()->glRasterPos2f(100,_screen.h-_screen.h/CSFRAC + 20);
00224 
00225 
00226         int yPos = _screen.h-_screen.h/CSFRAC+6;
00227 
00228         while(cutSceneText != "") {
00229                 glfuncs::instance()->glPrint(100, yPos, Parser::wordWrap(cutSceneText,60));
00230                 yPos += 32;
00231         }
00232 
00233         //glfuncs::instance()->glPrint(100,_screen.h-_screen.h/CSFRAC+6,cutSceneText);
00234 
00235 
00236 }
00237 
00238 void Screen::registerFollowObj(Object * o)
00239 {
00240         _followMe = o;
00241 }
00242 
00243 void Screen::move(int x, int y)
00244 {
00245         _screen.y += y;
00246         _screen.x += x;
00247         _center.y += y;
00248         _center.x += x;
00249 }
00250 
00251 void Screen::recenter()
00252 {
00253         SDL_Rect box = _followMe->getBox();
00254         int x = box.x + box.w/2 - WIDTH/2;
00255         int y = box.y + box.h/2 - HEIGHT/2;
00256         int moveX = x - _screen.x;
00257         int moveY = y - _screen.y;
00258         move(moveX,moveY);
00259 }
00260 
00261 void Screen::update()
00262 {
00263         if( _followMe == NULL )
00264                 return;
00265 
00266         SDL_Rect follow = _followMe->getBox();
00267 
00268         int x,y;
00269         if(!glfuncs::intersectX(follow,_center,&x))
00270                 move(x,0);
00271         if(!glfuncs::intersectY(follow,_center,&y))
00272                 move(0,y);
00273 }
00274 

Generated on Fri May 5 00:20:19 2006 for ProjectX by  doxygen 1.4.6-NO