Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (579)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  best way to update screen  (Read 806 times)
0 Members and 1 Guest are viewing this topic.
Offline ricardo

Senior Member


Medals: 2
Projects: 3



« Posted 2013-10-27 17:06:58 »

What is the best practice for tile based game, update all visible tiles on screen or update only tiles that need to be repainted?
If I update all the visible tiles my game will have more consistent frame rate, but if I update only tiles that need to be repainted game will have more performance because doesn't need to update all tiles over and over.
If the second option is better, what is the best way to check if tiles need to be repainted?
Offline Troubleshoots

JGO Coder


Medals: 35
Exp: 7-9 months


Damn maths.


« Reply #1 - Posted 2013-10-27 17:30:25 »

It depends.
By all visible tiles on screen I assume you're referring to the whole map? There are a couple of things you need to think about:

  • The size of your map
  • Whether you need better performance

If you don't need to increase performance, don't do it, unless you're a perfectionist like me. As the saying goes, if it doesn't need fixing, don't fix it. However, the second option is obviously the best in terms of performance. As it happens, a few days ago I created my own algorithm to calculate the visible tiles on the screen using Java2D. It isn't very good, but hopefully it could help you.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
public void drawMap(Graphics2D g2d) {
      int minOffsetX = 0, minOffsetY = 0;
      int maxOffsetX = (mapX - Frame.CANVAS_WIDTH);
      int maxOffsetY = (mapY - Frame.CANVAS_HEIGHT);
      int cameraX = (int) (Player.getX() - (Frame.CANVAS_WIDTH / 2));
      int cameraY = (int) (Player.getY() - (Frame.CANVAS_HEIGHT / 2));
      if(cameraX < minOffsetX)
         cameraX = minOffsetX;
      if(cameraY < minOffsetY)
         cameraY = minOffsetY;
      if(cameraX > maxOffsetX)
         cameraX = maxOffsetX;
      if(cameraY > maxOffsetY)
         cameraY = maxOffsetY;
     
      int tileX = cameraX, tileY = cameraY;
      while(tileX % TILE_SIZE != 0) {
         tileX--;
      }
      while(tileY % TILE_SIZE != 0) {
         tileY--;
      }
     
      for (int y = tileY; y < cameraY + Frame.CANVAS_HEIGHT; y += TILE_SIZE) {
         for (int x = tileX; x < cameraX + Frame.CANVAS_WIDTH; x += TILE_SIZE) {
            g2d.drawImage(map[y / TILE_SIZE][x / TILE_SIZE], x - cameraX, y - cameraY, null);
         }
      }
   }


The min/max offsets are the boundaries of the camera. It gets the position of the player, then calculates where the tiles need to be painted, then paints them. This needs quite a bit of improvement however, which I haven't been bothered to do, but I guess you could work something out. When you paint other objects you'll need to minus the camera's position from its co-ordinates.

Like I said, this is a simple solution but hopefully it can help.

Why are all OpenGL tutorials written in Brainf**k?
Offline ricardo

Senior Member


Medals: 2
Projects: 3



« Reply #2 - Posted 2013-10-27 17:43:47 »

When I said visible tiles on screen I was not referring all the map, just the tiles on screen.
For example if my map have 100x100 tiles of 32px and I can only show 10x8, then I just update 11x9 tiles (for edges in scrolling).
So far so good, not that hard to implement.
This is the first option. update all the visible tiles at a "constant" frame rate.

But for the second option...
Lets imagine player stop move. I don't need to update the same tiles over and over again because they are the same (if I don't have any animation like water).
If I stop update this frames, my game will gain performance because I don't need to loop to each tile and draw.
But instead, my game needs to loop to check if I need to update or not that current tile.

This can make a huge difference on old computers but game just don't have a consistent frame rate.
Every time I don't need to update game can have 100 fps, and if I need to update all the tiles (water for example) game can drop to 30. (this is just an example I don't have a old computer to test the exactly fps).

So, What is the best practice for tile based game, update all visible tiles on screen or update only tiles that need to be repainted?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline opiop65

JGO Kernel


Medals: 124
Projects: 7
Exp: 3 years


Team Alluminum


« Reply #3 - Posted 2013-10-27 20:07:53 »

Render tiles only on screen. Update tiles that either need to be updated (an entity offscreen is modifying a tile, so that tile needs to be updated), is on screen AND the player or another mob is modifying the tile.

Do not update when a tile is offscreen, and no mobs are modifying it. Don't update tiles that are onscreen that have no mobs modifying it.

Offline Troubleshoots

JGO Coder


Medals: 35
Exp: 7-9 months


Damn maths.


« Reply #4 - Posted 2013-10-28 14:45:00 »

To do the second option you'd need to check if anything has changed on the area of the tiles that you would need to update while the players position has not changed between the previous and current frames. That would mean a lot of (this is just an example):

1  
2  
3  
4  
5  
6  
if((skeleton.getX > leftMostTile.getX && skeleton.getY < rightMostTile.getY) && (skeleton.getX != skeleton.getPreviousX || skeleton.getY != skeleton.getPreviousY)) {
    render();
}
else if
else if
else if  

So yeah, this wouldn't affect performance that much. In my opinion, it isn't worth the time nor the effort.

Why are all OpenGL tutorials written in Brainf**k?
Offline Abuse

JGO Coder


Medals: 10


falling into the abyss of reality


« Reply #5 - Posted 2013-10-28 15:08:36 »

It depends:

- how expensive is drawing a tile
- how much memory do you have available.
- what proportion of tiles are static
- how many layers of tiles do you have?
- can tiles occlude one-another, do you have transparent tiles?

You can invent all manner of complex buffering, and dirty repaint algorithms, however if drawing a tile is super fast then you're not going to achieve anything (besides making your code more complex, and your renderer less flexible).

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline ricardo

Senior Member


Medals: 2
Projects: 3



« Reply #6 - Posted 2013-10-28 16:36:45 »

1- how expensive is drawing a tile
2- how much memory do you have available.
3- what proportion of tiles are static
4- how many layers of tiles do you have?
5- can tiles occlude one-another, do you have transparent tiles?

1- It is fast now but with my game resolution (480*320) if I need to draw 16*16 tiles I will need to draw 600 tiles 30 times every second with 30 fps (and I am not counting with border tiles if I need to scroll and objects, enemies, player, etc).

2- I have a good memory, but I am trying to make an old 2D adventure / rpg style game. I want to aim old computers.

3- Now all my tiles are static. I still need to code animated tiles. (But animated objects like npc and player are done).

4- Hum.. I will have, maybe 3 or 4 layers (depends on map). But just first and second layers can fill the whole screen. Others can't.

5- Yes, that is why I tell that just 1st and 2nd layers can fill all the screen. Others will be for example trees.
And yes, I have. Now I am using .png files with alpha channel. I don't know if I will use this, or just change color in-game (for example pink color in the background).
I have that topic here: http://www.java-gaming.org/topics/load-images-with-alpha/31041/view.html

Thanks for your time, and thanks for all the answers so far.
Offline StumpyStrust
« Reply #7 - Posted 2013-10-28 17:08:18 »

600 tiles 30 times a second is a very very easy goal to hit even if all tiles are dynamic. Easiest way to do this (may not be fastest) is to have a Java Rectangle that is the camera view and then check a tile to see if it intersects that rectangle. If so, render/update it. This is crazy easy and can drop the draw calls down which is the slow down in java2D.

In all honesty, you may be doing something grossly wrong if you cannot get 2-3k 16x16 tiles to render at a large resolution at 60 fps.

Offline ricardo

Senior Member


Medals: 2
Projects: 3



« Reply #8 - Posted 2013-10-28 17:17:25 »

I was thinking moving the "ground" instead of moving one fake camera to give fake movement effect and check if tiles are in range to draw, but your suggestion I think is better and I need to check that.
I had no idea I can draw 2-3k tiles at 60 fps in java. Before going to java I was programming in Python with pygame.
I will not care about performance now and draw all the visible tiles on screen.

Thank you for your answers.
Offline opiop65

JGO Kernel


Medals: 124
Projects: 7
Exp: 3 years


Team Alluminum


« Reply #9 - Posted 2013-10-28 20:42:53 »

There's no such thing as a camera in games. There's only moving your geometry so it looks like the "camera" is moving, when in reality its just the tiles moving.

Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (40 views)
2014-04-15 18:08:23

BurntPizza (36 views)
2014-04-15 03:46:01

UprightPath (52 views)
2014-04-14 17:39:50

UprightPath (34 views)
2014-04-14 17:35:47

Porlus (51 views)
2014-04-14 15:48:38

tom_mai78101 (73 views)
2014-04-10 04:04:31

BurntPizza (132 views)
2014-04-08 23:06:04

tom_mai78101 (232 views)
2014-04-05 13:34:39

trollwarrior1 (195 views)
2014-04-04 12:06:45

CJLetsGame (203 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!