Merged C code cleanup and low-cpu branch to master
authorPat Thoyts <patthoyts@users.sourceforge.net>
Wed, 16 Jun 2010 00:13:16 +0000 (01:13 +0100)
committerPat Thoyts <patthoyts@users.sourceforge.net>
Wed, 16 Jun 2010 00:13:16 +0000 (01:13 +0100)
Signed-off-by: Pat Thoyts <patthoyts@users.sourceforge.net>
1  2 
makefile.linux
src/ag.c
src/dlb.c
src/linked.c
src/sprite.c
src/sprite.h

diff --cc makefile.linux
index 82cf0771ec3ad95d732debcf412a418f5e26232f,66f4e651302762d065179b16e375800181d81d7e..66f4e651302762d065179b16e375800181d81d7e
mode 100644,100755..100644
diff --cc src/ag.c
index 47f894d71f8e2355c96801f6344b297b1da5601f,44ae895467f9a6da6fd222b5e2f3010ca1b23e41..df1070df4d17da92d38f01b00bf4f161b43b2994
mode 100644,100755..100644
+++ 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 <stdio.h>
  #include <stdlib.h>
  #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");
                }
        }
  
                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;
                }
                        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);
        }
  
        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 5f81f39506534f75fa2a27295cb2f9a613ab474b,3277378ee930d57d673eb90e1f86154ac65dbb4c..b29d852c8d3876c85c1dd46eacb9a8fc40f88b36
mode 100644,100755..100644
+++ b/src/dlb.c
diff --cc src/linked.c
index 3bb020452abc5aa8aa4650b3e67f03b83d59be55,45fd8d2a2dad374a94abb030aee1c4c859a21803..45fd8d2a2dad374a94abb030aee1c4c859a21803
mode 100644,100755..100644
diff --cc src/sprite.c
index 051703ae8dc61a8da34348941ea0a3e8cd0de03b,39c40fcc14125541d0399045e3297f7644cf70f0..39c40fcc14125541d0399045e3297f7644cf70f0
mode 100644,100755..100644
diff --cc src/sprite.h
index c54c2c115b6965113163319cd21d12a5aad06f1c,14aefbe54779fa2c54c3ddea95bbc8fa062b9698..14aefbe54779fa2c54c3ddea95bbc8fa062b9698
mode 100644,100755..100644