home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 123 / cdrom123.iso / edu / tux / Tuxtype2-1.5.3-installer.exe / src / setup.c < prev    next >
Encoding:
C/C++ Source or Header  |  2004-05-25  |  6.8 KB  |  240 lines

  1. /***************************************************************************
  2.                           setup.c 
  3.  -  description: Init SDL
  4.                              -------------------
  5.     begin                : Thu May 4 2000
  6.     copyright            : (C) 2000 by Sam Hart
  7.                          : (C) 2003 by Jesse Andrews
  8.     email                : tuxtype-dev@tux4kids.net
  9.  ***************************************************************************/
  10.  
  11. /***************************************************************************
  12.  *                                                                         *
  13.  *   This program is free software; you can redistribute it and/or modify  *
  14.  *   it under the terms of the GNU General Public License as published by  *
  15.  *   the Free Software Foundation; either version 2 of the License, or     *
  16.  *   (at your option) any later version.                                   *
  17.  *                                                                         *
  18.  ***************************************************************************/
  19.  
  20. #include "globals.h"
  21. #include "funcs.h"
  22.  
  23. //global vars
  24.  
  25.  
  26. int speed_up;
  27. int show_tux4kids;
  28. int debugOn;
  29.  
  30. int hidden; // Read the README file in the image directory for info on this ;)
  31.  
  32. settings localsettings;
  33.  
  34. /***************************
  35.     GraphicsInit: Initializes the graphic system
  36. ****************************/
  37. void GraphicsInit(Uint32 video_flags)
  38. {
  39.     LOG( "GraphicsInit - Initialize graphic system\n" );
  40.  
  41.     DEBUGCODE {
  42.         fprintf(stderr, "-SDL Setting VidMode to %ix%ix%i\n", RES_X, RES_Y, BPP);
  43.     }
  44.  
  45.     screen = SDL_SetVideoMode(RES_X, RES_Y, BPP, video_flags);
  46.  
  47.     if (screen == NULL) {
  48.         fprintf(stderr, "Couldn't set %ix%i video mode: %s\n", RES_X, RES_Y, SDL_GetError());
  49.         exit(2);
  50.     }
  51.  
  52.  
  53.     LOG( "SDL_SetClipRect(screen, NULL):\n" );
  54.  
  55.     SDL_SetClipRect(screen, NULL); // Let's set the appropriate clip rect  -- JA: is neccessary???  
  56.  
  57.     LOG( "SDL_ShowCursor(0):\n" );
  58.  
  59.     SDL_ShowCursor(0); // no cursor please
  60.  
  61.     LOG( "SDL_WM_SetCaption(\"Tux Typing\", PACKAGE);\n" );
  62.  
  63.     SDL_WM_SetCaption("Tux Typing", "tuxtype"); // Set window manager stuff
  64.  
  65.     /* --- setup color we use --- */
  66.     black.r       = 0x00; black.g       = 0x00; black.b       = 0x00;
  67.         gray.r        = 0x80; gray.g        = 0x80; gray.b        = 0x80;
  68.     dark_blue.r   = 0x00; dark_blue.g   = 0x00; dark_blue.b   = 0x60; 
  69.     red.r         = 0xff; red.g         = 0x00; red.b         = 0x00;
  70.     white.r       = 0xff; white.g       = 0xff; white.b       = 0xff;
  71.     yellow.r      = 0xff; yellow.g      = 0xff; yellow.b      = 0x00; 
  72.  
  73.     InitEngine();
  74.  
  75.     DEBUGCODE {
  76.         fprintf(stderr, "-SDL VidMode successfully set to %ix%ix%i\n", RES_X, RES_Y, BPP);
  77.     }
  78.  
  79.     LOG( "GraphicsInit():END\n" );
  80. }
  81.  
  82. /****************************
  83.     LibInit : Init the SDL
  84.     library
  85. *****************************/
  86. void LibInit(Uint32 lib_flags)
  87. {
  88.     LOG( "LibInit():\n-About to init SDL Library\n" );
  89.  
  90.     if (SDL_Init(lib_flags) < 0) 
  91.         if (sys_sound) {
  92.             if (SDL_Init(SDL_INIT_VIDEO) < 0) {
  93.                 fprintf(stderr, "Couldn't initialize SDL: %s\n",
  94.                 SDL_GetError());
  95.                 exit(2);
  96.             } else {
  97.                 LOG( "Couldn't initialize SDL Sound\n" );
  98.                 sys_sound = 0;
  99.             }
  100.  
  101.         }
  102.  
  103.     atexit(SDL_Quit); // fire and forget... 
  104.  
  105.     LOG( "-SDL Library init'd successfully\n" );
  106.  
  107.     if (sys_sound) 
  108.         if (Mix_OpenAudio( 22050, AUDIO_S16, 1, 2048) < 0) {
  109.             fprintf( stderr, "Warning: couldn't set 22050 Hz 8-bit audio\n - Reasons: %s\n", SDL_GetError());
  110.             sys_sound=0;
  111.         }
  112.  
  113.     LOG( "-about to init SDL_ttf\n" );
  114.  
  115.     if (TTF_Init() < 0) {
  116.         fprintf( stderr, "Couldn't initialize SDL_ttf\n" );
  117.         exit(2);
  118.     }
  119.  
  120.     atexit(TTF_Quit);
  121.  
  122.     SDL_EnableKeyRepeat( 0, SDL_DEFAULT_REPEAT_INTERVAL );
  123.     SDL_EnableUNICODE( 1 );
  124.  
  125.     LOG( "LibInit():END\n" );
  126. }
  127.  
  128. /* Load the settings from a file... make sure to update SaveSettings if you change
  129.  *  what can be saved/loaded 
  130.  */
  131. void LoadSettings( void ) {
  132.     
  133.     char fn[FNLEN];
  134.     char setting[FNLEN];
  135.     char value[FNLEN];
  136.     FILE *settingsFile;
  137.     
  138.     /* set the settings directory/file */
  139.  
  140.     #ifdef WIN32
  141.         snprintf( fn, FNLEN-1, "userdata/settings.txt" );
  142.     #else
  143.         snprintf( fn, FNLEN-1, (const char*)"%s/.tuxtype/settings.txt", getenv("HOME") );
  144.     #endif
  145.  
  146.     DEBUGCODE { printf("LoadSettings: settings file is '%s'\n", fn ); }
  147.     
  148.     LOG("LoadSettings: trying to open settings file\n");
  149.     
  150.     settingsFile = fopen( fn, "r" );
  151.  
  152.     
  153.     if (settingsFile == NULL) {
  154.         printf("LoadSettings: Settings file does not exist! settings not loaded\n");
  155.         localsettings.mus_volume = 100;
  156.         localsettings.sfx_volume = 100;
  157.         return;
  158.     }
  159.     
  160.     /* we load all the settings here */
  161.     
  162.     while (!feof(settingsFile)) {
  163.         fscanf( settingsFile, "%[^=]=%[^\n]\n", setting, value );
  164.     
  165.         DEBUGCODE { printf( "%s = %s", setting, value ); }
  166.         
  167.         if (strncmp( setting, "lang", FNLEN ) == 0 ) {
  168.             DEBUGCODE { printf("LoadSettings: Setting language to %s", value); }
  169.             strncpy( localsettings.lang, value, FNLEN-1 );
  170.             localsettings.lang[FNLEN-1]=0;
  171.             setupTheme( value );
  172.         }
  173.         if (strncmp( setting, "o_lives", FNLEN ) == 0 ) {
  174.             DEBUGCODE { printf("LoadSettings: Setting lives to %s", value); }
  175.             o_lives = atoi(value);
  176.         }
  177.         if (strncmp( setting, "mus_volume", FNLEN ) == 0 ) {
  178.             DEBUGCODE { printf("LoadSettings: Setting misic volume to %s", value); }
  179.             localsettings.mus_volume = atoi(value);
  180.         }
  181.         if (strncmp( setting, "sfx_volume", FNLEN ) == 0 ) {
  182.             DEBUGCODE { printf("LoadSettings: Setting effects volume to %s", value); }
  183.             localsettings.sfx_volume = atoi(value);
  184.         }
  185.         if (strncmp( setting, "window", FNLEN ) == 0 ) {
  186.                 strncpy(localsettings.window, value, FNLEN-1 );
  187.         }
  188.     }
  189.     
  190.     fclose( settingsFile );
  191.  
  192. }
  193.  
  194. /* Save the settings from a file... make sure to update LoadSettings if you change
  195.  *  what can be saved/loaded 
  196.  */
  197. void SaveSettings( void ) {
  198.     char fn[FNLEN];
  199.     FILE *settingsFile;
  200.     
  201.     /* set the settings directory/file */
  202.  
  203.     #ifdef WIN32
  204.         _mkdir( "userdata" );  // just in case try to create save location
  205.         snprintf( fn, FNLEN-1, "userdata/settings.txt" );
  206.     #else
  207.         snprintf( fn, FNLEN-1, (const char*)"%s/.tuxtype", getenv("HOME") );
  208.         mkdir( fn, 0755 ); // just in case try to create save location
  209.         snprintf( fn, FNLEN-1, (const char*)"%s/.tuxtype/settings.txt", getenv("HOME") );
  210.     #endif
  211.  
  212.  
  213.     DEBUGCODE { printf("SaveSettings: settings file is '%s'\n", fn ); }
  214.     
  215.     LOG("SaveSettings: trying to open settings file\n");
  216.     
  217.     settingsFile = fopen( fn, "w" );
  218.  
  219.     if (settingsFile == NULL) {
  220.         printf("SaveSettings: Settings file cannot be created!\n");
  221.         return;
  222.     }
  223.     
  224.     /* Save all the settings here! */
  225.     if (strncmp( themeName, "", FNLEN) != 0)
  226.         fprintf( settingsFile, "lang=%s\n", themeName );
  227.     if (o_lives > 9)
  228.         fprintf( settingsFile, "o_lives=%d\n", o_lives );
  229.  
  230.     fprintf( settingsFile, "mus_volume=%d\n", localsettings.mus_volume );
  231.     fprintf( settingsFile, "sfx_volume=%d\n", localsettings.sfx_volume );
  232.  
  233.     if (screen->flags & SDL_FULLSCREEN){
  234.         fprintf( settingsFile, "window=%s\n", "no" );
  235.     } else {
  236.         fprintf( settingsFile, "window=%s\n", "yes" );
  237.     }
  238.     fclose( settingsFile );
  239. }
  240.