Tux4Kids-Common
|
00001 //============================================================================== 00002 // 00003 // t4k_common.h: Contains public headers for the t4k_common library 00004 // 00005 // Copyright 2009, 2010. 00006 // Author: Brendan Luchen 00007 // Project email: <tuxmath-devel@lists.sourceforge.net> 00008 // Project website: http://tux4kids.alioth.debian.org 00009 // 00010 // t4k_common.h is part of the t4k_common library. 00011 // 00012 // t4k_common is free software: you can redistribute it and/or modify 00013 // it under the terms of the GNU General Public License as published by 00014 // the Free Software Foundation; either version 3 of the License, or 00015 // (at your option) any later version. 00016 // 00017 // t4k_common is distributed in the hope that it will be useful, 00018 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00020 // GNU General Public License for more details. 00021 // 00022 // You should have received a copy of the GNU General Public License 00023 // along with this program. If not, see <http://www.gnu.org/licenses/>. 00024 // 00025 //============================================================================== 00026 00027 //============================================================================== 00028 // 00051 00052 #ifndef TUX4KIDS_COMMON_H 00053 #define TUX4KIDS_COMMON_H 00054 00055 00056 #include <sys/types.h> 00057 #include <sys/stat.h> 00058 #include <fcntl.h> 00059 #include <unistd.h> 00060 #include <dirent.h> 00061 #include <wchar.h> 00062 #include "SDL.h" 00063 #include "SDL_image.h" 00064 #include "SDL_mixer.h" 00065 00066 //============================================================================== 00067 // Debugging macros 00068 // 00069 // All macros take a debug mask as their first argument, which should roughly 00070 // indicate what part of the program the debugging info is related to, and 00071 // which can be one of the flags defined below, a program-specific flag, or 00072 // an OR'd combination. 00073 // 00074 // DEBUGVAR prints out the name and value of a string variable 00075 #ifndef DEBUGMSG 00076 00078 #define DEBUGVAR(mask, Expr) \ 00079 if((mask) & (debug_status)) \ 00080 { \ 00081 fprintf(stderr, #Expr ": %s\n", (Expr)); fflush(stderr); \ 00082 } 00083 00085 #define DEBUGVARX(mask, Expr) \ 00086 if((mask) & (debug_status)) \ 00087 { \ 00088 fprintf(stderr, #Expr ": %x\n", (Expr)); fflush(stderr); \ 00089 } 00090 00092 #define DEBUGVARF(mask, Expr) \ 00093 if((mask) & (debug_status)) \ 00094 { \ 00095 fprintf(stderr, #Expr ": %f\n", (Expr)); fflush(stderr); \ 00096 } 00097 00099 #define DEBUGCODE(mask) if((mask) & debug_status) 00100 00102 #define DEBUGMSG(mask, ...) \ 00103 if((mask) & debug_status) \ 00104 { \ 00105 fprintf(stderr, __VA_ARGS__); fflush(stderr); \ 00106 } 00107 #endif 00108 00109 #if SDL_BYTEORDER == SDL_BIG_ENDIAN 00110 #define rmask 0xff000000 //!< SDL red mask 00111 #define gmask 0x00ff0000 //!< SDL green mask 00112 #define bmask 0x0000ff00 //!< SDL blue mask 00113 #define amask 0x000000ff //!< SDL alpha mask 00114 #else 00115 #define rmask 0x000000ff //!< SDL red mask 00116 #define gmask 0x0000ff00 //!< SDL green mask 00117 #define bmask 0x00ff0000 //!< SDL blue mask 00118 #define amask 0xff000000 //!< SDL alpha mask 00119 #endif 00120 00121 // Hold off on gettext until we decide if we are really going to 00122 // use it from within t4kcommon - DSB 00123 //#define _(String) String 00124 // #define _(String) gettext (String) 00125 00126 #ifndef bool 00127 typedef enum 00128 { 00129 false, 00130 true 00131 } 00132 bool; 00133 #endif 00134 00135 // Debug macros 00136 #define DEBUGCODE(mask) if((mask) & debug_status) 00137 #define DEBUGMSG(mask, ...) \ 00138 if((mask) & debug_status) \ 00139 { \ 00140 fprintf(stderr, __VA_ARGS__); fflush(stderr); \ 00141 } 00142 00143 // These values have to match those used in games. 00144 static const int debug_loaders = 1 << 0; 00145 static const int debug_menu = 1 << 1; 00146 static const int debug_menu_parser = 1 << 2; 00147 static const int debug_sdl = 1 << 3; 00148 static const int debug_linewrap = 1 << 4; 00149 static const int debug_i18n = 1 << 5; 00150 static const int debug_all = ~0; 00151 00152 extern int debug_status; 00153 00164 #define START_CUSTOM_DEBUG 4 00165 00166 // FIXME: global vars such as screen should be hidden when all games 00167 // are using only getters (such as GetScreen() ) 00168 extern SDL_Surface* screen; 00169 extern SDL_Rect menu_rect, stop_rect, prev_rect, next_rect; 00170 extern SDL_Surface *stop_button, *prev_arrow, *next_arrow, *prev_gray, *next_gray; 00171 00172 00173 #define MAX_SPRITE_FRAMES 15 00175 00176 00177 00178 00179 00180 00181 00182 typedef struct 00183 { 00184 SDL_Surface *frame[MAX_SPRITE_FRAMES]; 00185 SDL_Surface *default_img; 00186 int num_frames; 00187 int cur; 00188 } 00189 sprite; 00190 00191 //============================================================================== 00199 typedef enum 00200 { 00201 WIPE_BLINDS_VERT, 00202 WIPE_BLINDS_HORIZ, 00203 WIPE_BLINDS_BOX, 00204 RANDOM_WIPE, 00205 NUM_WIPES 00206 } 00207 WipeStyle; 00208 00212 enum 00213 { 00214 RUN_MAIN_MENU = -3, 00215 QUIT = -2, 00216 STOP = -1 00217 }; 00218 00222 typedef enum 00223 { 00224 MF_UNIFORM, 00225 MF_BESTFIT, 00226 MF_EXACTLY 00227 } 00228 MFStrategy; 00229 00230 // from tk4_loaders.c 00231 #define IMG_REGULAR 0x01 00232 #define IMG_COLORKEY 0x02 00233 #define IMG_ALPHA 0x04 00234 #define IMG_MODES 0x07 00235 00236 #define IMG_NOT_REQUIRED 0x10 00237 #define IMG_NO_PNG_FALLBACK 0x20 00238 00239 #define MAX_LINES 128 //!< Maximum lines to wrap. 00240 #define MAX_LINEWIDTH 256 //!< Maximum characters of each line. 00241 00242 char wrapped_lines[MAX_LINES][MAX_LINEWIDTH]; 00243 00244 //TODO separate headers for different areas a la SDL? 00245 00246 //============================================================================== 00247 // Public Definitions in t4k_main.c 00248 //============================================================================== 00249 00250 //============================================================================== 00251 // 00252 // InitT4KCommon 00253 // 00271 void InitT4KCommon( int debug_flags ); 00272 00273 //============================================================================== 00274 // 00275 // T4K_HandleStdEvents 00276 // 00289 int T4K_HandleStdEvents( const SDL_Event* event ); 00290 00291 00292 //============================================================================== 00293 // Public Definitions in t4k_menu.c 00294 //============================================================================== 00295 00296 //============================================================================== 00297 // 00298 // T4K_SetActivitiesList 00299 // 00311 void T4K_SetActivitiesList( int num, 00312 char** acts 00313 ); 00314 00315 //============================================================================== 00316 // 00317 // T4K_SetMenuSounds 00318 // 00332 void T4K_SetMenuSounds( char* mus_path, 00333 Mix_Chunk* click, 00334 Mix_Chunk* hover 00335 ); 00336 00337 //============================================================================== 00338 // 00339 // T4K_SetMenuSpritePrefix 00340 // 00350 void T4K_SetMenuSpritePrefix( char* pref ); 00351 00352 //============================================================================== 00353 // 00354 // T4K_SetMenuFontSize 00355 // 00368 void T4K_SetMenuFontSize( MFStrategy strategy, 00369 int size 00370 ); 00371 00372 //============================================================================== 00373 // 00374 // T4K_CreateOneLevelMenu 00375 // 00395 void T4K_CreateOneLevelMenu( int index, 00396 int items, 00397 char** item_names, 00398 char** sprite_names, 00399 char* title, 00400 char* trailer 00401 ); 00402 00403 //============================================================================== 00404 // 00405 // T4K_RunMenu 00406 // 00430 int T4K_RunMenu( int index, 00431 bool return_choice, 00432 void (*draw_background)(), 00433 int (*handle_event)(SDL_Event*), 00434 void (*handle_animations)(), 00435 int (*handle_activity)(int, int) 00436 ); 00437 00438 //============================================================================== 00439 // 00440 // T4K_PrerenderMenu 00441 // 00451 void T4K_PrerenderMenu( int index ); 00452 00453 //============================================================================== 00454 // 00455 // T4K_PrerenderAll 00456 // 00467 void T4K_PrerenderAll( void ); 00468 00469 //============================================================================== 00470 // 00471 // T4K_LoadMenu 00472 // 00485 void T4K_LoadMenu( int index, 00486 const char* file_name 00487 ); 00488 00489 //============================================================================== 00490 // 00491 // T4K_UnloadMenus 00492 // 00502 void T4K_UnloadMenus( void ); 00503 00504 00505 //============================================================================== 00506 // Public Definitions in tk4_sdl.c 00507 //============================================================================== 00508 00509 //============================================================================== 00510 // 00511 // T4K_GetScreen 00512 // 00523 SDL_Surface* T4K_GetScreen( void ); 00524 00525 00526 //============================================================================== 00527 // 00528 // T4K_GetResolutions 00529 // 00548 int T4K_GetResolutions( int* win_x, 00549 int* win_y, 00550 int* full_x, 00551 int* full_y 00552 ); 00553 00554 //============================================================================== 00555 // 00556 // T4K_DrawButton 00557 // 00580 void T4K_DrawButton( SDL_Rect* target_rect, 00581 int radius, 00582 Uint8 r, 00583 Uint8 g, 00584 Uint8 b, 00585 Uint8 a 00586 ); 00587 00588 //============================================================================== 00589 // 00590 // T4K_DrawButtonOn 00591 // 00620 void T4K_DrawButtonOn( SDL_Surface* target, 00621 SDL_Rect* target_rect, 00622 int radius, 00623 Uint8 r, 00624 Uint8 g, 00625 Uint8 b, 00626 Uint8 a 00627 ); 00628 00629 //============================================================================== 00630 // 00631 // T4K_CreateButton 00632 // 00656 SDL_Surface* T4K_CreateButton( int w, 00657 int h, 00658 int radius, 00659 Uint8 r, 00660 Uint8 g, 00661 Uint8 b, 00662 Uint8 a 00663 ); 00664 00665 //============================================================================== 00666 // 00667 // T4K_RoundCorners 00668 // 00681 void T4K_RoundCorners( SDL_Surface* s, 00682 Uint16 radius 00683 ); 00684 00685 //============================================================================== 00686 // 00687 // T4K_Flip 00688 // 00702 SDL_Surface* T4K_Flip( SDL_Surface* in, 00703 int x, 00704 int y 00705 ); 00706 00707 //============================================================================== 00708 // 00709 // T4K_Blend 00710 // 00732 SDL_Surface* T4K_Blend( SDL_Surface* S1, 00733 SDL_Surface* S2, 00734 float gamma 00735 ); 00736 00737 //============================================================================== 00738 // 00739 // T4K_FreeSurfaceArray 00740 // 00752 void T4K_FreeSurfaceArray( SDL_Surface** surfs, 00753 int length 00754 ); 00755 00756 //============================================================================== 00757 // 00758 // T4K_inRect 00759 // 00775 int T4K_inRect( SDL_Rect r, 00776 int x, 00777 int y 00778 ); 00779 00780 //============================================================================== 00781 // 00782 // T4K_SetRect 00783 // 00798 void T4K_SetRect( SDL_Rect* rect, 00799 const float* pos 00800 ); 00801 00802 //============================================================================== 00803 // 00804 // T4K_UpdateRect 00805 // 00817 void T4K_UpdateRect( SDL_Surface* surf, 00818 SDL_Rect* rect 00819 ); 00820 00821 //============================================================================== 00822 // 00823 // T4K_DarkenScreen 00824 // 00835 void T4K_DarkenScreen( Uint8 bits ); 00836 00837 //============================================================================== 00838 // 00839 // T4K_ChangeWindowSize 00840 // 00853 void T4K_ChangeWindowSize( int new_res_x, 00854 int new_res_y 00855 ); 00856 00857 //============================================================================== 00858 // 00859 // T4K_SwitchScreenMode 00860 // 00871 void T4K_SwitchScreenMode( void ); 00872 00873 //============================================================================== 00874 // 00878 typedef void (*ResSwitchCallback)(int resx, int resy); 00879 00880 //============================================================================== 00881 // 00882 // T4K_OnResolutionSwitch 00883 // 00894 void T4K_OnResolutionSwitch( ResSwitchCallback callback ); 00895 00896 //============================================================================== 00897 // 00898 // T4K_WaitForEvent 00899 // 00910 SDL_EventType T4K_WaitForEvent( SDL_EventMask events ); 00911 00912 //============================================================================== 00913 // 00914 // T4K_zoom 00915 // 00929 SDL_Surface* T4K_zoom( SDL_Surface* src, 00930 int new_w, 00931 int new_h 00932 ); 00933 00934 //============================================================================== 00935 // 00936 // T4K_TransWipe 00937 // 00954 int T4K_TransWipe( const SDL_Surface* newbkg, 00955 WipeStyle type, 00956 int segments, 00957 int duration 00958 ); 00959 00960 //============================================================================== 00961 // 00962 // T4K_InitBlitQueue 00963 // 00980 void T4K_InitBlitQueue( void ); 00981 00982 //============================================================================== 00983 // 00984 // T4K_ResetBlitQueue 00985 // 00995 void T4K_ResetBlitQueue( void ); 00996 00997 //============================================================================== 00998 // 00999 // T4K_AddRect 01000 // 01015 int T4K_AddRect( SDL_Rect* src, 01016 SDL_Rect* dst 01017 ); 01018 01019 //============================================================================== 01020 // 01021 // T4K_DrawSprite 01022 // 01037 int T4K_DrawSprite( sprite* gfx, 01038 int x, 01039 int y 01040 ); 01041 01042 //============================================================================== 01043 // 01044 // T4K_DrawObject 01045 // 01061 int T4K_DrawObject( SDL_Surface* surf, 01062 int x, 01063 int y 01064 ); 01065 01066 //============================================================================== 01067 // 01068 // T4K_UpdateScreen 01069 // 01079 void T4K_UpdateScreen( int* frame ); 01080 01081 //============================================================================== 01082 // 01083 // T4K_EraseSprite 01084 // 01103 int T4K_EraseSprite( sprite* img, 01104 SDL_Surface* curr_bkgd, 01105 int x, 01106 int y 01107 ); 01108 01109 //============================================================================== 01110 // 01111 // T4K_EraseObject 01112 // 01130 int T4K_EraseObject( SDL_Surface* surf, 01131 SDL_Surface* curr_bkgd, 01132 int x, 01133 int y 01134 ); 01135 01136 //============================================================================== 01137 // 01138 // T4K_SetFontName 01139 // 01149 void T4K_SetFontName( const char* name ); 01150 01151 //============================================================================== 01152 // 01153 // T4K_AskFontName 01154 // 01164 const char* T4K_AskFontName( void ); 01165 01166 //============================================================================== 01167 // 01168 // T4K_Setup_SDL_Text 01169 // 01181 int T4K_Setup_SDL_Text( void ); 01182 01183 //============================================================================== 01184 // 01185 // T4K_Cleanup_SDL_Text 01186 // 01196 void T4K_Cleanup_SDL_Text( void ); 01197 01198 //============================================================================== 01199 // 01200 // T4K_BlackOutline 01201 // 01219 SDL_Surface* T4K_BlackOutline( const char* t, 01220 int size, 01221 SDL_Color* c 01222 ); 01223 01224 //============================================================================== 01225 // 01226 // T4K_SimpleText 01227 // 01241 SDL_Surface* T4K_SimpleText( const char* t, 01242 int size, 01243 SDL_Color* col 01244 ); 01245 01246 //============================================================================== 01247 // 01248 // T4K_SimpleTextWithOffset 01249 // 01265 SDL_Surface* T4K_SimpleTextWithOffset( const char* t, 01266 int size, 01267 SDL_Color* col, 01268 int* glyph_offset 01269 ); 01270 01271 //============================================================================== 01272 // T4K_CharsForWidth 01273 // 01285 int T4K_CharsForWidth( int fontsize, 01286 int pixel_width 01287 ); 01288 01289 //============================================================================== 01290 // Public Definitions in t4k_loaders.c 01291 //============================================================================== 01292 01293 //============================================================================== 01294 // 01295 // T4K_AddDataPrefix 01296 // 01306 void T4K_AddDataPrefix( const char* path ); 01307 01308 //============================================================================== 01309 // 01310 // T4K_CheckFile 01311 // 01323 int T4K_CheckFile( const char* file ); 01324 01325 //============================================================================== 01326 // 01327 // T4K_RemoveSlash 01328 // 01338 char* T4K_RemoveSlash( char *path ); 01339 01340 //============================================================================== 01341 // 01342 // T4K_LoadImage 01343 // 01356 SDL_Surface* T4K_LoadImage( const char* file_name, 01357 int mode 01358 ); 01359 01360 //============================================================================== 01361 // 01362 // T4K_LoadScaledImage 01363 // 01385 SDL_Surface* T4K_LoadScaledImage( const char* file_name, 01386 int mode, 01387 int width, 01388 int height 01389 ); 01390 01391 //============================================================================== 01392 // 01393 // T4K_LoadImageOfBoundingBox 01394 // 01414 SDL_Surface* T4K_LoadImageOfBoundingBox( const char* file_name, 01415 int mode, 01416 int max_width, 01417 int max_height 01418 ); 01419 01420 //============================================================================= 01421 // 01422 // T4K_LoadBkgd 01423 // 01439 SDL_Surface* T4K_LoadBkgd( const char* file_name, 01440 int width, 01441 int height 01442 ); 01443 01444 //============================================================================= 01445 // 01446 // T4K_LoadBothBkgds 01447 // 01463 int T4K_LoadBothBkgds( const char* file_name, 01464 SDL_Surface** fs_bkgd, 01465 SDL_Surface** win_bkgd 01466 ); 01467 01468 //============================================================================== 01469 // 01470 // T4K_LoadSprite 01471 // 01486 sprite* T4K_LoadSprite( const char* name, 01487 int mode 01488 ); 01489 01490 //============================================================================== 01491 // 01492 // T4K_LoadScaledSprite 01493 // 01513 sprite* T4K_LoadScaledSprite( const char* name, 01514 int mode, 01515 int width, 01516 int height 01517 ); 01518 01519 //============================================================================== 01520 // 01521 // T4K_LoadSpriteOfBoundingBox 01522 // 01542 sprite* T4K_LoadSpriteOfBoundingBox( const char* name, 01543 int mode, 01544 int max_width, 01545 int max_height 01546 ); 01547 01548 //============================================================================== 01549 // 01550 // T4K_FlipSprite 01551 // 01568 sprite* T4K_FlipSprite ( sprite* in, 01569 int X, 01570 int Y 01571 ); 01572 01573 //============================================================================== 01574 // 01575 // T4K_FreeSprite 01576 // 01586 void T4K_FreeSprite( sprite* gfx ); 01587 01588 //============================================================================== 01589 // 01590 // T4K_NextFrame 01591 // 01601 void T4K_NextFrame( sprite* s ); 01602 01603 //============================================================================== 01604 // 01605 // T4K_LoadSound 01606 // 01616 Mix_Chunk* T4K_LoadSound( char* datafile ); 01617 01618 //============================================================================== 01619 // 01620 // T4K_LoadMusic 01621 // 01631 Mix_Music* T4K_LoadMusic( char *datafile ); 01632 01633 01634 //============================================================================== 01635 // Public Definitions from t4k_audio.c 01636 //============================================================================== 01637 01638 const static int T4K_AUDIO_PLAY_ONCE = 0; 01639 const static int T4K_AUDIO_LOOP_FOREVER = -1; 01640 01641 //============================================================================== 01642 // 01643 // T4K_PlaySound 01644 // 01654 void T4K_PlaySound( Mix_Chunk* sound ); 01655 01656 //============================================================================== 01657 // 01658 // T4K_PlaySoundLoop 01659 // 01671 void T4K_PlaySoundLoop( Mix_Chunk* sound, 01672 int loops 01673 ); 01674 01675 //============================================================================== 01676 // 01677 // T4K_AudioHaltChannel 01678 // 01689 void T4K_AudioHaltChannel( int channel ); 01690 01691 //============================================================================== 01692 // 01693 // T4K_AudioMusicLoad 01694 // 01706 void T4K_AudioMusicLoad( char* music_path, 01707 int loops 01708 ); 01709 01710 //============================================================================== 01711 // 01712 // T4K_AudioMusicUnload 01713 // 01724 void T4K_AudioMusicUnload( void ); 01725 01726 //============================================================================== 01727 // 01728 // T4K_IsPlayingMusic 01729 // 01741 bool T4K_IsPlayingMusic( void ); 01742 01743 //============================================================================== 01744 // 01745 // T4K_AudioMusicPlay 01746 // 01758 void T4K_AudioMusicPlay( Mix_Music* musicData, 01759 int loops 01760 ); 01761 01762 //============================================================================== 01763 // 01764 // T4K_AudioEnable 01765 // 01776 void T4K_AudioEnable( bool enabled ); 01777 01778 //============================================================================== 01779 // 01780 // T4K_AudioToggle 01781 // 01791 void T4K_AudioToggle( void ); 01792 01793 01794 //============================================================================= 01795 // Public Definitions for t4k_linewrap.c 01796 //============================================================================= 01797 01798 //============================================================================= 01799 // 01800 // T4K_LineWrap 01801 // 01825 int T4K_LineWrap( const char* input, 01826 char str_list[MAX_LINES][MAX_LINEWIDTH], 01827 int width, 01828 int max_lines, 01829 int max_width 01830 ); 01831 01832 //============================================================================= 01833 // 01834 // T4K_LineWrapInsBreaks 01835 // 01858 int T4K_LineWrapInsBreaks( const char* input, 01859 char* output, 01860 int width, 01861 int max_lines, 01862 int max_width 01863 ); 01864 //============================================================================= 01865 // 01866 // T4K_LineWrapList 01867 // 01891 void T4K_LineWrapList( const char input[MAX_LINES][MAX_LINEWIDTH], 01892 char str_list[MAX_LINES][MAX_LINEWIDTH], 01893 int width, 01894 int max_lines, 01895 int max_width 01896 ); 01897 01898 01899 //============================================================================= 01900 // Public Definitions for t4k_throttle.c 01901 //============================================================================= 01902 01903 01904 //============================================================================= 01905 // 01906 // T4K_Throttle 01907 // 01923 void T4K_Throttle( int loop_msec, 01924 Uint32* last_t 01925 ); 01926 01927 01928 #endif 01929 01930 01931 //============================================================================= 01932 // Public Definitions for t4k_convert_utf.c 01933 //============================================================================= 01934 01935 01936 //============================================================================= 01937 // 01938 // T4K_ConvertFromUTF8 01939 // 01956 int T4K_ConvertFromUTF8(wchar_t* wide_word, 01957 const char* UTF8_word, 01958 int max_length 01959 ); 01960 01961 //============================================================================= 01962 // 01963 // T4K_ConvertToUTF8 01964 // 01981 int T4K_ConvertToUTF8(const wchar_t* wide_word, 01982 char* UTF8_word, 01983 int max_length 01984 );