Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (707)
Games in Android Showcase (208)
games submitted by our members
Games in WIP (781)
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 3953 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  
You cannot reply to this message, because it is very, very old.

Galdo (446 views)
2017-01-12 13:44:09

Archive (599 views)
2017-01-02 05:31:41

0AndrewShepherd0 (1071 views)
2016-12-16 03:58:39

0AndrewShepherd0 (996 views)
2016-12-15 21:50:57

Lunch (1127 views)
2016-12-06 16:01:40

ral0r2 (1361 views)
2016-11-23 16:08:26

ClaasJG (1456 views)
2016-11-10 17:36:32

CoffeeChemist (1433 views)
2016-11-05 00:46:53

jay4842 (1515 views)
2016-11-01 19:04:52

theagentd (1254 views)
2016-10-24 17:51:53
List of Learning Resources
by elect
2016-09-09 09:47:55

List of Learning Resources
by elect
2016-09-08 09:47:20

List of Learning Resources
by elect
2016-09-08 09:46:51

List of Learning Resources
by elect
2016-09-08 09:46:27

List of Learning Resources
by elect
2016-09-08 09:45:41

List of Learning Resources
by elect
2016-09-08 08:39:20

List of Learning Resources
by elect
2016-09-08 08:38:19

Rendering resources
by Roquen
2016-08-08 05:55:21 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!