[Tux4kids-commits] r26 - tuxmath/trunk/src
dbruce at alioth.debian.org
dbruce at alioth.debian.org
Thu Mar 8 21:06:36 CET 2007
Author: dbruce
Date: 2006-09-07 20:40:14 +0000 (Thu, 07 Sep 2006)
New Revision: 26
Modified:
tuxmath/trunk/src/fileops.c
tuxmath/trunk/src/game.c
tuxmath/trunk/src/mathcards.c
tuxmath/trunk/src/mathcards.h
tuxmath/trunk/src/setup.c
tuxmath/trunk/src/tuxmath.h
Log:
code cleanup only, no user-visible changes
Modified: tuxmath/trunk/src/fileops.c
===================================================================
--- tuxmath/trunk/src/fileops.c 2006-09-06 17:28:49 UTC (rev 25)
+++ tuxmath/trunk/src/fileops.c 2006-09-07 20:40:14 UTC (rev 26)
@@ -993,8 +993,9 @@
if(verbose)
{
- fprintf (fp, "\n# Use either fullscreen at 640x480 resolution or window of that size\n"
- "# Default is 1. Change to 0 if SDL has trouble with fullscreen.\n");
+ fprintf (fp, "\n# Use fullscreen at 640x480 resolution instead of\n"
+ "640x480 window. Default is 1 (fullscreen). Change to 0\n"
+ "if SDL has trouble with fullscreen on your system.\n");
}
fprintf(fp, "fullscreen = %d\n", game_options->fullscreen);
@@ -1071,7 +1072,8 @@
if(verbose)
{
fprintf (fp, "\n# Allow speed and number of comets to increase with each\n"
- "# wave. May want to turn this off for smaller kids. Default is 1.\n");
+ "# wave. May want to turn this off for smaller kids.\n"
+ "Default is 1 (allow game to speed up)\n");
}
fprintf(fp, "allow_speedup = %d\n", game_options->allow_speedup);
Modified: tuxmath/trunk/src/game.c
===================================================================
--- tuxmath/trunk/src/game.c 2006-09-06 17:28:49 UTC (rev 25)
+++ tuxmath/trunk/src/game.c 2006-09-07 20:40:14 UTC (rev 26)
@@ -66,9 +66,9 @@
int expl;
int city;
float x, y;
- char formula[FORMULA_LEN];
+// char formula[FORMULA_LEN];
int answer;
- char answer_str[ANSWER_LEN];
+// char answer_str[ANSWER_LEN];
MC_FlashCard flashcard;
} comet_type;
@@ -955,31 +955,32 @@
}
- /* Draw comets: */
- for (i = 0; i < MAX_COMETS; i++)
- {
- if (comets[i].alive)
- {
- if (comets[i].expl < COMET_EXPL_END)
- {
- /* Decide which image to display: */
- img = IMG_COMET1 + ((frame + i) % 3);
- /* Display the formula (flashing, in the bottom half
+ /* Draw comets: */
+ for (i = 0; i < MAX_COMETS; i++)
+ {
+ if (comets[i].alive)
+ {
+ if (comets[i].expl < COMET_EXPL_END)
+ {
+ /* Decide which image to display: */
+ img = IMG_COMET1 + ((frame + i) % 3);
+ /* Display the formula (flashing, in the bottom half
of the screen) */
- if (comets[i].y < screen->h / 2 || frame % 8 < 6)
- {
- comet_str = comets[i].formula;
- }
- else
- {
- comet_str = NULL;
- }
+ if (comets[i].y < screen->h / 2 || frame % 8 < 6)
+ {
+ comet_str = comets[i].flashcard.formula_string;
+ }
+ else
+ {
+ comet_str = NULL;
+ }
}
else
{
img = comets[i].expl;
- comet_str = comets[i].answer_str;
+ comet_str = comets[i].flashcard.answer_string;
}
+
/* Draw it! */
dest.x = comets[i].x - (images[img]->w / 2);
@@ -1277,57 +1278,31 @@
/* If we make it to here, create a new comet! */
/* The answer may be num1, num2, or num3, depending on format. */
- /* Also, the formula string needs to be set accordingly: */
switch (comets[found].flashcard.format)
{
case MC_FORMAT_ANS_LAST: /* e.g. num1 + num2 = ? */
{
comets[found].answer = comets[found].flashcard.num3;
-
- snprintf(comets[found].formula, FORMULA_LEN,"%d %c %d = ?",
- comets[found].flashcard.num1,
- operchars[comets[found].flashcard.operation],
- comets[found].flashcard.num2);
break;
}
-
case MC_FORMAT_ANS_MIDDLE: /* e.g. num1 + ? = num3 */
{
comets[found].answer = comets[found].flashcard.num2;
-
- snprintf(comets[found].formula, FORMULA_LEN,"%d %c ? = %d",
- comets[found].flashcard.num1,
- operchars[comets[found].flashcard.operation],
- comets[found].flashcard.num3);
break;
}
-
case MC_FORMAT_ANS_FIRST: /* e.g. ? + num2 = num3 */
{
comets[found].answer = comets[found].flashcard.num1;
-
- snprintf(comets[found].formula, FORMULA_LEN,"? %c %d = %d",
- operchars[comets[found].flashcard.operation],
- comets[found].flashcard.num2,
- comets[found].flashcard.num3);
break;
}
-
default: /* should not get to here if MathCards behaves correctly */
{
- #ifdef TUXMATH_DEBUG
- printf("\nadd_comet() - invalid question format");
- #endif
fprintf(stderr, "\nadd_comet() - invalid question format");
return 0;
}
}
- /* set answer string: */
- snprintf(comets[found].answer_str, ANSWER_LEN, "%d",
- comets[found].answer);
-
comets[found].alive = 1;
num_comets_alive++;
@@ -1347,10 +1322,12 @@
comets[found].x = cities[i].x;
comets[found].y = 0;
+ #ifdef TUXMATH_DEBUG
+ printf ("add_comet(): formula string is: ", comets[found].flashcard.formula_string);
+ #endif
+
/* comet slot found and question found so return successfully: */
return 1;
-
-
}
/* Draw numbers/symbols over the attacker: */
@@ -2034,7 +2011,7 @@
comets[i].x = 0;
comets[i].y = 0;
comets[i].answer = 0;
- strncpy(comets[i].formula, " ", FORMULA_LEN);
- strncpy(comets[i].answer_str, " ", ANSWER_LEN);
+ strncpy(comets[i].flashcard.formula_string, " ", FORMULA_LEN);
+ strncpy(comets[i].flashcard.answer_string, " ", ANSWER_LEN);
}
}
Modified: tuxmath/trunk/src/mathcards.c
===================================================================
--- tuxmath/trunk/src/mathcards.c 2006-09-06 17:28:49 UTC (rev 25)
+++ tuxmath/trunk/src/mathcards.c 2006-09-07 20:40:14 UTC (rev 26)
@@ -13,6 +13,10 @@
* Copyright: See COPYING file that comes with this distribution. (Briefly, GNU GPL).
*
*/
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
#include "mathcards.h"
@@ -349,13 +353,15 @@
return 0;
}
-
+ /* FIXME: could clean this up a bit with a copy_card() function */
fc->num1 = question_list->card.num1;
fc->num2 = question_list->card.num2;
fc->num3 = question_list->card.num3;
fc->operation = question_list->card.operation;
fc->format = question_list->card.format;
-
+ strncpy(fc->formula_string, question_list->card.formula_string, MC_FORMULA_LEN);
+ strncpy(fc->answer_string, question_list->card.answer_string, MC_ANSWER_LEN);
+
/* take first question node out of list and free it: */
question_list = remove_node(question_list, question_list);
free(ptr);
@@ -1976,6 +1982,82 @@
ptr->card.format = f;
ptr->next = 0;
ptr->previous =0;
+
+ /* creating formula_string and answer_string is a little more work: */
+ {
+ char oper_char;
+ /* find out correct operation character */
+ switch (op)
+ {
+ case MC_OPER_ADD:
+ {
+ oper_char = '+';
+ break;
+ }
+ case MC_OPER_SUB:
+ {
+ oper_char = '-';
+ break;
+ }
+ case MC_OPER_MULT:
+ {
+ oper_char = '*';
+ break;
+ }
+ case MC_OPER_DIV:
+ {
+ oper_char = '/';
+ break;
+ }
+ default:
+ {
+ fprintf(stderr, "\nIn create_node(): invalid math operation\n");
+ free(ptr);
+ ptr = 0;
+ return 0;
+ }
+ }
+
+ switch (f) /* f is format argument */
+ {
+ case MC_FORMAT_ANS_LAST: /* e.g. num1 + num2 = ? */
+ {
+ snprintf(ptr->card.formula_string, MC_FORMULA_LEN,"%d %c %d = ?",
+ ptr->card.num1,
+ oper_char,
+ ptr->card.num2);
+ snprintf(ptr->card.answer_string, MC_ANSWER_LEN, "%d",ptr->card.num3);
+ break;
+ }
+ case MC_FORMAT_ANS_MIDDLE: /* e.g. num1 + ? = num3 */
+ {
+ snprintf(ptr->card.formula_string, MC_FORMULA_LEN,"%d %c ? = %d",
+ ptr->card.num1,
+ oper_char,
+ ptr->card.num3);
+ snprintf(ptr->card.answer_string, MC_ANSWER_LEN, "%d",ptr->card.num2);
+ break;
+ }
+ case MC_FORMAT_ANS_FIRST: /* e.g. ? + num2 = num3 */
+ {
+ snprintf(ptr->card.formula_string, MC_FORMULA_LEN,"? %c %d = %d",
+ oper_char,
+ ptr->card.num2,
+ ptr->card.num3);
+ snprintf(ptr->card.answer_string, MC_ANSWER_LEN, "%d",ptr->card.num1);
+ break;
+ }
+ default: /* should not get to here if MathCards behaves correctly */
+ {
+ fprintf(stderr, "\ncreate_node() - invalid question format\n");
+ free(ptr);
+ ptr = 0;
+ return 0;
+ }
+ }
+ }
+
+ /* ptr should now point to a properly constructed node: */
return ptr;
}
@@ -1985,22 +2067,15 @@
/* FIXME should properly return newly allocated list if more than one node DSB */
MC_MathQuestion* create_node_copy(MC_MathQuestion* other)
{
- MC_MathQuestion* ptr;
- if (!other)
- return 0;
- ptr = malloc(sizeof(MC_MathQuestion));
- ptr->card.num1 = other->card.num1;
- ptr->card.num2 = other->card.num2;
- ptr->card.num3 = other->card.num3;
- ptr->card.operation = other->card.operation;
- ptr->card.format = other->card.format;
- ptr->next = 0;
- ptr->previous = 0;
- return ptr;
+ return create_node(other->card.num1,
+ other->card.num2,
+ other->card.operation,
+ other->card.num3,
+ other->card.format);
}
#endif
-
+/* FIXME take care of strings */
MC_MathQuestion* create_node_from_card(MC_FlashCard* flashcard)
{
MC_MathQuestion* ptr;
@@ -2018,6 +2093,8 @@
}
#ifdef MC_DEBUG
+/* FIXME take care of strings */
+
MC_FlashCard* create_card_from_node(MC_MathQuestion* node)
{
MC_FlashCard* fc;
@@ -2034,6 +2111,7 @@
#endif
#ifdef MC_DEBUG
+/* FIXME take care of strings */
/* this one copies the contents, including pointers; both nodes must be allocated */
int copy_node(MC_MathQuestion* original, MC_MathQuestion* copy)
{
@@ -2339,8 +2417,7 @@
return;
}
MC_MathQuestion* ptr = list;
- fprintf(fp, "\nprint_list() printing list:");
- fprintf(fp, "\nlist_length():\t%d", list_length(list));
+ fprintf(fp, "\nList Length:\t%d", list_length(list));
while (ptr)
{
print_node(fp, ptr);
@@ -2349,86 +2426,16 @@
}
-/* convert node info into an (easily) human-readable string */
-/* and print it, taking question format into account. */
+
+/* Don't need this much now that formula_string part of card struct: */
void print_node(FILE* fp, MC_MathQuestion* ptr)
{
- char str[MC_FORMULA_LEN];
- char op;
-
- if (!ptr)
+ if (!ptr || !fp)
{
return;
}
- /* find out correct operation character */
- switch (ptr->card.operation)
- {
- case MC_OPER_ADD:
- {
- op = '+';
- break;
- }
- case MC_OPER_SUB:
- {
- op = '-';
- break;
- }
- case MC_OPER_MULT:
- {
- op = '*';
- break;
- }
- case MC_OPER_DIV:
- {
- op = '/';
- break;
- }
- default:
- {
- fprintf(stderr, "\nIn print_node(): invalid math operation\n");
- return;
- }
- }
-
- switch (ptr->card.format)
- {
- case MC_FORMAT_ANS_LAST: /* e.g. num1 + num2 = ? */
- {
- snprintf(str, MC_FORMULA_LEN,"%d %c %d = ?",
- ptr->card.num1,
- op,
- ptr->card.num2);
- break;
- }
-
- case MC_FORMAT_ANS_MIDDLE: /* e.g. num1 + ? = num3 */
- {
- snprintf(str, MC_FORMULA_LEN,"%d %c ? = %d",
- ptr->card.num1,
- op,
- ptr->card.num3);
- break;
- }
-
- case MC_FORMAT_ANS_FIRST: /* e.g. ? + num2 = num3 */
- {
- snprintf(str, MC_FORMULA_LEN,"? %c %d = %d",
- op,
- ptr->card.num2,
- ptr->card.num3);
- break;
- }
-
- default: /* should not get to here if MathCards behaves correctly */
- {
- fprintf(stderr, "\nprint_node() - invalid question format\n");
- return;
- }
- }
-
- /* Now simply print string: */
- fprintf(fp, "\n%s", str);
+ fprintf(fp, "\n%s", ptr->card.formula_string);
}
Modified: tuxmath/trunk/src/mathcards.h
===================================================================
--- tuxmath/trunk/src/mathcards.h 2006-09-06 17:28:49 UTC (rev 25)
+++ tuxmath/trunk/src/mathcards.h 2006-09-07 20:40:14 UTC (rev 26)
@@ -17,11 +17,10 @@
//#define MC_DEBUG
#define MC_FORMULA_LEN 14
+#define MC_ANSWER_LEN 5
-#include <stdio.h>
-#include <stdlib.h>
-#include <time.h>
+
/* type of math operation used in a given question */
enum {
MC_OPER_ADD,
@@ -157,6 +156,8 @@
int num3;
int operation;
int format;
+ char formula_string[MC_FORMULA_LEN];
+ char answer_string[MC_ANSWER_LEN];
} MC_FlashCard;
/* struct for node in math "flashcard" list */
@@ -213,8 +214,8 @@
int MC_AnsweredCorrectly(MC_FlashCard* q);
/* MC_AnsweredIncorrectly() is how the user interface */
-/* tells MathCards that the question has been answered */
-/* incorrectly. Returns 1 if no errors. */
+/* tells MathCards that the question has not been */
+/* answered correctly. Returns 1 if no errors. */
int MC_AnsweredIncorrectly(MC_FlashCard* q);
/* Like MC_NextQuestion(), but takes "flashcard" from */
Modified: tuxmath/trunk/src/setup.c
===================================================================
--- tuxmath/trunk/src/setup.c 2006-09-06 17:28:49 UTC (rev 25)
+++ tuxmath/trunk/src/setup.c 2006-09-07 20:40:14 UTC (rev 26)
@@ -48,7 +48,7 @@
/* Global data used in setup.c: */
-/* (These need to be 'extern'd in "setup.h") */
+/* (These are now 'extern'd in "tuxmath.h") */
SDL_Surface* screen;
SDL_Surface* images[NUM_IMAGES];
@@ -508,7 +508,10 @@
{
SDL_Quit();
if (game_options)
+ {
free(game_options);
+ game_options = 0;
+ }
/* frees any heap used by MathCards: */
MC_EndGame();
}
Modified: tuxmath/trunk/src/tuxmath.h
===================================================================
--- tuxmath/trunk/src/tuxmath.h 2006-09-06 17:28:49 UTC (rev 25)
+++ tuxmath/trunk/src/tuxmath.h 2006-09-07 20:40:14 UTC (rev 26)
@@ -109,11 +109,11 @@
/* Global data gets 'externed' here: */
extern game_option_type* game_options; /* used by setup.c, options.c, game.c */
-extern SDL_Surface* screen;
-extern SDL_Surface* images[];
+extern SDL_Surface* screen; /* declared in setup.c; also used in game.c, options.c, fileops.c, credits.c, title.c */
+extern SDL_Surface* images[]; /* declared in setup.c, used in same files as screen */
#ifndef NOSOUND
-extern Mix_Chunk* sounds[];
-extern Mix_Music* musics[];
+extern Mix_Chunk* sounds[]; /* declared in setup.c; also used in fileops.c, playsound.c */
+extern Mix_Music* musics[]; /* declared in setup.c; also used in fileops.c, game.c */
#endif
extern char operchars[NUM_OPERS];
More information about the Tux4kids-commits
mailing list