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