Tuesday 29 August 2017

Story Time - My First Serious Program

Having a basic knowledge of Commodore Basic v2.0 I took to my A-Levels in 1994 and started to learn Pascal, both "Turbo Pascal" on DOS and "Personal Pascal" on my Atari ST.

One of the first things I did, as most kids do is start to write a game, it was a 2D grid style war-game, 28x28 tiles to a map and maps stitched together, it was very roughly based on a table top game I had which itself was a dice & splice of the Guadalcanal Campaign of World War 2... Anyway, I had jungle, and grass, Desert and beaches, water and the tanks, and troop counters moved around, you could air-drop paratroopers into spots when you had earned enough points, and I last worked on it in around 1995 when I started to add Sound Blaster driven 16bit effects into it.

But, I also needed tools, I was working on DOS, with no Windows - I didn't get Windows 3.1 until later that year - and at college the compiler only ran in DOS, to stop the compiler and loads Windows, then execute a GUI tool just to exit again and load the IDE was not a workable solution, therefore I started to write a bunch of EGA based tools directly in DOS, drawing graphics in DOS.

The first tool was a simple string comparer, almost a version of "Diff" which I used for comparing old and new files.  The second tool was a back-up tool, to indicate the copying of files into a floppy, so I could squirrel them away.

Finally, I needed a serious tool, this was a graphics tool... Almost a version of Paint for DOS.  To help design maps and other information in my game.  It worked by letting me place squares and circles, lines and even text into set places on the screen... It looked just like this...


Unlike anyone else in the class of A-Level students (as far as I know) I was the only one who knew about Interupts and could therefore use the mouse in DOS, so this blew a few people away.

This could save my image too, and I built up a very basic form of "Vector" graphics, without actually knowing what vector graphics were.

Later on, I came to add scale and delete to these items too, and this is where my innocence as a new programmer came along, I first started doing delete by using "GetPixel" to return the colour of the pixel below the mouse cursor, however, this failed terribly the moment I run it up and realised if I clicked on either the red circle or the red square both would vanish and I only had 16 colours in my pallette and really got disheartened.


So, I came up with a scheme to get more colours and workout which item I had clicked on... I went to grey scale, I converted each shape in the list of shapes to a grey scale of RGB (0x01, 0x01, 0x01) to RGB (0xFE, 0xFE, 0xFE)... This suddenly gave me 253 possible shapes in my list (as I used black and white as control colours), but 16 versus 253 I thought I was making HUGE strides.


I could then look up the pixel at the X, Y of the mouse to delete the actual shape below the mouse when delete mode was set... Nifty... So my mind thought, it was certainly a programming exercise, and very laborious on the 25Mhz 486 CPU's the college had.  Only much later did I think about accessing the back-buffer directly, locking the bits and stepping over the image in scan lines at vastly higher speed, anyway...

My next challenge for this program was to stop shapes overlapping, I immediately went to my grey scale mode, and started to so this... as the song goes... with "White Lines".... 


As the line of white moved across line by line, pixel by pixel, really really slowly, it checked if it found a grey - rather than black - making the pixel flip from RGB (0x00, 0x00, 0x00) to (0xFF, 0xFF, 0xFF), if the pixel ALSO was the RGB of the Grey shape I was adding, then this was left black, like so...


And I knew the shape was "free floating", with no overlaps... However, if there was an overlay, I added the RGB differently... So I could see the overlay... like so...


I was not only doing the processing live on screen, but it acted as a debugger for me as I went, without needing to spool up the Turbo Pascal Debugger on my measly 543K of free RAM.

This was a serious amount of work for me, when I learned a lot more about programming I came back to this briefly, but it never warmed my cockles more than when I wrote this program for the first time, I learned so much, I learned patience, to think a head, to plan, to process flow, to research the topic and ultimately to get things done first - in a prototype - and then revisit them.

I also ultimately used this same code later, at university in C++... Making this the first time I had prototyped a working application in one language (Pascal) and converted it to another (C++) and could say it was properly prototyped.  The latter use as as s tool to layout gardens in a sort of designer, I remember far less about the C++ version than the Pascal version.  But it's shadow lasts a long time, and remains over me today, I recall so much more about this one application than most of the others I wrote at the time, because it was something I engaged with.

I can only express to any budding programmer today, no matter that language you use, to just dive in there... 

No comments:

Post a Comment