Scimac,
The reason that the books do this is because every game, is well... different. The difficult part is in understanding how to use logic to control the flow of your game.
It's basically a process of figuring out what data you need to store, how to store it, how to retrieve it, and how to use it to drive the display/graphics in the game, and finally how to update all of the logic for the next frame.
Sometimes this is is easier in a procedural language like DB (as opposed to object oriented) and in some cases it's easier (albeit usually messier - at least the way I write code)
====
All games have one major outer game loop. It can look as simple as this
`init variables
gameover=0
while gameover=0
`here is all the actual game code
loop
So,
gameover is a variable. In this case it could be an
int or a
bool type. As long as gameover is = 0 the game will loop. If gameover is set inside the loop to anything other than 0 (ie. 1) the game exits.
====
OK. I know that all the above was pretty obvious. But lets take it a little further.
So lets further define the game loop.
A video game: Is basically a continous loop that performs logic and draws an image on the screen, at a rate of 30 FPS or more. (Nowadays, 60 FPS is usually the mininmum
Here is a General game loop architecture from TOFTWPG by Andre Lamothe. (His explanation here will suffice)
Initialization:
-Allocate Memory
-Load Files
-Build tables
-Initaialize variables etc...
Main Event Loop:
-Exit? Y/N
If YES then break out of the loop and perform cleanup processes
such as:
Close files
Realocate memory
Exit back to the O/S
If NO then we Enter the game loop. This is where all the action takes place until the user quits, or the game ends. Most every game does the following
-Init Timing (ie. sync rate &/or other timing code to control the speed of the game events
-Retireve Player Input (KB, Joystick, Mouse etc...)
Main Logic
This section contains the majority of the game code. Artificial Intelligence, physics systems, and general game logic are executed and the results are used to draw the next frame on the screen.
-Game AI
-Collision Detection
-Physics
-
Render Next Frame
In this section, the results of the player's input and the execution of game AI and logic are used to generate the next frame of animation for the game. The image is usually drawn on an off-screen buffer area , so you can't see it being rendered, then it's copied very quickly to the visible display. [*note The buffering, and sync rate described here is performed automatically for you with DB's Sync rate and sync commands]
Now everything is locked to a certain FPS (or other additional timing code] and we go back to the beginning of the loop
=====
OK. So now your probablly like "Great, I already knew that", so the question becomes how do I make all of this happen???
Unfortunatley this is where every game, (and programmer for that matter differs on approach). Making the case worse is that usually looking at a finished games code doesn't help much, as even with good commenting the amount of logic and things going on become hard to follow if you were not the one programming it, debugging it, adding to it, re-coding it etc...
So what can one do? The most important thing (IMHO) is to understand data structures - what they are, How they work, and finally how to put them to use to make keeping track of all the logic taking place on every frame somewhat manageable. Almost every game, in fact I can't think of one that I have worked on that doesn't, has arrays. Usually mulitdimensional arrays. Your really need to understand them. From there you can move on to other data structures such as stacks, queues,
linked lists, Binary Search Trees etc...
More important than understanding how to define these structures, and how to get data in and out of them, is understanding how to use them to implement logic in your game.
=====
Say for instance the game tic-tac-toe. If I wanted to keep track of of the players X's and O's as well as open spaces, I would consider using a 2-dimensional array.
An empty board woulod look like this:
First in the initialization part of the game (before the main loop) I would dimension my array (allocate memory space for it). A tic-tac-toe board is a 3x3 grid for a total of 9 spots.
dim board(2,2)
(*note in DB arrays start a 0 snd up to the number in the dim statemeny. So 0 to 2; is a 3x3 grid)
Next I would initialize all values of the array to
0. Now I'll show you the method I use to fill a large array. (Keep in mind that a small array such as 3x3 can be easy to maintain, but the following code will help you see how to initialize a large array as well; say 100x100 or more)
for y=0 to 2
for x=0 to 2
board(x,y)=0
next x
next y
so now if we looked at the data elements in the board array it would look like this
012 X
0 000
1 000
2 000
Y
That would be the logic for an empty board. Now in the main game loop whe a player tries to place an X or an O all I need to do is check whether or not that location is = 0. If so, it's empty and a mark can be placed there. If it's something other than 0 say 1 or 2 then the spot is already taken.
(note: the logic could be that..)
0=empty space
1=an X is here
2=an O is here
I'll continue with this tic-tac-toe example in a little bit. Let me know if you understand thus far.
~zen