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