1 /***************************************************************************
2                           stk.h  -  description
3                              -------------------
4     begin                : Thu Oct 12 2002
5     copyright            : (C) 2002 by Michael Speck
6     email                : kulkanie@gmx.net
7  ***************************************************************************/
8 
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17 
18 #ifndef __STK_H
19 #define __STK_H
20 
21 #include <SDL.h>
22 #ifdef AUDIO_ENABLED
23   #include <SDL_mixer.h>
24 #endif
25 
26 /* GENERAL */
27 
28 #define SDL_NONFATAL 0x10000000
29 
30 #define STK_ABORT( msg ) \
31  { fprintf( stderr, "Fatal STK Error!\n%s\n", msg ); exit( 1 ); }
32 
33 #define STK_IN_RECT( rect, mx, my ) \
34  ( mx >= (rect).x && my >= (rect).y && \
35    mx < (rect).x + (rect).w && my < (rect).y + (rect).h )
36 
37 #define STK_OPAQUE SDL_ALPHA_OPAQUE
38 #define STK_TRANSPARENT SDL_ALPHA_TRANSPARENT
39 
40 enum {
41     STK_BUTTON_LEFT = 1,
42     STK_BUTTON_MIDDLE,
43     STK_BUTTON_RIGHT,
44     STK_WHEEL_UP,
45     STK_WHEEL_DOWN,
46     STK_BUTTON_COUNT
47 };
48 
49 /*
50 ====================================================================
51 Initiate SDL, build a default cursor and install the clean
52 up function stk_quit().
53 ====================================================================
54 */
55 void stk_init( int flags );
56 
57 /*
58 ====================================================================
59 Block until either a key or button was pressed. If SDL_QUIT
60 was received this will set stk_quit_request True.
61 ====================================================================
62 */
63 void stk_wait_for_input( void );
64 
65 /* SCREEN */
66 
67 /*
68 ====================================================================
69 Open a display with the passed settings. Depth may be modified due
70 to Xserver settings and if the resolution is completely
71 unavailable 640 x 480 x 16 x SDL_SWSURFACE is opened.
72 If the display is already open it is closed and re-opened.
73 There is no function to close the display as this is handled by the
74 stk_quit() function installed by stk_init().
75 The display can be accessed by
76   extern SDL_Surface *stk_display;
77 This funtion returns True if the wanted width and height are
78 available, False if not and it aborts when no display is found.
79 ====================================================================
80 */
81 int stk_display_open( int flags, int width, int height, int depth );
82 
83 /*
84 ====================================================================
85 Add an update rectangle that will be updated by
86 stk_display_update(). Regardless of clipping the rectangles
87 must fit the screen else it'll raise an X error.
88 If NULL is passed as 'rect' the whole screen is stored for update.
89 ====================================================================
90 */
91 void stk_display_store_rect( SDL_Rect *rect );
92 
93 /*
94 ====================================================================
95 Store the destination rectangle of the last blit operation. SDL
96 modified this rectangle to the actually updated clipping region.
97 ====================================================================
98 */
99 void stk_display_store_drect( void );
100 
101 /*
102 ====================================================================
103 Either update all gathered rects or simply the full screen.
104 In any case the stored regions are cleared.
105 ====================================================================
106 */
107 enum {
108     STK_UPDATE_ALL = 0,
109     STK_UPDATE_RECTS
110 };
111 void stk_display_update( int type );
112 
113 /*
114 ====================================================================
115 Fade the current contents of the display either in or out. 'time'
116 is the time in milliseconds the fading is supposed to take.
117 ====================================================================
118 */
119 enum {
120     STK_FADE_IN = 0,
121     STK_FADE_OUT,
122     STK_FADE_DEFAULT_TIME = 350
123 };
124 void stk_display_fade( int type, int time );
125 
126 /*
127 ====================================================================
128 Take a screenshot and save it to screenshot[index].bmp in the
129 current directory.
130 ====================================================================
131 */
132 void stk_display_take_screenshot();
133 
134 /*
135 ====================================================================
136 Switch fullscreen/windowed for current resolution.
137 ====================================================================
138 */
139 void stk_display_apply_fullscreen( int fullscreen );
140 
141 /* SURFACE */
142 
143 /*
144 ====================================================================
145 If stk_surface_load() is called with a relative path this prefix
146 is added. Default is '.'; 'path' is copied.
147 ====================================================================
148 */
149 void stk_surface_set_path( char *path );
150 
151 /*
152 ====================================================================
153 Load a surface from a path. If it's not an absolute directory
154 (starting with '/') the prefix passed in stk_surface_set_path()
155 is prepended.
156 If a video mode was set the surface will be converted to its
157 format to speed up blitting. As resource either BMP or PNG may
158 be provided.
159 If SDL_NONFATAL is passed a warning is displayed and the function
160 returns NULL else it will exit the program with an error.
161 The default color key of the surface is black and no alpha.
162 ====================================================================
163 */
164 SDL_Surface* stk_surface_load( int flags, char *format, ... );
165 
166 /*
167 ====================================================================
168 Create a surface with the given size and the format of the
169 video mode which must be set.
170 The default color key of the surface is black and no alpha.
171 ====================================================================
172 */
173 SDL_Surface* stk_surface_create( int flags, int width, int height );
174 
175 /*
176 ====================================================================
177 Free the memory of a surface if not NULL and reset the pointer
178 to NULL.
179 ====================================================================
180 */
181 void stk_surface_free( SDL_Surface **surface );
182 
183 /*
184 ====================================================================
185 Blit retangle from 'src' to 'dest' with current alpha of 'src'.
186 If 'sw' is -1 the full source width is used, 'sh' analogue.
187 stk_display_store_drect() can be used to store the update rect
188 of the blitted surface.
189 ====================================================================
190 */
191 void stk_surface_blit(
192     SDL_Surface *src, int sx, int sy, int sw, int sh,
193     SDL_Surface *dest, int dx, int dy );
194 
195 /*
196 ====================================================================
197 Different from stk_surface_blit() this function temporarily
198 overwrites 'src's alpha value. It is not recommended to use
199 this function if performance is important as it is 10 times slower
200 than an SDL_SetAlpha() combined with stk_surface_blit().
201 ====================================================================
202 */
203 void stk_surface_alpha_blit(
204     SDL_Surface *src, int sx, int sy, int sw, int sh,
205     SDL_Surface *dest, int dx, int dy, int alpha );
206 
207 /*
208 ====================================================================
209 Fill a rectangle of the surface with a given color of the format
210 0xRRGGBB. 'dw' == -1 and 'dh' == -1 have the same effect as in
211 stk_surface_blit().
212 ====================================================================
213 */
214 void stk_surface_fill(
215     SDL_Surface *dest, int dx, int dy, int dw, int dh,
216     int color );
217 
218 /*
219 ====================================================================
220 Set the clipping region of a surface. All blit operations into
221 this surface will only have effect within the clipping region.
222 'dw' == -1 and 'dh' == -1 have the same effect as in
223 stk_surface_blit().
224 ====================================================================
225 */
226 void stk_surface_clip(
227     SDL_Surface *dest, int dx, int dy, int dw, int dh );
228 
229 /*
230 ====================================================================
231 Lock/unlock surface for direct access.
232 ====================================================================
233 */
234 #define stk_surface_lock( surface ) \
235  if (SDL_MUSTLOCK((surface))) SDL_LockSurface((surface))
236 #define stk_surface_unlock( surface ) \
237  if (SDL_MUSTLOCK((surface))) SDL_UnlockSurface((surface))
238 
239 /*
240 ====================================================================
241 Get or set a pixel from/to a surface. This time the pixel must
242 already be in SDL format.
243 ====================================================================
244 */
245 void stk_surface_set_pixel(
246     SDL_Surface *dest, int dx, int dy, Uint32 pixel );
247 Uint32 stk_surface_get_pixel( SDL_Surface *src, int sx, int sy );
248 
249 /*
250 ====================================================================
251 Convert part of the surface to gray and if 'dark' is set cut the
252 brightness in half. 'dw' == -1 and 'dh' == -1 have the same
253 effect as in tk_surface_blit().
254 ====================================================================
255 */
256 void stk_surface_gray(
257     SDL_Surface *dest, int dx, int dy, int dw, int dh, int dark );
258 
259 /*
260 ====================================================================
261 Add a 3D frame to surface and dark to contents. The frame border
262 is 'border' thick.
263 ====================================================================
264 */
265 void stk_surface_add_3dframe(
266     SDL_Surface *dest, int dx, int dy, int dw, int dh, int border );
267 
268 /*
269 ====================================================================
270 Fill the surface with the wallpaper (clipped)
271 ====================================================================
272 */
273 void stk_surface_apply_wallpaper(
274     SDL_Surface *dest, int dx, int dy, int dw, int dh,
275     SDL_Surface *wallpaper, int alpha );
276 
277 /*
278 ====================================================================
279 Apply a frame to the surface. The frame resource provides the eight
280 square-like parts for the frame composed as a little frame: corners
281 and sides.
282 'dw' == -1 and 'dh' == -1 have the same effect as in
283 stk_surface_blit().
284 Returns the size of the border.
285 ====================================================================
286 */
287 int stk_surface_apply_frame(
288     SDL_Surface *dest, int dx, int dy, int dw, int dh,
289     SDL_Surface *frame );
290 
291 /* FIXED FONT */
292 
293 enum {
294     STK_FONT_ALIGN_LEFT     = (1L << 1),
295     STK_FONT_ALIGN_RIGHT    = (1L << 2),
296     STK_FONT_ALIGN_TOP      = (1L << 3),
297     STK_FONT_ALIGN_BOTTOM   = (1L << 4),
298     STK_FONT_ALIGN_CENTER_X = (1L << 5),
299     STK_FONT_ALIGN_CENTER_Y = (1L << 6)
300 };
301 typedef struct {
302     SDL_Surface *surface;
303     int         align; /* text aligment */
304     int         width; /* of a single character */
305     int         height; /* of a single character */
306 } StkFont;
307 
308 /*
309 ====================================================================
310 Load a fixed font which is simply a surface containing the ASCII
311 characters from 32 (blank) to 96 (whatever) where all characters
312 have the same width.
313 ====================================================================
314 */
315 StkFont* stk_font_load( int flags, char *format, ... );
316 
317 /*
318 ====================================================================
319 Free a font if not NULL and reset pointer to NULL.
320 ====================================================================
321 */
322 void stk_font_free( StkFont **font );
323 
324 /*
325 ====================================================================
326 Write string to surface. If 'alpha' is -1 the font is displayed
327 with the current alpha value else the new alpha is used and kept.
328 stk_display_store_drect() can be used to store the update rect
329 of the written string.
330 ====================================================================
331 */
332 void stk_font_write(
333     StkFont *font, SDL_Surface *dest, int dx, int dy,
334     int alpha, char *string );
335 
336 /*
337 ====================================================================
338 Query the length of the string in pixels.
339 ====================================================================
340 */
341 #define stk_font_string_width( font, string ) \
342  (strlen(string) * font->width)
343 
344 /* CURSOR */
345 
346 /*
347 ====================================================================
348 Build an SDL cursor with the given hotspot. The resource mask
349 contains 'b' for black 'w' for white and ' ' for transparent.
350 There are two cursors provided:
351   extern SDL_Cursor *empty_cursor  (all transparent)
352   extern SDL_Cursor *std_cursor    (SDL standard cursor)
353 ====================================================================
354 */
355 SDL_Cursor* stk_cursor_create(
356     int width, int height, int hot_x, int hot_y, char *src );
357 
358 /*
359 ====================================================================
360 Free a cursor if not NULL and reset pointer to NULL.
361 ====================================================================
362 */
363 void stk_cursor_free( SDL_Cursor **cursor );
364 
365 /* TIMER */
366 
367 /*
368 ====================================================================
369 Reset the timer.
370 ====================================================================
371 */
372 void stk_timer_reset( void );
373 
374 /*
375 ====================================================================
376 Get the time since last call or reset.
377 ====================================================================
378 */
379 int stk_timer_get_time( void );
380 
381 /* AUDIO */
382 
383 /*
384 ====================================================================
385 Open mixer. If this fails an error message is
386 displayed (program doesn't abort) and all stk_audio and stk_sound
387 functions have no effect.
388   'frequency': 11025, 22050, 44100 ...
389   'format': MIX_DEFAULT_FORMAT is recommended
390   'channels': mono(1) or stereo(2)
391   'chunksize': mix buffer size (1024 Bytes recommended)
392 Passing 0 for an argument means to use the SDL default:
393   22050Hz in format AUDIO_S16LSB stereo with 1024 bytes chunksize
394 stk_audio_open() installs stk_audio_close() as exit function.
395 There are 8 channels available for mixing.
396 ====================================================================
397 */
398 int stk_audio_open(
399     int frequency, Uint16 format, int channels, int chunksize );
400 
401 /*
402 ====================================================================
403 Close mixer if previously opened.
404 ====================================================================
405 */
406 void stk_audio_close( void );
407 
408 /*
409 ====================================================================
410 If stk_sound_load() is called with a relative path this prefix
411 is added. Default is '.'; 'path' is copied.
412 ====================================================================
413 */
414 void stk_audio_set_path( char *path );
415 
416 /*
417 ====================================================================
418 Enable/disable sound. If disabled stk_sound_play () has no effect.
419 ====================================================================
420 */
421 void stk_audio_enable_sound( int enable );
422 
423 /*
424 ====================================================================
425 Set default volume of all sounds: 0 - 128
426 ====================================================================
427 */
428 void stk_audio_set_sound_volume( int volume );
429 
430 /*
431 ====================================================================
432 Fade out a specific sound channel. If 'channel is -1 all
433 channels fade out. 'ms' is the time the fading shall take.
434 ====================================================================
435 */
436 void stk_audio_fade_out( int channel, int ms );
437 
438 /* SOUND */
439 
440 typedef struct {
441 #ifdef AUDIO_ENABLED
442     Mix_Chunk *chunk; /* SDL_Mixer's sound buffer */
443 #endif
444     int       channel;
445     int       volume;
446 } StkSound;
447 
448 /*
449 ====================================================================
450 Load a sound from a path. If it's not an absolute directory
451 (starting with '/') the prefix passed in stk_sound_set_path()
452 is prepended. Loading sounds is non-fatal thus if a sound
453 cannot be found it is created but empty. In this case
454 a warning is displayed.
455 The volume is set to the default set by
456 stk_audio_set_sound_volume(). Sounds with an equal 'channel'
457 will share it so if a new sound is played the old one breaks up
458 thus channel should be used to group sounds.
459 If channel is -1 the first available channel is used
460 to play sound.
461 ====================================================================
462 */
463 StkSound *stk_sound_load( int channel, char *format, ... );
464 
465 /*
466 ====================================================================
467 Free the memory of a sound if not NULL and reset the pointer
468 to NULL.
469 ====================================================================
470 */
471 void stk_sound_free( StkSound **sound );
472 
473 /*
474 ====================================================================
475 Set channel/volume of sound.
476 ====================================================================
477 */
478 void stk_sound_set_volume( StkSound *sound, int volume );
479 void stk_sound_set_channel( StkSound *sound, int channel );
480 
481 /*
482 ====================================================================
483 Play a sound.
484 ====================================================================
485 */
486 void stk_sound_play( StkSound *sound );
487 /*
488 ====================================================================
489 Play a sound at horizontal position x..
490 ====================================================================
491 */
492 void stk_sound_play_x( int x, StkSound *sound );
493 
494 /*
495 ====================================================================
496 Fade out the channel of this sound.
497 ====================================================================
498 */
499 void stk_sound_fade_out( StkSound *sound, int ms );
500 
501 
502 #endif
503