Skip to content

Instantly share code, notes, and snippets.

@Fighter19
Created February 15, 2017 03:05
Show Gist options
  • Save Fighter19/2e199d6a505dcda5fa9e05ab8f1840e1 to your computer and use it in GitHub Desktop.
Save Fighter19/2e199d6a505dcda5fa9e05ab8f1840e1 to your computer and use it in GitHub Desktop.
Simple SDL window
#include <SDL2/SDL.h>
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
SDL_Renderer * renderer = NULL;
int init()
{
SDL_Window* gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
if( gWindow == NULL )
{
printf( "Window could not be created! SDL Error: %s\n", SDL_GetError() );
return -1;
}
else
{
//Create vsynced renderer for window
renderer = SDL_CreateRenderer( gWindow, -1, SDL_RENDERER_ACCELERATED ); // SDL_RENDERER_PRESENTVSYNC deactivated
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0");
if( renderer == NULL )
{
printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() );
return -1;
}
else
{
//Initialize renderer color
SDL_SetRenderDrawColor( renderer, 255, 0, 0, 255);
SDL_RendererInfo info;
SDL_GetRendererInfo( renderer, &info);
printf("Used renderer: %s\n", info.name);
}
}
}
int main( int argc, char* args[] )
{
bool quit = false;
init();
SDL_Event e;
//While application is running
while( !quit )
{
//Handle events on queue
while( SDL_PollEvent( &e ) != 0 )
{
//User requests quit
if( e.type == SDL_QUIT )
{
printf("SDL_QUIT received\n");
quit = true;
}
}
SDL_RenderPresent( renderer );
//Clear screen
SDL_SetRenderDrawColor( renderer, 200, 200, 200, 200);
SDL_RenderClear( renderer );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment