March 28, 2024, 11:08:09 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Z order of Sprites

Started by pistol350, August 05, 2008, 04:46:01 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

pistol350

Does anyone have any idea on how to handle Z order drawing of sprites ?
I've googled the keywords but can't find any useful info, at least useful for a 2D newbie.
Any suggestions please ?
Regards,

Peter B.

J B Wood (Zumwalt)

August 05, 2008, 05:22:06 PM #1 Last Edit: August 05, 2008, 05:23:49 PM by Jonathan (zumwalt) Wood
Z order is more of an illusion.
Ok let me try to explain.
You create your sprite, load it per say, then assign a structure to it.
This structure then tells it what effect is and how.
What most programs do, is alter the code for a sprite and include a basic altered structure to it.
This structure then has a "layer" mode for the sprite, giving the sprite the illusion of a Z order.
Sprite.ZOrder = 1
This for instance would be Sprite structure, set value of ZOrder to 1.
All items in ZOrder 1 know to collide with each other.
Make sense?

http://www.ionicwind.com/forums/index.php/topic,1091.0.html

pistol350

August 06, 2008, 01:54:07 AM #2 Last Edit: August 06, 2008, 02:21:40 AM by pistol350
Yes it does make sense.
I knew of your space invaders clone game but had not really studied the code yet.
I guess it's time to do it.
I'll see what i can come up with.
Thank you Jonathan!

Edit :
Ouch! That won't be an easy task!  :-\

First because the code for the game is quite long (around 1641 lines of codes)
Second, playing the game i don't see anything like sprites changing order, so that does not help to focus on a given part of the code.
Third, i don't see anything in the Sprites structures related to Z order or at least said with words i can quickly get my eyes on.  ::)

Any suggestions on where to start or on which part of the code to focus ?
Thanks again.

By the way, i found that link in CodeProject, and it does exactly what i want do to.
the only issue for me is that the code is in C sharp (C #)
Unfortunately, I'm not used to that language yet, but i guess it won't be that hard to grab the parts of the code i'm interested in.

http://www.codeproject.com/KB/GDI-plus/sprites.aspx
Regards,

Peter B.

pistol350

I decided to try and do it by myself but things seems to be harder than i thought.

First, peeping at Aurora source, and after analyzing the C2DSprite Class, i found the m_PosX, m_PosY and m_PosZ member variables.
I then thought that i could play with the latter to get what i wanted.


C2DSprite *mySprite;
mySprite = new(C2DSprite,1);

mySprite->m_PosX=10; // member variable of Sprite Class
mySprite->m_PosY=400; // member variable of Sprite Class
mySprite->m_PosZ=0.0; // member variable of Sprite Class


But how can i update the changes of position in the Z buffer ?

Basing on what i see in the source, The Render() and RenderXY() functions do not seem to do the job.
So i guess i need to write the drawing routine myself right ? Or maybe i got it all wrong.
Any suggestions please ?

Regards,

Peter B.

Ionic Wind Support Team

Peter,
There is no Z buffer in 2D graphics.  The member variable is user defined and you can use it in whatever way you wish.

To simulate Z order you do what Windows does, sprites drawn first will appear on the bottom of sprites drawn later. 

Paul.
Ionic Wind Support Team

J B Wood (Zumwalt)

August 08, 2008, 10:20:21 PM #5 Last Edit: August 08, 2008, 10:22:29 PM by Jonathan (zumwalt) Wood
I'm out of town at the moment or I would have programmed up something fast for you on this.
As Paul has pointed out, sprites are a FILO method for drawing, first in, last out.
Once you load in your sprite collection, you render them in order you wish for them to draw.
This is where the layering concept comes into play, you can give a new value to a sprite structure that you create on your own, call it depth if you like, then anything on depth 0 you call its render first, then on 1, those next, so on and so forth.
Even at layer 0 though, they are drawn in order which you call them. So say you have an array:

PSEUDO: (NOT REAL CODE JUST EXAMPLE)
Sprite[][] MySprites = new Sprite[4][4];
Sprite[0][0] = new Sprite("sprite file");
etc...
While Rendering Sprites
{
    for(int a=0;a<4;a++)
    {
        for(int y=0;y<4;y++)
        {
             DrawSprites(MySprites[a][y]);
         }
     }
}

Now what this pseudo code represents, is that for all sprites in sprite array element 0, draw its sprites 0-3, repeat process from 1-3 for all the children sprites.

Now, if you have a structure:

struct spriteStruct
{
    Sprite mySprite;
    int layer;
}

You could then do something like:
spriteStruct theSprite=new spriteStruct();
theSprite.mySprite=Sprite("some sprite");
theSprite.layer=0;

You could then place that into an array or pointer list, then walk through the list, for all sprites in the collection, render the layers in order.
Layering in sprites is truely an illusion.
FILO

(NOTE: Again, the code here is not real Aurora code, this is for information only, I am on a business trip without Aurora on this work machine)


pistol350

Quote from: Paul Turley on August 07, 2008, 09:41:22 AM
Peter,
There is no Z buffer in 2D graphics.  The member variable is user defined and you can use it in whatever way you wish.

To simulate Z order you do what Windows does, sprites drawn first will appear on the bottom of sprites drawn later. 

Paul.

Thank you about the Clarification.


@Jonathan

I understand the idea as a whole but i still can't right anything.
I'll take a close look to your explanation and pseudo code to have a better understanding of it.

Thanks again.
Regards,

Peter B.