SDL window image flickers

Hi! I've been learning some programming with the SDL libraries and I've encountered a problem, whenever I debug the project, the window shown flickers with the image I'm trying to render and it doesn't respond (shows the loading cursor symbol) also the console window doesn't even open at all, I'm just starting to learn the code so hopefully it won't be a big problem to find the problem, here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_mixer.h>

using namespace std;

int main(int argc, char** argv){
	cout<<"HI!!!!"<<endl;
	system("pause");
	bool quit = 0;
    SDL_Init(SDL_INIT_EVERYTHING);

	SDL_Window* window = NULL;
	window = SDL_CreateWindow("My first RPG", 100, 100, 640, 480, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
	SDL_Renderer *renderer = NULL;
	renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);

	SDL_Event* mainEvent = new SDL_Event();

	SDL_Texture *grass_image = NULL;
	grass_image = IMG_LoadTexture(renderer,"grass.bmp");

	SDL_Rect grass_rect;
	grass_rect.h = 250;
	grass_rect.w = 250;
	grass_rect.x = 100;
	grass_rect.y = 100;


	while(!quit && mainEvent->type != SDL_QUIT){
		SDL_RenderClear(renderer);
		SDL_RenderPresent(renderer);

		SDL_RenderCopy(renderer, grass_image, NULL, &grass_rect);

		SDL_RenderPresent(renderer);
	}

	SDL_DestroyWindow(window);
	SDL_DestroyRenderer(renderer);

	delete mainEvent;
	system("pause");
    return 0;
}
I'm not practical with 2.0, but it seems like you're rendering the screen after you clear it, which should show some kind of 'empty' image that gives that flickering effect.
The window should be irresponsive because you're not processing the events correctly. Take a look at this http://wiki.libsdl.org/SDL_PollEvent .
The console window might not show up if you use an IDE and have selected a GUI project. What are you using?
Thanks! I removed "SDL_RenderClear(renderer);" and fixed it! Also I was missing "SDL_PollEvent(mainEvent);"

I'm using Visual Studio 2012, I selected Win32 project.
I'm still a beginner so sorry if I didn't interpret your question correctly.
It's better if you remove the first call to RenderPresent() instead of RenderClear(). Usually you clear the screen and redraw the whole scene (I can't tell you why, but that's how tutorial do it).

IIRC Win32 projects are those that don't spawn a console. Try switching to "console project" or something like that.
closed account (3qX21hU5)
Usually you clear the screen and redraw the whole scene (I can't tell you why, but that's how tutorial do it).


The reason for this is because you if you don't you might keep undefined pixels from the previous frame.
It's more because the contents of the frame buffer is undefined after you present. A present typically doesn't "copy" the image anywhere, it just swaps out which buffer is used for the display.

Often times with double buffering there are 2 buffers. You draw to buffer 1 while buffer 2 is displayed on screen and vice versa. A "present" merely switches which one you're rendering to.

Failure to clear before you draw means you might get fragments from 2+ frames ago (not necessarily the prev frame). But again, since implementation may vary depending on the hardware, there is no guarantee of this. On triple-buffering hardware you might get something entirely different.


So yeah. always clear.
Last edited on
closed account (3qX21hU5)
Ya that is what I meant to say ;p
Last edited on
Whoops yeah you did say that. For whatever reason I thought you said something else.

Zrrrrrrrggggg
Topic archived. No new replies allowed.