MODE SETTING AND PIXEL PLOTTING |
When learning to program graphics, the best place to start is Mode 13h. This is a graphics mode for DOS bas applications. It has a resolution of 320 pixels wide and 200 pixels high, with a maximum of 256 colours on screen out of a possible 262,144. Granted, in the times of Win '95 games all being in 64k colours at resolutions of 640x480, this mode may seem a little dull. However, the simplicity of Mode 13h makes it the mode of choice when learning.
Setting Mode 13h
The setting of Mode 13h is easy. It is achieved by placing a value of hexadecimal 13 in the AX register, then calling interrupt 10h. What this does is tell the computer that you wish to call function 10h, which is the function for setting video mode. The computer then looks in the AX register to see which video mode you wish to set, and sets it accordingly.
Setting Text Mode
Of course, after you have finished your work in Mode13h, you will wish to restore the original Text mode. This is achieved in the same method as setting Mode13h, with the exception being the value in the AX register is now hexadecimal 3, which is text mode.
Plotting Pixels
In Mode 13h, each Pixel is 1 byte of memory, with each pixel being placed sequentially. Think of the mode13h screen as an array of pixels. The size of the mode13h screen is 320x200x1, which equals 64000 bytes. The best way to write directly to this screen is to create a pointer to the screen, then use the following formula to calculate the offset:
offset = (Y-Position * ScreenWidth) + X-Position)
Of course, before we can create a pointer to the screen, we must actually know where to create the pointer. The video memory for mode13h starts at 0XA000. Creating a far pointer to this will allow us to access the mode13h screen.
Putting It All Together
Okay, now to display something on the screen. The sample program we will consctruct here will plot randomly coloured pixels on the screen until the user presses a key. In order to achieve this we must:
Set the desired graphics mode, in this case, Mode 13h.
Create a loop that executes until a key is pressed.
Generate random values for the X position, Y position and colour of the pixel
Plot the pixel
When a key is pressed, set Text Mode.
And that's all there is to it. Congratulations, you've just taken your first steps to becoming a games programmer. Check the source code provided if you have any troubles, and for any further problems, email me.
The source code for the sample program, and it's executable is available right here: