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

Go to the documentation of this file.
00001 #include "glfuncs.h"
00002 #include <iostream>
00003 
00004 using namespace std;
00005 
00006 glfuncs* glfuncs::_instance = NULL;
00007 
00008 glfuncs::glfuncs()
00009 {
00010         init_glfuncs();
00011         base = buildFont();
00012 }
00013 
00014 glfuncs* glfuncs::instance()
00015 {
00016         if(!_instance)
00017                 _instance = new glfuncs();
00018         return _instance;
00019 }
00020 
00021 void* glfuncs::get_funcaddr(const char* p)
00022 {
00023         void* f=SDL_GL_GetProcAddress(p);
00024         if (f)
00025         {
00026                 return f;
00027         }
00028         else
00029         {
00030                 printf("Unable to get function pointer for %s\n",p);
00031                 exit(1);
00032         }
00033 }
00034 
00035 void glfuncs::init_glfuncs()
00036 {
00037         glBegin=(void(APIENTRY*)(GLenum))get_funcaddr("glBegin");
00038         glEnd=(void(APIENTRY*)())get_funcaddr("glEnd");
00039         glVertex3f=(void(APIENTRY*)(GLfloat, GLfloat, GLfloat))get_funcaddr("glVertex3f");
00040         glClearColor=(void(APIENTRY*)(GLfloat, GLfloat, GLfloat, GLfloat))get_funcaddr("glClearColor");
00041         glClear=(void(APIENTRY*)(GLbitfield))get_funcaddr("glClear");
00042         glDisable=(void(APIENTRY*)(GLenum))get_funcaddr("glDisable");
00043         glEnable=(void(APIENTRY*)(GLenum))get_funcaddr("glEnable");
00044         glColor4ub=(void(APIENTRY*)(GLubyte,GLubyte,GLubyte,GLubyte))get_funcaddr("glColor4ub");
00045         glPointSize=(void(APIENTRY*)(GLfloat))get_funcaddr("glPointSize");
00046         glHint=(void(APIENTRY*)(GLenum,GLenum))get_funcaddr("glHint");
00047         glBlendFunc=(void(APIENTRY*)(GLenum,GLenum))get_funcaddr("glBlendFunc");
00048         glMatrixMode=(void(APIENTRY*)(GLenum))get_funcaddr("glMatrixMode");
00049         glLoadIdentity=(void(APIENTRY*)())get_funcaddr("glLoadIdentity");
00050         glOrtho=(void(APIENTRY*)(GLdouble,GLdouble,GLdouble,GLdouble,GLdouble,GLdouble))get_funcaddr("glOrtho");
00051         glRotatef=(void(APIENTRY*)(GLfloat,GLfloat,GLfloat,GLfloat))get_funcaddr("glRotatef");
00052         glViewport=(void(APIENTRY*)(GLint,GLint,GLsizei,GLsizei))get_funcaddr("glViewport");
00053         glFogf=(void(APIENTRY*)(GLenum,GLfloat))get_funcaddr("glFogf");
00054         glGenTextures=(void(APIENTRY*)(GLsizei,GLuint*))get_funcaddr("glGenTextures");
00055         glBindTexture=(void(APIENTRY*)(GLenum,GLuint))get_funcaddr("glBindTexture");
00056         glTexParameteri=(void(APIENTRY*)(GLenum,GLenum,GLint))get_funcaddr("glTexParameteri");
00057         glTexImage2D=(void(APIENTRY*)(GLenum,GLint,GLint,GLsizei,GLsizei,GLint,GLenum,GLenum,const GLvoid *))get_funcaddr("glTexImage2D");
00058         glColor3f=(void(APIENTRY*)(GLfloat,GLfloat,GLfloat))get_funcaddr("glColor3f");
00059         glVertex2f=(void(APIENTRY*)(GLfloat,GLfloat))get_funcaddr("glVertex2f");
00060         glTexCoord2f=(void(APIENTRY*)(GLfloat,GLfloat))get_funcaddr("glTexCoord2f");
00061         glTexEnvf=(void(APIENTRY*)(GLenum,GLenum,GLfloat))get_funcaddr("glTexEnvf");
00062         glColor4f=(void(APIENTRY*)(GLfloat,GLfloat,GLfloat,GLfloat))get_funcaddr("glColor4f");
00063         glDepthMask=(void(APIENTRY*)(GLboolean))get_funcaddr("glDepthMask");
00064         glClearDepth=(void(APIENTRY*)(GLclampd))get_funcaddr("glClearDepth");
00065         glDepthFunc=(void(APIENTRY*)(GLenum))get_funcaddr("glDepthFunc");
00066         glShadeModel=(void(APIENTRY*)(GLenum))get_funcaddr("glShadeModel");
00067         glFinish=(void(APIENTRY*)())get_funcaddr("glFinish");
00068 
00069         glGenLists=(GLuint(APIENTRY*)(GLsizei))get_funcaddr("glGenLists");
00070         glDeleteLists=(void(APIENTRY*)(GLuint,GLsizei))get_funcaddr("glDeleteLists");
00071         glPopAttrib=(void(APIENTRY*)())get_funcaddr("glPopAttrib");
00072         glCallLists=(void(APIENTRY*)(GLsizei,GLenum,const GLvoid*))get_funcaddr("glCallLists");
00073         glListBase=(void(APIENTRY*)(GLuint))get_funcaddr("glListBase");
00074         glPushAttrib=(void(APIENTRY*)(GLbitfield))get_funcaddr("glPushAttrib");
00075 
00076         glRasterPos2f=(void(APIENTRY*)(GLfloat,GLfloat))get_funcaddr("glRasterPos2f");
00077         glGetBooleanv=(void(APIENTRY*)(GLenum, GLboolean *))get_funcaddr("glGetBooleanv");
00078 
00079         wglUseFontBitmapsA=(void(APIENTRY*)(HDC,long,long,long))get_funcaddr("wglUseFontBitmapsA");
00080 }
00081 
00082 /* Quick utility function for texture creation */
00083 int glfuncs::power_of_two(int input)
00084 {
00085     int value = 1;
00086 
00087     while ( value < input ) {
00088         value <<= 1;
00089     }
00090     return value;
00091 }
00092 
00093 GLuint glfuncs::SDL_GL_LoadTexture(SDL_Surface *surface, GLfloat *texcoord)
00094 {
00095     GLuint texture;
00096     int w, h;
00097     SDL_Surface *image;
00098     SDL_Rect area;
00099     Uint32 saved_flags;
00100     Uint8  saved_alpha;
00101 
00102     /* Use the surface width and height expanded to powers of 2 */
00103     w = power_of_two(surface->w);
00104     h = power_of_two(surface->h);
00105     texcoord[0] = 0.0f;         /* Min X */
00106     texcoord[1] = 0.0f;         /* Min Y */
00107     texcoord[2] = (GLfloat)surface->w / w;  /* Max X */
00108     texcoord[3] = (GLfloat)surface->h / h;  /* Max Y */
00109 
00110     image = SDL_CreateRGBSurface(
00111             SDL_SWSURFACE,
00112             w, h,
00113             32,
00114 #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
00115             0x000000FF,
00116             0x0000FF00,
00117             0x00FF0000,
00118             0xFF000000
00119 #else
00120             0xFF000000,
00121             0x00FF0000,
00122             0x0000FF00,
00123             0x000000FF
00124 #endif
00125                );
00126     if ( image == NULL ) {
00127         return 0;
00128     }
00129 
00130     /* Save the alpha blending attributes */
00131     saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
00132     saved_alpha = surface->format->alpha;
00133     if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
00134         SDL_SetAlpha(surface, 0, 0);
00135     }
00136 
00137     /* Copy the surface into the GL texture image */
00138     area.x = 0;
00139     area.y = 0;
00140     area.w = surface->w;
00141     area.h = surface->h;
00142     SDL_BlitSurface(surface, &area, image, &area);
00143 
00144     /* Restore the alpha blending attributes */
00145     if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
00146         SDL_SetAlpha(surface, saved_flags, saved_alpha);
00147     }
00148 
00149     /* Create an OpenGL texture for the image */
00150     glGenTextures(1, &texture);
00151     glBindTexture(GL_TEXTURE_2D, texture);
00152     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
00153     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
00154     glTexImage2D(GL_TEXTURE_2D,
00155              0,
00156              GL_RGBA,
00157              w, h,
00158              0,
00159              GL_RGBA,
00160              GL_UNSIGNED_BYTE,
00161              image->pixels);
00162     SDL_FreeSurface(image); /* No longer needed */
00163 
00164     return texture;
00165 }
00166 
00167 SDL_Surface *glfuncs::load_image( std::string filename ) 
00168 { 
00169         //The image that's loaded 
00170         SDL_Surface* loadedImage = NULL; 
00171         
00172         //The optimized image that will be used 
00173         SDL_Surface* optimizedImage = NULL; 
00174         
00175         //Load the image using SDL_image 
00176         loadedImage = IMG_Load( filename.c_str() ); 
00177         
00178         //If the image loaded 
00179         if( loadedImage != NULL ) 
00180         { 
00181                 //Create an optimized image 
00182                 optimizedImage = SDL_DisplayFormatAlpha( loadedImage ); 
00183                 //Free the old image 
00184                 SDL_FreeSurface( loadedImage ); 
00185         } 
00186         
00187         //Return the optimized image 
00188         return optimizedImage; 
00189 }
00190 
00191 /* Taken from SDL Tutorial Lesson 13
00192  * http://lazyfooproductions.com/SDL_tutorials/lesson13/index.php
00193  * 
00194  * Highly modified
00195  */
00196 
00197 bool glfuncs::intersect(SDL_Rect &A, SDL_Rect &B, int* x , int* y)
00198 {
00199         return intersectX(A,B,x) && intersectY(A,B,y);
00200 }
00201 
00202 bool glfuncs::intersectX(SDL_Rect &A, SDL_Rect &B, int* x)
00203 {
00204         bool returnme;
00205         //The sides of the rectangles
00206     int leftA, leftB;
00207     int rightA, rightB;
00208         
00209         //Calculate the sides of rect A
00210     leftA = A.x;
00211     rightA = A.x + A.w;
00212 
00213         //Calculate the sides of rect B
00214     leftB = B.x;
00215     rightB = B.x + B.w;
00216 
00217         //If any of the sides from A are outside of B
00218     if( rightA <= leftB || leftA >= rightB )
00219                 returnme = false;
00220         else 
00221                 returnme = true;
00222 
00223         if(x != NULL)
00224         {
00225                 int centerAx = A.x + A.w/2;
00226                 int centerBx = B.x + B.w/2;
00227 
00228                 // this corresponds to the direction 
00229                 // b must be moved
00230                 bool left;
00231 
00232                 if(returnme)
00233                         left = (centerAx >= centerBx);
00234                 else
00235                         left = (centerAx <= centerBx);
00236                 
00237                 //this is the amount b needs to be moved
00238                 *x = min(abs(leftB-rightA),abs(leftA-rightB));
00239                 
00240                 // make sure the sign is right
00241                 if(left)
00242                         *x = -(*x);
00243         }
00244         return returnme;
00245 
00246 }
00247 bool glfuncs::intersectY(SDL_Rect &A, SDL_Rect &B, int* y)
00248 {
00249         bool returnme;
00250         //The sides of the rectangles
00251         int topA, topB;
00252     int bottomA, bottomB;
00253 
00254         //Calculate the sides of rect A
00255         topA = A.y;
00256     bottomA = A.y + A.h;
00257         
00258     //Calculate the sides of rect B
00259     topB = B.y;
00260     bottomB = B.y + B.h;
00261 
00262 
00263         //If any of the sides from A are outside of B
00264     if( bottomA <= topB || topA >= bottomB )
00265                 returnme = false;
00266         else 
00267                 returnme = true;
00268 
00269         if(y != NULL)
00270         {
00271                 
00272                 int centerAy = A.y + A.h/2;
00273                 int centerBy = B.y + B.h/2;
00274 
00275                 // this corresponds to the direction
00276                 // b must be moved
00277                 bool up;
00278                 
00279                 if(returnme)
00280                         up = (centerAy >= centerBy);
00281                 else
00282                         up = (centerAy <= centerBy);
00283 
00284                 //this is the amount b needs to be moved
00285                 *y = min(abs(bottomB-topA),abs(bottomA-topB));
00286                 
00287                 // make sure the sign is right
00288                 if(up)
00289                         *y = -(*y);
00290 
00291                 
00292         }
00293 
00294         
00295 
00296         return returnme;
00297 }
00298 
00299 /* Taken from http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=13 */
00300 GLuint glfuncs::buildFont(GLvoid)                                                               // Build Our Bitmap Font
00301 {
00302         SDL_SysWMinfo info;
00303         SDL_VERSION(&info.version);
00304         SDL_GetWMInfo(&info);
00305 
00306         HWND hWnd = info.window;
00307 
00308         HDC hDC = GetDC(hWnd); 
00309 
00310         HFONT   font;                                                                           // Windows Font ID
00311         HFONT   oldfont;                                                                        // Used For Good House Keeping
00312 
00313         GLuint base = glGenLists(96);                           // Storage For 96 Characters
00314 
00315         font = CreateFont(      -24,                                                    // Height Of Font
00316                                                 0,                                                              // Width Of Font
00317                                                 0,                                                              // Angle Of Escapement
00318                                                 0,                                                              // Orientation Angle
00319                                                 FW_BOLD,                                                // Font Weight
00320                                                 FALSE,                                                  // Italic
00321                                                 FALSE,                                                  // Underline
00322                                                 FALSE,                                                  // Strikeout
00323                                                 ANSI_CHARSET,                                   // Character Set Identifier
00324                                                 OUT_TT_PRECIS,                                  // Output Precision
00325                                                 CLIP_DEFAULT_PRECIS,                    // Clipping Precision
00326                                                 ANTIALIASED_QUALITY,                    // Output Quality
00327                                                 FF_DONTCARE|DEFAULT_PITCH,              // Family And Pitch
00328                                                 "Courier New");                                 // Font Name
00329 
00330         oldfont = (HFONT)SelectObject(hDC, font);           // Selects The Font We Want
00331         wglUseFontBitmaps(hDC, 32, 96, base);                           // Builds 96 Characters Starting At Character 32
00332         SelectObject(hDC, oldfont);                                                     // Selects The Font We Want
00333         DeleteObject(font);                                                                     // Delete The Font
00334 
00335         return base;
00336 }
00337 
00338 /* Taken from http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=13 */
00339 GLvoid glfuncs::killFont()                                                                      // Delete The Font List
00340 {
00341         glDeleteLists(base, 96);                                                        // Delete All 96 Characters
00342 }
00343 
00344 /* Taken from http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=13 */
00345 GLvoid glfuncs::glPrint(std::string text)                                       // Custom GL "Print" Routine
00346 {                                                       // Holds Our String
00347 
00348         if (text == "")                                                                 // If There's No Text
00349                 return;                                                                                 // Do Nothing
00350 
00351         glPushAttrib(GL_LIST_BIT);                                                      // Pushes The Display List Bits
00352         glListBase(base - 32);                                                          // Sets The Base Character to 32
00353         glCallLists(strlen(text.c_str()), GL_UNSIGNED_BYTE, text.c_str());      // Draws The Display List Text
00354         glPopAttrib();                                                                          // Pops The Display List Bits
00355 }
00356 
00357 
00358 int glfuncs::loadTexture(std::string filename, GLfloat coords[4], SDL_Rect & rect)
00359 {
00360         SDL_Surface *surface = load_image(filename);
00361         rect.x = 0;
00362     rect.y = 0;
00363     rect.w = surface->w;
00364     rect.h = surface->h;
00365         int texture = SDL_GL_LoadTexture(surface,coords);
00366         SDL_FreeSurface(surface);
00367         return texture;
00368 }

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