b932605be9231608c91c29988182081c232b1645
[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_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 name for new levels */
142 #define NAMELESS_LEVEL_NAME     "nameless level"
143
144 /* default text for non-existant artwork */
145 #define NOT_AVAILABLE           "(not available)"
146
147 /* default value for undefined filename */
148 #define UNDEFINED_FILENAME      "[NONE]"
149
150 /* default values for undefined configuration file parameters */
151 #define ARG_UNDEFINED           "-1000000"
152 #define ARG_UNDEFINED_VALUE     (atoi(ARG_UNDEFINED))
153
154 /* definitions for game sub-directories */
155 #ifndef RO_GAME_DIR
156 #define RO_GAME_DIR             "."
157 #endif
158
159 #ifndef RW_GAME_DIR
160 #define RW_GAME_DIR             "."
161 #endif
162
163 #define RO_BASE_PATH            RO_GAME_DIR
164 #define RW_BASE_PATH            RW_GAME_DIR
165
166 #define GRAPHICS_DIRECTORY      "graphics"
167 #define SOUNDS_DIRECTORY        "sounds"
168 #define MUSIC_DIRECTORY         "music"
169 #define LEVELS_DIRECTORY        "levels"
170 #define TAPES_DIRECTORY         "tapes"
171 #define SCORES_DIRECTORY        "scores"
172
173 #if !defined(PLATFORM_MSDOS)
174 #define GRAPHICS_SUBDIR         "gfx_classic"
175 #define SOUNDS_SUBDIR           "snd_classic"
176 #define MUSIC_SUBDIR            "mus_classic"
177 #else
178 #define GRAPHICS_SUBDIR         "gfx_orig"
179 #define SOUNDS_SUBDIR           "snd_orig"
180 #define MUSIC_SUBDIR            "mus_orig"
181 #endif
182
183 /* areas in bitmap PIX_DOOR */
184 /* meaning in PIX_DB_DOOR: (3 PAGEs)
185    PAGEX1: 1. buffer for DOOR_1
186    PAGEX2: 2. buffer for DOOR_1
187    PAGEX3: buffer for animations
188 */
189
190 #define DOOR_GFX_PAGESIZE       (gfx.dxsize)
191 #define DOOR_GFX_PAGEX1         (0 * DOOR_GFX_PAGESIZE)
192 #define DOOR_GFX_PAGEX2         (1 * DOOR_GFX_PAGESIZE)
193 #define DOOR_GFX_PAGEX3         (2 * DOOR_GFX_PAGESIZE)
194 #define DOOR_GFX_PAGEX4         (3 * DOOR_GFX_PAGESIZE)
195 #define DOOR_GFX_PAGEX5         (4 * DOOR_GFX_PAGESIZE)
196 #define DOOR_GFX_PAGEX6         (5 * DOOR_GFX_PAGESIZE)
197 #define DOOR_GFX_PAGEX7         (6 * DOOR_GFX_PAGESIZE)
198 #define DOOR_GFX_PAGEX8         (7 * DOOR_GFX_PAGESIZE)
199 #define DOOR_GFX_PAGEY1         (0)
200 #define DOOR_GFX_PAGEY2         (gfx.dysize)
201
202 /* functions for version handling */
203 #define VERSION_IDENT(x,y,z)    ((x) * 10000 + (y) * 100 + (z))
204 #define VERSION_MAJOR(x)        ((x) / 10000)
205 #define VERSION_MINOR(x)        (((x) % 10000) / 100)
206 #define VERSION_PATCH(x)        ((x) % 100)
207
208 /* functions for parent/child process identification */
209 #define IS_PARENT_PROCESS(pid)  ((pid) > 0 || (pid) == -1)
210 #define IS_CHILD_PROCESS(pid)   ((pid) == 0)
211
212
213 /* type definitions */
214 typedef int (*EventFilter)(const Event *);
215
216
217 /* structure definitions */
218
219 struct ProgramInfo
220 {
221   char *command_basename;
222   char *userdata_directory;
223
224   char *program_title;
225   char *window_title;
226   char *icon_title;
227
228   char *x11_icon_filename;
229   char *x11_iconmask_filename;
230   char *msdos_pointer_filename;
231
232   char *cookie_prefix;
233   char *filename_prefix;        /* prefix to cut off from DOS filenames */
234
235   int version_major;
236   int version_minor;
237   int version_patch;
238
239   void (*exit_function)(int);
240 };
241
242 struct OptionInfo
243 {
244   char *display_name;
245   char *server_host;
246   int server_port;
247
248   char *ro_base_directory;
249   char *rw_base_directory;
250   char *level_directory;
251   char *graphics_directory;
252   char *sounds_directory;
253   char *music_directory;
254   char *execute_command;
255
256   boolean serveronly;
257   boolean network;
258   boolean verbose;
259   boolean debug;
260 };
261
262 struct VideoSystemInfo
263 {
264   int default_depth;
265   int width, height, depth;
266   boolean fullscreen_available;
267   boolean fullscreen_enabled;
268 };
269
270 struct AudioSystemInfo
271 {
272   boolean sound_available;
273   boolean loops_available;
274   boolean music_available;
275
276   boolean sound_enabled;
277   boolean sound_deactivated;    /* for temporarily disabling sound */
278
279   int mixer_pipe[2];
280   int mixer_pid;
281   char *device_name;
282   int device_fd;
283
284   int num_channels;
285   int music_channel;
286   int first_sound_channel;
287 };
288
289 struct GfxInfo
290 {
291   int sx, sy;
292   int sxsize, sysize;
293   int real_sx, real_sy;
294   int full_sxsize, full_sysize;
295   int scrollbuffer_width, scrollbuffer_height;
296
297   int dx, dy;
298   int dxsize, dysize;
299
300   int vx, vy;
301   int vxsize, vysize;
302
303   int draw_deactivation_mask;
304   int draw_background_mask;
305
306   Bitmap *background_bitmap;
307   int background_bitmap_mask;
308 };
309
310 struct FontInfo
311 {
312   Bitmap *bitmap_initial;
313   Bitmap *bitmap_big;
314   Bitmap *bitmap_medium;
315   Bitmap *bitmap_small;
316   Bitmap *bitmap_tile;
317 };
318
319 struct JoystickInfo
320 {
321   int status;
322   int fd[MAX_PLAYERS];          /* file descriptor of player's joystick */
323 };
324
325 struct SetupJoystickInfo
326 {
327   char *device_name;            /* device name of player's joystick */
328
329   int xleft, xmiddle, xright;
330   int yupper, ymiddle, ylower;
331   int snap;
332   int bomb;
333 };
334
335 struct SetupKeyboardInfo
336 {
337   Key left;
338   Key right;
339   Key up;
340   Key down;
341   Key snap;
342   Key bomb;
343 };
344
345 struct SetupInputInfo
346 {
347   boolean use_joystick;
348   struct SetupJoystickInfo joy;
349   struct SetupKeyboardInfo key;
350 };
351
352 struct SetupEditorInfo
353 {
354   boolean el_boulderdash;
355   boolean el_emerald_mine;
356   boolean el_more;
357   boolean el_sokoban;
358   boolean el_supaplex;
359   boolean el_diamond_caves;
360   boolean el_dx_boulderdash;
361   boolean el_chars;
362   boolean el_custom;
363 };
364
365 struct SetupShortcutInfo
366 {
367   Key save_game;
368   Key load_game;
369   Key toggle_pause;
370 };
371
372 struct SetupInfo
373 {
374   char *player_name;
375
376   boolean sound;
377   boolean sound_loops;
378   boolean sound_music;
379   boolean sound_simple;
380   boolean toons;
381   boolean double_buffering;
382   boolean direct_draw;          /* !double_buffering (redundant!) */
383   boolean scroll_delay;
384   boolean soft_scrolling;
385   boolean fading;
386   boolean autorecord;
387   boolean quick_doors;
388   boolean team_mode;
389   boolean handicap;
390   boolean time_limit;
391   boolean fullscreen;
392   boolean ask_on_escape;
393
394   char *graphics_set;
395   char *sounds_set;
396   char *music_set;
397   boolean override_level_graphics;
398   boolean override_level_sounds;
399   boolean override_level_music;
400
401   struct SetupEditorInfo editor;
402   struct SetupShortcutInfo shortcut;
403   struct SetupInputInfo input[MAX_PLAYERS];
404 };
405
406 #define TREE_TYPE_GENERIC               0
407 #define TREE_TYPE_GRAPHICS_DIR          1
408 #define TREE_TYPE_SOUNDS_DIR            2
409 #define TREE_TYPE_MUSIC_DIR             3
410 #define TREE_TYPE_LEVEL_DIR             4
411
412 #define ARTWORK_TYPE_GRAPHICS           TREE_TYPE_GRAPHICS_DIR
413 #define ARTWORK_TYPE_SOUNDS             TREE_TYPE_SOUNDS_DIR
414 #define ARTWORK_TYPE_MUSIC              TREE_TYPE_MUSIC_DIR
415
416 struct TreeInfo
417 {
418   struct TreeInfo **node_top;           /* topmost node in tree */
419   struct TreeInfo *node_parent;         /* parent level directory info */
420   struct TreeInfo *node_group;          /* level group sub-directory info */
421   struct TreeInfo *next;                /* next level series structure node */
422
423   int cl_first;         /* internal control field for setup screen */
424   int cl_cursor;        /* internal control field for setup screen */
425
426   int type;             /* type of tree content */
427
428   /* fields for "type == TREE_TYPE_LEVEL_DIR" */
429
430   char *filename;       /* tree info sub-directory basename (may be ".") */
431   char *fullpath;       /* complete path relative to tree base directory */
432   char *basepath;       /* absolute base path of tree base directory */
433   char *identifier;     /* identifier string for configuration files */
434   char *name;           /* tree info name, as displayed in selection menues */
435   char *name_sorting;   /* optional sorting name for correct name sorting */
436   char *author;         /* level or artwork author name */
437   char *imported_from;  /* optional comment for imported levels or artwork */
438
439   char *graphics_set;   /* optional custom graphics set (level tree only) */
440   char *sounds_set;     /* optional custom sounds set (level tree only) */
441   char *music_set;      /* optional custom music set (level tree only) */
442   char *graphics_path;  /* path to optional custom graphics set (level only) */
443   char *sounds_path;    /* path to optional custom sounds set (level only) */
444   char *music_path;     /* path to optional custom music set (level only) */
445
446   int levels;           /* number of levels in level series */
447   int first_level;      /* first level number (to allow start with 0 or 1) */
448   int last_level;       /* last level number (automatically calculated) */
449   int sort_priority;    /* sort levels by 'sort_priority' and then by name */
450
451   boolean level_group;  /* directory contains more level series directories */
452   boolean parent_link;  /* entry links back to parent directory */
453   boolean user_defined; /* user defined levels are stored in home directory */
454   boolean readonly;     /* readonly levels can not be changed with editor */
455
456   int color;            /* color to use on selection screen for this level */
457   char *class_desc;     /* description of level series class */
458   int handicap_level;   /* number of the lowest unsolved level */
459 };
460
461 typedef struct TreeInfo TreeInfo;
462 typedef struct TreeInfo LevelDirTree;
463 typedef struct TreeInfo ArtworkDirTree;
464 typedef struct TreeInfo GraphicsDirTree;
465 typedef struct TreeInfo SoundsDirTree;
466 typedef struct TreeInfo MusicDirTree;
467
468 struct ArtworkInfo
469 {
470   GraphicsDirTree *gfx_first;
471   GraphicsDirTree *gfx_current;
472   SoundsDirTree *snd_first;
473   SoundsDirTree *snd_current;
474   MusicDirTree *mus_first;
475   MusicDirTree *mus_current;
476
477   char *gfx_current_identifier;
478   char *snd_current_identifier;
479   char *mus_current_identifier;
480 };
481
482 struct ConfigInfo
483 {
484   char *token;
485   char *value;
486   int type;
487 };
488
489 struct FileInfo
490 {
491   char *token;
492
493   char *default_filename;
494   char *filename;
495
496   int *default_parameter;                       /* array of file parameters */
497   int *parameter;                               /* array of file parameters */
498 };
499
500 struct SetupFileList
501 {
502   char *token;
503   char *value;
504
505   struct SetupFileList *next;
506 };
507
508 struct ListNodeInfo
509 {
510   char *source_filename;                        /* primary key for node list */
511   int num_references;
512 };
513
514 struct ArtworkListInfo
515 {
516   int type;                                     /* type of artwork */
517
518   int num_file_list_entries;
519   int num_dynamic_file_list_entries;
520
521   int num_suffix_list_entries;
522
523   struct FileInfo *file_list;                   /* static artwork file array */
524   struct FileInfo *dynamic_file_list;           /* dynamic artwrk file array */
525
526   struct ConfigInfo *suffix_list;               /* parameter suffixes array */
527
528   int num_base_prefixes;
529   int num_ext1_suffixes;
530   int num_ext2_suffixes;
531
532   char **base_prefixes;                         /* base token prefixes array */
533   char **ext1_suffixes;                         /* property suffixes array 1 */
534   char **ext2_suffixes;                         /* property suffixes array 2 */
535
536   int sizeof_artwork_list_entry;
537
538   struct ListNodeInfo **artwork_list;           /* static artwork node array */
539   struct ListNodeInfo **dynamic_artwork_list;   /* dynamic artwrk node array */
540   struct ListNode *content_list;                /* dynamic artwork node list */
541
542   void *(*load_artwork)(char *);                /* constructor function */
543   void (*free_artwork)(void *);                 /* destructor function */
544 };
545
546
547 /* ========================================================================= */
548 /* exported variables                                                        */
549 /* ========================================================================= */
550
551 extern struct ProgramInfo       program;
552 extern struct OptionInfo        options;
553 extern struct VideoSystemInfo   video;
554 extern struct AudioSystemInfo   audio;
555 extern struct GfxInfo           gfx;
556 extern struct FontInfo          font;
557 extern struct ArtworkInfo       artwork;
558 extern struct JoystickInfo      joystick;
559 extern struct SetupInfo         setup;
560
561 extern LevelDirTree            *leveldir_first;
562 extern LevelDirTree            *leveldir_current;
563 extern int                      level_nr;
564
565 extern Display                 *display;
566 extern Visual                  *visual;
567 extern int                      screen;
568 extern Colormap                 cmap;
569
570 extern DrawWindow              *window;
571 extern DrawBuffer              *backbuffer;
572 extern DrawBuffer              *drawto;
573
574 extern int                      button_status;
575 extern boolean                  motion_status;
576
577 extern int                      redraw_mask;
578 extern int                      redraw_tiles;
579
580 extern int                      FrameCounter;
581
582
583 /* function definitions */
584
585 void InitCommandName(char *);
586 void InitExitFunction(void (*exit_function)(int));
587 void InitPlatformDependantStuff(void);
588 void ClosePlatformDependantStuff(void);
589
590 void InitProgramInfo(char *, char *, char *, char *, char *, char *, char *,
591                      char *, char *, int);
592
593 void InitGfxFieldInfo(int, int, int, int, int, int, int, int);
594 void InitGfxDoor1Info(int, int, int, int);
595 void InitGfxDoor2Info(int, int, int, int);
596 void InitGfxScrollbufferInfo(int, int);
597 void SetDrawDeactivationMask(int);
598 void SetDrawBackgroundMask(int);
599 void SetMainBackgroundBitmap(Bitmap *);
600 void SetDoorBackgroundBitmap(Bitmap *);
601
602 inline void InitVideoDisplay(void);
603 inline void CloseVideoDisplay(void);
604 inline void InitVideoBuffer(DrawBuffer **,DrawWindow **, int,int,int, boolean);
605 inline Bitmap *CreateBitmapStruct(void);
606 inline Bitmap *CreateBitmap(int, int, int);
607 inline void FreeBitmap(Bitmap *);
608 inline void BlitBitmap(Bitmap *, Bitmap *, int, int, int, int, int, int);
609 inline void ClearRectangle(Bitmap *, int, int, int, int);
610 inline void ClearRectangleOnBackground(Bitmap *, int, int, int, int);
611 inline void SetClipMask(Bitmap *, GC, Pixmap);
612 inline void SetClipOrigin(Bitmap *, GC, int, int);
613 inline void BlitBitmapMasked(Bitmap *, Bitmap *, int, int, int, int, int, int);
614 inline boolean DrawingOnBackground(int, int);
615 inline void BlitBitmapOnBackground(Bitmap *, Bitmap *, int, int, int, int, int,
616                                    int);
617 inline void DrawSimpleWhiteLine(Bitmap *, int, int, int, int);
618 inline void DrawLines(Bitmap *, struct XY *, int, Pixel);
619 inline Pixel GetPixel(Bitmap *, int, int);
620 inline Pixel GetPixelFromRGB(Bitmap *, unsigned int,unsigned int,unsigned int);
621 inline Pixel GetPixelFromRGBcompact(Bitmap *, unsigned int);
622
623 inline void FlushDisplay(void);
624 inline void SyncDisplay(void);
625 inline void KeyboardAutoRepeatOn(void);
626 inline void KeyboardAutoRepeatOff(void);
627 inline boolean PointerInWindow(DrawWindow *);
628 inline boolean SetVideoMode(boolean);
629 inline boolean ChangeVideoModeIfNeeded(boolean);
630
631 Bitmap *LoadImage(char *);
632 Bitmap *LoadCustomImage(char *);
633 void ReloadCustomImage(Bitmap *, char *);
634
635 inline void OpenAudio(void);
636 inline void CloseAudio(void);
637 inline void SetAudioMode(boolean);
638
639 inline void InitEventFilter(EventFilter);
640 inline boolean PendingEvent(void);
641 inline void NextEvent(Event *event);
642 inline Key GetEventKey(KeyEvent *, boolean);
643 inline boolean CheckCloseWindowEvent(ClientMessageEvent *);
644
645 inline void InitJoysticks();
646 inline boolean ReadJoystick(int, int *, int *, boolean *, boolean *);
647
648 #endif /* SYSTEM_H */