[Tux4kids-commits] r1333 - branches/commonification/tuxmath/trunk/src

Bolesław Kulbabiński bolekk-guest at alioth.debian.org
Mon Aug 3 00:35:40 UTC 2009


Author: bolekk-guest
Date: 2009-08-03 00:35:40 +0000 (Mon, 03 Aug 2009)
New Revision: 1333

Modified:
   branches/commonification/tuxmath/trunk/src/SDL_extras.c
   branches/commonification/tuxmath/trunk/src/SDL_extras.h
   branches/commonification/tuxmath/trunk/src/audio.c
   branches/commonification/tuxmath/trunk/src/campaign.c
   branches/commonification/tuxmath/trunk/src/factoroids.c
   branches/commonification/tuxmath/trunk/src/fileops_media.c
   branches/commonification/tuxmath/trunk/src/game.c
   branches/commonification/tuxmath/trunk/src/globals.h
   branches/commonification/tuxmath/trunk/src/highscore.c
   branches/commonification/tuxmath/trunk/src/loaders.c
   branches/commonification/tuxmath/trunk/src/menu.c
   branches/commonification/tuxmath/trunk/src/menu.h
   branches/commonification/tuxmath/trunk/src/options.c
   branches/commonification/tuxmath/trunk/src/setup.c
   branches/commonification/tuxmath/trunk/src/titlescreen.c
   branches/commonification/tuxmath/trunk/src/titlescreen.h
Log:
tuxmath builds with and without t4kcommon

Modified: branches/commonification/tuxmath/trunk/src/SDL_extras.c
===================================================================
--- branches/commonification/tuxmath/trunk/src/SDL_extras.c	2009-08-03 00:34:32 UTC (rev 1332)
+++ branches/commonification/tuxmath/trunk/src/SDL_extras.c	2009-08-03 00:35:40 UTC (rev 1333)
@@ -18,8 +18,10 @@
 #include "options.h"
 
 
+SDL_Surface* screen = NULL;
 #ifndef HAVE_LIBT4KCOMMON
 
+
 /* window size */
 int win_res_x = 640;
 int win_res_y = 480;
@@ -28,7 +30,25 @@
 int fs_res_x = 0;
 int fs_res_y = 0;
 
+/*
+Return a pointer to the screen we're using, as an alternative to making screen
+global. Not sure what is involved performance-wise in SDL_GetVideoSurface,
+or if this check is even necessary -Cheez
+*/
+SDL_Surface* GetScreen()
+{
+  DEBUGCODE(debug_sdl)
+  {
+    if (screen != SDL_GetVideoSurface() )
+    {
+      fprintf(stderr, "Video Surface changed from outside of SDL_Extras!\n");
+      screen = SDL_GetVideoSurface();
+    }
+  }
+  return screen;
+}
 
+
 /* DrawButton() creates a translucent button with rounded ends
    and draws it on the screen.
    All colors and alpha values are supported.*/
@@ -36,9 +56,18 @@
                 int radius,
                 Uint8 r, Uint8 g, Uint8 b, Uint8 a)
 {
+  DrawButtonOn(screen, target_rect, radius, r, g, b, a);
+}
+
+void DrawButtonOn(SDL_Surface* target,
+                SDL_Rect* target_rect,
+                int radius,
+                Uint8 r, Uint8 g, Uint8 b, Uint8 a)
+
+{
   SDL_Surface* tmp_surf = CreateButton(target_rect->w, target_rect->h,
                                        radius, r, g, b, a);
-  SDL_BlitSurface(tmp_surf, NULL, screen, target_rect);
+  SDL_BlitSurface(tmp_surf, NULL, target, target_rect);
   SDL_FreeSurface(tmp_surf);
 }
 

Modified: branches/commonification/tuxmath/trunk/src/SDL_extras.h
===================================================================
--- branches/commonification/tuxmath/trunk/src/SDL_extras.h	2009-08-03 00:34:32 UTC (rev 1332)
+++ branches/commonification/tuxmath/trunk/src/SDL_extras.h	2009-08-03 00:35:40 UTC (rev 1333)
@@ -29,10 +29,11 @@
 #endif
 
 /* Non-text graphics functions: */
+SDL_Surface*    GetScreen();
 void            DrawButton(SDL_Rect* target_rect, int radius, Uint8 r, Uint8 g, Uint8 b, Uint8 a);
+void            DrawButtonOn(SDL_Surface* target, SDL_Rect* target_rect, int radius, Uint8 r, Uint8 g, Uint8 b, Uint8 a);
 SDL_Surface*    CreateButton(int w, int h, int radius, Uint8 r, Uint8 g, Uint8 b, Uint8 a);
 void            RoundCorners(SDL_Surface* s, Uint16 radius);
-#endif
 
 SDL_Surface*    Flip(SDL_Surface *in, int x, int y);
 SDL_Surface*    Blend(SDL_Surface *S1, SDL_Surface *S2, float gamma);
@@ -58,3 +59,4 @@
 
 
 #endif
+#endif

Modified: branches/commonification/tuxmath/trunk/src/audio.c
===================================================================
--- branches/commonification/tuxmath/trunk/src/audio.c	2009-08-03 00:34:32 UTC (rev 1332)
+++ branches/commonification/tuxmath/trunk/src/audio.c	2009-08-03 00:35:40 UTC (rev 1333)
@@ -25,13 +25,11 @@
 #include "options.h"   //Needed for Opts_UsingSound()
 #include "titlescreen.h"
 
-Mix_Music *music;
-
-void playsound(int snd)
+void PlaySound(Mix_Chunk* sound)
 {
 #ifndef NOSOUND
   if (Opts_UsingSound())
-    Mix_PlayChannel(-1, sounds[snd], 0);
+    Mix_PlayChannel(-1, sound, 0);
 #endif
 }
 
@@ -42,14 +40,14 @@
 /* audioMusicLoad attempts to load and play the music file 
  * Note: loops == -1 means forever
  */
-void audioMusicLoad(char *musicFilename, int loops)
+void AudioMusicLoad(char *musicFilename, int loops)
 {
   if (!Opts_UsingSound())
   {
     return;
   }
 
-  audioMusicUnload(); // make sure defaultMusic is clear
+  AudioMusicUnload(); // make sure defaultMusic is clear
   defaultMusic = LoadMusic(musicFilename);
   Mix_PlayMusic(defaultMusic, loops);
 }
@@ -58,7 +56,7 @@
 /* audioMusicUnload attempts to unload any music data that was
  * loaded using the audioMusicLoad function
  */
-void audioMusicUnload( void ) {
+void AudioMusicUnload( void ) {
   if (!Opts_UsingSound()) return;
 
   if ( defaultMusic )
@@ -72,9 +70,15 @@
  * it will be stopped and unloaded
  * Note: loops == -1 means forever
  */
-void audioMusicPlay( Mix_Music *musicData, int loops ) { 
+void AudioMusicPlay( Mix_Music *musicData, int loops ) { 
   if (!Opts_UsingSound()) return;
 
-  audioMusicUnload();        
+  AudioMusicUnload(); 
   Mix_PlayMusic( musicData, loops );
 }
+
+bool IsPlayingMusic()
+{
+    return (defaultMusic != NULL);
+}
+

Modified: branches/commonification/tuxmath/trunk/src/campaign.c
===================================================================
--- branches/commonification/tuxmath/trunk/src/campaign.c	2009-08-03 00:34:32 UTC (rev 1332)
+++ branches/commonification/tuxmath/trunk/src/campaign.c	2009-08-03 00:35:40 UTC (rev 1333)
@@ -169,22 +169,22 @@
 
 
   char* sprites[] = {
-    "sprites/tux_helmet_yellowd.png",
-    "sprites/tux_helmet_greend.png",
-    "sprites/tux_helmet_blued.png",
-    "sprites/tux_helmet_redd.png",
-    "sprites/tux_helmet_blackd.png"
+    DATA_PREFIX "/images/sprites/tux_helmet_yellowd.png",
+    DATA_PREFIX "/images/sprites/tux_helmet_greend.png",
+    DATA_PREFIX "/images/sprites/tux_helmet_blued.png",
+    DATA_PREFIX "/images/sprites/tux_helmet_redd.png",
+    DATA_PREFIX "/images/sprites/tux_helmet_blackd.png"
   };
 
-  SDL_Surface* icon = NULL;
+  SDL_Surface* icon = LoadSpriteOfBoundingBox(sprites[stage],
+                        IMG_REGULAR | IMG_NOT_REQUIRED,
+                        0.1 * screen->w, 0.1 * screen->h);
   SDL_Rect textarea = screen->clip_rect;
-  SDL_Surface* loadedsprite = LoadImage(sprites[stage], IMG_REGULAR|IMG_NOT_REQUIRED);
 
 
 
-  if (loadedsprite) //stretch the tiny sprite to 3x
+  if (icon) //stretch the tiny sprite to 3x
   {
-    icon = zoom(loadedsprite, loadedsprite->w*3, loadedsprite->h*3);
     textarea.x = icon->w;
     textarea.y = icon->h;
     textarea.w = screen->w - icon->w;
@@ -203,7 +203,6 @@
 
   DEBUGMSG(debug_game, "Finished briefing\n");
 
-  SDL_FreeSurface(loadedsprite);
   SDL_FreeSurface(icon);
 }
 

Modified: branches/commonification/tuxmath/trunk/src/factoroids.c
===================================================================
--- branches/commonification/tuxmath/trunk/src/factoroids.c	2009-08-03 00:34:32 UTC (rev 1332)
+++ branches/commonification/tuxmath/trunk/src/factoroids.c	2009-08-03 00:35:40 UTC (rev 1333)
@@ -439,8 +439,8 @@
   /* NOTE - optimization code moved into LoadBkgd() so rest of program     */
   /* can take advantage of it - DSB                                             */
 
-  scaled_bkgd = LoadBkgd("factoroids/gbstars.png", fs_res_x, fs_res_y);
-  bkgd = LoadBkgd("factoroids/gbstars.png", win_res_x, win_res_y);
+  scaled_bkgd = LoadBkgd(DATA_PREFIX "/images/factoroids/gbstars.png", fs_res_x, fs_res_y);
+  bkgd = LoadBkgd(DATA_PREFIX "/images/factoroids/gbstars.png", win_res_x, win_res_y);
 
   if (bkgd == NULL || scaled_bkgd == NULL)
   {
@@ -729,7 +729,7 @@
 		  tuxship.hurt=1;
 		  tuxship.hurt_count=50;
 		  FF_destroy_asteroid(i, tuxship.xspeed, tuxship.yspeed);
-		  playsound(SND_EXPLOSION);
+		  PlaySound(sounds[SND_EXPLOSION]);
 			 
 		}
 	      }
@@ -1537,7 +1537,7 @@
 	laser[i].destx = laser[i].x + (int)(ux * smin);
 	laser[i].desty = laser[i].y + (int)(uy * smin);
 	FF_destroy_asteroid(zapIndex,2*ux,2*uy);
-	playsound(SND_SIZZLE);
+	PlaySound(sounds[SND_SIZZLE]);
 
 	if (floor((float)score/100) < floor((float)(score+num)/100))
 	  tuxship.lives++;
@@ -1830,7 +1830,7 @@
     digits[1] = digits[2];
     digits[2] = key - SDLK_0;
     tux_pressing = 1;
-    playsound(SND_SHIELDSDOWN);
+    PlaySound(sounds[SND_SHIELDSDOWN]);
   }
   else if (key >= SDLK_KP0 && key <= SDLK_KP9)
   {
@@ -1839,7 +1839,7 @@
     digits[1] = digits[2];
     digits[2] = key - SDLK_KP0;
     tux_pressing = 1;
-    playsound(SND_SHIELDSDOWN);
+    PlaySound(sounds[SND_SHIELDSDOWN]);
   }
   /* support for negative answer input DSB */
   else if ((key == SDLK_MINUS || key == SDLK_KP_MINUS))
@@ -1848,7 +1848,7 @@
     /* allow player to make answer negative: */
     neg_answer_picked = 1;
     tux_pressing = 1;
-    playsound(SND_SHIELDSDOWN);
+    PlaySound(sounds[SND_SHIELDSDOWN]);
   }
   else if ((key == SDLK_PLUS || key == SDLK_KP_PLUS))
          //&& MC_AllowNegatives())  /* do nothing unless neg answers allowed */
@@ -1856,7 +1856,7 @@
     /* allow player to make answer positive: */
     neg_answer_picked = 0;
     tux_pressing = 1;
-    playsound(SND_SHIELDSDOWN);
+    PlaySound(sounds[SND_SHIELDSDOWN]);
   }
   else if (key == SDLK_BACKSPACE ||
            key == SDLK_CLEAR ||
@@ -1867,7 +1867,7 @@
     digits[1] = 0;
     digits[2] = 0;
     tux_pressing = 1;
-    playsound(SND_SHIELDSDOWN);
+    PlaySound(sounds[SND_SHIELDSDOWN]);
   }
  	else if (key == SDLK_RETURN ||
         	   key == SDLK_KP_ENTER ||
@@ -1875,7 +1875,7 @@
  	 {
 	       shoot_pressed = 1;
                doing_answer = 1;
-	       playsound(SND_LASER);
+	       PlaySound(sounds[SND_LASER]);
   }
 
 

Modified: branches/commonification/tuxmath/trunk/src/fileops_media.c
===================================================================
--- branches/commonification/tuxmath/trunk/src/fileops_media.c	2009-08-03 00:34:32 UTC (rev 1332)
+++ branches/commonification/tuxmath/trunk/src/fileops_media.c	2009-08-03 00:35:40 UTC (rev 1333)
@@ -144,10 +144,13 @@
   "comets/bonus_cometex"
   };
 
+  char fn[PATH_MAX];
+
   /* Load static images: */
   for (i = 0; i < NUM_IMAGES; i++)
   {
-    images[i] = LoadImage(image_filenames[i], IMG_ALPHA);
+    sprintf(fn, "%s%s%s", DATA_PREFIX, "/images/", image_filenames[i]);
+    images[i] = LoadImage(fn, IMG_ALPHA);
 
     if (images[i] == NULL)
     {
@@ -163,7 +166,8 @@
   /* Load animated graphics: */
   for (i = 0; i < NUM_SPRITES; i++)
   {
-    sprites[i] = LoadSprite(sprite_filenames[i], IMG_ALPHA);
+    sprintf(fn, "%s%s%s", DATA_PREFIX, "/images/", image_filenames[i]);
+    sprites[i] = LoadSprite(fn, IMG_ALPHA);
 
     if (sprites[i] == NULL)
     {

Modified: branches/commonification/tuxmath/trunk/src/game.c
===================================================================
--- branches/commonification/tuxmath/trunk/src/game.c	2009-08-03 00:34:32 UTC (rev 1332)
+++ branches/commonification/tuxmath/trunk/src/game.c	2009-08-03 00:35:40 UTC (rev 1333)
@@ -1163,8 +1163,8 @@
     laser.y1 = screen->h;
     laser.x2 = comets[lowest].x;
     laser.y2 = comets[lowest].y;
-    playsound(SND_LASER);
-    playsound(SND_SIZZLE);
+    PlaySound(sounds[SND_LASER]);
+    PlaySound(sounds[SND_SIZZLE]);
 
     /* Record data for feedback */
     if (Opts_UseFeedback())
@@ -1207,8 +1207,8 @@
     laser.y1 = screen->h;
     laser.x2 = laser.x1;
     laser.y2 = 0;
-    playsound(SND_LASER);
-    playsound(SND_BUZZ);
+    PlaySound(sounds[SND_LASER]);
+    PlaySound(sounds[SND_BUZZ]);
 
     if ((rand() % 10) < 5)
       tux_img = IMG_TUX_DRAT;
@@ -1246,7 +1246,7 @@
 
   if (level_start_wait == LEVEL_START_WAIT_START / 4)
   {
-    playsound(SND_ALARM);
+    PlaySound(sounds[SND_ALARM]);
   }
 }
 
@@ -1263,7 +1263,7 @@
     }
     while (tux_img == old_tux_img);
 
-    playsound(SND_CLICK);
+    PlaySound(sounds[SND_CLICK]);
   }
 
   /* If Tux is being animated, show the animation: */
@@ -1361,18 +1361,18 @@
         {
           cities[this_city].status = CITY_EXPLODING;
           if (Opts_GetGlobalOpt(USE_IGLOOS)) {
-            playsound(SND_IGLOO_SIZZLE);
+            PlaySound(sounds[SND_IGLOO_SIZZLE]);
             cities[this_city].counter = IGLOO_SWITCH_START;
             steam[this_city].status = STEAM_ON;
             steam[this_city].counter = STEAM_START;
           }
           else {
             if (cities[comets[i].city].hits_left == 2) {
-              playsound(SND_SHIELDSDOWN);
+              PlaySound(sounds[SND_SHIELDSDOWN]);
               cities[this_city].counter = 1;  /* Will act immediately */
             }
             else {
-              playsound(SND_EXPLOSION);
+              PlaySound(sounds[SND_EXPLOSION]);
               cities[this_city].counter = CITY_EXPL_START;
             }
           }
@@ -1410,7 +1410,7 @@
             DEBUGMSG(debug_game, "bonus_comet_counter is now %d\n",bonus_comet_counter);
           }
           if (comets[i].bonus && comets[i].zapped) {
-            playsound(SND_EXTRA_LIFE);
+            PlaySound(sounds[SND_EXTRA_LIFE]);
             extra_life_earned = 1;
             DEBUGMSG(debug_game, "Extra life earned!");
           }
@@ -2346,7 +2346,7 @@
 
   last_bkgd = i;
 
-  sprintf(fname, "backgrounds/%d.jpg", i);
+  sprintf(fname, "%s/images/backgrounds/%d.jpg", DATA_PREFIX, i);
 
   if (bkgd != NULL)
   {
@@ -2582,7 +2582,7 @@
   if (bonus_comet_counter == 1) {
     bonus_comet_counter = 0;
     comets[found].bonus = 1;
-    playsound(SND_BONUS_COMET);
+    PlaySound(sounds[SND_BONUS_COMET]);
 
     DEBUGMSG(debug_game, "Created bonus comet");
   }

Modified: branches/commonification/tuxmath/trunk/src/globals.h
===================================================================
--- branches/commonification/tuxmath/trunk/src/globals.h	2009-08-03 00:34:32 UTC (rev 1332)
+++ branches/commonification/tuxmath/trunk/src/globals.h	2009-08-03 00:35:40 UTC (rev 1333)
@@ -42,23 +42,27 @@
 extern int debug_status;
 
 /* bitmasks for debugging options (declared in options.c) */
+#ifndef HAVE_LIBT4KCOMMON
+extern const int debug_loaders;
+extern const int debug_menu;
+extern const int debug_menu_parser;
+extern const int debug_sdl;
+#endif
 extern const int debug_setup;
 extern const int debug_fileops;
-extern const int debug_loaders;
 extern const int debug_titlescreen;
-extern const int debug_menu;
-extern const int debug_menu_parser;
 extern const int debug_game;
 extern const int debug_factoroids;
 extern const int debug_lan;
 extern const int debug_mathcards;
-extern const int debug_sdl;
 extern const int debug_lessons;
 extern const int debug_highscore;
 extern const int debug_options;
 extern const int debug_convert_utf;
 extern const int debug_multiplayer;
+#ifndef HAVE_LIBT4KCOMMON
 extern const int debug_all;
+#endif
 
 /* debug macros */
 #define DEBUGCODE(mask) if((mask) & debug_status)

Modified: branches/commonification/tuxmath/trunk/src/highscore.c
===================================================================
--- branches/commonification/tuxmath/trunk/src/highscore.c	2009-08-03 00:34:32 UTC (rev 1332)
+++ branches/commonification/tuxmath/trunk/src/highscore.c	2009-08-03 00:35:40 UTC (rev 1333)
@@ -79,7 +79,7 @@
           if (inRect(stop_rect, event.button.x, event.button.y ))
           {
             finished = 1;
-            playsound(SND_TOCK);
+            PlaySound(sounds[SND_TOCK]);
           }
 
           /* "Left" button - go to previous page: */
@@ -90,7 +90,7 @@
               diff_level--;
               if (Opts_GetGlobalOpt(MENU_SOUND))
               {
-                playsound(SND_TOCK);
+                PlaySound(sounds[SND_TOCK]);
               }
             }
           }
@@ -103,7 +103,7 @@
               diff_level++;
               if (Opts_GetGlobalOpt(MENU_SOUND))
               {
-                playsound(SND_TOCK);
+                PlaySound(sounds[SND_TOCK]);
               }
             }
           }
@@ -114,7 +114,7 @@
         case SDL_KEYDOWN:
         {
           finished = 1;
-          playsound(SND_TOCK);
+          PlaySound(sounds[SND_TOCK]);
         }
       }
     }
@@ -374,7 +374,7 @@
           if (inRect(stop_rect, event.button.x, event.button.y ))
           {
             finished = 1;
-            playsound(SND_TOCK);
+            PlaySound(sounds[SND_TOCK]);
             break;
           }
         }
@@ -389,7 +389,7 @@
             case SDLK_KP_ENTER:
             {
               finished = 1;
-              playsound(SND_TOCK);
+              PlaySound(sounds[SND_TOCK]);
               break;
             }
             case SDLK_BACKSPACE:

Modified: branches/commonification/tuxmath/trunk/src/loaders.c
===================================================================
--- branches/commonification/tuxmath/trunk/src/loaders.c	2009-08-03 00:34:32 UTC (rev 1332)
+++ branches/commonification/tuxmath/trunk/src/loaders.c	2009-08-03 00:35:40 UTC (rev 1333)
@@ -22,6 +22,7 @@
 */
 
 #include "loaders.h"
+#include "globals.h"
 #include "SDL_extras.h"
 
 #ifdef HAVE_RSVG
@@ -46,20 +47,9 @@
 
 
 
-/* check to see if file exists, if so return true */
-// int checkFile( const char *file ) {
-//         static struct stat fileStats;
-//         fileStats.st_mode = 0;
-//         stat( file, &fileStats );
-//         return (S_IFREG & fileStats.st_mode);
-// }
-
-
-/* Returns 1 if valid file, 2 if valid dir, 0 if neither: */
 int check_file(const char* file)
 {
   FILE* fp = NULL;
-  DIR* dp = NULL;
 
   if (!file)
   {
@@ -69,14 +59,6 @@
 
   DEBUGMSG(debug_loaders, "check_file(): checking: %s\n", file);
 
-  dp = opendir(file);
-  if (dp)
-  {
-    DEBUGMSG(debug_loaders, "check_file(): Opened successfully as DIR\n");
-    closedir(dp);
-    return 2;
-  }
-
   fp = fopen(file, "r");
   if (fp)
   {
@@ -189,20 +171,20 @@
   }
 
   /* set color masks */
-  Rmask = screen->format->Rmask;
-  Gmask = screen->format->Gmask;
-  Bmask = screen->format->Bmask;
-  if(screen->format->Amask == 0)
+  Rmask = GetScreen()->format->Rmask;
+  Gmask = GetScreen()->format->Gmask;
+  Bmask = GetScreen()->format->Bmask;
+  if(GetScreen()->format->Amask == 0)
     /* find a free byte to use for Amask */
     Amask = ~(Rmask | Gmask | Bmask);
   else
-    Amask = screen->format->Amask;
+    Amask = GetScreen()->format->Amask;
 
   DEBUGMSG(debug_loaders, "render_svg_from_handle(): color masks: R=%u, G=%u, B=%u, A=%u\n",
         Rmask, Gmask, Bmask, Amask);
 
   dest = SDL_CreateRGBSurface(SDL_SWSURFACE | SDL_SRCALPHA,
-        width, height, screen->format->BitsPerPixel, Rmask, Gmask, Bmask, Amask);
+        width, height, GetScreen()->format->BitsPerPixel, Rmask, Gmask, Bmask, Amask);
 
   SDL_LockSurface(dest);
   temp_surf = cairo_image_surface_create_for_data(dest->pixels,
@@ -298,7 +280,7 @@
   /* run loader depending on file extension */
 
   /* add path prefix */
-  snprintf(fn, PATH_MAX, "%s/images/%s", DATA_PREFIX, file_name);
+  snprintf(fn, PATH_MAX, "%s", file_name);
   fn_len = strlen(fn);
 
   if(strcmp(fn + fn_len - 4, ".svg"))
@@ -465,6 +447,7 @@
   return final_pic;
 }
 
+
 sprite* LoadSprite(const char* name, int mode)
 {
   return LoadScaledSprite(name, mode, -1, -1);
@@ -488,7 +471,7 @@
 
 #ifdef HAVE_RSVG
   /* check if SVG sprite file is present */
-  sprintf(fn, "%s/images/%s.svg", DATA_PREFIX, name);
+  sprintf(fn, "%s.svg", name);
   if(1 == check_file(fn))
   {
     if(proportional)
@@ -607,8 +590,7 @@
   Mix_Chunk* tempChunk = NULL;
   char fn[PATH_MAX];
 
-//    sprintf(fn , "%s/sounds/%s", realPath[i], datafile);
-  sprintf(fn , "%s/sounds/%s", DATA_PREFIX, datafile);
+  sprintf(fn , "%s", datafile);
   tempChunk = Mix_LoadWAV(fn);
   if (!tempChunk)
   {
@@ -623,7 +605,7 @@
   char fn[PATH_MAX];
   Mix_Music* tempMusic = NULL;
 
-  sprintf( fn , "%s/sounds/%s", DATA_PREFIX, datafile );
+  sprintf(fn, "%s", datafile);
   if (1 != check_file(fn))
   {
     fprintf(stderr, "LoadMusic(): %s not found\n\n", fn);

Modified: branches/commonification/tuxmath/trunk/src/menu.c
===================================================================
--- branches/commonification/tuxmath/trunk/src/menu.c	2009-08-03 00:34:32 UTC (rev 1332)
+++ branches/commonification/tuxmath/trunk/src/menu.c	2009-08-03 00:35:40 UTC (rev 1333)
@@ -1,5 +1,5 @@
 /*
-  menu.c
+  t4k-menu.c
 
   Functions responsible for loading, parsing and displaying game menu.
 
@@ -8,54 +8,82 @@
 
   Author: Boleslaw Kulbabinski <bkulbabinski at gmail.com>, (C) 2009
 
-  (Functions responsible for running specific activities
-   are moved from titlescreen.c)
-
   Copyright: See COPYING file that comes with this distribution.
 */
 
+#include "globals.h"
 #include "menu.h"
+#include "loaders.h"
 #include "SDL_extras.h"
 #include "titlescreen.h"
-#include "highscore.h"
-#include "factoroids.h"
-#include "credits.h"
-#include "multiplayer.h"
-#include "mathcards.h"
-#include "campaign.h"
-#include "game.h"
-#include "options.h"
-#include "fileops.h"
-#include "setup.h"
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
+struct mNode {
+  struct mNode* parent;
 
-#ifndef HAVE_LIBT4KCOMMON
+  char* title;
+  int font_size;
 
-/* create string array of activities' names */
-#define X(name) #name
-char* activities[] = { ACTIVITIES };
-#undef X
+  char* icon_name;
+  sprite* icon;
 
-/* we may use a few separate menu trees */
-typedef enum {
-  MENU_MAIN,
-  MENU_LESSONS,
-  MENU_LOGIN,
-  N_OF_MENUS
-} MenuType;
+  SDL_Rect button_rect;
+  SDL_Rect icon_rect;
+  SDL_Rect text_rect;
 
+  /* submenu_size = 0 if no submenu */
+  int submenu_size;
+  struct mNode** submenu;
+
+  /* these fields are used only if submenu_size = 0 */
+  int activity;
+  int param;
+
+  /* these fields are used only if submenu_size > 0 */
+  bool show_title;
+  int entries_per_screen;
+  int first_entry;
+};
+
+typedef struct mNode MenuNode;
+
+/* titlescreen & menu frame rate */
+#define MAX_FPS                    30
+/* number of "real" frames per one sprite frame */
+#define SPRITE_FRAME_DELAY         6
+
+
+#define QUIT -2
+#define STOP -1
+#define RUN_MAIN_MENU -3
+
+int n_of_activities;
+char** activities;
+
+char* data_prefix;
+
+Mix_Chunk* snd_click;
+Mix_Chunk* snd_hover;
+char* music_path;
+
+#define N_OF_MENUS 10
 MenuNode* menus[N_OF_MENUS];
 
+/* font size used in current resolution */
+int curr_font_size;
+
+/* buffer size used when reading attributes or names */
+const int buf_size = 512;
+
 /* actions available while viewing the menu */
 enum { NONE, CLICK, PAGEUP, PAGEDOWN, STOP_ESC, RESIZED };
 
 /* stop button, left and right arrow positions do not
    depend on currently displayed menu */
-SDL_Rect menu_rect, stop_rect, prev_rect, next_rect;
+SDL_Rect menu_rect, stop_rect, prev_rect, next_rect, menu_title_rect;
 SDL_Surface *stop_button, *prev_arrow, *next_arrow, *prev_gray, *next_gray;
 
 /*TODO: move these constants into a config file (maybe together with
@@ -64,25 +92,16 @@
 const float stop_pos[4] = {0.94, 0.0, 0.06, 0.06};
 const float prev_pos[4] = {0.87, 0.93, 0.06, 0.06};
 const float next_pos[4] = {0.94, 0.93, 0.06, 0.06};
-const char* stop_path = "status/stop.svg";
-const char* prev_path = "status/left.svg";
-const char* next_path = "status/right.svg";
-const char* prev_gray_path = "status/left_gray.svg";
-const char* next_gray_path = "status/right_gray.svg";
+const char* stop_path = "/images/status/stop.svg";
+const char* prev_path = "/images/status/left.svg";
+const char* next_path = "/images/status/right.svg";
+const char* prev_gray_path = "/images/status/left_gray.svg";
+const char* next_gray_path = "/images/status/right_gray.svg";
 const float button_gap = 0.2, text_h_gap = 0.4, text_w_gap = 0.5, button_radius = 0.27;
 const int min_font_size = 8, default_font_size = 20, max_font_size = 40;
 
-/* font size used in current resolution */
-int curr_font_size;
 
-/* menu title rect */
-SDL_Rect menu_title_rect;
 
-/* buffer size used when reading attributes or names */
-const int buf_size = 128;
-
-
-
 /* local functions */
 MenuNode*       create_empty_node();
 char*           get_attribute_name(const char* token);
@@ -90,24 +109,32 @@
 void            read_attributes(FILE* xml_file, MenuNode* node);
 MenuNode*       load_menu_from_file(FILE* xml_file, MenuNode* parent);
 void            free_menu(MenuNode* menu);
-MenuNode*       create_one_level_menu(int items, char** item_names, char* title, char* trailer);
 
-int             handle_activity(int act, int param);
-int             run_academy(void);
-int             run_arcade(int choice);
-int             run_custom_game(void);
-void            run_multiplayer(int mode, int difficulty);
-int             run_factoroids(int choice);
-
-int             run_menu(MenuNode* menu, bool return_choice);
 SDL_Surface**   render_buttons(MenuNode* menu, bool selected);
-void            prerender_menu(MenuNode* menu);
 char*           find_longest_text(MenuNode* menu, int* length);
 void            set_font_size();
-void            prerender_all();
+void            prerender_menu(MenuNode* menu);
 
 
+/* initialization of menu module */
+void SetActivitiesList(int num, char** acts)
+{
+  n_of_activities = num;
+  activities = acts;
+}
 
+void SetMenuSounds(char* mus_path, Mix_Chunk* click, Mix_Chunk* hover)
+{
+  snd_click = click;
+  snd_hover = hover;
+  music_path = mus_path;
+}
+
+void SetImagePathPrefix(char* pref)
+{
+  data_prefix = pref;
+}
+
 /*
   functions responsible for parsing menu files
   and creating menu trees
@@ -160,7 +187,9 @@
       node->icon_name = strdup(attr_val);
     else if(strcmp(attr_name, "run") == 0)
     {
-      for(i = 0; i < N_OF_ACTIVITIES; i++)
+      if(strcmp(attr_val, "RUN_MAIN_MENU") == 0)
+        node->activity = RUN_MAIN_MENU;
+      for(i = 0; i < n_of_activities; i++)
         if(strcmp(attr_val, activities[i]) == 0)
           node->activity = i;
     }
@@ -252,9 +281,9 @@
   }
 }
 
-/* create a simple one-level menu without sprites.
-   all given strings are copied */
-MenuNode* create_one_level_menu(int items, char** item_names, char* title, char* trailer)
+/* create a simple one-level menu.
+   All given strings are copied */
+void CreateOneLevelMenu(int index, int items, char** item_names, char** sprite_names, char* title, char* trailer)
 {
   MenuNode* menu = create_empty_node();
   int i;
@@ -270,6 +299,8 @@
   {
     menu->submenu[i] = create_empty_node();
     menu->submenu[i]->title = strdup(item_names[i]);
+    if(sprite_names && sprite_names[i])
+      menu->submenu[i]->icon_name = strdup(sprite_names[i]);
     menu->submenu[i]->activity = i;
   }
 
@@ -280,291 +311,22 @@
     menu->submenu[items]->activity = items;
   }
 
-  return menu;
+  menus[index] = menu;
 }
 
-/*
-  handlers for specific game activities
-*/
 
-/* return QUIT if user decided to quit the application while running an activity
-   return 0 otherwise */
-int handle_activity(int act, int param)
-{
-  DEBUGMSG(debug_menu, "entering handle_activity()\n");
 
-  switch(act)
-  {
-    case RUN_CAMPAIGN:
-      audioMusicUnload();
-      start_campaign();
-      if (Opts_GetGlobalOpt(MENU_MUSIC)) //Turn menu music back on
-        audioMusicLoad( "tuxi.ogg", -1 );
-      break;
-
-    case RUN_ACADEMY:
-      if(run_academy() == QUIT)
-        return QUIT;
-      break;
-
-    case RUN_ARCADE:
-      run_arcade(param);
-      break;
-
-    case RUN_CUSTOM:
-      run_custom_game();
-      break;
-
-    case RUN_HALL_OF_FAME:
-      DisplayHighScores(CADET_HIGH_SCORE);
-      break;
-
-    case RUN_SCORE_SWEEP:
-      run_multiplayer(0, param);
-      break;
-
-    case RUN_ELIMINATION:
-      run_multiplayer(1, param);
-      break;
-
-    case RUN_HELP:
-      Opts_SetHelpMode(1);
-      Opts_SetDemoMode(0);
-      audioMusicUnload();
-      game();
-      if (Opts_GetGlobalOpt(MENU_MUSIC)) //Turn menu music back on
-        audioMusicLoad( "tuxi.ogg", -1 );
-      Opts_SetHelpMode(0);
-      break;
-
-    case RUN_FACTORS:
-      run_factoroids(0);
-      break;
-
-    case RUN_FRACTIONS:
-      run_factoroids(1);
-      break;
-
-    case RUN_DEMO:
-      if(read_named_config_file("demo"))
-      {
-        audioMusicUnload();
-        game();
-        if (Opts_GetGlobalOpt(MENU_MUSIC))
-          audioMusicLoad( "tuxi.ogg", -1 );
-      }
-      else
-        fprintf(stderr, "\nCould not find demo config file\n");
-      break;
-
-    case RUN_INFO:
-      ShowMessage(_("TuxMath is free and open-source!"),
-                  _("You can help make it better by reporting problems,"),
-                  _("suggesting improvements, or adding code."),
-                  _("Discuss the future at tuxmath-devel at lists.sourceforge.net"));
-      break;
-
-    case RUN_CREDITS:
-      credits();
-      break;
-
-    case RUN_QUIT:
-      return QUIT;
-  }
-
-  return 0;
-}
-
-int run_academy(void)
-{
-  int chosen_lesson = -1;
-
-  chosen_lesson = run_menu(menus[MENU_LESSONS], true);
-  while (chosen_lesson >= 0)
-  {
-    if (Opts_GetGlobalOpt(MENU_SOUND))
-      playsound(SND_POP);
-
-    /* Re-read global settings first in case any settings were */
-    /* clobbered by other lesson or arcade games this session: */
-    read_global_config_file();
-    /* Now read the selected file and play the "mission": */
-    if (read_named_config_file(lesson_list_filenames[chosen_lesson]))
-    {
-      audioMusicUnload();
-      game();
-
-      /* If successful, display Gold Star for this lesson! */
-      if (MC_MissionAccomplished())
-      {
-        lesson_list_goldstars[chosen_lesson] = 1;
-       /* and save to disk: */
-        write_goldstars();
-      }
-
-      if (Opts_GetGlobalOpt(MENU_MUSIC)) //Turn menu music back on
-        {audioMusicLoad("tuxi.ogg", -1);}
-    }
-    else  // Something went wrong - could not read lesson config file:
-    {
-      fprintf(stderr, "\nCould not find file: %s\n", lesson_list_filenames[chosen_lesson]);
-      chosen_lesson = -1;
-    }
-    // Let the user choose another lesson; start with the screen and
-    // selection that we ended with
-    chosen_lesson = run_menu(menus[MENU_LESSONS], true);
-  }
-  return chosen_lesson;
-}
-
-int run_arcade(int choice)
-{
-  const char* arcade_config_files[5] =
-    {"arcade/space_cadet",
-     "arcade/scout",
-     "arcade/ranger",
-     "arcade/ace",
-     "arcade/commando"
-    };
-
-  const int arcade_high_score_tables[5] =
-    {CADET_HIGH_SCORE,
-     SCOUT_HIGH_SCORE,
-     RANGER_HIGH_SCORE,
-     ACE_HIGH_SCORE,
-     COMMANDO_HIGH_SCORE
-    };
-
-  int hs_table;
-
-  if (choice < NUM_MATH_COMMAND_LEVELS) {
-    // Play arcade game
-    if (read_named_config_file(arcade_config_files[choice]))
-    {
-      audioMusicUnload();
-      game();
-      if (Opts_GetGlobalOpt(MENU_MUSIC))
-        audioMusicLoad( "tuxi.ogg", -1 );
-      /* See if player made high score list!                        */
-      read_high_scores();  /* Update, in case other users have added to it */
-      hs_table = arcade_high_score_tables[choice];
-      if (check_score_place(hs_table, Opts_LastScore()) < HIGH_SCORES_SAVED)
-      {
-        char player_name[HIGH_SCORE_NAME_LENGTH * 3];
-
-        /* Get name from player: */
-        HighScoreNameEntry(&player_name[0]);
-        insert_score(player_name, hs_table, Opts_LastScore());
-        /* Show the high scores. Note the user will see his/her */
-        /* achievement even if (in the meantime) another player */
-        /* has in fact already bumped this score off the table. */
-        DisplayHighScores(hs_table);
-        /* save to disk: */
-        /* See "On File Locking" in fileops.c */
-        append_high_score(choice,Opts_LastScore(),&player_name[0]);
-
-        DEBUGCODE(debug_titlescreen)
-          print_high_scores(stderr);
-      }
-    }
-    else {
-      fprintf(stderr, "\nCould not find %s config file\n",arcade_config_files[choice]);
-    }
-  }
-  return 0;
-}
-
-int run_custom_game(void)
-{
-  const char *s1, *s2, *s3, *s4;
-  s1 = _("Edit 'options' file in your home directory");
-  s2 = _("to create customized game!");
-  s3 = _("Press a key or click your mouse to start game.");
-  s4 = _("See README.txt for more information");
-  ShowMessage(s1, s2, s3, s4);
-
-  if (read_user_config_file()) {
-    audioMusicUnload();
-    game();
-    write_user_config_file();
-
-    if (Opts_GetGlobalOpt(MENU_MUSIC))
-      audioMusicLoad( "tuxi.ogg", -1 );
-  }
-
-  return 0;
-}
-
-void run_multiplayer(int mode, int difficulty)
-{
-  int nplayers = 0;
-  char npstr[HIGH_SCORE_NAME_LENGTH * 3];
-
-  while (nplayers <= 0 || nplayers > MAX_PLAYERS)
-  {
-    NameEntry(npstr, _("How many kids are playing?"),
-                     _("(Between 2 and 4 players)"));
-    nplayers = atoi(npstr);
-  }
-
-  mp_set_parameter(PLAYERS, nplayers);
-  mp_set_parameter(MODE, mode);
-  mp_set_parameter(DIFFICULTY, difficulty);
-  audioMusicUnload();
-  mp_run_multiplayer();
-	if (Opts_GetGlobalOpt(MENU_MUSIC))
-    audioMusicLoad( "tuxi.ogg", -1 );
-}
-
-int run_factoroids(int choice)
-{
-  const int factoroids_high_score_tables[2] =
-    {FACTORS_HIGH_SCORE, FRACTIONS_HIGH_SCORE};
-  int hs_table;
-
-  audioMusicUnload();
-  if(choice == 0)
-    factors();
-  else
-    fractions();
-
-	if (Opts_GetGlobalOpt(MENU_MUSIC))
-    audioMusicLoad( "tuxi.ogg", -1 );
-
-	hs_table = factoroids_high_score_tables[choice];
-	if (check_score_place(hs_table, Opts_LastScore()) < HIGH_SCORES_SAVED){
-	  char player_name[HIGH_SCORE_NAME_LENGTH * 3];
-	  /* Get name from player: */
-	  HighScoreNameEntry(&player_name[0]);
-	  insert_score(player_name, hs_table, Opts_LastScore());
-	  /* Show the high scores. Note the user will see his/her */
-	  /* achievement even if (in the meantime) another player */
-	  /* has in fact already bumped this score off the table. */
-	  DisplayHighScores(hs_table);
-	  /* save to disk: */
-	  /* See "On File Locking" in fileops.c */
-	  append_high_score(hs_table,Opts_LastScore(),&player_name[0]);
-    DEBUGCODE(debug_titlescreen)
-	    print_high_scores(stderr);
-	}
-  else {
-	  fprintf(stderr, "\nCould not find config file\n");
-  }
-
-  return 0;
-}
-
 /* Display the menu and run the event loop.
    if return_choice = true then return chosen value instead of
    running handle_activity()
    this function is a modified copy of choose_menu_item() */
-int run_menu(MenuNode* root, bool return_choice)
+int RunMenu(int index, bool return_choice, void (*draw_background)(), int (*handle_event)(SDL_Event*), void (*handle_animations)(), int (*handle_activity)(int, int))
 {
   SDL_Surface** menu_item_unselected = NULL;
   SDL_Surface** menu_item_selected = NULL;
   SDL_Surface* title_surf;
   SDL_Event event;
-  MenuNode* menu = root;
+  MenuNode* menu = menus[index];
   MenuNode* tmp_node;
 
   SDL_Rect tmp_rect;
@@ -586,7 +348,7 @@
   {
     DEBUGMSG(debug_menu, "run_menu(): drawing whole new menu page\n");
 
-    DrawTitleScreen();
+    draw_background();
     /* render buttons for current menu page */
     menu_item_unselected = render_buttons(menu, false);
     menu_item_selected = render_buttons(menu, true);
@@ -596,26 +358,26 @@
     for(i = 0; i < items; i++)
     {
       if(loc == i)
-        SDL_BlitSurface(menu_item_selected[i], NULL, screen, &menu->submenu[menu->first_entry + i]->button_rect);
+        SDL_BlitSurface(menu_item_selected[i], NULL, GetScreen(), &menu->submenu[menu->first_entry + i]->button_rect);
       else
-        SDL_BlitSurface(menu_item_unselected[i], NULL, screen, &menu->submenu[menu->first_entry + i]->button_rect);
+        SDL_BlitSurface(menu_item_unselected[i], NULL, GetScreen(), &menu->submenu[menu->first_entry + i]->button_rect);
       if(menu->submenu[menu->first_entry + i]->icon)
-        SDL_BlitSurface(menu->submenu[menu->first_entry + i]->icon->default_img, NULL, screen, &menu->submenu[menu->first_entry + i]->icon_rect);
+        SDL_BlitSurface(menu->submenu[menu->first_entry + i]->icon->default_img, NULL, GetScreen(), &menu->submenu[menu->first_entry + i]->icon_rect);
     }
 
-    SDL_BlitSurface(stop_button, NULL, screen, &stop_rect);
+    SDL_BlitSurface(stop_button, NULL, GetScreen(), &stop_rect);
 
     if(menu->entries_per_screen < menu->submenu_size)
     {
       /* display arrows */
       if(menu->first_entry > 0)
-        SDL_BlitSurface(prev_arrow, NULL, screen, &prev_rect);
+        SDL_BlitSurface(prev_arrow, NULL, GetScreen(), &prev_rect);
       else
-        SDL_BlitSurface(prev_gray, NULL, screen, &prev_rect);
+        SDL_BlitSurface(prev_gray, NULL, GetScreen(), &prev_rect);
       if(menu->first_entry + items < menu->submenu_size)
-        SDL_BlitSurface(next_arrow, NULL, screen, &next_rect);
+        SDL_BlitSurface(next_arrow, NULL, GetScreen(), &next_rect);
       else
-        SDL_BlitSurface(next_gray, NULL, screen, &next_rect);
+        SDL_BlitSurface(next_gray, NULL, GetScreen(), &next_rect);
     }
 
     if(menu->show_title)
@@ -623,10 +385,10 @@
       menu_title_rect = menu->submenu[0]->button_rect;
       menu_title_rect.y = menu_rect.y - menu_title_rect.h;
       title_surf = BlackOutline(_(menu->title), curr_font_size, &red);
-      SDL_BlitSurface(title_surf, NULL, screen, &menu_title_rect);
+      SDL_BlitSurface(title_surf, NULL, GetScreen(), &menu_title_rect);
       SDL_FreeSurface(title_surf);
     }
-    SDL_UpdateRect(screen, 0, 0, 0, 0);
+    SDL_UpdateRect(GetScreen(), 0, 0, 0, 0);
 
     SDL_WM_GrabInput(SDL_GRAB_OFF);
 
@@ -658,8 +420,8 @@
             {
               if (inRect(menu->submenu[menu->first_entry + i]->button_rect, event.motion.x, event.motion.y))
               {
-                if (Opts_GetGlobalOpt(MENU_SOUND) && old_loc != i)
-                  playsound(SND_TOCK);
+                if(snd_hover)
+                  PlaySound(snd_hover);
                 loc = i;
                 break;   /* from for loop */
               }
@@ -667,34 +429,35 @@
 
             /* "Left" button - make click if button active: */
             if(inRect(prev_rect, event.motion.x, event.motion.y)
-               && menu->first_entry > 0 && Opts_GetGlobalOpt(MENU_SOUND))
+               && menu->first_entry > 0)
             {
               if(click_flag)
               {
-                playsound(SND_TOCK);
+                if(snd_hover)
+                  PlaySound(snd_hover);
                 click_flag = 0;
               }
             }
 
             /* "Right" button - make click if button active: */
             else if(inRect(next_rect, event.motion.x, event.motion.y)
-               && menu->first_entry + items < menu->submenu_size
-               && Opts_GetGlobalOpt(MENU_SOUND))
+               && menu->first_entry + items < menu->submenu_size)
             {
               if(click_flag)
               {
-                playsound(SND_TOCK);
+                if(snd_hover)
+                  PlaySound(snd_hover);
                 click_flag = 0;
               }
             }
 
             /* "stop" button */
-            else if (inRect(stop_rect, event.motion.x, event.motion.y )
-               && Opts_GetGlobalOpt(MENU_SOUND))
+            else if (inRect(stop_rect, event.motion.x, event.motion.y ))
             {
               if(click_flag)
               {
-                playsound(SND_TOCK);
+                if(snd_hover)
+                  PlaySound(snd_hover);
                 click_flag = 0;
               }
             }
@@ -713,8 +476,8 @@
               if (inRect(menu->submenu[menu->first_entry + i]->button_rect, event.motion.x, event.motion.y))
               {
                 // Play sound if loc is being changed:
-                if (Opts_GetGlobalOpt(MENU_SOUND))
-                  playsound(SND_POP);
+                if(snd_click)
+                  PlaySound(snd_click);
                 loc = i;
                 action = CLICK;
                 break;   /* from for loop */
@@ -725,8 +488,8 @@
             if (inRect(prev_rect, event.motion.x, event.motion.y)
                && menu->first_entry > 0)
             {
-              if (Opts_GetGlobalOpt(MENU_SOUND))
-                playsound(SND_POP);
+              if(snd_click)
+                PlaySound(snd_click);
               action = PAGEUP;
             }
 
@@ -734,16 +497,16 @@
             else if (inRect(next_rect, event.motion.x, event.motion.y )
                && menu->first_entry + items < menu->submenu_size)
             {
-              if (Opts_GetGlobalOpt(MENU_SOUND))
-                playsound(SND_POP);
+              if(snd_click)
+                PlaySound(snd_click);
               action = PAGEDOWN;
             }
 
             /* "Stop" button - go to main menu: */
             else if (inRect(stop_rect, event.button.x, event.button.y ))
             {
-              if (Opts_GetGlobalOpt(MENU_SOUND))
-                playsound(SND_POP);
+              if(snd_click)
+                PlaySound(snd_click);
               action = STOP_ESC;
             }
 
@@ -765,8 +528,8 @@
               case SDLK_SPACE:
               case SDLK_KP_ENTER:
               {
-                if (Opts_GetGlobalOpt(MENU_SOUND))
-                  playsound(SND_POP);
+                if(snd_click)
+                  PlaySound(snd_click);
                 action = CLICK;
                 break;
               }
@@ -775,8 +538,8 @@
               case SDLK_LEFT:
               case SDLK_PAGEUP:
               {
-                if (Opts_GetGlobalOpt(MENU_SOUND))
-                  playsound(SND_TOCK);
+                if(snd_click)
+                  PlaySound(snd_click);
                 if (menu->first_entry > 0)
                   action = PAGEUP;
                 break;
@@ -786,8 +549,8 @@
               case SDLK_RIGHT:
               case SDLK_PAGEDOWN:
               {
-                if (Opts_GetGlobalOpt(MENU_SOUND))
-                  playsound(SND_TOCK);
+                if(snd_click)
+                  PlaySound(snd_click);
                 if (menu->first_entry + items < menu->submenu_size)
                   action = PAGEDOWN;
                 break;
@@ -796,12 +559,12 @@
               /* Go up one entry, if present: */
               case SDLK_UP:
               {
-                if (Opts_GetGlobalOpt(MENU_SOUND))
-                  playsound(SND_TOCK);
+                if(snd_hover)
+                  PlaySound(snd_hover);
                 if (loc > 0)
                   loc--;
                 else if (menu->submenu_size <= menu->entries_per_screen) 
-                  loc = menu->submenu_size - 1;  // wrap around if only 1 screen
+                  loc = menu->submenu_size - 1;  // wrap around if only 1 GetScreen()
                 else if (menu->first_entry > 0)
                 {
                   loc = menu->entries_per_screen - 1;
@@ -812,12 +575,12 @@
 
               case SDLK_DOWN:
               {
-                if (Opts_GetGlobalOpt(MENU_SOUND))
-                  playsound(SND_TOCK);
+                if(snd_hover)
+                  PlaySound(snd_hover);
                 if (loc + 1 < min(menu->submenu_size, menu->entries_per_screen))
                   loc++;
                 else if (menu->submenu_size <= menu->entries_per_screen) 
-                  loc = 0;  // wrap around if only 1 screen
+                  loc = 0;  // wrap around if only 1 GetScreen()
                 else if (menu->first_entry + menu->entries_per_screen < menu->submenu_size)
                 {
                   loc = 0;
@@ -826,50 +589,7 @@
                 break;
               }
 
-              /* Change window size (used only to debug) */
-              case SDLK_F5:
-              case SDLK_F6:
-              case SDLK_F7:
-              case SDLK_F8:
-              {
-                /* these keys are available only if in debug mode */
-                DEBUGCODE(debug_titlescreen | debug_menu)
-                {
-                  switch(event.key.keysym.sym)
-                  {
-                    case SDLK_F5:
-                    {
-                      /* decrease screen width */
-                      ChangeWindowSize(win_res_x - 50, win_res_y);
-                      break;
-                    }
-                    case SDLK_F6:
-                    {
-                      /* increase screen width */
-                      ChangeWindowSize(win_res_x + 50, win_res_y);
-                      break;
-                    }
-                    case SDLK_F7:
-                    {
-                      /* decrease screen height */
-                      ChangeWindowSize(win_res_x, win_res_y - 50);
-                      break;
-                    }
-                    case SDLK_F8:
-                    {
-                      /* increase screen height */
-                      ChangeWindowSize(win_res_x, win_res_y + 50);
-                      break;
-                    }
-                    default:
-                      break;
-                  }
-                  action = RESIZED;
-                }
-                break;
-              }
-
-              /* Toggle screen mode: */
+              /* Toggle GetScreen() mode: */
               case SDLK_F10:
               {
                 SwitchScreenMode();
@@ -880,15 +600,13 @@
               /* Toggle menu music: */
               case SDLK_F11:
               {
-                if (Opts_GetGlobalOpt(MENU_MUSIC))
+                if(IsPlayingMusic())
                 {
-                  audioMusicUnload( );
-                  Opts_SetGlobalOpt(MENU_MUSIC, 0);
+                  AudioMusicUnload();
                 }
-                else
+                else if(music_path)
                 {
-                  Opts_SetGlobalOpt(MENU_MUSIC, 1);
-                  audioMusicLoad("tuxi.ogg", -1);
+                  AudioMusicLoad(music_path, -1);
                 }
                 break;
               }
@@ -908,26 +626,26 @@
           if(old_loc >= 0 && old_loc < items)
           {
             tmp_rect = menu->submenu[old_loc + menu->first_entry]->button_rect;
-            SDL_BlitSurface(menu_item_unselected[old_loc], NULL, screen, &tmp_rect);
+            SDL_BlitSurface(menu_item_unselected[old_loc], NULL, GetScreen(), &tmp_rect);
             if(menu->submenu[menu->first_entry + old_loc]->icon)
-              SDL_BlitSurface(menu->submenu[menu->first_entry + old_loc]->icon->default_img, NULL, screen, &menu->submenu[menu->first_entry + old_loc]->icon_rect);
-            SDL_UpdateRect(screen, tmp_rect.x, tmp_rect.y, tmp_rect.w, tmp_rect.h);
+              SDL_BlitSurface(menu->submenu[menu->first_entry + old_loc]->icon->default_img, NULL, GetScreen(), &menu->submenu[menu->first_entry + old_loc]->icon_rect);
+            SDL_UpdateRect(GetScreen(), tmp_rect.x, tmp_rect.y, tmp_rect.w, tmp_rect.h);
           }
           if(loc >= 0 && loc < items)
           {
             tmp_rect = menu->submenu[loc + menu->first_entry]->button_rect;
-            SDL_BlitSurface(menu_item_selected[loc], NULL, screen, &tmp_rect);
+            SDL_BlitSurface(menu_item_selected[loc], NULL, GetScreen(), &tmp_rect);
             if(menu->submenu[menu->first_entry + loc]->icon)
             {
-              SDL_BlitSurface(menu->submenu[menu->first_entry + loc]->icon->default_img, NULL, screen, &menu->submenu[menu->first_entry + loc]->icon_rect);
+              SDL_BlitSurface(menu->submenu[menu->first_entry + loc]->icon->default_img, NULL, GetScreen(), &menu->submenu[menu->first_entry + loc]->icon_rect);
               menu->submenu[menu->first_entry + loc]->icon->cur = 0;
             }
-            SDL_UpdateRect(screen, tmp_rect.x, tmp_rect.y, tmp_rect.w, tmp_rect.h);
+            SDL_UpdateRect(GetScreen(), tmp_rect.x, tmp_rect.y, tmp_rect.w, tmp_rect.h);
           }
           old_loc = loc;
         }
 
-        if(HandleTitleScreenEvents(&event))
+        if(handle_event(&event))
           stop = true;
 
         switch(action)
@@ -935,7 +653,7 @@
           case RESIZED:
             RenderTitleScreen();
             menu->first_entry = 0;
-            prerender_all();
+            PrerenderAll();
             stop = true;
             break;
 
@@ -960,7 +678,7 @@
                   if(tmp_node->activity == RUN_MAIN_MENU)
                   {
                     /* go back to the root of this menu */
-                    menu = root;
+                    menu = menus[index];
                   }
                   else
                   {
@@ -1017,14 +735,14 @@
         tmp_sprite = menu->submenu[menu->first_entry + loc]->icon;
         if(tmp_sprite)
         {
-          SDL_BlitSurface(menu_item_selected[loc], NULL, screen, &menu->submenu[menu->first_entry + loc]->icon_rect);
-          SDL_BlitSurface(tmp_sprite->frame[tmp_sprite->cur], NULL, screen, &menu->submenu[menu->first_entry + loc]->icon_rect);
-          UpdateRect(screen, &menu->submenu[menu->first_entry + loc]->icon_rect);
+          SDL_BlitSurface(menu_item_selected[loc], NULL, GetScreen(), &menu->submenu[menu->first_entry + loc]->icon_rect);
+          SDL_BlitSurface(tmp_sprite->frame[tmp_sprite->cur], NULL, GetScreen(), &menu->submenu[menu->first_entry + loc]->icon_rect);
+          UpdateRect(GetScreen(), &menu->submenu[menu->first_entry + loc]->icon_rect);
           NextFrame(tmp_sprite);
         }
       }
 
-      HandleTitleScreenAnimations();
+      handle_animations();
 
       /* Wait so we keep frame rate constant: */
       frame_now = SDL_GetTicks();
@@ -1070,7 +788,7 @@
                                           32,
                                           rmask, gmask, bmask, amask);
 
-    SDL_BlitSurface(screen, &curr_rect, menu_items[i], NULL);
+    SDL_BlitSurface(GetScreen(), &curr_rect, menu_items[i], NULL);
     /* button */
     if(selected)
       tmp_surf = CreateButton(curr_rect.w, curr_rect.h, button_radius * curr_rect.h, SEL_RGBA);
@@ -1155,7 +873,7 @@
 
     if(curr_node->icon_name)
     {
-      sprintf(filename, "sprites/%s", curr_node->icon_name);
+      sprintf(filename, "%s/images/sprites/%s", data_prefix, curr_node->icon_name);
       DEBUGMSG(debug_menu, "prerender_menu(): loading sprite %s for item #%d.\n", filename, i);
       curr_node->icon = LoadSpriteOfBoundingBox(filename, IMG_ALPHA, button_h, button_h);
     }
@@ -1166,6 +884,11 @@
   }
 }
 
+void PrerenderMenu(int index)
+{
+  prerender_menu(menus[index]);
+}
+
 char* find_longest_text(MenuNode* menu, int* length)
 {
   SDL_Surface* text = NULL;
@@ -1237,211 +960,74 @@
 
 /* prerender arrows, stop button and all non-NULL menus from menus[] array
    this function should be invoked after every resolution change */
-void prerender_all()
+void PrerenderAll()
 {
   int i;
+  char fn[PATH_MAX];
 
   SetRect(&menu_rect, menu_pos);
 
   SetRect(&stop_rect, stop_pos);
   if(stop_button)
     SDL_FreeSurface(stop_button);
-  stop_button = LoadImageOfBoundingBox(stop_path, IMG_ALPHA, stop_rect.w, stop_rect.h);
+  sprintf(fn, "%s%s", data_prefix, stop_path);
+  stop_button = LoadImageOfBoundingBox(fn, IMG_ALPHA, stop_rect.w, stop_rect.h);
   /* move button to the right */
-  stop_rect.x = screen->w - stop_button->w;
+  stop_rect.x = GetScreen()->w - stop_button->w;
 
   SetRect(&prev_rect, prev_pos);
   if(prev_arrow)
     SDL_FreeSurface(prev_arrow);
-  prev_arrow = LoadImageOfBoundingBox(prev_path, IMG_ALPHA, prev_rect.w, prev_rect.h);
+  sprintf(fn, "%s%s", data_prefix, prev_path);
+  prev_arrow = LoadImageOfBoundingBox(fn, IMG_ALPHA, prev_rect.w, prev_rect.h);
   if(prev_gray)
     SDL_FreeSurface(prev_gray);
-  prev_gray = LoadImageOfBoundingBox(prev_gray_path, IMG_ALPHA, prev_rect.w, prev_rect.h);
+  sprintf(fn, "%s%s", data_prefix, prev_gray_path);
+  prev_gray = LoadImageOfBoundingBox(fn, IMG_ALPHA, prev_rect.w, prev_rect.h);
   /* move button to the right */
   prev_rect.x += prev_rect.w - prev_arrow->w;
 
   SetRect(&next_rect, next_pos);
   if(next_arrow)
     SDL_FreeSurface(next_arrow);
-  next_arrow = LoadImageOfBoundingBox(next_path, IMG_ALPHA, next_rect.w, next_rect.h);
+  sprintf(fn, "%s%s", data_prefix, next_path);
+  next_arrow = LoadImageOfBoundingBox(fn, IMG_ALPHA, next_rect.w, next_rect.h);
   if(next_gray)
     SDL_FreeSurface(next_gray);
-  next_gray = LoadImageOfBoundingBox(next_gray_path, IMG_ALPHA, next_rect.w, next_rect.h);
+  sprintf(fn, "%s%s", data_prefix, next_gray_path);
+  next_gray = LoadImageOfBoundingBox(fn, IMG_ALPHA, next_rect.w, next_rect.h);
 
   set_font_size();
 
   for(i = 0; i < N_OF_MENUS; i++)
     if(menus[i])
-      prerender_menu(menus[i]);
+      PrerenderMenu(i);
 }
 
-/* load menu trees from disk and prerender them */
-void LoadMenus(void)
+void LoadMenu(int index, const char* file_name)
 {
   FILE* menu_file = NULL;
-  int i;
 
-  for(i = 0; i < N_OF_MENUS; i++)
-    menus[i] = NULL;
+  if(menus[index])
+  {
+    free_menu(menus[index]);
+    menus[index] = NULL;
+  }
 
-  /* main menu */
-  menu_file = fopen(DATA_PREFIX "/menus/main_menu.xml", "r");
+  menu_file = fopen(file_name, "r");
   if(menu_file == NULL)
   {
-    DEBUGMSG(debug_menu, "LoadMenus(): Could not load main menu file !\n");
+    DEBUGMSG(debug_menu, "LoadMenu(): Could not load %s !\n", file_name);
   }
   else
   {
-    menus[MENU_MAIN] = load_menu_from_file(menu_file, NULL);
+    menus[index] = load_menu_from_file(menu_file, NULL);
     fclose(menu_file);
   }
-
-  prerender_all();
 }
 
 
 
-/* create login menu tree, run it and set the user home directory
-   -1 indicates that the user wants to quit without logging in,
-    0 indicates that a choice has been made. */
-int RunLoginMenu(void)
-{
-  int n_login_questions = 0;
-  char **user_login_questions = NULL;
-  char *title = NULL;
-  int n_users = 0;
-  char **user_names = NULL;
-  int chosen_login = -1;
-  int level;
-  int i;
-  char *trailer_quit = "Quit";
-  char *trailer_back = "Back";
-  char *trailer = NULL;
-  SDLMod mod;
-
-  DEBUGMSG(debug_menu, "Entering RunLoginMenu()");
-  // Check for & read user_login_questions file
-  n_login_questions = read_user_login_questions(&user_login_questions);
-
-  // Check for & read user_menu_entries file
-  n_users = read_user_menu_entries(&user_names);
-
-  if (n_users == 0)
-    return 0;   // a quick exit, there's only one user
-
-  // Check for a highscores file
-  if (high_scores_found_in_user_dir())
-    set_high_score_path();
-
-  level = 0;
-
-  if (n_login_questions > 0)
-    title = user_login_questions[0];
-
-  menus[MENU_LOGIN] = create_one_level_menu(n_users, user_names, title, trailer_quit);
-
-  while (n_users) {
-    // Get the user choice
-    set_font_size();
-    prerender_menu(menus[MENU_LOGIN]);
-    chosen_login = run_menu(menus[MENU_LOGIN], true);
-    // Determine whether there were any modifier (CTRL) keys pressed
-    mod = SDL_GetModState();
-    if (chosen_login < 0 || chosen_login == n_users) {
-      // User pressed escape or selected Quit/Back, handle by quitting
-      // or going up a level
-      if (level == 0) {
-        // We are going to quit without logging in.
-        // Clean up memory (prob. not necessary, but prevents Valgrind errors!)
-        for (i = 0; i < n_login_questions; i++)
-          free(user_login_questions[i]);
-        free(user_login_questions);
-        for (i = 0; i < n_users; i++)
-          free(user_names[i]);
-        free(user_names);
-        free_menu(menus[MENU_LOGIN]);
-        menus[MENU_LOGIN] = NULL;
-        return -1;
-      }
-      else {
-        // Go back up one level of the directory tree
-        user_data_dirname_up();
-        level--;
-      }
-    }
-    else {
-      // User chose an entry, set it up
-      user_data_dirname_down(user_names[chosen_login]);
-      level++;
-    }
-    // Check for a highscores file
-    if (high_scores_found_in_user_dir())
-      set_high_score_path();
-    // Free the entries from the previous menu
-    for (i = 0; i < n_users; i++)
-      free(user_names[i]);
-    free(user_names);
-    user_names = NULL;
-    // If the CTRL key was pressed, choose this as the identity, even
-    // if there is a lower level to the hierarchy
-    if (mod & KMOD_CTRL)
-      break;
-    // Set the title appropriately for the next menu
-    if (level < n_login_questions)
-      title = user_login_questions[level];
-    else
-      title = NULL;
-    if (level == 0)
-      trailer = trailer_quit;
-    else
-      trailer = trailer_back;
-    // Check to see if there are more choices to be made
-    n_users = read_user_menu_entries(&user_names);
-    free_menu(menus[MENU_LOGIN]);
-    menus[MENU_LOGIN] = create_one_level_menu(n_users, user_names, title, trailer);
-  }
-
-  // The user home directory is set, clean up remaining memory
-  for (i = 0; i < n_login_questions; i++)
-    free(user_login_questions[i]);
-  free(user_login_questions);
-  free_menu(menus[MENU_LOGIN]);
-  menus[MENU_LOGIN] = NULL;
-
-  // Signal success
-  return 0;
-}
-
-/* run main menu. If this function ends it means that tuxmath is going to quit */
-void RunMainMenu(void)
-{
-  int i;
-  MenuNode* tmp_node;
-  DEBUGMSG(debug_menu, "Entering RunMainMenu()\n");
-
-   /* lessons menu */
-  DEBUGMSG(debug_menu, "LoadMenus(): Generating lessons submenu. (%d lessons)\n", num_lessons);
-
-  tmp_node = create_empty_node();
-  tmp_node->submenu_size = num_lessons;
-  tmp_node->submenu = (MenuNode**) malloc(num_lessons * sizeof(MenuNode*));
-  for(i = 0; i < num_lessons; i++)
-  {
-    tmp_node->submenu[i] = create_empty_node();
-    tmp_node->submenu[i]->icon_name = strdup(lesson_list_goldstars[i] ? "goldstar" : "no_goldstar");
-    tmp_node->submenu[i]->title = (char*) malloc( (strlen(lesson_list_titles[i]) + 1) * sizeof(char) );
-    strcpy(tmp_node->submenu[i]->title, lesson_list_titles[i]);
-    tmp_node->submenu[i]->activity = i;
-  }
-  menus[MENU_LESSONS] = tmp_node;
-  set_font_size();
-  prerender_menu(menus[MENU_LESSONS]);
-  //prerender_all();
-
-  run_menu(menus[MENU_MAIN], false);
-  DEBUGMSG(debug_menu, "Leaving RunMainMenu()\n");
-}
-
 /* free all loaded menu trees */
 void UnloadMenus(void)
 {
@@ -1477,4 +1063,3 @@
   DEBUGMSG(debug_menu, "leaving UnloadMenus()\n");
 }
 
-#endif //HAVE_LIBT4KCOMMON

Modified: branches/commonification/tuxmath/trunk/src/menu.h
===================================================================
--- branches/commonification/tuxmath/trunk/src/menu.h	2009-08-03 00:34:32 UTC (rev 1332)
+++ branches/commonification/tuxmath/trunk/src/menu.h	2009-08-03 00:35:40 UTC (rev 1333)
@@ -15,92 +15,23 @@
 #ifndef MENU_H
 #define MENU_H
 
-#ifndef HAVE_LIBT4KCOMMON
+#include "tuxmath.h"
 
-#include "SDL.h"
-#include "globals.h"
-#include "loaders.h"
-
-/* titlescreen & menu frame rate */
-#define MAX_FPS                    30
-/* number of "real" frames per one sprite frame */
-#define SPRITE_FRAME_DELAY         6
-
-/* these are all menu choices that are available in tuxmath.
-   By using a define we can create both an enum and
-   a string array without writing these names twice */
-#define QUIT -2
-#define STOP -1
-
-#define ACTIVITIES \
-  X( RUN_QUIT ),\
-  X( RUN_ACADEMY ),\
-  X( RUN_CAMPAIGN ),\
-  X( RUN_ARCADE ),\
-  X( RUN_CUSTOM ),\
-  X( RUN_MAIN_MENU ),\
-  X( RUN_LAN_HOST ),\
-  X( RUN_LAN_JOIN ),\
-  X( RUN_SCORE_SWEEP ),\
-  X( RUN_ELIMINATION ),\
-  X( RUN_FACTORS ),\
-  X( RUN_FRACTIONS ),\
-  X( RUN_HELP ),\
-  X( RUN_DEMO ),\
-  X( RUN_INFO ),\
-  X( RUN_CREDITS ),\
-  X( RUN_HALL_OF_FAME ),\
-  X( RUN_SPACE_CADET ),\
-  X( RUN_SCOUT ),\
-  X( RUN_RANGER ),\
-  X( RUN_ACE ),\
-  X( RUN_COMMANDO ),\
-  X( N_OF_ACTIVITIES )  /* this one has to be the last one */
-
-/* create enum */
-#define X(name) name
-enum { ACTIVITIES };
-#undef X
-
-struct mNode {
-  struct mNode* parent;
-
-  char* title;
-  int font_size;
-
-  char* icon_name;
-  sprite* icon;
-
-  SDL_Rect button_rect;
-  SDL_Rect icon_rect;
-  SDL_Rect text_rect;
-
-  /* submenu_size = 0 if no submenu */
-  int submenu_size;
-  struct mNode** submenu;
-
-  /* these fields are used only if submenu_size = 0 */
-  int activity;
-  int param;
-
-  /* these fields are used only if submenu_size > 0 */
-  bool show_title;
-  int entries_per_screen;
-  int first_entry;
-};
-
-typedef struct mNode MenuNode;
-
-/* used also by highscore.c */
+#ifndef HAVE_LIBT4KCOMMON
 extern SDL_Rect menu_rect, stop_rect, prev_rect, next_rect;
 extern SDL_Surface *stop_button, *prev_arrow, *next_arrow, *prev_gray, *next_gray;
 
-/* global functions */
-void LoadMenus(void);
-int RunLoginMenu(void);
-void RunMainMenu(void);
-void UnloadMenus(void);
+void            SetActivitiesList(int num, char** acts);
+void            SetMenuSounds(char* mus_path, Mix_Chunk* click, Mix_Chunk* hover);
+void            SetImagePathPrefix(char* pref);
 
+void            CreateOneLevelMenu(int index, int items, char** item_names, char** sprite_names, char* title, char* trailer);
+int             RunMenu(int index, bool return_choice, void (*draw_background)(), int (*handle_event)(SDL_Event*), void (*handle_animations)(), int (*handle_activity)(int, int));
+void            PrerenderMenu(int index);
+void            PrerenderAll();
+void            LoadMenu(int index, const char* file_name);
+void            UnloadMenus(void);
+
 #endif // HAVE_LIBT4KCOMMON
 #endif // MENU_H
 

Modified: branches/commonification/tuxmath/trunk/src/options.c
===================================================================
--- branches/commonification/tuxmath/trunk/src/options.c	2009-08-03 00:34:32 UTC (rev 1332)
+++ branches/commonification/tuxmath/trunk/src/options.c	2009-08-03 00:35:40 UTC (rev 1333)
@@ -42,23 +42,27 @@
 int debug_status;
 
 /* bitmasks for debugging options */
-const int debug_setup          = 1 << 0;
-const int debug_fileops        = 1 << 1;
-const int debug_loaders        = 1 << 2;
-const int debug_titlescreen    = 1 << 3;
-const int debug_menu           = 1 << 4;
-const int debug_menu_parser    = 1 << 5;
-const int debug_game           = 1 << 6;
-const int debug_factoroids     = 1 << 7;
-const int debug_lan            = 1 << 8;
-const int debug_mathcards      = 1 << 9;
-const int debug_sdl            = 1 << 10;
+#ifndef HAVE_LIBT4KCOMMON
+const int debug_loaders        = 1 << 0;
+const int debug_menu           = 1 << 1;
+const int debug_menu_parser    = 1 << 2;
+const int debug_sdl            = 1 << 3;
+#endif
+const int debug_setup          = 1 << 4;
+const int debug_fileops        = 1 << 5;
+const int debug_titlescreen    = 1 << 6;
+const int debug_game           = 1 << 7;
+const int debug_factoroids     = 1 << 8;
+const int debug_lan            = 1 << 9;
+const int debug_mathcards      = 1 << 10;
 const int debug_lessons        = 1 << 11;
 const int debug_highscore      = 1 << 12;
 const int debug_options        = 1 << 13;
 const int debug_convert_utf    = 1 << 14;
 const int debug_multiplayer    = 1 << 15;
+#ifndef HAVE_LIBT4KCOMMON
 const int debug_all            = ~0;
+#endif
 
 /* extern'd constants */
 

Modified: branches/commonification/tuxmath/trunk/src/setup.c
===================================================================
--- branches/commonification/tuxmath/trunk/src/setup.c	2009-08-03 00:34:32 UTC (rev 1332)
+++ branches/commonification/tuxmath/trunk/src/setup.c	2009-08-03 00:35:40 UTC (rev 1333)
@@ -58,7 +58,6 @@
 /* Global data used in setup.c:              */
 /* (These are now 'extern'd in "tuxmath.h") */
 
-SDL_Surface* screen;
 SDL_Surface* images[NUM_IMAGES];
 sprite* sprites[NUM_SPRITES];
 /* Need special handling to generate flipped versions of images. This

Modified: branches/commonification/tuxmath/trunk/src/titlescreen.c
===================================================================
--- branches/commonification/tuxmath/trunk/src/titlescreen.c	2009-08-03 00:34:32 UTC (rev 1332)
+++ branches/commonification/tuxmath/trunk/src/titlescreen.c	2009-08-03 00:35:40 UTC (rev 1333)
@@ -24,12 +24,61 @@
 
 #include "tuxmath.h"
 #include "options.h"
+#include "game.h"
+#include "multiplayer.h"
+#include "highscore.h"
+#include "credits.h"
+#include "factoroids.h"
+#include "mathcards.h"
+#include "campaign.h"
 #include "fileops.h"
 #include "setup.h"
 #include "loaders.h"
 #include "SDL_extras.h"
 #include "menu.h"
 
+
+/* these are all menu choices that are available in tuxmath.
+   By using a define we can create both an enum and
+   a string array without writing these names twice */
+#define QUIT -2
+#define STOP -1
+
+#define ACTIVITIES \
+  X( RUN_QUIT ),\
+  X( RUN_ACADEMY ),\
+  X( RUN_CAMPAIGN ),\
+  X( RUN_ARCADE ),\
+  X( RUN_CUSTOM ),\
+  X( RUN_MAIN_MENU ),\
+  X( RUN_LAN_HOST ),\
+  X( RUN_LAN_JOIN ),\
+  X( RUN_SCORE_SWEEP ),\
+  X( RUN_ELIMINATION ),\
+  X( RUN_FACTORS ),\
+  X( RUN_FRACTIONS ),\
+  X( RUN_HELP ),\
+  X( RUN_DEMO ),\
+  X( RUN_INFO ),\
+  X( RUN_CREDITS ),\
+  X( RUN_HALL_OF_FAME ),\
+  X( RUN_SPACE_CADET ),\
+  X( RUN_SCOUT ),\
+  X( RUN_RANGER ),\
+  X( RUN_ACE ),\
+  X( RUN_COMMANDO ),\
+  X( N_OF_ACTIVITIES )  /* this one has to be the last one */
+
+/* create enum */
+#define X(name) name
+enum { ACTIVITIES };
+#undef X
+#define X(name) #name
+char* activities[] = { ACTIVITIES };
+#undef X
+
+enum { MENU_MAIN, MENU_LESSONS, MENU_LOGIN };
+
 /* --- Data Structure for Dirty Blitting --- */
 SDL_Rect srcupdate[MAX_UPDATES];
 SDL_Rect dstupdate[MAX_UPDATES];
@@ -60,11 +109,11 @@
   (together with menu.c constants ? ) */
 const float title_pos[4] = {0.0, 0.0, 0.3, 0.25};
 const float tux_pos[4]   = {0.0, 0.6, 0.3, 0.4};
-const char* bkg_path     = "title/menu_bkg.jpg";
-const char* standby_path = "status/standby.svg";
-const char* title_path   = "title/title1.svg";
-const char* egg_path     = "title/egg.svg";
-const char* tux_path     = "tux/bigtux";
+const char* bkg_path     = DATA_PREFIX "/images/title/menu_bkg.jpg";
+const char* standby_path = DATA_PREFIX "/images/status/standby.svg";
+const char* title_path   = DATA_PREFIX "/images/title/title1.svg";
+const char* egg_path     = DATA_PREFIX "/images/title/egg.svg";
+const char* tux_path     = DATA_PREFIX "/images/tux/bigtux";
 /* beak coordinates relative to tux rect */
 const float beak_pos[4]  = {0.36, 0.21, 0.27, 0.14};
 
@@ -116,6 +165,16 @@
 }
 
 /* Local function prototypes: */
+int             handle_activity(int act, int param);
+int             run_academy(void);
+int             run_arcade(int choice);
+int             run_custom_game(void);
+void            run_multiplayer(int mode, int difficulty);
+int             run_factoroids(int choice);
+int             run_login_menu(void);
+void            run_main_menu(void);
+
+
 void NotImplemented(void);
 
 void free_titlescreen(void);
@@ -167,12 +226,15 @@
     SDL_BlitSurface(logo, NULL, screen, &logo_rect);
     SDL_UpdateRect(screen, 0, 0, 0, 0);
     /* Play "harp" greeting sound lifted from Tux Paint */
-    playsound(SND_HARP);
+    PlaySound(sounds[SND_HARP]);
     SDL_FreeSurface(logo);
   }
 
   /* load menus */
-  LoadMenus();
+  SetActivitiesList(N_OF_ACTIVITIES, activities);
+  SetImagePathPrefix(DATA_PREFIX);
+  LoadMenu(MENU_MAIN, DATA_PREFIX "/menus/main_menu.xml");
+  PrerenderAll();
 
   /* load backgrounds */
   fs_bkg = LoadBkgd(bkg_path, fs_res_x, fs_res_y);
@@ -275,15 +337,15 @@
   /* Start playing menu music if desired: */
   if (Opts_GetGlobalOpt(MENU_MUSIC))
   {
-    audioMusicLoad("tuxi.ogg", -1);
+    AudioMusicLoad("tuxi.ogg", -1);
   }
 
   /* If necessary, have the user log in */
-  if (RunLoginMenu() != -1) {
+  if (run_login_menu() != -1) {
     /* Finish parsing user options */
     initialize_options_user();
     /* Start the main menu */
-    RunMainMenu();
+    run_main_menu();
   }
 
   /* User has selected quit, clean up */
@@ -386,7 +448,7 @@
 /* handle titlescreen events (easter egg)
    this function should be called from event loops
    return 1 if events require full redraw */
-int HandleTitleScreenEvents(const SDL_Event* evt)
+int HandleTitleScreenEvents(SDL_Event* evt)
 {
   return handle_easter_egg(evt);
 }
@@ -436,7 +498,405 @@
 /*                                                         */
 /***********************************************************/
 
+/*
+  handlers for specific game activities
+*/
 
+/* return QUIT if user decided to quit the application while running an activity
+   return 0 otherwise */
+int handle_activity(int act, int param)
+{
+  DEBUGMSG(debug_menu, "entering handle_activity()\n");
+
+  switch(act)
+  {
+    case RUN_CAMPAIGN:
+      AudioMusicUnload();
+      start_campaign();
+      if (Opts_GetGlobalOpt(MENU_MUSIC)) //Turn menu music back on
+        AudioMusicLoad( "tuxi.ogg", -1 );
+      break;
+
+    case RUN_ACADEMY:
+      if(run_academy() == QUIT)
+        return QUIT;
+      break;
+
+    case RUN_ARCADE:
+      run_arcade(param);
+      break;
+
+    case RUN_CUSTOM:
+      run_custom_game();
+      break;
+
+    case RUN_HALL_OF_FAME:
+      DisplayHighScores(CADET_HIGH_SCORE);
+      break;
+
+    case RUN_SCORE_SWEEP:
+      run_multiplayer(0, param);
+      break;
+
+    case RUN_ELIMINATION:
+      run_multiplayer(1, param);
+      break;
+
+    case RUN_HELP:
+      Opts_SetHelpMode(1);
+      Opts_SetDemoMode(0);
+      AudioMusicUnload();
+      game();
+      if (Opts_GetGlobalOpt(MENU_MUSIC)) //Turn menu music back on
+        AudioMusicLoad( "tuxi.ogg", -1 );
+      Opts_SetHelpMode(0);
+      break;
+
+    case RUN_FACTORS:
+      run_factoroids(0);
+      break;
+
+    case RUN_FRACTIONS:
+      run_factoroids(1);
+      break;
+
+    case RUN_DEMO:
+      if(read_named_config_file("demo"))
+      {
+        AudioMusicUnload();
+        game();
+        if (Opts_GetGlobalOpt(MENU_MUSIC))
+          AudioMusicLoad( "tuxi.ogg", -1 );
+      }
+      else
+        fprintf(stderr, "\nCould not find demo config file\n");
+      break;
+
+    case RUN_INFO:
+      ShowMessage(_("TuxMath is free and open-source!"),
+                  _("You can help make it better by reporting problems,"),
+                  _("suggesting improvements, or adding code."),
+                  _("Discuss the future at tuxmath-devel at lists.sourceforge.net"));
+      break;
+
+    case RUN_CREDITS:
+      credits();
+      break;
+
+    case RUN_QUIT:
+      return QUIT;
+  }
+
+  return 0;
+}
+
+int run_academy(void)
+{
+  int chosen_lesson = -1;
+
+  chosen_lesson = RunMenu(MENU_LESSONS, true, DrawTitleScreen, HandleTitleScreenEvents, HandleTitleScreenAnimations, NULL);
+  while (chosen_lesson >= 0)
+  {
+    if (Opts_GetGlobalOpt(MENU_SOUND))
+      PlaySound(sounds[SND_POP]);
+
+    /* Re-read global settings first in case any settings were */
+    /* clobbered by other lesson or arcade games this session: */
+    read_global_config_file();
+    /* Now read the selected file and play the "mission": */
+    if (read_named_config_file(lesson_list_filenames[chosen_lesson]))
+    {
+      AudioMusicUnload();
+      game();
+
+      /* If successful, display Gold Star for this lesson! */
+      if (MC_MissionAccomplished())
+      {
+        lesson_list_goldstars[chosen_lesson] = 1;
+       /* and save to disk: */
+        write_goldstars();
+      }
+
+      if (Opts_GetGlobalOpt(MENU_MUSIC)) //Turn menu music back on
+        {AudioMusicLoad("tuxi.ogg", -1);}
+    }
+    else  // Something went wrong - could not read lesson config file:
+    {
+      fprintf(stderr, "\nCould not find file: %s\n", lesson_list_filenames[chosen_lesson]);
+      chosen_lesson = -1;
+    }
+    // Let the user choose another lesson; start with the screen and
+    // selection that we ended with
+    chosen_lesson = RunMenu(MENU_LESSONS, true, DrawTitleScreen, HandleTitleScreenEvents, HandleTitleScreenAnimations, NULL);
+  }
+  return chosen_lesson;
+}
+
+int run_arcade(int choice)
+{
+  const char* arcade_config_files[5] =
+    {"arcade/space_cadet",
+     "arcade/scout",
+     "arcade/ranger",
+     "arcade/ace",
+     "arcade/commando"
+    };
+
+  const int arcade_high_score_tables[5] =
+    {CADET_HIGH_SCORE,
+     SCOUT_HIGH_SCORE,
+     RANGER_HIGH_SCORE,
+     ACE_HIGH_SCORE,
+     COMMANDO_HIGH_SCORE
+    };
+
+  int hs_table;
+
+  if (choice < NUM_MATH_COMMAND_LEVELS) {
+    // Play arcade game
+    if (read_named_config_file(arcade_config_files[choice]))
+    {
+      AudioMusicUnload();
+      game();
+      if (Opts_GetGlobalOpt(MENU_MUSIC))
+        AudioMusicLoad( "tuxi.ogg", -1 );
+      /* See if player made high score list!                        */
+      read_high_scores();  /* Update, in case other users have added to it */
+      hs_table = arcade_high_score_tables[choice];
+      if (check_score_place(hs_table, Opts_LastScore()) < HIGH_SCORES_SAVED)
+      {
+        char player_name[HIGH_SCORE_NAME_LENGTH * 3];
+
+        /* Get name from player: */
+        HighScoreNameEntry(&player_name[0]);
+        insert_score(player_name, hs_table, Opts_LastScore());
+        /* Show the high scores. Note the user will see his/her */
+        /* achievement even if (in the meantime) another player */
+        /* has in fact already bumped this score off the table. */
+        DisplayHighScores(hs_table);
+        /* save to disk: */
+        /* See "On File Locking" in fileops.c */
+        append_high_score(choice,Opts_LastScore(),&player_name[0]);
+
+        DEBUGCODE(debug_titlescreen)
+          print_high_scores(stderr);
+      }
+    }
+    else {
+      fprintf(stderr, "\nCould not find %s config file\n",arcade_config_files[choice]);
+    }
+  }
+  return 0;
+}
+
+int run_custom_game(void)
+{
+  const char *s1, *s2, *s3, *s4;
+  s1 = _("Edit 'options' file in your home directory");
+  s2 = _("to create customized game!");
+  s3 = _("Press a key or click your mouse to start game.");
+  s4 = _("See README.txt for more information");
+  ShowMessage(s1, s2, s3, s4);
+
+  if (read_user_config_file()) {
+    AudioMusicUnload();
+    game();
+    write_user_config_file();
+
+    if (Opts_GetGlobalOpt(MENU_MUSIC))
+      AudioMusicLoad( "tuxi.ogg", -1 );
+  }
+
+  return 0;
+}
+
+void run_multiplayer(int mode, int difficulty)
+{
+  int nplayers = 0;
+  char npstr[HIGH_SCORE_NAME_LENGTH * 3];
+
+  while (nplayers <= 0 || nplayers > MAX_PLAYERS)
+  {
+    NameEntry(npstr, _("How many kids are playing?"),
+                     _("(Between 2 and 4 players)"));
+    nplayers = atoi(npstr);
+  }
+
+  mp_set_parameter(PLAYERS, nplayers);
+  mp_set_parameter(MODE, mode);
+  mp_set_parameter(DIFFICULTY, difficulty);
+  AudioMusicUnload();
+  mp_run_multiplayer();
+	if (Opts_GetGlobalOpt(MENU_MUSIC))
+    AudioMusicLoad( "tuxi.ogg", -1 );
+}
+
+int run_factoroids(int choice)
+{
+  const int factoroids_high_score_tables[2] =
+    {FACTORS_HIGH_SCORE, FRACTIONS_HIGH_SCORE};
+  int hs_table;
+
+  AudioMusicUnload();
+  if(choice == 0)
+    factors();
+  else
+    fractions();
+
+	if (Opts_GetGlobalOpt(MENU_MUSIC))
+    AudioMusicLoad( "tuxi.ogg", -1 );
+
+	hs_table = factoroids_high_score_tables[choice];
+	if (check_score_place(hs_table, Opts_LastScore()) < HIGH_SCORES_SAVED){
+	  char player_name[HIGH_SCORE_NAME_LENGTH * 3];
+	  /* Get name from player: */
+	  HighScoreNameEntry(&player_name[0]);
+	  insert_score(player_name, hs_table, Opts_LastScore());
+	  /* Show the high scores. Note the user will see his/her */
+	  /* achievement even if (in the meantime) another player */
+	  /* has in fact already bumped this score off the table. */
+	  DisplayHighScores(hs_table);
+	  /* save to disk: */
+	  /* See "On File Locking" in fileops.c */
+	  append_high_score(hs_table,Opts_LastScore(),&player_name[0]);
+    DEBUGCODE(debug_titlescreen)
+	    print_high_scores(stderr);
+	}
+  else {
+	  fprintf(stderr, "\nCould not find config file\n");
+  }
+
+  return 0;
+}
+
+
+/* create login menu tree, run it and set the user home directory
+   -1 indicates that the user wants to quit without logging in,
+    0 indicates that a choice has been made. */
+int run_login_menu(void)
+{
+  int n_login_questions = 0;
+  char **user_login_questions = NULL;
+  char *title = NULL;
+  int n_users = 0;
+  char **user_names = NULL;
+  int chosen_login = -1;
+  int level;
+  int i;
+  char *trailer_quit = "Quit";
+  char *trailer_back = "Back";
+  char *trailer = NULL;
+  SDLMod mod;
+
+  DEBUGMSG(debug_menu, "Entering RunLoginMenu()");
+  // Check for & read user_login_questions file
+  n_login_questions = read_user_login_questions(&user_login_questions);
+
+  // Check for & read user_menu_entries file
+  n_users = read_user_menu_entries(&user_names);
+
+  if (n_users == 0)
+    return 0;   // a quick exit, there's only one user
+
+  // Check for a highscores file
+  if (high_scores_found_in_user_dir())
+    set_high_score_path();
+
+  level = 0;
+
+  if (n_login_questions > 0)
+    title = user_login_questions[0];
+
+  CreateOneLevelMenu(MENU_LOGIN, n_users, user_names, NULL, title, trailer_quit);
+
+  while (n_users) {
+    // Get the user choice
+    PrerenderMenu(1);
+    chosen_login = RunMenu(MENU_LOGIN, true, DrawTitleScreen, HandleTitleScreenEvents, HandleTitleScreenAnimations, NULL);
+    // Determine whether there were any modifier (CTRL) keys pressed
+    mod = SDL_GetModState();
+    if (chosen_login < 0 || chosen_login == n_users) {
+      // User pressed escape or selected Quit/Back, handle by quitting
+      // or going up a level
+      if (level == 0) {
+        // We are going to quit without logging in.
+        // Clean up memory (prob. not necessary, but prevents Valgrind errors!)
+        for (i = 0; i < n_login_questions; i++)
+          free(user_login_questions[i]);
+        free(user_login_questions);
+        for (i = 0; i < n_users; i++)
+          free(user_names[i]);
+        free(user_names);
+        return -1;
+      }
+      else {
+        // Go back up one level of the directory tree
+        user_data_dirname_up();
+        level--;
+      }
+    }
+    else {
+      // User chose an entry, set it up
+      user_data_dirname_down(user_names[chosen_login]);
+      level++;
+    }
+    // Check for a highscores file
+    if (high_scores_found_in_user_dir())
+      set_high_score_path();
+    // Free the entries from the previous menu
+    for (i = 0; i < n_users; i++)
+      free(user_names[i]);
+    free(user_names);
+    user_names = NULL;
+    // If the CTRL key was pressed, choose this as the identity, even
+    // if there is a lower level to the hierarchy
+    if (mod & KMOD_CTRL)
+      break;
+    // Set the title appropriately for the next menu
+    if (level < n_login_questions)
+      title = user_login_questions[level];
+    else
+      title = NULL;
+    if (level == 0)
+      trailer = trailer_quit;
+    else
+      trailer = trailer_back;
+    // Check to see if there are more choices to be made
+    n_users = read_user_menu_entries(&user_names);
+    CreateOneLevelMenu(MENU_LOGIN, n_users, user_names, NULL, title, trailer);
+  }
+
+  // The user home directory is set, clean up remaining memory
+  for (i = 0; i < n_login_questions; i++)
+    free(user_login_questions[i]);
+  free(user_login_questions);
+
+  // Signal success
+  return 0;
+}
+
+/* run main menu. If this function ends it means that tuxmath is going to quit */
+void run_main_menu(void)
+{
+  int i;
+  char** sprite_names;
+  DEBUGMSG(debug_menu, "Entering RunMainMenu()\n");
+
+   /* lessons menu */
+  DEBUGMSG(debug_menu, "LoadMenus(): Generating lessons submenu. (%d lessons)\n", num_lessons);
+
+  sprite_names = (char**) malloc(num_lessons * sizeof(char*));
+  for(i = 0; i < num_lessons; i++)
+    sprite_names[i] = lesson_list_goldstars[i] ? "goldstar" : "no_goldstar";
+
+  CreateOneLevelMenu(MENU_MAIN, num_lessons, lesson_list_titles, sprite_names, NULL, NULL);
+  free(sprite_names);
+
+  PrerenderMenu(MENU_LESSONS);
+  RunMenu(MENU_MAIN, false, DrawTitleScreen, HandleTitleScreenEvents, HandleTitleScreenAnimations, handle_activity);
+  DEBUGMSG(debug_menu, "Leaving RunMainMenu()\n");
+}
+
 void free_titlescreen(void)
 {
   DEBUGMSG(debug_titlescreen, "Entering free_titlescreen()\n");
@@ -562,14 +1022,14 @@
           if (inRect(stop_rect, event.button.x, event.button.y ))
           {
             finished = 1;
-            playsound(SND_TOCK);
+            PlaySound(sounds[SND_TOCK]);
             break;
           }
         }
         case SDL_KEYDOWN:
         {
           finished = 1;
-          playsound(SND_TOCK);
+          PlaySound(sounds[SND_TOCK]);
         }
       }
     }

Modified: branches/commonification/tuxmath/trunk/src/titlescreen.h
===================================================================
--- branches/commonification/tuxmath/trunk/src/titlescreen.h	2009-08-03 00:34:32 UTC (rev 1332)
+++ branches/commonification/tuxmath/trunk/src/titlescreen.h	2009-08-03 00:35:40 UTC (rev 1333)
@@ -100,16 +100,17 @@
 void          TitleScreen(void);
 int           RenderTitleScreen(void);
 void          DrawTitleScreen(void);
-int           HandleTitleScreenEvents(const SDL_Event* evt);
+int           HandleTitleScreenEvents(SDL_Event* evt);
 void          HandleTitleScreenAnimations();
 void          ShowMessage(const char* str1, const char* str2, const char* str3, const char* str4);
 SDL_Surface*  current_bkg(); //appropriate background for current video mode
 
 
 /* in audio.c  (from tuxtype): */
-void          playsound(int snd);
-void          audioMusicLoad(char* musicFilename, int repeatQty);
-void          audioMusicUnload(void);
-void          audioMusicPlay(Mix_Music* musicData, int repeatQty);
+void          PlaySound(Mix_Chunk* sound);
+void          AudioMusicLoad(char* musicFilename, int repeatQty);
+void          AudioMusicUnload(void);
+void          AudioMusicPlay(Mix_Music* musicData, int repeatQty);
+bool          IsPlayingMusic();
 
 #endif //TITLESCREEN_H




More information about the Tux4kids-commits mailing list