PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

GameLoop - When to blit?

What is the best solution for updating the main screen from an offscreen window?

1) Blit offscreen window at a continuous frame-rate (even if contents have not changed.
2) Only update window if offscreen window has changed? (For example: InvalidateRect only when a StylusDown event occurs, etc.)

It seems to me that choice #1 would cause a lot of unnecessary updates even when the offscreen contents has not changed. This would also cause faster battery drainage.

Any thoughts or suggestions? How are most people doing this?

Thanks!
:)
I for one am lazy so I go for No 1. Each time round the game loop I blit the screen. This comes from my view that devices nowadays should be fast enough to cope & I cannot be bothered with all the dirty rectangle stuff etc we had to do in the old days on 8-bit devices which were vastly slower than today's cpu's. These days I just want to write games & avoid anything too technical.
Thanks, but that seems very wasteful. What if I only need to update the score or a game timer...why redraw the entire screen?

Let's say, for example, that I'm updating the entire screen at 20 frames/sec. But, I have a 1 second timer that needs to be updated. I would only want to update the timer on every 20th frame. Would you create a counter that increments each frame and when it reaches 20, you would then update the timer? If so, the previous 19 frames would be identical (assuming the timer was all that changed). Why use CPU cycles to redraw the same screen over and over?

Just doesn't seem very efficient to me... :)
You're talking about Dirty Rectangles.

Dan East
Dirty Rectangles? Are you talking about updating partial areas of the screen rather than blitting the entire window? Is this bad practice? I'm open to any suggestions on this topic.

Dan, what would be your answer to my original question (choice 1 or 2)??

Thanks...
dirty rectangles & similar techniques are not bad practice, far from it. If you write in such a way as to greatly reduce the amount of data written to the screen then you can't be doing the wrong thing. All I'm saying is that today you really don't need to worry too much about such things as you will get pretty good performance with a simple game loop blit. We still work hard to gain speed with PPC compared to the big PC's with graphics acceleration but it's just not as critical as it once was. You will still find lots of discussions on these forums about performance & quite a few guys using assembler to gain an edge.
This decision is what's known as a "trade off." The real world is full of them, and programming models the real world, so the relationship is transitive.

Dirty rectangles are "smarter" but therefore "more complicated."

Decide what your priorities are, make your decision accordingly, do it, then reevaluate.
Dan, what would be your answer to my original question (choice 1 or 2)??


That completely depends on the game. If it is a 3D game or a 2D game that scrolls the entire screen, then you won't see much advantage using dirty rectangles.

I see 3 reasons to use something like dirty rectangles:
1) There are bandwidth problems transferring data to display (Axim X5 Basic, etc)
2) Your game is not running at a desired FPS and you can make up the difference by not blitting an entire screen worth each frame
3) Your game is not graphically intensive (ie little of the screen changes each frame) or is completely user event driven, and you want to be able to Sleep each frame to reduce power consumption. Take Solitaire for example. It would be silly to redraw the entire window every frame, because it would hurt battery life, and not be friendly to other tasks that may be running in the background. Those are not really issues for more hardcode games that completely take over the device.

So I can't give a generic answer as to whether dirty rectangles would be good in your case. Usually it is, but if the entire display changes almost every frame then it can actually hurt performance.

If your game is tile based then you can almost certainly take advantage of dirty rects with practically no overhead.

Dan East
I remember seeing a post about PocketHAL automatically only updated the areas of display that had changed - saves having to worry about it yourself. However the discussion also pointed out that if the whole screen continually changes it actually will drop performance a little from the additional checking.
I've thought dirty rectangles have been more effort than worth....but my games always have some animation going on etc, in reality is your game gonna have enough 'blank frames' for it to be worthwhile, the extra overhead and complexity for when it does do a fullscreen refresh.

If so an added benefit from dirty rect's (or just simply not updating front buffer) is that you you can save power by using less cpu....maybe ;)

Jim.
I think Dan summarized the issues well. Really, 1 and 2 are the same reason: failure to maintain FPS, due to either blitting or game logic taking too long.

Concentrix uses dirty regions (non-rectangular) to save on blitting, and if it still takes too long on a frame, it will skip rendering frames to maintain FPS. This seems to work rather well, saves battery power, and prevents cheating by overloading the device to slow the game down. However, it requires a bit of sophisticated code, which takes effort to write and maintain, and takes up a bit of space in the executable.

It's up to you whether your app requires this sophistication, it may not.
Thanks Guys! I really appreciate the feedback on this question. All of my games are puzzle-type. So, I usually don't need to update the screen except when the user taps the screen, etc. (other than updating game timer). It seems like a lot more work to add code to determine "when" the screen should be blitted rather than updating the screen based on user events.

Since my games are puzzle-type, I'm not really concerned about FPS (at least not as most people are). The game waits for a user event, then does something on the screen, and waits again for the user. Some animation, but I've been handling this by setting up system TIMER events and calling InvalidateRect at each clock tick to cause a WM_PAINT to update the screen. For the most part, this seems to work fine. At times, the animation is a bit sluggish which I'm still trying to understand.

I should mention that this is my first Pocket PC conversion. For the past 3 years I've been developing for Palm OS...which I might add is MUCH easier to develop on than Windows!! :wink:

Thanks again!!
Well, you're into windows messaging and the like, which has a legacy of over 15 years or so back to Windows 3.0. So yeah, Windows programming can be clunky.

By "clock tick" are you referring to a millisecond? If so then that is not good, because you are trying to run at 1000 FPS. First of all I assume you have a back buffer (or Memory DC as it is known in Windows) that you draw to, then you BitBlt that to the actual Window DC when you need to update. That will prevent flickering and other rendering artifacts (by not directly drawing to the visible window DC). You can then use the Update Region basically as your dirty rectangle system. So when you draw the pieces you maintain a region that covers all the areas you have drawn to. Then you call InvalidateRect with only that region that has changed. The disadvantage is you could update one small thing in one corner of the screen, and update another small area in the opposite corner of the screen, and end up with an Update Region as big as the whole window.

Dan East
Actually, I guess I should've said "timer event" rather than "clock tick". I set up multiple system timers at certain time intervals based on how fast I want a particular animation to occur. When a timer event occurs, I draw the updates to an offscreen window (backbuffer) and then BitBlt the offscreen to the main window. Dan, you are correct...all of my updates to the main screen come from an offscreen buffer. I don't do any direct drawing to the main window.

This is actually what prompted all of my questions about how I am currently doing things. I wondered if (instead of using system timer events) I shouldn't just blit from the offscreen buffer at a constant frame rate (all the time). I figured it wasn't very efficient since in most frames, nothing has changed on the screen.
It sounds like what you are doing is reasonable.
why would Concentrix require such sophisticated screen updating? It's a 2D game updating the various ring segments as they move down/out. I would have though you have oodles of cpu cycles to spare. Where are all the cpu cycles going?

ps: if you have not played Concentrix then give it a try. I think it's brill :lol:
This might be somewhat beside the question, but blitting on slower devices, can be very slow.

On my port of HPC Doom, I played around with blitting the game screen every frame and the status bar every 1/5th frame. The FPS increase was significant on a Jornada 680.

The bottom line is that copying memory around unnecessarily is not a good thing.
Thanks for the compliment Conan!

I originally implemented Concentrix to draw the entire screen every frame, but it was too slow, especially with the overdraw (draw background then coloured blocks).

So I changed it to draw only the blocks that changed, using a series of span lines for each block.

Blocks are coloured using MULTIPLY, and faded using OPACITY, so that remains a performance drag. It really depends on what's happening onscreen, though.

It's pretty snappy on almost every device now, so it was worth it. It does make the code more compilcated, though.