[Tux4kids-commits] r1010 - in tuxmath/trunk: . data data/menus doc linebreak po src

Bolesław Kulbabiński bolekk-guest at alioth.debian.org
Thu Jun 4 17:54:35 UTC 2009


Author: bolekk-guest
Date: 2009-06-04 17:54:34 +0000 (Thu, 04 Jun 2009)
New Revision: 1010

Added:
   tuxmath/trunk/data/menus/
   tuxmath/trunk/data/menus/main_menu.xml
   tuxmath/trunk/src/menu.c
   tuxmath/trunk/src/menu.h
Modified:
   tuxmath/trunk/
   tuxmath/trunk/data/
   tuxmath/trunk/doc/
   tuxmath/trunk/linebreak/
   tuxmath/trunk/po/
   tuxmath/trunk/src/
   tuxmath/trunk/src/CMakeLists.txt
   tuxmath/trunk/src/Makefile.am
   tuxmath/trunk/src/globals.h
   tuxmath/trunk/src/setup.c
Log:
Added menu parser in TuxMath


Property changes on: tuxmath/trunk
___________________________________________________________________
Modified: svn:ignore
   - *~
*.o
Makefile
Makefile.in
autom4te.cache
configure
config.h
config.h.in
config.log
config.status
autoscan.log

   + *~
*.o
CMakeFiles
Makefile
Makefile.in
autom4te.cache
configure
config.h
config.h.in
config.log
config.status
autoscan.log



Property changes on: tuxmath/trunk/data
___________________________________________________________________
Modified: svn:ignore
   - *~
*.o
Makefile
Makefile.in

   + *~
*.o
CMakeFiles
Makefile
Makefile.in


Added: tuxmath/trunk/data/menus/main_menu.xml
===================================================================
--- tuxmath/trunk/data/menus/main_menu.xml	                        (rev 0)
+++ tuxmath/trunk/data/menus/main_menu.xml	2009-06-04 17:54:34 UTC (rev 1010)
@@ -0,0 +1,28 @@
+<menu title="Main Menu" entries="7">
+    <menu title="Play Alone" entries="5">
+        <item title="Math Command Training Academy" run="RUN_ACADEMY">
+        <item title="Math Command Fleet Missions" run="RUN_FLEET_MISSIONS">
+        <item title="Play Arcade Game" run="RUN_ARCADE">
+        <item title="Play Custom Game" run="RUN_CUSTOM">
+        <item title="Main Menu" run="RUN_MAIN_MENU">
+    </menu>
+    <menu title="LAN Game" entries="3">
+        <item title="Host" run="RUN_LAN_HOST">
+        <item title="Join" run="RUN_LAN_JOIN">
+        <item title="Main Menu" run="RUN_MAIN_MENU">
+    </menu>
+    <menu title="Play With Friends" entries="0">
+    </menu>
+    <menu title="Factoroids!" entries="2">
+        <item title="Factors" run="RUN_FACTORS">
+        <item title="Fractions" run="RUN_FRACTIONS">
+    </menu>
+    <item title="Help" run="RUN_HELP">
+    <menu title="More Options" entries="4">
+        <item title="Demo" run="RUN_DEMO">
+        <item title="Project Info" run="RUN_PROJECT_INFO">
+        <item title="Credits" run="RUN_CREDITS">
+        <item title="Main Menu" run="RUN_MAIN_MENU">
+    </menu>
+    <item title="Quit" sprite="quit" run="RUN_QUIT">
+</menu>


Property changes on: tuxmath/trunk/doc
___________________________________________________________________
Modified: svn:ignore
   - *~
*.o
Makefile
Makefile.in

   + *~
*.o
CMakeFiles
Makefile
Makefile.in



Property changes on: tuxmath/trunk/linebreak
___________________________________________________________________
Modified: svn:ignore
   - *~
*.o
Makefile
Makefile.in

   + *~
*.o
.deps
CMakeFiles
Makefile
Makefile.in



Property changes on: tuxmath/trunk/po
___________________________________________________________________
Modified: svn:ignore
   - Makefile
Makefile.in

   + Makefile
Makefile.in
CMakeFiles



Property changes on: tuxmath/trunk/src
___________________________________________________________________
Modified: svn:ignore
   - *~
*.o
Makefile
Makefile.in
tuxmath
tuxmathadmin
generate-lesson

   + *~
*.o
.deps
CMakeFiles
Makefile
Makefile.in
tuxmath
tuxmathadmin
generate_lesson


Modified: tuxmath/trunk/src/CMakeLists.txt
===================================================================
--- tuxmath/trunk/src/CMakeLists.txt	2009-06-01 00:52:28 UTC (rev 1009)
+++ tuxmath/trunk/src/CMakeLists.txt	2009-06-04 17:54:34 UTC (rev 1010)
@@ -24,6 +24,7 @@
   fileops.c
   fileops_media.c
   game.c
+  menu.c
   highscore.c
   lessons.c
   loaders.c

Modified: tuxmath/trunk/src/Makefile.am
===================================================================
--- tuxmath/trunk/src/Makefile.am	2009-06-01 00:52:28 UTC (rev 1009)
+++ tuxmath/trunk/src/Makefile.am	2009-06-04 17:54:34 UTC (rev 1010)
@@ -40,6 +40,7 @@
 	linewrap.c	\
 	loaders.c	\
 	audio.c 	\
+	menu.c   	\
 	mathcards.c	\
 	campaign.c	\
 	multiplayer.c	\
@@ -49,7 +50,7 @@
 	SDL_rotozoom.c	\
 	lessons.c	\
 	scandir.c	\
-	pixels.c	
+	pixels.c
 
 
 # HACK "TuxMath" is the Windows program, whereas "tuxmath" is the Unix program
@@ -67,6 +68,7 @@
 	factoroids.h	\
 	fileops.h 	\
 	game.h		\
+	menu.h		\
 	globals.h	\
 	highscore.h 	\
 	linewrap.h	\

Modified: tuxmath/trunk/src/globals.h
===================================================================
--- tuxmath/trunk/src/globals.h	2009-06-01 00:52:28 UTC (rev 1009)
+++ tuxmath/trunk/src/globals.h	2009-06-04 17:54:34 UTC (rev 1010)
@@ -13,7 +13,7 @@
 
   Part of "Tux4Kids" Project
   http://www.tux4kids.org/
-      
+
   Added March 2, 2006
 
   Copyright: See COPYING file that comes with this distribution
@@ -38,6 +38,30 @@
 #define tmdprintf(...) 0
 #endif
 
+/* debug data (declared in setup.c) */
+extern int debug_status;
+
+/* bitmasks for debugging options (declared in setup.c) */
+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_svg;
+extern const int debug_mathcards;
+extern const int debug_sdl;
+extern const int debug_lessons;
+extern const int debug_highscore;
+extern const int debug_all;
+
+/* debug macros */
+#define DEBUGCODE(mask) if(mask & debug_status)
+#define DEBUGMSG(mask, ...) if(mask & debug_status) fprintf(stderr, __VA_ARGS__); fflush(stderr);
+
 /* Maximum length of file path: */
 #define PATH_MAX 4096
 

Added: tuxmath/trunk/src/menu.c
===================================================================
--- tuxmath/trunk/src/menu.c	                        (rev 0)
+++ tuxmath/trunk/src/menu.c	2009-06-04 17:54:34 UTC (rev 1010)
@@ -0,0 +1,205 @@
+/*
+  menu.c
+
+  Functions responsible for loading, parsing and displaying game menu.
+
+  Part of "Tux4Kids" Project
+  http://www.tux4kids.com/
+
+  Author: Boleslaw Kulbabinski <bkulbabinski at gmail.com>, (C) 2009
+
+  Copyright: See COPYING file that comes with this distribution.
+*/
+
+#include "globals.h"
+#include "menu.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+/* create string array of activities' names */
+#define X(name) #name
+char* activities[] = { ACTIVITIES };
+#undef X
+
+/* we may use a few separate menu trees */
+typedef enum {
+  MENU_MAIN,
+  MENU_GAME_DIFFICULTY,
+  N_OF_MENUS
+} MenuType;
+
+MenuNode* menus[N_OF_MENUS];
+
+/* 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);
+char* get_attribute_value(const char* token);
+void read_attributes(FILE* xml_file, MenuNode* node);
+MenuNode* load_menu_from_file(FILE* xml_file);
+void free_menu(MenuNode* menu);
+
+
+/* creates new MenuNode struct with all field set to NULL (or 0) */
+MenuNode* create_empty_node()
+{
+  MenuNode* new_node = malloc(sizeof(MenuNode));
+  new_node->title = NULL;
+  new_node->sprite = NULL;
+  new_node->activity = 0;
+  new_node->submenu_size = 0;
+  new_node->submenu = NULL;
+
+  return new_node;
+}
+
+/* read attributes and fill appropriate node fields */
+void read_attributes(FILE* xml_file, MenuNode* node)
+{
+  char attr_name[buf_size];
+  char attr_val[buf_size];
+  int i;
+
+  /* read tokens until closing '>' is found */
+  do
+  {
+    fscanf(xml_file, " %[^=\n]", attr_name);
+
+    DEBUGMSG(debug_menu_parser, "read_attributes(): read attribute name: %s\n", attr_name);
+    if(strchr(attr_name, '>'))
+      break;
+
+    fscanf(xml_file, "=\"%[^\"]\"", attr_val);
+    DEBUGMSG(debug_menu_parser, "read_attributes(): read attribute value: %s\n", attr_val);
+
+    if(strcmp(attr_name, "title") == 0)
+      node->title = strdup(attr_val);
+    else if(strcmp(attr_name, "entries") == 0)
+      node->submenu_size = atoi(attr_val);
+    else if(strcmp(attr_name, "sprite") == 0)
+      node->sprite = strdup(attr_val);
+    else if(strcmp(attr_name, "run") == 0)
+    {
+      for(i = 0; i < N_OF_ACTIVITIES; i++)
+        if(strcmp(attr_val, activities[i]) == 0)
+          node->activity = i;
+    }
+    else
+      DEBUGMSG(debug_menu_parser, "read_attributes(): unknown attribute %s , omitting\n", attr_name);
+
+  } while(strchr(attr_val, '>') == NULL);
+}
+
+/* recursively read and parse given xml menu file and create menu tree
+   return NULL in case of problems */
+MenuNode* load_menu_from_file(FILE* xml_file)
+{
+  MenuNode* new_node = create_empty_node();
+  char buffer[buf_size];
+  int i;
+
+  DEBUGMSG(debug_menu_parser, "entering load_menu_from_file()\n");
+  fscanf(xml_file, " < %s", buffer);
+
+  if(strcmp(buffer, "menu") == 0)
+  {
+    read_attributes(xml_file, new_node);
+    if(new_node->title == NULL)
+    {
+      DEBUGMSG(debug_menu_parser, "load_menu_from_file(): no title attribute, exiting\n");
+      return NULL;
+    }
+
+    if(new_node->submenu_size > 0)
+    {
+      new_node->submenu = malloc(new_node->submenu_size * sizeof(MenuNode));
+      for(i = 0; i < new_node->submenu_size; i++)
+        new_node->submenu[i] = load_menu_from_file(xml_file);
+    }
+
+    fscanf(xml_file, " </%[^>\n]> ", buffer);
+    if(strcmp(buffer, "menu") != 0)
+      DEBUGMSG(debug_menu_parser, "load_menu_from_file(): warning - no closing menu tag, found %s instead\n", buffer);
+  }
+  else if(strcmp(buffer, "item") == 0)
+  {
+    read_attributes(xml_file, new_node);
+    if(new_node->title == NULL)
+    {
+      DEBUGMSG(debug_menu_parser, "load_menu_from_file(): no title attribute, exiting\n");
+      return NULL;
+    }
+  }
+  else
+  {
+    DEBUGMSG(debug_menu_parser, "load_menu_from_file(): unknown tag: %s\n, exiting", buffer);
+    return NULL;
+  }
+
+  DEBUGMSG(debug_menu_parser, "load_menu_from_file(): node loaded successfully\n");
+  return new_node;
+}
+
+/* recursively free all non-NULL pointers in a menu tree */
+void free_menu(MenuNode* menu)
+{
+  int i;
+
+  if(menu != NULL)
+  {
+    if(menu->title != NULL)
+      free(menu->title);
+    if(menu->sprite != NULL)
+      free(menu->sprite);
+
+    if(menu->submenu != NULL)
+    {
+      for(i = 0; i < menu->submenu_size; i++)
+        if(menu->submenu[i] != NULL)
+        {
+          free_menu(menu->submenu[i]);
+          menu->submenu[i] = NULL;
+        }
+      free(menu->submenu);
+    }
+
+    free(menu);
+  }
+}
+
+/* load menu trees from disk */
+void LoadMenus(void)
+{
+  FILE* main_menu_file = fopen(DATA_PREFIX "/menus/main_menu.xml", "r");
+  if(main_menu_file == NULL)
+  {
+    DEBUGMSG(debug_menu, "LoadMenus(): Could not load main menu file !\n");
+  }
+  else
+  {
+    menus[MENU_MAIN] = load_menu_from_file(main_menu_file);
+    fclose(main_menu_file);
+  }
+}
+
+/* calculate proper sizes and positions of menu elements,
+   load sprites */
+void RenderMenus(void)
+{
+
+}
+
+/* free all loaded menu trees */
+void UnloadMenus(void)
+{
+  int i;
+
+  DEBUGMSG(debug_menu, "entering UnloadMenus()\n");
+  for(i = 0; i < N_OF_MENUS; i++)
+    free_menu(menus[i]);
+}
+

Added: tuxmath/trunk/src/menu.h
===================================================================
--- tuxmath/trunk/src/menu.h	                        (rev 0)
+++ tuxmath/trunk/src/menu.h	2009-06-04 17:54:34 UTC (rev 1010)
@@ -0,0 +1,51 @@
+/*
+  menu.h
+
+  Functions responsible for loading, parsing and displaying game menu.
+  (interface)
+
+  Part of "Tux4Kids" Project
+  http://www.tux4kids.com/
+
+  Author: Boleslaw Kulbabinski <bkulbabinski at gmail.com>, (C) 2009
+
+  Copyright: See COPYING file that comes with this distribution.
+*/
+
+#ifndef MENU_H
+#define 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 ACTIVITIES \
+  X( RUN_SUBMENU ),\
+  X( RUN_HELP ),\
+  X( N_OF_ACTIVITIES )  /* this one has to be the last one */
+
+/* create enum */
+#define X(name) name
+typedef enum {
+  ACTIVITIES
+} ActivityType;
+#undef X
+
+struct mNode {
+  char* title;
+  char* sprite;
+
+  ActivityType activity;
+
+  /* submenu available only if activity = RUN_SUBMENU */
+  int submenu_size;
+  struct mNode** submenu;
+};
+
+typedef struct mNode MenuNode;
+
+/* global functions */
+void LoadMenus(void);
+void RenderMenus(void);
+void UnloadMenus(void);
+
+#endif // MENU_H

Modified: tuxmath/trunk/src/setup.c
===================================================================
--- tuxmath/trunk/src/setup.c	2009-06-01 00:52:28 UTC (rev 1009)
+++ tuxmath/trunk/src/setup.c	2009-06-04 17:54:34 UTC (rev 1010)
@@ -45,6 +45,7 @@
 #include "fileops.h"
 #include "loaders.h"
 #include "game.h"
+#include "menu.h"
 #include "titlescreen.h"
 #include "highscore.h"
 #include "SDL_extras.h"
@@ -85,7 +86,26 @@
 Mix_Music* musics[NUM_MUSICS];
 #endif
 
+/* global debug masks */
+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_svg            = 1 << 9;
+const int debug_mathcards      = 1 << 10;
+const int debug_sdl            = 1 << 11;
+const int debug_lessons        = 1 << 12;
+const int debug_highscore      = 1 << 13;
+const int debug_all            = ~0;
+
 /* Local function prototypes: */
 void initialize_options(void);
 void handle_command_args(int argc, char* argv[]);
@@ -112,6 +132,8 @@
   initialize_options();
   /* Command-line code now in own function: */
   handle_command_args(argc, argv);
+  /* load menus */
+  LoadMenus();
   /* SDL setup in own function:*/
   initialize_SDL();
   /* Read image and sound files: */
@@ -400,7 +422,18 @@
       Opts_SetSpeed(strtod(argv[i + 1], (char **) NULL));
       i++;
     }
-
+    else if (strcmp(argv[i], "--debug-all") == 0)
+    {
+      debug_status |= debug_all;
+    }
+    else if (strcmp(argv[i], "--debug-menu") == 0)
+    {
+      debug_status |= debug_menu;
+    }
+    else if (strcmp(argv[i], "--debug-menu-parser") == 0)
+    {
+      debug_status |= debug_menu_parser;
+    }
     else
     /* TODO try to match unrecognized strings to config file names */
     {
@@ -714,6 +747,7 @@
     n_timesopened--;
   }
 
+  UnloadMenus();
 
   // Finally, quit SDL
   SDL_Quit();




More information about the Tux4kids-commits mailing list