PocketMatrix forums

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

orientation change runtime

Hi !!!
I want to change orientation runtime.
Is it posible?

I made following:


    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)

    {
    _Module.Init( 0, hInstance );

    again:

    myGame * game = myGame::inst();

    game->Run();

    if (game->changeOrientationFlag) { game->freeInst(); goto again; }

    else { game->freeInst(); }

    return 0;
    }


After orientation change via settings dialog my program saves the changed orientation into the settings file, it sets the changeOrientationFlag and calls Shutdown().
If orientation was changed the program creates instance of myGame class again.

In the emulator it works without problems, but
when i ran my project in the device and changed orientation the instance was not created again.
Why?

Please help.
are there other methods of orientation change?
In my games, I've just put up a message that says to restart the game for the orientation to take.

But you got me wondering, so I tried what you're doing. I used this:
Code:

RESTARTFLAG = true;
while( RESTARTFLAG )
{
RESTARTFLAG = false;
QUIT = false; //this is set when user wants to exit

MainProg * game = new MainProg();
game->Run();
}


This seems to almost work. I use some display updates in game init, and I see them. I even get to the start screen, but that's where the problem is. By then, it appears that I've lost window focus or something. I can see my battery bar updating through my game screen and when I click the screen, I am operating on things 'behind' my program screen. If I click in just the right place, I can regain focus and it works. I'll probably try to send a windows message to try to force the focus later.

What is your ::inst() doing?

FYI, I'm using PHAL 0.7.7 with PocketFrog...
lapkos · 23 Apr 2007, 19:07 UTC — singleton
It is just a singleton.

Code:
//myGame.h
...
class myGame : public Game
{

static myGame * _self;
...

//myGame.cpp
...
myGame * myGame::inst()
{
if(!_self) _self = new myGame();
return _self;
}

void myGame::freeInst()
{

delete this; _self=NULL;
}

myGame* myGame ::_self=NULL;

...
fast_rx, could you give me your game.cpp file please?
I don't have the exact code here, but what I'm using is practically a copy of the PFrog samples...

Code:

class MainProg : public Game
{

DECLARE_WND_CLASS( APPNAME )


public:

MainProg() : WC(0,100,250,220, W_WINDOW | W_SHOWTITLE | W_SHOW_VSCROLL | W_SIZEABLE, TEXT("WC"))
{
m_config.szWindowName = APPNAME;
m_config.orientation = ORIENTATION_NORMAL;
m_config.desktopWidth = partmemx;
m_config.desktopHeight = partmemy;
}

...

virtual bool GameInit()
{
...
}
virtual void GameLoop()
{
...
}


One thing I thought about was that I'm waiting till the end of GameLoop/Display Update() before I shutdown. That could be causing you some problems I suppose.

I never went back to see if I could fix the loss of focus problem.
lapkos, did you have any success?
I tryed to change orientation in blit sample and i
got message "Window creation failed". This message was called in Game::Init():

Code:

bool Game::Init()
{

#if defined(FROG_HPC)
// Handheld PC: force orientation to NORTH
m_config.orientation = ORIENTATION_NORMAL;
#endif

m_bSuspended = false;

//GetWndClassInfo().m_wc.style &= ~CS_DBLCLKS;
GetWndClassInfo().m_wc.style = CS_HREDRAW | CS_VREDRAW;
GetWndClassInfo().m_wc.hbrBackground = 0;

// Create the window
#if defined(_WIN32_WCE)

Rect r( 0, 0, ::GetSystemMetrics( SM_CXSCREEN ), ::GetSystemMetrics( SM_CYSCREEN ) );
DWORD style = WS_VISIBLE;

#else
Rect r( CW_USEDEFAULT, CW_USEDEFAULT, 0, 0 );
DWORD style = WS_SYSMENU;

#endif
#if defined(FROG_HPC)
DWORD exStyle = WS_EX_TOPMOST;
#else
DWORD exStyle = 0;
#endif


if (!Create( 0, r, m_config.szWindowName, style, exStyle ))
{
FatalError( _T("Window creation failed") ); // <= here
return false;
}
...



I can't fix it yet.

fast_rx, what do you think about it?
Here's the blit example with my way of doing it...

It behaves like I mentioned before - with loss of focus (today screen flashes through). I can regain focus by clicking at the top of the screen just to the right of where the start menu would be.

Note that button A quits and B restarts with toggled orientation.



Code:

#include "blit.h"
#include "resource.h"

using namespace Frog;


bool RESTARTFLAG;
bool QUIT=false;
bool TOGGLE_ORIENTATION=true;

//////////////////////////////////////////////////////////////////////////////
//
// _Module : This is needed to use ATL Windows
//
//////////////////////////////////////////////////////////////////////////////

CComModule _Module;



//////////////////////////////////////////////////////////////////////////////
//
// BlitSample
//
//////////////////////////////////////////////////////////////////////////////

BlitSample::BlitSample()
{

m_config.szWindowName = _T("Blit Sample");
m_config.orientation = TOGGLE_ORIENTATION ? ORIENTATION_ROTATE90CCW : ORIENTATION_NORMAL;

TOGGLE_ORIENTATION = !TOGGLE_ORIENTATION;
}



bool BlitSample::GameInit()
{

// Initialize random seed
srand( PocketPC::GetTickCount() );

Display* display = GetDisplay();

m_images[0] = LoadImage( display, IDB_BITMAP1 );
m_images[1] = LoadImage( display, IDB_BITMAP2 );
m_images[2] = LoadImage( display, IDB_BITMAP3 );
m_images[3] = LoadImage( display, IDB_BITMAP4 );
m_background = LoadImage( display, IDR_IMAGE1 );

for (int i = 0; i < 4; ++i)
{
// Set color mask
m_images[i]->SetColorMask( Color( 255, 0, 255) );

// Set initial position
m_position[i].Set( 0, 0, m_images[i]->GetWidth(), m_images[i]->GetHeight() );

int x = rand() % (display->GetWidth() - m_images[i]->GetWidth());
int y = rand() % (display->GetHeight() - m_images[i]->GetHeight());

m_position[i].Move( x, y );
}


m_numbers = LoadImage( display, IDB_NUMBERS );
m_FPSCounter = 0;

m_dragImage = -1; // Not dragging any image

return true;

}



void BlitSample::GameEnd()
{

for (int i = 0; i < 4; ++i)
{
delete m_images[i];
}

delete m_background;
delete m_numbers;

}



void BlitSample::GameLoop()
{

Display* display = GetDisplay();

display->Blit( 0, 0, m_background );

#if defined(FROG_HPC)
display->Blit( 320, 0, m_background );
#endif

for (int i = 0; i < 4; ++i)
{
Rect& pos = m_position[i];

// Display image
display->Blit( pos.left, pos.top, m_images[i] );

if (i != m_dragImage)
{
// Move image (horizontal)
switch (rand() % 3)
{
case 0: break; // Don't move
case 1: if (pos.left > 0) pos.Translate( -1, 0 ); break;
case 2: if (pos.right < display->GetWidth()) pos.Translate( 1, 0 ); break;
}

// Move image (vertical)
switch (rand() % 3)
{
case 0: break; // Don't move
case 1: if (pos.top > 0) pos.Translate( 0, -1 ); break;
case 2: if (pos.bottom < display->GetHeight()) pos.Translate( 0, 1 ); break;
}
}
}


// Update FPS
m_FPSTicks[ m_FPSCounter & 15 ] = PocketPC::GetTickCount();
if (m_FPSCounter > 15)
{
uint32_t totalTime = m_FPSTicks[ m_FPSCounter & 15 ] - m_FPSTicks[ (m_FPSCounter+1) & 15 ];
if (totalTime == 0) totalTime = 1;
uint32_t fps = 16000 / totalTime;
uint32_t n100 = fps / 100; fps -= n100 * 100;
uint32_t n10 = fps / 10; fps -= n10 * 10;
uint32_t n1 = fps;

if (n100 > 9) { n100 = 9; n10 = 9; n1 = 9; }

// Display fps
display->Blit( 0, 0, m_numbers, &Rect( n100 * 8, 0, (n100+1)*8, 11 ) );
display->Blit( 8, 0, m_numbers, &Rect( n10 * 8, 0, (n10+1)*8, 11 ) );
display->Blit( 16, 0, m_numbers, &Rect( n1 * 8, 0, (n1+1)*8, 11 ) );
}
++m_FPSCounter;

// Update screen
display->Update();


if(QUIT)
Shutdown();

}



void BlitSample::StylusDown( Point stylus )
{


// Check if the point is on one of the images
for (int i = 3; i >= 0; --i)
{
if (m_position[i].Contains( stylus ))
{
m_dragImage = i;
m_dragOffset = stylus - Point( m_position[i].left, m_position[i].top );
break;
}
}

}




void BlitSample::StylusUp( Point stylus )
{

m_dragImage = -1;

}



void BlitSample::StylusMove( Point stylus )
{

if (m_dragImage == -1)
return;

Point newPosition ( stylus - m_dragOffset );

const Surface* image = m_images[m_dragImage];
Rect limit( 0, 0, GetDisplay()->GetWidth() - image->GetWidth(), GetDisplay()->GetHeight() - image->GetHeight() );

if (newPosition.x < limit.left) newPosition.x = limit.left;
else if (newPosition.x > limit.right) newPosition.x = limit.right;

if (newPosition.y < limit.top) newPosition.y = limit.top;
else if (newPosition.y > limit.bottom) newPosition.y = limit.bottom;

m_dragOffset = stylus - newPosition;

m_position[m_dragImage].Move( newPosition.x, newPosition.y );

}


void BlitSample::ButtonDown( int button )
{

if(button == m_keys.vkA)
{
// Shutdown();//end the game
QUIT = true;
}

if(button == m_keys.vkB)
{
RESTARTFLAG = true;
QUIT = true;
}

}


//////////////////////////////////////////////////////////////////////////////
//
// WinMain - Entry point
//
//////////////////////////////////////////////////////////////////////////////

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE, LPTSTR, int )
{
_Module.Init( 0, hInstance );

/* BlitSample game;

game.Run();
*/

RESTARTFLAG = true;
while( RESTARTFLAG )
{
RESTARTFLAG = false;
QUIT = false; //this is set when user wants to exit

BlitSample * game = new BlitSample();
game->Run();
}


return 0;
}
Thanks for this code, but it does not help.
Blit did not restart on my device and emulators.
I don't know why!!!

i think trouble in the PocketFrog becouse i changed it several month's ago.

I have not got time for problem solving now, but
i would write here as soon as i do it.

many thanks for help