This series of articles, entitled Into the Arc, is intended to introduce the newcomer to the Archimedes to its various features. Previously, I have tended to concentrate on certain aspects which are important when you want to write programs in Basic, but our series is not aimed only at programmers. It is also our intention to cover other aspects of the Archimedes system for the benefit of programmers and users alike.
This month, therefore, I propose to look at the use of sprites on the Archimedes. You can hardly miss them anyway, as they are visible on the screen from the moment you switch on your Archimedes and the Desktop appears. Many of the icons you see on the screen are sprites, and in the course of this article I will show you how to create a sprite to represent any program in a directory viewer. Sprites are often used within programs as well, to provide a wealth of graphics objects to be used as the program determines. I will also be dealing with this and the commands provided by RISC OS for this purpose.
Although a sprite is just a graphics image, what distinguishes a sprite from any other form of graphics is that the Archimedes has a range of built-in functions specifically for manipulating sprites. This makes life particularly easy for the user, and results in some very efficient screen operations.
In fact, sprites can be treated in two ways, as system sprites and as user sprites. System sprites are the ones we are talking about, and one of the things you need to watch is the allocation of sufficient memory for the sprites you are using with the help of the Task Manager. User sprites are similar, but everything about them including the allocation of memory is directly under the control of the user program. Many of the programs on the Applications discs, and much commercial software, use user sprites, which is why you never need to worry about allocating sprite space when using these programs. For more information on user sprites refer to the article on this subject in RISC User Volume 2 Issue 3, and again in Volume 2 Issue 6.
One of the easiest ways of finding out more about sprites is by using the Paint program supplied on Applications Disc 1. In fact, this package has as its prime function the creation and editing of sprites, so what more could we want?
Sprites are saved in a sprite file, and there is a sample sprite file on the disc called PaintDemo. Once you have installed the Paint application on the menu bar (as described in the User Guide) just drag the sprite file icon over the Paint icon to get started.
Alternatively, double click on any application icon on an Applications disc while holding down the Shift key. This will open the application's directory, displaying the files which comprise that application, including a file called !Sprites, and maybe another file called Sprites as well. All sprite files are distinguished by the paintbrush-over-house image.
Try this out for the Paint application itself. Drag the !Sprites file over the Paint icon, and a window will open showing two sprites which are the large and small icons used to represent Paint on the screen. If you do the same with the file Sprites, the resulting window will show the 49 different sprites used by the Paint program.
At this stage what we are seeing is a sprite file window. The sprite file has been loaded into memory, and any of the sprites can be selected and edited. This is accomplished by double clicking on any individual sprite. This in turn opens a sprite window showing the sprite itself. Clicking the Menu button over the sprite window reveals a series of options. In particular, the Paint option allows you to Show colours (the palette used by the sprite), and to Show tools (the tools which you use for creating and editing sprites). There is also a zoom option which allows you to temporarily enlarge or shrink a sprite to help with any editing.
One point to note concerns the saving of a newly created or edited sprite. Clicking Menu over a sprite window provides the usual RISC OS save option. Drag the sprite icon to the sprite file window. This saves the sprite in the current copy of the sprite file in memory. It does not alter any copy of the sprite file previously saved to disc. To do this, click the Menu button, this time over the sprite file window, and repeat the save operation, this time dragging the relevant file icon to the directory viewer where you want to save the file. So remember, saving a sprite only produces a temporary copy in memory; a second save is needed to make a permanent copy on disc.
Of course, if you edit and save any of the sprites used by the Applications discs, you could end up permanently changing some of these carefully designed icons, so beware. Let's look instead at how we could create an icon ourselves.
First of all click on the Paint icon on the menu bar which will open up an empty sprite file window. Now click the Menu button over this window and choose Create. Enter a name for your sprite (whatever you choose). We also have to specify a size for the sprite in terms of pixels. A sprite used as a file icon is usually 34 pixels wide by 17 high, so that's what we'll choose. The other information can be ignored as long as we do not use a 256 colour mode. Click on 'OK' and a blank sprite of the size defined will appear in the sprite file window with the name you chose.
Now double click on this icon to open its sprite window (which will be blank at this stage). You can now proceed to design your sprite. If you haven't used Paint much before, then click the Menu button over the sprite window, choosing Paint and then Show colours. From now on Select whichever colour you want to work with and colour in the pixels for your sprite. Remember that the sprite window is just a window, and you would be well advised to drag the window to its maximum size using the bottom right-hand corner so that you can see your sprite design in full.
It's also a good idea to position the colour palette window, the sprite window and the sprite file window so that all three are visible at the same time. You can then readily select colours at will, and you can see your sprite at normal size building up in the sprite file window if you create it at a magnified size in the sprite file window. Of course, Paint offers many other facilities, and these are described at length in the User Guide.
Once you are satisfied with your sprite then you need to save it to the sprite file in memory, and then save the sprite file to disc. It obviously makes sense to create a directory in which to put any and all of the sprite files which you build up. Remember too, that by repeating the process described above (by clicking Menu over the sprite file window and selecting Create) you could create a second and further sprites, saving them all in the same sprite file.
Now let's see how we could use the sprite we have created as an icon to represent a program in a directory viewer. Suppose our application is to be called 'RiscEx'. First of all you will need to create a new directory with the name !RiscEx (open the directory viewer for your disc, and then press Menu to select the appropriate option - New directory). At the moment the icon for this will be the standard RISC OS applications icon. Now hold down the Shift key and double click on this icon to open the corresponding directory viewer. Copy, by dragging, your program into this window, and then rename it as !RunImage (click on Menu, selecting the file name, and then the Rename option).
Next, copy your sprite file from wherever you saved it into the same window and rename this as !Sprites. To make everything work properly you will also need to change the name of the sprite which represents your program so that it has the same name, in our example !RiscEx. To do this drag the sprite file from the !RiscEx directory to Paint in order to open the sprite file window. Now use the Menu button, selecting the Rename option to change the name of the sprite itself to !RiscEx, and re-save the sprite file again as !Sprites.
If you now remove all the windows from the screen, and then click on the drive icon on the menu bar to re-open the directory viewer for your disc, you should find that your application is represented by the icon which you created with Paint. If you are editing or changing an existing sprite, you will also need to press Ctrl-Break before clicking on the drive icon because of the way the Wimp sprite area is organised.
In order to complete the application, you will also need to use Edit to create one more short file, to be called !Run, to be saved in the !RiscEx directory.
Assuming Edit has been installed, click with the Menu button on the Edit icon, selecting Create and then New Obey file. In the Edit window which appears enter:
WimpSlot -min 32K -max 32K
Run <Obey$Dir>.!RunImage %*0
and save this file in the !RiscEx directory as !Run. The value given with the WimpSlot command will depend on the size of your program. Remember that to open the !RiscEx directory you will need to double click on its icon while you hold down the Shift key, because it is an applications directory. For more help in creating an applications directory, refer to the article on this subject in RISC User Volume 2 Issue 4.
The end result is not only that your program will be represented by a customised icon on the screen, but that your program can be run from the Desktop simply by double clicking on its icon.
The above description of the steps needed may sound daunting, but if you follow what I have described, referring to the User Guide and other articles cited you should be able to achieve the same result, and the effort of learning this process is well worthwhile for the professional results achieved. After a few tries, you should find the whole process quite straightforward to use.
As I said at the start there are two obvious uses for sprites, as icons to represent files and programs, and as icons or other objects which we can use within a program. For the latter, RISC OS provides a number of commands. Assuming you have created a sprite file with one or more sprites as already described, proceed as follows.
Exit from the Desktop (press Ctrl-Shift-F12) and enter Basic by typing BASIC. Use the *DIR command to get to the directory containing your sprite file and enter:
*SLOAD <sprite file>
specifying the appropriate name, e.g.:
*SLOAD MySprites
This loads the sprite file into the system sprite area of memory. If there is insufficient space, go back to the Desktop (*Desktop) and use the Task Manager to allocate more sprite space.
Assuming that all is well, the command:
*SList
will list the names of the sprites contained in that sprite file, for example:
sprite1
sprite2
car
boat
etc
Within a program, any action such as placing a sprite in a particular position on the screen, refers to the current sprite. This is determined by the command *SChoose, e.g.:
*SChoose sprite1
Once a sprite has been selected as the current sprite it can be positioned wherever you like on the screen with Basic's PLOT instruction. If you had a sprite called Car in your sprite file, then:
*SChoose car
PLOT &ED,0,0
would plot the sprite on the screen with its bottom left-hand corner in position (0,0), the bottom left-hand corner of the screen. All the usual considerations that apply to graphics, such as style of plotting (Exclusive OR plotting for example) can be applied to sprites.
For more information on the subject of sprites, see the article Animating Archie in RISC User Volume 1 Issue 8. See also the User Guide with regard to the use of Paint, and the Basic Guide for information on the various commands that may be used to manipulate sprites from within Basic programs. Above all else, greater familiarity with the subject is best achieved by experimenting yourself, and I hope this article will have given you the encouragement to do that.