[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
BeginnersGuideToGameDev

COMPLETE GAME PROGRAMMING GUIDE - I

[I] An Introduction To Game Programming


So you want to be a game programmer? You want to know how did they manage to program all those fast action graphics that ate up all your creative time. I too as a child was intrigued by simple primitive games like paratrooper pacman and the likes. Then came Prince of Persia and Wolfeinstien 3D which started a never ending series of 3D games.Game programming though most interesting is not the simplest. The reasons being speed, complex AI algorithms, 3D algorithms and physics. We have to develop our own routines and cannot depend on the standard ones as they are too slow for games(Edit: This article is old. There are very lots of excellent game engines available and dependency on hardware has increased]. Let's see what i'll be covering in this series. You can't just directly jump on to making fast action 3D windows games. Though I'll be covering Direct X programming a li'l later which will require a knowledge of Visual C++ We'll start with simple C/C++ programming for MS-DOS.


[1] Programming the VGA
NOTE: For this section you need to have a knowledge of C/C++ and a little bit of Assembly Language programming.



Ok so we are all set to make our first game with superb graphics, fantastic gameplay and so on. So what are we waiting for ? Here We Go ......
Most of you must be familiar with the ROM-BIOS functions that dos provides for creating graphics [rather dull graphics]. Have you ever tried making a game with these functions. What was the final outcome ? Jerky, flickering ,dull images making you feel like banging your head on your keyboard ? All these functions are too slow as they are quite high level and hardware independent. Well these are also the things that we desire but we cannot trade off with speed. So we start by making our own routines.


[*] Setting Video Mode
To set a mode manually we will have to set up a lot of registers. Hey but why go into that much trouble ? Although the ROM-BIOS functions are slow, the function for setting the mode is usually called only twice once for entering into graphics mode and once for coming out of it. So this is easy. Let's code it --
void SetMode(int ModeNumber)
{
        union REGS regs;
        regs.x.ax = ModeNumber;
        int86(0x10,&regs,&regs);
}
NOTE: You need to include DOS.H for this function to work

What have we done here ? Function 00 of Interrupt no 16 (or 10h (hexadecimal)) is used for setting the video mode. so to set mode -
Call Int 10h
With AH = 00h
And AL = Mode Number


You can try various modes. The best VGA mode of all the predefined modes for game programming is unambiguously mode 13h. It uses linear organization (to be discussed shortly). it supports 8 bit colors that is you can have 256 colurs at once. Resolution is 320 * 200. The obvious question that you would ask is "On my pc I play games at a resolution of 1024 * 768 with 16 million colors and that too with dazzling graphics, then how come this dumb mode of yours became the best mode ?". Well you are right but here we are talking about VGA and we are using 16-bit compiler. 16-bit programming or Real mode programming isnt fast enough for such a heavy mode. If you do program in some heavy mode then your Real Mode games will all become kinda slide show. "So how did they do it ?" They used protected mode programming (This topic will be covered later after we finish through our 16 bit games). The games that are in market now a days use directx programming under windows (Don't worry dear ! This would also be covered, but much later).I mentioned that mode 13h is a linear mode. What the heck is this linear mode.Here I describe ....


[*] Linear Organization
Whatever you see on the screen of your monitor is actually stored in memory. This organization can be done in follwing ways
1. Linear Organiztion
2. Planar Organization
3. (Let's say) Hybrid Organization
Linear organization is the simplest. Each byte of display memory is used to store one pixel. So the first byte represents the colour of the first pixel, the second byte of the second pixel and so on. Enough of this magazmaari , Let's code something for mode 13h. Um.... let's make a function for plotting a pixel on the screen. Here's the function -
void PlotPlanar(int X,int Y,int Color)
{
                /* starting address of video memory */
        char far * DisplayStartAddress;
        int Offset;
        DisplayStartAddress=(char far *) MK_FP(0xA000,0);
                /* calculate offset */
        Offset=Y * MODE_WIDTH + X;
                /* write to video memory */
        *(DisplayStartAddress + Offset) = Color;
}

Hey Hey wait. Have mercy on your keyboard newbies. I'll explain the code. As i told you before whatever there is on the screen is stored in memory. Now the starting address for this memory is 0xA0000000h,a far pointer. MK_FP returns far pointer by taking two arguments base and offset. Ok we now know the starting address. Next we have to calculate the offset. As organization is linear that is one byte per pixel. We just multiply the width of the screen to Y and add it to X. So we get the required offset. Add this to Starting address and we get the address of the pixel. SIMPLE !!!!!


[*] Planar Organization


To understand Planar Organization imagine a monochrome linearly organized page. That is only one bit is required for color instead of a full byte. Wow ! this can be really fast as 8 bits can be set at one go. (only 2 colors ??? What age do we live in ???) Hold on Everything would come out perfect. To increase the number of colors what is done in most modes of VGAs is 4 such planes are stacked over each other ( What the heck !!!! ) Ok so we have 4 bits for 1 pixel (one from each plane). So number of colurs supported equals to 2^4 bits = 16.



What am i going to cover in next series ?? Here's a peek -
-Plotting a pixel for planar mode using VGA registers
-Hybrid mode Mode X and Y. -Animation -Bitmaps And BitBlts -Dealing with problems in animation - Flickering,shearing,strobing .... -Programming Keyboard Handlers And after we finish through this we will start 32 bit programming -Protected Mode Basics -VESA modes -Using DOS4GW and other protected mode interfaces And Then -An Introduction to Visual C++ -Visual C++ for Game Programming -DirectX Programming -OpenGL Programming

and so on .............

last edited (April 16, 2008) by berserked, Number of views: 8295, Current Rev: 3 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.