// Animation main loop // period - maximum time between redraws in ms void mainLoop(unsigned period) { // This main loop requires timer support if(SDL_InitSubSystem(SDL_INIT_TIMER) < 0) throw SDL_Exception(); // Create redraw timer class RedrawTimer { private : SDL_TimerID id; static Uint32 callback(Uint32 interval, void *) { redraw(); return interval; } public : RedrawTimer(unsigned interval) : id(SDL_AddTimer(interval, callback, NULL)) { if(id == NULL) throw SDL_Exception(); } ~RedrawTimer() { if(id != NULL) SDL_RemoveTimer(id); } } redrawTimer(period); // Start simple main loop mainLoop(); }
/** * Initialize the output framebuffer * @param width The width of the screen * @param height The height of the screen * @param color SDL attribute * @return The created sdl surface */ SDL_Surface * initScreen(unsigned int width, unsigned int height, unsigned int color) { // Create window SDL_Surface * screen = SDL_SetVideoMode(width, height, color, SDL_DOUBLEBUF | SDL_RESIZABLE); if(screen == NULL) throw SDL_Exception(); return screen; }
// Simple main loop void mainLoop() { // Window is not minimized bool active = true; for(;;)// Infinite loop { SDL_Event event; // Wait for event if(SDL_WaitEvent(&event) == 0) throw SDL_Exception(); // Screen needs redraw bool redraw = false; // Handle all waiting events do { camera += cam_velocity; // Call proper event handlers switch(event.type) { case SDL_ACTIVEEVENT : // Stop redraw when minimized if(event.active.state == SDL_APPACTIVE) active = event.active.gain; break; case SDL_KEYDOWN : onKeyDown(event.key.keysym.sym, event.key.keysym.mod); break; case SDL_KEYUP : onKeyUp(event.key.keysym.sym, event.key.keysym.mod); break; case SDL_MOUSEMOTION : onMouseMove(event.motion.x, event.motion.y, event.motion.xrel, event.motion.yrel, event.motion.state); break; case SDL_MOUSEBUTTONDOWN : onMouseDown(event.button.button, event.button.x, event.button.y); break; case SDL_MOUSEBUTTONUP : onMouseUp(event.button.button, event.button.x, event.button.y); break; case SDL_QUIT : return; // End main loop case SDL_VIDEORESIZE : onWindowResized(event.resize.w, event.resize.h); break; case SDL_VIDEOEXPOSE : redraw = true; break; default : // Do nothing break; } } while(SDL_PollEvent(&event) == 1); // Optionally redraw window if(active && redraw) onWindowRedraw(); } }
SDL_Surface * init(unsigned width, unsigned height, unsigned color, unsigned depth, unsigned stencil) { // Set OpenGL attributes if(SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, color) < 0) throw SDL_Exception(); if(SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, depth) < 0) throw SDL_Exception(); if(SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, stencil) < 0) throw SDL_Exception(); if(SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) < 0) throw SDL_Exception(); // Create window SDL_Surface * screen = SDL_SetVideoMode(width, height, color, SDL_OPENGL | SDL_RESIZABLE); if(screen == NULL) throw SDL_Exception(); // Call init code onInit(); onWindowResized(width, height); return screen; }
SDL_Surface * init(unsigned width, unsigned height, unsigned color, unsigned depth, unsigned stencil) { // Set OpenGL attributes if(SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, color) < 0) throw SDL_Exception(); if(SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, depth) < 0) throw SDL_Exception(); if(SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, stencil) < 0) throw SDL_Exception(); if(SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) < 0) throw SDL_Exception(); // Create window SDL_Surface * screen = SDL_SetVideoMode(width, height, color, SDL_OPENGL | SDL_RESIZABLE); if(screen == NULL) throw SDL_Exception(); #ifndef USE_GLEE // Init extensions GLenum error = glewInit(); if(error != GLEW_OK) throw std::runtime_error(std::string("GLEW : Init failed : ") + (const char*)glewGetErrorString(error)); #endif //USE_GLEE // Call init code onInit(); onWindowResized(width, height); return screen; }
RedrawTimer(unsigned interval) : id(SDL_AddTimer(interval, callback, NULL)) { if(id == NULL) throw SDL_Exception(); }
// Animation main loop // period - maximum time between redraws in ms void mainLoop(unsigned period) { // This main loop requires timer support if(SDL_InitSubSystem(SDL_INIT_TIMER) < 0) throw SDL_Exception(); // Create redraw timer class RedrawTimer { private : SDL_TimerID id; static Uint32 callback(Uint32 interval, void *) { redraw(); return interval; } public : RedrawTimer(unsigned interval) : id(SDL_AddTimer(interval, callback, NULL)) { if(id == NULL) throw SDL_Exception(); } ~RedrawTimer() { if(id != NULL) SDL_RemoveTimer(id); } } redrawTimer(period); // Window is not minimized bool active = true; for(;;)// Infinite loop { SDL_Event event; // Wait for event if(SDL_WaitEvent(&event) == 0) throw SDL_Exception(); // Screen needs redraw bool redraw = false; // Handle all waiting events do { // Call proper event handlers switch(event.type) { case SDL_ACTIVEEVENT :// Stop redraw when minimized if(event.active.state == SDL_APPACTIVE) active = event.active.gain; break; case SDL_KEYDOWN : onKeyDown(event.key.keysym.sym, event.key.keysym.mod); break; case SDL_KEYUP : onKeyUp(event.key.keysym.sym, event.key.keysym.mod); break; case SDL_MOUSEMOTION : onMouseMove(event.motion.x, event.motion.y, event.motion.xrel, event.motion.yrel, event.motion.state); break; case SDL_MOUSEBUTTONDOWN : onMouseDown(event.button.button, event.button.x, event.button.y); break; case SDL_MOUSEBUTTONUP : onMouseUp(event.button.button, event.button.x, event.button.y); break; case SDL_QUIT : return;// End main loop case SDL_VIDEORESIZE : onWindowResized(event.resize.w, event.resize.h); break; case SDL_VIDEOEXPOSE : redraw = true; break; default :// Do nothing break; } } while(SDL_PollEvent(&event) == 1); // Optionally redraw window if(active && redraw) onWindowRedraw(); } }