Sunday, 30 April 2017

Teach Yourself Game Programming in 21 Days - Day 3

"Painting the Screen with the VGA Card", the first chapter with whole programs.

First, a bit of hardware history. The PC, being general-purpose, did not originally have a dedicated video card. The need for this became evident quickly, but there were many competing standards available.  VGA started to be used in 1987 (almost as old as I am), and the interface was so useful and easy to implement that almost every video card manufacturer conformed to it. Even cards today support it, I believe. Super VGA was a later improvement which added larger resolutions, but was otherwise quite similar architecturally.

VGA provides for a number of display 'modes'. The most important modes are:
  • Mode 12h: 640x480 with 16 colours, 1 page
  • Mode 13h: 320x200 with 256 colours, 1 page
  • "Mode X": 320x240 with 256 colours, 3 pages
There are a huge number of auxiliary modes with sliding scales of resolution, colour bit depth and refresh rate. If you don't mind being limited to the EGA palette, for example, you can run at 800x600 if your monitor supports it.

You might be asking why anybody would ever use Mode 13h when Mode X is available; the difference is in how the memory is laid out. Mode X (which is really just one of a family of unchained versions of other modes) uses a planar memory layout. This means that the bits for each pixel on screen are split between different parts of memory, requiring a command to be sent to the VGA controller for each plane. By contrast, Mode 13h simply has one byte per pixel, so it's easy to write code for.

The first code listing in the book is presented in its entirety below:
This gives some insight into both how André lays out his code and how lengthy this stuff gets. All this code does is change the video mode then back again. The next example shows how to actually draw stuff. I'll be uploading the code I write to a public repository so that I'm the only one who has to break their fingers. If you look in the repository, you'll notice I've worked ahead slightly. This is mostly because I'm not sure how much time I'll have each day to do this work.

About the code; as mentioned before, the author is using Microsoft C. I'm using Turbo C, which has different syntax and names. I'll make note when I find these out. I'll also be formatting my code differently because I'm not insane. Still, if you want to follow along then grab my code, run it through your favourite 16-bit compiler and run it in DOSBox for a blast from the past.

There are three more listings in this chapter:
  • colorrot: Showing how to do 'colour animation' by changing the palette contents instead of drawing new pixels. This is used in pretty much every DOS game I can think of, so it's a useful technique to know
  • strfield: A parallax, three-plane starfield. I remember recreating this effect about a zillion times while messing with The Games Factory. Also shows off text writing with the built-in font.
  • dots: Drawing random dots on the screen! Not very exciting, but a demonstration of what you can do with essentially one line of active code.
There are quite a few hardware details in this chapter, and a brief treatment of how to create a code library which all the later examples will link against. Of course, the details depend on your linker, but I'll be providing the scripts I've been using in each case. He also mentions the Mode X, but mainly to clarify that all the examples in this book will be using Mode 13h because of its simplicity. Mode X is "probably the best", though.

Let me know if there are any more details you want me to add.

No comments:

Post a Comment