Add dynamic generated terrain
This commit is contained in:
parent
b7d91f259f
commit
5b4847d15c
5 changed files with 44 additions and 17 deletions
|
@ -7,4 +7,5 @@
|
||||||
SDL_Surface *screen = NULL;
|
SDL_Surface *screen = NULL;
|
||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
bool gameRunning = true;
|
bool gameRunning = true;
|
||||||
|
int *terrain = NULL;
|
||||||
|
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
extern SDL_Surface *screen; // Screen to paint on
|
extern SDL_Surface *screen; // Screen to paint on
|
||||||
extern SDL_Event event; // Event storage
|
extern SDL_Event event; // Event storage
|
||||||
extern bool gameRunning; // Set this variable to false to shutdown the game
|
extern bool gameRunning; // Set this variable to false to shutdown the game
|
||||||
|
extern int *terrain; // Game terrain
|
||||||
|
|
||||||
// Constants
|
// Constants
|
||||||
|
|
||||||
|
|
42
src/draw.c
42
src/draw.c
|
@ -17,6 +17,10 @@ int _sign(float a) {
|
||||||
return a > 0 ? 1 : (a < 0 ? -1 : 0);
|
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) {
|
void drawLine(int x1, int y1, int x2, int y2, int color) {
|
||||||
float u, s, v, d1x, d1y, d2x, d2y, m, n;
|
float u, s, v, d1x, d1y, d2x, d2y, m, n;
|
||||||
int x = x1, y = y1;
|
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 drawCircle(int x, int y, int radius, int color, bool filled) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void drawScreen() {
|
int* generateTerrain(float peakheight, float flatness) {
|
||||||
Uint32 white = SDL_MapRGB(screen->format, 255, 255, 255);
|
time_t start; srand(start);
|
||||||
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));
|
|
||||||
|
|
||||||
drawRect(0, 50, 200, 300, white, true);
|
float offset = (float)SCREEN_HEIGHT/2;
|
||||||
drawRect(600, 300, 799, 550, white, false);
|
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 x=0; x<SCREEN_WIDTH; x++) {
|
||||||
for (int i=0; i<100; i++)
|
float y;
|
||||||
drawPixel(rand()%200+600, rand()%250+300, white);
|
y = peakheight/r1*sin((x/flatness*r1)+r1);
|
||||||
|
y += peakheight/r2*sin((x/flatness*r2)+r2);
|
||||||
drawCircle(SCREEN_WIDTH/2, SCREEN_HEIGHT/2, 100, white, true);
|
y += peakheight/r3*sin((x/flatness*r3)+r3);
|
||||||
drawCircle(SCREEN_WIDTH/2, SCREEN_HEIGHT/2, 104, white, false);
|
y += offset;
|
||||||
|
yvals[x] = (int)y;
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
12
src/draw.h
12
src/draw.h
|
@ -2,13 +2,21 @@
|
||||||
#ifndef DRAW_H
|
#ifndef DRAW_H
|
||||||
#define 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
|
* function to paint on the screen. We will be use this function for all paint
|
||||||
* stuff.
|
* stuff.
|
||||||
*/
|
*/
|
||||||
void drawPixel(int x, int y, int color);
|
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
|
* This function will be called for every frame. Here you can actual draw things
|
||||||
* on the screen.
|
* on the screen.
|
||||||
|
|
|
@ -10,6 +10,9 @@ int main(int argc, char **argv) {
|
||||||
SDL_Init(SDL_INIT_VIDEO);
|
SDL_Init(SDL_INIT_VIDEO);
|
||||||
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
|
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
|
// the main game loop
|
||||||
while (gameRunning == true) {
|
while (gameRunning == true) {
|
||||||
// Check for events
|
// Check for events
|
||||||
|
@ -24,6 +27,9 @@ int main(int argc, char **argv) {
|
||||||
SDL_Flip(screen);
|
SDL_Flip(screen);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Cleanup and quit
|
||||||
|
free(terrain);
|
||||||
|
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue