[Tux4kids-commits] r1448 - in branches/commonification/tuxmath/trunk: data/images/status src

Bolesław Kulbabiński bolekk-guest at alioth.debian.org
Sun Aug 16 19:49:23 UTC 2009


Author: bolekk-guest
Date: 2009-08-16 19:49:23 +0000 (Sun, 16 Aug 2009)
New Revision: 1448

Modified:
   branches/commonification/tuxmath/trunk/data/images/status/stop.svg
   branches/commonification/tuxmath/trunk/src/audio.c
   branches/commonification/tuxmath/trunk/src/campaign.c
   branches/commonification/tuxmath/trunk/src/compiler.h
   branches/commonification/tuxmath/trunk/src/fileops_media.c
   branches/commonification/tuxmath/trunk/src/globals.h
   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/titlescreen.c
Log:
moved the latest versions of tux4kids-common files into TuxMath

Modified: branches/commonification/tuxmath/trunk/data/images/status/stop.svg
===================================================================
--- branches/commonification/tuxmath/trunk/data/images/status/stop.svg	2009-08-16 18:35:12 UTC (rev 1447)
+++ branches/commonification/tuxmath/trunk/data/images/status/stop.svg	2009-08-16 19:49:23 UTC (rev 1448)
@@ -9,8 +9,8 @@
    xmlns:xlink="http://www.w3.org/1999/xlink"
    xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
    xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
-   width="259.45691"
-   height="259.45691"
+   width="32.000011"
+   height="32.000011"
    id="svg2"
    sodipodi:version="0.32"
    inkscape:version="0.46"
@@ -64,9 +64,9 @@
      objecttolerance="10"
      inkscape:pageopacity="0.0"
      inkscape:pageshadow="2"
-     inkscape:zoom="0.35358141"
-     inkscape:cx="378.77863"
-     inkscape:cy="581.22201"
+     inkscape:zoom="2.8286513"
+     inkscape:cx="43.247149"
+     inkscape:cy="13.687266"
      inkscape:document-units="px"
      inkscape:current-layer="layer1"
      showgrid="false"
@@ -89,7 +89,7 @@
      inkscape:label="Layer 1"
      inkscape:groupmode="layer"
      id="layer1"
-     transform="translate(-19.774132,-58.953316)">
+     transform="translate(-19.774133,-286.41021)">
     <path
        sodipodi:type="arc"
        style="opacity:1;fill:url(#linearGradient3284);fill-opacity:1;fill-rule:nonzero;stroke:#d10000;stroke-width:15;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:2;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1"
@@ -98,10 +98,11 @@
        sodipodi:cy="188.68176"
        sodipodi:rx="122.22845"
        sodipodi:ry="122.22845"
-       d="M 271.73103,188.68176 A 122.22845,122.22845 0 1 1 27.274132,188.68176 A 122.22845,122.22845 0 1 1 271.73103,188.68176 z" />
+       d="M 271.73103,188.68176 A 122.22845,122.22845 0 1 1 27.274132,188.68176 A 122.22845,122.22845 0 1 1 271.73103,188.68176 z"
+       transform="matrix(0.1233346,0,0,0.1233346,17.335298,279.13923)" />
     <path
-       style="opacity:1;fill:#ffffff;fill-opacity:1;fill-rule:nonzero;stroke:#000000;stroke-width:3;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:1.89999998;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1"
-       d="M 92.57437,106.81218 L 65.499446,133.91626 L 121.42709,189.84391 L 65.499446,245.77156 L 92.57437,272.87564 L 148.50202,216.91884 L 204.4588,272.87564 L 231.53373,245.77156 L 175.60609,189.84391 L 231.53373,133.91626 L 204.4588,106.81218 L 148.50202,162.76898 L 92.57437,106.81218 z"
+       style="opacity:1;fill:#ffffff;fill-opacity:1;fill-rule:nonzero;stroke:#000000;stroke-width:0.37000367;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:1.89999998;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1"
+       d="M 28.752917,292.31286 L 25.413643,295.65573 L 32.311454,302.55354 L 25.413643,309.45135 L 28.752917,312.79422 L 35.650728,305.89282 L 42.552133,312.79422 L 45.891407,309.45135 L 38.993597,302.55354 L 45.891407,295.65573 L 42.552133,292.31286 L 35.650728,299.21427 L 28.752917,292.31286 z"
        id="rect3277" />
   </g>
 </svg>

Modified: branches/commonification/tuxmath/trunk/src/audio.c
===================================================================
--- branches/commonification/tuxmath/trunk/src/audio.c	2009-08-16 18:35:12 UTC (rev 1447)
+++ branches/commonification/tuxmath/trunk/src/audio.c	2009-08-16 19:49:23 UTC (rev 1448)
@@ -26,6 +26,8 @@
 #include "titlescreen.h"
 
 #ifndef HAVE_LIBT4KCOMMON
+static Mix_Music *default_music = NULL;
+
 void PlaySound(Mix_Chunk* sound)
 {
 #ifndef NOSOUND
@@ -34,14 +36,10 @@
 #endif
 }
 
-Mix_Music *defaultMusic = NULL; // holds music for audioMusicLoad/unload
-
-
-
-/* audioMusicLoad attempts to load and play the music file 
+/* audioMusicLoad attempts to load and play the music file
  * Note: loops == -1 means forever
  */
-void AudioMusicLoad(char *musicFilename, int loops)
+void AudioMusicLoad(char* music_path, int loops)
 {
   if (!Opts_UsingSound())
   {
@@ -49,38 +47,39 @@
   }
 
   AudioMusicUnload(); // make sure defaultMusic is clear
-  defaultMusic = LoadMusic(musicFilename);
-  Mix_PlayMusic(defaultMusic, loops);
+  default_music = LoadMusic(music_path);
+  Mix_PlayMusic(default_music, loops);
 }
 
-
 /* audioMusicUnload attempts to unload any music data that was
  * loaded using the audioMusicLoad function
  */
-void AudioMusicUnload( void ) {
+void AudioMusicUnload()
+{
   if (!Opts_UsingSound()) return;
 
-  if ( defaultMusic )
-    Mix_FreeMusic( defaultMusic );
+  if(default_music)
+    Mix_FreeMusic(default_music);
+  default_music = NULL;
+}
 
-  defaultMusic=NULL;
+bool IsPlayingMusic()
+{
+  return (default_music != NULL);
 }
 
-/* audioMusicPlay attempts to play the passed music data. 
+/* audioMusicPlay attempts to play the passed music data.
  * if a music file was loaded using the audioMusicLoad
  * 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(); 
-  Mix_PlayMusic( musicData, loops );
+  AudioMusicUnload();
+  Mix_PlayMusic(musicData, loops);
 }
 
-bool IsPlayingMusic()
-{
-    return (defaultMusic != NULL);
-}
 
 #endif //HAVE_LIBT4KCOMMON

Modified: branches/commonification/tuxmath/trunk/src/campaign.c
===================================================================
--- branches/commonification/tuxmath/trunk/src/campaign.c	2009-08-16 18:35:12 UTC (rev 1447)
+++ branches/commonification/tuxmath/trunk/src/campaign.c	2009-08-16 19:49:23 UTC (rev 1448)
@@ -169,20 +169,22 @@
 
 
   char* sprites[] = {
-    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"
+    DATA_PREFIX "/images/menu/tux_helmet_yellow",
+    DATA_PREFIX "/images/menu/tux_helmet_green",
+    DATA_PREFIX "/images/menu/tux_helmet_blue",
+    DATA_PREFIX "/images/menu/tux_helmet_red",
+    DATA_PREFIX "/images/menu/tux_helmet_black"
   };
 
-  SDL_Surface* icon = LoadSpriteOfBoundingBox(sprites[stage],
+  sprite* icon_sprite = LoadSpriteOfBoundingBox(sprites[stage],
                         IMG_REGULAR | IMG_NOT_REQUIRED,
                         0.1 * screen->w, 0.1 * screen->h);
+  SDL_Surface* icon = NULL;
   SDL_Rect textarea = screen->clip_rect;
 
+  if(icon_sprite)
+    icon = icon_sprite->default_img;
 
-
   if (icon) //stretch the tiny sprite to 3x
   {
     textarea.x = icon->w;
@@ -203,7 +205,7 @@
 
   DEBUGMSG(debug_game, "Finished briefing\n");
 
-  SDL_FreeSurface(icon);
+  FreeSprite(icon_sprite);
 }
 
 void readStageSettings(int stage)

Modified: branches/commonification/tuxmath/trunk/src/compiler.h
===================================================================
--- branches/commonification/tuxmath/trunk/src/compiler.h	2009-08-16 18:35:12 UTC (rev 1447)
+++ branches/commonification/tuxmath/trunk/src/compiler.h	2009-08-16 19:49:23 UTC (rev 1448)
@@ -33,7 +33,6 @@
 
 */
 #include "config.h"
-#ifndef HAVE_LIBT4KCOMMON
 #ifdef WIN32
 /* Horrible, dangerous macros. */
 /*
@@ -156,4 +155,3 @@
 #undef CLOCK_ASM
 #define CLOCK_ASM(x) x=42
 #endif
-#endif //HAVE_LIBT4KCOMMON

Modified: branches/commonification/tuxmath/trunk/src/fileops_media.c
===================================================================
--- branches/commonification/tuxmath/trunk/src/fileops_media.c	2009-08-16 18:35:12 UTC (rev 1447)
+++ branches/commonification/tuxmath/trunk/src/fileops_media.c	2009-08-16 19:49:23 UTC (rev 1448)
@@ -20,97 +20,97 @@
 static char fn[PATH_MAX];
 
 static char* image_filenames[NUM_IMAGES] = {
-  "status/title.png",
-  "status/left.png",
-  "status/left_gray.png",
-  "status/right.png",
-  "status/right_gray.png",
-  "status/tux4kids.png",
-  "status/nbs.png",
-  "cities/city-blue.png",
-  "cities/csplode-blue-1.png",
-  "cities/csplode-blue-2.png",
-  "cities/csplode-blue-3.png",
-  "cities/csplode-blue-4.png",
-  "cities/csplode-blue-5.png",
-  "cities/cdead-blue.png",
-  "cities/city-green.png",
-  "cities/csplode-green-1.png",
-  "cities/csplode-green-2.png",
-  "cities/csplode-green-3.png",
-  "cities/csplode-green-4.png",
-  "cities/csplode-green-5.png",
-  "cities/cdead-green.png",
-  "cities/city-orange.png",
-  "cities/csplode-orange-1.png",
-  "cities/csplode-orange-2.png",
-  "cities/csplode-orange-3.png",
-  "cities/csplode-orange-4.png",
-  "cities/csplode-orange-5.png",
-  "cities/cdead-orange.png",
-  "cities/city-red.png",
-  "cities/csplode-red-1.png",
-  "cities/csplode-red-2.png",
-  "cities/csplode-red-3.png",
-  "cities/csplode-red-4.png",
-  "cities/csplode-red-5.png",
-  "cities/cdead-red.png",
-  "cities/shields.png",
-  "status/nums.png",
-  "status/lednums.png",
-  "status/led_neg_sign.png",
-  "status/paused.png",
-  "status/demo.png",
-  "status/demo-small.png",
-  "status/keypad.png",
-  "status/keypad_no_neg.png",
-  "tux/console_led.png",
-  "tux/console_bash.png",
+  "status/title.svg",
+  "status/left.svg",
+  "status/left_gray.svg",
+  "status/right.svg",
+  "status/right_gray.svg",
+  "status/tux4kids.svg",
+  "status/nbs.svg",
+  "cities/city-blue.svg",
+  "cities/csplode-blue-1.svg",
+  "cities/csplode-blue-2.svg",
+  "cities/csplode-blue-3.svg",
+  "cities/csplode-blue-4.svg",
+  "cities/csplode-blue-5.svg",
+  "cities/cdead-blue.svg",
+  "cities/city-green.svg",
+  "cities/csplode-green-1.svg",
+  "cities/csplode-green-2.svg",
+  "cities/csplode-green-3.svg",
+  "cities/csplode-green-4.svg",
+  "cities/csplode-green-5.svg",
+  "cities/cdead-green.svg",
+  "cities/city-orange.svg",
+  "cities/csplode-orange-1.svg",
+  "cities/csplode-orange-2.svg",
+  "cities/csplode-orange-3.svg",
+  "cities/csplode-orange-4.svg",
+  "cities/csplode-orange-5.svg",
+  "cities/cdead-orange.svg",
+  "cities/city-red.svg",
+  "cities/csplode-red-1.svg",
+  "cities/csplode-red-2.svg",
+  "cities/csplode-red-3.svg",
+  "cities/csplode-red-4.svg",
+  "cities/csplode-red-5.svg",
+  "cities/cdead-red.svg",
+  "cities/shields.svg",
+  "status/nums.svg",
+  "status/lednums.svg",
+  "status/led_neg_sign.svg",
+  "status/paused.svg",
+  "status/demo.svg",
+  "status/demo-small.svg",
+  "status/keypad.svg",
+  "status/keypad_no_neg.svg",
+  "tux/console_led.svg",
+  "tux/console_bash.svg",
   "tux/tux-relax.svg",
   "tux/tux-alarm.svg",
-  "tux/tux-drat.png",
-  "tux/tux-yipe.png",
-  "tux/tux-sit.png",
-  "penguins/flapdown.png",
-  "penguins/flapup.png",
-  "penguins/incoming.png",
-  "penguins/grumpy.png",
-  "penguins/worried.png",
-  "penguins/standing-up.png",
-  "penguins/sitting-down.png",
-  "penguins/walk-on1.png",
-  "penguins/walk-on2.png",
-  "penguins/walk-on3.png",
-  "penguins/walk-off1.png",
-  "penguins/walk-off2.png",
-  "penguins/walk-off3.png",
-  "igloos/melted3.png",
-  "igloos/melted2.png",
-  "igloos/melted1.png",
-  "igloos/half.png",
-  "igloos/intact.png",
-  "igloos/rebuilding1.png",
-  "igloos/rebuilding2.png",
-  "igloos/cloud.png",
-  "igloos/snow1.png",
-  "igloos/snow2.png",
-  "igloos/snow3.png",
-  "igloos/extra_life.png",
-  "status/wave.png",
-  "status/score.png",
-  "status/stop.png",
-  "status/numbers.png",
-  "status/gameover.png",
-  "status/gameover_won.png",
-  "factoroids/gbstars.png",
-  "factoroids/asteroid1.png",
-  "factoroids/asteroid2.png",
-  "factoroids/asteroid3.png",
-  "factoroids/ship01.png",
-  "factoroids/factoroids.png",
-  "factoroids/factors.png",
-  "factoroids/tux.png",
-  "factoroids/good.png"
+  "tux/tux-drat.svg",
+  "tux/tux-yipe.svg",
+  "tux/tux-sit.svg",
+  "penguins/flapdown.svg",
+  "penguins/flapup.svg",
+  "penguins/incoming.svg",
+  "penguins/grumpy.svg",
+  "penguins/worried.svg",
+  "penguins/standing-up.svg",
+  "penguins/sitting-down.svg",
+  "penguins/walk-on1.svg",
+  "penguins/walk-on2.svg",
+  "penguins/walk-on3.svg",
+  "penguins/walk-off1.svg",
+  "penguins/walk-off2.svg",
+  "penguins/walk-off3.svg",
+  "igloos/melted3.svg",
+  "igloos/melted2.svg",
+  "igloos/melted1.svg",
+  "igloos/half.svg",
+  "igloos/intact.svg",
+  "igloos/rebuilding1.svg",
+  "igloos/rebuilding2.svg",
+  "igloos/cloud.svg",
+  "igloos/snow1.svg",
+  "igloos/snow2.svg",
+  "igloos/snow3.svg",
+  "igloos/extra_life.svg",
+  "status/wave.svg",
+  "status/score.svg",
+  "status/stop.svg",
+  "status/numbers.svg",
+  "status/gameover.svg",
+  "status/gameover_won.svg",
+  "factoroids/gbstars.svg",
+  "factoroids/asteroid1.svg",
+  "factoroids/asteroid2.svg",
+  "factoroids/asteroid3.svg",
+  "factoroids/ship01.svg",
+  "factoroids/factoroids.svg",
+  "factoroids/factors.svg",
+  "factoroids/tux.svg",
+  "factoroids/good.svg"
   };
 
 static char* sprite_filenames[NUM_IMAGES] = {

Modified: branches/commonification/tuxmath/trunk/src/globals.h
===================================================================
--- branches/commonification/tuxmath/trunk/src/globals.h	2009-08-16 18:35:12 UTC (rev 1447)
+++ branches/commonification/tuxmath/trunk/src/globals.h	2009-08-16 19:49:23 UTC (rev 1448)
@@ -31,9 +31,6 @@
 typedef enum { false, true } bool;
 #endif // HAVE_LIBT4KCOMMON
 
-#define min(a,b) (((a) < (b)) ? (a) : (b))
-#define max(a,b) (((a) > (b)) ? (a) : (b))
-
 /* for Tim's feedback speed control code           */
 //#define FEEDBACK_DEBUG
 //#define LINEBREAK

Modified: branches/commonification/tuxmath/trunk/src/loaders.c
===================================================================
--- branches/commonification/tuxmath/trunk/src/loaders.c	2009-08-16 18:35:12 UTC (rev 1447)
+++ branches/commonification/tuxmath/trunk/src/loaders.c	2009-08-16 19:49:23 UTC (rev 1448)
@@ -24,6 +24,8 @@
 
 #include "loaders.h"
 #include "globals.h"
+#include "tuxmath.h"
+#include "compiler.h"
 #include "SDL_extras.h"
 
 #ifndef HAVE_LIBT4KCOMMON
@@ -33,7 +35,6 @@
 #endif
 
 /* local functions */
-int             check_file(const char* file);
 
 #ifdef HAVE_RSVG
 SDL_Surface*    load_svg(const char* file_name, int width, int height, const char* layer_name);

Modified: branches/commonification/tuxmath/trunk/src/menu.c
===================================================================
--- branches/commonification/tuxmath/trunk/src/menu.c	2009-08-16 18:35:12 UTC (rev 1447)
+++ branches/commonification/tuxmath/trunk/src/menu.c	2009-08-16 19:49:23 UTC (rev 1448)
@@ -1,7 +1,10 @@
 /*
-  t4k-menu.c
+  menu.c
 
   Functions responsible for loading, parsing and displaying game menu.
+  This file is almost identical to t4k-menu.c from tux4kids-common library.
+  The only differences are button paths and data_prefix in PrerenderAll().
+  Please copy any changes done to t4kcommon here.
 
   Part of "Tux4Kids" Project
   http://www.tux4kids.com/
@@ -12,8 +15,10 @@
 */
 
 #include "globals.h"
+#include "tuxmath.h"
 #include "menu.h"
 #include "loaders.h"
+#include "compiler.h"
 #include "SDL_extras.h"
 #include "titlescreen.h"
 
@@ -23,6 +28,10 @@
 
 #ifndef HAVE_LIBT4KCOMMON
 
+/*
+  representation of a menu tree node
+*/
+
 struct mNode {
   struct mNode* parent;
 
@@ -52,38 +61,52 @@
 
 typedef struct mNode MenuNode;
 
-#define QUIT -2
-#define STOP -1
-#define RUN_MAIN_MENU -3
 
-int n_of_activities;
+/*
+  menu globals
+*/
+
+/* activities array is used to parse xml menu files */
+static int n_of_activities;
 static char** activities;
 
-char* data_prefix;
+/* prefix that is added to sprite paths */
+static char* data_prefix;
 
-Mix_Chunk* snd_click;
-Mix_Chunk* snd_hover;
-char* music_path;
+static Mix_Chunk* snd_click;
+static Mix_Chunk* snd_hover;
+static char* music_path;
 
-#define N_OF_MENUS 10
-MenuNode* menus[N_OF_MENUS];
-
 /* font size used in current resolution */
-int curr_font_size;
+static int curr_font_size;
 
 /* buffer size used when reading attributes or names */
-const int buf_size = 512;
+static const int buf_size = 512;
 
+/* maximum menu buttons per page */
+static const int MAX_PAGE_SIZE = 8;
+
 /* actions available while viewing the menu */
 enum { NONE, CLICK, PAGEUP, PAGEDOWN, STOP_ESC, RESIZED };
 
+/* menus is a global array when user can save up to 10 loaded menus.
+   From outside this file we identify menu trees by their ids (indexes in this array)
+   so as not to expose MenuNode struct to the user */
+#define N_OF_MENUS 10
+static MenuNode* menus[N_OF_MENUS];
+
 /* stop button, left and right arrow positions do not
    depend on currently displayed menu */
 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
-  titlescreen paths and rects ? ) */
+
+/*
+  positioning constants
+*/
+
+/* NOTE: maybe it is better to move these constants into a config file ? */
+/* X Y W H */
 const float menu_pos[4] = {0.38, 0.23, 0.55, 0.72};
 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};
@@ -93,12 +116,15 @@
 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 float button_gap = 0.18, text_h_gap = 0.35, text_w_gap = 0.5, button_radius = 0.27;
 const int min_font_size = 8, default_font_size = 20, max_font_size = 40;
 
 
 
-/* local functions */
+/*
+  local functions
+*/
+
 MenuNode*       create_empty_node();
 char*           get_attribute_name(const char* token);
 char*           get_attribute_value(const char* token);
@@ -108,11 +134,16 @@
 
 SDL_Surface**   render_buttons(MenuNode* menu, bool selected);
 char*           find_longest_text(MenuNode* menu, int* length);
+int             find_longest_menu_page(MenuNode* menu);
 void            set_font_size();
 void            prerender_menu(MenuNode* menu);
 
 
-/* initialization of menu module */
+/*
+  functions initializing the menu module
+  (they shoul be called before any other menu activity)
+*/
+
 void SetActivitiesList(int num, char** acts)
 {
   n_of_activities = num;
@@ -126,6 +157,7 @@
   music_path = mus_path;
 }
 
+/* prefix that is used whe loading menu sprites */
 void SetImagePathPrefix(char* pref)
 {
   data_prefix = pref;
@@ -183,6 +215,7 @@
       node->icon_name = strdup(attr_val);
     else if(strcmp(attr_name, "run") == 0)
     {
+      /* special activity - should be handled separately */
       if(strcmp(attr_val, "RUN_MAIN_MENU") == 0)
         node->activity = RUN_MAIN_MENU;
       else
@@ -311,12 +344,72 @@
   menus[index] = menu;
 }
 
+/* load menu from given XML file and store its tree under given index
+   in "menus" array */
+void LoadMenu(int index, const char* file_name)
+{
+  FILE* menu_file = NULL;
 
+  if(menus[index])
+  {
+    free_menu(menus[index]);
+    menus[index] = NULL;
+  }
 
-/* Display the menu and run the event loop.
+  menu_file = fopen(file_name, "r");
+  if(menu_file == NULL)
+  {
+    DEBUGMSG(debug_menu, "LoadMenu(): Could not load %s !\n", file_name);
+  }
+  else
+  {
+    menus[index] = load_menu_from_file(menu_file, NULL);
+    fclose(menu_file);
+  }
+}
+
+/* free all loaded menu trees */
+void UnloadMenus(void)
+{
+  int i;
+
+  DEBUGMSG(debug_menu, "entering UnloadMenus()\n");
+
+  if(stop_button)
+  {
+    SDL_FreeSurface(stop_button);
+    stop_button = NULL;
+  }
+
+  if(prev_arrow)
+  {
+    SDL_FreeSurface(prev_arrow);
+    prev_arrow = NULL;
+  }
+
+  if(next_arrow)
+  {
+    SDL_FreeSurface(next_arrow);
+    next_arrow = NULL;
+  }
+
+  for(i = 0; i < N_OF_MENUS; i++)
+    if(menus[i] != NULL)
+    {
+      DEBUGMSG(debug_menu, "UnloadMenus(): freeing menu #%d\n", i);
+      free_menu(menus[i]);
+    }
+
+  DEBUGMSG(debug_menu, "leaving UnloadMenus()\n");
+}
+
+
+/*
+   RunMenu - main function to 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() */
+   this function is a modified copy of choose_menu_item()
+*/
 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;
@@ -352,6 +445,7 @@
     menu_item_selected = render_buttons(menu, true);
     items = min(menu->entries_per_screen, menu->submenu_size - menu->first_entry);
 
+    /* draw buttons */
     DEBUGMSG(debug_menu, "run_menu(): drawing %d buttons\n", items);
     for(i = 0; i < items; i++)
     {
@@ -365,6 +459,7 @@
 
     SDL_BlitSurface(stop_button, NULL, GetScreen(), &stop_rect);
 
+    /* check if there is a need to draw menu arrows */
     if(menu->entries_per_screen < menu->submenu_size)
     {
       /* display arrows */
@@ -378,6 +473,7 @@
         SDL_BlitSurface(next_gray, NULL, GetScreen(), &next_rect);
     }
 
+    /* display red menu title (if present) */
     if(menu->show_title)
     {
       menu_title_rect = menu->submenu[0]->button_rect;
@@ -404,6 +500,7 @@
       {
         switch (event.type)
         {
+          /* user decided to quit the application (for example by closing the window) */
           case SDL_QUIT:
           {
             FreeSurfaceArray(menu_item_unselected, items);
@@ -619,6 +716,7 @@
           }  // End of case SDL_KEYDOWN in outer switch statement
         }  // End event switch statement
 
+        /* handle button focus */
         if (old_loc != loc) {
           DEBUGMSG(debug_menu, "run_menu(): changed button focus, old=%d, new=%d\n", old_loc, loc);
           if(old_loc >= 0 && old_loc < items)
@@ -626,7 +724,8 @@
             tmp_rect = menu->submenu[old_loc + menu->first_entry]->button_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, GetScreen(), &menu->submenu[menu->first_entry + old_loc]->icon_rect);
+              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)
@@ -635,7 +734,8 @@
             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, GetScreen(), &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(GetScreen(), tmp_rect.x, tmp_rect.y, tmp_rect.w, tmp_rect.h);
@@ -643,9 +743,12 @@
           old_loc = loc;
         }
 
+        /* check if catched event causes any changes to titlescreen,
+           if handle_event() returns 1, menu should be redrawn */
         if(handle_event(&event))
           stop = true;
 
+        /* handle special action that was caused by an event */
         switch(action)
         {
           case RESIZED:
@@ -666,6 +769,7 @@
               {
                 if(return_choice)
                 {
+                  /* return choice instead of running a handler function */
                   FreeSurfaceArray(menu_item_unselected, items);
                   FreeSurfaceArray(menu_item_selected, items);
                   return tmp_node->activity;
@@ -679,17 +783,20 @@
                   }
                   else
                   {
+                    /* we are going to run a handler function that probably will run a game,
+                       save current screen resolution in case it is changed while running a game */
                     old_w = GetScreen()->w;
                     old_h = GetScreen()->h;
                     if(handle_activity(tmp_node->activity, tmp_node->param) == QUIT)
                     {
+                      /* user decided to quit while playing a game */
                       DEBUGMSG(debug_menu, "run_menu(): handle_activity() returned QUIT message, exiting.\n");
                       FreeSurfaceArray(menu_item_unselected, items);
                       FreeSurfaceArray(menu_item_selected, items);
                       return QUIT;
                     }
                     if(old_w != GetScreen()->w || old_h != GetScreen()->h)
-                      PrerenderAll();
+                      PrerenderAll(); /* resolution has changed */
                   }
                 }
               }
@@ -729,8 +836,10 @@
       }  // End of SDL_PollEvent while loop
 
       if(stop)
+        /* whole menu will be redrawn so there is no need to draw anything now */
         break;
 
+      /* handle icon animation of selected button */
       if(!stop && frame_counter % 5 == 0 && loc >= 0 && loc < items)
       {
         tmp_sprite = menu->submenu[menu->first_entry + loc]->icon;
@@ -743,6 +852,7 @@
         }
       }
 
+      /* handle titlescreen animations */
       handle_animations();
 
       /* Wait so we keep frame rate constant: */
@@ -764,6 +874,10 @@
   return QUIT;
 }
 
+/*
+  functins responsible for rendering menus
+*/
+
 /* return button surfaces that are currently displayed (without sprites) */
 SDL_Surface** render_buttons(MenuNode* menu, bool selected)
 {
@@ -895,6 +1009,8 @@
   prerender_menu(menus[index]);
 }
 
+/* recursively search for longest menu caption in currently
+   used menus and in current language */
 char* find_longest_text(MenuNode* menu, int* length)
 {
   SDL_Surface* text = NULL;
@@ -925,6 +1041,21 @@
   return ret;
 }
 
+/* recursively search for longest (sub)menu that is not longer
+   than MAX_PAGE_SIZE to ensure better menu outlook */
+int find_longest_menu_page(MenuNode* menu)
+{
+  int longest = 0, i;
+
+  if(menu->submenu_size <= MAX_PAGE_SIZE)
+    longest = menu->submenu_size;
+
+  for(i = 0; i < menu->submenu_size; i++)
+    longest = max(longest, find_longest_menu_page(menu->submenu[i]));
+
+  return longest;
+}
+
 /* find the longest text in all existing menus and binary search
    for the best font size */
 void set_font_size()
@@ -932,7 +1063,7 @@
   char* longest = NULL;
   char* temp;
   SDL_Surface* surf;
-  int length = 0, i, min_f, max_f, mid_f;
+  int length = 0, i, min_f, max_f, mid_f, max_buttons = 0;
 
   curr_font_size = default_font_size;
 
@@ -940,6 +1071,7 @@
   {
     if(menus[i])
     {
+      max_buttons = max(max_buttons, find_longest_menu_page(menus[i]));
       temp = find_longest_text(menus[i], &length);
       if(temp)
         longest = temp;
@@ -952,11 +1084,13 @@
   min_f = min_font_size;
   max_f = max_font_size;
 
+  /* run binary search */
   while(min_f < max_f)
   {
     mid_f = (min_f + max_f) / 2;
     surf = SimpleText(_(longest), mid_f, &black);
-    if(surf->w + (1.0 + 2.0 * text_w_gap) * (1.0 + 2.0 * text_h_gap) * surf->h < menu_rect.w)
+    if(surf->w + (1.0 + 2.0 * text_w_gap) * (1.0 + 2.0 * text_h_gap) * surf->h  <  menu_rect.w
+       && (1.0 + button_gap) * (max_buttons + 1) + (1.5 + 2.0 * text_h_gap) * surf->h * max_buttons  <  menu_rect.h)
       min_f = mid_f + 1;
     else
       max_f = mid_f;
@@ -1012,63 +1146,4 @@
       PrerenderMenu(i);
 }
 
-void LoadMenu(int index, const char* file_name)
-{
-  FILE* menu_file = NULL;
-
-  if(menus[index])
-  {
-    free_menu(menus[index]);
-    menus[index] = NULL;
-  }
-
-  menu_file = fopen(file_name, "r");
-  if(menu_file == NULL)
-  {
-    DEBUGMSG(debug_menu, "LoadMenu(): Could not load %s !\n", file_name);
-  }
-  else
-  {
-    menus[index] = load_menu_from_file(menu_file, NULL);
-    fclose(menu_file);
-  }
-}
-
-
-
-/* free all loaded menu trees */
-void UnloadMenus(void)
-{
-  int i;
-
-  DEBUGMSG(debug_menu, "entering UnloadMenus()\n");
-
-  if(stop_button)
-  {
-    SDL_FreeSurface(stop_button);
-    stop_button = NULL;
-  }
-
-  if(prev_arrow)
-  {
-    SDL_FreeSurface(prev_arrow);
-    prev_arrow = NULL;
-  }
-
-  if(next_arrow)
-  {
-    SDL_FreeSurface(next_arrow);
-    next_arrow = NULL;
-  }
-
-  for(i = 0; i < N_OF_MENUS; i++)
-    if(menus[i] != NULL)
-    {
-      DEBUGMSG(debug_menu, "UnloadMenus(): freeing menu #%d\n", i);
-      free_menu(menus[i]);
-    }
-
-  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-16 18:35:12 UTC (rev 1447)
+++ branches/commonification/tuxmath/trunk/src/menu.h	2009-08-16 19:49:23 UTC (rev 1448)
@@ -20,7 +20,14 @@
 #ifndef HAVE_LIBT4KCOMMON
 
 #define MAX_FPS 30
+/* special values used by RunMenu. RUN_MAIN_MENU is a special
+   activity that can be used in .xml menu structures but should not
+   be declared in activities' lists.
+   RunMenu returning QUIT indicates that user decided to quit application while
+   running the menu. Returning STOP indicates that user pressed stop button. */
+enum { RUN_MAIN_MENU = -3, QUIT = -2, STOP = -1 };
 
+
 extern SDL_Rect menu_rect, stop_rect, prev_rect, next_rect;
 extern SDL_Surface *stop_button, *prev_arrow, *next_arrow, *prev_gray, *next_gray;
 

Modified: branches/commonification/tuxmath/trunk/src/titlescreen.c
===================================================================
--- branches/commonification/tuxmath/trunk/src/titlescreen.c	2009-08-16 18:35:12 UTC (rev 1447)
+++ branches/commonification/tuxmath/trunk/src/titlescreen.c	2009-08-16 19:49:23 UTC (rev 1448)
@@ -28,6 +28,7 @@
 #include "multiplayer.h"
 #include "highscore.h"
 #include "credits.h"
+#include "compiler.h"
 #include "factoroids.h"
 #include "mathcards.h"
 #include "campaign.h"




More information about the Tux4kids-commits mailing list