Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Sprite layering  (Read 3627 times)

0 Members and 1 Guest are viewing this topic.

dotty

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
Sprite layering
« on: March 14, 2012, 09:14:06 pm »
Hay all, I've made a "SpriteManager" class which takes a bunch of "gameObjects" (which is a class which inherits from sf::Sprite) and sorts them by their zIndex. The SpriteManager then draws the gameObjects to the screen based on their zIndex.

This gives me a lot more control over the ordering of the sprites. However there still seems to be an issue where the ordering does sit right. It seems that the only way to do this correct is to sort them by how they appear on the screen. The sprites at the very top of the screen are drawn first and the ones at the bottom are drawn last.

Is this the only way to do this? What other ways are there to tackle this problem

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Sprite layering
« Reply #1 on: March 15, 2012, 05:22:50 pm »
Quote from: "dotty"
Hay all, I've made a "SpriteManager" class which takes a bunch of "gameObjects" (which is a class which inherits from sf::Sprite)
Is there a specific reason why you limit the Z ordering to custom classes? I mean, you could use sf::Drawable and work with anything that can be drawn (texts, shapes, custom drawables).

And yes, changing the drawing order is the only possibility with current SFML and without OpenGL.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Mjonir

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: Sprite layering
« Reply #2 on: March 15, 2012, 08:36:59 pm »
Quote from: "Nexus"
Is there a specific reason why you limit the Z ordering to custom classes? I mean, you could use sf::Drawable and work with anything that can be drawn (texts, shapes, custom drawables).


Speaking from experience, I don't think that's a good idea. Have a look at this recent topic (French forum).

To sum it up, Laurent commented on the issue: Base classes should not be used directly since he reserves the right to change them at any moment. One should at least build a layer of abstraction in order not to be too dependent upon that :)

dotty

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
Sprite layering
« Reply #3 on: March 16, 2012, 02:43:35 pm »
I decided to do away with the z-index ordering and programatically work out what sprite is at the top of the screen and draw them first, then come down the screen and then draw them secondly.

This works well and my character will now get draw correctly behind and in front of other sprites. However, this only works if my sprites are the same size as me character (32x32) if I use double height sprites (32x64) the object gets drawn top of my character if Im standing above the top half of the sprite.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Sprite layering
« Reply #4 on: March 16, 2012, 08:19:08 pm »
Mjonir, thanks for the link. I have already used sf::Drawable quite often. Honestly I am a little bit surprised Laurent doesn't intend the base classes to be used for user-side abstractions, I mean they're part of the public API. And as far as I remember, he planned not to break the API in SFML 2 so often ;)

Furthermore, dynamic_cast is mostly an indicator for bad design, here it wouldn't be necessary (since only the virtual draw() function of sf::Drawable is required).
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything