Yesterday I unveiled chapter 22 cover the GET/PUT commands, and I hope I did them justice. Part of the chapter, was showing off my version of the chapter’s DIY challenge, create a spaceship, and have it blast off and fly through space.
I did what I thought was a fairly fancy version of that demo, I created a backdrop of planets, and used page flipping to simulate “spite” style animation where the spaceship did not “erase” the background. This involved having a backup copy of the entire graphics page, “getting” the area behind the spaceship, “putting” the spaceship on the screen, using PCOPY to “copy” the composited screen with background and superimposed spaceship to the visible page, restoring the original background in place, and repeating as it moved.
The results were good, but I felt it was a little “flat”, as my spaceship was essentially a line-art drawing with lots of “black” or “blank” space inside it’s frame, it could get lost “behind” some of the planets, the illustration below was my original demo, and when it first left the ground, it was hidden behind the white planet.
So I decided I wanted to One-Up that demo, and have the spaceship always be in “front” of everything, and still keep it’s lineart wireframe look, although have the inside of it remain black, which is technically color 0, or an “off bit” so it’s hard to mask “nothing” onto a screen, but here’s the final version with this being accomplished;
Before I get into all the “how” of accomplishing this fancy feat, let’s step through the revised program and really did into the anatomy of this entire demo.
At the start of the program in Line 10, I PLEAR 8, which reserves RAM to use all 8 “pages” of graphics memory, giving me access to both the high res screens. I set the PMODE to 4, start on page 1, set the color set to “high” using the Black and White (vs black and green) mode, clear that screen, then “DIM” or “declare” the variables I’ll used to store graphics data, SP is the Spaceship, BG is the background, and RM is used to create the Reverse Mask. The extra steps I’ll be taking to get backgrounds, and do several levels of “pasting” images to composite the final frame took a heaver toll on the CPU, so in line 12, I introduced the CoCo BASIC programmer’s best friend, POKE 65495,0, this sets ROM into a “high speed” mode and BASIC actually runs faster!
Line 20 uses the DRAW command to draw out the spaceship, it it’s original lineart/wireframe mode, and I GET the primary ship’s graphical detail no line 30.
Line 31 and 39 are where I start to use some VooDoo magic, first I “paint” in the spaceship, so it’s solid white now, except for it’s window. I then “get” that painted image using the RM variable to have a “solid” ship. I then PUT that solid image back in the same place, but using the PSET option, which does a color reversal, everything white becomes black, and black becomes white. I now have a “black” solid spaceship, on a white solid background, a negative “cutout” of the spaceship, this is captured again using the GET command, in the same screen coordinates, and stored again in the RM variable for the now proper and truly “reversed” mask of the spaceship. I now have two copies to be used to apply special compositing effects to the screen, speaking of the screen, the image below from yesterday’s demo shows how the “AND” option created a reverse or “Ghost” circle against the yellow bar that was “blanked out” in just the shape of the object, the circle closest to the bottom was created as a “reverse mask”, it was a circle in green the low or “off” color against the red, which was the highest color.
How did I make the planets in the background, and I did I mention I added stars?
Lines 40-70 create the outer space backdrop for the demo. Line 40 sets the graphics mode, and clears the screen. Line 45 is a bit of a “hack” because PMODE 4, is actually the high res monochrome mode of the CoCo with no “true” colors available or supported in that mode. However, NTSC televisions allowed for “artifacts” which were “false” colors that appeared on screen. We can force those artifact colors by setting the mode to PMODE 4, first, activating the “Screen”, and then switching to PMODE 3, which is medium res 4-color mode, and start producing stars and planets in color, which is triggered on line 45
Line 46 is a new routine I created today to generate 99 random stars on the screen before drawing the planets, each will be either red, white or blue (go USA!), I then go back to the original planet drawing routine I had from the first version of this demo, although the “number of planets logic” was tweaked to have a minimum of 3 planets at all time, originally it was based on a single RND(9) decision which would yield a number between 1 and 9, and I didn’t like having only a “few” planets, now I’m guaranteed at least 3, with a maximum of 11.
The planets are circles derived from random X and Y screen coordinates, a random radius of up to 50, and a random color, of either red, white, or blue again, and these are forced artifact colors by forcing the CoCo do produce graphics in PMODE 3, which supports these colors on the visibly PMODE 4 screen, which will then show them as artifact colors.
Line 55 generates the random variables, line 60 creates the circles, line 65 paints them in, and line 70 finishes the loop, and then sets the PMODE back to 4, on the top of page 1 and sets the screen mode to “1” again ensuring the high color palette vs the mute green one.
Now that the stage has been set, and the backdrop of stars and planets is complete, it’s time to start animating our spaceship and allowing it to fly through space!
We begin the process by taking a copy of the entire graphic screen of stars and planets we just finished producing. Using the PCOPY command copies the visible screen, consisting of 4 actual “pages” of memory, to the invisible/background screen, which is exactly 4 “pages” higher in memory. The PMODE 4,1 command sets the graphics screen to the top of screen 1, page 1, and pages 2-4 fill in the rest of the screen from top to bottom. When we want to work “behind the scenes”, keep the viewers eyes on PMODE 4,1 (page one) and we’ll switch over to PMODE 4,5 (page 2) and do all our dirty work 🙂
Line 80 is a nice little comment to explain that we’re about to do just that!
The bulk of this voodoo and black magic is in the image just above, line 90 sets the visible page to PMODE 4,1 screen 1,1, and that’s what the viewer sees. We then switch to PMODE 4,5, where we’ll do our work, but we don’t switch the screen again, so nobody will see what we’re doing, we also pick a random X coordinate for the spaceship between 1 and 200 as to where it will take off from.
Line 95 starts the animation loop by starting at vertical screen position 151 which is toward the bottom. Because my spaceship’s graphical data is exactly 40 pixels wide by 40 pixels tall, adding the 40 pixels to starting position 151 makes the bottom of the spaceship end at exactly pixel 191, which is the “highest” value, but appears to be on the “bottom” of the screen.
The CoCo PMODE 4 resolution is 256 x 192, so the X, or horizontal axis is from 0-255, zero being absolute left, 255 being hard right. The Y or vertical axis if from 0 (top) to 191 (bottom) so we start at the bottom (151+40=191) and we work our way to the top (1) and we “step” this by a -4 which means we’ll jump “up” 4 pixels on the screen for each count of this loop until we reach the top.
Line 96 “gets” the background of this page which would contain any potential stars and planets that were produced and copied here. This is the “undo” option we’ll restore when we’re done.
Line 97 then puts the reverse mask of the spaceship on the same spot, using the AND option, which only applies pixels to where pixels already exist in the background, so anything that was visible gets “blacked out” using that negative cookie cutter of the space ship. Once the background area, in the perfect shape of our space ship is blacked out, we can now paste the original line art vector “hollow” version of our ship in the same space, so we’ll see all of the outline detail, and the clear black internals to it’s shape, minus the window, I never painted or masked that (on purpose) so the window remains “clear” and we can see the stars and planets “Behind” the spaceship.
At this point, the final composited foreground and background elements have been combined, and line 98 does the reverse PCOPY routine to take what we just finished, and presents that to the visible page that’s currently being viewed, and we actually see the final product of this composition. This process is known as “page flipping” and some people refer to it as “double buffering”. After the page is copied, and before we continue to loop, and move up further, the last thing we do is to “PUT back” the original background image behind the spaceship, essentially restoring it to it’s original state, and removing all traces of the spaceship in that location. As we go “more next” through the loop, the Y coordinate will jump up 4 pixels, and the process will repeat. Putting the array variable “BG” data back is how we restore the background that we “got” before we started to “put” the two spaceship layers.
Line 99 was modified to allow an “any key” to be pressed to end the demo, as it loops forever. If no key is pressed, it continues the FOR/NEXT loop bringing the spaceship all the way to the top of the screen and the loop ends.
Line 100 basically starts the loop all over again, each time the loop starts, a new random value for X is generated so the spaceship will usually appear somewhere else on the bottom of the screen and start flying up again. To add variety to this, and prevent us from having to look at the same planets all the time, I included a random decision, where if the number 3 chosen from a 1-5 random decision, the program will loop back to line 40 and re-draw a whole new screen of stars and planets and then start the animation loop all over again.
This will continue to loop until you press a key, and greeted with the closing message:
When you press “any” key, the computer issues the slow down poke again, putting the system in normal speed (high speed can affect disk utilization), clears the screen and says “Thank you for trying the rocket demo”, “Visit ogsteviestrow.com for all kinds of cool coco stuff”
And there we have it, the anatomy of a CoCo demo program, including some pretty fancy trickery! Let me know what you think, and look for a YouTube video of this updated demo coming soon. In the meantime, if you want to download it to try yourself, download the Spaceship Disk Image by clicking the link.
The YouTube video explaining all this can be found here.
4145total visits,9visits today