68a540b3e9b293c259874e2b018498f55e7895ba
[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 name for new levels */
135 #define NAMELESS_LEVEL_NAME     "nameless level"
136
137 /* definitions for game sub-directories */
138 #ifndef RO_GAME_DIR
139 #define RO_GAME_DIR             "."
140 #endif
141
142 #ifndef RW_GAME_DIR
143 #define RW_GAME_DIR             "."
144 #endif
145
146 #define RO_BASE_PATH            RO_GAME_DIR
147 #define RW_BASE_PATH            RW_GAME_DIR
148
149 #define GRAPHICS_DIRECTORY      "graphics"
150 #define SOUNDS_DIRECTORY        "sounds"
151 #define MUSIC_DIRECTORY         "music"
152 #define LEVELS_DIRECTORY        "levels"
153 #define TAPES_DIRECTORY         "tapes"
154 #define SCORES_DIRECTORY        "scores"
155
156 #if !defined(PLATFORM_MSDOS)
157 #define GRAPHICS_SUBDIR         "gfx_classic"
158 #define SOUNDS_SUBDIR           "snd_classic"
159 #define MUSIC_SUBDIR            "mus_classic"
160 #else
161 #define GRAPHICS_SUBDIR         "gfx_orig"
162 #define SOUNDS_SUBDIR           "snd_orig"
163 #define MUSIC_SUBDIR            "mus_orig"
164 #endif
165
166 /* areas in bitmap PIX_DOOR */
167 /* meaning in PIX_DB_DOOR: (3 PAGEs)
168    PAGEX1: 1. buffer for DOOR_1
169    PAGEX2: 2. buffer for DOOR_1
170    PAGEX3: buffer for animations
171 */
172
173 #define DOOR_GFX_PAGESIZE       (gfx.dxsize)
174 #define DOOR_GFX_PAGEX1         (0 * DOOR_GFX_PAGESIZE)
175 #define DOOR_GFX_PAGEX2         (1 * DOOR_GFX_PAGESIZE)
176 #define DOOR_GFX_PAGEX3         (2 * DOOR_GFX_PAGESIZE)
177 #define DOOR_GFX_PAGEX4         (3 * DOOR_GFX_PAGESIZE)
178 #define DOOR_GFX_PAGEX5         (4 * DOOR_GFX_PAGESIZE)
179 #define DOOR_GFX_PAGEX6         (5 * DOOR_GFX_PAGESIZE)
180 #define DOOR_GFX_PAGEX7         (6 * DOOR_GFX_PAGESIZE)
181 #define DOOR_GFX_PAGEX8         (7 * DOOR_GFX_PAGESIZE)
182 #define DOOR_GFX_PAGEY1         (0)
183 #define DOOR_GFX_PAGEY2         (gfx.dysize)
184
185 /* functions for version handling */
186 #define VERSION_IDENT(x,y,z)    ((x) * 10000 + (y) * 100 + (z))
187 #define VERSION_MAJOR(x)        ((x) / 10000)
188 #define VERSION_MINOR(x)        (((x) % 10000) / 100)
189 #define VERSION_PATCH(x)        ((x) % 100)
190
191 /* functions for parent/child process identification */
192 #define IS_PARENT_PROCESS(pid)  ((pid) > 0)
193 #define IS_CHILD_PROCESS(pid)   ((pid) == 0)
194
195
196 /* type definitions */
197 typedef int (*EventFilter)(const Event *);
198
199
200 /* structure definitions */
201
202 struct ProgramInfo
203 {
204   char *command_basename;
205   char *userdata_directory;
206
207   char *program_title;
208   char *window_title;
209   char *icon_title;
210
211   char *x11_icon_filename;
212   char *x11_iconmask_filename;
213   char *msdos_pointer_filename;
214
215   char *cookie_prefix;
216   char *filename_prefix;        /* prefix to cut off from DOS filenames */
217
218   int version_major;
219   int version_minor;
220   int version_patch;
221
222   void (*exit_function)(int);
223 };
224
225 struct OptionInfo
226 {
227   char *display_name;
228   char *server_host;
229   int server_port;
230   char *ro_base_directory;
231   char *rw_base_directory;
232   char *level_directory;
233   char *graphics_directory;
234   char *sounds_directory;
235   char *music_directory;
236   boolean serveronly;
237   boolean network;
238   boolean verbose;
239   boolean debug;
240   char *debug_command;
241 };
242
243 struct VideoSystemInfo
244 {
245   int default_depth;
246   int width, height, depth;
247   boolean fullscreen_available;
248   boolean fullscreen_enabled;
249 };
250
251 struct AudioSystemInfo
252 {
253   boolean sound_available;
254   boolean loops_available;
255   boolean music_available;
256
257   boolean sound_enabled;
258   boolean sound_deactivated;    /* for temporarily disabling sound */
259
260   int mixer_pipe[2];
261   int mixer_pid;
262   char *device_name;
263   int device_fd;
264
265   int num_channels;
266   int music_channel;
267   int first_sound_channel;
268 };
269
270 struct GfxInfo
271 {
272   int sx, sy;
273   int sxsize, sysize;
274   int real_sx, real_sy;
275   int full_sxsize, full_sysize;
276   int scrollbuffer_width, scrollbuffer_height;
277
278   int dx, dy;
279   int dxsize, dysize;
280
281   int vx, vy;
282   int vxsize, vysize;
283
284   boolean draw_deactivation_mask;
285 };
286
287 struct JoystickInfo
288 {
289   int status;
290   int fd[MAX_PLAYERS];          /* file descriptor of player's joystick */
291 };
292
293 struct SetupJoystickInfo
294 {
295   char *device_name;            /* device name of player's joystick */
296
297   int xleft, xmiddle, xright;
298   int yupper, ymiddle, ylower;
299   int snap;
300   int bomb;
301 };
302
303 struct SetupKeyboardInfo
304 {
305   Key left;
306   Key right;
307   Key up;
308   Key down;
309   Key snap;
310   Key bomb;
311 };
312
313 struct SetupInputInfo
314 {
315   boolean use_joystick;
316   struct SetupJoystickInfo joy;
317   struct SetupKeyboardInfo key;
318 };
319
320 struct SetupShortcutInfo
321 {
322   Key save_game;
323   Key load_game;
324   Key toggle_pause;
325 };
326
327 struct SetupInfo
328 {
329   char *player_name;
330
331   boolean sound;
332   boolean sound_loops;
333   boolean sound_music;
334   boolean sound_simple;
335   boolean toons;
336   boolean double_buffering;
337   boolean direct_draw;          /* !double_buffering (redundant!) */
338   boolean scroll_delay;
339   boolean soft_scrolling;
340   boolean fading;
341   boolean autorecord;
342   boolean quick_doors;
343   boolean team_mode;
344   boolean handicap;
345   boolean time_limit;
346   boolean fullscreen;
347   boolean ask_on_escape;
348
349   char *graphics_set;
350   char *sounds_set;
351   char *music_set;
352   boolean override_level_graphics;
353   boolean override_level_sounds;
354   boolean override_level_music;
355
356   struct SetupShortcutInfo shortcut;
357   struct SetupInputInfo input[MAX_PLAYERS];
358 };
359
360 #define TREE_TYPE_GENERIC               0
361 #define TREE_TYPE_LEVEL_DIR             1
362 #define TREE_TYPE_GRAPHICS_DIR          2
363 #define TREE_TYPE_SOUNDS_DIR            3
364 #define TREE_TYPE_MUSIC_DIR             4
365
366 struct TreeInfo
367 {
368   struct TreeInfo **node_top;           /* topmost node in tree */
369   struct TreeInfo *node_parent;         /* parent level directory info */
370   struct TreeInfo *node_group;          /* level group sub-directory info */
371   struct TreeInfo *next;                /* next level series structure node */
372
373   int cl_first;         /* internal control field for setup screen */
374   int cl_cursor;        /* internal control field for setup screen */
375
376   int type;             /* type of tree content */
377
378   /* fields for "type == TREE_TYPE_LEVEL_DIR" */
379
380   char *filename;       /* tree info sub-directory basename (may be ".") */
381   char *fullpath;       /* complete path relative to tree base directory */
382   char *basepath;       /* absolute base path of tree base directory */
383   char *identifier;     /* identifier string for configuration files */
384   char *name;           /* tree info name, as displayed in selection menues */
385   char *name_sorting;   /* optional sorting name for correct name sorting */
386   char *author;         /* level or artwork author name */
387   char *imported_from;  /* optional comment for imported levels or artwork */
388
389   int levels;           /* number of levels in level series */
390   int first_level;      /* first level number (to allow start with 0 or 1) */
391   int last_level;       /* last level number (automatically calculated) */
392   int sort_priority;    /* sort levels by 'sort_priority' and then by name */
393
394   boolean level_group;  /* directory contains more level series directories */
395   boolean parent_link;  /* entry links back to parent directory */
396   boolean user_defined; /* user defined levels are stored in home directory */
397   boolean readonly;     /* readonly levels can not be changed with editor */
398
399   int color;            /* color to use on selection screen for this level */
400   char *class_desc;     /* description of level series class */
401   int handicap_level;   /* number of the lowest unsolved level */
402 };
403
404 typedef struct TreeInfo TreeInfo;
405 typedef struct TreeInfo LevelDirTree;
406 typedef struct TreeInfo ArtworkDirTree;
407 typedef struct TreeInfo GraphicsDirTree;
408 typedef struct TreeInfo SoundsDirTree;
409 typedef struct TreeInfo MusicDirTree;
410
411 struct ArtworkInfo
412 {
413   GraphicsDirTree *gfx_first;
414   GraphicsDirTree *gfx_current;
415   SoundsDirTree *snd_first;
416   SoundsDirTree *snd_current;
417   MusicDirTree *mus_first;
418   MusicDirTree *mus_current;
419
420   char *gfx_current_identifier;
421   char *snd_current_identifier;
422   char *mus_current_identifier;
423 };
424
425
426 /* ========================================================================= */
427 /* exported variables                                                        */
428 /* ========================================================================= */
429
430 extern struct ProgramInfo       program;
431 extern struct OptionInfo        options;
432 extern struct VideoSystemInfo   video;
433 extern struct AudioSystemInfo   audio;
434 extern struct GfxInfo           gfx;
435 extern struct ArtworkInfo       artwork;
436 extern struct JoystickInfo      joystick;
437 extern struct SetupInfo         setup;
438
439 extern LevelDirTree            *leveldir_first;
440 extern LevelDirTree            *leveldir_current;
441 extern int                      level_nr;
442
443 extern Display                 *display;
444 extern Visual                  *visual;
445 extern int                      screen;
446 extern Colormap                 cmap;
447
448 extern DrawWindow              *window;
449 extern DrawBuffer              *backbuffer;
450 extern DrawBuffer              *drawto;
451
452 extern int                      button_status;
453 extern boolean                  motion_status;
454
455 extern int                      redraw_mask;
456 extern int                      redraw_tiles;
457
458 extern int                      FrameCounter;
459
460
461 /* function definitions */
462
463 void InitCommandName(char *);
464 void InitExitFunction(void (*exit_function)(int));
465 void InitPlatformDependantStuff(void);
466 void ClosePlatformDependantStuff(void);
467
468 void InitProgramInfo(char *, char *, char *, char *, char *, char *, char *,
469                      char *, char *, int);
470
471 void InitGfxFieldInfo(int, int, int, int, int, int, int, int);
472 void InitGfxDoor1Info(int, int, int, int);
473 void InitGfxDoor2Info(int, int, int, int);
474 void InitGfxScrollbufferInfo(int, int);
475 void SetDrawDeactivationMask(int );
476
477 inline void InitVideoDisplay(void);
478 inline void CloseVideoDisplay(void);
479 inline void InitVideoBuffer(DrawBuffer **,DrawWindow **, int,int,int, boolean);
480 inline Bitmap *CreateBitmapStruct(void);
481 inline Bitmap *CreateBitmap(int, int, int);
482 inline void FreeBitmap(Bitmap *);
483 inline void BlitBitmap(Bitmap *, Bitmap *, int, int, int, int, int, int);
484 inline void ClearRectangle(Bitmap *, int, int, int, int);
485 inline void SetClipMask(Bitmap *, GC, Pixmap);
486 inline void SetClipOrigin(Bitmap *, GC, int, int);
487 inline void BlitBitmapMasked(Bitmap *, Bitmap *, int, int, int, int, int, int);
488 inline void DrawSimpleWhiteLine(Bitmap *, int, int, int, int);
489 inline void DrawLines(Bitmap *, struct XY *, int, Pixel);
490 inline Pixel GetPixel(Bitmap *, int, int);
491 inline Pixel GetPixelFromRGB(Bitmap *, unsigned int,unsigned int,unsigned int);
492 inline Pixel GetPixelFromRGBcompact(Bitmap *, unsigned int);
493
494 inline void FlushDisplay(void);
495 inline void SyncDisplay(void);
496 inline void KeyboardAutoRepeatOn(void);
497 inline void KeyboardAutoRepeatOff(void);
498 inline boolean PointerInWindow(DrawWindow *);
499 inline boolean SetVideoMode(boolean);
500 inline boolean ChangeVideoModeIfNeeded(boolean);
501
502 Bitmap *LoadImage(char *);
503 Bitmap *LoadCustomImage(char *);
504 void ReloadCustomImage(Bitmap *, char *);
505
506 inline void OpenAudio(void);
507 inline void CloseAudio(void);
508 inline void SetAudioMode(boolean);
509
510 inline void InitEventFilter(EventFilter);
511 inline boolean PendingEvent(void);
512 inline void NextEvent(Event *event);
513 inline Key GetEventKey(KeyEvent *, boolean);
514 inline boolean CheckCloseWindowEvent(ClientMessageEvent *);
515
516 inline void InitJoysticks();
517 inline boolean ReadJoystick(int, int *, int *, boolean *, boolean *);
518
519 #endif /* SYSTEM_H */