[Tux4kids-commits] [SCM] tuxhistory - Educational history game branch, master, updated. d8a7a3c9d7fd71be8130bb096aea7e55720f9985

julio (none) julio at julio-desktop.
Tue Jun 29 04:12:57 UTC 2010


The following commit has been merged in the master branch:
commit d8a7a3c9d7fd71be8130bb096aea7e55720f9985
Author: julio <julio at julio-desktop.(none)>
Date:   Mon Jun 28 23:12:11 2010 -0500

    Some advance in generate_map() function.

diff --git a/src/game.c b/src/game.c
index a990ef4..459b136 100644
--- a/src/game.c
+++ b/src/game.c
@@ -93,7 +93,8 @@ static int game_init(void)
     SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
     SDL_Flip(screen);
 
-    SDL_FillRect(map_image, NULL, SDL_MapRGB(map_image->format, 0, 0 ,0));
+    generate_map();
+
     //SDL_Flip(map_image);
     quit = 0;
     
@@ -121,6 +122,7 @@ static int game_init(void)
         DEBUGMSG(debug_game, "File not found!");
         return 1;
     }
+
     
     if(map_xml(fp))
     {
@@ -129,6 +131,8 @@ static int game_init(void)
         return 1;
     }
 
+    generate_map();
+
     return 0;
 }
 
@@ -183,11 +187,7 @@ static void game_draw(void)
 {
     SDL_Rect dest;
 
-    dest.x = 200;
-    dest.y = 200;
 
-    SDL_BlitSurface(terrain[TUNDRA_CENTER_1], NULL, map_image, &dest);
-    SDL_BlitSurface(objects[OBJ_TROPICAL], NULL, map_image, &dest);
 
     origin.x = 0;
     origin.y = 0;
diff --git a/src/map.c b/src/map.c
index 06a5e85..4cfa1e9 100644
--- a/src/map.c
+++ b/src/map.c
@@ -12,13 +12,20 @@
  */
 
 #include "tuxhistory.h"
-#include "fileops.h"
 
 #include<ctype.h>
 #include<mxml.h>
 
+#include "SDL.h"
+#include "SDL_image.h"
+#include "SDL_extras.h"
+
+#include "fileops.h"
 #include "map.h"
 
+SDL_Surface* map_image;
+
+
 int get_terrain_enum(char *);
 void str_upper(char *string);
 
@@ -36,12 +43,16 @@ int map_xml(FILE *fp)
     x = 0;
     y = 0;
 
+    x_tildes = -1;
+    y_tildes = -1;
+
     for(inode = mxmlFindElement(tree, tree, "row", 
                 NULL, NULL, MXML_DESCEND);
             inode != NULL;
             inode = mxmlFindElement(inode, tree, "row",
                 NULL, NULL, MXML_DESCEND))
     {
+        y = 0;
         for(jnode = mxmlFindElement(inode, inode, "tilde",
                     NULL, NULL, MXML_DESCEND);
                 jnode != NULL;
@@ -73,9 +84,26 @@ int map_xml(FILE *fp)
 
             y++;
         }
+        if(y_tildes == -1)
+        {
+            y_tildes = y - 1;
+        }
+        else
+        {
+            if((y - 1) != y_tildes)
+            {
+                printf("\nBad map file...\n");
+                return 1;
+            }
+        }
         x++;
         printf("\n");
     }
+
+    if(x_tildes == -1)
+    {
+        x_tildes = x - 1;
+    }
     
     
 
@@ -121,6 +149,58 @@ int get_terrain_enum(char *terrain_string)
         return -1;
 }
 
+
+int generate_map(void)
+{
+    SDL_Rect dest;
+    int i,j;
+    int x, y;
+
+    map_image = NULL;
+    int w, h;
+    w = terrain[TUNDRA_CENTER_1]->w * x_tildes * 2;
+    h = terrain[TUNDRA_CENTER_1]->h * y_tildes * 2;
+    map_image = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 
+            32, rmask, gmask, bmask, amask); 
+
+    if(map_image == NULL)
+    {
+        DEBUGMSG(debug_setup,"Couldn't create img_image\n");
+        return 1;
+    }
+
+    SDL_FillRect(map_image, NULL, SDL_MapRGB(map_image->format, 0, 0 ,0));
+
+    dest.x = (map_image->w/2)-(terrain[TUNDRA_CENTER_1]->w/2);
+    dest.y = terrain[TUNDRA_CENTER_1]->h/2;
+
+    printf("[%d,%d]\n", x_tildes, y_tildes);
+
+    x = dest.x;
+    y = dest.y;
+
+    for (i = 0; i <= x_tildes; i++)
+    {
+        for (j = 0; j <= y_tildes; j++)
+        {
+            printf(" (%d,%d) (%d,%d)\n", dest.x, dest.y, i, j);
+            SDL_BlitSurface(terrain[TUNDRA_CENTER_1], NULL, map_image, &dest);
+            dest.x = dest.x + (terrain[TUNDRA_CENTER_1]->w/2);
+            dest.y = dest.y + (terrain[TUNDRA_CENTER_1]->h/2);
+        }
+        x = x - (terrain[TUNDRA_CENTER_1]->w/2);
+        y = y + (terrain[TUNDRA_CENTER_1]->h/2);
+        dest.x = x;
+        dest.y = y;
+    }
+    return 0;
+}
+
+void free_map(void)
+{
+  SDL_FreeSurface(map_image);
+}
+
 // TODO: Segfault error still in this function!
 void str_upper(char *string)
 {
diff --git a/src/map.h b/src/map.h
index 2c49b98..969ac88 100644
--- a/src/map.h
+++ b/src/map.h
@@ -53,12 +53,20 @@ typedef struct {
     //th_obj *obj;// Pointer to object
 }th_map;
 
+int flag_map; // Map flag: is a map surface allocated? 
+int x_tildes;
+int y_tildes;
+
 int map_xml(FILE *fp);
 
-// generate a map from the basic xml input to a 
-// visual atractive one.
+// generate_map() generate a map from the basic
+// xml input to a visual atractive one and store 
+// the image in SDL_Surface *map_image.
+// NOTE: map_xml() need to be called before!
+// Use free_map() at the end of the program.
 
 int generate_map(void);
+void free_map(void);
 
 // th_obj represent each object in the field, in 
 // each tilde there can be only one object.
diff --git a/src/setup.c b/src/setup.c
index 7489cdc..542385a 100644
--- a/src/setup.c
+++ b/src/setup.c
@@ -73,7 +73,6 @@ th_obj* object = NULL;
 
 
 SDL_Surface* screen;
-SDL_Surface* map_image;
 SDL_Surface* images[NUM_IMAGES];
 sprite* sprites[NUM_SPRITES];
 SDL_Surface* terrain[NUM_TERRAINS];
@@ -625,17 +624,6 @@ void data_memory_alloc(void)
         }
     }
 
-    map_image = NULL;
-    //printf("%d", terrain[TUNDRA_CENTER_1]->w);
-    
-    map_image = SDL_CreateRGBSurface(SDL_SWSURFACE, 1000, 1000, 
-            32, rmask, gmask, bmask, amask); 
-    if(map_image == NULL)
-    {
-        DEBUGMSG(debug_setup,"Couldn't create img_image\n");
-        exit(1);
-    }
-
 }
 
 /* save options and free heap */
@@ -683,7 +671,7 @@ void cleanup_memory(void)
   }
   FREE(map);
 
-  SDL_FreeSurface(map_image);
+  free_map();
 
   /* Free all images and sounds used by SDL: */
   Cleanup_SDL_Text();

-- 
tuxhistory - Educational history game



More information about the Tux4kids-commits mailing list