#include #include #include #include #include #include #include #include using namespace std; #define PI 3.14159 #define DISPLAY_INTERVAL 30 // in milli-secs void draw() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // draw here glFlush(); } void on_key(SDL_keysym* key) { switch (key->sym) { case SDLK_ESCAPE: SDL_Quit(); exit(0); break; } } void init(Uint32 width, Uint32 height) { glClearColor(0, 0, 0.5, 0); glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); double ratio = double(width)/height; gluOrtho2D(-1*ratio, ratio, -1, 1); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } int main() { if (SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0) { cout << "SDL init error!" << endl; return 1; } // Check that we are using 32 bpp, and tell the user to change it to 32 if not. // Required for the X-Window System, since the bpp cannot be changed without // restarting the display server. const SDL_VideoInfo* vinfo = SDL_GetVideoInfo(); assert(vinfo); if (vinfo->vfmt->BitsPerPixel != 32) { cout << "Error: Your video is not set to use 32 bits-per-pixel, which is" << endl; cout << " required by this program. Please change it to 32-bits" << endl; cout << " and restart this program." << endl; exit(EXIT_FAILURE); } // Set opengl specific stuff SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 1); Uint32 flags = SDL_OPENGL; char c; cout << "Do you want to use fullscreen mode? (Y/N)"; cin >> c; if (c == 'Y' || c == 'y') flags |= SDL_FULLSCREEN; // Open screen SDL_Surface* screen = SDL_SetVideoMode(640, 480, 32, flags); assert(screen); init(648, 480); // opengl-specific initialisation while (1) { static Uint32 next_time; Uint32 now = SDL_GetTicks(); if (now < next_time) SDL_Delay(next_time - now); next_time += DISPLAY_INTERVAL; // Check for events SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_KEYDOWN: on_key(&event.key.keysym); break; case SDL_QUIT: SDL_Quit(); exit(0); break; } } // Draw the scene draw(); SDL_GL_SwapBuffers(); } return 0; }