MFC Game Loop

Started by
3 comments, last by Nullio 23 years, 5 months ago
I''m reading "Tricks of the windows game programming gurus" By Andre LaMothe (very sweet book, a must read) and all of the examples are done is precedure style programming. I''m a OOPer myself and am wondering how to convert 2 things from precedure programming -> Object Programming. 1) The main game loop. I''ve been putting my game loop in the ::OnIdle method, is this the best way? 2) Creating a frameless window, I set up the code and flags perfectly but when I start DirectDraw and go full screen there is a border around the screen. here''s the code I''m using: //---Variables--- CString WndClass; //---Window Int Code--- WndClass= AfxRegisterWndClass(CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW, LoadCursor(NULL, IDC_ARROW), (HBRUSH)GetStockObject(BLACK_BRUSH), LoadIcon(NULL, IDI_APPLICATION)); CreateEx(NULL,WndClass, "Direct Draw Test",WS_POPUP | WS_VISIBLE, 0,0, 640,480, NULL, NULL, NULL); //---Thats All /* Thanks to anyone who can help P.S. If anyone loves game programming please find me on icq (please be a nice person who doesn''t mind being asked questions)
Advertisement
quote:
1) The main game loop. I''ve been putting my game loop in the ::OnIdle method, is this the best way?

NOOOOO!!! Fel will back me up on this. You almost never want to override the default behavior of OnIdle in an MFC app.

As for what the best method is, it''s hard to say. Maybe OnTimer if it''s not critical (doubtful). Maybe in a separate thread? The problem is that a "game loop" is usually a procedural concept; when you move from that to a message-based program, like windows, it might not be appropriate.

Re: 2nd question, I''m not used to changing the flags (only write business apps), but I believe WS_POPUP includes a border; you might want to find that #define and pick only the flags you want.
First off I''m going to beg you: please please PLEASE do NOT use OnIdle. Ever. Because if you do, in about a week you''re going to be back here complaining that you added some AI and all the sudden your game loop isn''t processing because OnIdle is never guaranteed to be called. Set up a windows timer and loop when you get a WM_TIMER message *if* you''re doing something that requires animation. If you''re doing something simple, like say a board game, then you just need to process mouse and keyboard input events. If you''re trying to do background stuff like AI, throw it in a worker thread.

As for getting rid of your frame: you can get rid of your frame and make your window basically any shape your heart desires (I have a pretty darn cool rhino-head-shaped one, personally) if you use CRgn. Rather than go into an in-depth explanation I''ll send you here. It''s Spiffy(TM).

-fel
~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~
WM_TIMER is [a really bad] way to render, it''s worse than locking the frame rate...

If you do use OnIdle, as soon as you click on the one of the menu items or move the window, it stops & stalls. You can use a seperate UI thread and override its OnIdle to run the game loop.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara


I have had moderate success overriding CWinApp::Run to include calls to my rendering code, for what its worth.

This topic is closed to new replies.

Advertisement