HaxeFlixel / flixel

Free, cross-platform 2D game engine powered by Haxe and OpenFL
https://haxeflixel.com/
MIT License
1.98k stars 439 forks source link

Limitation for cpp layers over being able to sort depths of objects by a property #155

Closed Beeblerox closed 11 years ago

Beeblerox commented 11 years ago

possible solution is a "DrawStack" system, but it's another damn bunch of work, plus it could be slower. I'll experiment with this after v1.07 release

Beeblerox commented 11 years ago

This "DrawStack" system will replace "Layer" system

Beeblerox commented 11 years ago

StackItem will have following properties:

atlas:Atlas;
colored:Bool;
blending:Int;
drawData:Array<Float>;
position:Int;
cameraID:Int; // not sure about this
Beeblerox commented 11 years ago

This new "DrawStack" system will draw objects in the same order as they appear in FlxState's members array. The number of drawTiles() calls will be equal to the number of StackItem objects. New StackItem will be added to the draw stack if the properties of previous StackItem do not meet the requirements of the current object: different atlas, blending or colored flag.

Beeblerox commented 11 years ago

Of course, there will be StackItem recycling

Beeblerox commented 11 years ago

And I forgot to say that FlxBasic objects will have _atlas property instead of _layer.

bradparks commented 11 years ago

hey Zaphod.... kind of unrelated.... but do you work on HaxeFlixel in your day job?

On Fri, Nov 9, 2012 at 8:25 AM, Zaphod notifications@github.com wrote:

And I forgot to say that FlxBasic objects will have _atlas property instead of _layer.

— Reply to this email directly or view it on GitHubhttps://github.com/Beeblerox/HaxeFlixel/issues/155#issuecomment-10224847.

Beeblerox commented 11 years ago

@bradparks No, it's my hobby project :)

bradparks commented 11 years ago

So have you released any games using it yourself? It seems like you put in tons of time on it (thanks!) and that you're really experienced with Flixel, game engines, and development ;-)

Thanks again for all your great work.... it's inspiring and very useful....

On Fri, Nov 9, 2012 at 9:13 AM, Zaphod notifications@github.com wrote:

@bradparks https://github.com/bradparks No, it's my hobby project :)

— Reply to this email directly or view it on GitHubhttps://github.com/Beeblerox/HaxeFlixel/issues/155#issuecomment-10225960.

Beeblerox commented 11 years ago

@bradparks actually I haven't released any game at all. I just like to dig in the guts of the engine. I know that's strange ;)

bradparks commented 11 years ago

cool.... well it definitely seems like you know lots about it, so you're investments paying off!

I'll definitely let you know if I do release a game with it, and if you don't mind, I'll at least put you and HaxeFlixel in the credits....

On Fri, Nov 9, 2012 at 11:35 AM, Zaphod notifications@github.com wrote:

@bradparks https://github.com/bradparks actually I haven't released any game at all. I just like to dig in the guts of the engine. I know that's strange ;)

— Reply to this email directly or view it on GitHubhttps://github.com/Beeblerox/HaxeFlixel/issues/155#issuecomment-10232977.

Beeblerox commented 11 years ago

@bradparks That would be great!

impaler commented 11 years ago

Thanks Beeblerox that sounds like it will enable us to do sorting again. The reason I asked you before if _layers could be in FlxGroup instead of FlxState like members are is to replicate this render order in cpp. For the flash target I just tested this and it works well.


        add(new FlxSprite().makeGraphic(FlxG.width,FlxG.height,FlxColor.getRandomColor()));

        gameObjects = new FlxGroup();
        for ( i in 0...30 ) {
            var object = new FlxSprite().makeGraphic(40,40,FlxColor.getRandomColor());
            gameObjects.add(object);
            object.y =i * (object.height)-i*10;
            object.x =i * (object.width)-i*10;
        }
        add(gameObjects);

        var UI = new FlxGroup();
        UI.add(new FlxSprite().makeGraphic(300,40));
        UI.add(new FlxSprite().makeGraphic(40,40,FlxColor.getRandomColor()));
        add(UI);

// put in override of update
//only sort the draw order of a particular FlxGroup so that BG and UI stay on top or bottom
        gameObjects.sort("y",1);
// or       gameObjects.sort("y",-1);

If you still use FlxState for _layers sorting you wont be able to sort only a group of things like above as the UI depth will be changed too?

Beeblerox commented 11 years ago

@impaler With layer system you can try to do the same. But in your case there are too much sprites for 1024*1024 atlas (you can try to use bigger atlas but it would not work on particular mobile devices). Here is "workaround":

#if (cpp || neko)
var gameObjectsLayer = new FlxLayer("gameObjects");
gameObjectsLayer.atlas = FlxLayer.createAtlas(1024, 1024, "gameObjects");
addLayer(gameObjectsLayer);
#end

add(new FlxSprite().makeGraphic(FlxG.width,FlxG.height,FlxColor.getRandomColor()));

gameObjects = new FlxGroup();
for ( i in 0...30 ) {
    // I decreased the size of sprite to fit in the atlas   
    var object = new FlxSprite().makeGraphic(30,40,FlxColor.getRandomColor());
    gameObjects.add(object);
    object.y =i * (object.height)-i*10;
    object.x =i * (object.width)-i*10;
    gameObjectsLayer.add(object);
}
add(gameObjects);

var UI = new FlxGroup();
UI.add(new FlxSprite().makeGraphic(300,40));
UI.add(new FlxSprite().makeGraphic(40,40,FlxColor.getRandomColor()));
add(UI);

// put in override of update
//only sort the draw order of a particular FlxGroup so that BG and UI stay on top or bottom
gameObjects.sort("y",1);
// or       gameObjects.sort("y",-1);

There is 2 determining factors for draw order:

  1. order in FlxState's members array
  2. order of object's layer in _layers array.

So if all sorted objects are in the same layer, they will be drawn in the same order as they appear in members array in one draw call.

impaler commented 11 years ago

@Beeblerox thanks I overlooked this, it works like you suggested just had to add bg first. Its not that much of a workaround.

Beeblerox commented 11 years ago

I need to write some numbers somewhere to compare performance after changes related to this task. So this place looks good for it. In "High perfomance" mode on my machine I can get (before I made any changes): 10000 bunnies at 59 fps 15000 bunnies at 51 fps 20000 bunnies at 41 fps 25000 bunnies at 33 fps 30000 bunnies at 28 fps Target fps is set to 60 in all cases

Beeblerox commented 11 years ago

with new render system i get: 10000 bunnies at 59 fps 15000 bunnies at 47 fps 20000 bunnies at 38 fps 25000 bunnies at 30 fps 30000 bunnies at 25 fps I think that this little performance drop is acceptable, so i'll merge it to master branch soon

Beeblerox commented 11 years ago

latest release (v 1.08) fixes this issue