Add dynamic generated terrain

This commit is contained in:
Aaron Mueller 2009-12-14 00:28:06 +01:00
parent b7d91f259f
commit 5b4847d15c
5 changed files with 44 additions and 17 deletions

View file

@ -7,4 +7,5 @@
SDL_Surface *screen = NULL;
SDL_Event event;
bool gameRunning = true;
int *terrain = NULL;

View file

@ -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

View file

@ -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; x<SCREEN_WIDTH; x++) {
float y;
y = peakheight/r1*sin((x/flatness*r1)+r1);
y += peakheight/r2*sin((x/flatness*r2)+r2);
y += peakheight/r3*sin((x/flatness*r3)+r3);
y += offset;
yvals[x] = (int)y;
}
drawCircle(SCREEN_WIDTH/2, SCREEN_HEIGHT/2, 100, white, true);
drawCircle(SCREEN_WIDTH/2, SCREEN_HEIGHT/2, 104, white, false);
for (int i=0; i<=600; i++) drawLine(0, i, 799, i, white);
return &(yvals[0]);
}
void drawScreen() {
Uint32 white = SDL_MapRGB(screen->format, 255, 255, 255);
for (int x=0; x<SCREEN_WIDTH; x++) drawLine(x, terrain[x], x, SCREEN_HEIGHT, white);
}

View file

@ -2,13 +2,21 @@
#ifndef DRAW_H
#define DRAW_H
/*
* Draw a single pixel on the surface with the given color. This is the basic
/* Draw a single pixel on the surface with the given color. This is the basic
* function to paint on the screen. We will be use this function for all paint
* stuff.
*/
void drawPixel(int x, int y, int color);
/* Some basic shapes
*/
void drawLine(int x1, int y1, int x2, int y2, int color);
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);
int* generateTerrain(float peakheight, float flatness);
/*
* This function will be called for every frame. Here you can actual draw things
* on the screen.

View file

@ -10,6 +10,9 @@ int main(int argc, char **argv) {
SDL_Init(SDL_INIT_VIDEO);
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
// Initialize internal game state
terrain = generateTerrain(150.0, 180.0);
// the main game loop
while (gameRunning == true) {
// Check for events
@ -24,6 +27,9 @@ int main(int argc, char **argv) {
SDL_Flip(screen);
}
// Cleanup and quit
free(terrain);
SDL_Quit();
return 0;
}