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