PocketMatrix forums

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

calling display update..

Strangest things happen.. I've a 'callback' function to draw loading progress. It looks like this:

Code:

void loadingGameInit(..)
{
display->Update();
GamePointer->UpdateDisplayPointer();
// draw stuff..
}


Where UpdateDisplayPointer contains
display = GetDisplay(); - this is because I cannot call Game::GetDisplay directly.

So, I call it from the main loop, and the real loop looks like this:

Code:
GameLoop {
display = GetDisplay();
//drawings..
display->Update();
display = GetDisplay();
//drawings..
display->Update();
} // End of GameLoop.


This works fine on most devices. But it doesn't on some WM5 - ipaqs 2xxx(have one tester with 21xx and one with 27xx).

This actually extremely puzzles and annoys me, because I simply don't understand why such thing happens.

Any ideas?


PS: One another odd thing: It works fine(loadingGameInit is called, display is updated, everyone're happy) when inside GameInit(). Once I do such tricks inside GameLoop(), everything hangs without any message or exception.
Maybe it's a typo, but why are you calling ->Update() before getting the display or drawing anything?
(in loadingGameInit)

I've seen plenty of hangs and strange behaviours when not having just 1 getdisplay and 1 update per draw. I do stack several in my gameinit like you do, it's just tricky to make sure an update is called if you detect an error and need to exit out of gameinit.
fast_rx wrote:Maybe it's a typo, but why are you calling ->Update() before getting the display or drawing anything?
(in loadingGameInit)

No, it's not. You see, the display is got in the very beginning of GameLoop(), and I'm calling loadingGameInit() in the middle of it. So I first update what's already been drawn, then calling GetDisplay again, and then draw stuff again. Many times actually before the end of GameLoop().

fast_rx wrote:
I've seen plenty of hangs and strange behaviours when not having just 1 getdisplay and 1 update per draw.

8O So I shouldn't at all call getdisplay+update more than one time per draw?? 8O

I probably may exit GameLoop() when first display update is done, but it will be very very tricky to return into the loop I started.

fast_rx wrote:
I do stack several in my gameinit like you do, it's just tricky to make sure an update is called if you detect an error and need to exit out of gameinit.


I don't bother it, actually. I just want to make things that work in GameInit work in GameLoop.
Am I wanting too much?
Let me clarify... You can use getdisplay/update more than once per loop, as long as you have 1 and only 1 getdisplay before each draw and only 1 update at then end of each draw.

I may post my framework for using pointers to gameloop functions. It may make what you're doing easier.
fast_rx wrote:Let me clarify... You can use getdisplay/update more than once per loop, as long as you have 1 and only 1 getdisplay before each draw and only 1 update at then end of each draw.

I see. It's what I was thinking in a first place :).

Ok, I maybe have found what was wrong(waiting for beta testers reports).

Also, what about calling GetDisplay() and Update inside StylusDown(), ButtonDown() and such? Is it OK as long as I call GetDisplay and Update in appropriate place?

I also believe there should be a function to know the state of display buffer. Probably setting display->m_backbuffer unprotected would help?

I may post my framework for using pointers to gameloop functions. It may make what you're doing easier.

Probably it's clear enough anyway, don't think there can be anything easier :).
Ok, I maybe have found what was wrong(waiting for beta testers reports).


No, I didn't :((.

Now this is really strange. I traced it with logs, it looks like this:

Code:
void BlitSample::GameLoop()
{
display = GetDisplay();
if (lastGameMode != GameMode)
{
g_prevMode = lastGameMode;
InitMode(GameMode);
}
// other stuff continues..
}

Where everything is ok in InitMode until loadingGameInit() is called - and there are no display updates. So, without any display drawing calls or updates we come into first line of loadingGameInit where
display->Update() is called. Game immidiately hangs.

Omg...
maybe you should draw something before the update is called. Shouldn't matter, but maybe it's something internal to the library.
I shifted
Code:
display->Update();
GamePointer->UpdateDisplayPointer();


from top of update function to the bottom. It looks like THIS finally worked. I'm in total confusion.
I've seen stuff fix itself when moving code around - when I have problems with memory pointers and allocation...

Good luck with finding it.

BTW, I still don't quite understand why you need the GamePointer->UpdateDisplayPointer();

That's storing the display pointer so that non-member functions can access it? Maybe you should just pass the display to the functions that need it.
fast_rx wrote:I've seen stuff fix itself when moving code around - when I have problems with memory pointers and allocation...

Yes, me too. But It doesn't seem like it's this problem when something corrupting memory pointers or stack. It's just more basic or more complicated - and I don't know which.

fast_rx wrote:
BTW, I still don't quite understand why you need the GamePointer->UpdateDisplayPointer();
That's storing the display pointer so that non-member functions can access it? Maybe you should just pass the display to the functions that need it.


Because I cannot pass Game::GetDisplay() wherever I want :). So I have a global pointer to BlitSample and call UpdateDisplayPointer function like this.


PS: Anyway, WM5 still seems like a buggy and not relieable OS - even if there are problems, it becomes more unstable than previous OSes.
I had to make at least 30/40 changes to Space Treker for WM5 to do an extra 'get display' all over the place.

I used to do this at the start of any function which wrote to the screen but with WM5 it's needed after anything significant/unusual is done. So in each funtion there are 2 or 3 'get display' calls now & it all works.
I'm not trying to drag out this thread, but what would be considered significant/unusual?

In assaultaire, during some situations, I am locking/unlocking the screen surface, creating several rasterizers and deleting them, and even redirecting all drawing to a temp surface - all with a single getDisplay per frame. Surely that can be considered significant ;)

But then again, maybe it's crashing everyone's devices...
best way I can illustrate is with a code sample. Don't ask me why I need the second GetDisplay()
Code:
display = MyGame::GetDisplay();
r_general.Set(15,8,225,90);//the rect filled & used for pointer reads
display->SetBlending(160);//transparancy level
display->BlitStretch(r_general,backing.SpriteData);
display->Blit(0,200,lowpanel);
display->SetBlending(0xFF);//transparancy level
display->DrawRect(14,7,225,90,Color(160,105,49));//a bounding box
l_font.draw(display,120,15, FONT_CENTER,tr_items[this_item].name);
l_font.draw(display,120,35, FONT_CENTER,TEXT("FITTED"));
m_font.draw(display,120,55, FONT_CENTER,TEXT("Hit Points :"));
_itot(bays[wepbay].weapon.hitp,keystr,10);
m_font.draw(display,120,70, FONT_CENTER,keystr);
display->Update();//immediate update
Sleep(1500);//pause for a moment

b_sale=false;
b_dosale=false;
b_selling=false;
b_hold=true;
display = MyGame::GetDisplay();
Surface* screen = display->SetRenderTarget(back3d);
display->Blit(0,0,holdback);//put hold background on 3d screen background
display->SetRenderTarget(screen);

in this example I am not coming back to the main loop for the regular update of the screen but pausing a second to show a message then carrying on. Bad coding probably.
Conan, why you need to SetRenderTarget away from screen surface? Maybe you can pDisplay->SetClipping(RectAreaFor3DRender);

I do not see a single reason to have more than one GetDisplay/Update per frame.
Even with loading function - just use one byte variable to store the "current loading position" and a couple of if's to return to the proper place in the next GameLoop.
As for passing a Display pointer - why does someone need this?

Code:

//Globals.h
extern Display * pDisplay;

Code:

//Globals.cpp
Display * pDisplay;

Code:

//Somecode.cpp
#include Globals.h
void SomeFunction()
{
pDisplay->BlitMySurface;
}


I try to use globals in much places of my game to save speed&size.;

fast_rx, I've got a speedup after removing surface locking (replacing with just surf->m_buffer->GetPixels();) from several of my fuctions (not needed anymore with PHal'ed Frog)[/list]
BIGBEN wrote:
fast_rx, I've got a speedup after removing surface locking (replacing with just surf->m_buffer->GetPixels();) from several of my fuctions (not needed anymore with PHal'ed Frog)


The lock just gets the pitch and returns getpixels in the newer one. Back in 0.7, there was the swizzling being done to match screen rotation that WOULD kill performance... I still use it because I don't want to rework my old code and it shouldn't make any difference.
(except of course, the overhead of passing a couple of unused parameters (discard and rect) and calling 1 more function)


As for passing a Display pointer - why does someone need this?



I'm using something like this in my games, maybe someone will find it useful:

Code:

typedef int (MainProg::*FP)(Display * display, bool initmode);
FP gamemode;


Then I can change modes by pointing this to different functions (for main menu, game options, init level, etc.)

then my game loop is simply
Code:

virtual void GameLoop()
{
static FP lastgamemode = NULL;

Display * display = GetDisplay();

bool initmode = false;
if(gamemode != lastgamemode) initmode = true;

int stat = (*gamemode)(display, initmode);

display->Update();

}


int mode1(Display *display, bool initmode)
{
if(initmode)
{
//do somestuff first time through
}

//draw some stuff

return 0;
}




in the case of loading a bunch of stuff, the gamemode could be set up like this:
Code:

int loadlotsofstuff(Display *display, bool initmode)
{
static int loop=0;

if(initmode)
{
loop = 0;
}

switch(loop)
{
case 0:
//load 1st set of bitmaps
display->DrawText("loading bitmaps", ....
break;
case 1:
//load this...
break;
case 2:
//load that...
break;
}

loop++;
return 0;
}



the main this is that it does a little at a time, so the display gets updated frequently and you don't have juggle a global pointer to display or use a bunch of getDisplay's stacked up in one function.
Interesting... thanks for sharing!

fast_rx wrote:
you don't have juggle a global pointer to display


I really do not see why global variables are bad for games?

Why people need to pass pDisplay to functions initializing 4byte pointer in any drawing function each time it is called (which will cost in time and size).

I really do not see a reason why :(

Please help me to understand the benefits of passing pointer to the Display each time.
the main this is that it does a little at a time, so the display gets updated frequently and you don't have juggle a global pointer to display or use a bunch of getDisplay's stacked up in one function.


I wonder, how many of such switchcases I will need to do, considering that in my game(which is, btw being released today or tomorrow, depending on your time zone :)) there are a ~1700 gfx files to be loaded(though they are in a pak file, it woundn't matter).
The main benefit for me is knowing I'm not going to start using a display pointer that's already been ->Update()'d or never started with GetDisplay(), or a double ->Update() which seems to cause a crash.

I've spent a lot of time tracking down problems like that - so I've changed the way I do things. It's more of a problem I've created myself by poor programming.

The other reason for me is code reuse... If I pass the display pointer into the functions, I don't care what I named it or what the scope is.

For instance, I have a class that contains all information about enemy tanks. So I call enemies.Draw(display)
If I don't pass the display, how is this class going to get the display pointer? The global is in another .cpp file, so you'd at least have to declare it extern, right?

I'm not trying to tell anyone the way to code, just the way I'm doing it to keep me from shooting myself in the foot.
fast_rx wrote:
If I pass the display pointer into the functions, I don't care what I named it or what the scope is.

If I don't pass the display, how is this class going to get the display pointer? The global is in another .cpp file, so you'd at least have to declare it extern, right?

I'm not trying to tell anyone the way to code, just the way I'm doing it to keep me from shooting myself in the foot.


Funny, but for the same reasons (not to care about the name & scope) I do use it as global pointer, all I need is (as you said):

extern pDisplay;
in Globals.h

Not trying to shoot in your feet ;)

Thanks for telling your experience.

jaguard wrote:
I wonder, how many of such switchcases I will need to do, considering that in my game(which is, btw being released today or tomorrow, depending on your time zone :)) there are a ~1700 gfx files to be loaded


You don't need switches at all, if you just load files by passing name as a string that is loaded by string number (from external txt or by resource string).
In my main Game class I have the following:
Code:
void CMyGame::DrawScreen()
{
pDisplay->Update();
pDisplay = GetDisplay();
}
In the rare case where I need to call Update() outside of the main game loop, I use pMyGame->DrawScreen(); which updates the screen and then gets a new pointer to it. Both pDisplay and pMyGame are global variables.

-John
Presto wrote:In my main Game class I have the following:
Code:
void CMyGame::DrawScreen()
{
pDisplay->Update();
pDisplay = GetDisplay();
}


That's what I did in a first place. And this is where ipaq hx 2xxx(WM5) hangs. I didn't entirely understand what happened there, but I now have special functions like this:

Code:
// To make sure that display is always updated before draw is occured.
void displayGetDisplay()
{
if (g_displayState == 1)
{
GamePointer->UpdateDisplayPointer();
g_displayState = 0;
}
}

void displayUpdate()
{
if (g_displayState == 0)
{
display->Update();
g_displayState = 1;
}
}


UpdateDisplayPointer is a realisation of display = GetDisplay();

So I make sure that every time I call draw or update I have correct display pointer.
Looks like it works.