Why is SDL2 window fading out? - c

I want a quick fix to this issue:
I wrote a simple program to play around with the SDL2 libraries. A cyan box moves along a blue background from left to right. Then the window closes.
The problem is that the window's color "fades out" while the program is running. The contrast decreases significantly and it's annoying. Sometimes it happens when the box is in the middle of the window. Sometimes it happens when the box reaches the right side of the window. Sometimes it doesn't happen at all. This fading of colors seems to be sporadic and random. It is a run time issue. Theoretically, I do not see any issue with the code. What is wrong?
#include "SDL.h"
#include <stdio.h>
#ifdef __cplusplus
extern "C"
#endif
int main(int argc, char* argv[])
{
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
window = SDL_CreateWindow("Boxes", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, 0);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_Rect myBox = { 200, 150, 50, 50 };
int go = 0;
while (go <= 590) {
myBox.x = go;
SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 0, 255, 255, 255);
SDL_RenderFillRect(renderer, &myBox);
SDL_RenderPresent(renderer);
if (go == 0)
SDL_Delay(2000);
SDL_Delay(100);
go += 10;
}
SDL_Delay(2000);
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
SDL_Quit();
return EXIT_SUCCESS;
}

That's a classical SDL mistake.
You're not handling events that your window receives, and because of that your OS assumes that your program has hung up.
Inside of your while loop, add following:
SDL_Event e;
while (SDL_PollEvent(&e))
if (e.type == SDL_QUIT)
return 0;

Related

How do I use a software SDL_Renderer correctly while resizing the window?

I'm not sure how to use a software SDL_Renderer to render to a window while resizing that window. For example, I expect the following code to constantly display a fully black window even as the uses resizes the window. Instead, when the user resizes the window, it's not filled with black, you get kind of "streaks" of black against a transparent background.
I'm sure I could cobble something together with catching resize events, maybe using SDL_RenderFilleRect() instead of SDL_RenderClear(), but fundamentally I just don't know what I should be doing at all. Should I be freeing the SDL_Surface and getting an entirely new surface and renderer every time the window resizes? Should I be calling SDL_RenderSetViewport()?
#include <SDL.h>
int main()
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window *window = SDL_CreateWindow(
"Main Window",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
600,
600,
SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE
);
SDL_Surface *window_surface = SDL_GetWindowSurface(window);
SDL_Renderer *renderer = SDL_CreateSoftwareRenderer(window_surface);
SDL_Event event;
bool quit = false;
while (!quit)
{
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
quit = true;
break;
}
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
SDL_UpdateWindowSurface(window);
SDL_Delay(1000 / 60);
}
SDL_Quit();
return 0;
}

SDL (C)- Window Won't Appear without Event Loop

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 result (I'm new here by the way, is it possible to directly show images in a post instead of having to just include a link to it?)
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?
You're right it was asked many times before. Short answer is "window managers behave differently, and worse, they may have compositing enabled". Basically you create window, start drawing, tell window manager you're done, and some time later get signal that your window you thought you're drawing into was created just now, so would you kindly redraw whatever you wanted to display. The same thing happens when your window is resized, restored from minimised state, overshadowed by other windows, etc - that was always a thing, simple 3-line examples just ignored it; window manager is not responsible for keeping your image, whenever something happens it just sends you signal to redraw (doesn't mean it isn't allowed to keep image for its own purposes, mostly for compositing, previews, etc.). Before compositing age it "worked most of the times".
Window manager also may rely on target application's event processing to show "application not responding" dialog.
So actually your second version is still incorrect as you draw only once and don't watch for redraw events. Games often don't bother with these events as their image is very dynamic anyway and redraw unconditionally. You not only need event loop but also redraw.
As for how you can break loop when some time is passed - you need to look at the time and when given interval have passed trigger your quit or break loop in other way. Aside from OS provided time functions there are SDL_GetTicks, SDL_AddTimer, etc..
Here is an example:
#include <SDL2/SDL.h>
#include <stdio.h>
#include <stdbool.h>
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
int main(int argc, char **argv) {
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) { return 1; }
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (renderer == NULL) { return 1; }
const Uint32 time_started = SDL_GetTicks();
SDL_Event e;
bool quit = false;
while (!quit){
// check for timeout
if(SDL_GetTicks() - time_started > 2000) { quit = true; }
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;
}
}
// break early - doesn't really matter
if(quit) { break; }
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
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_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}

SDL clears the window screen when clicking on the window of another software in Linux

My program is about drawing the Mandelbrot set. I discovered that when I click another window in the operating system (eg. Chrome, or text editor ...) my program clears the screen to black but continues drawing where it finished. It is during drawing. If the drawing is finished and I click another window then nothing clearing happens.
Is there a solution to fix this (eg. switching something on or off in SDL) without tracking all the pixels onto the screen and print the entire pixel matrix all the times?
Minimal code:
#include <stdlib.h>
#include <stdbool.h>
#include <SDL2/SDL.h>
enum {WIDTH = 1700, HEIGHT = 950};
int main(){
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *window = SDL_CreateWindow("Minimal", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, SDL_WINDOW_SHOWN);
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_Event event;
bool exit_ = false;
while (! exit_){
for (int i = 1; i < WIDTH; i++){
for (int j = 1; j < HEIGHT; j++){
SDL_RenderDrawPoint(renderer, i, j);
}
SDL_RenderPresent(renderer);
}
while (SDL_PollEvent(&event)){
if (event.type == SDL_QUIT) exit_ = true;
}
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
(Note: I haven't got any window event handling in the program.)
I think you need to put SDL_RenderClear(Renderer) at the top of the while loop.
If you do like that, window will be never cleared after rendering.
For example if you don't clear the window, a moving dot will leave his traces..
I work woth SDL too, and i always do this

C SDL Object movement flow

I just started with SDL and made a small square moving around on a window.
I used DL_GetKeyboardState, and got it moving kind of smoothly. However, just when I start moving it one direction by holding down one key, it moves a bit, stops, then move like I want. This messes up the smoothness. My best guess is that it waits for a doubleclick from the mouse, but im not sure. Any suggestions?
The code:
#include <stdio.h>
#include <SDL.h>
int main(int argc, char * argv[])
{
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Event event;
SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow("Yelda!", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1600, 1200, 0);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
const Uint8 *state;
int run = 1;
int xpos=300;
int ypos=300;
int projectileYPos;
int projectileXPos;
int loop;
int lanceLength = 10;
while(run)
{
while(SDL_PollEvent(&event))
{
state=SDL_GetKeyboardState(NULL);
if(state[SDL_SCANCODE_ESCAPE])
run=0;
if(state[SDL_SCANCODE_W])
ypos-=10;
if(state[SDL_SCANCODE_S])
ypos+=10;
if(state[SDL_SCANCODE_A])
xpos-=10;
if(state[SDL_SCANCODE_D])
xpos+=10;
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 255, 255, 255 ,255);
SDL_Rect rect = {xpos, ypos, 20, 20 };
SDL_RenderFillRect(renderer, &rect);
SDL_RenderPresent(renderer);
SDL_Delay(20);
}
}
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
SDL_Quit();
return 0;
}
The SDL wiki says that you should use SDL_PumpEvents() to update the keyboard state array.
And indeed, if we
remove the while(SDL_PollEvent(&event)) loop (not its contents) as it's unneeded,
move state=SDL_GetKeyboardState(NULL); before the while(run) loop (the address of the array "will be valid for the whole lifetime of the application", so it always returns the same pointer), and
only call SDL_PumpEvents() inside the loop,
then the movement is smooth.

C and SDL in Ubuntu not working as intended

I am following a tutorial on Youtube, but it does not work as intended when I try it.
Here is the code:
#include <stdio.h>
#include <SDL2/SDL.h>
void start(SDL_Window* window, SDL_Renderer* renderer)
{
SDL_Init(SDL_INIT_EVERYTHING);
window = SDL_CreateWindow("Hello", 200, 200, 200, 200, 0);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_Rect rect = {220, 140, 200, 200};
SDL_RenderFillRect(renderer, &rect);
}
void stop(SDL_Window* window, SDL_Renderer* renderer)
{
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
}
int main(int argc, char **argv)
{
SDL_Window* window;
SDL_Renderer* renderer;
start(window, renderer);
SDL_Delay(5000);
stop(window, renderer);
return 0;
}
It is supposed to create a white rectangle in the middle of the window and fill the rest with blue color, but that does not happen when I run it. Instead, it "captures a picture" of the program under it, for example the background. Why is this happening, and how can I solve it?
You have two issues to fix. First, you need to modify your start() function to take pointers-to-pointers, rather than just pointers, so that the window and renderer variables in main() can be modified... otherwise, window and renderer within start() will be valid, but the corresponding pointer variables in main() will not be changed and will remain uninitialized. So...
/* Use 'SDL_Window**' and 'SDL_Renderer**' instead of 'SDL_Window*' and
* 'SDL_Renderer*', and dereference once during use (eg. '*window'
* instead of 'window' */
void start(SDL_Window** window, SDL_Renderer** renderer)
{
SDL_Init(SDL_INIT_EVERYTHING);
*window = SDL_CreateWindow("Hello", 200, 200, 200, 200, 0);
*renderer = SDL_CreateRenderer(*window, -1, SDL_RENDERER_ACCELERATED);
SDL_SetRenderDrawColor(*renderer, 0, 0, 255, 255);
SDL_RenderClear(*renderer);
SDL_SetRenderDrawColor(*renderer, 255, 255, 255, 255);
SDL_Rect rect = {220, 140, 200, 200};
SDL_RenderFillRect(*renderer, &rect);
}
Then in main() use:
start(&window, &renderer); /* pass addresses, not values */
The second issue is that you need to update the screen after finishing the drawing operations. You can do this with SDL_RenderPresent() when you're
done drawing, such as in main() right after calling your start() function:
...
start(&window, &renderer);
SDL_RenderPresent(renderer); /* add this */
SDL_Delay(5000);
...
You may also want to change the values in your SDL_Rect so that it's not drawn outside the window.

Resources