Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (534)
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  
  Some clarification on repainting dirty regions  (Read 1901 times)
0 Members and 1 Guest are viewing this topic.
Offline Waffle

Junior Newbie





« Posted 2013-09-10 05:33:07 »

What I've got is multiple layers of tile grids, each with the same dimensions and everything, "stacked" on top of each other (because some things need to be drawn under other things).

When being painted, each "layer" is cycled through from back to front, meaning if each layer is 15x15 squares, then that's how many times the layer's repaint loops have to check for null squares/sprites/tiles to be drawn.

I'm starting to just learn about taking advantage of clipping areas, and repainting "dirty" regions with Java Graphics objects.

The system I use for traversing each layer's grid is fairly simple: each layer has a Rectangle Field called "bounds," which (for example) could be set to 15x15 (since I work with multiple levels of zoom, the actual pixel size of the layer is determined by [bounds * tile size * zoom factor]). So, the loops just go from 0 to bounds.width and bounds.height, originally designed to try and reduce working strictly with pixel locations.

Just so you understand my own understanding of my code, each time repaint is called, even with a dirty region, the loops have to traverse over 0 to bounds.width / bounds.height (right? Since the loops are still there..).

The way I have it currently makes me feel like even though I've specified dirty regions to repaint (for example, when mouse hovering), there is wasted CPU usage / resources iterating over each layers' grid, even though (somehow, I don't exactly understand) the graphics object currently does what I intend it to do (repaint for hovering mouse over new square / repainting to reset from mouse leaving square). I've checked this by setting the stroke to some obscure color and making it paint the clipBounds. Only the affected Tiles were changed.

Begin question:
Would it be more efficient to locate the clipBounds on-screen (pixel x & y) coordinates with the current Graphics object, and somehow start the loops only checking grid squares that are contained in and intersect the clipBounds? This would require some "extra" arithmetic prior to beginning the loops in order to determine which squares, from [x1][y1] to [x2][y2], lie in intersection with the clip bounds (the indices would be the same for all layers, since they all have equal tile dimensions).

For example, currently I have:
Mouse movement detected at point (x,y) on-screen. Convert (x,y) coordinate of MouseEvent to (X,Y) tile within the grid, retrieve Tile's Rectangle bounds, and send that Rectangle as a dirty region to be repainted. Repaint then iterates over all layers, and within each layer, it iterates over all tiles from grid location (0,0) to (WIDTH)(HEIGHT). Somehow, because of the dirty region parameter passed, it only repaints the affected area once it reaches it within the loops, and the rest are left unchanged. However due to the nature of the repaint method, each Tile on each layer had to be passed and done "something" with, correct?

Would it be better to have:
Mouse movement detected at point (x,y) on-screen. Convert (x,y) coordinate of MouseEvent to (X,Y) tile within the grid, retrieve Tile's Rectangle bounds, and send that as a dirty region to be repainted. Now, instead of iterating over the indices of the grid, start loop at first affected Tile's (x,y) pixel location. Traverse loop in pixels, incrementing by the Tile's width and height, until the clipBounds' size has been exceeded.

Does this second way reduce computation energy and resources used?

Keep in mind, I am trying to highly optimize because I'm using a slightly old machine. This thing can give me a really good hunch as to whether or not what I've coded was done well or not. The reason this "problem" even came about is because I noticed a performance drop once the currently open Layers actually contain sprites/tiles and not empty space. It made me feel like something was being done with the entirety of each layers' objects, and not just the objects that lie directly beneath the intersection of the clipBounds.
Offline Jeremy
« Reply #1 - Posted 2013-09-10 10:50:42 »

Some things I'd try:

Culling out tiles that don't need to be rendered.
Using some partitioning system to minimize the traversing of world tiles and entities
Have you considered that it may just be because your graphics device has poor support for OpenGL (Which java defaults to)? Especially if it is an old device.

JevaEngine, Latest Playthrough (This demo is networked with a centralized server model)

http://www.youtube.com/watch?v=rWA8bajpVXg
Offline Waffle

Junior Newbie





« Reply #2 - Posted 2013-09-10 21:24:37 »

What does "culling out tiles that don't need to be rendered" mean?

And I think my proposed solution "partitions" the area of the map that needs to be traversed. There's just a little but of calculating done to find the locations of the affected tiles, and it would only loop over those, instead of looping over everything and skipping what hasn't been changed.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

pw (35 views)
2014-07-24 01:59:36

Riven (33 views)
2014-07-23 21:16:32

Riven (21 views)
2014-07-23 21:07:15

Riven (24 views)
2014-07-23 20:56:16

ctomni231 (55 views)
2014-07-18 06:55:21

Zero Volt (47 views)
2014-07-17 23:47:54

danieldean (38 views)
2014-07-17 23:41:23

MustardPeter (43 views)
2014-07-16 23:30:00

Cero (59 views)
2014-07-16 00:42:17

Riven (56 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!