7. Entity Systems, drawing trees and static tiles

In my last post I talked about the edge bleeding I had between the tiles and how I solved that. I also mentioned the drawing of trees. In the last 2 days I looked at ways to draw trees, spent loads of time on fixing transparency issues (more about that below) and some small drawing improvements.

Drawing trees

I originally planned on drawing trees and other resources using a separate layer for those while also creating several tiles that combines 2 trees for example (so I do not need loads of layers). One of the issues I ran into was the fact that I wasn't able to easily change locations etc. Another issue was that I wasn't quite sure how to change the tiles/info about the tree once they were removed. Using objects was the way to go so I added it to my EntityFactory and let my SpriteRenderingSystem do the job but this didn't turn out how I wanted it. The idea was to draw the trees ABOVE the enemies so they could walk behind it, but that didn't work. I created a new System that rendered the resources to solve it. (Also tried Ashley, another entity system, but reverted back to Artemis cause it wasn't that much faster. May revert back later).

Next issues: the transparent shadow of the trees is gone!

Transparency issues

Somehow the shadows no longer worked. In the images below you can see the result, the way it should be and the editor version. For about 10 hours I have been looking at my code, trying all sorts of OpenGL stuff, Googling, changing rendering methods, getting rid of a duplicate SpriteBatch but none of that worked. My last option was checking whether TextureRegions were the issue so I drew the entire texture manually, which seemed to work. I then replaced that by a TextureRegion, which also worked. So now I have the situation in which manual creation of trees works, but using a loop to do so does not, odd. I skimmed through my loop and finally noticed that I placed FOUR trees on each spot (because I decided to use a loop instead of lots of manual calls some time ago). O lord, how stupid. Removing three of them fixed the issue. Now I at least know that my code can handle 4 times the current amount of trees without breaking a sweat, though.

Faulty transparency

Editor version

Correct shadow

Small drawing improvements

Performance is something I have some problems with but I notice that I spend more and more time on writing more efficient code or ways to do something. My Tiled map consisted of several layers, but after deciding to draw the trees and other objects that the player interacts with separately from LibGDX' TiledMapRenderer it now only consists of 1, maybe 2 layers. More importantly, those layers are static and do not require changes at all (for now at least). While fixing the transparency issues I noticed that OrthogonalTiledMapRenderer uses a SpriteBatch to do its drawing and I figured a SpriteCache would be more efficient (remember one of my earlier blogs?). I asked this question in LibGDX' IRC channel and Nexsoftware happened so have one laying around which I was free to use. After hooking it up (i.e. change 2 lines) I took it for a test drive. For 1 layer drawing my fps went from 1300 to 2200 (desktop) and 75 -> 150 on my Desire HD. Drawing 4 layers hardly reduced that at all, some nice coding if you ask me.

I also got the tip of using frame buffer textures, which I will look into later.

Up next

My next objective is to try some Scene2D stuff and play with the GUI a little.

comments powered by Disqus