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