1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
// -------------------------------------------
// SDL/OpenGL OpenGL application framework
// Jean-Luc PONS (2007)
// -------------------------------------------
#include "GLApp.h"
#ifndef WINDOWS
#include <unistd.h>
#endif
#undef LoadImage
#include <CImage.h>
extern char *LID(char *fileName);
// -------------------------------------------
GLApplication::GLApplication(const char * strWindowTitle) :
m_strWindowTitle(strWindowTitle)
{
m_bWindowed = true;
m_bVSync = false;
//m_strWindowTitle = (char *)"GL application";
strcpy((char *)m_strFrameStats,"");
m_screenWidth = 640;
m_screenHeight = 480;
}
// -------------------------------------------
int GLApplication::SetVideoMode() {
// Enable/Disable vertical blanking
// Work only at application startup
SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL,m_bVSync);
// Set the video mode
Uint32 flags;
if( m_bWindowed ) flags = SDL_DOUBLEBUF | SDL_OPENGL;
else flags = SDL_DOUBLEBUF | SDL_OPENGL | SDL_FULLSCREEN;
if( SDL_SetVideoMode( m_screenWidth, m_screenHeight, 0, flags ) == NULL )
{
#ifdef WINDOWS
char message[256];
sprintf(message,"SDL_SetVideoMode() failed : %s\n",SDL_GetError());
MessageBox(NULL,message,"ERROR",MB_OK|MB_ICONERROR);
#else
printf("SDL_SetVideoMode() failed : %s\n",SDL_GetError());
#endif
return GL_FAIL;
}
if( !m_bWindowed )
SDL_ShowCursor(SDL_DISABLE);
else
SDL_ShowCursor(SDL_ENABLE);
return GL_OK;
}
// -------------------------------------------
int GLApplication::ToggleFullscreen() {
int errCode;
InvalidateDeviceObjects();
m_bWindowed = !m_bWindowed;
if( !SetVideoMode() ) return GL_FAIL;
SDL_Surface *vSurf = SDL_GetVideoSurface();
m_bitsPerPixel = vSurf->format->BitsPerPixel;
errCode = RestoreDeviceObjects();
if( !errCode ) {
printGlError();
exit(1);
}
return GL_OK;
}
// -------------------------------------------
int GLApplication::Create(int width, int height, BOOL bFullScreen, BOOL bVSync ) {
int errCode;
m_bVSync = bVSync;
m_screenWidth = width;
m_screenHeight = height;
m_bWindowed = !bFullScreen;
#ifndef WINDOWS
if( getenv("DISPLAY")==NULL ) {
printf("Warning, DISPLAY not defined, it may not work.\n");
}
#endif
//Initialize SDL
if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
{
#ifdef WINDOWS
char message[256];
sprintf(message,"SDL_Init() failed : %s\n" , SDL_GetError() );
MessageBox(NULL,message,"Error",MB_OK|MB_ICONERROR);
#else
printf("SDL_Init() failed : %s\n" , SDL_GetError() );
#endif
return GL_FAIL;
}
SDL_WM_SetCaption(m_strWindowTitle, NULL);
//SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 0);
SDL_EnableUNICODE( 1 );
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY,SDL_DEFAULT_REPEAT_INTERVAL);
//Create Window
if( !SetVideoMode() ) return GL_FAIL;
SDL_Surface *vSurf = SDL_GetVideoSurface();
m_bitsPerPixel = vSurf->format->BitsPerPixel;
OneTimeSceneInit();
errCode = RestoreDeviceObjects();
if( !errCode ) {
printGlError();
exit(0);
}
return GL_OK;
}
// -------------------------------------------
void GLApplication::Pause(BOOL bPause) {
}
// -------------------------------------------
int GLApplication::Resize( DWORD width, DWORD height ) {
int errCode;
m_screenWidth = width;
m_screenHeight = height;
InvalidateDeviceObjects();
if( !SetVideoMode() ) return GL_FAIL;
SDL_Surface *vSurf = SDL_GetVideoSurface();
m_bitsPerPixel = vSurf->format->BitsPerPixel;
errCode = RestoreDeviceObjects();
if( !errCode ) {
printGlError();
exit(1);
}
return GL_OK;
}
// -------------------------------------------
int GLApplication::Run() {
SDL_Event event;
bool quit = false;
int nbFrame = 0;
int lastTick = 0;
int lastFrTick = 0;
int errCode;
int fTick;
int firstTick;
m_fTime = 0.0f;
m_fElapsedTime = 0.0f;
m_fFPS = 0.0f;
lastTick = lastFrTick = firstTick = SDL_GetTicks();
//Wait for user exit
while( quit == false )
{
//While there are events to handle
while( SDL_PollEvent( &event ) ) {
if( event.type == SDL_QUIT )
quit = true;
else
EventProc(&event);
}
fTick = SDL_GetTicks();
// Update timing
nbFrame++;
if( (fTick - lastTick) >= 1000 ) {
int t0 = fTick;
int t = t0 - lastTick;
m_fFPS = (float)(nbFrame*1000) / (float)t;
nbFrame = 0;
lastTick = t0;
sprintf(m_strFrameStats,"%.2f fps (%dx%dx%d)",m_fFPS,m_screenWidth,m_screenHeight,m_bitsPerPixel);
}
m_fTime = (float) ( fTick - firstTick ) / 1000.0f;
m_fElapsedTime = (fTick - lastFrTick) / 1000.0f;
lastFrTick = fTick;
if(!quit) errCode = FrameMove();
if( !errCode ) quit = true;
if( glGetError() != GL_NO_ERROR ) { printGlError(); quit = true; }
if(!quit) errCode = Render();
if( !errCode ) quit = true;
if( glGetError() != GL_NO_ERROR ) { printGlError(); quit = true; }
//Swap buffer
SDL_GL_SwapBuffers();
}
//Clean up
SDL_Quit();
return GL_OK;
}
// -------------------------------------------
void GLApplication::SetMaterial(GLMATERIAL *mat) {
float acolor[] = { mat->Ambient.r, mat->Ambient.g, mat->Ambient.b, mat->Ambient.a };
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, acolor);
float dcolor[] = { mat->Diffuse.r, mat->Diffuse.g, mat->Diffuse.b, mat->Diffuse.a };
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, dcolor);
float scolor[] = { mat->Specular.r, mat->Specular.g, mat->Specular.b, mat->Specular.a };
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, scolor);
float ecolor[] = { mat->Emissive.r, mat->Emissive.g, mat->Emissive.b, mat->Emissive.a };
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, ecolor);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, mat->Power);
glColor4f(mat->Ambient.r, mat->Ambient.g, mat->Ambient.b, mat->Ambient.a);
}
// -------------------------------------------
void GLApplication::printGlError() {
char message[256];
GLenum errCode = glGetError();
switch( errCode ) {
case GL_INVALID_ENUM:
sprintf(message,"OpenGL failure: An unacceptable value is specified for an enumerated argument. The offending function is ignored, having no side effect other than to set the error flag.");
break;
case GL_INVALID_VALUE:
sprintf(message, "OpenGL failure: A numeric argument is out of range. The offending function is ignored, having no side effect other than to set the error flag.");
break;
case GL_INVALID_OPERATION:
sprintf(message, "OpenGL failure: The specified operation is not allowed in the current state. The offending function is ignored, having no side effect other than to set the error flag.");
break;
case GL_STACK_OVERFLOW:
sprintf(message, "OpenGL failure: This function would cause a stack overflow. The offending function is ignored, having no side effect other than to set the error flag.");
break;
case GL_STACK_UNDERFLOW:
sprintf(message, "OpenGL failure: This function would cause a stack underflow. The offending function is ignored, having no side effect other than to set the error flag.");
break;
case GL_OUT_OF_MEMORY:
sprintf(message, "OpenGL failure: There is not enough memory left to execute the function. The state of OpenGL is undefined, except for the state of the error flags, after this error is recorded.");
break;
default:
sprintf(message, "Application failure.");
break;
}
#ifdef WINDOWS
MessageBox(NULL, message, "Error", MB_OK | MB_ICONERROR);
#else
printf("%s", message);
#endif
}
|