Boone's game programming Tutorial: 1!


#4

CREATING A SOLID PROGRAM STRUCTURE…

NOTE: This should be the first agenda on your list. I just wanted to show you the game-loop theory to assure you that games programming isn’t a god-like task but something that can be broken down into simple steps…

Okay, lets clear our minds.

When you begin programming in most languages, the first function( I’ll use this term for now…) you’ll program is the “entry-point” - or the “Main” function. Think of it as the “General” of the enitre program. It will call a small amount of other functions - who in turn will call many other functions to do a specific task…a bit like a “tree-diagram”.

This concept should be familar to any programmer!

Now, then - lets take a look at the contents of a “General” function in a computer game…

Call 1: Program initialisation etc...
Call 2: Copyright & credits.
Call 3: Game/Story Introduction.
Call 4: The game itself...
Call 5: Cleanup and exit program.

…After “Call 5”, the program will end and return back to the calling Operating System. Each of the calls are functions - being called to perform an indivial part of the program. But the one thing that I want you all to take note of is this - EACH OF THESE FUNCTIONS COULD CONTAIN A “GAME-LOOP”!

However, before we get back to the game loop, we must also talk about an important group of variables that can act as a group of signals for the purpose of deciding which part of the program we want to go to. So lets make a “command group” of variables…

1) Current_menu_selected
2) Exit_program
3) Number_of_hours_played

…and thus we have three command( or global ) variables. Now we have these, we can use them to decide which part of the program we want to go to. The first variable, “Current_Menu_Selected”, would be used as a selector for a “Switch” or “Case” statement. Lets look at the “options” for a basic “Which menu shall we enter” switch/Case statement…

1) Menu for general game options.
2) Menu for Graphics options.
3) Menu for Audio options.
4) Menu for Controller calibration.
5) Start a new game.

…so maybe at run-time, you would encounter a menu that presented the above items - you would press “1” on the keyboard and thus the value stored at “Current_menu_Selected” would be changed to the value of “1”. So of course the switch statement will select the first item in the selection list and go to the area of the program that deals with general-game-options. This could be another basic function call…

Now at some point you want to be able to exit the program. That could be where the “Exit_Program” variable comes in. Storing a basic “True or False” value in “Exit_Program”, we can use these two values to decide in a conditional statement if we want to…ummm…exit the program! This could be in the “Main” or “General” function - most likey in a simple loop.

If there is a menu option that asks “Exit back to Windows” - we select it and the value stored at “Exit_program” changes from “0”( False ) to “1”( True ). And there for the “General”/“Main” function will then call the function to clean-up and then return control back to the operating system.

Lastly, there is “Number_Of_Hours_Played”. I’ve thrown this in as an example of additional information that you may need access to. For instance - how many games have you played where a “hidden extra” appears when you have played for a certain amount of hours? You get the idea. This is how important setting up a good program-architecture is…

Phew! A few basic concepts there that aren’t usually considered by most programming authors…and yet before we are finished, we must look a little more indepth at the “Game-loop” again…


#5

KEEPING THE GAME LOOP’S FRAME RATE…

WIth the incredible processing rates of the new computers, one must make sure a program sticks to a set frame rate.

NOTE: There are many books out there by Robert Dunlop, Andre LaMothe etc, that covers this in greater detail but I just want to make you aware of how to go about “designing” a frame-rate system…

It basically goes along the lines of first checking to see if 1/30th of a second has elapsed - if so, let the game loop do its business. If not, wait until it has…

The functions for obtaining the current system time varys from language-to-language, but by creating the following set of variables…

1) Current_Time
2) Last_Time
3) Elapsed_Time
4) Execute_game_loop
5) Frames_Per_Second

…we can keep track of the nessecary information that we need. The variables speak for themselves, except “Last-time”…

When you actually execute a frame, whatever the current time is, you place it into “Last_Time”. This way, you can subtract it from “Current_Time” to determine if 1/30th of a second has elapsed…


#6

THATS IT FOR NOW, BUT BEAR IN MIND…

Well, I hope you have a little idea now, on what to do first regarding developing a game. What I’ve discussed here, in a nut-shell, is nothing more that using a couple of basic functions in conjunction with their accompanying data-structures for them to use in making decisions…

With this knowledge, you can quite easily create a menu-based game with confidence. However, just make sure you plan it out first!


#7

FUTURE TUTORIALS

Right, what I have tried to do is give you guys a few “concepts” - not specific code. I program in C++ using MS Visual C++, and use DirectX 7.0’s DirectDraw componet for my Visual output. I’d like to know what you lot are using…

If you want more specific tutorials etc, then you should really be looking at “www.gamedev.net”. This is basically the CGTalk.com for games development!

Anyway - rant’n’rave at me about this tutorial. Its the first I’ve ever done, and I wasn’t too sure what you guys wanted so I just slapped it together! With a bit of feedback, I can improve future tutorials.

Happy games programming, guys!


#8

So are you going to describe a layered tiling engine ala Jazz Jackrabbit or sort of a hybrid engine like the Megaman games)?

You can still blit and draw sprites with DirectX 9.0b :p. It’s just the blitting functions have moved into the D3DX utility library (D3DXCreateSprite). He he he, I remember DirectX 7.0… ooo the memories. Give DirectX 9.0 a try, you’ll be glad you did :p. Programming DirectX is still nasty because it uses COM and stuff like this

struct HELLOTHISISALONGASSTRUCTTHATTAKESFOREVERTOTYPE
{
DWORD dwSize;
union {
DWORD dwBlah1;
DWORD dwBlah2
};
WORD wValidFlags;
// a million other structure members
};

LOL. My thoughts on DirectX :p. But I love how you don’t need to load special extensions to use vertex buffers as you do in OpenGL because Microsoft will never update their OpenGL header beyond 1.1 and the D3DX library is getting better and better with each new version :cool:. It’ll be interesting to see how Microsoft handles OpenGL 2.0 when it comes out. If they spit on it and ignore new specifications as they have done in the past, it’s dead in the water.


#9

Re: Dead_Eye.

Phew! You didn’t slap me round the face for such a lousy tutorial - thanks!:beer:

Yeah…DirectX 9.0. I’m yet to order Visual C++.NET 2003 for my new PC, so as soon as I get my act together - I shall be going through DirectX 9!

One question about DirectX 9 - is it still possible to draw a single pixel to the screen? You could do this by manipulating a surface directly - y’know, locking/un-locking a surface…that sort of thing?


#10

No, I would never flame anybody :).

Sure you can, you use the IDirect3DDevice9::GetBackBuffer to get an IDirect3DSurface9 pointer. From there, you call IDirect3DSurface9::LockRect to get a D3DLOCKED_RECT structure, for which one of the members is void* pBits, which is a pointer to the locked surface bits.


#11

Re: Dead_Eye.

Thanks! This is very important information for me…I shall investigate it further!:applause:


#12

Good start there Boone,

Quite the undertaking… can’t wait to see more. Glad you went the general route rather than the app/platform/language specific direction. I am interested to see if I can apply the later tuts to a shockwave game I am pondering. Keep it up :thumbsup:


#13

Re: JDex.

Oh, thank God for that!

I just assumed that some of you guys most likely work for companies that program for PS2, Shockwave and other assorted languages/apps that didn’t use DirectX…

The only thing I need from you guys is to be able to make something similar to “Structures” - IE a group of variables that can be accessed by a single name or label…thats kinda important! The rest can be “tailored” to suit the language/app you’re using…

BTW, what kind of game are you pondering? I don’t need the game’s ideas etc, just what kind of game it is. If you need to Draw lines, AI scripting, advanced collision detection - just tell me what you need from the game and I’ll consider it for a tutorial!

Cheers.:beer:


#14

No probs Boone :). Yeah, ever since DirectX 7 I’ve wanted to make a 2D game engine too, like the jazz jackrabbit one, because it’s not that hard – you don’t have to worry about vertex buffers, there are simple particles, simple collision detection, simple animations, no need for BSP trees – it’s just that I suck at ART! :scream:


#15

Re: Dead_Eye.

You suck at art? Well, I found making animation for games to be quite a challenge at first. However, I realised that I was trying to run before I could walk…

To be honest, My first computer was a ZX Spectrum 48k+! And that ultimately limited machine is the reason why games-programming clicks with me. With only 16-colours and 48k of ram to work with, one had to be resourceful. You’d be surprised at what SFX was accomplished with this under-powered machine…

I hope to cover this “limited” side of programming in a future tutorial…in the meantime - go hunt down a speccy emulator and the “Myth: History in the Making” rom!:applause:

ONE MORE THING! I’m kinda crap at collision detection. The best I can do is check to see if two “rectangles” overlap…so I can only make R-type-Without-scenery! And thats it…I have idea on how to make a platformer…I really need help!:sad:


#16

Very cool start…

Keep it up man :thumbsup:


#17

Good news, guys - I got Cinema 4D R6 CE free with 3DWorld magazine, today!

I’m now able to make…DIRECT3D FILES!!!:buttrock:

All I need is my new copy of Visual C++.net 2003 to arrive on my doorstep and I’ll be going through the 3D side of things in no-time!!!:applause:

BTW, I’m considering a “Font/text” engine for my next tutorial…I figure that this is an important topic before rushing off into hard-core 2D…is that okay with you guys?:shrug:


#18

Post anything you feel like :). The more information around, the better.


#19

hey nice work boone.
sorry i could not reply earlier(underwent surgery on my nose :wip:)…am still messed up a lil :p.

i’ll keep the crits till i’m in a position to add something worthwile.

thanks for doing this for the cgtalk peeps ya :slight_smile:

b


#20

Boone that really was a great tutorial and i’llbe very happy if the next are C++ specific. As said write whatever YOU want to write about it will make the tut come out better (and faster)
Thanks again!! :thumbsup:


#21

Re: Barbas.

I’d love to do C++ specific tuts( I bought a new version of Visual C++ last night! ), but I should really keep it to language/app independant. The information I’m giving you will help you become a “true programmer” - not someone who has to beg Microsoft, Sony or Nintendo for a API call for every little task…

Still, if you’re having a problem with C++, give me a Private-message. I will help you.:beer:

Re: Boomji.

As my game is nearing “mop-up” stage, I’m still pondering over my next project…

I have an Idea for a game that invloves gameplay like the original “Strider”, but I know shit about collision detection…

For example - say you have an object such as a five-sided polygon and make sure your character doesn’t go-through-it??? Ya’know - like making Strider-Hiryu walk around on the ST.Petersberg roof-tops?

That I really need help on!


#22

:beer: :beer: :beer:

can you give a preview of your demo???

I look forward it!!!


#23

Ncie work Boone, the article covered fundementals that are important to all languages and API’s it will certainly be nice to see future tutorials.