Tux4Kids-Common
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
t4k_common.h
Go to the documentation of this file.
1 //==============================================================================
2 //
3 // t4k_common.h: Contains public headers for the t4k_common library
4 //
5 // Copyright 2009, 2010.
6 // Author: Brendan Luchen
7 // Project email: <tuxmath-devel@lists.sourceforge.net>
8 // Project website: http://tux4kids.alioth.debian.org
9 //
10 // t4k_common.h is part of the t4k_common library.
11 //
12 // t4k_common is free software: you can redistribute it and/or modify
13 // it under the terms of the GNU General Public License as published by
14 // the Free Software Foundation; either version 3 of the License, or
15 // (at your option) any later version.
16 //
17 // t4k_common is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU General Public License
23 // along with this program. If not, see <http://www.gnu.org/licenses/>.
24 //
25 //==============================================================================
26 
27 //==============================================================================
28 //
51 
52 #ifndef TUX4KIDS_COMMON_H
53 #define TUX4KIDS_COMMON_H
54 
55 
56 #include <sys/types.h>
57 #include <sys/stat.h>
58 #include <fcntl.h>
59 #include <unistd.h>
60 #include <dirent.h>
61 #include <wchar.h>
62 #include "SDL.h"
63 #include "SDL_image.h"
64 #include "SDL_mixer.h"
65 
66 //==============================================================================
67 // Debugging macros
68 //
69 // All macros take a debug mask as their first argument, which should roughly
70 // indicate what part of the program the debugging info is related to, and
71 // which can be one of the flags defined below, a program-specific flag, or
72 // an OR'd combination.
73 //
74 // DEBUGVAR prints out the name and value of a string variable
75 #ifndef DEBUGMSG
76 
78 #define DEBUGVAR(mask, Expr) \
79  if((mask) & (debug_status)) \
80  { \
81  fprintf(stderr, #Expr ": %s\n", (Expr)); fflush(stderr); \
82  }
83 
85 #define DEBUGVARX(mask, Expr) \
86  if((mask) & (debug_status)) \
87  { \
88  fprintf(stderr, #Expr ": %x\n", (Expr)); fflush(stderr); \
89  }
90 
92 #define DEBUGVARF(mask, Expr) \
93  if((mask) & (debug_status)) \
94  { \
95  fprintf(stderr, #Expr ": %f\n", (Expr)); fflush(stderr); \
96  }
97 
99 #define DEBUGCODE(mask) if((mask) & debug_status)
100 
102 #define DEBUGMSG(mask, ...) \
103  if((mask) & debug_status) \
104  { \
105  fprintf(stderr, __VA_ARGS__); fflush(stderr); \
106  }
107 #endif
108 
109 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
110  #define rmask 0xff000000
111  #define gmask 0x00ff0000
112  #define bmask 0x0000ff00
113  #define amask 0x000000ff
114 #else
115  #define rmask 0x000000ff
116  #define gmask 0x0000ff00
117  #define bmask 0x00ff0000
118  #define amask 0xff000000
119 #endif
120 
121 // Hold off on gettext until we decide if we are really going to
122 // use it from within t4kcommon - DSB
123 //#define _(String) String
124 // #define _(String) gettext (String)
125 
126 #ifndef bool
127 typedef enum
128 {
129  false,
130  true
131 }
132 bool;
133 #endif
134 
135 // Debug macros
136 #define DEBUGCODE(mask) if((mask) & debug_status)
137 #define DEBUGMSG(mask, ...) \
138  if((mask) & debug_status) \
139  { \
140  fprintf(stderr, __VA_ARGS__); fflush(stderr); \
141  }
142 
143 // These values have to match those used in games.
144 static const int debug_loaders = 1 << 0;
145 static const int debug_menu = 1 << 1;
146 static const int debug_menu_parser = 1 << 2;
147 static const int debug_sdl = 1 << 3;
148 static const int debug_linewrap = 1 << 4;
149 static const int debug_i18n = 1 << 5;
150 static const int debug_all = ~0;
151 
152 extern int debug_status;
153 
164 #define START_CUSTOM_DEBUG 4
165 
166 // FIXME: global vars such as screen should be hidden when all games
167 // are using only getters (such as GetScreen() )
168 extern SDL_Surface* screen;
169 extern SDL_Rect menu_rect, stop_rect, prev_rect, next_rect;
170 extern SDL_Surface *stop_button, *prev_arrow, *next_arrow, *prev_gray, *next_gray;
171 
172 
173 #define MAX_SPRITE_FRAMES 15
175 
176 
177 
178 
179 
180 
181 
182 typedef struct
183 {
184  SDL_Surface *frame[MAX_SPRITE_FRAMES];
185  SDL_Surface *default_img;
187  int cur;
188 }
189 sprite;
190 
191 //==============================================================================
199 typedef enum
200 {
206 }
207 WipeStyle;
208 
212 enum
213 {
215  QUIT = -2,
216  STOP = -1
217 };
218 
222 typedef enum
223 {
227 }
228 MFStrategy;
229 
230 // from tk4_loaders.c
231 #define IMG_REGULAR 0x01
232 #define IMG_COLORKEY 0x02
233 #define IMG_ALPHA 0x04
234 #define IMG_MODES 0x07
235 
236 #define IMG_NOT_REQUIRED 0x10
237 #define IMG_NO_PNG_FALLBACK 0x20
238 
239 #define MAX_LINES 128
240 #define MAX_LINEWIDTH 256
241 
243 
244 //TODO separate headers for different areas a la SDL?
245 
246 //==============================================================================
247 // Public Definitions in t4k_main.c
248 //==============================================================================
249 
250 //==============================================================================
251 //
252 // InitT4KCommon
253 //
271 void InitT4KCommon( int debug_flags );
272 
273 //==============================================================================
274 //
275 // T4K_HandleStdEvents
276 //
289 int T4K_HandleStdEvents( const SDL_Event* event );
290 
291 
292 //==============================================================================
293 // Public Definitions in t4k_menu.c
294 //==============================================================================
295 
296 //==============================================================================
297 //
298 // T4K_SetActivitiesList
299 //
311 void T4K_SetActivitiesList( int num,
312  char** acts
313  );
314 
315 //==============================================================================
316 //
317 // T4K_SetMenuSounds
318 //
332 void T4K_SetMenuSounds( char* mus_path,
333  Mix_Chunk* click,
334  Mix_Chunk* hover
335  );
336 
337 //==============================================================================
338 //
339 // T4K_SetMenuSpritePrefix
340 //
350 void T4K_SetMenuSpritePrefix( char* pref );
351 
352 //==============================================================================
353 //
354 // T4K_SetMenuFontSize
355 //
368 void T4K_SetMenuFontSize( MFStrategy strategy,
369  int size
370  );
371 
372 //==============================================================================
373 //
374 // T4K_CreateOneLevelMenu
375 //
395 void T4K_CreateOneLevelMenu( int index,
396  int items,
397  char** item_names,
398  char** sprite_names,
399  char* title,
400  char* trailer
401  );
402 
403 //==============================================================================
404 //
405 // T4K_RunMenu
406 //
430 int T4K_RunMenu( int index,
431  bool return_choice,
432  void (*draw_background)(),
433  int (*handle_event)(SDL_Event*),
434  void (*handle_animations)(),
435  int (*handle_activity)(int, int)
436  );
437 
438 //==============================================================================
439 //
440 // T4K_PrerenderMenu
441 //
451 void T4K_PrerenderMenu( int index );
452 
453 //==============================================================================
454 //
455 // T4K_PrerenderAll
456 //
467 void T4K_PrerenderAll( void );
468 
469 //==============================================================================
470 //
471 // T4K_LoadMenu
472 //
485 void T4K_LoadMenu( int index,
486  const char* file_name
487  );
488 
489 //==============================================================================
490 //
491 // T4K_UnloadMenus
492 //
502 void T4K_UnloadMenus( void );
503 
504 
505 //==============================================================================
506 // Public Definitions in tk4_sdl.c
507 //==============================================================================
508 
509 //==============================================================================
510 //
511 // T4K_GetScreen
512 //
523 SDL_Surface* T4K_GetScreen( void );
524 
525 
526 //==============================================================================
527 //
528 // T4K_GetResolutions
529 //
548 int T4K_GetResolutions( int* win_x,
549  int* win_y,
550  int* full_x,
551  int* full_y
552  );
553 
554 //==============================================================================
555 //
556 // T4K_DrawButton
557 //
580 void T4K_DrawButton( SDL_Rect* target_rect,
581  int radius,
582  Uint8 r,
583  Uint8 g,
584  Uint8 b,
585  Uint8 a
586  );
587 
588 //==============================================================================
589 //
590 // T4K_DrawButtonOn
591 //
620 void T4K_DrawButtonOn( SDL_Surface* target,
621  SDL_Rect* target_rect,
622  int radius,
623  Uint8 r,
624  Uint8 g,
625  Uint8 b,
626  Uint8 a
627  );
628 
629 //==============================================================================
630 //
631 // T4K_CreateButton
632 //
656 SDL_Surface* T4K_CreateButton( int w,
657  int h,
658  int radius,
659  Uint8 r,
660  Uint8 g,
661  Uint8 b,
662  Uint8 a
663  );
664 
665 //==============================================================================
666 //
667 // T4K_RoundCorners
668 //
681 void T4K_RoundCorners( SDL_Surface* s,
682  Uint16 radius
683  );
684 
685 //==============================================================================
686 //
687 // T4K_Flip
688 //
702 SDL_Surface* T4K_Flip( SDL_Surface* in,
703  int x,
704  int y
705  );
706 
707 //==============================================================================
708 //
709 // T4K_Blend
710 //
732 SDL_Surface* T4K_Blend( SDL_Surface* S1,
733  SDL_Surface* S2,
734  float gamma
735  );
736 
737 //==============================================================================
738 //
739 // T4K_FreeSurfaceArray
740 //
752 void T4K_FreeSurfaceArray( SDL_Surface** surfs,
753  int length
754  );
755 
756 //==============================================================================
757 //
758 // T4K_inRect
759 //
775 int T4K_inRect( SDL_Rect r,
776  int x,
777  int y
778  );
779 
780 //==============================================================================
781 //
782 // T4K_SetRect
783 //
798 void T4K_SetRect( SDL_Rect* rect,
799  const float* pos
800  );
801 
802 //==============================================================================
803 //
804 // T4K_UpdateRect
805 //
817 void T4K_UpdateRect( SDL_Surface* surf,
818  SDL_Rect* rect
819  );
820 
821 //==============================================================================
822 //
823 // T4K_DarkenScreen
824 //
835 void T4K_DarkenScreen( Uint8 bits );
836 
837 //==============================================================================
838 //
839 // T4K_ChangeWindowSize
840 //
853 void T4K_ChangeWindowSize( int new_res_x,
854  int new_res_y
855  );
856 
857 //==============================================================================
858 //
859 // T4K_SwitchScreenMode
860 //
871 void T4K_SwitchScreenMode( void );
872 
873 //==============================================================================
874 //
878 typedef void (*ResSwitchCallback)(int resx, int resy);
879 
880 //==============================================================================
881 //
882 // T4K_OnResolutionSwitch
883 //
895 
896 //==============================================================================
897 //
898 // T4K_WaitForEvent
899 //
910 SDL_EventType T4K_WaitForEvent( SDL_EventMask events );
911 
912 //==============================================================================
913 //
914 // T4K_zoom
915 //
929 SDL_Surface* T4K_zoom( SDL_Surface* src,
930  int new_w,
931  int new_h
932  );
933 
934 //==============================================================================
935 //
936 // T4K_TransWipe
937 //
954 int T4K_TransWipe( const SDL_Surface* newbkg,
955  WipeStyle type,
956  int segments,
957  int duration
958  );
959 
960 //==============================================================================
961 //
962 // T4K_InitBlitQueue
963 //
980 void T4K_InitBlitQueue( void );
981 
982 //==============================================================================
983 //
984 // T4K_ResetBlitQueue
985 //
995 void T4K_ResetBlitQueue( void );
996 
997 //==============================================================================
998 //
999 // T4K_AddRect
1000 //
1015 int T4K_AddRect( SDL_Rect* src,
1016  SDL_Rect* dst
1017  );
1018 
1019 //==============================================================================
1020 //
1021 // T4K_DrawSprite
1022 //
1037 int T4K_DrawSprite( sprite* gfx,
1038  int x,
1039  int y
1040  );
1041 
1042 //==============================================================================
1043 //
1044 // T4K_DrawObject
1045 //
1061 int T4K_DrawObject( SDL_Surface* surf,
1062  int x,
1063  int y
1064  );
1065 
1066 //==============================================================================
1067 //
1068 // T4K_UpdateScreen
1069 //
1079 void T4K_UpdateScreen( int* frame );
1080 
1081 //==============================================================================
1082 //
1083 // T4K_EraseSprite
1084 //
1103 int T4K_EraseSprite( sprite* img,
1104  SDL_Surface* curr_bkgd,
1105  int x,
1106  int y
1107  );
1108 
1109 //==============================================================================
1110 //
1111 // T4K_EraseObject
1112 //
1130 int T4K_EraseObject( SDL_Surface* surf,
1131  SDL_Surface* curr_bkgd,
1132  int x,
1133  int y
1134  );
1135 
1136 //==============================================================================
1137 //
1138 // T4K_SetFontName
1139 //
1149 void T4K_SetFontName( const char* name );
1150 
1151 //==============================================================================
1152 //
1153 // T4K_AskFontName
1154 //
1164 const char* T4K_AskFontName( void );
1165 
1166 //==============================================================================
1167 //
1168 // T4K_Setup_SDL_Text
1169 //
1181 int T4K_Setup_SDL_Text( void );
1182 
1183 //==============================================================================
1184 //
1185 // T4K_Cleanup_SDL_Text
1186 //
1196 void T4K_Cleanup_SDL_Text( void );
1197 
1198 //==============================================================================
1199 //
1200 // T4K_BlackOutline
1201 //
1219 SDL_Surface* T4K_BlackOutline( const char* t,
1220  int size,
1221  SDL_Color* c
1222  );
1223 
1224 //==============================================================================
1225 //
1226 // T4K_SimpleText
1227 //
1241 SDL_Surface* T4K_SimpleText( const char* t,
1242  int size,
1243  SDL_Color* col
1244  );
1245 
1246 //==============================================================================
1247 //
1248 // T4K_SimpleTextWithOffset
1249 //
1265 SDL_Surface* T4K_SimpleTextWithOffset( const char* t,
1266  int size,
1267  SDL_Color* col,
1268  int* glyph_offset
1269  );
1270 
1271 //==============================================================================
1272 // T4K_CharsForWidth
1273 //
1285 int T4K_CharsForWidth( int fontsize,
1286  int pixel_width
1287  );
1288 
1289 //==============================================================================
1290 // Public Definitions in t4k_loaders.c
1291 //==============================================================================
1292 
1293 //==============================================================================
1294 //
1295 // T4K_AddDataPrefix
1296 //
1306 void T4K_AddDataPrefix( const char* path );
1307 
1308 //==============================================================================
1309 //
1310 // T4K_CheckFile
1311 //
1323 int T4K_CheckFile( const char* file );
1324 
1325 //==============================================================================
1326 //
1327 // T4K_RemoveSlash
1328 //
1338 char* T4K_RemoveSlash( char *path );
1339 
1340 //==============================================================================
1341 //
1342 // T4K_LoadImage
1343 //
1356 SDL_Surface* T4K_LoadImage( const char* file_name,
1357  int mode
1358  );
1359 
1360 //==============================================================================
1361 //
1362 // T4K_LoadScaledImage
1363 //
1385 SDL_Surface* T4K_LoadScaledImage( const char* file_name,
1386  int mode,
1387  int width,
1388  int height
1389  );
1390 
1391 //==============================================================================
1392 //
1393 // T4K_LoadImageOfBoundingBox
1394 //
1414 SDL_Surface* T4K_LoadImageOfBoundingBox( const char* file_name,
1415  int mode,
1416  int max_width,
1417  int max_height
1418  );
1419 
1420 //=============================================================================
1421 //
1422 // T4K_LoadBkgd
1423 //
1439 SDL_Surface* T4K_LoadBkgd( const char* file_name,
1440  int width,
1441  int height
1442  );
1443 
1444 //=============================================================================
1445 //
1446 // T4K_LoadBothBkgds
1447 //
1463 int T4K_LoadBothBkgds( const char* file_name,
1464  SDL_Surface** fs_bkgd,
1465  SDL_Surface** win_bkgd
1466  );
1467 
1468 //==============================================================================
1469 //
1470 // T4K_LoadSprite
1471 //
1486 sprite* T4K_LoadSprite( const char* name,
1487  int mode
1488  );
1489 
1490 //==============================================================================
1491 //
1492 // T4K_LoadScaledSprite
1493 //
1513 sprite* T4K_LoadScaledSprite( const char* name,
1514  int mode,
1515  int width,
1516  int height
1517  );
1518 
1519 //==============================================================================
1520 //
1521 // T4K_LoadSpriteOfBoundingBox
1522 //
1542 sprite* T4K_LoadSpriteOfBoundingBox( const char* name,
1543  int mode,
1544  int max_width,
1545  int max_height
1546  );
1547 
1548 //==============================================================================
1549 //
1550 // T4K_FlipSprite
1551 //
1569  int X,
1570  int Y
1571  );
1572 
1573 //==============================================================================
1574 //
1575 // T4K_FreeSprite
1576 //
1586 void T4K_FreeSprite( sprite* gfx );
1587 
1588 //==============================================================================
1589 //
1590 // T4K_NextFrame
1591 //
1601 void T4K_NextFrame( sprite* s );
1602 
1603 //==============================================================================
1604 //
1605 // T4K_LoadSound
1606 //
1616 Mix_Chunk* T4K_LoadSound( char* datafile );
1617 
1618 //==============================================================================
1619 //
1620 // T4K_LoadMusic
1621 //
1631 Mix_Music* T4K_LoadMusic( char *datafile );
1632 
1633 
1634 //==============================================================================
1635 // Public Definitions from t4k_audio.c
1636 //==============================================================================
1637 
1638 const static int T4K_AUDIO_PLAY_ONCE = 0;
1639 const static int T4K_AUDIO_LOOP_FOREVER = -1;
1640 
1641 //==============================================================================
1642 //
1643 // T4K_PlaySound
1644 //
1654 void T4K_PlaySound( Mix_Chunk* sound );
1655 
1656 //==============================================================================
1657 //
1658 // T4K_PlaySoundLoop
1659 //
1671 void T4K_PlaySoundLoop( Mix_Chunk* sound,
1672  int loops
1673  );
1674 
1675 //==============================================================================
1676 //
1677 // T4K_AudioHaltChannel
1678 //
1689 void T4K_AudioHaltChannel( int channel );
1690 
1691 //==============================================================================
1692 //
1693 // T4K_AudioMusicLoad
1694 //
1706 void T4K_AudioMusicLoad( char* music_path,
1707  int loops
1708  );
1709 
1710 //==============================================================================
1711 //
1712 // T4K_AudioMusicUnload
1713 //
1724 void T4K_AudioMusicUnload( void );
1725 
1726 //==============================================================================
1727 //
1728 // T4K_IsPlayingMusic
1729 //
1741 bool T4K_IsPlayingMusic( void );
1742 
1743 //==============================================================================
1744 //
1745 // T4K_AudioMusicPlay
1746 //
1758 void T4K_AudioMusicPlay( Mix_Music* musicData,
1759  int loops
1760  );
1761 
1762 //==============================================================================
1763 //
1764 // T4K_AudioEnable
1765 //
1776 void T4K_AudioEnable( bool enabled );
1777 
1778 //==============================================================================
1779 //
1780 // T4K_AudioToggle
1781 //
1791 void T4K_AudioToggle( void );
1792 
1793 
1794 //=============================================================================
1795 // Public Definitions for t4k_linewrap.c
1796 //=============================================================================
1797 
1798 //=============================================================================
1799 //
1800 // T4K_LineWrap
1801 //
1825 int T4K_LineWrap( const char* input,
1826  char str_list[MAX_LINES][MAX_LINEWIDTH],
1827  int width,
1828  int max_lines,
1829  int max_width
1830  );
1831 
1832 //=============================================================================
1833 //
1834 // T4K_LineWrapInsBreaks
1835 //
1858 int T4K_LineWrapInsBreaks( const char* input,
1859  char* output,
1860  int width,
1861  int max_lines,
1862  int max_width
1863  );
1864 //=============================================================================
1865 //
1866 // T4K_LineWrapList
1867 //
1891 void T4K_LineWrapList( const char input[MAX_LINES][MAX_LINEWIDTH],
1892  char str_list[MAX_LINES][MAX_LINEWIDTH],
1893  int width,
1894  int max_lines,
1895  int max_width
1896  );
1897 
1898 
1899 //=============================================================================
1900 // Public Definitions for t4k_throttle.c
1901 //=============================================================================
1902 
1903 
1904 //=============================================================================
1905 //
1906 // T4K_Throttle
1907 //
1923 void T4K_Throttle( int loop_msec,
1924  Uint32* last_t
1925  );
1926 
1927 
1928 #endif
1929 
1930 
1931 //=============================================================================
1932 // Public Definitions for t4k_convert_utf.c
1933 //=============================================================================
1934 
1935 
1936 //=============================================================================
1937 //
1938 // T4K_ConvertFromUTF8
1939 //
1956 int T4K_ConvertFromUTF8(wchar_t* wide_word,
1957  const char* UTF8_word,
1958  int max_length
1959  );
1960 
1961 //=============================================================================
1962 //
1963 // T4K_ConvertToUTF8
1964 //
1981 int T4K_ConvertToUTF8(const wchar_t* wide_word,
1982  char* UTF8_word,
1983  int max_length
1984  );