Game Programming with SDL: Getting Started with OpenGL - SDL-Based Framework: Creating and Testing (
Page 3 of 4 )
Up to now I have discussed SDL's various APIs. Now it's time to put them together so that working with OpenGL and SDL becomes easy. So here we go. The framework is based upon NeHe's excellent framework for SDL.net -- the .Net port of SDL.
First the includes:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <GL/gl.h> // The OpenGL
#include <GL/glu.h> // and OpenGL utility
#include <SDL.h> // and SDL headers
Here are the global variables which save the state of the surface and the program:
bool isProgramLooping;//This one is being used to know if the program
//needs to be continued or exited
SDL_Surface *Screen;
Now the common functionalities: initialization, termination and full-screen toggling.
bool Initialize(void)// Any Application & User Initialization Code would be here
{
AppStatus.Visible= true; // When program begins, the
// application window is
// visible
AppStatus.MouseFocus= true;// and have both mouse
AppStatus.KeyboardFocus = true;// and input focus
// start of user initialization. These are just examples
angle = 0.0f;// Set the starting angle to zero
cnt1= 0.0f;// Set the cos(for the x axis) counter to zero
cnt2= 0.0f;// Set the sin(for the y axis) counter to zero
{
printf("Cannot load graphic: %sn", SDL_GetError() );
return false;
}
return true;
// return true if initialization is successful
}
void Deinitialize(void) // Any User Deinitialization such as releasing file handles etc has to be performed here
{
return;
}
void TerminateApplication(void)// terminate the application
{
static SDL_Event Q;// This function sends aSDL_QUIT event
Q.type = SDL_QUIT;// to the sdl event queue
if(SDL_PushEvent(&Q) == -1) // Sending the event
{
printf("SDL_QUIT event can't be pushed: %sn", SDL_GetError() ); exit(1);
// And Exit
}
}
void ToggleFullscreen(void)// Toggle Fullscreen/Windowed
//(Works On Linux/BeOS Only)
{
SDL_Surface *S; // a surface to point the screen
S = SDL_GetVideoSurface(); // gets the video surface
if(!S || (SDL_WM_ToggleFullScreen(S)!=1))
// If SDL_GetVideoSurface Fails, Or if cant toggle to fullscreen
{
printf("Unable to toggle fullscreen: %sn", SDL_GetError() );
// only reporting the error, not exiting
}
}