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