🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Hi I need help with some questions

Started by
1 comment, last by xterminenatorx 22 years, 10 months ago
I have been wondering about this for a long time now. I understand some things about programming but its not clear how do you set it up.. I mean I know you first set up includes,defines, windproc next I think and then from here on i get stuck. how shjould a game flow and where should all the draw bitmap commands and AI commands be under. PLEASE HELP. If theres turtorials around Please letr me know THANKS
Advertisement
Here is a start.

YAP-YFIO
-deadlinegrunt

~deadlinegrunt

Hi, xterminenatorx.

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:

while(1)
{
if(PeekMessage(&msg, hwnd, 0, 0, PM_REMOVE))
{
if(msg.message == WM_QUIT) break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
//Game Loop
GameMain();
}

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:

        void GameMain(void){    GetInput(); //Get input first    DoAI();     //This is where you'd generally do your AI and    DoPhysics();//physics, etc.    DrawFrame();//This function should contain the code that                //actually draws the frame to the screen.                //To answer one of your questions, yes this                //is where all the bitmap drawing goes    DisplayFrame(); //Here's where you page-flip if you're                     //using DirectX, or where you blit your                    //buffer to the display or whatever.}        


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

This topic is closed to new replies.

Advertisement