C:/Documents and Settings/mtauraso/Desktop/proj3/projectX/Screen.cpp

Go to the documentation of this file.
00001 #include "Screen.h"
00002 #include "Object.h"
00003 #include "CommandManager.h"
00004 
00005 #include <iostream>
00006 
00007 using namespace std;
00008 
00009 Screen* Screen::_instance = NULL;
00010 
00011 Screen::Screen()
00012 :_followMe(NULL)
00013 {
00014         _screen.h = HEIGHT;
00015         _screen.w = WIDTH;
00016         _screen.x = 0;
00017         _screen.y = 0;
00018 
00019         _center.h = HEIGHT/3.0;
00020         _center.w = WIDTH/3.0;
00021         _center.x = WIDTH/3.0;
00022         _center.y = HEIGHT/3.0;
00023 
00024         #if defined(_WIN32)
00025         char *gl_library = "OpenGL32.DLL";
00026 #elif defined(__QNXNTO__)
00027         char *gl_library = "libGL.so.3";
00028 #else
00029         char *gl_library = "libGL.so.1";
00030 #endif
00031         
00032         
00033         if (SDL_Init(SDL_INIT_VIDEO)<0)
00034         {
00035                 printf("Unable to init SDL : %s\n",SDL_GetError());
00036                 exit(1);
00037         }
00038 
00039         atexit(SDL_Quit);
00040         
00041         if (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1)<0)
00042         {
00043                 printf("Unable to set GL attribute : %s\n",SDL_GetError());
00044                 exit(1);
00045         }
00046         
00047         if (SDL_GL_LoadLibrary(gl_library)<0)
00048         {
00049                 printf("Unable to dynamically open GL lib : %s\n",SDL_GetError());
00050                 exit(1);
00051         }
00052 
00053         SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
00054         SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
00055         SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
00056         SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
00057         SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 32);
00058 
00059         if (SDL_SetVideoMode(_screen.w,_screen.h,32,SDL_OPENGL)==NULL)
00060         {
00061                 printf("Unable to open video mode : %s\n",SDL_GetError());
00062                 exit(1);
00063         }
00064 
00065         /* Set the window manager title bar */
00066         SDL_WM_SetCaption( "SDL Dynamic OpenGL Loading Test", "testdyngl" );
00067 
00068         glfuncs::instance()->glViewport(0,0,_screen.w,_screen.h);
00069         glfuncs::instance()->glMatrixMode(GL_PROJECTION);
00070         glfuncs::instance()->glLoadIdentity();
00071 
00072         glfuncs::instance()->glOrtho(0.0f, _screen.w, _screen.h, 0.0f, -1.0f, 1.0f);
00073                 
00074         glfuncs::instance()->glMatrixMode(GL_MODELVIEW);
00075         glfuncs::instance()->glLoadIdentity();
00076 
00077         glfuncs::instance()->glClearColor(0.0f,0.0f,0.0f,0.5f);
00078         glfuncs::instance()->glClearDepth(1.0f);
00079         glfuncs::instance()->glDepthFunc(GL_LEQUAL);    
00080         glfuncs::instance()->glEnable(GL_DEPTH_TEST);
00081         glfuncs::instance()->glShadeModel(GL_SMOOTH);
00082         glfuncs::instance()->glDisable(GL_CULL_FACE);
00083         glfuncs::instance()->glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
00084 
00085         glfuncs::instance()->glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
00086         glfuncs::instance()->glEnable( GL_BLEND );
00087 
00088         glfuncs::instance()->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00089         glfuncs::instance()->glEnable(GL_TEXTURE_2D);
00090 }
00091 
00092 Screen* Screen::instance()
00093 {
00094         if(!_instance)
00095                 _instance = new Screen();
00096         return _instance;
00097 }
00098 
00099 void Screen::drawLevel(int texture, GLfloat texCoords[4], SDL_Rect quad)
00100 {
00101         if(glfuncs::intersect(quad,_screen)) {
00102                 int x1 = quad.x - _screen.x;
00103                 int x2 = x1 + quad.w;
00104                 int y1 = quad.y - _screen.y;
00105                 int y2 = y1 + quad.h;
00106 
00107                 glfuncs::instance()->glBindTexture(GL_TEXTURE_2D, texture);
00108                 glfuncs::instance()->glBegin(GL_QUADS);
00109                 glfuncs::instance()->glTexCoord2f(texCoords[0],texCoords[1]);   glfuncs::instance()->glVertex2f(x1,y1);
00110                 glfuncs::instance()->glTexCoord2f(texCoords[2],texCoords[1]);   glfuncs::instance()->glVertex2f(x2,y1);
00111                 glfuncs::instance()->glTexCoord2f(texCoords[2],texCoords[3]);   glfuncs::instance()->glVertex2f(x2,y2);
00112                 glfuncs::instance()->glTexCoord2f(texCoords[0],texCoords[3]);   glfuncs::instance()->glVertex2f(x1,y2);
00113                 glfuncs::instance()->glEnd();
00114         }
00115 }
00116 
00117 void Screen::drawScreen(int texture, GLfloat texCoords[4], SDL_Rect quad)
00118 {
00119         int x1 = quad.x;
00120         int x2 = x1 + quad.w;
00121         int y1 = quad.y;
00122         int y2 = y1 + quad.h;
00123 
00124         glfuncs::instance()->glBindTexture(GL_TEXTURE_2D, texture);
00125         glfuncs::instance()->glBegin(GL_QUADS);
00126         glfuncs::instance()->glTexCoord2f(texCoords[0],texCoords[1]);   glfuncs::instance()->glVertex2f(x1,y1);
00127         glfuncs::instance()->glTexCoord2f(texCoords[2],texCoords[1]);   glfuncs::instance()->glVertex2f(x2,y1);
00128         glfuncs::instance()->glTexCoord2f(texCoords[2],texCoords[3]);   glfuncs::instance()->glVertex2f(x2,y2);
00129         glfuncs::instance()->glTexCoord2f(texCoords[0],texCoords[3]);   glfuncs::instance()->glVertex2f(x1,y2);
00130         glfuncs::instance()->glEnd();
00131 }
00132 
00133 void Screen::drawCutScene(std::string cutSceneText)
00134 {
00135         glfuncs::instance()->glDisable(GL_TEXTURE_2D);
00136 
00137 
00138         glfuncs::instance()->glColor4f(0,0,0,1);
00139         
00140         glfuncs::instance()->glBegin(GL_QUADS);
00141         glfuncs::instance()->glVertex2f(0,0);
00142         glfuncs::instance()->glVertex2f(_screen.w,0);
00143         glfuncs::instance()->glVertex2f(_screen.w,_screen.h/CSFRAC);
00144         glfuncs::instance()->glVertex2f(0,_screen.h/CSFRAC);
00145         glfuncs::instance()->glEnd();
00146 
00147         glfuncs::instance()->glBegin(GL_QUADS);
00148         glfuncs::instance()->glVertex2f(0,_screen.h-_screen.h/CSFRAC);
00149         glfuncs::instance()->glVertex2f(_screen.w,_screen.h-_screen.h/CSFRAC);
00150         glfuncs::instance()->glVertex2f(_screen.w,_screen.h);
00151         glfuncs::instance()->glVertex2f(0,_screen.h);
00152         glfuncs::instance()->glEnd();
00153 
00154         glfuncs::instance()->glColor4f(1,1,1,1);
00155 
00156         glfuncs::instance()->glRasterPos2f(100,_screen.h-_screen.h/CSFRAC + 20);
00157         glfuncs::instance()->glPrint(cutSceneText);
00158 
00159         glfuncs::instance()->glEnable(GL_TEXTURE_2D);
00160 }
00161 
00162 void Screen::registerFollowObj(Object * o)
00163 {
00164         _followMe = o;
00165 }
00166 
00167 void Screen::move(int x, int y)
00168 {
00169         _screen.y += y;
00170         _screen.x += x;
00171         _center.y += y;
00172         _center.x += x;
00173 }
00174 
00175 
00176 void Screen::update()
00177 {
00178         if( _followMe == NULL )
00179                 return;
00180 
00181         SDL_Rect follow = _followMe->getBox();
00182 
00183         int x,y;
00184         if(!glfuncs::intersectX(follow,_center,&x))
00185                 move(x,0);
00186         if(!glfuncs::intersectY(follow,_center,&y))
00187                 move(0,y);
00188 }
00189 

Generated on Sat Apr 22 15:05:20 2006 for ProjectX by  doxygen 1.4.6-NO