Surface copying not working

Helo
I have a function in my program that will copy a “piece” of a
surface, to another surface. I dont know if im not using SDL_BlitSurface the
correct way for copying off screen surfaces, or if its a C++ problem (it
doesnt seem to be), or what.

As i dont have yet a good knowlage of debugging, i used a kind of “log
file” where i put the values of some variables. That way i found the
error… but i dont know how to solve it.

Here is my function, commented (without the log file stuff)

int CSprite::load(CSpriteBase *base, SDL_Surface *screen)
{
cBase = base; // checked, and not NULL
if (cBase->aBuilt) // checked and its “1” (built)
{
SDL_Rect src;
src.y = cBase->Anim[cFrame].aY; // checked, aY = 300;
src.x = cBase->Anim[cFrame].aX; // checked, aX = 250;
src.w = cBase->aW; // checked, aW = 34;
src.h = cBase->aH; // checked, aH = 96;

          // OK, cBase->frame is a BMP with some frames for my sprite.
          // its checked, and it loads the file perfectly, and shows the

cFrame
// current frame on the screen.
// So… now I want to put the piece of that frame (first
frame)
// in BackReplace surface. As it will be the first thing i
will “replace”.

           SDL_BlitSurface(cBase->frame, &src, BackReplace, NULL);

           if (BackReplace == NULL)
              return -1;
           // and as i check here, BackReplace is not been loaded : /
           cScreen = screen;
 }

return 0;
}

I also checked almost everything on my code… it seems to be ok,
everthing
works fine! BUT, the only problem is that BackReplace wont get loaded.
I Also use BackReplace on some other functions to replace a piece of the
background, and it doesnt works either.

I think that the probelm may be the SDL_BlitSurface function im using.

May be theres an other way to do that ? I mean, to copy a piece of the
surface?
Remember that cBAse->frame is ONE surface, with ONE BIG BMP file, that has
some frames for my sprite animation.

The program seems to work perfectly if I use separated BMP files and
Surfaces for each frame, and in the function i wrote, using
SDL_DisplayFormat.

Thanks in advance for any help!

                       Eduardo Garcia Rajo (h)------------------------------------------------------------------

Visite: http://www.solucion-digital.com.ar
SOLUCION DIGITAL
Redes - Software - Servicios

Did you create the BackReplace surface elsewhere? You should make it
with SDL_CreateRGBSurface before using it in a blit. The
SDL_BlitSurface only copies from one existing surface to another
existing surface, it won’t create surfaces for you. I think what you
want is to create the BackReplace surface in your program initialization
(like after you make the screen surface) with the appropriate
dimensions. Eg:

BackReplace = SDL_CreateRGBSurface(flags, width, height, …);
if(BackReplace == NULL)
{
// Display error message
}

(Check the man page on it, there’s a bunch of options)

Then in your blit function you would have:

if(SDL_BlitSurface(cBase->frame, &src, BackReplace, NULL) == -1)
return -1;

As far as I know, there is no reason to check if BackReplace is NULL
here (assuming it was created properly), since it is not changed by
BlitSurface.

-Mike ShalOn Tue, 2003-06-10 at 19:13, eDU! wrote:

Helo
I have a function in my program that will copy a “piece” of a
surface, to another surface. I dont know if im not using SDL_BlitSurface the
correct way for copying off screen surfaces, or if its a C++ problem (it
doesnt seem to be), or what.

As i dont have yet a good knowlage of debugging, i used a kind of “log
file” where i put the values of some variables. That way i found the
error… but i dont know how to solve it.

Here is my function, commented (without the log file stuff)

int CSprite::load(CSpriteBase *base, SDL_Surface *screen)
{
cBase = base; // checked, and not NULL
if (cBase->aBuilt) // checked and its “1” (built)
{
SDL_Rect src;
src.y = cBase->Anim[cFrame].aY; // checked, aY = 300;
src.x = cBase->Anim[cFrame].aX; // checked, aX = 250;
src.w = cBase->aW; // checked, aW = 34;
src.h = cBase->aH; // checked, aH = 96;

          // OK, cBase->frame is a BMP with some frames for my sprite.
          // its checked, and it loads the file perfectly, and shows the

cFrame
// current frame on the screen.
// So… now I want to put the piece of that frame (first
frame)
// in BackReplace surface. As it will be the first thing i
will “replace”.

           SDL_BlitSurface(cBase->frame, &src, BackReplace, NULL);

           if (BackReplace == NULL)
              return -1;
           // and as i check here, BackReplace is not been loaded : /
           cScreen = screen;
 }

return 0;
}

I also checked almost everything on my code… it seems to be ok,
everthing
works fine! BUT, the only problem is that BackReplace wont get loaded.
I Also use BackReplace on some other functions to replace a piece of the
background, and it doesnt works either.

I think that the probelm may be the SDL_BlitSurface function im using.

May be theres an other way to do that ? I mean, to copy a piece of the
surface?
Remember that cBAse->frame is ONE surface, with ONE BIG BMP file, that has
some frames for my sprite animation.

The program seems to work perfectly if I use separated BMP files and
Surfaces for each frame, and in the function i wrote, using
SDL_DisplayFormat.

Thanks in advance for any help!

                       Eduardo Garcia Rajo (h)

Visite: http://www.solucion-digital.com.ar
SOLUCION DIGITAL
Redes - Software - Servicios


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Helo!

First of all, Thanks u very much Mike! You reply has been REALLY useful!

As you said, the problem was that i was not initializating the Surface!
(sorry im still very new in SDL :stuck_out_tongue: )

So, i searched the man page and did initializate the BackReplace with
CreateRGBSurface and worked perfectly! :slight_smile: (phew, like one week searching for
that error! ).

And as I saw, the if (BackReplace==NULL) worked just fine, now the program
wont stop when checking the BackRepalce surface.

I also played around a little with the Alpha thing… i wonder where could i
read more detailed information, in general (not SDL only) about alpha
blending. May be i can give some good effects with it rght?

Again, thanks for your reply!

bye!

                                       Eduardo Garcia Rajo (h)------------------------------------------------------------------

Visite: http://www.solucion-digital.com.ar
SOLUCION DIGITAL
Redes - Software - Servicios

----- Original Message -----
From: marf@psu.edu (Mike Shal)
To:
Sent: Tuesday, June 10, 2003 8:34 PM
Subject: Re: [SDL] Surface copying not working

[…]

I also played around a little with the Alpha thing… i wonder
where could i read more detailed information, in general (not SDL
only) about alpha blending. May be i can give some good effects
with it rght?

I dunno, maybe you could find something related to image processing
with applications like GIMP, PhotoShop and the like. It’s used a lot
in OpenGL applications as well; at least in reasonably modern games.

Anyway, there isn’t much to it, really. Alpha is effectively the
balance between two images, and can (in SDL) be applied per-pixel
(alpha channel; ie RGBA surface) or per-surface (the surface alpha
value). Sweeping the alpha value gives you a cross-fade. Alpha is
useful for antialiasing and translucency effects, and it’s also
possible to abuse it for shadows and highlights in some special
cases. (Shadows must be black and highlighs must be white.)

That’s about it, though. SDL supports only alpha blending, unless you
use OpenGL for rendering. For general lighting effects (lightmaps,
coronas, bumpmapping etc) you’ll need other types of blending for
correct, good looking results. Additive (explosions, coronas etc) and
multiplicative (lightmaps) blending are the most commonly used
variants, and just like alpha blending, they’re rather intuitive from
the content creator POV.

Additive blending just adds the values of the source to the target,
making everything lighter. A black source does nothing. A gray source
makes the target very bright and “foggy”. A white source saturates to
white regardless of the target colors.

Multiplicative blending scales the target values with the source
values. A white source does nothing. (1 * x == x) A gray source
darkens the target to 50% intensity. A black source “mutes” the
target to all black. (0 * x = 0)

//David Olofson - Programmer, Composer, Open Source Advocate

.- The Return of Audiality! --------------------------------.
| Free/Open Source Audio Engine for use in Games or Studio. |
| RT and off-line synth. Scripting. Sample accurate timing. |
`-----------------------------------> http://audiality.org -’
http://olofson.nethttp://www.reologica.se —On Wednesday 11 June 2003 09.03, eDU! wrote:

David thanks a lot for explanation about alpha!

Its very clear now. In the moment i finish the base of my little first game
i will start searching about using it so I can give some good effects to it
:slight_smile:

bye!

                       Eduardo Garcia Rajo (h)------------------------------------------------------------------

Visite: http://www.solucion-digital.com.ar
SOLUCION DIGITAL
Redes - Software - Servicios

----- Original Message -----
From: david@olofson.net (David Olofson)
To:
Sent: Wednesday, June 11, 2003 4:51 AM
Subject: Re: [SDL] Surface copying not working

On Wednesday 11 June 2003 09.03, eDU! wrote:
[…]

I also played around a little with the Alpha thing… i wonder
where could i read more detailed information, in general (not SDL
only) about alpha blending. May be i can give some good effects
with it rght?

I dunno, maybe you could find something related to image processing
with applications like GIMP, PhotoShop and the like. It’s used a lot
in OpenGL applications as well; at least in reasonably modern games.

Anyway, there isn’t much to it, really. Alpha is effectively the
balance between two images, and can (in SDL) be applied per-pixel
(alpha channel; ie RGBA surface) or per-surface (the surface alpha
value). Sweeping the alpha value gives you a cross-fade. Alpha is
useful for antialiasing and translucency effects, and it’s also
possible to abuse it for shadows and highlights in some special
cases. (Shadows must be black and highlighs must be white.)

That’s about it, though. SDL supports only alpha blending, unless you
use OpenGL for rendering. For general lighting effects (lightmaps,
coronas, bumpmapping etc) you’ll need other types of blending for
correct, good looking results. Additive (explosions, coronas etc) and
multiplicative (lightmaps) blending are the most commonly used
variants, and just like alpha blending, they’re rather intuitive from
the content creator POV.

Additive blending just adds the values of the source to the target,
making everything lighter. A black source does nothing. A gray source
makes the target very bright and “foggy”. A white source saturates to
white regardless of the target colors.

Multiplicative blending scales the target values with the source
values. A white source does nothing. (1 * x == x) A gray source
darkens the target to 50% intensity. A black source “mutes” the
target to all black. (0 * x = 0)

file://David Olofson - Programmer, Composer, Open Source Advocate

.- The Return of Audiality! --------------------------------.
| Free/Open Source Audio Engine for use in Games or Studio. |
| RT and off-line synth. Scripting. Sample accurate timing. |
`-----------------------------------> http://audiality.org -’
http://olofson.nethttp://www.reologica.se


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl