Timers

Published May 30, 2008
Advertisement
Tonight I worked through my implementation of "timers" a little bit, and fleshed out how I want the game to run a little more. My "delay" techinque, quite simply, was restrictive. Luckily, I found out about the SetTimer function. I had to rework the main loop of my program (as seen in my last journal entry), but I think it was definitely worth it.

My current approach leaves the keypress checking to the main loop, and moves the actual HUD updates to a TimerProc function. I have to create a timer with SetTimer and pass it the address of TimerProc, so during my main loop I also check for messages, and translate/dispatch the WM_TIMER messages. Dispatching the WM_TIMER message, if it was caused by the timer I created, will call TimerProc!

Below is my new and improved main() function, as well as my current TimerProc function.

void CALLBACK TimerProc(HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime){    static bool timestatinc = false;    //Because this function is called every 500ms, I need to keep track    //of whena full second has passed. Only then do I update the time data.            if (timestatinc)        ++GameInfo.time;        GameInfo.score += 10;        hud.Draw(0, 0);        timestatinc = !timestatinc;}int main(){    Init();    hud.Draw(0, 0);            if (!SetTimer(NULL, 1, 500, &TimerProc))        return 1;        while (true)    {        int vkey = CheckForKeypress();        if (vkey == VK_ESCAPE)            break;            MSG msg;            if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))        {            if (msg.message == WM_TIMER)            {                TranslateMessage(&msg);                DispatchMessage(&msg);            }        }    }        KillTimer(NULL, 1);}


I'm still not sure what the commenters meant in my last post, but I'm pretty sure this approach will be sufficient for Cripes!. Thanks for the input though, always great to have! *hint hint*


EDIT: In other news, my grandmother let me pick out and buy three books, which totalled to $140 total. I know, it's mean for me to do that, but she let me AND she said it would be an early birthday present. "As long as you read them and learn from them, I'm fine!" So, yeah. I'm now the proud-to-semi-proud owner of World of Warcraft Programming (addons and such), Breaking Into the Game Industry (my favorite so far!), and Windows From C/C++ (at $70, I should like this best, but... I don't. It's handy though, and I got the SetTimer idea from it!)
Previous Entry Timed stuffs!
Next Entry Playing view
0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement