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