[Tux4kids-commits] r9 - in tuxmath/trunk: docs src

dbruce at alioth.debian.org dbruce at alioth.debian.org
Thu Mar 8 20:58:02 CET 2007


Author: dbruce
Date: 2006-03-08 13:36:25 +0000 (Wed, 08 Mar 2006)
New Revision: 9

Added:
   tuxmath/trunk/src/tuxmath.h
Modified:
   tuxmath/trunk/docs/CHANGES.txt
   tuxmath/trunk/src/credits.c
   tuxmath/trunk/src/game.c
   tuxmath/trunk/src/options.c
   tuxmath/trunk/src/playsound.c
   tuxmath/trunk/src/setup.c
   tuxmath/trunk/src/setup.h
   tuxmath/trunk/src/title.c
   tuxmath/trunk/src/tuxmath.c
Log:
centralization of options into two new structs: math_options and game_options


Modified: tuxmath/trunk/docs/CHANGES.txt
===================================================================
--- tuxmath/trunk/docs/CHANGES.txt	2006-03-01 17:19:00 UTC (rev 8)
+++ tuxmath/trunk/docs/CHANGES.txt	2006-03-08 13:36:25 UTC (rev 9)
@@ -1,9 +1,20 @@
 CHANGES.txt for "tuxmath"
 
+2006.Mar.8 (https://svn.tux4kids.net/tuxmath/ - revision 7)
+  Setup:
+    * updated usage() to include all command-line options
+  Credits:
+    * updated credits to include more recent contributions
+  Code:
+    * added tuxmath.h containing the global structs math_options
+      and game_options as well as defaults for all values.
+      Goal is to have one place to look for all global data.
+      David Bruce <dbruce at tampabay.rr.com>
+
 2006.Mar.1 (https://svn.tux4kids.net/tuxmath/ - revision 6)
   Game:
     * Added support for negative subtraction answers.
-      Updated led drawing to include negative sign.
+      Updated LED drawing to include negative sign.
       Updated on-screen keypad to include '+' and '-'. 
       David Bruce <dbruce at tampabay.rr.com>
 

Modified: tuxmath/trunk/src/credits.c
===================================================================
--- tuxmath/trunk/src/credits.c	2006-03-01 17:19:00 UTC (rev 8)
+++ tuxmath/trunk/src/credits.c	2006-03-08 13:36:25 UTC (rev 9)
@@ -29,7 +29,7 @@
 
 char * credit_text[] = {
   "-TUX, OF MATH COMMAND",  /* '-' at beginning makes highlighted: */
-  "COPYRIGHT 2001-2005",
+  "COPYRIGHT 2001-2006",
   "",
   "PART OF THE 'TUX4KIDS' PROJECT",
   "",
@@ -44,6 +44,8 @@
   "GLEN DITCHFIELD",
   "MICHAEL BEHRISCH",
   "DONNY VISZNEKI",
+  "YVES COMBE",
+  "DAVID BRUCE",
   "",
   "-LEAD ARTIST",
   "SAM HART",
@@ -74,6 +76,7 @@
   "PETE SALZMAN",
   "ST. CATHERINE ELEM., CINCINNATI, OH",
   "WESTWOOD ELEMENTARY, CINCINNATI, OH",
+  "LAURA BRUCE",
   "",
   "",
   "-WEBSITE",

Modified: tuxmath/trunk/src/game.c
===================================================================
--- tuxmath/trunk/src/game.c	2006-03-01 17:19:00 UTC (rev 8)
+++ tuxmath/trunk/src/game.c	2006-03-08 13:36:25 UTC (rev 9)
@@ -29,8 +29,8 @@
 #include "setup.h"
 #include "sounds.h"
 #include "playsound.h"
+#include "tuxmath.h"
 
-
 #define FPS (1000 / 15)   /* 15 fps max */
 
 #define CITY_EXPL_START 3 * 5  /* Must be mult. of 5 (number of expl frames) */
@@ -230,7 +230,7 @@
 
 
 
-      if (demo_mode)
+      if (game_options->demo_mode)
       {
         /* Demo mode! */
         /* FIXME update this to handle negatives correctly */
@@ -298,8 +298,8 @@
 	if (demo_countdown <= 0 || num_cities_alive == 0)
           done = 1;
       }
+      /* end of demo mode code */
       
-      
       /* Handle answer: */
       
       if (doing_answer)
@@ -483,7 +483,7 @@
 	      num_comets_alive++;
 
 	      comets[i].x = comets[i].x + 0;
-	      comets[i].y = comets[i].y + (speed * wave);
+	      comets[i].y = comets[i].y + (game_options->speed * wave);
 	      
 	      if (comets[i].y >= (screen->h - images[IMG_CITY_BLUE]->h) &&
 	          comets[i].expl < COMET_EXPL_END)
@@ -634,7 +634,7 @@
 
       /* Draw "Demo" */
 
-      if (demo_mode)
+      if (game_options->demo_mode)
       {
 	dest.x = (screen->w - images[IMG_DEMO]->w) / 2;
 	dest.y = (screen->h - images[IMG_DEMO]->h) / 2;
@@ -779,10 +779,9 @@
 
 
       /* Draw numeric keypad: */
-      /* TODO should have keypad with grayed-out '+' and '-' when no negatives allowed */
-      if (use_keypad)
+      if (game_options->use_keypad)
       {
-        if (allow_neg_answer)
+        if (math_options->allow_neg_answer)
         /* draw regular keypad */
         { 
           dest.x = (screen->w - images[IMG_KEYPAD]->w) / 2;
@@ -817,27 +816,6 @@
       /* LED code moved into separate function by DSB */
       draw_led_nums();
 
-
-      /* Draw LED digits at the top of the screen: */
-      /* TODO add negative sign DSB */ 
-/* 
-     for (i = 0; i < 3; i++)
-	{
-	  src.x = digits[i] * ((images[IMG_LEDNUMS]->w) / 10);
-	  src.y = 0;
-	  src.w = (images[IMG_LEDNUMS]->w) / 10;
-	  src.h = images[IMG_LEDNUMS]->h;
-	  
-	  dest.x = (((screen->w - (((images[IMG_LEDNUMS]->w) / 10) * 3)) / 2) +
-		    (i * (images[IMG_LEDNUMS]->w) / 10));
-	  dest.y = 4;
-	  dest.w = src.w;
-	  dest.h = src.h;
-	  
-	  SDL_BlitSurface(images[IMG_LEDNUMS], &src, screen, &dest);
-	}
-*/
-
       /* Draw "Game Over" */
 
       if (gameover > 0)
@@ -868,7 +846,7 @@
       /* Keep playing music: */
       
 #ifndef NOSOUND
-      if (use_sound)
+      if (game_options->use_sound)
 	{
 	  if (!Mix_PlayingMusic())
 	    Mix_PlayMusic(musics[MUS_GAME + (rand() % 3)], 0);
@@ -894,7 +872,7 @@
 
   /* Stop music: */
 #ifndef NOSOUND
-  if (use_sound)
+  if (game_options->use_sound)
   {
     if (Mix_PlayingMusic())
     {
@@ -949,7 +927,7 @@
     SDL_FreeSurface(bkgd);
 
   
-  if (use_bkgd == 1)
+  if (game_options->use_bkgd)
   {
     bkgd = IMG_Load(fname);
     if (bkgd == NULL)
@@ -959,7 +937,7 @@
 	      "%s\n"
 	      "The Simple DirectMedia error that ocurred was: %s\n",
 	      fname, SDL_GetError());
-      use_bkgd = 0;
+      game_options->use_bkgd = 0;
     }
   }
 
@@ -1021,7 +999,8 @@
 
 
       /* Pick an operation (+, -, *, /): */
-     
+      /* FIXME now that negative answers a possibility, */
+      /* should test absolute value against max_answer  */
       do
       { 
         comets[found].oper = (rand() % NUM_OPERS);
@@ -1039,7 +1018,7 @@
 	    comets[found].eq2 = pick_operand(0);
 	    comets[found].answer = comets[found].eq1 + comets[found].eq2;
 	  }
-	  while (comets[found].answer > max_answer);
+	  while (comets[found].answer > math_options->max_answer);
 	}
 
       else if (comets[found].oper == OPER_SUB)
@@ -1052,7 +1031,7 @@
 	    comets[found].eq2 = pick_operand(0);
 	    /* try again until answer not negative  */
 	    /* unless neg_answer_allowed DSB */
-	    if (!allow_neg_answer)
+	    if (!math_options->allow_neg_answer)
             {
 	      do
 	      {
@@ -1062,16 +1041,19 @@
             }
 	    comets[found].answer = comets[found].eq1 - comets[found].eq2;
 	  }
-	  while (comets[found].answer > max_answer);
+	  while (comets[found].answer > math_options->max_answer);
 	}
 
       else if (comets[found].oper == OPER_MULT)
 	{
 	  /* Multiplication: */
-	
-          comets[found].eq1 = pick_operand(1);
-	  comets[found].eq2 = pick_operand(0);
-	  comets[found].answer = comets[found].eq1 * comets[found].eq2;
+	  do
+          {
+            comets[found].eq1 = pick_operand(1);
+	    comets[found].eq2 = pick_operand(0);
+	    comets[found].answer = comets[found].eq1 * comets[found].eq2;
+          }
+	  while (comets[found].answer > math_options->max_answer);
 	}
 
       else if (comets[found].oper == OPER_DIV)
@@ -1092,7 +1074,7 @@
 	  
 	    comets[found].answer = comets[found].eq1 / comets[found].eq2;
 	  }
-	  while (comets[found].answer > max_answer ||
+	  while (comets[found].answer > math_options->max_answer ||
 		 !in_range(comets[found].eq1 / 2));
 	}
       snprintf(comets[found].formula, FORMULA_LEN,"%d%c%d",
@@ -1248,7 +1230,7 @@
 
 
 #ifndef NOSOUND
-  if (use_sound)
+  if (game_options->use_sound)
     Mix_PauseMusic();
 #endif
   
@@ -1269,7 +1251,7 @@
 
 
 #ifndef NOSOUND
-  if (use_sound)
+  if (game_options->use_sound)
     Mix_ResumeMusic();
 #endif
 
@@ -1419,7 +1401,7 @@
 
   /* begin drawing so as to center display depending on whether minus */
   /* sign needed (4 digit slots) or not (3 digit slots) DSB */
-  if (allow_neg_answer)
+  if (math_options->allow_neg_answer)
     dest.x = ((screen->w - ((images[IMG_LEDNUMS]->w) / 10) * 4) / 2);
   else
     dest.x = ((screen->w - ((images[IMG_LEDNUMS]->w) / 10) * 3) / 2);
@@ -1429,7 +1411,7 @@
   { 
     if (-1 == i)
     {
-      if (allow_neg_answer)
+      if (math_options->allow_neg_answer)
       {
         if (neg_answer_picked)
           src.x =  (images[IMG_LED_NEG_SIGN]->w) / 2;
@@ -1469,43 +1451,60 @@
 }
 
 /* Translates mouse events into keyboard events when on-screen keypad used */
-/* FIXME this code uses IMG_KEYPAD to figure out the mouse events even     */
-/* if IMG_KEYPAD_NO_NEG is actually on the screen. This relies on the two  */
-/* graphics having the same shape and key layout. If this changes in the   */
-/* future, this function will need to be rewritten                         */
-
 void game_mouse_event(SDL_Event the_event)
 {
-  int x, y, row, column;
+  int keypad_w, keypad_h, x, y, row, column;
   SDLKey keypad_key = SDLK_UNKNOWN;
 
+  keypad_w = 0;
+  keypad_h = 0;
+
   /* get out unless we really are using keypad */
   if ( level_start_wait 
-    || demo_mode
-    || !use_keypad)
+    || game_options->demo_mode
+    || !game_options->use_keypad)
   {
     return;
   }
   /* make sure keypad image is valid and has non-zero dimensions: */
-  if (!images[IMG_KEYPAD])
+  /* FIXME maybe this checking should be done once at the start */
+  /* of game() rather than with every mouse click */
+  if (math_options->allow_neg_answer)
   {
-    return;
+    if (!images[IMG_KEYPAD])
+      return;
+    else
+    {
+      keypad_w = images[IMG_KEYPAD]->w;
+      keypad_h = images[IMG_KEYPAD]->h;
+    }
   }
-  if (!(images[IMG_KEYPAD]->w)
-    ||!(images[IMG_KEYPAD]->h))
+  else
   {
+    if (!images[IMG_KEYPAD_NO_NEG])
+      return;
+    else
+    {
+      keypad_w = images[IMG_KEYPAD]->w;
+      keypad_h = images[IMG_KEYPAD]->h;
+    }
+  }
+
+ 
+  if (!keypad_w || !keypad_h)
+  {
     return;
   }
 
   /* only proceed if click falls within keypad: */
   if (!((the_event.button.x >=
-        (screen->w / 2) - (images[IMG_KEYPAD]->w / 2) &&
+        (screen->w / 2) - (keypad_w / 2) &&
         the_event.button.x <=
-        (screen->w / 2) + (images[IMG_KEYPAD]->w / 2) &&
+        (screen->w / 2) + (keypad_w / 2) &&
         the_event.button.y >= 
-        (screen->h / 2) - (images[IMG_KEYPAD]->h / 2) &&
+        (screen->h / 2) - (keypad_h / 2) &&
         the_event.button.y <=
-        (screen->h / 2) + (images[IMG_KEYPAD]->h / 2))))
+        (screen->h / 2) + (keypad_h / 2))))
   /* click outside of keypad - do nothing */
   {
     return;
@@ -1513,8 +1512,8 @@
   
   else /* click was within keypad */ 
   {
-    x = (the_event.button.x - ((screen->w / 2) - (images[IMG_KEYPAD]->w / 2)));
-    y = (the_event.button.y - ((screen->h / 2) - (images[IMG_KEYPAD]->h / 2)));
+    x = (the_event.button.x - ((screen->w / 2) - (keypad_w / 2)));
+    y = (the_event.button.y - ((screen->h / 2) - (keypad_h / 2)));
  
   /* Now determine what onscreen key was pressed */
   /*                                             */
@@ -1542,8 +1541,8 @@
   /*  row and column based on x and y and looks  */
   /*  up the SDlKey accordingly.                 */
 
-    column = x/((images[IMG_KEYPAD]->w)/4);
-    row    = y/((images[IMG_KEYPAD]->h)/4);
+    column = x/((keypad_w)/4);
+    row    = y/((keypad_h)/4);
 
     /* make sure row and column are sane */
     if (column < 0
@@ -1628,7 +1627,7 @@
     paused = 1;
   }
       
-  if (level_start_wait > 0 || demo_mode)
+  if (level_start_wait > 0 || game_options->demo_mode)
   {
     /* Eat other keys until level start wait has passed,
     or if game is in demo mode: */
@@ -1653,14 +1652,14 @@
   }
   /* support for negative answer input DSB */
   else if ((pressed_key == SDLK_MINUS || pressed_key == SDLK_KP_MINUS)
-        && allow_neg_answer)  /* do nothing unless neg answers allowed */
+        && math_options->allow_neg_answer)  /* do nothing unless neg answers allowed */
   {
     /* allow player to make answer negative: */
     neg_answer_picked = 1;
     tux_pressing = 1;
   }
   else if ((pressed_key == SDLK_PLUS || pressed_key == SDLK_KP_PLUS)
-         && allow_neg_answer)  /* do nothing unless neg answers allowed */
+         && math_options->allow_neg_answer)  /* do nothing unless neg answers allowed */
   {
     /* allow player to make answer positive: */
     neg_answer_picked = 0;

Modified: tuxmath/trunk/src/options.c
===================================================================
--- tuxmath/trunk/src/options.c	2006-03-01 17:19:00 UTC (rev 8)
+++ tuxmath/trunk/src/options.c	2006-03-08 13:36:25 UTC (rev 9)
@@ -19,13 +19,18 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+
 #include <SDL.h>
+
 #include "options.h"
 #include "images.h"
 #include "setup.h"
 #include "sounds.h"
 #include "playsound.h"
 #include "game.h"
+#include "tuxmath.h"
+/* FIXME figure out what oper_override is supposed to do and make sure */
+/* this file behaves accordingly! */
 
 /* Local (to options.c) 'globals': */
 /* moved to file scope to allow update_selected_option() to use them DSB */
@@ -76,7 +81,7 @@
     {
       /* Maximum answer: */
 
-      snprintf(tmp_str, sizeof(tmp_str), "%04d", max_answer);
+      snprintf(tmp_str, sizeof(tmp_str), "%04d", math_options->max_answer);
       draw_nums(tmp_str,
 		screen->w - ((images[IMG_NUMS]->w / 14) * 2) - 16,
 		y + images[IMG_OPT_MAX_ANSWER]->h);
@@ -86,9 +91,9 @@
     }
     else if (i == OPT_A_SPEED)
     {
-      /* Maximum answer: */
+      /* Speed control: */
 
-      snprintf(tmp_str, sizeof(tmp_str), "%.1f", speed);
+      snprintf(tmp_str, sizeof(tmp_str), "%.1f", game_options->speed);
       draw_nums(tmp_str,
 		screen->w - ((images[IMG_NUMS]->w / 14) * 2) - 16,
 		y + images[IMG_OPT_SPEED]->h);
@@ -313,9 +318,9 @@
 
     else if (option == OPT_A_MAX)
     {
-      max_answer = (max_answer * 2) / 3;
-      if (max_answer < 12)
-        max_answer = 144;
+      math_options->max_answer = (math_options->max_answer * 2) / 3;
+      if (math_options->max_answer < 12)
+        math_options->max_answer = 144;
 
       dest.x = screen->w - ((images[IMG_NUMS]->w / 14) * 4) - 16;
       dest.y = (images[IMG_OPTIONS]->h + 8 +
@@ -325,7 +330,7 @@
       dest.h = images[IMG_OPT_MAX_ANSWER]->h;
 
       SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, 0, 0, 0));
-      snprintf(tmp_str, sizeof(tmp_str), "%04d", max_answer);
+      snprintf(tmp_str, sizeof(tmp_str), "%04d", math_options->max_answer);
       draw_nums(tmp_str,
                 screen->w - ((images[IMG_NUMS]->w / 14) * 2) - 16,
                 (images[IMG_OPTIONS]->h + 8 +
@@ -336,9 +341,9 @@
 
     else if (option == OPT_A_SPEED)
     {
-      speed = speed - 0.1;
-      if (speed < 0.1)
-        speed = 5;
+      game_options->speed = game_options->speed - 0.1;
+      if (game_options->speed < 0.1)
+        game_options->speed = 5;
 
       dest.x = screen->w - ((images[IMG_NUMS]->w / 14) * 4) - 16;
       dest.y = (images[IMG_OPTIONS]->h + 8 +
@@ -349,7 +354,7 @@
 
       SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, 0, 0, 0));
 
-      snprintf(tmp_str, sizeof(tmp_str), "%.1f", speed);
+      snprintf(tmp_str, sizeof(tmp_str), "%.1f", game_options->speed);
       draw_nums(tmp_str,
                 screen->w - ((images[IMG_NUMS]->w / 14) * 2) - 16,
                 (images[IMG_OPTIONS]->h + 8 +

Modified: tuxmath/trunk/src/playsound.c
===================================================================
--- tuxmath/trunk/src/playsound.c	2006-03-01 17:19:00 UTC (rev 8)
+++ tuxmath/trunk/src/playsound.c	2006-03-08 13:36:25 UTC (rev 9)
@@ -22,12 +22,12 @@
 #include <SDL_mixer.h>
 #endif
 #include "setup.h"
+#include "tuxmath.h"
 
-
 void playsound(int snd)
 {
 #ifndef NOSOUND
-  if (use_sound)
+  if (game_options->use_sound)
     Mix_PlayChannel(-1, sounds[snd], 0);
 #endif
 }

Modified: tuxmath/trunk/src/setup.c
===================================================================
--- tuxmath/trunk/src/setup.c	2006-03-01 17:19:00 UTC (rev 8)
+++ tuxmath/trunk/src/setup.c	2006-03-08 13:36:25 UTC (rev 9)
@@ -22,11 +22,14 @@
 #include <stdlib.h>
 #include <string.h>
 #include <stdlib.h>
+
 #include <SDL.h>
 #ifndef NOSOUND
 #include <SDL_mixer.h>
 #endif
 #include <SDL_image.h>
+
+#include "tuxmath.h"
 #include "setup.h"
 #include "images.h"
 #include "sounds.h"
@@ -150,27 +153,29 @@
   DATA_PREFIX "/sounds/game3.mod"
 };
 
+/* Global data used in setup.c:              */
 /* (These need to be 'extern'd in "setup.h") */
-
 SDL_Surface * screen;
 SDL_Surface * images[NUM_IMAGES];
 #ifndef NOSOUND
 Mix_Chunk * sounds[NUM_SOUNDS];
 Mix_Music * musics[NUM_MUSICS];
 #endif
-int use_sound, fullscreen, use_bkgd, demo_mode,
-    oper_override, use_keypad, allow_neg_answer;
-float speed;
+
 int opers[NUM_OPERS], range_enabled[NUM_Q_RANGES];
-int max_answer;
 
+math_option_type* math_options;
+game_option_type* game_options;
 
 /* Local function prototypes: */
 
 void seticon(void);
 void usage(int err, char * cmd);
+int initialize_math_options(math_option_type* opts);
+int initialize_game_options(game_option_type* opts);
+void print_math_options(math_option_type* opts);
+void print_game_options(game_option_type* opts);
 
-
 /* --- Set-up function! --- */
 
 void setup(int argc, char * argv[])
@@ -180,19 +185,26 @@
 
   screen = NULL;
 
-  /* Set default options: */
+  /* initialize game_options and math_options structs with defaults DSB */
+  /* FIXME: Program should load options from disk */
+  math_options = malloc(sizeof(math_option_type));
+  if (!initialize_math_options(math_options))
+  {
+    printf("\nUnable to initialize math_options");
+    fprintf(stderr, "\nUnable to initialize math_options");
+    exit(1);
+  }
 
-  use_sound = 1;
-  fullscreen = 0;
-  use_bkgd = 1;
-  demo_mode = 0;
-  use_keypad = 0;
-  speed = 1.0;
-  oper_override = 0;
-  max_answer = 144;
-  allow_neg_answer = 0;
+  game_options = malloc(sizeof(game_option_type));
+  if (!initialize_game_options(game_options))
+  {
+    printf("\nUnable to initialize game_options");
+    fprintf(stderr, "\nUnable to initialize game_options");
+    exit(1);
+  }
 
 
+
   for (i = 0; i < NUM_OPERS; i++)
   {
     opers[i] = 1;
@@ -203,9 +215,7 @@
     range_enabled[i] = 1;
   }
 
-  /* FIXME: Program should load options from disk */
 
-
   /* See if operator settings are being overridden by command-line: */
 
   for (i = 1; i < argc; i++)
@@ -213,14 +223,14 @@
     if (strcmp(argv[i], "--operator") == 0 ||
         strcmp(argv[i], "-o") == 0)
     {
-      oper_override = 1;
+      game_options->oper_override = 1;
     }
   }
 
 
   /* If operator settings are being overridden, clear them first: */
 
-  if (oper_override)
+  if (game_options->oper_override)
   {
     for (i = 0; i < NUM_OPERS; i++)
     {
@@ -295,14 +305,14 @@
     else if (strcmp(argv[i], "--fullscreen") == 0 ||
 	     strcmp(argv[i], "-f") == 0)
     {
-      fullscreen = 1;
+      game_options->fullscreen = 1;
     }
     else if (strcmp(argv[i], "--nosound") == 0 ||
 	     strcmp(argv[i], "-s") == 0 ||
 	     strcmp(argv[i], "--quiet") == 0 ||
 	     strcmp(argv[i], "-q") == 0)
     {
-      use_sound = 0;
+      game_options->use_sound = 0;
     }
     else if (strcmp(argv[i], "--version") == 0 ||
 	     strcmp(argv[i], "-v") == 0)
@@ -314,22 +324,22 @@
     else if (strcmp(argv[i], "--nobackground") == 0 ||
              strcmp(argv[i], "-b") == 0)
     {
-      use_bkgd = 0;
+      game_options->use_bkgd = 0;
     }
     else if (strcmp(argv[i], "--demo") == 0 ||
 	     strcmp(argv[i], "-d") == 0)
     {
-      demo_mode = 1;
+      game_options->demo_mode = 1;
     }
     else if (strcmp(argv[i], "--keypad") == 0 ||
              strcmp(argv[i], "-k") == 0)
     {
-      use_keypad = 1;
+      game_options->use_keypad = 1;
     }
     else if (strcmp(argv[i], "--allownegatives") == 0 ||
              strcmp(argv[i], "-n") == 0)
     {
-      allow_neg_answer = 1;
+      math_options->allow_neg_answer = 1;
     }
     else if (strcmp(argv[i], "--speed") == 0 ||
 	     strcmp(argv[i], "-s") == 0)
@@ -340,9 +350,9 @@
 	usage(1, argv[0]);
       }
 
-      speed = strtod(argv[i + 1], (char **) NULL);
+      game_options->speed = strtod(argv[i + 1], (char **) NULL);
 
-      if (speed <= 0)
+      if (game_options->speed <= 0)
       {
 	fprintf(stderr, "Invalided argument to %s: %s\n",
 		argv[i], argv[i + 1]);
@@ -389,10 +399,10 @@
   }
 
 
-  if (demo_mode && use_keypad)
+  if (game_options->demo_mode && game_options->use_keypad)
   {
     fprintf(stderr, "No use for keypad in demo mode!\n");
-    use_keypad = 0;
+    game_options->use_keypad = 0;
   }
 
   
@@ -411,7 +421,7 @@
 
 #ifndef NOSOUND
 
-  if (use_sound)
+  if (game_options->use_sound)
     { 
       if (SDL_Init(SDL_INIT_AUDIO) < 0)
         {
@@ -419,11 +429,11 @@
   	          "\nWarning: I could not initialize audio!\n"
 	          "The Simple DirectMedia error that occured was:\n"
 	          "%s\n\n", SDL_GetError());
-	  use_sound = 0;
+	  game_options->use_sound = 0;
         }
     }
   
-  if (use_sound)
+  if (game_options->use_sound)
     {
       if (Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 2048) < 0)
         {
@@ -432,14 +442,14 @@
 	          "16-bit stereo.\n"
 	          "The Simple DirectMedia error that occured was:\n"
 	          "%s\n\n", SDL_GetError());
-          use_sound = 0;
+          game_options->use_sound = 0;
         }
     }
 
 #endif
   
 
-  if (fullscreen)
+  if (game_options->fullscreen)
   {
     screen = SDL_SetVideoMode(640, 480, 16, SDL_FULLSCREEN | SDL_HWSURFACE);
 
@@ -449,11 +459,11 @@
               "\nWarning: I could not open the display in fullscreen mode.\n"
 	      "The Simple DirectMedia error that occured was:\n"
 	      "%s\n\n", SDL_GetError());
-      fullscreen = 0;
+      game_options->fullscreen = 0;
     }
   }
 
-  if (!fullscreen)
+  if (!game_options->fullscreen)
   {
     screen = SDL_SetVideoMode(640, 480, 16, SDL_HWSURFACE);
   }
@@ -472,7 +482,7 @@
   SDL_WM_SetCaption("Tux, of Math Command", "TuxMath");
 
 
-  if (use_sound)
+  if (game_options->use_sound)
     total_files = NUM_IMAGES + NUM_SOUNDS + NUM_MUSICS;
   else
     total_files = NUM_IMAGES;
@@ -535,7 +545,7 @@
   }
 
 #ifndef NOSOUND
-  if (use_sound)
+  if (game_options->use_sound)
   {
     for (i = 0; i < NUM_SOUNDS; i++)
     {
@@ -602,7 +612,191 @@
     }
 }
 
+/* free any heap memory used during game DSB */
+void cleanup()
+{
+  if (math_options)
+    free(math_options);
+  if (game_options)
+    free(game_options);
+}
 
+/* Set up math_options struct with defaults from tuxmath.h, */
+/* with simple sanity check for negatives                   */
+/* FIXME Should there be more error checking here?          */
+
+int initialize_math_options(math_option_type* opts)
+{
+  /* bail out if no struct */
+  if (!opts)
+    return 0;
+
+  /* set general math options */
+  opts->allow_neg_answer = DEFAULT_ALLOW_NEG_ANSWER;
+  opts->max_answer = DEFAULT_MAX_ANSWER;
+  opts->max_questions = DEFAULT_MAX_QUESTIONS;
+  opts->format_answer_last = DEFAULT_FORMAT_ANSWER_LAST;
+  opts->format_answer_first = DEFAULT_FORMAT_ANSWER_FIRST;
+  opts->format_answer_middle = DEFAULT_FORMAT_ANSWER_MIDDLE;
+  opts->question_copies = DEFAULT_QUESTION_COPIES;
+  /* set addition options */
+  opts->addition_allowed = DEFAULT_ADDITION_ALLOWED;
+  opts->min_augend = DEFAULT_MIN_AUGEND;
+  opts->max_augend = DEFAULT_MAX_AUGEND;
+  opts->min_addend = DEFAULT_MIN_ADDEND;
+  opts->max_addend = DEFAULT_MAX_ADDEND;
+  /* set subtraction options */
+  opts->subtraction_allowed = DEFAULT_SUBTRACTION_ALLOWED;
+  opts->min_minuend = DEFAULT_MIN_MINUEND;
+  opts->max_minuend = DEFAULT_MAX_MINUEND;
+  opts->min_subtrahend = DEFAULT_MIN_SUBTRAHEND;
+  opts->max_subtrahend = DEFAULT_MAX_SUBTRAHEND;
+  /* set multiplication options */
+  opts->multiplication_allowed = DEFAULT_MULTIPLICATION_ALLOWED;
+  opts->min_multiplier = DEFAULT_MIN_MULTIPLIER;
+  opts->max_multiplier = DEFAULT_MAX_MULTIPLIER;
+  opts->min_multiplicand = DEFAULT_MIN_MULTIPLICAND;
+  opts->max_multiplicand = DEFAULT_MAX_MULTIPLICAND;
+  /* set division options */
+  opts->division_allowed = DEFAULT_DIVISION_ALLOWED;
+  opts->min_divisor = DEFAULT_MIN_DIVISOR;
+  opts->max_divisor = DEFAULT_MAX_DIVISOR;
+  opts->min_quotient = DEFAULT_MIN_QUOTIENT;
+  opts->max_quotient = DEFAULT_MAX_QUOTIENT;
+
+  /* if no negatives to be used, reset any negatives to 0 */
+  if (!opts->allow_neg_answer)
+  {
+    if (opts->min_augend < 0)
+      opts->min_augend = 0;
+    if (opts->max_augend < 0)
+      opts->max_augend = 0;
+    if (opts->min_addend < 0)
+      opts->min_addend = 0;
+    if (opts->max_addend < 0)
+      opts->max_addend = 0;
+
+    if (opts->min_minuend < 0)
+      opts->min_minuend = 0;
+    if (opts->max_minuend < 0)
+      opts->max_minuend = 0;
+    if (opts->min_subtrahend < 0)
+      opts->min_subtrahend = 0;
+    if (opts->max_subtrahend < 0)
+      opts->max_subtrahend = 0;
+
+    if (opts->min_multiplier < 0)
+      opts->min_multiplier = 0;
+    if (opts->max_multiplier < 0)
+      opts->max_multiplier = 0;
+    if (opts->min_multiplicand < 0)
+      opts->min_multiplicand = 0;
+    if (opts->max_multiplicand < 0)
+      opts->max_multiplicand = 0;
+
+    if (opts->min_divisor < 0)
+      opts->min_divisor = 0;
+    if (opts->max_divisor < 0)
+      opts->max_divisor = 0;
+    if (opts->min_quotient < 0)
+      opts->min_quotient = 0;
+    if (opts->max_quotient < 0)
+      opts->max_quotient = 0;
+  }
+  
+  /* for testing purposes */
+  /* print_math_options(opts); */ 
+  return 1;
+}
+
+int initialize_game_options(game_option_type* opts)
+{
+  /* bail out if no struct */
+  if (!opts)
+    return 0;
+
+  /* set general game options */
+  opts->use_sound = DEFAULT_USE_SOUND;
+  opts->fullscreen = DEFAULT_FULLSCREEN;
+  opts->use_bkgd = DEFAULT_USE_BKGD;
+  opts->demo_mode = DEFAULT_DEMO_MODE;
+  opts->oper_override = DEFAULT_OPER_OVERRIDE;
+  opts->use_keypad = DEFAULT_USE_KEYPAD;
+  opts->speed = DEFAULT_SPEED;
+  opts->allow_speedup = DEFAULT_ALLOW_SPEEDUP;
+  opts->reuse_questions = DEFAULT_REUSE_QUESTIONS;
+  opts->max_comets = DEFAULT_MAX_COMETS;
+  opts->num_cities = DEFAULT_NUM_CITIES;   /* MUST BE AN EVEN NUMBER! */
+  opts->num_bkgds = DEFAULT_NUM_BKGDS;
+  opts->max_city_colors = DEFAULT_MAX_CITY_COLORS;
+
+  /* for testing purposes */
+  /* print_game_options(opts); */
+  return 1;
+}
+
+/* prints struct to stdout for testing purposes */
+void print_math_options(math_option_type* opts)
+{
+ /* bail out if no struct */
+  if (!opts)
+    return;
+
+  printf("\nPrinting members of math_options struct:\n");
+  printf("\nGeneral math options:\n");
+  printf("allow_neg_answer:\t%d\n", opts->allow_neg_answer);
+  printf("max_answer:\t%d\n", opts->max_answer);
+  printf("max_questions:\t%d\n", opts->max_questions);
+  printf("format_answer_last:\t%d\n", opts->format_answer_last);
+  printf("format_answer_first:\t%d\n", opts->format_answer_first);
+  printf("format_answer_middle:\t%d\n", opts->format_answer_middle);
+  printf("question_copies:\t%d\n", opts->question_copies);
+
+  printf("\nSpecific math operation options:\n");
+  printf("addition_allowed:\t%d\n", opts->addition_allowed);
+  printf("min_augend:\t%d\n", opts->min_augend);
+  printf("max_augend:\t%d\n", opts->max_augend);
+  printf("min_addend:\t%d\n", opts->min_addend);
+  printf("max_addend:\t%d\n", opts->max_addend);
+
+  printf("subtraction_allowed\t%d\n", opts->subtraction_allowed);
+  printf("min_minuend:\t%d\n", opts->min_minuend);
+  printf("max_minuend:\t%d\n", opts->max_minuend);
+  printf("min_subtrahend:\t%d\n", opts->min_subtrahend);
+  printf("max_subtrahend:\t%d\n", opts->max_subtrahend);
+
+  printf("multiplication_allowed:\t%d\n", opts->multiplication_allowed);
+  printf("min_multiplier:\t%d\n", opts->min_multiplier);
+  printf("max_multiplier:\t%d\n", opts->max_multiplier);
+  printf("min_multiplicand:\t%d\n", opts->min_multiplicand);
+  printf("max_multiplicand:\t%d\n", opts->max_multiplicand);
+
+  printf("division_allowed:\t%d\n", opts->division_allowed);
+  printf("min_divisor:\t%d\n",opts->min_divisor);
+  printf("max_divisor:\t%d\n", opts->max_divisor);
+  printf("min_quotient:\t%d\n", opts->min_quotient);
+  printf("max_quotient:\t%d\n", opts->max_quotient);
+}
+
+/* prints struct to stdout for testing purposes */
+void print_game_options(game_option_type* opts)
+{
+ /* bail out if no struct */
+  if (!opts)
+    return;
+
+  printf("\nPrinting members of game_options struct:\n");
+  printf("\nGeneral game options:\n");
+  printf("use_sound:\t%d\n", opts->use_sound);
+  printf("fullscreen:\t%d\n", opts->fullscreen);
+  printf("use_bkgd:\t%d\n", opts->use_bkgd);
+  printf("demo_mode:\t%d\n", opts->demo_mode);
+  printf("oper_override:\t%d\n", opts->oper_override);
+  printf("use_keypad:\t%d\n", opts->use_keypad);
+  printf("reuse_questions:\t%d\n", opts->reuse_questions);
+  printf("speed:\t%f\n", opts->speed);
+}
+
 /* Set the application's icon: */
 
 void seticon(void)
@@ -648,7 +842,6 @@
   srand(SDL_GetTicks());
 }
 
-
 void usage(int err, char * cmd)
 {
   FILE * f;
@@ -660,10 +853,12 @@
 
   fprintf(f,
    "\nUsage: %s {--help | --usage | --copyright}\n"
-   "       %s [--fullscreen] [--nosound] [--nobackground] [--demo] [--keypad]\n"
+   "       %s [--fullscreen] [--nosound] [--nobackground]\n"
+   "          [--demo] [--keypad] [--allownegatives]\n"
    "          [--operator {add | subtract | multiply | divide} ...]\n"
    "          [--speed <val>]\n"
     "\n", cmd, cmd);
 
   exit (err);
 }
+

Modified: tuxmath/trunk/src/setup.h
===================================================================
--- tuxmath/trunk/src/setup.h	2006-03-01 17:19:00 UTC (rev 8)
+++ tuxmath/trunk/src/setup.h	2006-03-08 13:36:25 UTC (rev 9)
@@ -33,12 +33,13 @@
 extern Mix_Chunk * sounds[];
 extern Mix_Music * musics[];
 #endif
-extern int use_sound, fullscreen, use_bkgd, demo_mode, oper_override,
-  use_keypad, allow_neg_answer;
-extern float speed;
+/* extern int use_sound, fullscreen, use_bkgd, demo_mode, oper_override,
+  use_keypad, allow_neg_answer;*/
+/*extern float speed;*/
 extern int opers[NUM_OPERS], range_enabled[NUM_Q_RANGES];
-extern int max_answer;
+/*extern int max_answer;*/
 
 void setup(int argc, char * argv[]);
+void cleanup(void);
 
 #endif

Modified: tuxmath/trunk/src/title.c
===================================================================
--- tuxmath/trunk/src/title.c	2006-03-01 17:19:00 UTC (rev 8)
+++ tuxmath/trunk/src/title.c	2006-03-08 13:36:25 UTC (rev 9)
@@ -19,7 +19,10 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+
 #include <SDL.h>
+
+#include "tuxmath.h"
 #include "title.h"
 #include "images.h"
 #include "setup.h"
@@ -29,7 +32,6 @@
 
 #define START_DEMO_COUNTDOWN 150  /* Some time unit.. not sure yet :) */
 
-
 int title(void)
 {
   int i, cmd, old_cmd, done, img, blinking, widest, left, width, demo_countdown;
@@ -119,7 +121,7 @@
   SDL_BlitSurface(images[IMG_NBS], NULL, screen, &dest);
 
 
-  if (demo_mode)
+  if (game_options->demo_mode)
   {
     dest.x = (screen->w - images[IMG_DEMO_SMALL]->w) / 2;
     dest.y = (screen->h - images[IMG_DEMO_SMALL]->h);
@@ -265,7 +267,7 @@
 
       /* Handle demo countdown: */
 
-      if (demo_mode)
+      if (game_options->demo_mode)
       {
 	demo_countdown--;
 

Modified: tuxmath/trunk/src/tuxmath.c
===================================================================
--- tuxmath/trunk/src/tuxmath.c	2006-03-01 17:19:00 UTC (rev 8)
+++ tuxmath/trunk/src/tuxmath.c	2006-03-08 13:36:25 UTC (rev 9)
@@ -18,12 +18,17 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+
 #include "setup.h"
 #include "title.h"
 #include "game.h"
 #include "options.h"
 #include "credits.h"
+#include "tuxmath.h"
 
+/* global data: */
+math_option_type* math_options; /* used by setup.c, options.c, game.c */
+game_option_type* game_options; /* used by setup.c, options.c, game.c */
 
 int main(int argc, char * argv[])
 {
@@ -49,7 +54,7 @@
   while (!done);
   
   SDL_Quit();
-
+  cleanup();
   return 0;
 }
 

Added: tuxmath/trunk/src/tuxmath.h
===================================================================
--- tuxmath/trunk/src/tuxmath.h	2006-03-01 17:19:00 UTC (rev 8)
+++ tuxmath/trunk/src/tuxmath.h	2006-03-08 13:36:25 UTC (rev 9)
@@ -0,0 +1,132 @@
+/*
+  tuxmath.h
+
+  For TuxMath
+  Contains global data for configuration of math questions and
+  for general game options, as well as constants and defaults.
+
+  Author: David Bruce <dbruce at tampabay.rr.com>, (C) 2006
+
+
+  Part of "Tux4Kids" Project
+  http://www.tux4kids.org/
+      
+  Added March 2, 2006
+
+  Copyright: See COPYING file that comes with this distribution
+  (briefly - GNU GPL v2 or later)
+*/
+
+#ifndef TUXMATH_H
+#define TUXMATH_H
+
+/* This struct contains all options that determine what */
+/* math questions are asked during a game */
+typedef struct math_option_type {
+  /* general math options */
+  int allow_neg_answer;
+  int max_answer;
+  int max_questions;
+  int format_answer_last;
+  int format_answer_first;
+  int format_answer_middle;
+  int question_copies;
+  /* addition options */
+  int addition_allowed;
+  int min_augend;  /* the "augend" is the first addend i.e. "a" in "a + b = c" */
+  int max_augend;
+  int min_addend;  /* options for the other addend */
+  int max_addend;
+  /* subtraction options */
+  int subtraction_allowed;
+  int min_minuend;  /* minuend - subtrahend = difference */
+  int max_minuend;
+  int min_subtrahend;
+  int max_subtrahend;
+  /* multiplication options */
+  int multiplication_allowed;
+  int min_multiplier;  /* multiplier * multiplicand = product */
+  int max_multiplier;
+  int min_multiplicand;
+  int max_multiplicand;
+  /* division options */
+  int division_allowed;
+  int min_divisor;    /* dividend/divosor = quotient */
+  int max_divisor;
+  int min_quotient;
+  int max_quotient;
+} math_option_type;
+
+/* this struct contains all other options regarding general */
+/* gameplay but not having to do with math questions per se */
+typedef struct game_option_type {
+  /* general game options */
+  int use_sound;
+  int fullscreen;
+  int use_bkgd;
+  int demo_mode;
+  int oper_override;
+  int use_keypad;
+  int reuse_questions;
+  float speed;
+  int allow_speedup;
+  int max_comets;
+  /* not sure the rest of these belong in here */
+  int num_cities;  /* MUST BE AN EVEN NUMBER! */
+  int num_bkgds;
+  int max_city_colors;
+} game_option_type;
+
+/* make option data accessible to rest of program */
+extern math_option_type* math_options; /* used by setup.c, options.c, game.c */
+extern game_option_type* game_options; /* used by setup.c, options.c, game.c */
+
+/* default values for math_options */
+#define DEFAULT_ALLOW_NEG_ANSWER 0
+#define DEFAULT_MAX_ANSWER 144
+#define DEFAULT_MAX_QUESTIONS 5000
+#define DEFAULT_FORMAT_ANSWER_LAST 1      /* question format is: a + b = ? */
+#define DEFAULT_FORMAT_ANSWER_FIRST 0     /* question format is: ? + b = c */
+#define DEFAULT_FORMAT_ANSWER_MIDDLE 0    /* question format is: a + ? = c */
+#define DEFAULT_QUESTION_COPIES 1         /* how many times each question is put in list */
+
+#define DEFAULT_ADDITION_ALLOWED 1
+#define DEFAULT_MIN_AUGEND 0              /* the "augend" is the first addend i.e. "a" in "a + b = c" */
+#define DEFAULT_MAX_AUGEND 12
+#define DEFAULT_MIN_ADDEND 0
+#define DEFAULT_MAX_ADDEND 12
+
+#define DEFAULT_SUBTRACTION_ALLOWED 1     /* minuend - subtrahend = difference */
+#define DEFAULT_MIN_MINUEND 0
+#define DEFAULT_MAX_MINUEND 24
+#define DEFAULT_MIN_SUBTRAHEND 0
+#define DEFAULT_MAX_SUBTRAHEND 12
+
+#define DEFAULT_MULTIPLICATION_ALLOWED 1
+#define DEFAULT_MIN_MULTIPLIER 0          /* multiplier * multiplicand = product */
+#define DEFAULT_MAX_MULTIPLIER 12
+#define DEFAULT_MIN_MULTIPLICAND 0
+#define DEFAULT_MAX_MULTIPLICAND 6
+
+#define DEFAULT_DIVISION_ALLOWED 0        /* dividend/divisor = quotient */
+#define DEFAULT_MIN_DIVISOR 0             /* note - generate_list() will prevent */
+#define DEFAULT_MAX_DIVISOR 3             /* questions with division by zero.    */
+#define DEFAULT_MIN_QUOTIENT 0
+#define DEFAULT_MAX_QUOTIENT 3
+
+/* default values for game_options */
+#define DEFAULT_USE_SOUND 1
+#define DEFAULT_FULLSCREEN 0
+#define DEFAULT_USE_BKGD 1
+#define DEFAULT_DEMO_MODE 0
+#define DEFAULT_OPER_OVERRIDE 0
+#define DEFAULT_USE_KEYPAD 1
+#define DEFAULT_REUSE_QUESTIONS 0
+#define DEFAULT_SPEED 1
+#define DEFAULT_ALLOW_SPEEDUP 0
+#define DEFAULT_MAX_COMETS 2	/* CHANGED FROM 10 BY DSB */
+#define DEFAULT_NUM_CITIES 4   /* MUST BE AN EVEN NUMBER! */
+#define DEFAULT_NUM_BKGDS 5
+#define DEFAULT_MAX_CITY_COLORS 4
+
+#endif




More information about the Tux4kids-commits mailing list