Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fernandoherreradelasheras/ec2f3cbf57f38ba876148493ca1f7cf6 to your computer and use it in GitHub Desktop.
Save fernandoherreradelasheras/ec2f3cbf57f38ba876148493ca1f7cf6 to your computer and use it in GitHub Desktop.
#include <SDL2/SDL.h>
#include <unistd.h>
#include <stdio.h>
/* Compile with:
* g++ -g -o sdl2_wayland_libdecor_bug sdl2_wayland_libdecor_bug.cpp `sdl2-config --cflags --libs`
*/
const int disp_w = 400, disp_h = 300;
static SDL_Window *window = NULL;
static SDL_Renderer *renderer = NULL;
SDL_Rect rect = { 0, 0, 100, 100 };
#define FPS 60
static void update_screen() {
if (++rect.x + rect.w >= disp_w) {
rect.x = 0;
}
if (++rect.y + rect.h >= disp_h) {
rect.y = 0;
}
SDL_RenderClear (renderer);
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_RenderFillRect(renderer, &rect);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderPresent(renderer);
}
static uint32_t timer_handler(uint32_t interval, void *param) {
SDL_Event event;
event.type = SDL_USEREVENT;
SDL_PushEvent(&event);
return interval;
}
void wait_loop() {
while (1) {
SDL_Event event;
SDL_WaitEvent(&event);
switch (event.type) {
case SDL_QUIT: exit(0); break;
case SDL_USEREVENT: update_screen(); break;
}
}
}
void poll_loop() {
while (1) {
SDL_Event event;
int ret = SDL_PollEvent(&event);
if (ret == 1)
switch (event.type) {
case SDL_QUIT: exit(0); break;
case SDL_USEREVENT: update_screen(); break;
}
SDL_Delay(1000/FPS);
}
}
int main(int argc, char *argv[]) {
SDL_InitSubSystem(SDL_INIT_VIDEO | SDL_INIT_TIMER);
SDL_CreateWindowAndRenderer(disp_w, disp_h, 0, &window, &renderer);
SDL_SetWindowTitle(window, "Test app");
int mode = (argc > 1) ? (int)strtol(argv[1], (char **)NULL, 10) : 0;
switch(mode) {
case 0:
printf("Scheduling SDL_USEREVENT with no window displayed. SDL_WaitEvent will hang\n");
SDL_AddTimer(1000 / FPS, timer_handler, NULL);
wait_loop();
break;
case 1:
printf("Scheduling SDL_USEREVENT and force window display. SDL_WaitEvent will read events ~on window interactions\n");
SDL_AddTimer(1000 / FPS, timer_handler, NULL);
update_screen();
wait_loop();
break;
case 2:
printf("Displaying window and then scheduling SDL_USEREVENT. SDL_WaitEvent will read events ~on window interactions\n");
update_screen();
SDL_AddTimer(1000 / FPS, timer_handler, NULL);
wait_loop();
break;
case 3:
printf("Using SDL_PollEvent instead of SDL_WaitEvent. Should be fine\n");
SDL_AddTimer(1000 / FPS, timer_handler, NULL);
poll_loop();
break;
default:
printf("Run as %s [0 | 1 | 2 | 3]\n", argv[0]);
exit(1);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment