Hello,
I’ve downloaded the “Source snapshot for SDL 2.0 (Updated Mon Feb 20 )” and I have compiled it on my system (GNU/Linux x86_64).
I’ve tried some things and I encountered a problem: in a loop where I check for keyboard/mouse/window events, when I minimize the SDL window or switch to another workspace or raise another window above the SDL one, sometimes, when I get back to the SDL window, it is frozen and either doesn’t seem to respond anymore or it appears completely black (whereas there should be a yellowish rectangle drawn in it). My only way of exiting the application is by killing it.
Here is my C code:
Code:
#include <SDL2/SDL.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#define WIDTH 512
#define HEIGHT 512
#define SQW 40
#define SQH SQW
#define FPS 60
void guard(SDL_Rect *rec) {
if(rec->x > WIDTH-SQW) {
rec->x = WIDTH-SQW;
} else if(rec->x < 0) {
rec->x = 0;
}
if(rec->y > HEIGHT-SQH) {
rec->y = HEIGHT-SQH;
} else if(rec->y < 0) {
rec->y = 0;
}
}
int main(void) {
const Uint32 duration = 1000 / FPS;
SDL_Rect coord = {
.x = (WIDTH / 2) - 40 / 2,
.y = (HEIGHT / 2) - 40 / 2,
.w = SQW,
.h = SQH};
int speed = 2;
bool redraw = true, done = false;
SDL_Window *window;
SDL_Renderer *renderer;
int mx, my; // mouse coordinates
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "SDL failed initializing.\n");
return EXIT_FAILURE;
}
window = SDL_CreateWindow("SDL_RenderClear",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
WIDTH, HEIGHT,
SDL_WINDOW_SHOWN);
// -1: use the default one
renderer = SDL_CreateRenderer(window, -1, 0);
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_RenderClear(renderer);
SDL_Event tmp;
Uint8* keystate = SDL_GetKeyboardState(NULL);
Uint8 mousestate;
Uint32 time = SDL_GetTicks();
while(!done) {
SDL_PumpEvents();
mousestate = SDL_GetMouseState(&mx, &my);
if(keystate[SDL_SCANCODE_UP]) {
coord.y -= speed;
redraw = true;
}
if(keystate[SDL_SCANCODE_DOWN]) {
coord.y += speed;
redraw = true;
}
if(keystate[SDL_SCANCODE_LEFT]) {
coord.x -= speed;
redraw = true;
}
if(keystate[SDL_SCANCODE_RIGHT]) {
coord.x += speed;
redraw = true;
}
if(keystate[SDL_SCANCODE_ESCAPE]) {
done = true;
continue;
}
if(mousestate & SDL_BUTTON(SDL_BUTTON_LEFT)) {
coord.x = mx - SQW/2;
coord.y = my - SQH/2;
redraw = true;
} else if(mousestate & SDL_BUTTON(SDL_BUTTON_MIDDLE)) {
coord.x = WIDTH/2 - SQW/2;
coord.y = HEIGHT/2 - SQH/2;
redraw = true;
} else if(mousestate & SDL_BUTTON(SDL_BUTTON_RIGHT)) {
done = true;
continue;
}
if(SDL_PollEvent(&tmp) && tmp.type == SDL_WINDOWEVENT) {
if(tmp.window.event == SDL_WINDOWEVENT_CLOSE) {
done = true;
continue;
} else {
SDL_FlushEvent(SDL_WINDOWEVENT);
fprintf(stderr, "SDL_WINDOWEVENT_ANY");
}
}
if(SDL_GetTicks() - time > duration) {
time = SDL_GetTicks();
redraw = true;
} else {
SDL_Delay((duration) - (SDL_GetTicks()-time));
}
if(redraw) {
guard(&coord);
redraw = false;
SDL_SetRenderDrawColor(renderer, 50, 50, 50, 255);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 155, 155, 0, 255);
SDL_RenderFillRect(renderer, &coord);
SDL_RenderPresent(renderer);
}
}
SDL_Quit();
return EXIT_SUCCESS;
}
I hope it isn’t too awful, I’m not sure if I’m doing well when striving to check for different kinds of events (window closing, keys pressed, mouse motion, etc.).
Can you help me find the origin of my problem? (thanks)