Layered Surfaces

I’ve been trying every possible way to do this and I cant seem to find a way that works. What I want to do is have a surface I draw multiple other surfaces to. For example my surfaces are…

(I’m using all PNG images)

SDL_Surface* player;
SDL_Surface* layers[7];
SDL_Surface* screen;

I have screen set up correct I know this for a fact because I have it working for the rest of the game.

At this point I try to apply the seven layers in order onto the player surface. Then once I do that apply it to the screen and flip it.
However the program always crashes. I assume its a problem with trying to apply the multiple surfaces to the player surface.

You will need to post more code for us to see what might be causing the crash.

This seems like a very inefficient way of doing things though… As I
understand it, you’re blitting your sprites (or whatever) to each
layer, then blitting each layer (which is most likely the size of the
entire screen) to the player surface, which you’re then blitting to
the screen (instead of using SDL’s double buffering mechanism).
That’s at least 8 more very large blits than you actually need.On 5 November 2010 17:20, Durama wrote:

I’ve been trying every possible way to do this and I cant seem to find a way
that works. What I want to do is have a surface I draw multiple other
surfaces to. For example my surfaces are…

(I’m using all PNG images)

SDL_Surface* player;
SDL_Surface* layers[7];
SDL_Surface* screen;

I have screen set up correct I know this for a fact because I have it
working for the rest of the game.

At this point I try to apply the seven layers in order onto the player
surface. Then once I do that apply it to the screen and flip it.
However the program always crashes. I assume its a problem with trying to
apply the multiple surfaces to the player surface.

I’ve been trying every possible way to do this and I cant seem to find a
way that works. What I want to do is have a surface I draw multiple other
surfaces to. For example my surfaces are…

(I’m using all PNG images)

SDL_Surface* player;
SDL_Surface* layers[7];
SDL_Surface* screen;

I have screen set up correct I know this for a fact because I have it
working for the rest of the game.

One fundamental rule of programming: The fact that some code works in a
particular situation does not mean that the code is correct. :slight_smile:

At this point I try to apply the seven layers in order onto the player
surface. Then once I do that apply it to the screen and flip it. However
the program always crashes.

Why? Where?

Without some debugger output or some code to look at, I don’t think there’s
much point even trying to make a guess as to what’s going on.

I assume its a problem with trying to apply
the multiple surfaces to the player surface.

That, however, I would pretty much rule out directly, unless you’re using
custom blitter code, or bypassing the “official” SDL API. SDL blitters will
clip off-surface areas and all, so at worst, all a mistake there will give you
is an incorrect result on your screen.

Are you getting NULL or corrupt pointers in somewhere? Any other code that
might be corrupting data?On Friday 05 November 2010, at 22.20.27, “Durama” wrote:


//David Olofson - Consultant, Developer, Artist, Open Source Advocate

.— Games, examples, libraries, scripting, sound, music, graphics —.
| http://olofson.net http://olofsonarcade.com http://kobodeluxe.com |
’---------------------------------------------------------------------’

Ill hunt down the code in my project real quick.

Code:
for(int i =0; i<=6; i++){
Apply(layers[i],player,0,0,NULL);
}

void Apply(SDL_Surface* Source, SDL_Surface* dest, double x, double y, SDL_Rect* clip = NULL)
{
//set ofset
SDL_Rect Offset;
double(Offset.x = x),double( Offset.y = y);

//apply Actor to surface
SDL_BlitSurface(Source, clip, dest, &Offset);

}

That would pretty much be the only code for this feature besides creating the surfaces and setting them to NULL.
-Apply() is just the function I use for bliting through out the whole game code-

This seems like a very inefficient way of doing things though… As I
understand it, you’re blitting your sprites (or whatever) to each
layer, then blitting each layer (which is most likely the size of the
entire screen) to the player surface, which you’re then blitting to
the screen (instead of using SDL’s double buffering mechanism).
That’s at least 8 more very large blits than you actually need.

Well I’m not actually using it as a screen buffer. I’m try to add a feature in a game so you can customize your character. So lets say for example layer one is the base character then layer two would be an outfit and then layer three a hat. So I have multiple sprite sheets that that I want to combine into a single sheet after the user creates his character. Then I want to use that new “player” surface to blit from. So then I’d clip pieces from the player surface and use it as if it was an regularly loaded image from hard disk.

Quoth Durama , on 2010-11-05 16:00:56 -0700:

That would pretty much be the only code for this feature besides creating the surfaces and setting them to NULL.

And where and how do you create the surfaces? “Setting a surface to
NULL” makes no real sense, either, unless you mean “deallocating the
surface and then setting its pointer to NULL” (at which point you
can’t use it anymore, of course).

—> Drake Wilson

Ill hunt down the code in my project real quick.

Code:

for(int i =0; i<=6; i++){
Apply(layers[i],player,0,0,NULL);
}

void Apply(SDL_Surface* Source, SDL_Surface* dest, double x, double y,
SDL_Rect* clip = NULL)
{
//set ofset
SDL_Rect Offset;
double(Offset.x = x),double( Offset.y = y);

//apply Actor to surface
SDL_BlitSurface(Source, clip, dest, &Offset);
}

That would pretty much be the only code for this feature besides creating
the surfaces and setting them to NULL.
-Apply() is just the function I use for bliting through out the whole game
code-

Apply() is wrong, but harmlessly so.

double(Offset.x = x) converts the value in x to an int (implicitly), assigns
it to Offset.x, then converts the value in Offset.x to a double and discards
it. That last bit will probably be optimized out, but it’s still bad code.
You really shouldn’t be using double here at all. x and y should be int.

Most likely, the error you’re seeing here is because either one of your
layers or player is still NULL when you call Apply().On 5 November 2010 19:00, Durama wrote:

Hello,

Checkout one of my projects dedicated to it:
http://code.google.com/p/sdl-layer

SDL 1.2 only.

Best,

Julien

Le 5 nov. 2010 ? 23:24, sdl-request at lists.libsdl.org a ?crit :> Send SDL mailing list submissions to

sdl at lists.libsdl.org

To subscribe or unsubscribe via the World Wide Web, visit
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
or, via email, send a message with subject or body ‘help’ to
sdl-request at lists.libsdl.org

You can reach the person managing the list at
sdl-owner at lists.libsdl.org

When replying, please edit your Subject line so it is more specific
than “Re: Contents of SDL digest…”

Today’s Topics:

  1. Layered Surfaces (Durama)
  2. Re: Layered Surfaces (Kenneth Bull)
  3. Re: Layered Surfaces (David Olofson)
  4. Re: Rotating iPhone to landscape, mouse messed up? (Ian Mallett)
  5. Re: Rotating iPhone to landscape, mouse messed up? (Ken Rogoway)

Message: 1
Date: Fri, 05 Nov 2010 14:20:27 -0700
From: “Durama”
To: sdl at lists.libsdl.org
Subject: [SDL] Layered Surfaces
Message-ID: <1288992027.m2f.25936 at forums.libsdl.org>
Content-Type: text/plain; charset=“iso-8859-1”

I’ve been trying every possible way to do this and I cant seem to find a way that works. What I want to do is have a surface I draw multiple other surfaces to. For example my surfaces are…

(I’m using all PNG images)

SDL_Surface* player;
SDL_Surface* layers[7];
SDL_Surface* screen;

I have screen set up correct I know this for a fact because I have it working for the rest of the game.

At this point I try to apply the seven layers in order onto the player surface. Then once I do that apply it to the screen and flip it.
However the program always crashes. I assume its a problem with trying to apply the multiple surfaces to the player surface.

-------------- next part --------------
An HTML attachment was scrubbed…
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20101105/b6b01a43/attachment-0001.htm


Message: 2
Date: Fri, 5 Nov 2010 17:41:32 -0400
From: Kenneth Bull
To: sdl at lists.libsdl.org
Subject: Re: [SDL] Layered Surfaces
Message-ID:
<AANLkTiktvyyvb=S+K83XuJtU7KCM-m1XFdjHJXnFe1rJ at mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

On 5 November 2010 17:20, Durama wrote:

I’ve been trying every possible way to do this and I cant seem to find a way
that works. What I want to do is have a surface I draw multiple other
surfaces to. For example my surfaces are…

(I’m using all PNG images)

SDL_Surface* player;
SDL_Surface* layers[7];
SDL_Surface* screen;

I have screen set up correct I know this for a fact because I have it
working for the rest of the game.

At this point I try to apply the seven layers in order onto the player
surface. Then once I do that apply it to the screen and flip it.
However the program always crashes. I assume its a problem with trying to
apply the multiple surfaces to the player surface.

You will need to post more code for us to see what might be causing the crash.

This seems like a very inefficient way of doing things though… As I
understand it, you’re blitting your sprites (or whatever) to each
layer, then blitting each layer (which is most likely the size of the
entire screen) to the player surface, which you’re then blitting to
the screen (instead of using SDL’s double buffering mechanism).
That’s at least 8 more very large blits than you actually need.


Message: 3
Date: Fri, 5 Nov 2010 22:48:42 +0100
From: David Olofson
To: sdl at lists.libsdl.org
Subject: Re: [SDL] Layered Surfaces
Message-ID: <201011052248.42418.david at olofson.net>
Content-Type: text/plain; charset=“iso-8859-15”

On Friday 05 November 2010, at 22.20.27, “Durama” wrote:

I’ve been trying every possible way to do this and I cant seem to find a
way that works. What I want to do is have a surface I draw multiple other
surfaces to. For example my surfaces are…

(I’m using all PNG images)

SDL_Surface* player;
SDL_Surface* layers[7];
SDL_Surface* screen;

I have screen set up correct I know this for a fact because I have it
working for the rest of the game.

One fundamental rule of programming: The fact that some code works in a
particular situation does not mean that the code is correct. :slight_smile:

At this point I try to apply the seven layers in order onto the player
surface. Then once I do that apply it to the screen and flip it. However
the program always crashes.

Why? Where?

Without some debugger output or some code to look at, I don’t think there’s
much point even trying to make a guess as to what’s going on.

I assume its a problem with trying to apply
the multiple surfaces to the player surface.

That, however, I would pretty much rule out directly, unless you’re using
custom blitter code, or bypassing the “official” SDL API. SDL blitters will
clip off-surface areas and all, so at worst, all a mistake there will give you
is an incorrect result on your screen.

Are you getting NULL or corrupt pointers in somewhere? Any other code that
might be corrupting data?


//David Olofson - Consultant, Developer, Artist, Open Source Advocate

.— Games, examples, libraries, scripting, sound, music, graphics —.
| http://olofson.net http://olofsonarcade.com http://kobodeluxe.com |
’---------------------------------------------------------------------’
-------------- next part --------------
An HTML attachment was scrubbed…
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20101105/773e86ad/attachment-0001.htm


Message: 4
Date: Fri, 5 Nov 2010 16:15:34 -0600
From: Ian Mallett
To: sdl
Subject: Re: [SDL] Rotating iPhone to landscape, mouse messed up?
Message-ID:
<AANLkTikXo-=FO5PtX+VN6TLhUzDCwtVVtkTo3TFquc-+ at mail.gmail.com>
Content-Type: text/plain; charset=“iso-8859-1”

Hi,

I cannot profess knowledge about how iPhone development works, but you might
try rotating the modelview matrix by 90 instead of the projection matrix.
Put it directly after the glLoadIdentity() call. This should rotate your
world instead of doing something weird.

This isn’t really an SDL question though. Next time, ask some OpenGL
specific mailing list.

Thanks,
Ian
-------------- next part --------------
An HTML attachment was scrubbed…
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20101105/94c41ee5/attachment-0001.htm


Message: 5
Date: Fri, 5 Nov 2010 17:18:14 -0500
From: “Ken Rogoway”
To: “‘SDL Development List’”
Subject: Re: [SDL] Rotating iPhone to landscape, mouse messed up?
Message-ID: 062501cb7d37$57d23ba0$0776b2e0$@com
Content-Type: text/plain; charset=“us-ascii”

Actually Ian, this is a SDL question for a couple of reasons.

  1.  Handling Landscape mode has been on the iPhone task list for some
    

time.

  1.  One of the issues the poster listed was that his mouse input was
    

wrong. That should be handled by SDL, and is not an OpenGL issue.

Ken Rogoway

From: sdl-bounces at lists.libsdl.org [mailto:sdl-bounces at lists.libsdl.org] On
Behalf Of Ian Mallett
Sent: Friday, November 05, 2010 5:16 PM
To: sdl
Subject: Re: [SDL] Rotating iPhone to landscape, mouse messed up?

Hi,

I cannot profess knowledge about how iPhone development works, but you might
try rotating the modelview matrix by 90 instead of the projection matrix.
Put it directly after the glLoadIdentity() call. This should rotate your
world instead of doing something weird.

This isn’t really an SDL question though. Next time, ask some OpenGL
specific mailing list.

Thanks,
Ian

-------------- next part --------------
An HTML attachment was scrubbed…
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20101105/301f2806/attachment.htm



SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

End of SDL Digest, Vol 47, Issue 10


Julien CLEMENT
@Julien_Clement1

Kenneth Bull wrote:> On 5 November 2010 19:00, Durama <@Durama (@Durama)> wrote:

  Ill hunt down the code in my project real quick. 



Code:


for(int i =0; i<=6; i++){

? ?Apply(layers[i],player,0,0,NULL);
}

void Apply(SDL_Surface* Source, SDL_Surface* dest, double x, double y, SDL_Rect* clip = NULL)
{
? ?//set ofset
? ?SDL_Rect Offset;
? ?double(Offset.x = x),double( Offset.y = y);

? ?//apply Actor to surface
? ?SDL_BlitSurface(Source, clip, dest, &Offset);
}

That would pretty much be the only code for this feature besides creating the surfaces and setting them to NULL.
-Apply() is just the function I use for bliting through out the whole game code-

Apply() is wrong, but harmlessly so.

double(Offset.x = x) converts the value in x to an int (implicitly), assigns it to Offset.x, then converts the value in Offset.x to a double and discards it.? That last bit will probably be optimized out, but it’s still bad code.? You really shouldn’t be using double here at all.? x and y should be int.

Most likely, the error you’re seeing here is because either one of your layers or player is still NULL when you call Apply().

Thats It! found it thank you. I had to call SDL_CreateRGBSurface for the player surface

And if its okay to ask even though its off topic what would be the best way of doing the offset then? Then reason I have it that way is because if I don’t convert it one of my images doesn’t move on the screen smoothly which I’m guessing is because it has extra transparent pixels Surrounding the image.

That double() really does nothing, and the conversion from double to
int could be done just as easily when you call the Apply() function as
within it.
Most likely, the problem you were having is because a line like this:
Offset.x = x, Offset.y = y;

Is evaluated like this:
(Offset.x = (x, (Offset.y = y)));

Where the comma operator returns the second value, not the first, so
it works out to:
Offset.x = Offset.y = y;

Use a semicolon, not a comma. Like so:
Offset.x = x; Offset.y = y;On 5 November 2010 20:23, Durama wrote:

Thats It! found it thank you. I had to call SDL_CreateRGBSurface for the player surface

And if its okay to ask even though its off topic what would be the best way of doing the offset then? Then reason I have it that way is because if I don’t convert it one of my images doesn’t move on the screen smoothly which I’m guessing is because it has extra transparent pixels Surrounding the image.