Created
July 31, 2018 20:36
-
-
Save krpors/860adc39437bce1b4a4619b01030e9c8 to your computer and use it in GitHub Desktop.
segfault with SDL2.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <assert.h> | |
#include <math.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdbool.h> | |
#include <inttypes.h> | |
#include <time.h> | |
#include <SDL.h> | |
int main(int argc, char* argv[]) { | |
(void)(argc); | |
(void)(argv); | |
srand(time(NULL)); | |
if (SDL_Init(SDL_INIT_VIDEO) < 0) { | |
fprintf(stderr, "Cannot init SDL: %s\n", SDL_GetError()); | |
exit(1); | |
} | |
SDL_Window* window = SDL_CreateWindow( | |
"Pixel renderer", | |
SDL_WINDOWPOS_UNDEFINED, | |
SDL_WINDOWPOS_UNDEFINED, | |
800, 600, | |
SDL_WINDOW_SHOWN); | |
if (window == NULL) { | |
fprintf(stderr, "Cannot open window: %s\n", SDL_GetError()); | |
exit(1); | |
} | |
SDL_Surface* surface = SDL_GetWindowSurface(window); | |
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); | |
SDL_Event e; | |
bool quit = false; | |
bool pause = false; | |
while (!quit) { | |
while (SDL_PollEvent(&e) != 0) { | |
if (e.type == SDL_QUIT) { | |
quit = true; | |
} | |
switch (e.key.keysym.sym) { | |
case SDLK_ESCAPE: quit = true; break; | |
case SDLK_SPACE: | |
if (e.key.type == SDL_KEYDOWN) { | |
pause = !pause; | |
printf("pausing: %d\n", pause); | |
} | |
break; | |
} | |
} | |
if (pause) { | |
continue; | |
} | |
SDL_Delay(20); | |
// uncomment this next line to prevent segfault. | |
// Has something to do with the creation of the renderer on #35 | |
SDL_UpdateWindowSurface(window); | |
} | |
SDL_FreeSurface(surface); | |
SDL_DestroyWindow(window); | |
SDL_DestroyRenderer(renderer); | |
SDL_Quit(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment