PocketMatrix forums

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

Need help on improving a game's performance using GapiDraw

Got any advice on how I can improve the performance of my game? I just plugged all my drawing and updating functions inside the ProcessNextFrame loop and my FPS dropped to 20-24 FPS. Are there any tips or techniques I should observe to make it work faster? (graphical, computational considerations?) Any help would really be appreciated. :-)

Code:
HRESULT CMyApplication::ProcessNextFrame(CGapiSurface& backbuffer, DWORD dwFlags)
{
RECT rect;
GDFILLRECTFX rectfx;
TCHAR str[128];
FLOAT nActualFrameTime = 0;
FLOAT nFrameRate = 0;

DWORD dwScreenWidth = backbuffer.GetWidth();
DWORD dwScreenHeight = backbuffer.GetHeight();

switch(fi_WindowState)
{
case MAIN_GAME:
//draw stuff
DrawStuff1(backbuffer, fi_CurRegion);
DrawStuff2(backbuffer, fi_CurRegion);
DrawStuff3(backbuffer, fi_CurRegion);
DrawStuff4(backbuffer, fi_CurRegion);
DrawStuff5(backbuffer, fi_CurRegion);
DrawStuff6(backbuffer, fi_CurRegion);
//update stuff
UpdateStuff1(fi_CurRegion);
UpdateStuff2(fi_CurRegion);
UpdateStuff3();
UpdateStuff4();
break;
}
m_timer.GetActualFrameRate(&nFrameRate);
_stprintf(str, TEXT("Current framerate: [%.3f fps]"), nFrameRate);
DrawFrameInfo(backbuffer, dwScreenHeight - 315, str, RGB(90, 60, 170));

return S_OK;
}
Hah. I'm not sure what you were expecting but the only optimization I can imagine is to remove all those function calls in your one test switch.

Now obviously this is a joke I expect all the real work is being handled in those functions, so the only real optimizations will happen there.
There is not much else you can do as far as Gapi API is concerned.
If your frame rate is that low then either you are trying to do too much or you are doing it wrong way :-)

I can only be that "specific" given code example posted on this forum by you.

:-)
Oops, I'm sorry guys. I knew I didn't give enough info on my last post. Anyway, I'm posting the code from one of those "drawstuff" functions above. Is it safe to create a "dynamic" rectangle inside my function that let's me "cut" that part out from the imagemap and blit it on the screen? Or using a global rect for each of the values would make my code faster? All my drawstuff functions are like these. I wanted to blit the images on the screen in layers that's why I separated them into diff. functions.

Thanks in advance! :-)

Code:
HRESULT CMyApplication::DrawStuff1(CGapiSurface& backbuffer)
{
RECT m_rTempRect;
int x, y;

for(i = 0; i < 62; i++){
for(j = 0; j < 46; j++){
if(FaceValue[i][j] > 0){
if(FaceValue[i][j] == FACE1){
::SetRect(&m_rTempRect, 120, 120, 135, 140);
}
else if(FaceValue[i][j] == FACE2){
::SetRect(&m_rTempRect, 135, 120, 150, 140);
}
else if(FaceValue[i][j] == FACE3){
::SetRect(&m_rTempRect, 120, 140, 135, 160);
}
else if(FaceValue[i][j] == FACE4){
::SetRect(&m_rTempRect, 150, 140, 165, 160);
}
backbuffer.BltFast(x, y, &m_facedata, &m_rTempRect, GDBLTFAST_KEYSRC, NULL);
}
x = x + 15;
if(x == 150) x = 0;
}
y = y + 20;
if(y == 200) y = 0;
}
I did some improvments on the code and made som comments about it. Hope this helps...

Code:
HRESULT CMyApplication::DrawStuff1(CGapiSurface& backbuffer)
{
RECT m_rTempRect;
int x = 0, y = 0; // x and y needs to have an initial value for the first blit

if(FaceValue > 0){ // no need to do anything in function if this is not true

if(FaceValue == FACE1){ // no need to do this in for-loops since it is not dependent of i and j
::SetRect(&m;_rTempRect, 120, 120, 135, 140);
}
else if(FaceValue == FACE2){
::SetRect(&m;_rTempRect, 135, 120, 150, 140);
}
else if(FaceValue == FACE3){
::SetRect(&m;_rTempRect, 120, 140, 135, 160);
}
else if(FaceValue == FACE4){
::SetRect(&m;_rTempRect, 150, 140, 165, 160);
}

for(i = 0; i < 62; i++){
for(j = 0; j < 46; j++){

backbuffer.BltFast(x, y, &m;_facedata, &m;_rTempRect, GDBLTFAST_KEYSRC, NULL);
x = x + 15;
if(x == 150) x = 0;
}

y = y + 20;
if(y == 200) y = 0;
}
}
}
Hi,

One very low-level thing you could do is remove the if() from the loops. According to Intel optimization books, this is always bad.

What you could do is create a 2 dimension array of 62x46, which would hold the location, or offset to an origin. That way you would get rid of all code except the loops and the blit itself.

Also, all those if(FaceValue) are really bugging me. You should really come up with something better. If FaceValue is FACE4, the program needs to go through 3 if() before it gets to FACE4.

What you could do to optimize this is make sure FACE1 to FACE 4 are sequential in value (1 to 4), and use switch() instead. When compiling for release, your switch() will probably be optimized with some kind of offset jump by the compiler. One of my app was optimized that way, but I don't know the specifics.

There's always more that can be done to optimize, but that's what I see now.

HTH

--Eric
I modified my "previous" code above and what if FaceValue needs to be a 2-dimensional array? I guess the only optimization I can do for now is replace the IFs with switch()'s?

I already increased my "targetFPS" to 50 and its still dropping to 20-24FPS when played on an iPAQ 3870. I should also minimize my computations (multiplications and divisions) inside my functions right?

Thank you so much for your time guys. I'm going to have to include you on my "credits" when my game finally comes out. :-)
One more thing you can do is to have four temporary rects (one for each face). By doing this you get four calls to ::SetRect instead of a maximum of 62*46 calls. Also you may want to consider having the rects as member variables of the class. That way you can just call ::SetRect once for each rect in the constructor.

Code:
HRESULT CMyApplication::DrawStuff1(CGapiSurface& backbuffer)
{
RECT m_rTempRectFace1, m_rTempRectFace2, m_rTempRectFace3, m_rTempRectFace4;
::SetRect(&m;_rTempRectFace1, 120, 120, 135, 140);
::SetRect(&m;_rTempRectFace2, 135, 120, 150, 140);
::SetRect(&m;_rTempRectFace3, 120, 140, 135, 160);
::SetRect(&m;_rTempRectFace4, 150, 140, 165, 160);

int x = ???, y = ???;

for(i = 0; i < 62; i++){
for(j = 0; j < 46; j++){

switch(FaceValue[i][j]){
case FACE1 :
backbuffer.BltFast(x, y, &m;_facedata, &m;_rTempRectFace1, GDBLTFAST_KEYSRC, NULL);
break;

case FACE2 :
backbuffer.BltFast(x, y, &m;_facedata, &m;_rTempRectFace2, GDBLTFAST_KEYSRC, NULL);
break;

case FACE3 :
backbuffer.BltFast(x, y, &m;_facedata, &m;_rTempRectFace3, GDBLTFAST_KEYSRC, NULL);
break;

case FACE4 :
backbuffer.BltFast(x, y, &m;_facedata, &m;_rTempRectFace4, GDBLTFAST_KEYSRC, NULL);
break;
}
x = x + 15;
if(x == 150) x = 0;
}
y = y + 20;
if(y == 200) y = 0;
}
Pejo Software - Per · 22 Feb 2003, 18:53 UTC
You are doing quite a lot of blits in that function!
Maybe I am missing something but aren't you doing the same blits several times.
The best advise I can give you is to find out what parts of your code that takes the longest time and start there. I doubt that changing the if statements to switch would give you much.
Blits (or rendering) are costly and if you can reduce the number of points (pixels) you are blitting you could optimize your code quite much.
For example in our game 3Dris we don't render the pit (including pieces that already has fallen down) on every frame since it only needs to be rendered when the piece falling has reached the bottom. Instead we render the pit into a separate surface when needed and this surface is blitted to the back buffer on every frame. In our game Beshaped we only redraw those parts of the screen that has been changed since the last frame.
There was just another person asking about optimizing his code this week, did you see that posting? I went into more depth in that one but you have the same problem, all those indirected memory accesses are loop invariants, meaning they don't change during the loop so you don't need to recalculate them more than once. Hence they can be removed from the loop. (ie the memory location where that array is stored does *not* change inside your loops)

For example (assuming FaceValue is int, no reference of it in your code so I'm also guessing its global):
Code:

for(i = 0; i < 62; i++)
{
int * ip = FaceValue[i];
for(j = 0; j < 46; j++, ip++)
{
do your work
...
}
...


You'll have to dereference each acces, but don't think that isn't happening already, and in larger quanity.


Code:

HRESULT CMyApplication::DrawStuff1(CGapiSurface& backbuffer)
{
RECT m_rTempRect;


I found this very humorous. Ya see I've been against that technique for years because A) its just memetic noise, B) there are ways to accomplish the same thing with language constructs and C) because its a human system and therefore open to human errors. What you see there is a member variable local to a function. ;)

No offense of course, I'm sure this wasn't intentional. Part of the optimization process I'm guessing.


Definately remove the SetRect calls, if you have to do that switch inside your loop, make 4 local rects and set them once before the loop, since they don't change either, only the FACEX does which is what you are testing.

Another thing, although this is minor but I would do it anyway since it makes clearer code, put the zero case in your switch:

Code:

switch(*ip)
{
case 0: break; // do nothing
case FACE1:
rect = rectFACE1;
break;
...


This will certainly become a jump table, which is about equal to a test, but only needs 1 per loop, as opposed to various tests and jumps with a multilevel if.


Can't comment much more with knowing some important aspects, but anything else is more of a designed for speed sort of determinination like your bound check at the end of the loops. What is the importance of 150 and 200? Can you change them? If you can get a number that is close to a binary power, you can reduce the test and assign to a binary and.
Thank you very much for the help guys. I'll try all your suggestions and get back with my progress. PejoSoftware mentioned something about determining "which part of my loop" takes the longest time to process, how do I do this? How can I "set" a timer before a function call and "end" it after the function to determine how much time the function takes? I reckon I also have to record this to a file, right?
Pejo Software - Per · 23 Feb 2003, 14:47 UTC
You could use GetTickCount().
If you want higher resolution and your hardware supports it you can use a high-resolution performance counter (QueryPerformanceCounter and so on...).
I've tweaked my code a bit by applying some of the stuff you adviced and the FPS gradually went up to an additional 2 FPS. :-) I know there's a lot more I can do to increase this.

Just a question, I'm trying to create a sky for my game and wanted my clouds to blend in with the sky's color, what's the best and fastest way to do this? I'm using FillRect for my sky and bltfast for the cloud sprites. What I did was created another rect above each cloud sprite with less opacity to "cover up" the image such that it sort of blends in with the background. Would it be better to just use "colorize" or "tint" effects on the clouds rather than adding another rectangle layer above them?

This forum is really helpful. Thanks a lot for the support guys. :-)