[Tux4kids-commits] r1177 - tuxmath/trunk/src

Bolesław Kulbabiński bolekk-guest at alioth.debian.org
Fri Jul 10 13:29:02 UTC 2009


Author: bolekk-guest
Date: 2009-07-10 13:29:00 +0000 (Fri, 10 Jul 2009)
New Revision: 1177

Modified:
   tuxmath/trunk/src/SDL_extras.c
   tuxmath/trunk/src/SDL_extras.h
   tuxmath/trunk/src/menu.c
   tuxmath/trunk/src/menu.h
   tuxmath/trunk/src/titlescreen.c
Log:
added code displaying menu title and made arrow keys work; made new menu system the default one

Modified: tuxmath/trunk/src/SDL_extras.c
===================================================================
--- tuxmath/trunk/src/SDL_extras.c	2009-07-10 10:34:38 UTC (rev 1176)
+++ tuxmath/trunk/src/SDL_extras.c	2009-07-10 13:29:00 UTC (rev 1177)
@@ -385,6 +385,14 @@
   SDL_UpdateRect(surf, rect->x, rect->y, rect->w, rect->h);
 }
 
+void SetRect(SDL_Rect* rect, const float* pos)
+{
+  rect->x = pos[0] * screen->w;
+  rect->y = pos[1] * screen->h;
+  rect->w = pos[2] * screen->w;
+  rect->h = pos[3] * screen->h;
+}
+
 /* Darkens the screen by a factor of 2^bits */
 void DarkenScreen(Uint8 bits)
 {

Modified: tuxmath/trunk/src/SDL_extras.h
===================================================================
--- tuxmath/trunk/src/SDL_extras.h	2009-07-10 10:34:38 UTC (rev 1176)
+++ tuxmath/trunk/src/SDL_extras.h	2009-07-10 13:29:00 UTC (rev 1177)
@@ -38,6 +38,7 @@
 
 void            FreeSurfaceArray(SDL_Surface** surfs, int length);
 int             inRect(SDL_Rect r, int x, int y);
+void            SetRect(SDL_Rect* rect, const float* pos);
 void            UpdateRect(SDL_Surface* surf, SDL_Rect* rect);
 
 void            DarkenScreen(Uint8 bits);

Modified: tuxmath/trunk/src/menu.c
===================================================================
--- tuxmath/trunk/src/menu.c	2009-07-10 10:34:38 UTC (rev 1176)
+++ tuxmath/trunk/src/menu.c	2009-07-10 13:29:00 UTC (rev 1177)
@@ -60,10 +60,10 @@
 
 /*TODO: move these constants into a config file (maybe together with
   titlescreen paths and rects ? ) */
-const float menu_pos[4] = {0.4, 0.25, 0.55, 0.7};
+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.9, 0.95, 0.05, 0.05};
-const float next_pos[4] = {0.95, 0.95, 0.05, 0.05};
+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.png";
 const char* prev_path = "status/left.png";
 const char* next_path = "status/right.png";
@@ -74,7 +74,7 @@
 int curr_font_size;
 
 /* menu title rect */
-SDL_Rect title_rect;
+SDL_Rect menu_title_rect;
 
 /* buffer size used when reading attributes or names */
 const int buf_size = 128;
@@ -100,7 +100,6 @@
 int             run_menu(MenuNode* menu, bool return_choice);
 SDL_Surface**   render_buttons(MenuNode* menu, bool selected);
 void            prerender_menu(MenuNode* menu);
-void            set_rect(SDL_Rect* rect, const float* pos);
 char*           find_longest_text(MenuNode* menu, int* length);
 void            set_font_size();
 void            prerender_all();
@@ -125,6 +124,7 @@
   new_node->activity = 0;
   new_node->param = 0;
   new_node->first_entry = 0;
+  new_node->show_title = false;
 
   return new_node;
 }
@@ -258,7 +258,10 @@
   int i;
 
   if(title)
+  {
     menu->title = strdup(title);
+    menu->show_title = true;
+  }
   menu->submenu_size = items + (trailer ? 1 : 0);
   menu->submenu = (MenuNode**) malloc(menu->submenu_size * sizeof(MenuNode*));
   for(i = 0; i < items; i++)
@@ -557,6 +560,7 @@
 {
   SDL_Surface** menu_item_unselected = NULL;
   SDL_Surface** menu_item_selected = NULL;
+  SDL_Surface* title_surf;
   SDL_Event event;
   MenuNode* menu = root;
   MenuNode* tmp_node;
@@ -596,11 +600,20 @@
       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(stop_button, NULL, screen, &stop_rect);
     if(menu->first_entry > 0)
       SDL_BlitSurface(prev_arrow, NULL, screen, &prev_rect);
     if(menu->first_entry + items < menu->submenu_size)
       SDL_BlitSurface(next_arrow, NULL, screen, &next_rect);
+    if(menu->show_title)
+    {
+      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_FreeSurface(title_surf);
+    }
     SDL_UpdateRect(screen, 0, 0, 0, 0);
 
     SDL_WM_GrabInput(SDL_GRAB_OFF);
@@ -718,7 +731,7 @@
             else if (inRect(stop_rect, event.button.x, event.button.y ))
             {
               if (Opts_GetGlobalOpt(MENU_SOUND))
-                playsound(SND_TOCK);
+                playsound(SND_POP);
               action = STOP_ESC;
             }
 
@@ -769,40 +782,37 @@
               }
 
               /* Go up one entry, if present: */
-              /*case SDLK_UP:
+              case SDLK_UP:
               {
                 if (Opts_GetGlobalOpt(MENU_SOUND))
                   playsound(SND_TOCK);
-                if (loc > title_offset)
-                  {loc--;}
-                else if (n_menu_entries <= n_entries_per_screen) {
-                  loc = n_menu_entries-1;  // wrap around if only 1 screen
+                if (loc > 0)
+                  loc--;
+                else if (menu->submenu_size <= menu->entries_per_screen) 
+                  loc = menu->submenu_size - 1;  // wrap around if only 1 screen
+                else if (menu->first_entry > 0)
+                {
+                  loc = menu->entries_per_screen - 1;
+                  action = PAGEUP;
                 }
-                else if (loc == -1 && loc_screen_start > 0) {
-                  loc = loc_screen_start-1;
-                  loc_screen_start -= n_entries_per_screen;
-                }
-                if (loc != old_loc)
-                  warp_mouse = 1;
                 break;
-              }*/
+              }
 
-
-              /* Go down one entry, if present: */
-              /*case SDLK_DOWN:
+              case SDLK_DOWN:
               {
                 if (Opts_GetGlobalOpt(MENU_SOUND))
                   playsound(SND_TOCK);
-                if (loc >= 0 && loc + 1 < n_menu_entries)
-                  {loc++;}
-                else if (n_menu_entries <= n_entries_per_screen)
-                  loc = title_offset;       // wrap around if only 1 screen
-                else if (loc == -1)
-                  loc = loc_screen_start;
-                if (loc != old_loc)
-                  warp_mouse = 1;
+                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
+                else if (menu->first_entry + menu->entries_per_screen < menu->submenu_size)
+                {
+                  loc = 0;
+                  action = PAGEDOWN;
+                }
                 break;
-              }*/
+              }
 
               /* Change window size (used only to debug) */
               case SDLK_F5:
@@ -1072,6 +1082,7 @@
   MenuNode* curr_node;
   int i, imod, max_text_h = 0, max_text_w = 0;
   int button_h, button_w;
+  bool found_icons = false;
   char filename[buf_size];
 
   if(NULL == menu)
@@ -1088,6 +1099,8 @@
 
   for(i = 0; i < menu->submenu_size; i++)
   {
+    if(menu->submenu[i]->icon_name)
+      found_icons = true;
     temp_surf = NULL;
     temp_surf = SimpleText(_(menu->submenu[i]->title), curr_font_size, &black);
     if(temp_surf)
@@ -1099,7 +1112,7 @@
   }
 
   button_h = (1.0 + 2.0 * text_h_gap) * max_text_h;
-  button_w = max_text_w + (1.0 + 2.0 * text_w_gap) * button_h;
+  button_w = max_text_w + ( (found_icons ? 1.0 : 0.0) + 2.0 * text_w_gap) * button_h;
 
   menu->entries_per_screen = (int) ( (menu_rect.h - button_gap * button_h) /
                                    ( (1.0 + button_gap) * button_h ) );
@@ -1116,7 +1129,7 @@
     curr_node->icon_rect = curr_node->button_rect;
     curr_node->icon_rect.w = curr_node->icon_rect.h;
 
-    curr_node->text_rect.x = (1.0 + text_w_gap) * curr_node->icon_rect.w;
+    curr_node->text_rect.x = ( (found_icons ? 1.0 : 0.0) + text_w_gap) * curr_node->icon_rect.w;
     curr_node->text_rect.y = text_h_gap * max_text_h;
     curr_node->text_rect.h = max_text_h;
     curr_node->text_rect.w = max_text_w;
@@ -1137,14 +1150,6 @@
   }
 }
 
-void set_rect(SDL_Rect* rect, const float* pos)
-{
-  rect->x = pos[0] * screen->w;
-  rect->y = pos[1] * screen->h;
-  rect->w = pos[2] * screen->w;
-  rect->h = pos[3] * screen->h;
-}
-
 char* find_longest_text(MenuNode* menu, int* length)
 {
   SDL_Surface* text = NULL;
@@ -1220,23 +1225,23 @@
 {
   int i;
 
-  set_rect(&menu_rect, menu_pos);
+  SetRect(&menu_rect, menu_pos);
 
-  set_rect(&stop_rect, stop_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);
   /* move button to the right */
   stop_rect.x = screen->w - stop_button->w;
 
-  set_rect(&prev_rect, prev_pos);
+  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);
   /* move button to the right */
   prev_rect.x += prev_rect.w - prev_arrow->w;
 
-  set_rect(&next_rect, next_pos);
+  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);
@@ -1320,7 +1325,6 @@
 
   level = 0;
 
-
   if (n_login_questions > 0)
     title = user_login_questions[0];
 
@@ -1420,8 +1424,9 @@
     tmp_node->submenu[i]->activity = i;
   }
   menus[MENU_LESSONS] = tmp_node;
-  set_font_size();
-  prerender_menu(menus[MENU_LESSONS]);
+  //set_font_size();
+  //prerender_menu(menus[MENU_LESSONS]);
+  prerender_all();
 
   run_menu(menus[MENU_MAIN], false);
   DEBUGMSG(debug_menu, "Leaving RunMainMenu()\n");

Modified: tuxmath/trunk/src/menu.h
===================================================================
--- tuxmath/trunk/src/menu.h	2009-07-10 10:34:38 UTC (rev 1176)
+++ tuxmath/trunk/src/menu.h	2009-07-10 13:29:00 UTC (rev 1177)
@@ -82,7 +82,7 @@
   int param;
 
   /* these fields are used only if submenu_size > 0 */
-  bool display_title;
+  bool show_title;
   int entries_per_screen;
   int first_entry;
 };

Modified: tuxmath/trunk/src/titlescreen.c
===================================================================
--- tuxmath/trunk/src/titlescreen.c	2009-07-10 10:34:38 UTC (rev 1176)
+++ tuxmath/trunk/src/titlescreen.c	2009-07-10 13:29:00 UTC (rev 1177)
@@ -69,7 +69,19 @@
 char **lesson_list_filenames = NULL;
 int num_lessons = 0;
 
+/*TODO: move these constants into a config file
+  (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.png";
+const char* title_path = "title/title1.png";
+const char* egg_path = "title/egg.png";
+const char* tux_path = "tux/bigtux";
+/* beak coordinates relative to tux rect */
+const float beak_pos[4] = {0.36, 0.21, 0.27, 0.14};
 
+
 /* --- media for menus --- */
 
 enum {
@@ -236,9 +248,8 @@
   }
 
   start_time = SDL_GetTicks();
+  logo = LoadImage(standby_path, IMG_REGULAR);
 
-  logo = images[IMG_STANDBY]; /* going to be svg ? */
-
   /* display the Standby screen */
   if(logo)
   {
@@ -254,10 +265,11 @@
     SDL_UpdateRect(screen, 0, 0, 0, 0);
     /* Play "harp" greeting sound lifted from Tux Paint */
     playsound(SND_HARP);
+    SDL_FreeSurface(logo);
   }
 
   /* load backgrounds */
-  LoadBothBkgds("title/menu_bkg.jpg", &fs_bkg, &win_bkg);
+  LoadBothBkgds(bkg_path, &fs_bkg, &win_bkg);
   if(fs_bkg == NULL || win_bkg == NULL)
   {
     fprintf(stderr, "Backgrounds were not properly loaded, exiting");
@@ -268,7 +280,7 @@
     return;
   }
 
-  TitleScreen_load_menu();
+  //TitleScreen_load_menu();
 
   /* load titlescreen images */
   if(RenderTitleScreen() == 0)
@@ -290,6 +302,7 @@
     SDL_Delay(50);
   }
 
+  /* NOTE: do we need this ? */
   DEBUGCODE(debug_titlescreen)
     SDL_WM_GrabInput(SDL_GRAB_OFF); /* in case of a freeze, this traps the cursor */
   else
@@ -357,7 +370,7 @@
 
   /* Red "Stop" circle in upper right corner to go back to main menu: */
   /* this is going to be part of the menu */
-  if (images[IMG_STOP])
+  /*if (images[IMG_STOP])
   {
     stopRect.w = images[IMG_STOP]->w;
     stopRect.h = images[IMG_STOP]->h;
@@ -365,7 +378,7 @@
     stopRect.y = 0;
     SDL_BlitSurface(images[IMG_STOP], NULL, screen, &stopRect);
   }
-  SDL_UpdateRect(screen, 0, 0, 0, 0);
+  SDL_UpdateRect(screen, 0, 0, 0, 0);*/
 
   /* Start playing menu music if desired: */
   if (Opts_GetGlobalOpt(MENU_MUSIC))
@@ -374,20 +387,20 @@
   }
 
   /* If necessary, have the user log in */
-  if (run_login_menu() != -1) {
-  //if (RunLoginMenu() != -1) {
+  //if (run_login_menu() != -1) {
+  if (RunLoginMenu() != -1) {
     /* Finish parsing user options */
     initialize_options_user();
     /* Start the main menu */
-    run_main_menu();
-    //RunMainMenu();
+    //run_main_menu();
+    RunMainMenu();
   }
 
   /* User has selected quit, clean up */
   DEBUGMSG(debug_titlescreen, "TitleScreen(): Freeing title screen images\n");
 
   free_titlescreen();
-  TitleScreen_unload_menu();
+  //TitleScreen_unload_menu();
 
   DEBUGMSG(debug_titlescreen, "leaving TitleScreen()\n");
 }
@@ -416,7 +429,7 @@
     /* we keep two backgrounds to make screen mode switch faster */
     if(current_bkg()->w != screen->w || current_bkg()->h != screen->h)
     {
-      new_bkg = LoadBkgd("title/menu_bkg.jpg", screen->w, screen->h);
+      new_bkg = LoadBkgd(bkg_path, screen->w, screen->h);
       if(new_bkg == NULL)
       {
         DEBUGMSG(debug_titlescreen, "RenderTitleScreen(): Failed to load new background.\n");
@@ -433,14 +446,13 @@
     bkg_rect.x = (screen->w - bkg_rect.w) / 2;
     bkg_rect.y = (screen->h - bkg_rect.h) / 2;
 
-    /* Tux in lower left corner of the screen
-       (no more than 20% of screen width and 50% of screen height) */
-    Tux = LoadSpriteOfBoundingBox("tux/bigtux", IMG_ALPHA, (int)(0.2 * screen->w), (int)(0.5 * screen->h));
+    /* Tux in lower left corner of the screen */
+    SetRect(&tux_rect, tux_pos);
+    Tux = LoadSpriteOfBoundingBox(tux_path, IMG_ALPHA, tux_rect.w, tux_rect.h);
     if(Tux && Tux->frame[0])
     {
-      tux_rect = Tux->frame[0]->clip_rect;
-      tux_rect.x = 0;
-      tux_rect.y = screen->h - tux_rect.h;
+      tux_rect.w = Tux->frame[0]->clip_rect.w;
+      tux_rect.h = Tux->frame[0]->clip_rect.h;
     }
     else
     {
@@ -449,12 +461,12 @@
     }
 
     /* "Tux, of math command" title in upper right corner */
-    title = images[IMG_MENU_TITLE];
+    SetRect(&title_rect, title_pos);
+    title = LoadImageOfBoundingBox(title_path, IMG_ALPHA, title_rect.w, title_rect.h);
     if(title)
     {
-      title_rect = title->clip_rect;
-      title_rect.x = 0;
-      title_rect.y = 0;
+      title_rect.w = title->clip_rect.w;
+      title_rect.h = title->clip_rect.h;
     }
     else
     {
@@ -463,13 +475,12 @@
     }
 
     /* easter egg */
-    egg = LoadImage("title/egg.png", IMG_COLORKEY | IMG_NOT_REQUIRED);
+    egg = LoadImage(egg_path, IMG_COLORKEY | IMG_NOT_REQUIRED);
 
-    /* FIXME: it would be  better to read those values from file */
-    beak.x = tux_rect.x + 0.36 * tux_rect.w;
-    beak.y = tux_rect.y + 0.21 * tux_rect.h;
-    beak.w = 0.27 * tux_rect.w;
-    beak.h = 0.14 * tux_rect.h;
+    beak.x = tux_rect.x + beak_pos[0] * tux_rect.w;
+    beak.y = tux_rect.y + beak_pos[1] * tux_rect.h;
+    beak.w = beak_pos[2] * tux_rect.w;
+    beak.h = beak_pos[3] * tux_rect.h;
 
 
     /* stop button - going to be part of the menu */
@@ -590,6 +601,12 @@
     egg = NULL;
   }
 
+  if(title)
+  {
+    SDL_FreeSurface(title);
+    title = NULL;
+  }
+
   if(fs_bkg)
   {
     SDL_FreeSurface(fs_bkg);




More information about the Tux4kids-commits mailing list