Skip to content

Instantly share code, notes, and snippets.

@haxpor
Last active October 27, 2018 21:45
Show Gist options
  • Save haxpor/c9f4870947eacfb5e974d8f5c33e5a03 to your computer and use it in GitHub Desktop.
Save haxpor/c9f4870947eacfb5e974d8f5c33e5a03 to your computer and use it in GitHub Desktop.
Minimal code to show window in SDL2. Note that you need to call SDL_PollEvent to let the window rendered on screen.
#include <SDL2/SDL.h>
#include <stdio.h>
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
int main(int argc, char* argv[])
{
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
SDL_Log("failed to init: %s", SDL_GetError());
return -1;
}
window = SDL_CreateWindow(
"Hello", 0, 0, 640, 480,
SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL
);
if (window == NULL) {
SDL_Log("Failed to create window: %s", SDL_GetError());
return -1;
}
renderer = SDL_CreateRenderer(window, -1, 0);
if (renderer == NULL)
{
SDL_Log("Failed to create renderer: %s", SDL_GetError());
return -1;
}
SDL_bool quit = SDL_FALSE;
while (!quit)
{
SDL_Event e;
// we need to call SDL_PollEvent to let window rendered, otherwise
// no window will be shown
while (SDL_PollEvent(&e) != 0)
{
if (e.type == SDL_QUIT)
{
quit = SDL_TRUE;
}
}
}
// free
if (renderer != NULL)
{
SDL_DestroyRenderer(renderer);
renderer = NULL;
}
if (window != NULL)
{
SDL_DestroyWindow(window);
window = NULL;
}
SDL_Quit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment