[Tux4kids-commits] [SCM] tuxhistory - Educational history game branch, master, updated. a8a3fe57a7d8b0502f9e680a47f88816eb78c497
julio (none)
julio at julio-desktop.
Wed Jun 30 03:07:33 UTC 2010
The following commit has been merged in the master branch:
commit a8a3fe57a7d8b0502f9e680a47f88816eb78c497
Author: julio <julio at julio-desktop.(none)>
Date: Tue Jun 29 22:06:24 2010 -0500
TuxHistory reads and render map files.
diff --git a/src/globals.h b/src/globals.h
index 019adf8..40011ec 100644
--- a/src/globals.h
+++ b/src/globals.h
@@ -40,7 +40,9 @@ typedef enum { false, true } bool;
#define max(a,b) (((a) > (b)) ? (a) : (b))
// Free memory marco
-#define FREE(p) do { free(p); (p) = NULL; } while(0)
+#define FREE(p) do { free(p); (p) = NULL; } while(0)
+// Number of elements of a array
+#define NUM_ELEM(x) (sizeof (x) / sizeof (*(x)))
/* for Tim's feedback speed control code */
//#define FEEDBACK_DEBUG
diff --git a/src/map.c b/src/map.c
index 4cfa1e9..255f5a5 100644
--- a/src/map.c
+++ b/src/map.c
@@ -1,7 +1,7 @@
/*
* map.c
*
- * Description: The main game loop for TuxHistory
+ * Description: Map functions for TuxHistory
*
* Author: Jesús Manuel Mager Hois (fongog at gmail.com) 2010
* Copyright: GPL v3 or later
@@ -20,14 +20,18 @@
#include "SDL_image.h"
#include "SDL_extras.h"
+#include "globals.h"
#include "fileops.h"
#include "map.h"
SDL_Surface* map_image;
-int get_terrain_enum(char *);
-void str_upper(char *string);
+static int get_terrain_enum(char *);
+static int *get_context_tildes(int, int);
+static int *get_draw_tilde(int *, int);
+static void str_upper(char *);
+
int map_xml(FILE *fp)
{
@@ -104,9 +108,13 @@ int map_xml(FILE *fp)
{
x_tildes = x - 1;
}
-
-
+ if(x_tildes != y_tildes)
+ {
+ printf("Map file must be a perfect squer");
+ return 1;
+ }
+
mxmlDelete(jnode);
mxmlDelete(inode);
mxmlDelete(node);
@@ -149,12 +157,218 @@ int get_terrain_enum(char *terrain_string)
return -1;
}
+/* ---------------------------
+ * | | | |
+ * | 1 | 2 | 3 |
+ * | NW | N | NE |
+ * --------+---------+--------
+ * | | NW N NE | |
+ * | 4 W | W 0 E | E 5 |
+ * | | SW S SE | |
+ * --------+---------+--------
+ * | 6 SW | S | SE 8 |
+ * | | 7 | |
+ * | | | |
+ * ---------------------------
+ */
+
+//Return array has 9 elements
+static int *get_context_tildes(int x, int y)
+{
+ //int i,j;
+ int *a;
+
+ a = (int *)malloc(9*sizeof(int));
+ if( a == NULL )
+ {
+ printf("get_context_tildes: Error trying to allocate memory!");
+ return NULL;
+ }
+
+
+ *a = map[x][y].terrain;
+ if(y <= 0)
+ {
+ *(a + 1) = -1;
+ *(a + 2) = -1;
+ *(a + 3) = -1;
+ if(x <= 0)
+ {
+ *(a + 4) = -1;
+ *(a + 6) = -1;
+ *(a + 5) = map[x+1][y].terrain;
+ *(a + 7) = map[x+1][y].terrain;
+ *(a + 8) = map[x+1][y+1].terrain;
+
+ }
+ else if(x >= x_tildes)
+ {
+ *(a + 5) = -1;
+ *(a + 8) = -1;
+ *(a + 4) = map[x-1][y].terrain;
+ *(a + 6) = map[x-1][y+1].terrain;
+ *(a + 7) = map[x+1][y].terrain;
+ }
+ else
+ {
+ *(a + 4) = map[x-1][y].terrain;
+ *(a + 5) = map[x+1][y].terrain;
+ *(a + 6) = map[x-1][y+1].terrain;
+ *(a + 7) = map[x+1][y].terrain;
+ *(a + 8) = map[x+1][y+1].terrain;
+ }
+ }
+ else if(y >= y_tildes)
+ {
+ *(a + 6) = -1;
+ *(a + 7) = -1;
+ *(a + 8) = -1;
+ if(x <= 0)
+ {
+ *(a + 1) = -1;
+ *(a + 4) = -1;
+ *(a + 2) = map[x][y-1].terrain;
+ *(a + 3) = map[x+1][y-1].terrain;
+ *(a + 5) = map[x+1][y].terrain;
+ }
+ else if(x >= x_tildes)
+ {
+ *(a + 3) = -1;
+ *(a + 5) = -1;
+ *(a + 1) = map[x-1][y-1].terrain;
+ *(a + 2) = map[x][y-1].terrain;
+ *(a + 4) = map[x-1][y].terrain;
+ }
+ else
+ {
+ *(a + 1) = map[x-1][y-1].terrain;
+ *(a + 2) = map[x][y-1].terrain;
+ *(a + 3) = map[x+1][y-1].terrain;
+ *(a + 4) = map[x-1][y].terrain;
+ *(a + 5) = map[x+1][y].terrain;
+ }
+ }
+ else
+ {
+ if(x <= 0)
+ {
+ *(a + 1) = -1;
+ *(a + 4) = -1;
+ *(a + 6) = -1;
+ *(a + 2) = map[x][y-1].terrain;
+ *(a + 3) = map[x+1][y-1].terrain;
+ *(a + 5) = map[x+1][y].terrain;
+ *(a + 7) = map[x+1][y].terrain;
+ *(a + 8) = map[x+1][y+1].terrain;
+ }
+ else if(x >= x_tildes)
+ {
+ *(a + 3) = -1;
+ *(a + 5) = -1;
+ *(a + 8) = -1;
+ *(a + 1) = map[x-1][y-1].terrain;
+ *(a + 2) = map[x][y-1].terrain;
+ *(a + 4) = map[x-1][y].terrain;
+ *(a + 6) = map[x-1][y+1].terrain;
+ *(a + 7) = map[x+1][y].terrain;
+ }
+ else
+ {
+ *(a + 1) = map[x-1][y-1].terrain;
+ *(a + 2) = map[x][y-1].terrain;
+ *(a + 3) = map[x+1][y-1].terrain;
+ *(a + 4) = map[x-1][y].terrain;
+ *(a + 5) = map[x+1][y].terrain;
+ *(a + 6) = map[x-1][y+1].terrain;
+ *(a + 7) = map[x+1][y].terrain;
+ *(a + 8) = map[x+1][y+1].terrain;
+ }
+ }
+ return a;
+}
+
+// return a array with the terrain enum values to draw the map.
+static int *get_draw_tilde(int *array, int oe)
+{
+ int *a;
+
+ a = (int *)malloc(3*sizeof(int));
+ if( a == NULL )
+ {
+ printf("get_context_tildes: Error trying to allocate memory!");
+ return NULL;
+ }
+
+ if(*array == HIGHSEA)
+ {
+ *a = ((oe%2)==0) ? HIGHSEAS_CENTER_0 : HIGHSEAS_CENTER_1;
+ }
+ else if(*array == TUNDRA)
+ {
+ *a = ((oe%2)==0) ? TUNDRA_CENTER_0 : TUNDRA_CENTER_1;
+ }
+ else if(*array == SWAMP)
+ {
+ *a = ((oe%2)==0) ? SWAMP_CENTER_0 : SWAMP_CENTER_1;
+ }
+ else if(*array == UNEXPLORED)
+ {
+ *a = ((oe%2)==0) ? UNEXPLORED_CENTER_0 : UNEXPLORED_CENTER_1;
+ }
+ else if(*array == DESERT)
+ {
+ *a = ((oe%2)==0) ? DESERT_CENTER_0 : DESERT_CENTER_1;
+ }
+ else if(*array == GRASSLAND)
+ {
+ *a = ((oe%2)==0) ? GRASSLAND_CENTER_0 : GRASSLAND_CENTER_1;
+ }
+ else if(*array == ARCTIC)
+ {
+ *a = ((oe%2)==0) ? ARCTIC_CENTER_0 : ARCTIC_CENTER_1;
+ }
+ else if(*array == OCEAN)
+ {
+ *a = ((oe%2)==0) ? OCEAN_CENTER_0 : OCEAN_CENTER_1;
+ }
+ else if(*array == MARSH)
+ {
+ *a = ((oe%2)==0) ? MARSH_CENTER_0 : MARSH_CENTER_1;
+ }
+ else if(*array == SAVANNAH)
+ {
+ *a = ((oe%2)==0) ? SAVANNAH_CENTER_0 : SAVANNAH_CENTER_1;
+ }
+ else if(*array == PLAINS)
+ {
+ *a = ((oe%2)==0) ? PLAINS_CENTER_0 : PLAINS_CENTER_1;
+ }
+ else if(*array == PRAIRIE)
+ {
+ *a = ((oe%2)==0) ? PRAIRIE_CENTER_0 : PRAIRIE_CENTER_1;
+ }
+ else
+ {
+ printf("Unknown: %d\n", *array);
+ return NULL;
+
+ }
+ //*(a + 1) = -1;
+ //*(a + 2) = -1;
+
+
+
+ return a;
+}
int generate_map(void)
{
SDL_Rect dest;
- int i,j;
+ int i, j, k;
+ int oe;
int x, y;
+ int *context_array;
+ int *img_enums;
map_image = NULL;
int w, h;
@@ -172,26 +386,54 @@ int generate_map(void)
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;
+ dest.y = (map_image->h/2);
printf("[%d,%d]\n", x_tildes, y_tildes);
x = dest.x;
y = dest.y;
-
- for (i = 0; i <= x_tildes; i++)
+ k = 0;
+ for (i = x_tildes; i >= 0; i--)
{
- for (j = 0; j <= y_tildes; j++)
+ oe = k + 1;
+ for (j = y_tildes; j >= 0; j--)
{
+ k++;
+
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);
+
+ context_array = get_context_tildes(i,j);
+ if (context_array == NULL)
+ {
+ return 1;
+ }
+ img_enums = get_draw_tilde(context_array, k);
+ if(img_enums == NULL)
+ {
+ return 1;
+ }
+
+ printf("ENUM: %d GRASSLAND: %d\n", *img_enums, GRASSLAND_CENTER_0);
+ //Draw in the map buffer the resulting values
+ SDL_BlitSurface(terrain[*img_enums], NULL, map_image, &dest);
+
+ //Prepare te new coords for the next tilde
+ dest.x = dest.x - (terrain[*img_enums]->w/2);
+ dest.y = dest.y - (terrain[*img_enums]->h/2);
+ FREE(context_array);
+ FREE(img_enums);
+
}
- x = x - (terrain[TUNDRA_CENTER_1]->w/2);
- y = 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;
+
+ //k counter defines if the tilde is even or odd.
+ //The next row must be diferent to this row.
+ if((((oe%2)==0) && (((k+1)%2)==0)) ||
+ (((oe%2)!=0) && (((k+1)%2)!=0)))
+ k++;
}
return 0;
}
--
tuxhistory - Educational history game
More information about the Tux4kids-commits
mailing list