Chapter 3: Graphics

The original code used Mode 13h of the VGA card which was 320x200 and 256 colors. In the conversion code I am using Screen 18 which is 640x480 and I am using 32bit color mode. Using 32bit colors will make the following chapter code look much beter and allow some flexibility in creating both the models and lighting.

Since I am using 32bit colors, I did not add any of the palette switching code that was in the original C code. However, I did create a fake VGA palette, just to have the look anf feel of the old code. In future installments, I will only use the 32bit mode and inline more of the graphics functions to make things run a bit faster.

Here is the include file,

'Library file from Chapter 3 of Black Art of 3D Game Programming by Andre LaMothe
'Converted from C to FreeBasic by Richard Clark
'I have included the C comments as a reference point for the C source.
'Copyright The Waite Group (c) 1995
'// I N C L U D E S ///////////////////////////////////////////////////////////

'RDC: None needed.

'// G L O B A L S //////////////////////////////////////////////////////////////
'RDC: We can skip the Mode 13h stuff. We don't need it. We are going to use Screen 18
'for all these programs, i.e. 640x480, 32bit colors. However, we will build a fake
'vga palette so as to implement the palette transformations. This is the base palette
'that we will use to restore the palette if we need to make changes to the working palette.
'RDC: This will be our working vga palette.
Dim Shared As Uinteger vga_palette(0 to 255) = {_

'RDC: We will store the font sizes here.  

Dim Shared char_height As Integer = 16 'RDC: Using the default settings for screen 18.
Dim Shared char_width As Integer = 8

'// F U N C T I O N S /////////////////////////////////////////////////////////

'RDC: In the orirignal source this did a lookup into the ROM for the font and was
'used by Print_String to print characters. We really don't need this as we can just 
'print a whole string using Draw String which is faster and easier. However, this may
'be used in the sources elsewhere so I will include it here. The var clr here (and following)
'refers to the vga_palette index which stores all of our colors.
Sub Print_Char (xc As Integer, yc As Integer, c As string, clr As Integer, transparent As Integer)
    '// this function is used to print a character on the screen. 
    'RDC: We are just going to use Draw String for this since it is easier and faster. If the
    'character is not transparent then we'll draw the background in black first.
    If transparent = 0 Then
        'Set background to black.
        Line (xc, yc) - (xc + char_width, yc + char_height), vga_palette(0), BF 'Filled box.
        'Draw the character.
        Draw String (xc, yc), c, vga_palette(clr)
        'Just draw the chatacter on the screen.
        Draw String (xc, yc), c, vga_palette(clr) 
End Sub '// end Print_Char


Sub Print_String(x As Integer, y As integer, clr As Integer, c As string, transparent As Integer)
    Dim slen As Integer

    '// this function prints an entire string on the screen with fixed spacing
    '// between each character by calling the Print_Char() function
    'RDC: We are going to just print the whole string here, rather than calling
    'Print_Char since we don't need to do a character lookup like the original source.

    'RDC: Get the length of the string. Not in original source, but we will need it
    'to draw a background and for some error checking.
    slen = Len(c)
    'RDC: Nothing to print so exit.
    If slen > 0 Then
        If transparent = 0 Then
            'Set background to black.
            Line (x, y) - (((x + CHAR_WIDTH) * slen), y + CHAR_HEIGHT), vga_palette(0), BF 'Filled box.
            'Draw the character.
            Draw String (x, y), c, vga_palette(clr)
            'Just draw the chatacter on the screen.
            Draw String (x, y), c, vga_palette(clr) 
    End If
End Sub '// end Print_String

Sub Write_Pixel(x As Integer, y As integer, clr As Integer)
        '// plots the pixel in the desired color a little quicker using binary shifting
        '// to accomplish the multiplications
        '// use the fact that 320*y = 256*y + 64*y = y<<8 + y<<6

        'RDC: Just use Pset here.
        PSet(x, y), vga_palette(clr)
End sub '// end Write_Pixel


Sub Set_Graphics_Mode
    '// use the video interrupt 10h and the C interrupt function to set
    '// the video mode
    'RDC: We are just going to use screen 18, 32bit color. 
    Screen 18, 32 'We are only going to use 1 page here, since we can use Screenlock to write to screen.
End Sub '// end Set_Graphics_Mode


Sub Time_Delay(clicks As Integer)
'// this function uses the internal timer to wait a specified number of "clicks"
'// the actual amount of real time is the number of clicks * (time per click)
'// usually the time per click is set to 1/18th of a second or 55ms

Dim dnow As Double

'// get current time

dnow = Timer

'// Wait for number of click to pass

    Sleep 1 'Need to do this so system can run other tasks
Loop Until Timer > (dnow + clicks)

End Sub


Sub Line_H(x1 As integer, x2 As integer, y As integer, clr As Integer)
    '// draw a horizontal line using the memset function
    '// this function does not do clipping hence x1,x2 and y must all be within
    '// the bounds of the screen
    'RDC: We can just use the FB's line function for this since it will be faster
    'than anything we can code here.

    '// sort x1 and x2, so that x2 > x1

    if x1 > x2 Then
        Swap x1, x2
   End if '// end swap
    '// draw the row of pixels
    Line (x1, y)-(x2, y), vga_palette(clr)
End sub '// end Line_H


Sub Line_V(y1 As integer, y2 As integer, x As integer, clr As Integer)
    '// draw a vertical line, note that a memset function can no longer be
    '// used since the pixel addresses are no longer contiguous in memory
    '// note that the end points of the line must be on the screen
    'RDC: Again we will just use FB's line method.

    if y1 > y2 Then
        Swap y1, y2
    EndIf '// end swap
    Line (x, y1)-(x, y2), vga_palette(clr)
End sub '// end Line_V

'RDC: For clr here we need an RGB color to put into the palette array, hence it is a UInteger.
Sub Write_Color_Reg(index As integer, clr As UInteger)
    '// this function is used to write a color register with the RGB value
    '// within "color"
    '// tell vga card which color register to update
    'RDC: We are just going to update our palette array here.

    If (index >= 0) And (index <= UBound(vga_palette)) Then
        vga_palette(index) = clr

End sub '// end Write_Color_Reg


Function Read_Color_Reg(index As integer) As UInteger
    '// this function reads the RGB triple out of a palette register and places it
    '// into "color"
    'RDC: We are just going to get out value from the vag_palette array.

    If (index >= 0) And (index <= UBound(vga_palette)) Then
        Return vga_palette(index)
        Return 0

End function '// end Read_Color_Reg


Sub Read_Palette(start_reg As integer, end_reg As integer, the_palette() As UInteger)
    '// this function will read the palette registers in the range start_reg to
    '// end_reg and save them into the appropriate positions in the_palette

    Dim index As integer '// used for looping

    '// read all the registers
    for index = start_reg to end_reg
       '// read the color register
        the_palette(index) = Read_Color_Reg(index)
   Next '// end for index
End sub '// end Read_Palette


Sub Write_Palette(start_reg As integer, end_reg As integer, the_palette() As UInteger)
    '// this function will write to the color registers using the data in the
    '// sen palette structure
    'RDC: We are just updating the vga_palette array here.

    Dim index As integer '// used for looping

    '// write all the registers

    for index = start_reg to end_reg
       '// write the color registers using the data from the sent palette

        Write_Color_Reg(index, the_palette(index))

    Next '// end for index

End sub '// end Write_Palette


Sub Draw_Rectangle(x1 As integer, y1 As integer, x2 As integer, y2 As integer, clr As integer)
    '// this function will draw a rectangle from (x1,y1) - (x2,y2)
    '// it is assumed that each endpoint is within the screen boundaries
    '// and (x1,y1) is the upper left hand corner and (x2,y2) is the lower
    '// right hand corner
    'RDC: We are just going to use the box function of line here.
    Line (x1, y1)-(x2, y2), vga_palette(clr), B
End sub '// end Draw_Rectangle


Sub Fill_Screen(clr As Integer)
    '// this function will fill the entire screen with the sent color
    '// use the inline assembler for speed
    'RDC: We are just going to use CLS for this.
    Color , vga_palette(clr)

End sub '// end Fill_Screen

'RDC: I am not including the ModeZ functions since they are the same as the Mode 13h functions.

Here is the main code, Mode18.bas:

'// MODE13.C - A demo of all the mode 13h functions for this chapter
'RDC: Mode18: sing screen 18 here, 640x480 rather than 320x200. Hence,
'I am calling this Mode18 rather than Mode13.

'// I N C L U D E S ///////////////////////////////////////////////////////////
#include "" '/ the header file for this module

Dim index As Integer
Dim save_palette(0 To 255) As UInteger

Randomize Timer

'/ set the graphics mode to mode 13h
'RDC: Will be screen 18

'// show some text

Print_String 0, 0, 15, "Hit a key to see text printing...", 0 

'// print a few hundred strings on the screen

For index = 0  To 1000
    Print_String Rnd * (639 - Len("This is a demo of text printing")), Rnd * 479, Rnd * 254, "This is a demo of text printing", 1
Print_String 0, 0, 15, "Hit a key to see screen filling...", 0

'// fill the screen dark grey

Fill_Screen 8
Print_String 0, 0, 15, "Hit a key to see pixel plotting...", 0

'// plot 10000 random pixels

for index = 0 To 10000
    Write_Pixel Rnd * 639, Rnd * 479, 12
Print_String 0, 0, 15, "Hit a key to see lines...", 0

'// draw 1000 randomly positioned horizontal and vertical lines

for index = 0 To 1000
    Line_H Rnd * 639, Rnd * 639, Rnd * 479, Rnd * 254
    Line_V Rnd * 479, Rnd * 479, Rnd * 639, Rnd * 254
Next '// end for index

Print_String 0, 0, 15, "Hit a key to end program...", 0

'Since we are doing this in 32bit color mode, we can't do palette shifts
'so they won't be in ths program. However, in FB 256 color mode, you can shift 
'palettes much like you could in the old VGA cards.