Note: The source code for this tutorial is available in the tutorials/TutorialGame_Introduction/part3.ras (in the TRSE framework), together with all resources mentioned in these texts.
Sprites, or movable blocks (MOBs, a technical term that nobody used) are the main source of movable entities on the C64. Sprites are usually the backbone of any game, being rasterized indepedently from the background character data. The C64 supports 8 simultaneous sprites, but this limit can be extended by using some neat tricks like multiplexing.
Creating a sprite
In your TRSE game tutorial project, press “New File->Image/Charset/Sprite” and select Sprite. For now, TRSE only supports multicolor sprites (verson 0.04 will support regular sprites as well). After having created the file, save it in a directory called “sprites” within the project folder.
C64 sprites are 3×21 = 63 byte blocks, with a resolution of 24×21 pixels. However, multicolor sprites will sacrifice this resolution for having two more colors per sprite, but halving the number of pixels to 12×21 pixels. Note: even if each sprite will contain only 63 bytes of data, they each occupy 64 bytes in RAM
Now click the “Colors” tab and select some different colors. Even though your multicolor sprite can contain 4 colors, 3 of them must be shared between all your sprites. These three colors are defined as “Background“, “Multicolor 1” and “Multicolor 2“. The final color is chosen freely. If you try to use another color, your previous freely chosen color will be replaced. Here’s a random sprite:
f you press the “Charset” tab, you’ll be presented with a selectable view of all your sprites in the current file. You can also switch between sprites by pressing the left/right arrow.
Finally, export your sprite by clicking the toolbox tab selecting the “Export bin” button. Save the file as “sprites/sprites.bin”.
Rendering sprites
Create a new source file by clicking file->new source with “part3.ras” (or whatever) as the filename. Let’s start by initializing and clearing the screen:
program part3;
var
i: byte;
begin
SCREEN_BG_COL:=BLACK;
SCREEN_FG_COL:=BLACK;
ClearScreen($20, $0400);
Loop();
end.
Here, we set bakcground and border color to black, and clear the screen (fill the screen area at $0400 with 40*25 bytes of value $20, which is clear space). Finally, the program just loops endlessly. Let’s include and initialize our sprites!
program part3;
var
mySprites:incbin("sprites/sprites.bin", $3200);
procedure InitSprites();
begin
setspriteloc(0,$C8,0);
setspriteloc(1,$C9,0);
spritepos(60,100,0);
spritepos(220,100,1);
SPRITE_BITMASK:=%00000011;
end;
Some comments:
- We included the binary file at memory address $3200. Since the sprite locations are divided into 64 byte chunks, in order to find the first sprite we need to divide $3200 / 64 = $C8. The next sprites will be located at $C9, $CA etc.
- SetSpriteLoc sets sprite from parameter 1 at location parameter 2 on bank parameter 3. For now, we stick to bank 0.
- SpritePos sets the x and y coordinates of the sprite from parameter 3. Note that the first value can either be a 0-255 constant or an integer for values >255<320.
- We set SPRITE_BITMASK to enable sprite 0 and 1 by toggling the first two bits.
When calling this method (remember to call InitSprites before the loop), you should see something like this:
All we need to do now is the following:
Set (global) multicolor values to grey and light green
SPRITE_MULTICOLOR_REG1:=LIGHT_GREEN;
SPRITE_MULTICOLOR_REG2:=GREY;
Enable multicolor for sprite 0 and 1
SPRITE_MULTICOLOR:=%00000011;
and finally set individual colors
SPRITE_COLOR[0]:=RED;
SPRITE_COLOR[1]:=BLUE;
and that’s it! Tutorial part 3 done!