Repos

MELHARFI-2D-Game-Engine.

Got to repo.

02. Superposition and ZIndex

Superposition of layers

For any 2D game, you should notice that there is background, content and top layer:

BackgroundLayer, is the color or a background picture, it could be many assets as the first layer and it stand in the back (layer 1)

ObjectLayer like players, monsters... that come front of background (layer 2)

TopLayer, like Hud, Chat box, Buttons... (front of content layer 3)

The same thing is handled by this engine

FixedObjectLayer i add that layer to reference some object that i dont want them to be removed especially when cleaning screen, usefull when i want to redraw my screen or change it whitout touching some objects like hud which is static, that mean it's not a layer to draw objects but reference some object already drawn and not cosidered as a drawning layer therefore not superposed to the previouse layers.

ControlLayer in this layer we reference all Windows controls used like buttons, radio, checkbox, panel... it's necessary to reference them in case we want to hide a menu that thoses controls and we must keep a reference of them to ControlLayer to hide them or remove them, kkep in minde that those controls area always drawn in top even over TopLayer but excluded from superposition of other layers cause it store only Windows Controls not shapes.

Thoses layers are displayed in superposition, the BackgroundLayer come first, and ObjectLayer came over the background, and TopLayer is always displayed on the Top.

On each layer, you can add many item (image, shape) as you want.

For background layer maybe you have a color background, cloud, building...

For content layer you have your plane, and enemy planes that shoot you some missiles...

And finally you have TopLayer to draw everything on the top like some messages, chat, hud ...

  • Z-Index
  • Another arranging step is the Z-Index

    To do that you have to use what we call a Z-INDEX parameter, it's the depth of the object inside the layer itself, like the 3rd coordinate "3D" but we are doing this in 2D gaming, some is calling that 2.5 D lol

    In each Layer as Background or Object or Top could have many objects inside, so you need a way to arange them to make some superposed over the others, like building over cloud.

    To achieve this let's say you have 3 background objects.

    Background_Color, cloud, building

    To order them with z-index you should make

    Background_Color z-index = 0;

    Cloud z-index = 1;

    Building z-index = 2;

    And you increment each time you want an object to be over another object.

    In coding, those layers are only a List of objects,

    Background layer is represented as BackgroundLayer (Graphics Background List).

    Object layer is represented as ObjectLayer (Graphics Object List).

    Top layer is represented as TopLayer (Graphics Top List)

    Other extra layers has been added for some issues that we are going to see later called like

    FixedObjectLayer

    ControlLayer

    And Z-index is coded line this :

    label1.zindex = 1;

    While the zindex is higher, more it's on the top.

    When creating an object without attributing a value to it's zindex, it's automatically attributed as the higher value to make it in the very top on the screen.

    So you don't need to affect the Z-index to an object eache time as it's automatically incremented when a new object is created UNLESS if you want to make an object created at last be behind another object created from the begining, so in that case you should initialize the z-index of the last object to be less than the first one like

                            
    Bmp building = new Bmp(@"c:\building.jpg", new Point(0, 0), manager);
    Bmp cloud = new Bmp(@"c:\cloud.jpg", new Point(0, 0), manager);
    cloud.Zindex = building.Zndex - 1;
                            
                          

    Services

    App Developpement

    Want me to dev an app for you ? dont hesitate to contact me.

    Programming

    Are you looking for a coder/teammate for your project ? Let's give it a try.

    Job Invitation

    Have a proposal for me ? we can discuss about it.

    Donation maybe ?!

    You want to buy me a coffe ? m.elharfi@gmail.com