Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (781)
Games in Android Showcase (233)
games submitted by our members
Games in WIP (857)
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 5400 times)
0 Members and 1 Guest are viewing this topic.
Offline Waffle

Junior Newbie

« Posted 2013-09-10 03: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 08: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)
Offline Waffle

Junior Newbie

« Reply #2 - Posted 2013-09-10 19: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  

hadezbladez (1165 views)
2018-11-16 13:46:03

hadezbladez (525 views)
2018-11-16 13:41:33

hadezbladez (1185 views)
2018-11-16 13:35:35

hadezbladez (272 views)
2018-11-16 13:32:03

EgonOlsen (2567 views)
2018-06-10 19:43:48

EgonOlsen (2786 views)
2018-06-10 19:43:44

EgonOlsen (1564 views)
2018-06-10 19:43:20

DesertCoockie (2268 views)
2018-05-13 18:23:11

nelsongames (2121 views)
2018-04-24 18:15:36

nelsongames (2815 views)
2018-04-24 18:14:32
Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20 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‑
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!