C Game Programming

Contents

Setting the video mode 13h.
Plotting a pixel in mode 13h.
Mouse Routines.
Sound Rountines.
Keyboard Routines.
Joystuck Routines.

Setting the video mode to 13h

What is mode 13h? Mode 13h is graphics mode in which most C games are programmed. It is the easiest mode to use because the array of memory that represents the screen is exactly 64,000 k (320 pixels x 200 pixels = 64,000 k). This mode is capable of 256 colors. To set the VGA monitor to mode 13h:

#include "dos.h"
#include "bios.h"


void set_video_mode(int mode) 

{ 

   union REGS inregs,outregs; 

   inregs.h.ah=0 

   outregs.ah=mode; 

   _int86(0x10,inregs,outregs); 

}
or using the inline assembler:

void set_video_mode(int mode) 

{ 

   _asm {

   mov ah,0

   mov al,mode

   int 10h

   } 

}

Plotting a pixel in mode to 13h

Plotting a pixel in mode 13h is easy because each pixel takes up one byte and it easy to do calulations to find where in memory that pixel is. Then, we put in a value between 0 and 255(each number represents a color: 0 is black and 255 is white). But first we have to know where the memory for the VGA card starts, and then create a pointer to it:
unsigned char video_buffer[]=0xA0000000L

Then we have to calculate its position:
offset=y*320+x;

Then change its color:
video_buffer[offset]=color;

Let's look at the entire function:
unsigned char video_buffer[]=0xA0000000L


void plot(int x, int y, char color) 

{ 

  offset=y*320+x;

  video_buffer[offset]=color;

}

Mouse Routines

The mouse is not often used in fast action games but can be useful in slower RPG games. Here we will create functions that will allow you to detect the mouse position and buttons. Let's take at a look at the first.

First we have to declare some variables that will hold the X and Y values:




int MOUSE_X;

int MOUSE_Y;



int MOUSE_BUTTONS;



union _REGS inregs,outregs;    // To hold _int86() calls

Now, we have to show the mouse:



void Show_Mouse()

{

  inregs.x.ax=0x01

  _int86(0x33,&inregs,&outregs);

}

Now, we can detect the X and Y and get the button position:



void Mouse_X_Y(int *x,int *y,int *buttons)

{

   inregs.x.ax=0x03;

   _int86(0x33,&inregs,&outregs);

   

   *x= outregs.x.cx;

   *y= outregs.x.dx;

   *buttons=outregs.x.bx;

}
Finally, let's see a demo program.

#include <stdio.h>

#include <stdlib.h>

#include <dos.h>

#include <bios.h>



int MOUSE_X;

int MOUSE_Y;



int MOUSE_BUTTONS;



union REGS inregs,outregs;



void main()

{

   int x=0

  

   Mouse_Show();

   

   do {

      Mouse_X_Y(MOUSE_X,MOUSE_Y,MOUSE_BUTTONS);

      printf("%i %i %i",MOUSE_X,MOUSE_Y,MOUSE_BUTTONS);

   } (x=1);

Wow, wasn't that easy.

Keyboard Routines


Joystick Routines


Copyright (c) David Gerasimow 1997.
1