rnd-20030118-1-src
[rocksndiamonds.git] / src / libgame / system.h
1 /***********************************************************
2 * Artsoft Retro-Game Library                               *
3 *----------------------------------------------------------*
4 * (c) 1994-2002 Artsoft Entertainment                      *
5 *               Holger Schemel                             *
6 *               Detmolder Strasse 189                      *
7 *               33604 Bielefeld                            *
8 *               Germany                                    *
9 *               e-mail: info@artsoft.org                   *
10 *----------------------------------------------------------*
11 * system.h                                                 *
12 ***********************************************************/
13
14 #ifndef SYSTEM_H
15 #define SYSTEM_H
16
17 #include "platform.h"
18 #include "types.h"
19
20 #if defined(PLATFORM_MSDOS)
21 #include "msdos.h"
22 #endif
23
24 #if defined(TARGET_SDL)
25 #include "sdl.h"
26 #elif defined(TARGET_X11)
27 #include "x11.h"
28 #endif
29
30 #if defined(PLATFORM_MACOSX)
31 /* some symbols are already defined on Mac OS X */
32 #define Delay Delay_internal
33 #define DrawLine DrawLine_internal
34 #define DrawText DrawText_internal
35 #define GetPixel GetPixel_internal
36 #endif
37
38
39 /* the additional 'b' is needed for Win32 to open files in binary mode */
40 #define MODE_READ               "rb"
41 #define MODE_WRITE              "wb"
42 #define MODE_APPEND             "ab"
43
44 #define DEFAULT_DEPTH           0
45
46 #define FULLSCREEN_NOT_AVAILABLE FALSE
47 #define FULLSCREEN_AVAILABLE     TRUE
48
49 /* default input keys */
50 #define DEFAULT_KEY_LEFT        KSYM_Left
51 #define DEFAULT_KEY_RIGHT       KSYM_Right
52 #define DEFAULT_KEY_UP          KSYM_Up
53 #define DEFAULT_KEY_DOWN        KSYM_Down
54 #if defined(PLATFORM_MACOSX)
55 #define DEFAULT_KEY_SNAP        KSYM_Control_L
56 #define DEFAULT_KEY_BOMB        KSYM_KP_Enter
57 #else
58 #define DEFAULT_KEY_SNAP        KSYM_Shift_L
59 #define DEFAULT_KEY_BOMB        KSYM_Shift_R
60 #endif
61 #define DEFAULT_KEY_OKAY        KSYM_Return
62 #define DEFAULT_KEY_CANCEL      KSYM_Escape
63
64 /* default shortcut keys */
65 #define DEFAULT_KEY_SAVE_GAME   KSYM_F1
66 #define DEFAULT_KEY_LOAD_GAME   KSYM_F2
67 #define DEFAULT_KEY_TOGGLE_PAUSE KSYM_space
68
69 /* values for move directions and special "button" keys */
70 #define MV_BIT_LEFT             0
71 #define MV_BIT_RIGHT            1
72 #define MV_BIT_UP               2
73 #define MV_BIT_DOWN             3
74 #define NUM_MV_DIRECTIONS       4
75
76 #define MV_NO_MOVING            0
77 #define MV_LEFT                 (1 << MV_BIT_LEFT)
78 #define MV_RIGHT                (1 << MV_BIT_RIGHT)
79 #define MV_UP                   (1 << MV_BIT_UP)
80 #define MV_DOWN                 (1 << MV_BIT_DOWN)
81 #define KEY_BUTTON_1            (1 << 4)
82 #define KEY_BUTTON_2            (1 << 5)
83 #define KEY_MOTION              (MV_LEFT | MV_RIGHT | MV_UP | MV_DOWN)
84 #define KEY_BUTTON              (KEY_BUTTON_1 | KEY_BUTTON_2)
85 #define KEY_ACTION              (KEY_MOTION | KEY_BUTTON)
86
87 #define MV_DIR_BIT(x)           ((x) == MV_LEFT  ? MV_BIT_LEFT  :       \
88                                  (x) == MV_RIGHT ? MV_BIT_RIGHT :       \
89                                  (x) == MV_UP    ? MV_BIT_UP    : MV_BIT_DOWN)
90
91 /* values for button status */
92 #define MB_NOT_PRESSED          FALSE
93 #define MB_NOT_RELEASED         TRUE
94 #define MB_RELEASED             FALSE
95 #define MB_PRESSED              TRUE
96 #define MB_MENU_CHOICE          FALSE
97 #define MB_MENU_MARK            TRUE
98 #define MB_MENU_INITIALIZE      (-1)
99 #define MB_MENU_LEAVE           (-2)
100 #define MB_LEFTBUTTON           1
101 #define MB_MIDDLEBUTTON         2
102 #define MB_RIGHTBUTTON          3
103
104 /* values for redraw_mask */
105 #define REDRAW_NONE             (0)
106 #define REDRAW_ALL              (1 << 0)
107 #define REDRAW_FIELD            (1 << 1)
108 #define REDRAW_TILES            (1 << 2)
109 #define REDRAW_DOOR_1           (1 << 3)
110 #define REDRAW_VIDEO_1          (1 << 4)
111 #define REDRAW_VIDEO_2          (1 << 5)
112 #define REDRAW_VIDEO_3          (1 << 6)
113 #define REDRAW_MICROLEVEL       (1 << 7)
114 #define REDRAW_MICROLABEL       (1 << 8)
115 #define REDRAW_FROM_BACKBUFFER  (1 << 9)
116 #define REDRAW_DOOR_2           (REDRAW_VIDEO_1 | \
117                                  REDRAW_VIDEO_2 | \
118                                  REDRAW_VIDEO_3)
119 #define REDRAW_DOOR_3           (1 << 10)
120 #define REDRAW_DOORS            (REDRAW_DOOR_1 | \
121                                  REDRAW_DOOR_2 | \
122                                  REDRAW_DOOR_3)
123 #define REDRAW_MAIN             (REDRAW_FIELD | \
124                                  REDRAW_TILES | \
125                                  REDRAW_MICROLEVEL)
126 #define REDRAW_FPS              (1 << 11)
127 #define REDRAWTILES_THRESHOLD   (SCR_FIELDX * SCR_FIELDY / 2)
128
129 /* maximum number of parallel players supported by libgame functions */
130 #define MAX_PLAYERS             4
131
132 /* maximum allowed length of player name */
133 #define MAX_PLAYER_NAME_LEN     10
134
135 /* default name for empty highscore entry */
136 #define EMPTY_PLAYER_NAME       "no name"
137
138 /* default name for unknown player names */
139 #define ANONYMOUS_NAME          "anonymous"
140
141 /* default text for non-existant artwork */
142 #define NOT_AVAILABLE           "(not available)"
143
144 /* default value for undefined filename */
145 #define UNDEFINED_FILENAME      "[NONE]"
146
147 /* default name for new levels */
148 #define NAMELESS_LEVEL_NAME     "nameless level"
149
150 /* definitions for game sub-directories */
151 #ifndef RO_GAME_DIR
152 #define RO_GAME_DIR             "."
153 #endif
154
155 #ifndef RW_GAME_DIR
156 #define RW_GAME_DIR             "."
157 #endif
158
159 #define RO_BASE_PATH            RO_GAME_DIR
160 #define RW_BASE_PATH            RW_GAME_DIR
161
162 #define GRAPHICS_DIRECTORY      "graphics"
163 #define SOUNDS_DIRECTORY        "sounds"
164 #define MUSIC_DIRECTORY         "music"
165 #define LEVELS_DIRECTORY        "levels"
166 #define TAPES_DIRECTORY         "tapes"
167 #define SCORES_DIRECTORY        "scores"
168
169 #if !defined(PLATFORM_MSDOS)
170 #define GRAPHICS_SUBDIR         "gfx_classic"
171 #define SOUNDS_SUBDIR           "snd_classic"
172 #define MUSIC_SUBDIR            "mus_classic"
173 #else
174 #define GRAPHICS_SUBDIR         "gfx_orig"
175 #define SOUNDS_SUBDIR           "snd_orig"
176 #define MUSIC_SUBDIR            "mus_orig"
177 #endif
178
179 /* areas in bitmap PIX_DOOR */
180 /* meaning in PIX_DB_DOOR: (3 PAGEs)
181    PAGEX1: 1. buffer for DOOR_1
182    PAGEX2: 2. buffer for DOOR_1
183    PAGEX3: buffer for animations
184 */
185
186 #define DOOR_GFX_PAGESIZE       (gfx.dxsize)
187 #define DOOR_GFX_PAGEX1         (0 * DOOR_GFX_PAGESIZE)
188 #define DOOR_GFX_PAGEX2         (1 * DOOR_GFX_PAGESIZE)
189 #define DOOR_GFX_PAGEX3         (2 * DOOR_GFX_PAGESIZE)
190 #define DOOR_GFX_PAGEX4         (3 * DOOR_GFX_PAGESIZE)
191 #define DOOR_GFX_PAGEX5         (4 * DOOR_GFX_PAGESIZE)
192 #define DOOR_GFX_PAGEX6         (5 * DOOR_GFX_PAGESIZE)
193 #define DOOR_GFX_PAGEX7         (6 * DOOR_GFX_PAGESIZE)
194 #define DOOR_GFX_PAGEX8         (7 * DOOR_GFX_PAGESIZE)
195 #define DOOR_GFX_PAGEY1         (0)
196 #define DOOR_GFX_PAGEY2         (gfx.dysize)
197
198 /* functions for version handling */
199 #define VERSION_IDENT(x,y,z)    ((x) * 10000 + (y) * 100 + (z))
200 #define VERSION_MAJOR(x)        ((x) / 10000)
201 #define VERSION_MINOR(x)        (((x) % 10000) / 100)
202 #define VERSION_PATCH(x)        ((x) % 100)
203
204 /* functions for parent/child process identification */
205 #define IS_PARENT_PROCESS(pid)  ((pid) > 0)
206 #define IS_CHILD_PROCESS(pid)   ((pid) == 0)
207
208
209 /* type definitions */
210 typedef int (*EventFilter)(const Event *);
211
212
213 /* structure definitions */
214
215 struct ProgramInfo
216 {
217   char *command_basename;
218   char *userdata_directory;
219
220   char *program_title;
221   char *window_title;
222   char *icon_title;
223
224   char *x11_icon_filename;
225   char *x11_iconmask_filename;
226   char *msdos_pointer_filename;
227
228   char *cookie_prefix;
229   char *filename_prefix;        /* prefix to cut off from DOS filenames */
230
231   int version_major;
232   int version_minor;
233   int version_patch;
234
235   void (*exit_function)(int);
236 };
237
238 struct OptionInfo
239 {
240   char *display_name;
241   char *server_host;
242   int server_port;
243
244   char *ro_base_directory;
245   char *rw_base_directory;
246   char *level_directory;
247   char *graphics_directory;
248   char *sounds_directory;
249   char *music_directory;
250   char *execute_command;
251
252   boolean serveronly;
253   boolean network;
254   boolean verbose;
255   boolean debug;
256 };
257
258 struct VideoSystemInfo
259 {
260   int default_depth;
261   int width, height, depth;
262   boolean fullscreen_available;
263   boolean fullscreen_enabled;
264 };
265
266 struct AudioSystemInfo
267 {
268   boolean sound_available;
269   boolean loops_available;
270   boolean music_available;
271
272   boolean sound_enabled;
273   boolean sound_deactivated;    /* for temporarily disabling sound */
274
275   int mixer_pipe[2];
276   int mixer_pid;
277   char *device_name;
278   int device_fd;
279
280   int num_channels;
281   int music_channel;
282   int first_sound_channel;
283 };
284
285 struct GfxInfo
286 {
287   int sx, sy;
288   int sxsize, sysize;
289   int real_sx, real_sy;
290   int full_sxsize, full_sysize;
291   int scrollbuffer_width, scrollbuffer_height;
292
293   int dx, dy;
294   int dxsize, dysize;
295
296   int vx, vy;
297   int vxsize, vysize;
298
299   int draw_deactivation_mask;
300   int draw_background_mask;
301
302   Bitmap *background_bitmap;
303   int background_bitmap_mask;
304 };
305
306 struct FontInfo
307 {
308   Bitmap *bitmap_initial;
309   Bitmap *bitmap_big;
310   Bitmap *bitmap_medium;
311   Bitmap *bitmap_small;
312   Bitmap *bitmap_tile;
313 };
314
315 struct JoystickInfo
316 {
317   int status;
318   int fd[MAX_PLAYERS];          /* file descriptor of player's joystick */
319 };
320
321 struct SetupJoystickInfo
322 {
323   char *device_name;            /* device name of player's joystick */
324
325   int xleft, xmiddle, xright;
326   int yupper, ymiddle, ylower;
327   int snap;
328   int bomb;
329 };
330
331 struct SetupKeyboardInfo
332 {
333   Key left;
334   Key right;
335   Key up;
336   Key down;
337   Key snap;
338   Key bomb;
339 };
340
341 struct SetupInputInfo
342 {
343   boolean use_joystick;
344   struct SetupJoystickInfo joy;
345   struct SetupKeyboardInfo key;
346 };
347
348 struct SetupEditorInfo
349 {
350   boolean el_boulderdash;
351   boolean el_emerald_mine;
352   boolean el_more;
353   boolean el_sokoban;
354   boolean el_supaplex;
355   boolean el_diamond_caves;
356   boolean el_dx_boulderdash;
357   boolean el_chars;
358   boolean el_custom;
359 };
360
361 struct SetupShortcutInfo
362 {
363   Key save_game;
364   Key load_game;
365   Key toggle_pause;
366 };
367
368 struct SetupInfo
369 {
370   char *player_name;
371
372   boolean sound;
373   boolean sound_loops;
374   boolean sound_music;
375   boolean sound_simple;
376   boolean toons;
377   boolean double_buffering;
378   boolean direct_draw;          /* !double_buffering (redundant!) */
379   boolean scroll_delay;
380   boolean soft_scrolling;
381   boolean fading;
382   boolean autorecord;
383   boolean quick_doors;
384   boolean team_mode;
385   boolean handicap;
386   boolean time_limit;
387   boolean fullscreen;
388   boolean ask_on_escape;
389
390   char *graphics_set;
391   char *sounds_set;
392   char *music_set;
393   boolean override_level_graphics;
394   boolean override_level_sounds;
395   boolean override_level_music;
396
397   struct SetupEditorInfo editor;
398   struct SetupShortcutInfo shortcut;
399   struct SetupInputInfo input[MAX_PLAYERS];
400 };
401
402 #define TREE_TYPE_GENERIC               0
403 #define TREE_TYPE_GRAPHICS_DIR          1
404 #define TREE_TYPE_SOUNDS_DIR            2
405 #define TREE_TYPE_MUSIC_DIR             3
406 #define TREE_TYPE_LEVEL_DIR             4
407
408 #define ARTWORK_TYPE_GRAPHICS           TREE_TYPE_GRAPHICS_DIR
409 #define ARTWORK_TYPE_SOUNDS             TREE_TYPE_SOUNDS_DIR
410 #define ARTWORK_TYPE_MUSIC              TREE_TYPE_MUSIC_DIR
411
412 struct TreeInfo
413 {
414   struct TreeInfo **node_top;           /* topmost node in tree */
415   struct TreeInfo *node_parent;         /* parent level directory info */
416   struct TreeInfo *node_group;          /* level group sub-directory info */
417   struct TreeInfo *next;                /* next level series structure node */
418
419   int cl_first;         /* internal control field for setup screen */
420   int cl_cursor;        /* internal control field for setup screen */
421
422   int type;             /* type of tree content */
423
424   /* fields for "type == TREE_TYPE_LEVEL_DIR" */
425
426   char *filename;       /* tree info sub-directory basename (may be ".") */
427   char *fullpath;       /* complete path relative to tree base directory */
428   char *basepath;       /* absolute base path of tree base directory */
429   char *identifier;     /* identifier string for configuration files */
430   char *name;           /* tree info name, as displayed in selection menues */
431   char *name_sorting;   /* optional sorting name for correct name sorting */
432   char *author;         /* level or artwork author name */
433   char *imported_from;  /* optional comment for imported levels or artwork */
434
435   char *graphics_set;   /* optional custom graphics set (level tree only) */
436   char *sounds_set;     /* optional custom sounds set (level tree only) */
437   char *music_set;      /* optional custom music set (level tree only) */
438   char *graphics_path;  /* path to optional custom graphics set (level only) */
439   char *sounds_path;    /* path to optional custom sounds set (level only) */
440   char *music_path;     /* path to optional custom music set (level only) */
441
442   int levels;           /* number of levels in level series */
443   int first_level;      /* first level number (to allow start with 0 or 1) */
444   int last_level;       /* last level number (automatically calculated) */
445   int sort_priority;    /* sort levels by 'sort_priority' and then by name */
446
447   boolean level_group;  /* directory contains more level series directories */
448   boolean parent_link;  /* entry links back to parent directory */
449   boolean user_defined; /* user defined levels are stored in home directory */
450   boolean readonly;     /* readonly levels can not be changed with editor */
451
452   int color;            /* color to use on selection screen for this level */
453   char *class_desc;     /* description of level series class */
454   int handicap_level;   /* number of the lowest unsolved level */
455 };
456
457 typedef struct TreeInfo TreeInfo;
458 typedef struct TreeInfo LevelDirTree;
459 typedef struct TreeInfo ArtworkDirTree;
460 typedef struct TreeInfo GraphicsDirTree;
461 typedef struct TreeInfo SoundsDirTree;
462 typedef struct TreeInfo MusicDirTree;
463
464 struct ArtworkInfo
465 {
466   GraphicsDirTree *gfx_first;
467   GraphicsDirTree *gfx_current;
468   SoundsDirTree *snd_first;
469   SoundsDirTree *snd_current;
470   MusicDirTree *mus_first;
471   MusicDirTree *mus_current;
472
473   char *gfx_current_identifier;
474   char *snd_current_identifier;
475   char *mus_current_identifier;
476 };
477
478 struct ConfigInfo
479 {
480   char *token;
481   char *value;
482   int type;
483 };
484
485 struct FileInfo
486 {
487   char *token;
488
489   char *default_filename;
490   char *filename;
491
492   int *default_parameter;                       /* array of file parameters */
493   int *parameter;                               /* array of file parameters */
494 };
495
496 struct SetupFileList
497 {
498   char *token;
499   char *value;
500
501   struct SetupFileList *next;
502 };
503
504 struct ListNodeInfo
505 {
506   char *source_filename;                        /* primary key for node list */
507   int num_references;
508 };
509
510 struct ArtworkListInfo
511 {
512   int type;                                     /* type of artwork */
513
514   int num_file_list_entries;
515   int num_suffix_list_entries;
516
517   struct FileInfo *file_list;                   /* static artwork file array */
518   struct ConfigInfo *suffix_list;               /* parameter suffixes array */
519   struct SetupFileList *custom_setup_list;      /* additional definitions */
520
521   struct ListNodeInfo **artwork_list;           /* static artwork node array */
522
523   struct ListNode *content_list;                /* dynamic artwork node list */
524
525   void *(*load_artwork)(char *);                /* constructor function */
526   void (*free_artwork)(void *);                 /* destructor function */
527 };
528
529
530 /* ========================================================================= */
531 /* exported variables                                                        */
532 /* ========================================================================= */
533
534 extern struct ProgramInfo       program;
535 extern struct OptionInfo        options;
536 extern struct VideoSystemInfo   video;
537 extern struct AudioSystemInfo   audio;
538 extern struct GfxInfo           gfx;
539 extern struct FontInfo          font;
540 extern struct ArtworkInfo       artwork;
541 extern struct JoystickInfo      joystick;
542 extern struct SetupInfo         setup;
543
544 extern LevelDirTree            *leveldir_first;
545 extern LevelDirTree            *leveldir_current;
546 extern int                      level_nr;
547
548 extern Display                 *display;
549 extern Visual                  *visual;
550 extern int                      screen;
551 extern Colormap                 cmap;
552
553 extern DrawWindow              *window;
554 extern DrawBuffer              *backbuffer;
555 extern DrawBuffer              *drawto;
556
557 extern int                      button_status;
558 extern boolean                  motion_status;
559
560 extern int                      redraw_mask;
561 extern int                      redraw_tiles;
562
563 extern int                      FrameCounter;
564
565
566 /* function definitions */
567
568 void InitCommandName(char *);
569 void InitExitFunction(void (*exit_function)(int));
570 void InitPlatformDependantStuff(void);
571 void ClosePlatformDependantStuff(void);
572
573 void InitProgramInfo(char *, char *, char *, char *, char *, char *, char *,
574                      char *, char *, int);
575
576 void InitGfxFieldInfo(int, int, int, int, int, int, int, int);
577 void InitGfxDoor1Info(int, int, int, int);
578 void InitGfxDoor2Info(int, int, int, int);
579 void InitGfxScrollbufferInfo(int, int);
580 void SetDrawDeactivationMask(int);
581 void SetDrawBackgroundMask(int);
582 void SetMainBackgroundBitmap(Bitmap *);
583 void SetDoorBackgroundBitmap(Bitmap *);
584
585 inline void InitVideoDisplay(void);
586 inline void CloseVideoDisplay(void);
587 inline void InitVideoBuffer(DrawBuffer **,DrawWindow **, int,int,int, boolean);
588 inline Bitmap *CreateBitmapStruct(void);
589 inline Bitmap *CreateBitmap(int, int, int);
590 inline void FreeBitmap(Bitmap *);
591 inline void BlitBitmap(Bitmap *, Bitmap *, int, int, int, int, int, int);
592 inline void ClearRectangle(Bitmap *, int, int, int, int);
593 inline void ClearRectangleOnBackground(Bitmap *, int, int, int, int);
594 inline void SetClipMask(Bitmap *, GC, Pixmap);
595 inline void SetClipOrigin(Bitmap *, GC, int, int);
596 inline void BlitBitmapMasked(Bitmap *, Bitmap *, int, int, int, int, int, int);
597 inline boolean DrawingOnBackground(int, int);
598 inline void BlitBitmapOnBackground(Bitmap *, Bitmap *, int, int, int, int, int,
599                                    int);
600 inline void DrawSimpleWhiteLine(Bitmap *, int, int, int, int);
601 inline void DrawLines(Bitmap *, struct XY *, int, Pixel);
602 inline Pixel GetPixel(Bitmap *, int, int);
603 inline Pixel GetPixelFromRGB(Bitmap *, unsigned int,unsigned int,unsigned int);
604 inline Pixel GetPixelFromRGBcompact(Bitmap *, unsigned int);
605
606 inline void FlushDisplay(void);
607 inline void SyncDisplay(void);
608 inline void KeyboardAutoRepeatOn(void);
609 inline void KeyboardAutoRepeatOff(void);
610 inline boolean PointerInWindow(DrawWindow *);
611 inline boolean SetVideoMode(boolean);
612 inline boolean ChangeVideoModeIfNeeded(boolean);
613
614 Bitmap *LoadImage(char *);
615 Bitmap *LoadCustomImage(char *);
616 void ReloadCustomImage(Bitmap *, char *);
617
618 inline void OpenAudio(void);
619 inline void CloseAudio(void);
620 inline void SetAudioMode(boolean);
621
622 inline void InitEventFilter(EventFilter);
623 inline boolean PendingEvent(void);
624 inline void NextEvent(Event *event);
625 inline Key GetEventKey(KeyEvent *, boolean);
626 inline boolean CheckCloseWindowEvent(ClientMessageEvent *);
627
628 inline void InitJoysticks();
629 inline boolean ReadJoystick(int, int *, int *, boolean *, boolean *);
630
631 #endif /* SYSTEM_H */