Archived

This topic is now archived and is closed to further replies.

cody2002

How to avoid a while loop in game intro???

Recommended Posts

Hi! The intro of my game uses a while(quitintro==false) loop to check an input command (gamepad or keyboard using DirectInput). How could i avoid the while loop? My game needs 100% CPU and the user (gamer) is not able to move the window. Cheers, codx

Share this post


Link to post
Share on other sites
We need more info! I take it you are saying that you have a chunk of code somthing like:

void checkKeys()
{
boolean quitintro = false;
while (quitintro==false)
{
if (CHECK_FOR_KEYPRESS)
{
quitintro = true;
}
}
}

and that all your program is doing is sitting there waiting for a keypress? There are a couple of ways around it...

Here is an example straight out of the DirectX 8.1 Reference manual available on-line at msdn.microsoft.com/library Look in the graphics section, there''s all the DirectX documentation in there.

void WINAPI ProcessKBInput()
{
#define KEYDOWN(name, key) (name[key] & 0x80)

char buffer[256];
HRESULT hr;

hr = g_lpDIDevice->GetDeviceState(sizeof(buffer),(LPVOID)&buffer);
if FAILED(hr)
{
// If it failed, the device has probably been lost.
// Check for (hr == DIERR_INPUTLOST)
// and attempt to reacquire it here.
return;
}

// Turn the spaceship right or left
if (KEYDOWN(buffer, DIK_RIGHT));
// Turn right.
else if(KEYDOWN(buffer, DIK_LEFT));
// Turn left.

// Thrust or stop the spaceship
if (KEYDOWN(buffer, DIK_UP)) ;
// Move the spaceship forward.
else if (KEYDOWN(buffer, DIK_DOWN));
// Stop the spaceship.
}

The key parts for you are the macro KEYDOWN which returns true if a key is held down when the call to GetDeviceState occurs. This is useful to check for specific keys being held down, in this case, the program is only interested in the left, right, up or down arrows being held. (Note the structure of the if and else if allows for diagonal movement)

This isn''t any good if you''re after a keypress, regardless of which key it is.

Have a look at the sample code that comes with DirectX and look at the Keyboard utility, there''s loads of interesting code in there!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
In order for the window to be responsive you must process events/messages. If you''re using Windows, something like this should do it:

while (!quit_intro)
{
MSG msg;
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

// Do the other stuff here (and check for key presses etc.)
}

Your program will still likely use 100% cpu, though. You could insert a Sleep(0) call in there too if you want to give some extra time to other processes. For further info search MSDN.

Share this post


Link to post
Share on other sites

That event checking must be the right way! Will try it when i´m home

@first reply: i read keyboard asynchron with KEY_DOWN or such macros but that doesn´t help.

Will try PeekMessage and Dispatch ...

Thanks for the first replies!

Share this post


Link to post
Share on other sites