[Tux4kids-commits] r558 - tuxmath/branches/mathcards_newarch/src

cheezmeister-guest at alioth.debian.org cheezmeister-guest at alioth.debian.org
Thu Jul 3 02:58:57 UTC 2008


Author: cheezmeister-guest
Date: 2008-07-03 02:58:56 +0000 (Thu, 03 Jul 2008)
New Revision: 558

Modified:
   tuxmath/branches/mathcards_newarch/src/game.c
   tuxmath/branches/mathcards_newarch/src/loaders.c
   tuxmath/branches/mathcards_newarch/src/mathcards.c
   tuxmath/branches/mathcards_newarch/src/mathcards.h
   tuxmath/branches/mathcards_newarch/src/titlescreen.c
   tuxmath/branches/mathcards_newarch/src/titlescreen.h
   tuxmath/branches/mathcards_newarch/src/tuxmath.h
Log:
Branch: fixed segfaults when exiting (not to mention the more serious 
underlying problem of rampant out-of-bounds writing)


Modified: tuxmath/branches/mathcards_newarch/src/game.c
===================================================================
--- tuxmath/branches/mathcards_newarch/src/game.c	2008-07-02 16:09:13 UTC (rev 557)
+++ tuxmath/branches/mathcards_newarch/src/game.c	2008-07-03 02:58:56 UTC (rev 558)
@@ -478,6 +478,19 @@
   SDL_quit_received = 0;
   escape_received = 0;
 
+  /* Start MathCards backend: */
+  /* FIXME may need to move this into tuxmath.c to accomodate option */
+  /* to use MC_StartUsingWrongs() */
+  /* NOTE MC_StartGame() will return 0 if the list length is zero due */
+  /* (for example) to all math operations being deselected */
+  if (!MC_StartGame())
+  {
+    tmdprintf("\nMC_StartGame() failed!");
+    fprintf(stderr, "\nMC_StartGame() failed!");
+    return 0;
+  }
+
+
   /* Allocate memory */
   comets = NULL;  // set in case allocation fails partway through
   cities = NULL;
@@ -488,18 +501,21 @@
     printf("Allocation of comets failed");
     return 0;
   }
-  for (i = 0; i < MAX_MAX_COMETS; ++i)
-    {
-    comets[i].flashcard = MC_AllocateFlashcard();
-    if (!MC_FlashCardGood(&comets[i].flashcard) ) 
+  else {
+    for (i = 0; i < MAX_MAX_COMETS; ++i)
       {
-      //something's wrong
-      printf("Allocation of flashcard %d failed\n", i);
-      for (; i >= 0; --i) //free anything we've already gotten
-        MC_FreeFlashcard(&comets[i].flashcard);
-      return 0;
+      comets[i].flashcard = MC_AllocateFlashcard();
+      if (!MC_FlashCardGood(&comets[i].flashcard) ) 
+        {
+        //something's wrong
+        printf("Allocation of flashcard %d failed\n", i);
+        for (; i >= 0; --i) //free anything we've already gotten
+          MC_FreeFlashcard(&comets[i].flashcard);
+        return 0;
+        }
       }
-    }
+  }
+  
   cities = (city_type *) malloc(NUM_CITIES * sizeof(city_type));
   if (cities == NULL) {
     printf("Allocation of cities failed");
@@ -516,18 +532,7 @@
     return 0;
   }
 
-  /* Start MathCards backend: */
-  /* FIXME may need to move this into tuxmath.c to accomodate option */
-  /* to use MC_StartUsingWrongs() */
-  /* NOTE MC_StartGame() will return 0 if the list length is zero due */
-  /* (for example) to all math operations being deselected */
-  if (!MC_StartGame())
-  {
-    tmdprintf("\nMC_StartGame() failed!");
-    fprintf(stderr, "\nMC_StartGame() failed!");
-    return 0;
-  }
-
+  
   /* Write pre-game info to game summary file: */
   if (Opts_SaveSummary())
   {
@@ -1071,10 +1076,10 @@
 */
   /* negative answer support DSB */
   
-  ans[0] = '-';
-  for (i = j = (neg_answer_picked); i < MAX_DIGITS; ++i)
-    if (digits[i])
-      ans[j++] = digits[i] + '0';
+  ans[0] = '-'; //this is replaced for a positive answer
+  for (i = 0; i < MAX_DIGITS - 1 && !digits[i]; ++i); //skip leading 0s
+  for (j = neg_answer_picked ? 1 : 0; i < MAX_DIGITS; ++i, ++j)
+    ans[j] = digits[i] + '0';
   ans[j] = '\0';
   
 /*

Modified: tuxmath/branches/mathcards_newarch/src/loaders.c
===================================================================
--- tuxmath/branches/mathcards_newarch/src/loaders.c	2008-07-02 16:09:13 UTC (rev 557)
+++ tuxmath/branches/mathcards_newarch/src/loaders.c	2008-07-03 02:58:56 UTC (rev 558)
@@ -330,13 +330,15 @@
         int x;
         if (!gfx)
                 return;
+        printf("Freeing image");
+        tmdprintf(" at %p", gfx);
         for (x = 0; x < gfx->num_frames; x++)
         {
-                tmdprintf(".");
+                printf(".");
                 SDL_FreeSurface( gfx->frame[x] );
         }              
-        tmdprintf("\nFreeing default\n");
         SDL_FreeSurface( gfx->default_img );
+        printf("done\n");
         free(gfx);
 }
 

Modified: tuxmath/branches/mathcards_newarch/src/mathcards.c
===================================================================
--- tuxmath/branches/mathcards_newarch/src/mathcards.c	2008-07-02 16:09:13 UTC (rev 557)
+++ tuxmath/branches/mathcards_newarch/src/mathcards.c	2008-07-03 02:58:56 UTC (rev 558)
@@ -1,7 +1,7 @@
 /*
 *  C Implementation: mathcards.c
 *
-*         Description: implementation of backend for a flashcard-type math game. 
+*       Description: implementation of backend for a flashcard-type math game. 
         Developed as an enhancement to Bill Kendrick's "Tux of Math Command"
         (aka tuxmath).  (If tuxmath were a C++ program, this would be a C++ class).
         MathCards could be used as the basis for similar games using a different interface.
@@ -95,8 +95,8 @@
   999,  //MAX_ANSWER                
   5000, //MAX_QUESTIONS             
   1,    //QUESTION_COPIES           
-  3,    //MAX_FORMULA_NUMS          
-  1,    //MIN_FORMULA_NUMS          
+  2,    //MAX_FORMULA_NUMS          
+  2,    //MIN_FORMULA_NUMS          
         //                          
   1,    //FORMAT_ANSWER_LAST        
   0,    //FORMAT_ANSWER_FIRST       
@@ -184,7 +184,7 @@
 static void clear_negatives(void);
 static int validate_question(int n1, int n2, int n3);
 static MC_MathQuestion* create_node(int n1, int n2, int op, int ans, int f);
-static MC_MathQuestion* create_node_from_card(MC_FlashCard* flashcard);
+static MC_MathQuestion* create_node_from_card(const MC_FlashCard* flashcard);
 static MC_MathQuestion* insert_node(MC_MathQuestion* first, MC_MathQuestion* current, MC_MathQuestion* new_node);
 static MC_MathQuestion* append_node(MC_MathQuestion* list, MC_MathQuestion* new_node);
 static MC_MathQuestion* remove_node(MC_MathQuestion* first, MC_MathQuestion* n);
@@ -200,7 +200,9 @@
 static int int_to_bool(int i);
 static int sane_value(int i);
 static int abs_value(int i);
+#ifndef MC_USE_NEWARC
 static int randomly_keep(void);
+#endif
 static int floatCompare(const void *v1,const void *v2);
 
 static void print_list(FILE* fp,MC_MathQuestion* list);
@@ -264,7 +266,7 @@
   } 
   
   /* set defaults */
-  math_opts->fraction_to_keep = DEFAULT_FRACTION_TO_KEEP;
+  //math_opts->fraction_to_keep = DEFAULT_FRACTION_TO_KEEP;
   for (i = 0; i < NOPTS; ++i)
     {
     math_opts->iopts[i] = MC_DEFAULTS[i];
@@ -334,8 +336,8 @@
   /* determine how much space needed for strings, based on user options */
   max_formula_size = MC_GetOpt(MAX_FORMULA_NUMS)
                    * ((int)(log10f(MC_GLOBAL_MAX) ) + 4) //sign/operator/spaces
-                   + 2; //question mark for answer, and ending '\0'
-  max_answer_size = (int)(log10f(MC_GLOBAL_MAX) ) + 2; //sign and ending '\0'
+                   + 1; //question mark for answer
+  max_answer_size = (int)(log10f(MC_GLOBAL_MAX) ) + 1; //negative sign
   
   mcdprintf("max answer, formula size: %d, %d\n", 
             max_answer_size, max_formula_size);
@@ -1509,15 +1511,15 @@
 {
   MC_MathQuestion* ret = malloc(sizeof(MC_MathQuestion) );
   if (ret)
-    copy_card(&other->card, &ret->card);
+    copy_card(&(other->card), &(ret->card) );
   return ret;
 }
 #endif
 
-MC_MathQuestion* create_node_from_card(MC_FlashCard* flashcard)
+MC_MathQuestion* create_node_from_card(const MC_FlashCard* flashcard)
 {
   MC_MathQuestion* ret = allocate_node();
-  copy_card(flashcard, &ret->card);
+  copy_card(flashcard, &(ret->card));
   return ret;
 }
 
@@ -1530,7 +1532,7 @@
   if (!node)
     return DEFAULT_CARD;
   fc = MC_AllocateFlashcard();
-  copy_card(&node->card, &fc);
+  copy_card(&(node->card), &fc);
   return fc;
 }
 #endif
@@ -1551,7 +1553,7 @@
     return 0;
   }  
 
-  copy_card(&original->card, &copy->card);
+  copy_card(&(original->card), &(copy->card) );
  
   copy->next = original->next;
   copy->previous = original->previous;
@@ -1849,7 +1851,7 @@
 {
   if (!first || !other)
     return 0;
-  if (compare_card(&first->card, &first->card) ) //cards are equal
+  if (compare_card(&(first->card), &(first->card) ) ) //cards are equal
     return 1;
   else
     return 0;
@@ -1909,7 +1911,7 @@
     return -i;
 }
 
-
+#ifndef MC_USE_NEWARC
 /* Returns true at probability set by math_opts->fraction_to_keep */
 int randomly_keep(void)
 {
@@ -1929,6 +1931,7 @@
   else
     return 0;
 }
+#endif
 
 /* Compares two floats (needed for sorting in MC_MedianTimePerQuestion) */
 int floatCompare(const void *v1,const void *v2)
@@ -1946,18 +1949,32 @@
     return 0;
 }
 
+
+
+
+
+
+
+
+
 #ifdef MC_USE_NEWARC
 
 /****************************************************
 Experimental functions for new mathcards architecture
 ****************************************************/
 
+
+
+
+
 /* allocate space for an MC_Flashcard */
 MC_FlashCard MC_AllocateFlashcard(void)
 {
   MC_FlashCard ret;  
-  ret.formula_string = malloc(max_formula_size * sizeof(char));
-  ret.answer_string = malloc(max_answer_size * sizeof(char));
+  mcdprintf("Allocating %d + %d bytes for flashcard\n", 
+            max_formula_size + 1, max_answer_size + 1);
+  ret.formula_string = malloc( (max_formula_size + 1) * sizeof(char));
+  ret.answer_string = malloc( (max_answer_size + 1) * sizeof(char));
   if (!ret.formula_string || !ret.answer_string)
     {
     free(ret.formula_string);
@@ -1971,7 +1988,6 @@
 {
   if (!fc) 
     return;
-#ifndef MC_DEBUG
   mcdprintf("Freeing formula_string\n");
   if (fc->formula_string)
     {
@@ -1984,7 +2000,6 @@
     free(fc->answer_string);
     fc->answer_string = NULL;
     }
-#endif
 }
 
 void copy_card(const MC_FlashCard* src, MC_FlashCard* dest)
@@ -2003,7 +2018,7 @@
 {
   if (!mq)
     return;
-  MC_FreeFlashcard(&mq->card);
+  MC_FreeFlashcard(&(mq->card) );
   free(mq);
 }
 
@@ -2033,7 +2048,17 @@
   MC_FlashCard ret;
   
   mcdprintf("Entering generate_random_flashcard()\n");
-  pt = rand() % MC_NUM_PTYPES;
+  
+  do
+    pt = rand() % MC_NUM_PTYPES;
+  while ( (pt == MC_PT_TYPING && !MC_GetOpt(TYPING_PRACTICE_ALLOWED) ) ||
+          (pt == MC_PT_ARITHMETIC && !MC_GetOpt(ADDITION_ALLOWED) && 
+                                   !MC_GetOpt(SUBTRACTION_ALLOWED) && 
+                                   !MC_GetOpt(MULTIPLICATION_ALLOWED) && 
+                                   !MC_GetOpt(DIVISION_ALLOWED) ) ||
+         pt == MC_PT_COMPARISON //&& !MC_GetOpt(COMPARISION_ALLOWED) 
+         );
+  
   if (pt == MC_PT_TYPING) //typing practice
   {
     mcdprintf("Generating typing question\n");
@@ -2050,7 +2075,7 @@
                     +  MC_GetOpt(MIN_FORMULA_NUMS);
     mcdprintf("Generating question of length %d", length);
     ret = generate_random_ooo_card_of_length(length);
-    
+    strncat(ret.formula_string, " = ?", max_formula_size - strlen(ret.formula_string) );
   }
   //TODO comparison problems (e.g. "6 ? 9", "<")
   
@@ -2068,7 +2093,7 @@
   int r1 = 0;
   int r2 = 0;
   int ans = 0;
-  char* tempstr[max_formula_size];
+  char tempstr[max_formula_size];
   MC_FlashCard ret;
   MC_Operation op;
     
@@ -2147,7 +2172,9 @@
       else if (op == MC_OPER_DIV)
         r1 = 1;
       
-      sprintf(tempstr, "%c %d", operchars[op], r1);
+      snprintf(tempstr, max_formula_size, "%s %c %d", //append
+               ret.formula_string, operchars[op], r1);
+      strncpy(ret.formula_string, tempstr, max_formula_size);
     }
     else //we're prepending
     {
@@ -2199,7 +2226,6 @@
   int i;
   for (i = 0; i < NOPTS; ++i)
   {
-    mcdprintf("%d: %s", i, MC_OPTION_TEXT[i] );
     if (!strcasecmp(text, MC_OPTION_TEXT[i]) )
       return i;
   }
@@ -2212,7 +2238,7 @@
 {
   if (index >= NOPTS)
   {
-    printf("Invalid option index: %du\n", index);
+    mcdprintf("Invalid option index: %d\n", index);
     return;
   }
   math_opts->iopts[index] = val;
@@ -2227,7 +2253,7 @@
 {
   if (index >= NOPTS)
   {
-    printf("Invalid option index: %du\n", index);
+    mcdprintf("Invalid option index: %d\n", index);
     return MC_MATH_OPTS_INVALID;
   }  
   if (!math_opts)
@@ -2243,16 +2269,16 @@
   return MC_GetOpt(MC_MapTextToIndex(param) );
 }
 
-void MC_SetFractionToKeep(float val)
-{
-  math_opts->fraction_to_keep = val;
-}
+//void MC_SetFractionToKeep(float val)
+//{
+//  math_opts->fraction_to_keep = val;
+//}
+//
+//float MC_GetFractionToKeep(void)
+//{
+//  return math_opts->fraction_to_keep;
+//}
 
-float MC_GetFractionToKeep(void)
-{
-  return math_opts->fraction_to_keep;
-}
-
 int MC_VerifyOptionListSane(void)
 {
   return MC_OPTION_TEXT[NOPTS] == "END_OF_OPTS";

Modified: tuxmath/branches/mathcards_newarch/src/mathcards.h
===================================================================
--- tuxmath/branches/mathcards_newarch/src/mathcards.h	2008-07-02 16:09:13 UTC (rev 557)
+++ tuxmath/branches/mathcards_newarch/src/mathcards.h	2008-07-03 02:58:56 UTC (rev 558)
@@ -15,7 +15,7 @@
 #ifndef MATHCARDS_H
 #define MATHCARDS_H
 
-#define MC_DEBUG
+//#define MC_DEBUG
 #ifdef MC_DEBUG
 #define mcdprintf(...) printf(__VA_ARGS__)
 #else
@@ -128,13 +128,13 @@
                                    /* can be entered for math question values.*/
 #define MC_MATH_OPTS_INVALID -9999 /* Return value for accessor functions     */
                                    /* if math_opts not valid                  */
-#define DEFAULT_FRACTION_TO_KEEP 1
+//#define DEFAULT_FRACTION_TO_KEEP 1
 
 
 typedef struct _MC_Options 
 {
   int iopts[NOPTS];
-  float fraction_to_keep; //being a float, we can't keep this in the same array
+  //float fraction_to_keep; //being a float, we can't keep this in the same array
 } MC_Options;             //it'll stay a special case, unless more float options
 
 #ifndef MC_USE_NEWARC

Modified: tuxmath/branches/mathcards_newarch/src/titlescreen.c
===================================================================
--- tuxmath/branches/mathcards_newarch/src/titlescreen.c	2008-07-02 16:09:13 UTC (rev 557)
+++ tuxmath/branches/mathcards_newarch/src/titlescreen.c	2008-07-03 02:58:56 UTC (rev 558)
@@ -432,7 +432,7 @@
 
   for (i = 0; i < N_SPRITES; i++)
   {
-    tmdprintf("Freeing image %d", i);
+    tmdprintf("Freeing image #%d: ", i);
     FreeSprite(sprite_list[i]);
   }
   free(sprite_list);
@@ -1728,11 +1728,11 @@
         imod = loc-loc_screen_start;
         use_sprite = (menu_sprites != NULL && loc >= title_offset && menu_sprites[loc] != NULL);
         temp_rect = menu_button_rect[imod];
-        SDL_BlitSurface(current_bkg, &back_button_rect[imod], screen, &temp_rect);
+        SDL_BlitSurface(current_bkg(), &(back_button_rect[imod]), screen, &temp_rect);
         if (use_sprite)
         {
           temp_rect = menu_sprite_rect[imod];
-          SDL_BlitSurface(current_bkg, &back_sprite_rect[imod], screen, &temp_rect);
+          SDL_BlitSurface(current_bkg(), &(back_sprite_rect[imod]), screen, &temp_rect);
         }
         DrawButton(&menu_button_rect[imod], 10, SEL_RGBA);
         SDL_BlitSurface(menu_item_selected[loc], NULL, screen, &menu_text_rect[imod]);
@@ -1804,7 +1804,8 @@
     }
     
     if (egg_active) { //if we need to, draw the egg cursor
-      SDL_GetMouseState(&cursor.x, &cursor.y);
+      //who knows why GetMouseState() doesn't take Sint16's...
+      SDL_GetMouseState((int*)&cursor.x, (int*)&cursor.y); 
       cursor.x -= egg->w / 2; //center vertically
       SDL_BlitSurface(egg, NULL, screen, &cursor);
       SDL_UpdateRect(screen, cursor.x, cursor.y, cursor.w, cursor.h);

Modified: tuxmath/branches/mathcards_newarch/src/titlescreen.h
===================================================================
--- tuxmath/branches/mathcards_newarch/src/titlescreen.h	2008-07-02 16:09:13 UTC (rev 557)
+++ tuxmath/branches/mathcards_newarch/src/titlescreen.h	2008-07-03 02:58:56 UTC (rev 558)
@@ -74,7 +74,7 @@
 
 
 #define menu_font  "AndikaDesRevA.ttf"  /*  "GenAI102.ttf" */
-#define menu_font_size        18
+#define menu_font_size       18
 
 #define ttf_font  "AndikaDesRevA.ttf"  /*   "GenAI102.ttf" */
 #define ttf_font_size        18
@@ -87,15 +87,15 @@
 #define MAX_UPDATES 180
 
 
-#define WAIT_MS                                2500
-#define        FRAMES_PER_SEC                        50
-#define FULL_CIRCLE                        140
+#define WAIT_MS                               2500
+#define FRAMES_PER_SEC                        50
+#define FULL_CIRCLE                           140
 
 
 /* Title sequence constants */
-#define PRE_ANIM_FRAMES                        10
+#define PRE_ANIM_FRAMES                       10
 #define PRE_FRAME_MULT                        3
-#define MENU_SEP                        20
+#define MENU_SEP                              20
 
 /* paths */
 

Modified: tuxmath/branches/mathcards_newarch/src/tuxmath.h
===================================================================
--- tuxmath/branches/mathcards_newarch/src/tuxmath.h	2008-07-02 16:09:13 UTC (rev 557)
+++ tuxmath/branches/mathcards_newarch/src/tuxmath.h	2008-07-03 02:58:56 UTC (rev 558)
@@ -42,7 +42,7 @@
 
 //#define NOSOUND
 /* for conditional compilation of debugging output */
-#define TUXMATH_DEBUG
+//#define TUXMATH_DEBUG
 /* for Tim's feedback speed control code           */
 //#define FEEDBACK_DEBUG
 /* nice inline debugging macro */




More information about the Tux4kids-commits mailing list