Learning C++ I created a class to draw pictures. Now I want to use SDL
to make them visible. But unfortunately what I programmed doesn’t work.
It just shows a black window, but the ‘shots’ created at the end of the
program do show what I expect. Since several people weren’t able to help
me with that I’m asking you to have a look at
You’re overwriting the “display” variable with a different SDL_Surface
pointer, so you can’t get pixels to the video hardware anymore.
Change the line with this…
display=SDL_CreateRGBSurfaceFrom(
…to this…
SDL_Surface *xdisplay=SDL_CreateRGBSurfaceFrom(
…so that (display) is still the screen surface. (feel free to pick a
better name than “xdisplay” too.)
Then, you can move the pixels from your new surface to the screen
surface, converting formats on the fly if necessary, right before your
call to SDL_Flip()…
SDL_BlitSurface(xdisplay, NULL, display, NULL);
…now that SDL_Flip() call will get the pixels from your screen surface
to video hardware. That change made the program work here.
After blitting the surface, you can SDL_FreeSurface(xdisplay) to prevent
a memory leak. And don’t call SDL_FreeSurface() on the screen surface:
SDL will clean this up when you call SDL_Quit(), which you should do
before your final “return 0;”.
Optimizations for the future experimentation: keep a single surface
around and using SDL_LockSurface() before copying pixels right into it
instead of generating a new one each frame, or if you don’t need to
convert formats, just lock the screen surface and write directly into
there before flipping, without a second surface at all (otherwise, the
SDL_BlitSurface() call will take care of that for you).
(and all of this is a totally different interface in SDL2, but that’s
how you’d do it with SDL 1.2, which is still fun to learn with!)
Good luck,
–ryan.