Reputation: 1
I ran into an issue with getting my picture to display. I couldn't get the picture to show up in the window.
I accidentally ran the program twice (two windows) and the second window had the image in it. I can close the first and the image holds in the second window. It looks sketchy if I moved the window around.
Said all that to ask, why would my picture only show in a 2nd(3rd 4th) window and not the original.
Here is the code.
#include <iostream>
#include <SDL.h>
int main(int argc, char ** argv)
{
bool quit = false;
SDL_Event event;
SDL_Init(SDL_INIT_VIDEO);
SDL_Window * gWindow = SDL_CreateWindow("SDL_Yo",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640,480,SDL_WINDOW_SHOWN );
SDL_Surface* gScreenSurface = SDL_GetWindowSurface( gWindow );
if( gScreenSurface == NULL )
{
SDL_Quit();
}
SDL_Surface* gHelloWorld = SDL_LoadBMP("IMG_0004.bmp");
if( gHelloWorld == NULL )
{
SDL_Quit();
}
SDL_BlitSurface( gHelloWorld, NULL, gScreenSurface, NULL );
quit = SDL_UpdateWindowSurface( gWindow );
while (!quit)
{
SDL_WaitEvent(&event);
switch (event.type)
{
case SDL_QUIT:
quit = true;
break;
}
}
SDL_Quit();
return 0;
}
Upvotes: 0
Views: 316
Reputation: 18399
You can't just throw something to be drawn once and expect it to stay on screen. What you've described in question is exactly how window manager operates - if some part of your window is not shown, there is no need to update it. But once it is visible again, window manager sends you a message that you need to redraw, as there is no image data held anywhere and display system have nothing to display. You didn't redraw, so some garbage (stuff that was there before) is being displayed.
As to why you didn't see image initially even though nothing was overshadowing your window - is because you've displayed your image too early. Same deal, SDL gives you an event when you need to draw.
Adequate event looop shoold be something like this:
while (!quit)
{
// get all events from event queue
while(SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
quit = true;
break;
}
}
// all your display 'redraw' stuff here
SDL_BlitSurface( gHelloWorld, NULL, gScreenSurface, NULL );
SDL_UpdateWindowSurface( gWindow );
}
If you really want SDL_WaitEvent instead, you should watch for SDL_WINDOWEVENT with SDL_WINDOWEVENT_EXPOSED and SDL_WINDOWEVENT_SHOWN flags as your signal to redraw.
Upvotes: 1