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