SDL_CreateRGBSurfaceFrom() doesn't work?!

Hi there,

first of all: English is not my mother’s tongue.

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

http://test.szaktilla.de/sdltest-0.43.tbz

and help me.

Thanks a lot!

Gregor

What compiler / IDE are you using? If you’re using Visual Studio, would it be possible for me to have the whole project? If I’m gonna be able to help you, I’m gonna need to do some breakpoint’ing in your code.

Naith wrote:

What compiler / IDE are you using? If you’re using Visual Studio, would it be possible for me to have the whole project? If I’m gonna be able to help you, I’m gonna need to do some breakpoint’ing in your code.

I am using g++ (Debian 4.7.2-5) 4.7.2 and binutils 2.22-8.

The files in http://test.szaktilla.de/sdltest-0.43.tbz are the whole project. I wrote this just for trying to ?connect? my own stuff to SDL (and therfore having problems with SDL_CreateRGBSurfaceFrom())

Thanks for helping!

Gregor

Alright.

One question about your code: is there any specific reason why you’re using the SDL_CreateRGBSurfaceFrom-function to render the circle onto the screen surface?

What I usually do (before I changed to SDL2 and started working with textures instead, I mean) was to create all the surfaces I need (in your case a screen surface and a surface containing the circle) and then, in the while loop, render the circle onto the surface and execute SDL_Flip to render everything to the window / screen.

I’m not an expert on the SDL_CreateRGBSurfaceFrom-function but as I understand it, you’re re-creating the screen surface every frame, with the new pixel data (‘ptr’ in your code) and that can’t be too effective (memory wise). I might be wrong about that though, since I’m, like I said, is not an expert on the CreateRGBSurfaceFrom-function.

So yeah, I’m just wondering why you’re doing it like that instead of creating the screen surface and the other surface(s) before the main loop starts, render the surface(s) to the screen surface and then SDL_Flip to render everything to the window / screen.

Naith wrote:

One question about your code: is there any specific reason why you’re using the SDL_CreateRGBSurfaceFrom-function to render the circle onto the screen surface?

What I did (over the past years, I?m not an professional programmer) is a class that handles Pixels. If everyting is done one can save that as an TGA file. The problem I have lies in providing SDL_CreateRGBSurfaceFrom() with the right parameters.

My image-class is why I?m doing things this way. I first create an image using my stuff and then (want to) show it on the screen.

Gregor

gregors wrote:

Naith wrote:

One question about your code: is there any specific reason why you’re using the SDL_CreateRGBSurfaceFrom-function to render the circle onto the screen surface?

What I did (over the past years, I?m not an professional programmer) is a class that handles Pixels. If everyting is done one can save that as an TGA file. The problem I have lies in providing SDL_CreateRGBSurfaceFrom() with the right parameters.

My image-class is why I?m doing things this way. I first create an image using my stuff and then (want to) show it on the screen.

Gregor

Some additional information: My ?project? is makefile based (I don?t use configure). If you extract the archive, cd into it and run make test everything should be done the way one might expect (compilation-running-displaying).

Gregor

Hi,

When running ?make test?, after building ?

cat test.tga | tgatoppm | ppmtobmp -bpp=24 > test.bmp
/bin/sh: tgatoppm: command not found
/bin/sh: ppmtobmp: command not found

Where do these two external commands come from?

At any rate, SDL_CreateRGBSurfaceFrom arguments are indeed tricky to get just right. You might take a look at the test directory of the SDL source tree, and find tests that use this function to get an idea at how to best utilize it.

Personally, I often find that I have the pitch parameter wrong :slight_smile: I?ve used the function in the past for things like scale2x algorithm ? in fact, you might want to take a look at the contrib directory in the source tree of scale2x. It has source for using the algorithm with SDL. If memory serves me right, it uses SDL_CreateRGBSurfaceFrom.

Cheers,
Jeffrey Carpenter <@Jeffrey_Carpenter>

-------------- next part --------------
A non-text attachment was scrubbed…
Name: smime.p7s
Type: application/pkcs7-signature
Size: 1572 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20141203/6299ce41/attachment-0001.bin

Then I understand.

I’m afraid I can’t help you much with the problem. The only thing I can say is that my guess is that it’s something with the SDL_CreateSurfaceFrom-function and ‘ptr’ that you’re passing to it.

Sorry I can’t do more to help.

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.