Painting algorithm Tilemap issue

Hi everyone,

I’m working on a tower defense project and i have the map already drawn in the screen. But now i’m having an issue when i place other images on top of the map tiles.

My painting algorithm is somethin like this one:

for(int i = 0; i < map.length; i++) {
for(int j = 0; j < map['i].length; i++) {

      switch(map['i][j].type) {
           case 0: draw normal tile.
           case 1: draw route tile.
           case 9: draw enemy image.
      }

}

}

So i was expecting case 9 to be drawn in top of the other ones (case 0 and case 1) and it worked until i moved (updated) the enemy position into a route tile. Route tile was on top of the enemy image.

I want to mention that the map is being repainted in the game loop.

How can i achieve that the enemy image is on top of some tiles?

Thanks in advance!

Very wisely chosen title for your post, as the “Painter’s Algorithm” is actually the solution to your problem. :slight_smile:

That being: First paint the things that should be in the background. Then paint the things in the foreground on top of that.

Apart from that, It seems not obvious why some tile can render two things in the wrong order, as it seems it can only contain one single type of something. I guess you have then something else rendered apart from your tile map?

I’m actually only drawing those things on each iteration of my game loop

In case 0: i draw a black rectangle
In case 1: i draw a green rectangle
In case 9: i draw an image

So in my case, the solution would be 2 painter algorithms? one that draws tiles and then another one that draws units? I see that this works but is this the common way?

Well, there are so many ways to do things, that I don’t want to impose one on you.
The only thing I’m saying is that you would want to draw the things that should be on top of other things last.
If you do not use a Z-buffer algorithm (like when using OpenGL/Direct3D), then the Painter’s Algorithm is your best choice.

I’m gonna search more info about painter’s algorithm so i can improve what i have.

Informing yourself about the principles of Computer Graphics I would generally consider a good thing to do.
But I doubt you would find anything particularly worldshaking about that simple “Painter’s Algorithm.” :slight_smile:

I mean, just be clear on that: Naturally, you need to draw things in the background first (your floor) and then afterwards on top of that your units.
The reason for that being that your renderer (one without “Z-buffer”) paints the things as you command it to.
If you paint B after A (“after” as in program execution order “after”) then clearly B will overwrite A’s image.

I understand the concept, but i fail to improve what i have or i only see a way to do it.

I mean if i place in my paintComponent() method one algorithm to draw case 0 and 1, wich are the map tiles (background), and then another algorithm to draw case 9 wich is the enemy unit, towers … it works perfectly.

But this results in going over each tile of the map twice in each rendering of the screen and i’m afraid this will cost much memory or cause performance issues in the future but i suppose the only way to know is to try.