C game programming Basics

Source: Internet
Author: User
The following uses the Computer VGA 13 H video mode as an example to systematically introduce the basic methods for making computer games. Vga int 13 H mode is a mode that works in graphics mode. It has a screen resolution of X, 256 colors can be displayed on the same screen (Super Nintendo and Sega's 5-generation game consoles only have 256 colors ), many early games were developed using this graphic method (for example, xianjian Qixia, redguard, and doom ), in this mode, game development features simple programming, fast running speed, and rich colors. Although the 320x200 resolution is nothing to do with today's standards, it is still a good entry environment for learning computer game programming. we can develop a game in the VGA 13h mode, gradually learn the basic methods of game design from simple to complex, as long as you master the basic methods and theories of game design, using other software tools (such as Watcom C, djgpp, Vc, direct X, and other 32-bit C compilers) to design games will become very easy. Functions are compiled using Turbo C 2.0. You can also use other C compilers to compile and run functions. Let's take a look at the working principle of the Computer Monitor and the structure of the display card.
I. Working Principle of the monitor
Currently, grating scanning displays using cathode ray tube (CRT) are widely used in personal computers, the color we see on the screen is formed by the point of the electron beam emitted by the electron gun on the fluorescent layer on the back of the CRT screen. Different colors can be produced through the brightness of the control point. The electron beam continuously scans the screen from left to right, from top to bottom, so that the screen shows a pattern. The electron beam redraws the pattern at a rate of about 70 times per second, this process is called display refresh or screen refresh. The specific scan frequency depends on the display adapter used (also known as the display card ). The electron beam starts scanning right from the upper left corner of the screen. After arriving at the right edge of the screen, the electron beam is disabled (horizontally disconnected) and then quickly returns to the left edge of the screen (horizontally scanned) start horizontal scanning of the next row. After scanning in all horizontal directions, the electron beam ends at the bottom right corner of the screen. The electron beam is closed (vertically disconnected ), then, the system quickly returns to the upper left corner of the screen (vertical sweep back) to start the next screen scan. In this way, the electron beam repeatedly scans the entire screen. The display works in two ways: text and graphics. computer games are generally played in graphics mode.

Ii. Coordinate System of the monitor
The coordinates on the computer screen are different from the normal Cartesian coordinate system. The coordinate origin (0, 0) is in the upper left corner of the screen, the horizontal coordinate is in the right direction, and the vertical coordinate is in the downward direction, the coordinates are not negative.

3. display card structure
The display card on the monitor displays the image on the screen. The Display memory stores the image data displayed on the screen, and the display card hardware keeps displaying the content on the screen. The Display memory is actually one or several large-scale integrated circuits installed on the display card. Its capacity is 1 m, 2 m, 4 m, 8 m, etc, in dos, the memory that we can access is only 1 MB (this is the limitation of DoS). The address ranges from H to fffffh. The memory is divided into different blocks based on the purpose, the address allocated by the system to the graphic buffer (Display memory) is between a0000h and bffffh, And the size is KB. Among them, VGA occupies the a0000h to affffh segments, with a total of 64 KB, this address is the memory ing address for us to access the Display memory. In VGA 13 H graphics mode, the display memory uses a linear memory space from a0000h to af9ffh. Each byte represents a point, corresponding to a photo point on the screen, the screen resolution of 320*200 requires a total of 64000 bytes, Which is exactly 64 KB. Because one byte can represent a maximum integer of 256, each image point can represent 256 colors.

Iv. Set the video mode
Before drawing, you must make the screen work in the graphics mode. In this case, you must set the video mode of the screen. There are many ways to set the video mode. The most simple method is to call the video BIOS function. by calling the BIOS to interrupt the 0x10 service program, you can easily set the screen mode. The call method is to put the value 0 into the ah register, display mode into the Al register, and then call the int86 () function.

Function for setting the video mode:

Void setvideomode (INT Mode)
{
Union regs R;
R. h.ah = 0;
R. H. Al = mode;
Int86 (0x10, & R, & R );
}

The mode is the video mode.

5. Draw points on the screen
Because the Display memory is linearly arranged and each image point is represented by one byte, it is very easy to address the image point. The offset address in the display memory can be determined by this formula: y * 320 + X, where Y is the coordinate of the image point in the vertical direction, X is the coordinate of the horizontal direction, and 320 is the screen width. With the offset address of the image point, and the first address of the display memory, you can get the absolute address of the image point in the display memory. You only need to place the color value of the vertex to this address, and then you can draw a dot on the screen. First, create a pointer videobufferptr to point it to the first address of the display memory:

Char far * videobufferptr = (char far *) 0xa0000000;

Add the pointer to the offset address of the point, and the final address of the point is determined. It is equal:

Videobufferptr + y * 320 + X;

Write the color value color to this address:

* (Videobufferptr + y * 320 + x) = color;

The image point function:

Void drawpoint (int x, int y, unsigned char color)
{
* (Videobufferptr + y * 320 + x) = color;
}

6. display characters

Games often print text on the screen that shows the game status so that gamers can understand the status of the current game. This is essential for the game to interact with gamers. However, the text displayed in the graphic mode is very different from that in the text mode. In the graphic mode, the text must be displayed using a bitmap-based method. Here is a simple method: use ASCII font data in computer read-only memory. The ASCII font data is inherent in the computer read-only memory (ROM), which can be found on the base address f000: fa6e. We only need to know how the data is stored, and then obtain the algorithm for accessing the data, we can draw the characters in any color on the screen using the bitmap method. In the Rom, the characters are placed in the ASCII character encoding order. Because each character is 8*8 lattice, each character occupies 8 bytes of storage space and needs to find a character, we only need to multiply the ASCII code of this character by 8, and then add the result to the base address f000: fa6e. Because the characters are displayed in bitmap mode, we can specify the color (foreground or background color) for them at will ). In graphic mode, we only need two functions to print the text: one for displaying a single character and the other for displaying a string. Define the starting position of a remote pointer pointing to the ROM character set:

Char far * romcharptr = (char far *) 0xf000fa6e;

The following functions print characters and strings on the screen:

1. The function that prints characters.

Void printchar (int cx, int cy, char C, unsigned char fcolor, unsigned char bcolor, int flag)
{
Int offset, X, Y;
Char far * tempptr;
Unsigned char bit_mask;
Tempptr = romcharptr + (C> 1 );
}
Offset ++ = 320;
Tempptr ++;
}
}

Note:

CX and Cy are the coordinates of characters on the screen.

The ASCII code of the C character.

Fcolor and bcolor are the foreground and background colors of characters respectively.

Flag: Specifies the flag. When flag is set to 1, the background color of the character is displayed. Otherwise, the printed characters are transparent.

2. Print the string function.

Void printstring (int x, int y, char * string, unsigned char fcolor, unsigned char bcolor, int flag)
{
Int index;
For (Index = 0; string [Index]! = 0; index ++)
Printchar (x + (indexred );
Outportb (0x3c9, color-> green );
Outportb (0x3c9, color-> blue );
}

Function for obtaining the color register value:

Void getpaletteregister (INT index, rgbcolorptr color)
{
Outportb (0x3c6, 0xff );
Outportb (0x3c7, index );
Color-> Red = inportb (0x3c9 );
Color-> Green = inportb (0x3c9 );
Color-> Blue = inportb (0x3c9 );
}

8. Draw a bitmap on the screen
A computer usually uses a graphical processing method called bitmap. a bitmap is a rectangular lattice structure (two-dimensional matrix) that is displayed on the screen, corresponding to the area of a rectangle on the screen, the data that makes up the bitmap is stored in the memory for a continuous interval. Common bitmap files include BMP, PCX, GIF, and jpg. A bitmap is usually stored in an external file and must be transferred from the disk file to the memory before it is used. The following describes how to read a 256-color PCX image file into the memory:

1. Define the PCX File Header structure:

Typedef struct pcx_header
{
Char menufactrue;/* manufacturer ID 0x0a */
Char version;/* file version number */
Char packing_type;/* compression mode */
Char bits_per_pixel;/* number of digits occupied by each point */
Int Minx;/* Minimum X coordinate value */
Int miny;/* min y coordinate value */
Int Maxx;/* maximum X coordinate value */
Int Maxy;/* maximum y coordinate value */
Int hres;/* horizontal resolution */
Int vres;/* vertical resolution */
Char palette [48];/* color palette */
Char unused;/* not used */
Char bit_plance;/* Number of Bit Planes */
Int bytes;/* number of bytes occupied by a single horizontal line */
Int palette_type;/* color palette type */
Char unused2 [58];/* not used */
} Pcxheader, * pcxheaderptr;

2. Define the structure used to store PCX image data:

Typedef struct pcx_picture
{
Int width;
Int height;
Char far * buffer;
Rgbcolor palette [256];
} Pcxpicture, * pcxpictureptr;

3. Function for initializing image data:

Int initpcx (pcxpictureptr image, int W, int H)
{
Unsigned size = W * h;
Image-> width = W;
Image-> Height = h;
Image-> buffer = (char far *) farmalloc (size );
If (image-> buffer = NULL) return 0;
Return 1;
}

4. Functions for reading data from external files:

Int loadpcx (char * filename, pcxpictureptr image, int flag)
{
File * FP;
Unsigned num_bytes, Count, size;
Int index;
Unsigned char data;
Pcxheader;
Size = image-> width * image-> height;
If (FP = fopen (filename, "rb") = NULL)
Return 0;
Fread (& pcxheader, sizeof (pcxheader), 1, FP );
Count = 0;
While (COUNT = 192 & data0)
{
* (Image-> buffer + count) = data;
++ Count;
}
}
Else
{
* (Image-> buffer + count) = data;
++ Count;
}
}
Fseek (FP,-768l, seek_end );
For (Index = 0; indexpalette [Index]. Red = (fgetc (FP)> 2 );
Image-> palette [Index]. Green = (fgetc (FP)> 2 );
Image-> palette [Index]. Blue = (fgetc (FP)> 2 );
}
Fclose (FP );
If (flag = 1)
For (Index = 0; indexpalette [Index]);
Return 1;
}

The flag parameter specifies whether to set the color register (flag = 1) when calling the file ).

----------------------------------------------
Please click here

23:26:57

Crazy dizzy

Level: Administrator
Article: 65
Credit: 1067
Registration:
2nd floor

5. Bitmap function:

Void drawimage (int x, int y, int width, int height, char far * image)
{
Int I, J;
For (I = 0; I = 0 & (x + J) = 0 & (Y + I) = 0 & (x + J) = 0 & (Y + I) = 0 & (x + J) = 0 & (Y + I)> 8) & 0x00ff)
Int far * CLK = (INT far *) 0x0000046c;

1. Function for changing the time timer value:

Void changtime (unsigned CNT)
{
Outportb (0x43, 0x3c );
Outportb (0x40, low_byte (CNT ));
Outportb (0x40, hi_byte (CNT ));
}

2. latency functions:

Void delay (int d)
{
Int TM = * CLK;
While (* CLK-TM .*/

# Define key_comma 0x34 /*

  • Previous: ASCII table
  • Next article: alternative use of the net user command in the XP System
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.