| 1 |
|
| 2 |
Level 9 interpreter |
| 3 |
Version 5.2 |
| 4 |
|
| 5 |
This guide is by David Kinder. |
| 6 |
|
| 7 |
|
| 8 |
Level9 has already been compiled on Windows, MS-DOS, Apple Mac, Amiga, Acorn |
| 9 |
and Unix systems, so it should be quite easy to get it working on any other |
| 10 |
modern computer. |
| 11 |
|
| 12 |
The first thing that must be done is to check the typedefs in level9.h. |
| 13 |
The typedefs must have the following properties: |
| 14 |
|
| 15 |
L9BYTE unsigned 8 bit quantity |
| 16 |
L9UINT16 unsigned 16 bit quantity |
| 17 |
L9UINT32 unsigned 32 bit quantity |
| 18 |
L9BOOL quantity capable of holding the values |
| 19 |
TRUE (1) and FALSE (0) |
| 20 |
|
| 21 |
Also the define MAX_PATH (the maximum length of the full pathname of a file) |
| 22 |
must be set, e.g. |
| 23 |
|
| 24 |
#define MAX_PATH 256 |
| 25 |
|
| 26 |
If graphics are not supported defining NO_SCAN_GRAPHICS will stop the |
| 27 |
intialization code from looking for graphics data, which may take a noticeable |
| 28 |
length of time on slower computers. |
| 29 |
|
| 30 |
|
| 31 |
It is required that several os_ functions be written for your system. Given |
| 32 |
below is a guide to these functions, and a very simple interface is included |
| 33 |
in generic.c. |
| 34 |
|
| 35 |
|
| 36 |
void os_printchar(char c) |
| 37 |
|
| 38 |
os_printchar() prints a character to the output. The interface |
| 39 |
can either buffer this character or print it immediately, but |
| 40 |
if buffering is used then the characters must all be sent to the |
| 41 |
output when the interpreter calls os_flush(). A paragraph of |
| 42 |
text is output as one long stream of characters, without line |
| 43 |
breaks, so the interface must provide its own word wrapping and |
| 44 |
any other features that are desired, such as justification or a |
| 45 |
[More] prompt. The carriage return character is always '\r', |
| 46 |
rather than '\n'. |
| 47 |
|
| 48 |
|
| 49 |
L9BOOL os_input(char* ibuff, int size) |
| 50 |
|
| 51 |
os_input() reads a line of text from the user, usually to accept |
| 52 |
the next command to be sent to the game. The text input must be |
| 53 |
stored in ibuff with a terminating zero, and be no longer than |
| 54 |
size characters. Normally os_input() should return TRUE, but may |
| 55 |
return FALSE to cause the entire input so far to be discarded. |
| 56 |
The reason for doing so is discussed in the section at the end |
| 57 |
on allowing the interpreter to load a new game without exiting. |
| 58 |
|
| 59 |
|
| 60 |
char os_readchar(L9UINT32 millis) |
| 61 |
|
| 62 |
os_readchar() looks to see if a key has been pressed if one has, |
| 63 |
returns the character to the interpreter immediately. If no key |
| 64 |
has been pressed the interpreter should wait for a key for at |
| 65 |
least the number of milliseconds given in the argument. If after |
| 66 |
this period no key has been pressed, 0 should be returned. This |
| 67 |
is most commonly used when a game is exited, causing it to print |
| 68 |
"Press SPACE to play again" and then call os_readchar(). |
| 69 |
|
| 70 |
|
| 71 |
L9BOOL os_stoplist(void) |
| 72 |
|
| 73 |
Called during dictionary listing. If true is returned (typically |
| 74 |
because the user has pressed a key) then the listing is stopped. |
| 75 |
This routine should return immediately, without waiting. If this |
| 76 |
is not possible then FALSE should be returned. |
| 77 |
|
| 78 |
|
| 79 |
void os_flush(void) |
| 80 |
|
| 81 |
If the calls to os_printchar() are being buffered by the |
| 82 |
interface then the buffered text must be printed when os_flush() |
| 83 |
is called. |
| 84 |
|
| 85 |
|
| 86 |
L9BOOL os_save_file(L9BYTE* Ptr, int Bytes) |
| 87 |
|
| 88 |
os_save_file() should prompt the user in some way (with either |
| 89 |
text or a file requester) for a filename to save the area of |
| 90 |
memory of size Bytes pointed to by Ptr. TRUE or FALSE should be |
| 91 |
returned depending on whether or not the operation was successful. |
| 92 |
|
| 93 |
|
| 94 |
L9BOOL os_load_file(L9BYTE* Ptr, int* Bytes, int Max) |
| 95 |
|
| 96 |
os_load_file() should prompt the user for the name of a file to |
| 97 |
load. At most Max bytes should be loaded into the memory pointed |
| 98 |
to by Ptr, and the number of bytes read should be placed into the |
| 99 |
variable pointed to by Bytes. |
| 100 |
|
| 101 |
|
| 102 |
L9BOOL os_get_game_file(char* NewName, int Size) |
| 103 |
|
| 104 |
os_get_game_file() should prompt the user for a new game file, to |
| 105 |
be stored in NewName, which can take a maximum name of Size |
| 106 |
characters. When this function is called the NewName array |
| 107 |
contains the name of the currently loaded game, which can be used |
| 108 |
to derive a name to prompt the user with. |
| 109 |
|
| 110 |
This is used by at least the Adrian Mole games, which load in the |
| 111 |
next part of the game after the part currently being played has |
| 112 |
been completed. These games were originally written for tape-based |
| 113 |
systems where the call was simply "load the next game from the |
| 114 |
tape". |
| 115 |
|
| 116 |
|
| 117 |
void os_set_filenumber(char* NewName, int Size, int n) |
| 118 |
|
| 119 |
os_set_filename() is for multi-part games originally written for |
| 120 |
disk-based systems, which used game filenames such as |
| 121 |
|
| 122 |
gamedat1.dat |
| 123 |
gamedat2.dat |
| 124 |
|
| 125 |
etc. The routine should take the full filename in NewName (of |
| 126 |
maximum size Size) and modify it to reflect the number n, e.g. |
| 127 |
os_set_filename("gamedat1.dat",2) should leave "gamedat2.dat" |
| 128 |
in NewName. |
| 129 |
|
| 130 |
|
| 131 |
FILE* os_open_script_file(void) |
| 132 |
|
| 133 |
os_open_script_file() should prompt the user for the name of a |
| 134 |
script file, from which input will be read until the end of the |
| 135 |
script file is reached, and should return a pointer to the opened |
| 136 |
script file, or NULL. This function is called in response to |
| 137 |
the player entering the "#play" meta-command. |
| 138 |
|
| 139 |
|
| 140 |
void os_graphics(int mode) |
| 141 |
|
| 142 |
Called when graphics are turned on or off, either by the game or |
| 143 |
by the user entering "graphics" or "text" as a command. If mode |
| 144 |
is 0 graphics should be turned off. If mode is 1 then line drawn |
| 145 |
graphics will follow, so graphics should be turned on. If mode is |
| 146 |
2 then bitmap graphics will follow, so graphics should be turned |
| 147 |
on, provided that appropriate bitmap graphics files are available |
| 148 |
(This can be determined by calling DetectBitmaps(), which is |
| 149 |
discussed below.). After an os_graphics(0) call all the other |
| 150 |
graphics functions should do nothing. |
| 151 |
|
| 152 |
Typically, if mode is not 0 the code will allocate some suitable |
| 153 |
bitmap for drawing graphics into. For line drawn graphics, to |
| 154 |
determine the size of the bitmap the code should call |
| 155 |
GetPictureSize(). The graphics routines should draw in a bitmap of |
| 156 |
the size returned by this function, and then scale the bitmap |
| 157 |
appropriately for display. If instead the graphics code tries to |
| 158 |
scale the co-ordinates passed to os_drawline() and os_fill() then |
| 159 |
problems occur with fill colours "leaking" into other areas of the |
| 160 |
picture. The values returned by GetPictureSize() will not change |
| 161 |
unless a new game is loaded. |
| 162 |
|
| 163 |
The graphics bitmap for line drawn graphics is always 4 colour. |
| 164 |
The 4 colours are chosen from a possible 8 by calls to |
| 165 |
os_setcolour (see below). Note that a call to os_setcolour() must |
| 166 |
affect the colour of pixels already drawn on the bitmap. For |
| 167 |
example, suppose a pixel in the bitmap is set to the first colour |
| 168 |
in the palette during drawing, which at that moment is red. If |
| 169 |
later the first colour in the palette is set to blue, at the end |
| 170 |
the pixel should be shown blue. |
| 171 |
|
| 172 |
In order to actually draw graphics, the input routines os_input() |
| 173 |
and os_readchar() should call RunGraphics(). This is discussed |
| 174 |
further below. |
| 175 |
|
| 176 |
|
| 177 |
void os_cleargraphics(void) |
| 178 |
|
| 179 |
Clear the current graphics bitmap by filling the entire bitmap |
| 180 |
with colour 0. |
| 181 |
|
| 182 |
|
| 183 |
void os_setcolour(int colour, int index) |
| 184 |
|
| 185 |
Set the given colour in the graphics bitmap's palette to the |
| 186 |
colour at the given index in the interpreter's table of colours. |
| 187 |
|
| 188 |
The actual table of colours in the interpreters provided by |
| 189 |
Level 9 vary across different machines. An acceptable palette |
| 190 |
that matches reasonably closely to the Amiga releases is as |
| 191 |
follows (all colours are 8 bit R,G,B): |
| 192 |
|
| 193 |
0x00,0x00,0x00 (black) |
| 194 |
0xFF,0x00,0x00 (red) |
| 195 |
0x30,0xE8,0x30 (green) |
| 196 |
0xFF,0xFF,0x00 (yellow) |
| 197 |
0x00,0x00,0xFF (blue) |
| 198 |
0xA0,0x68,0x00 (brown) |
| 199 |
0x00,0xFF,0xFF (cyan) |
| 200 |
0xFF,0xFF,0xFF (white) |
| 201 |
|
| 202 |
|
| 203 |
void os_drawline(int x1, int y1, int x2, int y2, int colour1, int colour2) |
| 204 |
|
| 205 |
Draw a line on the graphics bitmap between (x1,y1) and (x2,y2). |
| 206 |
Note that either point may lie outside of the bitmap, and that it |
| 207 |
is the responsibility of the routine to clip to the appropriate |
| 208 |
co-ordinates. |
| 209 |
|
| 210 |
For each point on the line, if the colour at that point is equal |
| 211 |
to colour2 the pixel's colour should be changed to colour1, else |
| 212 |
it should not be modified. |
| 213 |
|
| 214 |
|
| 215 |
void os_fill(int x, int y, int colour1, int colour2) |
| 216 |
|
| 217 |
If the pixel's colour at (x,y) is equal to colour2, fill the |
| 218 |
region containing (x,y) with colour1. The boundaries of the |
| 219 |
region are defined as those areas of the bitmap with a colour |
| 220 |
other than colour2. |
| 221 |
|
| 222 |
|
| 223 |
void os_show_bitmap(int pic, int x, int y) |
| 224 |
|
| 225 |
Show the bitmap given by the number pic at the co-ordinates |
| 226 |
(x,y). |
| 227 |
|
| 228 |
Note that the game can request the same picture several times |
| 229 |
in a row: it is a good idea for ports to record the last picture |
| 230 |
number and check it against any new requests. |
| 231 |
|
| 232 |
The interpreter source code provides a decoder that understands |
| 233 |
most Level 9 bitmap formats. The decoder is accessed by calling |
| 234 |
DecodeBitmap(), which is discussed below. |
| 235 |
|
| 236 |
L9BOOL os_find_file(char* NewName) |
| 237 |
|
| 238 |
Return whether the given file exists or not. On case sensitive |
| 239 |
file systems, the interpreter should check whether the file |
| 240 |
exists as all uppercase and all lowercase. If it does, the |
| 241 |
routine should return TRUE, with the string pointed to by |
| 242 |
NewName changed to match the file name as it appears in the |
| 243 |
file system. |
| 244 |
|
| 245 |
|
| 246 |
int main(int argc, char** argv) |
| 247 |
|
| 248 |
You must provide your own main() entry point for the program. |
| 249 |
The simplest such main() is given in generic.c, which just calls |
| 250 |
LoadGame() and then sits in a loop calling RunGame(). These |
| 251 |
functions are discussed below. |
| 252 |
|
| 253 |
|
| 254 |
The interpreter provides several functions to be called by the interface |
| 255 |
code. These are: |
| 256 |
|
| 257 |
|
| 258 |
L9BOOL LoadGame(char* filename, char* picname) |
| 259 |
|
| 260 |
LoadGame() attempts to load filename and then searches it for |
| 261 |
a valid Level 9 game. If it is successful TRUE is returned, else |
| 262 |
FALSE. The previous game in memory will be overwritten if the |
| 263 |
file filename can be loaded, even if it does not contain a Level 9 |
| 264 |
game, so even if LoadGame() returns FALSE it must be assumed that |
| 265 |
the game memory has changed. |
| 266 |
|
| 267 |
The second argument is the name of the file containing picture |
| 268 |
data, and may be NULL. Ports should usually ask the user for just |
| 269 |
the filename and derive picname from it in some way. The |
| 270 |
recommended approach is to first try the filename with an extension |
| 271 |
of ".pic" and then try replacing the filename with "picture.dat". |
| 272 |
|
| 273 |
|
| 274 |
L9BOOL RunGame(void) |
| 275 |
|
| 276 |
If LoadGame() has been successful, RunGame() can be called to run |
| 277 |
the Level 9 game. Each call to RunGame() executes a single opcode |
| 278 |
of the game. In pre-emptive multitasking systems or systems without |
| 279 |
any multitasking it is enough to sit in a loop calling RunGame(), |
| 280 |
e.g. |
| 281 |
while (RunGame()); |
| 282 |
|
| 283 |
RunGame() returns TRUE if an opcode code was executed and FALSE if |
| 284 |
the game is stopped, either by an error or by a call to StopGame(). |
| 285 |
|
| 286 |
|
| 287 |
void StopGame(void) |
| 288 |
|
| 289 |
StopGame() stops the current game from playing. |
| 290 |
|
| 291 |
|
| 292 |
void RestoreGame(char *inFile) |
| 293 |
|
| 294 |
RestoreGame() attempts to restore the currently running game to |
| 295 |
the position stored in the inFile saved game file. This gives |
| 296 |
interface code a means to restore a game position. |
| 297 |
|
| 298 |
|
| 299 |
void FreeMemory(void) |
| 300 |
|
| 301 |
FreeMemory() frees memory used to store the game. This routine |
| 302 |
should be called when exiting the interpreter. |
| 303 |
|
| 304 |
|
| 305 |
void GetPictureSize(int* width, int* height) |
| 306 |
|
| 307 |
Returns the width and height of the bitmap that graphics should |
| 308 |
be drawn into. This is constant for any particular game. |
| 309 |
|
| 310 |
|
| 311 |
L9BOOL RunGraphics(void) |
| 312 |
|
| 313 |
Runs an opcode of the graphics routines. If a graphics opcode was |
| 314 |
run TRUE is returned, otherwise FALSE. |
| 315 |
|
| 316 |
The simplest way to get graphics to display is to add a loop to |
| 317 |
repeatedly call RunGraphics() to os_input() and os_readchar(): |
| 318 |
|
| 319 |
while (RunGraphics()); |
| 320 |
/* Now draw graphics bitmap on display... */ |
| 321 |
|
| 322 |
Optionally, the code can provide a more "atmospheric" recreation |
| 323 |
of the games by drawing the graphics slowly, as was the case on |
| 324 |
the old 8-bit computers. This is achieved by calling RunGraphics() |
| 325 |
several times then waiting for a while before calling it again. |
| 326 |
Note that when waiting the code should still respond to user |
| 327 |
input. |
| 328 |
|
| 329 |
|
| 330 |
BitmapType DetectBitmaps(char* dir) |
| 331 |
|
| 332 |
Given a directory, returns the type of bitmap picture files in it, |
| 333 |
or NO_BITMAPS if there are no bitmaps. |
| 334 |
|
| 335 |
This function is only available if the preprocessor symbol |
| 336 |
BITMAP_DECODER is defined. |
| 337 |
|
| 338 |
|
| 339 |
Bitmap* DecodeBitmap(char* dir, BitmapType type, int num, int x, int y) |
| 340 |
|
| 341 |
This function loads and decodes the specified bitmap, returning |
| 342 |
a Bitmap structure. The structure contains the width and height |
| 343 |
of the bitmap, a palette of up to 32 colours used in the bitmap, |
| 344 |
and the actual data as an array of indexes into the palette. |
| 345 |
|
| 346 |
This function is only available if the preprocessor symbol |
| 347 |
BITMAP_DECODER is defined. |
| 348 |
|
| 349 |
|
| 350 |
One more complex feature of the interpreter is that a new Level 9 game can |
| 351 |
be loaded without exiting and restarting the interpreter. This is of use |
| 352 |
in a windowing environment. In this case, both main() and the code that |
| 353 |
catches a "New Game" menu item should call a routine such as the example |
| 354 |
new_game() below. This ensures that each new game does not use up more and |
| 355 |
more of the interpreter's stack. |
| 356 |
|
| 357 |
int newgame(char* game_name) |
| 358 |
{ |
| 359 |
static int playing = FALSE; |
| 360 |
|
| 361 |
if (LoadGame(game,NULL)) |
| 362 |
{ |
| 363 |
if (playing) |
| 364 |
return -1; |
| 365 |
|
| 366 |
playing = TRUE; |
| 367 |
while (RunGame()); |
| 368 |
playing = FALSE; |
| 369 |
} |
| 370 |
else |
| 371 |
warn("Unable to load game"); |
| 372 |
|
| 373 |
return -1; |
| 374 |
} |
| 375 |
|