Hi I need help with some questions,
Members - Reputation: 122
Posted 18 August 2001 - 11:38 AM
Members - Reputation: 122
Posted 18 August 2001 - 07:35 PM
I think I can help. You could probably set up a game any way you want and still get it to work, but you're probably better off doing this:
Okay, basically you have your WinMain, as you mentioned before. As you probably know, normal Windows programs have message loops within WinMain. A game however, should have a slightly different message loop than other applications. It should look something like this:
if(PeekMessage(&msg, hwnd, 0, 0, PM_REMOVE))
if(msg.message == WM_QUIT) break;
The above code assumes your MSG struct within WinMain is named msg. This code should be at the bottom of WinMain. Basically, games should set up a frame, and then render it on the screen. Then they should set up the next frame, render it again, and keep drawing frames until the game exits. That's why the above message loop is used. The call to GameMain (you don't have to name it GameMain, it can be anything) is made constantly, and within GameMain you should set up and render each frame.
Also, an easy way to do things is to have a finite state machine, which basically means your game can be in different 'states'. For instance, your game can be in STATE_LOADING when it's being loaded, or STATE_RUNNING when the game is running, STATE_PAUSED, etc. Whatever you want. I'll explain how to do this in just a minute. Getting back to GameMain, your GameMain function should look something like this:
Now, as I mentioned, if your project is going to be of any size, it should have seperate states and stuff. Basically, you just keep a variable to keep track of the game's state. You should #define all of your game states to use for the value of this variable. For instance, if you have a menu in your game, when the game starts you could set the game state to a STATE_MENU or whatever. Then, in GameMain, it would check the state, and if the game was in a menu it would render the menu. That's basically how it works. For some instances, it may be useful to save the previous game state as well as the current.
I really hoped I could help. Note that the code for GameMain above is very simplified. There is a lot more you should do in it (such as calculate the framerate). You should also maybe time your input so the game only checks the input every so many milliseconds.
And as for WndProc, you really don't need to process any windows messages other than a couple (in some cases), like when you want to know when the app loses focus. So you should mainly just let DefWindowProc process everything.
Finally, I would recommend that you keep your code in several source files so that everything's seperated and easy to find. Also, keep every part of your game independant of the rest of the game so you can make changes easier.
Edited by - Midnight Coder on August 19, 2001 2:37:52 AM