Base code

Now i want to get one thing straight before i start explaining things, this tutorial is not a full sized tutorial, no it’s just a small entry of misc comments about the changes of the nehe base code, it does not contain all changes, like variables and such.
Also this tut is mainly for win32 systems, my guess is that if you have another system then you might just use this tut as an inspiration.
Also there is not comments in the code below, just download the base code from the bottom of the page and look in that

Ok, lets begin.
The first change i made was that i added not one but four timing systems, the timing system is what makes the animations smooth and it dictates the speed of things.
I have included the following timing codes

Full delta time: it runs as fast as it can and reports the delta time to the update function, it gives max performance and works well for most things except physics

Full delta time paused: as above, but includes a sleep function in order to try and cap the framerate since it’s useless to run at a higher framerate than the monitor refresh rate, this also saves in on a lot of cpu time.

Set delta time: runs the update function at a set framerate: this saves in a little on performance and will work well with physics

Set delta time paused : again as above but also caps the actual rendered framerate with the use of the sleep function.

Here is the code itself


// timing functions
tickCount = GetTickCount ();
window.deltaTime = ((float)(tickCount - window.lastTickCount))/1000;
window.lastTickCount = tickCount;

switch(timingFormula)
{
case TIMER_FULLDT:
Update (window.deltaTime);
Draw ();
SwapBuffers (window.hDC);
break;

case TIMER_SETDT:
dtTemp+=window.deltaTime;

while (dtTemp>window.frameRate)
{
Update (window.frameRate);
dtTemp-=window.frameRate;
}

Draw ();
SwapBuffers (window.hDC);
break;

case TIMER_SETDT_PAUSED:
dtTemp+=window.deltaTime;

while (dtTemp>window.frameRate)
{
Update (window.frameRate);
dtTemp-=window.frameRate;
}
Draw ();
SwapBuffers (window.hDC);
timeSpent=(float)(GetTickCount()-window.lastTickCount)/1000;

if (timeSpent
{ Sleep((unsigned long)((window.frameRate-timeSpent)*900)); }
break;

case TIMER_FULLDT_PAUSED:
Update (window.deltaTime);
Draw ();
SwapBuffers (window.hDC);
timeSpent=(float)(GetTickCount()-window.lastTickCount)/1000;
if (timeSpent
{ Sleep((unsigned long)((window.frameRate-timeSpent)*900)); }
break;

default:
Update (window.deltaTime);
Draw ();
SwapBuffers (window.hDC);
break;
}

A bit much perhaps, but in essence it all about supplying the right values to the update function.
This in coincidently the only thing i changed with the interface to the actual application ( the init, draw and update functions), i changed it do that it sends the delta time in seconds instead of milliseconds.
Its better that way, it’s more clear in how to use it and it is a SI (Systeme Internationale) standard.

Using set delta time you have the possibility to do some cool time effects, normally the update function updates at the same rate as is given to it but if you change the value it gets to lets say a tenth of the update rate, you get some kind of bullet time effect, this is something you could play around with later on.

Now for the second thing i changed.
The original base code could switch from windowed to fullscreen at the push of a button, this is good, but unfortunately it was done in such a way that the context has to be released, this means that all the nice little textures you have loaded disappears.
The way i do it means that the render context does not get released, but this was only possible if i sacrificed one thing, the border around the window, but that’s ok, i don’t need it and it actually makes the window smaller then intended.

To change thing then first make sure the two top lines in the CreateWindowGL function looks like this.

DWORD windowStyle = WS_POPUPWINDOW;
DWORD windowExtendedStyle = WS_EX_APPWINDOW;

Next in WindowProc replace everything between case WM_TOGGLEFULLSCREEN: and the following break; with this

g_createFullScreen = (g_createFullScreen == TRUE) ? FALSE : TRUE;

if (g_createFullScreen== TRUE)
{
ShowCursor (FALSE);
SetWindowLong(hWnd,GWL_EXSTYLE,WS_EX_TOPMOST); ChangeScreenResolution (window->init.width, window->init.height,
window->init.bitsPerPixel);
SetWindowPos(hWnd,HWND_TOPMOST,0,0,window->init.width,
window->init.height,SWP_NOZORDER);
}
else
{
ShowCursor (TRUE);
SetWindowLong(hWnd,GWL_EXSTYLE,WS_EX_APPWINDOW); ChangeDisplaySettings (NULL,0); SetWindowPos(hWnd,HWND_TOPMOST,window->x,window->y,
window->init.width,window->init.height,SWP_NOZORDER);
}

Now this code will now toggle fullscreen/windowed without releasing the render context.
It will Also make sure the window is topmost, has not mouse cursor and is always positioned at the upper left corner when in fullscreen mode.
As a bonus the code will also return the window as it was when switching back from fullscreen.

Download the MSVCPP 6.0 base code here.


No Comments

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

*


WordPress Themes