From 5b4847d15c390df9e05b52ab8ad8150f539db905 Mon Sep 17 00:00:00 2001 From: Aaron Mueller Date: Mon, 14 Dec 2009 00:28:06 +0100 Subject: [PATCH] Add dynamic generated terrain --- src/config.c | 1 + src/config.h | 2 +- src/draw.c | 40 ++++++++++++++++++++++++++-------------- src/draw.h | 12 ++++++++++-- src/main.c | 6 ++++++ 5 files changed, 44 insertions(+), 17 deletions(-) diff --git a/src/config.c b/src/config.c index e4f8716..f49bae8 100644 --- a/src/config.c +++ b/src/config.c @@ -7,4 +7,5 @@ SDL_Surface *screen = NULL; SDL_Event event; bool gameRunning = true; +int *terrain = NULL; diff --git a/src/config.h b/src/config.h index 70b2c70..9cb6ffc 100644 --- a/src/config.h +++ b/src/config.h @@ -9,7 +9,7 @@ extern SDL_Surface *screen; // Screen to paint on extern SDL_Event event; // Event storage extern bool gameRunning; // Set this variable to false to shutdown the game - +extern int *terrain; // Game terrain // Constants diff --git a/src/draw.c b/src/draw.c index a426d83..e449574 100644 --- a/src/draw.c +++ b/src/draw.c @@ -17,6 +17,10 @@ int _sign(float a) { return a > 0 ? 1 : (a < 0 ? -1 : 0); } +float _random(float from, float to) { + return ((to-from)*((float)rand()/RAND_MAX))+from; +} + void drawLine(int x1, int y1, int x2, int y2, int color) { float u, s, v, d1x, d1y, d2x, d2y, m, n; int x = x1, y = y1; @@ -73,22 +77,30 @@ void drawRect(int x1, int y1, int x2, int y2, int color, bool filled) { void drawCircle(int x, int y, int radius, int color, bool filled) { } -void drawScreen() { - Uint32 white = SDL_MapRGB(screen->format, 255, 255, 255); - drawPixel(SCREEN_WIDTH/2, SCREEN_HEIGHT/2, white); - for (int i=0; i<250; i+=1) - drawLine(200, 300-i, 600, 300+i, SDL_MapRGB(screen->format, 0, 0, i)); +int* generateTerrain(float peakheight, float flatness) { + time_t start; srand(start); - drawRect(0, 50, 200, 300, white, true); - drawRect(600, 300, 799, 550, white, false); + float offset = (float)SCREEN_HEIGHT/2; + float r1 = _random(1.0, 5.0); + float r2 = _random(1.0, 5.0); + float r3 = _random(1.0, 5.0); + int *yvals = (int*)malloc(SCREEN_WIDTH*sizeof(int)); + printf("r1=%f, r2=%f, r3=%f\n", r1, r2, r3); - // Random dots - for (int i=0; i<100; i++) - drawPixel(rand()%200+600, rand()%250+300, white); + for (int x=0; xformat, 255, 255, 255); + for (int x=0; x