From 3a92d73887f0e648fd54da765142ad888d481480 Mon Sep 17 00:00:00 2001 From: Miguel Angel Astor Romero Date: Fri, 28 Feb 2014 13:26:55 -0430 Subject: [PATCH] Turned into a lib. --- .gitignore | 2 +- Makefile | 26 +++++++++++++++++--------- ds.c => island.c | 25 +++++++++++++++++++++++-- ds.h => island.h | 23 +++++++++++++++++++---- main.c | 2 +- 5 files changed, 61 insertions(+), 17 deletions(-) rename ds.c => island.c (93%) rename ds.h => island.h (58%) diff --git a/.gitignore b/.gitignore index 2145e4e..a4316c2 100644 --- a/.gitignore +++ b/.gitignore @@ -16,7 +16,7 @@ *.exe *.out *.app -ds +island # Emacs backups *~ diff --git a/Makefile b/Makefile index eda70ff..695ed3d 100644 --- a/Makefile +++ b/Makefile @@ -1,16 +1,24 @@ -LDLIBS = -lm +CC := gcc +CFLAGS := -I./ -ansi -pedantic +LDFLAGS := -L./ +LDLIBS := -lm -lisland -all: CFLAGS = -O3 -ansi -pedantic -all: ds +all: CFLAGS += -O3 +all: lib island -debug: CFLAGS = -g -ansi -pedantic -debug: ds +debug: CFLAGS += -g +debug: lib island -ds: main.o ds.o +lib: libisland.a -main.o: main.c ds.h +libisland.a: island.o + ar rcs libisland.a island.o -ds.o: ds.c ds.h +island: main.o + +main.o: main.c island.h + +island.o: island.c island.h clean: - $(RM) *.o ds + $(RM) *.o *.a island diff --git a/ds.c b/island.c similarity index 93% rename from ds.c rename to island.c index fbf7aee..2912bd4 100644 --- a/ds.c +++ b/island.c @@ -18,7 +18,7 @@ #endif -#include "ds.h" +#include "island.h" static const float SEED = 1000.0; @@ -160,7 +160,6 @@ extern void mult ( float *** fmap, int *** imap, unsigned int MAP_SIZE ) { for ( i = 0; i < MAP_SIZE; ++i ) { for ( j = 0; j < MAP_SIZE; ++j ) { - /*fprintf ( stderr, "%f %d\n", ( * fmap )[ i ][ j ], ( * imap )[ i ][ j ] );*/ ( * imap )[ i ][ j ] = ( int )( ( ( * fmap )[ i ][ j ] * ( ( float ) ( * imap )[ i ][ j ] / 255.0f ) ) * 250.0f ); } } @@ -212,3 +211,25 @@ static float randomFloat() { r = ( r * 2.0 ) - 1.0; return r; } + +terrain_t terrainType( int h ) { + if ( h < 30 ) { + if ( h < 15 ) { + return DEEP_WATER; + } else { + return SHALLOW_WATER; + } + } else { + if ( h < 40 ) { + return SAND; + } else if ( h >= 40 && h < 100 ) { + return GRASS; + } else if ( h >= 100 && h < 150 ) { + return FOREST; + } else if ( h >= 180 ) { + return MOUNTAIN; + } else { + return HILL; + } + } +} diff --git a/ds.h b/island.h similarity index 58% rename from ds.h rename to island.h index d4eecc2..5230be0 100644 --- a/ds.h +++ b/island.h @@ -1,15 +1,24 @@ -#ifndef DS_H -#define DS_H +#ifndef ISLAND_H +#define ISLAND_H #ifdef __cplusplus extern "C" { #endif +typedef enum TERRAIN_TYPE { DEEP_WATER, + SHALLOW_WATER, + SAND, + GRASS, + FOREST, + HILL, + MOUNTAIN + } terrain_t; + /** * Generate a diamond-square fractal map. */ extern void ds ( float ***, const unsigned int ); - + /** * Generate a mask using particle deposition. */ @@ -34,7 +43,13 @@ extern "C" { * Multiply the diamond square map with the island mask. * Both matrices must have been normalized before. */ - extern void mult ( float ***, int *** , unsigned int); + extern void mult ( float ***, int ***, unsigned int ); + + /** + * Given a sample from a heightmap, return the terrain + * type that correspond it. + */ + terrain_t terrainType( int ); #ifdef __cplusplus } diff --git a/main.c b/main.c index e3e9d30..e975c06 100644 --- a/main.c +++ b/main.c @@ -8,7 +8,7 @@ #include #include -#include "ds.h" +#include "island.h" struct COLOR { unsigned char r;