From: Pat Thoyts Date: Wed, 16 Jun 2010 00:13:16 +0000 (+0100) Subject: Merged C code cleanup and low-cpu branch to master X-Git-Tag: v0.4~20 X-Git-Url: http://privyetmir.co.uk/gitweb.cgi?a=commitdiff_plain;h=afe4868a7984a2122744a608fe54ce038e47453e;p=anagramarama Merged C code cleanup and low-cpu branch to master Signed-off-by: Pat Thoyts --- afe4868a7984a2122744a608fe54ce038e47453e diff --cc makefile.linux index 82cf077,66f4e65..66f4e65 mode 100644,100755..100644 --- a/makefile.linux +++ b/makefile.linux diff --cc src/ag.c index 47f894d,44ae895..df1070d mode 100644,100755..100644 --- a/src/ag.c +++ b/src/ag.c @@@ -1,62 -1,61 +1,62 @@@ /* - Anagramarama - A word game. Like anagrams? You'll love anagramarama! - Copyright (C) 2003 Colm Gallagher + * Anagramarama - A word game. Like anagrams? You'll love anagramarama! + * Copyright (C) 2003 Colm Gallagher + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Contact Details: colm@coralquest.com + * 12 Weston Terrace, West Kilbride, KA23 9JX. Scotland. + */ - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - - Contact Details: colm@coralquest.com - 12 Weston Terrace, West Kilbride, KA23 9JX. Scotland. - */ - - - /*********************************************************** - Contributors - - Colm Gallagher : Concept and initial programming - Alan Grier : Graphics - THomas Plunkett : Audio - Shard : BEOS Port and bugfixes - Adolfo : Bugfix for Linux Red-Hat version 7.3 - - ************************************************************ - version who changes - ------------------------------------------------------------------- - 0.1 Colm initial Linux & Windows revisions - - 0.2 Shard Bugfix: buffer overrun in clearWord - function corrupted memory. Strange - thing is it crashed BEOS, but not - Linux or Windows - guess they handle - memory differently or BEOS is much - better at detecting exceptions - - 0.3 Shard added BEOS port (new makefile) - - 0.4 Adolfo Bugfix: oops! in the checkGuess - function, I tried to initialise - test[] using a variable as if I was - using vb6 ! Have changed this to a - static buffer and all is now well. - - 0.5 Colm Added keyboard input - - - 0.6 Paulo Added i18n and Portugues dict - - - ***********************************************************/ + /* + * Contributors + * + * Colm Gallagher : Concept and initial programming + * Alan Grier : Graphics + * THomas Plunkett : Audio + * Shard : BEOS Port and bugfixes + * Adolfo : Bugfix for Linux Red-Hat version 7.3 + * Pat Thoyts : C code cleanup, memory leak and cpu usage fixes + * + * - * ---------------------------------------------------------------- ++ * ------------------------------------------------------------------- + * version who changes + * ------------------------------------------------------------------- + * 0.1 Colm initial Linux & Windows revisions - * ++ * + * 0.2 Shard Bugfix: buffer overrun in clearWord - * function corrupted memory. Strange - * thing is it crashed BEOS, but not - * Linux or Windows - guess they handle - * memory differently or BEOS is much - * better at detecting exceptions ++ * function corrupted memory. Strange ++ * thing is it crashed BEOS, but not ++ * Linux or Windows - guess they handle ++ * memory differently or BEOS is much ++ * better at detecting exceptions + * + * 0.3 Shard added BEOS port (new makefile) + * + * 0.4 Adolfo Bugfix: oops! in the checkGuess + * function, I tried to initialise + * test[] using a variable as if I was + * using vb6 ! Have changed this to a + * static buffer and all is now well. + * + * 0.5 Colm Added keyboard input + * ++ * 0.6 Paulo Added i18n and Portugues dict + * ------------------------------------------------------------------- + */ #include #include @@@ -71,11 -69,14 +70,15 @@@ #include "sprite.h" #include "ag.h" - //module level variables for game control + #ifdef _MSC_VER + #define snprintf _snprintf + #endif + + /* module level variables for game control */ char shuffle[] = "£££££££"; char answer[] = "£££££££"; - +char language[10]; +char txt[50]; char* rootWord; int updateAnswers = 0; int startNewGame = 0; @@@ -977,30 -935,28 +937,31 @@@ inputs: n/ outputs: a random word ***********************************************************/ - char* getRandomWord(){ - - FILE* wordlist; - int filelocation; - int i; - char* wordFromList = malloc(sizeof(char) * 50); - int len=0; - int done = 0; - + static char * + getRandomWord() + { + FILE* wordlist; + int filelocation; + int i; + char* wordFromList = malloc(sizeof(char) * 50); + int len=0; + int done = 0; + filelocation = rand()%10000; - wordlist=fopen("wordlist.txt","r"); + + strcpy(txt,language); + wordlist=fopen(strcat(txt,"wordlist.txt"),"r"); for (i=0;i<=filelocation;i++){ if(fscanf(wordlist, "%s", wordFromList) != EOF){ - // spin on + /* spin on */ } else{ - // go back to the start of the file + /* go back to the start of the file */ fclose(wordlist); - fopen("wordlist.txt", "r"); + strcpy(txt,language); + fopen(strcat(txt,"wordlist.txt"), "r"); } } @@@ -1014,13 -970,12 +975,13 @@@ else{ if(fscanf(wordlist, "%s", wordFromList) != EOF){ - // spin on + /* spin on */ } else{ - // go back to the start of the file + /* go back to the start of the file */ fclose(wordlist); - fopen("wordlist.txt", "r"); + strcpy(txt,language); + fopen(strcat(txt,"wordlist.txt"), "r"); fscanf(wordlist, "%s", wordFromList); } } @@@ -1486,18 -1437,20 +1443,21 @@@ inputs: head - first node in the answer outputs: n/a ***********************************************************/ - void newGame(struct node** head, struct dlb_node* dlbHead, SDL_Surface* screen, struct sprite** letters){ - - char* guess; - char* remain; - int happy = 0; // we don't want any more than ones with 66 answers - that's all we can show... - int i; - - // show background - strcpy(txt,language); + static void + newGame(struct node** head, struct dlb_node* dlbHead, + SDL_Surface* screen, struct sprite** letters) + { + char* guess; + char* remain; + int happy = 0; /* we don't want any more than ones with 66 answers */ + /* - that's all we can show... */ + int i; + + /* show background */ - ShowBMP("images/background.bmp",screen, 0,0); ++ strcpy(txt, language); + ShowBMP(strcat(txt,"images/background.bmp"),screen, 0,0); - destroyLetters(&(*letters)); + destroyLetters(letters); guess = malloc(sizeof(char)*50); remain = malloc(sizeof(char)*50); @@@ -1596,16 -1560,22 +1567,21 @@@ inputs: head - first node in the answer outputs: n/a ***********************************************************/ - void gameLoop(struct node** head, struct dlb_node* dlbHead, SDL_Surface* screen, struct sprite** letters){ - - int done=0; - SDL_Event event; - int timeNow; - - // main game loop - while (!done){ - if(winGame){ - + static void + gameLoop(struct node **head, struct dlb_node *dlbHead, + SDL_Surface *screen, struct sprite **letters) + { + int done=0; + SDL_Event event; + int timeNow; + SDL_TimerID timer; + int timer_delay = 20; + + timer = SDL_AddTimer(timer_delay, TimerCallback, NULL); + /* main game loop */ + while (!done) { + + if (winGame) { - stopTheClock = 1; solvePuzzle = 1; } @@@ -1694,87 -1662,58 +1668,95 @@@ done=1; } - while (SDL_PollEvent(&event)) + while (SDL_WaitEvent(&event)) { - switch (event.type) { - case SDL_MOUSEBUTTONDOWN: - clickDetect(event.button.button, event.button.x, event.button.y, screen, *head, &(*letters)); - break; - - case SDL_KEYUP: - handleKeyboardEvent(&event, *head, &(*letters)); - break; - case SDL_QUIT: - done=1; + if (event.type == SDL_USEREVENT) { + timer_delay = anySpritesMoving(letters) ? 10 : 100; + moveSprites(&screen, letters, letterSpeed); + timer = SDL_AddTimer(timer_delay, TimerCallback, NULL); + break; + } else if (event.type == SDL_MOUSEBUTTONDOWN) { + clickDetect(event.button.button, event.button.x, + event.button.y, screen, *head, letters); + } else if (event.type == SDL_KEYUP) { + handleKeyboardEvent(&event, *head, letters); + } else if (event.type == SDL_QUIT) { + done = 1; + break; } - } - moveSprites(&screen, &(*letters), letterSpeed); + moveSprites(&screen, letters, letterSpeed); } + } } - - - - /*********************************************************** - synopsis: initialise graphics and sound, build the dictionary - cache the images and start the game loop - when the game is done tidy up - - inputs: argc - argument count - argv - arguments - - outputs: retval 0 = success 1 = failure - ***********************************************************/ - int main(int argc, char *argv[]){ - - struct node* head = NULL; - struct dlb_node* dlbHead = NULL; - SDL_Surface *screen; - struct sprite* letters = NULL; - //pthread_t audio; ++/* ++ * Get the current language string from either the environment or ++ * the command line. This is used to location the wordlist and ++ * other localized resources. ++ * ++ * Sets the global variable 'language' ++ */ + ++static void ++init_locale(int argc, char *argv[]) ++{ + strcpy(language,"i18n/"); - - if(argc == 2) { - strcat(language,argv[1]); - } - else { ++ if (argc == 2) { ++ strcat(language, argv[1]); ++ } else { + char * language2 = getenv("LANG"); + - if(language2 == 0){ ++ if (language2 == 0) { + strcpy(language,"i18n/en_GB/"); - } - else { ++ } else { + char local[10]; + int len = strlen(language2); + int i = 0; - ++ + while((language2[i] != '.')&&(i< len)){ + local[i] = language2[i]; + i++; + } + local[i] = '\0'; + strcat(language,local); + } + } ++ strcat(language, "/"); ++} - strcat(language,"/"); + /*********************************************************** + synopsis: initialise graphics and sound, build the dictionary + cache the images and start the game loop + when the game is done tidy up - strcpy(txt,language); - strcat(txt,"wordlist.txt"); - FILE* wordlist; - wordlist = fopen(txt,"r"); - //if(wordlist == 0){ - // strcpy(language,"i18n/en_GB/"); - //} - //else { - // fclose(wordlist); - //} - - // seed the random generator + inputs: argc - argument count + argv - arguments + + outputs: retval 0 = success 1 = failure + ***********************************************************/ ++ + int + main(int argc, char *argv[]) + { + struct node* head = NULL; + struct dlb_node* dlbHead = NULL; + SDL_Surface *screen; + struct sprite* letters = NULL; + + /* buffer sounds */ + int audio_rate = MIX_DEFAULT_FREQUENCY; + Uint16 audio_format = AUDIO_S16; + int audio_channels = 1; + int audio_buffers = 256; + + /* seed the random generator */ srand(time(NULL)); - if (SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0){ ++ /* identify the resource locale */ ++ init_locale(argc, argv); ++ + if (SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO|SDL_INIT_TIMER) < 0){ fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError()); exit(1); } @@@ -1803,18 -1736,13 +1779,16 @@@ bufferSounds(&soundCache); - // create dictionary - createDLBTree(&dlbHead,language); + /* create dictionary */ - createDLBTree(&dlbHead); ++ createDLBTree(&dlbHead, language); - - - // cache in-game graphics - strcpy(txt,language); + /* cache in-game graphics */ - letterBank = SDL_LoadBMP("images/letterBank.bmp"); - smallLetterBank = SDL_LoadBMP("images/smallLetterBank.bmp"); - numberBank = SDL_LoadBMP("images/numberBank.bmp"); ++ strcpy(txt, language); + letterBank = SDL_LoadBMP(strcat(txt,"images/letterBank.bmp")); - strcpy(txt,language); ++ strcpy(txt, language); + smallLetterBank = SDL_LoadBMP(strcat(txt,"images/smallLetterBank.bmp")); - strcpy(txt,language); ++ strcpy(txt, language); + numberBank = SDL_LoadBMP(strcat(txt,"images/numberBank.bmp")); rootWord = malloc(sizeof(char)*9); newGame(&head, dlbHead, screen, &letters); diff --cc src/dlb.c index 5f81f39,3277378..b29d852 mode 100644,100755..100644 --- a/src/dlb.c +++ b/src/dlb.c diff --cc src/linked.c index 3bb0204,45fd8d2..45fd8d2 mode 100644,100755..100644 --- a/src/linked.c +++ b/src/linked.c diff --cc src/sprite.c index 051703a,39c40fc..39c40fc mode 100644,100755..100644 --- a/src/sprite.c +++ b/src/sprite.c diff --cc src/sprite.h index c54c2c1,14aefbe..14aefbe mode 100644,100755..100644 --- a/src/sprite.h +++ b/src/sprite.h