I'm trying to make a program that renders several colored rectangles within a window, then disappears after a few seconds. Basically, the following should appear (and I'll shortly explain how I got it to in the first place despite the problems I'm having):
The code I'm trying to run is as follows, obviously withstanding the inclusion of < stdio.h > and < SDL2/SDL.h > as well as definitions for "WINDOW_WIDTH" and "WINDOW_HEIGHT" which both appear in the code below (and are set to 800 and 640, respectively).
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow("Game Window", 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, 0);
if (window == NULL) {
// In the case that the window could not be made...
printf("Could not create window: %s\n", SDL_GetError());
return 1;
}
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 140, 0, 100, 100);
SDL_Rect rect = { 0, 0, 800, 640 };
SDL_RenderFillRect(renderer, &rect);
SDL_SetRenderDrawColor(renderer, 60, 0, 255, 100);
SDL_Rect rect2 = { 40, 40, 720, 560 };
SDL_RenderFillRect(renderer, &rect2);
SDL_SetRenderDrawColor(renderer, 50, 140, 0, 100);
SDL_Rect rect3 = { 80, 80, 640, 480 };
SDL_RenderFillRect(renderer, &rect3);
SDL_SetRenderDrawColor(renderer, 200, 50, 25, 0);
SDL_Rect rect4 = { 120, 120, 560, 400 };
SDL_RenderFillRect(renderer, &rect4);
SDL_RenderPresent(renderer);
SDL_Delay(2000);
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
SDL_Quit();
return 0;
This code seems like it should work fine, and it in fact does for the person in the aforementioned video (who, like me, is running this using Xcode on MacOS, albeit a far older version of it). However, the same thing that happens in the video does not seem to happen for me when I run my written code. Instead, the window simply doesn't appear at all. The code itself does indeed run, and perfectly fine for that matter without any errors or warnings, but the window with the multicolored rectangles doesn't show up at all before the delay is over and the SDL window is destroyed.
I realize that this question has been asked before on this website, and each with very similar issues, and I need to make it clear that yes, I've tried using an event loop instead and it works perfectly fine. For instance, if I run the following code- which is identical to the first batch of code except with "SDL_DELAY" replaced by an event handler which detects when the keyboard or mouse is pressed (and also includes < stdbool.h > for the boolean variable used)- the window appears just fine:
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow("Game Window", 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, 0);
if (window == NULL) {
// In the case that the window could not be made...
printf("Could not create window: %s\n", SDL_GetError());
return 1;
}
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 140, 0, 100, 100);
SDL_Rect rect = { 0, 0, 800, 640 };
SDL_RenderFillRect(renderer, &rect);
SDL_SetRenderDrawColor(renderer, 60, 0, 255, 100);
SDL_Rect rect2 = { 40, 40, 720, 560 };
SDL_RenderFillRect(renderer, &rect2);
SDL_SetRenderDrawColor(renderer, 50, 140, 0, 100);
SDL_Rect rect3 = { 80, 80, 640, 480 };
SDL_RenderFillRect(renderer, &rect3);
SDL_SetRenderDrawColor(renderer, 200, 50, 25, 0);
SDL_Rect rect4 = { 120, 120, 560, 400 };
SDL_RenderFillRect(renderer, &rect4);
SDL_RenderPresent(renderer);
SDL_Event e;
bool quit = false;
while (!quit){
while (SDL_PollEvent(&e)){
if (e.type == SDL_QUIT){
quit = true;
}
if (e.type == SDL_KEYDOWN){
quit = true;
}
if (e.type == SDL_MOUSEBUTTONDOWN){
quit = true;
}
}
}
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
SDL_Quit();
return 0;
However, this is different from what the first attempt was trying to achieve, which as I said is a window that is rendered and then gets destroyed after a brief period of time. How am I supposed to get this to happen if I can't use the first set of code shown above? Moreover, why does such a set-up using SDL_DELAY clearly work for the person in the video (which for the sake of convenience, can again be found here) but not when I try to input it?