Skip to content

Instantly share code, notes, and snippets.

@noio noio/gist:8730246
Created Jan 31, 2014

Embed
What would you like to do?
//
// Run the game loop
//
void Engine::Run(){
Loop();
}
//========= Privates ==========//
//
// Initializes all the required SDL modules.
//
void Engine::InitSDL(){
//Start SDL
if (SDL_Init( SDL_INIT_EVERYTHING ) != 0){
cerr << "SDL_Init() Failed: " <<
SDL_GetError() << endl;
exit(1);
}
// Initialize SDL_ttf library
if (TTF_Init() != 0) {
cerr << "TTF_Init() Failed: " << TTF_GetError() << endl;
SDL_Quit();
exit(1);
}
SDL_CreateWindowAndRenderer(res_width_, res_height_, SDL_WINDOW_OPENGL, &window_, &renderer_);
SDL_SetRenderDrawColor(renderer_, 0, 0, 0, 255);
screen_surface_ = SDL_CreateRGBSurface(0, res_width_, res_height_, 32,
0x00FF0000,
0x0000FF00,
0x000000FF,
0xFF000000);
if (!screen_surface_) {
cerr << "SDL_CreateRGBSurface failed: " << SDL_GetError() << endl;
SDL_Quit();
exit(1);
}
screen_texture_ = SDL_CreateTexture(renderer_,
SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING,
res_width_, res_height_);
}
void Engine::InitOpenGL()
{
glDisable(GL_LIGHTING);
glDisable(GL_TEXTURE_2D);
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glViewport( 0, 0, res_width_, res_height_ );
glClear( GL_COLOR_BUFFER_BIT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho(0.0f, res_width_, res_height_, 0.0f, -1.0f, 1.0f);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
}
//
// The main game loop
//
void Engine::Loop(){
double real_fps;
int frame_start, frame_end;
running_ = true;
while (running_) {
real_fps = 1000.0 / (SDL_GetTicks() - frame_start);
frame_start = SDL_GetTicks();
Update();
Display();
// Schedule next frame
frame_end = SDL_GetTicks();
printf("%d ms\n", (frame_end - frame_start));
SDL_Delay( std::max(0, int(frame_start + (1000.0 / target_fps_) - frame_end )));
}
}
//
// Render
//
void Engine::Display(){
SDL_Surface* frame_surface = ConvertToSDLSurface(flow_.frame());
SDL_BlitScaled(frame_surface, NULL, screen_surface_, NULL);
SDL_FreeSurface(frame_surface);
// Push to texture and render
SDL_UpdateTexture(screen_texture_, NULL, screen_surface_->pixels, screen_surface_->pitch);
SDL_RenderClear(renderer_);
SDL_RenderCopy(renderer_, screen_texture_, NULL, NULL);
glLoadIdentity();
glDisable(GL_TEXTURE_2D);
glColor3f(1.0, 1.0, 1.0);
glBegin( GL_QUADS );
glVertex3f( 10.0f, 50.0f, 0.0f ); /* Top Left */
glVertex3f( 50.0f, 50.0f, 0.0f ); /* Top Right */
glVertex3f( 50.0f, 10.0f, 0.0f ); /* Bottom Right */
glVertex3f( 10.0f, 10.0f, 0.0f ); /* Bottom Left */
glEnd( );
glColor3f(1.0, 1.0, 1.0);
SDL_RenderPresent(renderer_);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.