Turned into a lib.

This commit is contained in:
2014-02-28 13:26:55 -04:30
parent cea1003804
commit 3a92d73887
5 changed files with 61 additions and 17 deletions

2
.gitignore vendored
View File

@@ -16,7 +16,7 @@
*.exe *.exe
*.out *.out
*.app *.app
ds island
# Emacs backups # Emacs backups
*~ *~

View File

@@ -1,16 +1,24 @@
LDLIBS = -lm CC := gcc
CFLAGS := -I./ -ansi -pedantic
LDFLAGS := -L./
LDLIBS := -lm -lisland
all: CFLAGS = -O3 -ansi -pedantic all: CFLAGS += -O3
all: ds all: lib island
debug: CFLAGS = -g -ansi -pedantic debug: CFLAGS += -g
debug: ds 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: clean:
$(RM) *.o ds $(RM) *.o *.a island

View File

@@ -18,7 +18,7 @@
#endif #endif
#include "ds.h" #include "island.h"
static const float SEED = 1000.0; 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 ( i = 0; i < MAP_SIZE; ++i ) {
for ( j = 0; j < MAP_SIZE; ++j ) { 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 ); ( * 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; r = ( r * 2.0 ) - 1.0;
return r; 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;
}
}
}

View File

@@ -1,10 +1,19 @@
#ifndef DS_H #ifndef ISLAND_H
#define DS_H #define ISLAND_H
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
typedef enum TERRAIN_TYPE { DEEP_WATER,
SHALLOW_WATER,
SAND,
GRASS,
FOREST,
HILL,
MOUNTAIN
} terrain_t;
/** /**
* Generate a diamond-square fractal map. * Generate a diamond-square fractal map.
*/ */
@@ -36,6 +45,12 @@ extern "C" {
*/ */
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 #ifdef __cplusplus
} }
#endif #endif

2
main.c
View File

@@ -8,7 +8,7 @@
#include <float.h> #include <float.h>
#include <string.h> #include <string.h>
#include "ds.h" #include "island.h"
struct COLOR { struct COLOR {
unsigned char r; unsigned char r;