Graphic Routines within 'Graphics.asm'

by Michael Schwarzlose

What's in this document

Short Description

This game is an overhead, player centered, tile based game.  Most of the drawing in the game involves doing lookups from a map and blitting the images to the screen. This also means that transparent blitting takes place to preserve the background  when animated sprites like players and bullets are drawn.  This is done by ignoring certain color indicies when drawing the image to the double buffer.  The team emblems and jerseys are drawn from the same image but while drawing them to the double buffer, if a mask value is encountered, it looks up the team the player belongs to and replaces those values by the team color while drawing to the double buffer.  One major obstacle is going to be clipping of tiles and players when they are only partially on the screen.  Basically, I will compute an offset to specify a rectangle within the sprite buffer which should be the only portion of the sprite which is drawn to the screen.  The rest should not be drawn. A double buffer is used to reduce the amount of flicker between screen refreshes.

Screen Shot

Click on the picture for a larger version.

 

The methods associated with the graphics rendering have been encapsulated into 6 main procedures.  These six procedures represent the main entry point into the whole graphics engine.

 

InitGraphics - this procedure loads the sprites from the bitmaps into the correct buffers, sets the palette for the 256 color registers on the VGA card, sets the video mode to mode 13h.

 

Draw_double_buf - takes the state variables of the game and renders the appearance of the screen to the double buffer which should then be blitted to video memory afterwords

 

Blit_double_buf - does a straight blit from the double buffer to the video memory

 

FinalizeGraphics - performs general cleanup of the graphics engine and returns to text mode

 

range_rotate_pal - rotates the rgb values within a range over the palette registers on the vga card

 

Intro - Draws the intro screens in sequence

1. the title screen
2. the instruction screen
3. choose team screen

 

Dependant Structure Formats From Other Source Files

Listed below are the members of the different structures used in graphics.asm which are defined in other source files.  The procedures in Graphics.asm require that these members exist!.

 

PointType struct
  x dw    ?	  ; the x position of the coordinate
  y dw    ?	  ; the y position of the coordinate
PointType ends
PlayerType struct
  Team          db        ?     ; team number the player belongs to
  Firing        db        ?     ; a bool as to weather the person is firing
  Walk          db        ?     ; a status as to current walk state
  GamePos       dw2 dup(?)    ; the position in game coordinates that player exists
  Dir           db        ?     ; direction player is facing
  Bullets       dbMAX_BULLETS*6 dup (0)    ; the bullets fired by the player
  Dead          db        ?     ; bool as to whether player is dead
PlayerType ends
																	
BulletType struct
  pos	  dw 2 dup(0) ; the bullet's current location on the screen
  dir     db 0        ; the direction of the bullet, 1-32
  status  db ?        ; the status of the bullet
BulletType ends

 

Dependant Constants From Other Source Files

Listed below are constants used from other source files.  The procedures in Graphics.asm require that these constants exist and are set properly.

 

NUM_PLAYERS
  • the max number of players in the game
MAX_BULLETS
  • the max number of bullets a player can fire
TRUE  = 1
FALSE = 0
  • for boolean logic (make code readable)
MAP_WIDTH
  • the number of tiles in the x-direction in the game map
MAP_HEIGHT
  • the number of tiles in the y-direction in the game map
NUM_TEAMS
  • the number of teams allowed in the game 
NUM_WALK_STATES
  • the number of walk states a player takes on as he moves
WALK_STATE_STILL
  • state when standing still

WALK_STATE_LEFT

  • state when taking step with left foot

WALK_STATE_RIGHT

  • state when taking step with right foot

WALK_STATE_DEATH_START

  • starting index of the death state

WALK_STATE_DEATH_END

  • ending index of the death state
 

 

Dependant Variables From Other Source Files

Listed below are variables used from other source files.  The procedures in Graphics.asm require that these variables exist.

 

Players PlayerType NUM_PLAYERS dup (<>)
  • holds the list of players in the game with their states
  • used when drawing players and bullets to the double buffer
tile_map db MAP_WIDTH*MAP_HEIGHT dup (0)
  • holds the map tile indexes
MyNum db ?
  • client player number in the game
 

 

Defined Structures

 

Used for storing rectange coordinates
RectType struct
  left     dw?; the x pos of left side
  right    dw  ?; the x pos of the right side
  bottom   dw  ?; the y pos of the bottom side
  top      dw  ?; the y pos of the top side
RectType ends

                
Used by drawTile proc to specify parameters and return values
TileDrawStruct struct
  rectDraw   dw4 dup (0); the rectangle within the tile to draw
  tileIndex  dw2 dup(0); the index of the tile from the map
  drawn	     dw 2 dup (0); the number of pixels drawn in x, y dir
TileDrawStruct ends

                
Used by drawPlayer proc to specify params and returns
PlayerDrawStruct struct
  rectDraw        dw4 dup (0); the rectangle within the player image to draw to double buffer
  playerIndex     db ?        ; the index of the player to draw
  drawn		  dw 2 dup (0); the number of pixels drawn in the x, y direction
PlayerDrawStruct ends
 
The bmp structure used for loading images
BMPTYPE STRUC
; --- BitMapFileHeader --- ; 14 bytes
  BFType            DB 'BM' ; File Type
  BFSize            DD   ?  ; File Size (in Bytes)
  BFReserved1       DW   0  ; Reserved 
  BFReserved2       DW   0  ;    "
  BFOffBits         DD   ?  ; Offset to start of image data 
; --- BitMapInfoHeader --- ; 40 bytes
  BISize            DD   ?  ; Size of BitMapInfoHeader (28h = 40d)
  BIWidth           DD   ?  ; # Pixel Rows 
  BIHeight          DD   ?  ; # Pixel Columns
  BIPlanes          DW   1  ;
  BIBitCount        DW   ?  ; Log2(palette size) = 4 for 16-color image
  BICompression     DD   0  ; RGB = 0 = Uncompressed 
  BISizeImage       DD   ?  ; Size of Image (Bytes)
  BIXPelsPerMeter   DD   ?  ;
  BIYPelsPerMeter   DD   ?  ;
  BIColorsUsed      DD   0  ; 0=All
  BIColorsImportant DD   0  ;
; --- Color Table --- ; 64 Bytes
  RGBQuad           DB 16 dup ( 4 dup(?) ) ; Blue, Green, Red, Unused
; --- Image Data Follows ; n Bytes
  ImageData         DB 63882 dup(0) ; Image data (bottom row first)
BMPTYPE ENDS

                            
The RGB values as they are stored in memory
RGB Struct
  R db?  ; the red portion
  G db?  ; the green portion
  B db?  ; the blue portion
RGB ends
              
Used to pass parameters to the drawImage proc
ImageDrawStruct Struct
  IHeight     dw 0 ; the height of the image
  IWidth      dw 0 ; the width of the image
  BufWidth    dw 0 ; the width of the buffer image
ImageDrawStruct ends
 
              

 

Declared Varaiables

 

tDraw TileDrawStruct <>
  • used and updated by drawTile
  • holds parameters and return values from drawTile
vis_rect RectType <>
  • updated by findVisRect
  • contains the world map coordinates which are visible on screen
pDraw PlayerDrawStruct <>
  • used by drawPlayer to get params
double_buffer db 64,000 dup (0)
  • the double buffer for rendering screen before sent to video memory
TileBuffer db TILE_HEIGHT*TILE_WIDTH *MAX_TILES dup(0)
  • stores all the tiles loaded from file 
PlayerSpriteBuffer db NUM_TEAMS * PLAYER_HEIGHT * PLAYER_WIDTH * NUM_WALK_STATES dup(0)
  • stores the player sprites loaded from file
palette RGB 256 dup (<>)
  • the palette stored in memory for construction before sending to vga card
bmp BMPTYPE <>
  • memory location where loaded bmp files from memory are stored
PlayerFile db 'player.bmp',0
  • NULL  terminated filename for player images - walking
TileFile db 'tiles.bmp',0
  • NULL terminated filename for tiles of the game
MiscFile db 'misc.bmp',0
  • NULL terminated filename containing misc images for numbers, bullets, and other items
TeamColors db NUM_TEAMS dup(0)
  • array of color indices for translating team value into a color
IntroFilename db 'intro.bmp',0
  • NULL terminated filename containing the intro screen
InstFilename db 'inst.bmp',0
  • NULL terminated filename containing the instruction screen
ChooseFilename db 'choose.bmp',0
  • NULL terminated filename containing the choose team screen
PlayerRGB db 0,0,100, 0,100,0, 255,0,0
  • the player colors
TeamPalette db 253, 254, 255
  • the palette register indices for the different team colors
iDraw ImageDrawStruct <>
  • used to pass parameters to drawImage
tile_file db 1724 dup(?)
  • temp storage when loading the map from file
tile_images db 25600 dup (?)
  • the image buffer for the tiles 160x160
misc_images db 10000 dup (0)
  • the misc images, bullets, numbers, team emblems 59x25
MapFilename db 'map.dat', 0
  • the name of the file with the map data

 

Declared Constants

 

TILE_WIDTH  EQU 32
TILE_HEIGHT EQU 32
- Specifies the tile width and height in pixels
PLAYER_HEIGHT EQU 25
PLAYER_WIDTH  EQU 25
- Specifies the size of the player sprite in pixels
BULLET_WIDTH  EQU 6
BULLET_HEIGHT EQU 6

- Specifies the size of bullet sprites on in pixels

PALETTE_MASK		EQU 03C6h
PALETTE_REGISTER_RD     EQU 03C7h
PALETTE_REGISTER_WR	EQU 03C8h
PALETTE_DATA		EQU03C9h

- Palette port numbers to VGA card 

TRANS_MASK  EQU 0

- The value of the color register which is the transparent mask register when doing bitblits 

TEAM_MASK  EQU 1
-  The color register value which gets translated into team colors when in drawPlayer
MAX_TILES  EQU 25

- The maximum number of different tiles

 

Procedures

 

  • All implemented by Michael Schwarzlose

InitGraphics
  • Reg In:
    • none
  • Reg Out:
    • none
  • Variables read:
    • none
  • Variables updated:
    • all image buffers, and the palette variable
  • calls:
    • LoadFile, storeBMP, storePlayerImages, addPalette, setPalette
  • description:
    • called to initialize the buffers, graphics mode, the palette registers, load sprites from files into memory,...etc.
    • actually just calls other functions to perform the initialization
    • sets video mode to mode 13h
Draw_double_buf
  • Reg In:
    • none
  • Reg Out:
    • none
  • Variables read:
    • none
  • Variables updated:
    • double_buffer
  • calls:
    • findVisRect, drawTiles, drawPlayers, drawStats
  • description:
    • draws the double buffer according to state variables
    • draws in the following order:
      1. find the visible rect
      2. draw the map to the screen
      3. draw the players over the map
      4. the stats get drawn last
Blit_double_buf
  • Reg In:
    • none
  • Reg Out:
    • none
  • Variables read:
    • double_buffer
  • Variables updated:
    • video memory
  • calls:
    • none
  • description:
    • transfers byte from double_buffer to video memory
FinalizeGraphics
  • Reg In:
    • none
  • Reg Out:
    • none
  • Variables read:
    • none
  • Variables updated:
    • none
  • calls:
    • none
  • description:
    • cleans up after the graphics engine and sets vid mode to text mode
drawTiles
  • Reg In:
    • none
  • Reg Out:
    • none
  • Variables read:
    • vis_rect
  • Variables updated:
    • double_buffer, tDraw
  • calls:
    • drawTile, findTileIndex, findTileRect
  • description:
    • iterates through the visibile tiles in the map and calculates the clipping of them.
    • calls drawTile to draw them after this calculation
drawTile
  • Reg In:
    • BX = x coords in screen coords as upper left
    • AX = y coords in screen coords of upper left
    • CX = y coords in screen coords of upper left
    • DX = x coords in screen coords of upper left
  • Reg Out:
    • none
  • Variables read:
    • tDraw, TileBuffer
  • Variables updated:
    • double_buffer, tDraw
  • calls:
    • none
  • description:
    • draws the tile specified in tDraw struct to the screen buffers
drawPlayers
  • Reg In:
    • none
  • Reg Out:
    • none
  • Variables read:
    • vis_rect, players
  • Variables updated:
    • double_buffer, pDraw
  • calls:
    • drawBullets, drawPlayer
  • description:
    • steps through all the player's state and draws them if they are in the game and on the visible screen
    • also then steps through all of player's fired bullets and draws them via a drawBuffer call
    • then calls drawPlayer with settings in pDraw variable to have him drawn to double_buffer
drawPlayer
  • Reg In:
    • none
  • Reg Out:
    • none
  • Variables read:
    • pDraw, PlayerSpriteBuffer, Players
  • Variables updated:
    • double_buffer
  • calls:
    • none
  • description:
    • draws the player specified in pDraw struct
    • uses the team member in the player structure to find which color to replace masks in shirt with
setPalette
  • Reg In:
    • none
  • Reg Out:
    • none
  • Variables read:
    • palette
  • Variables updated:
    • none
  • calls:
    • none
  • description:
    • sets the vga palette to the one specified in the 'palette' variable
LoadFile
  • Reg In:
    • DS = segment of ASCIIZ string of filename
    • DX = offset of ASCIIZ string of filename
    • AX = segment of the destination buffer
    • BX = offset of destination buffer
    • CX = number of bytes to read
  • Reg Out:
    • none
  • Variables read:
    • none
  • Variables updated:
    • bmp
  • calls:
    • none
  • description:
    • loads a file into a buffer
addPalette
  • Reg In:
    • none
  • Reg Out:
    • none
  • Variables read:
    • bmp
  • Variables updated:
    • palette
  • calls:
    • none
  • description:
    • takes the data from bmps as RGBs and sets the values into the palette variable
storeBMP
  • Reg In:
    • SI = starting offset into the image data of the bmp var
    • ES = segment of the destination buffer
    • DI = offset of the destination buffer
    • AX = number of lines to read
    • BX = length of each line
  • Reg Out:
    • none
  • Variables read:
    • bmp
  • Variables updated:
    • buffer pointed to by ES:[DI]
  • calls:
    • none
  • description:
    • stores a rectangular region from bmp starting at SI into a buffer
storePlayerImages
  • Reg In:
    • none
  • Reg Out:
    • none
  • Variables read:
    • bmp
  • Variables updated:
    • PlayerSpriteBuffer
  • calls:
    • storeBMP
  • description:
    • loads player sprites into memory from the bmp variable to PlayerSpriteBuffer
findVisRect
  • Reg In:
    • none
  • Reg Out:
    • none
  • Variables read:
    • players[MY_NUM].pos
  • Variables updated:
    • vis_rect
  • calls:
    • none
  • description:
    • Takes the coordinates of the local client player and finds the rect in world coordinates that is visible on the screen
storeTileImages
  • Reg In:
    • none
  • Reg Out:
    • none
  • Variables read:
    • bmp
  • Variables updated:
    • TileBuffer
  • calls:
    • storeBMP
  • description:
    • call after bmp has been filled with the tilemap bmp
    • loads the tiles off the bmp and stores them into the correct tile buffer locations
drawNumber
  • Reg In:
    • AL = index of number from NumSprites
    • BX = x position on screen
    • DX = y position on screen
  • Reg Out:
    • none
  • Variables read:
    • NumSprites
  • Variables updated:
    • double_buffer
  • calls:
    • none
  • description:
    • draws the number located in NumSprite from Index AL, 0 based

 

drawBuffer
  • Reg In:
    • BX = x position on screen
    • DX = y position on screen
    • DS = source buffer segment
    • SI = source buffer offset
  • Reg Out:
    • none
  • Variables read:
    • none
  • Variables updated:
    • double_buffer
  • calls:
    • none
  • description:
    • blits the contents from the buffer into the double_buffer variable and takes care of transparent blitting
    • only called to blit bullets at this point, but will be used for any thing new
Intro
  • Reg In:
    • none
  • Reg Out:
    • none
  • Variables read:
    • none
  • Variables updated:
    • none
  • calls:
    • none
  • description:
    • Draws the intro screens in sequence

      1. the title screen
      2. the instruction screen
      3. choose team screen
drawStats
  • Reg In:
    • none
  • Reg Out:
    • none
  • Variables read:
    • none
  • Variables updated:
    • none
  • calls:
    • drawEmblem, drawNumber
  • description:
    • Draws the team scores in the upper right corner of the screen
drawNumber
  • Reg In:
    • AX = y screen coordinates to put upper left corner
    • BX = x screen coordinates to put upper left corder
  • Reg Out:
    • none
  • Variables read:
    • misc_images
  • Variables updated:
    • double_buffer
  • calls:
    • none
  • description:
    • Draws a number to the screen
drawEmblem
  • Reg In:
    • AX = y screen coordinates to put upper left corner
    • BX = x screen coordinates to put upper left corder
    • CL = the team color to draw the emblem with
  • Reg Out:
    • none
  • Variables read:
    • misc_images
  • Variables updated:
    • double_buffer
  • calls:
    • none
  • description:
    • Draws a team emblem with the color associated with the team
drawBullet
  • Reg In:
    • AX = y screen coordinates to put upper left corner
    • BX = x screen coordinates to put upper left corder
    • CL = the team color to draw the emblem with
  • Reg Out:
    • none
  • Variables read:
    • misc_images
  • Variables updated:
    • double_buffer
  • calls:
    • none
  • description:
    • Draws a team emblem with the color associated with the team
drawBullets
  • Reg In:
    • BX = the starting position in the bullet array for a player
  • Reg Out:
    • none
  • Variables read:
    • visRect
  • Variables updated:
    • double_buffer
  • calls:
    • drawBullet
  • description:
    • Steps through each bullet struct in the bullet array for a player and draws them if they are active and in the visible rectangle in game coordinates
drawImage
  • Reg In:
    • AX = y screen coordinates to put upper left corner
    • BX = x screen coordinates to put upper left corner
    • CX = segment that the image buffer belongs
    • SI = offset of the image buffer to start from
  • Reg Out:
    • none
  • Variables read:
    • iDraw
  • Variables updated:
    • double_buffer
  • calls:
    • none
  • description:
    • Reads the iDraw structure and draws an image with rectangle specified in the iDraw variable
      and draws it to the screen buffer.
FindTileIndex
  • Reg In:
    • AX = x game coords
    • BX = y game coords
    • DX = x screen coords
    • CX = y screen coords
  • Reg Out:
    • none
  • Variables read:
    • none
  • Variables updated:
    • tDraw
  • calls:
    • none
  • description:
    • given the current x,y in screen coordinates and the game coordinates and calculates the bounding rectangle for the tile to be drawn to the screen
FindTileIndex
  • Reg In:
    • AX = y game coordinates to look up tile
    • BX = x game coordinates to look up tile
    • CX = segment that the image buffer belongs
    • SI = offset of the image buffer to start from
  • Reg Out:
    • none
  • Variables read:
    • iDraw
  • Variables updated:
    • double_buffer
  • calls:
    • none
  • description:
    • Reads the iDraw structure and draws an image with rectangle specified in the iDraw variable
      and draws it to the screen buffer.