|
|
dab153 |
|
|
|
3395b9 |
#include <math.h></math.h>
|
|
|
3395b9 |
#include <time.h></time.h>
|
|
|
3395b9 |
#include <string.h></string.h>
|
|
|
3395b9 |
#include <stdlib.h></stdlib.h>
|
|
|
3395b9 |
|
|
|
dab153 |
#include <helianthus.h></helianthus.h>
|
|
|
dab153 |
|
|
|
3395b9 |
#include "chunk.h"
|
|
|
3395b9 |
|
|
|
3395b9 |
|
|
|
3395b9 |
#define COUNT 100
|
|
|
3395b9 |
|
|
|
3395b9 |
double t;
|
|
|
3395b9 |
double x, y;
|
|
|
815578 |
double vx = 10, vy = 0;
|
|
|
3395b9 |
|
|
|
3395b9 |
int rootSeed;
|
|
|
3395b9 |
|
|
|
3395b9 |
Chunk chunks[COUNT];
|
|
|
3395b9 |
|
|
|
3395b9 |
|
|
|
3395b9 |
void initChunks() {
|
|
|
3395b9 |
srand(time(NULL));
|
|
|
3395b9 |
rootSeed = rand();
|
|
|
3395b9 |
Chunk *c = &chunks[0];
|
|
|
3395b9 |
chunkGenerate(c, rootSeed, NULL, -COUNT/2);
|
|
|
3395b9 |
for(int i = 1; i < COUNT; ++i, ++c)
|
|
|
3395b9 |
chunkGenerate(c+1, rootSeed, c, 1);
|
|
|
3395b9 |
}
|
|
|
3395b9 |
|
|
|
dab153 |
|
|
|
3395b9 |
void prepareChunks(double x) {
|
|
|
3395b9 |
Chunk *first = chunks, *last = first + COUNT - 1;
|
|
|
3395b9 |
int i0 = (int)floor(x/CHUNK_WIDTH) - COUNT/2;
|
|
|
3395b9 |
int offset = i0 - chunks[0].index;
|
|
|
3395b9 |
|
|
|
3395b9 |
if (offset < 0) {
|
|
|
3395b9 |
Chunk *next = last, *prev = first;
|
|
|
3395b9 |
if (offset > -COUNT) {
|
|
|
3395b9 |
memmove(first - offset, first, sizeof(*chunks)*(COUNT + offset));
|
|
|
3395b9 |
prev = first - offset;
|
|
|
3395b9 |
next = prev - 1;
|
|
|
3395b9 |
offset = 1;
|
|
|
3395b9 |
}
|
|
|
3395b9 |
while(next >= first)
|
|
|
3395b9 |
{ chunkGenerate(next, rootSeed, prev, offset); prev = next--; offset = -1; }
|
|
|
3395b9 |
} else
|
|
|
3395b9 |
if (offset > 0) {
|
|
|
3395b9 |
Chunk *next = first, *prev = last;
|
|
|
3395b9 |
if (offset < COUNT) {
|
|
|
3395b9 |
memmove(first, first + offset, sizeof(*chunks)*(COUNT - offset));
|
|
|
3395b9 |
prev = last - offset;
|
|
|
3395b9 |
next = prev + 1;
|
|
|
3395b9 |
offset = 1;
|
|
|
3395b9 |
} else {
|
|
|
3395b9 |
offset -= COUNT - 1;
|
|
|
3395b9 |
}
|
|
|
3395b9 |
while(next <= last)
|
|
|
3395b9 |
{ chunkGenerate(next, rootSeed, prev, offset); prev = next++; offset = 1; }
|
|
|
3395b9 |
}
|
|
|
3395b9 |
}
|
|
|
dab153 |
|
|
|
dab153 |
|
|
|
3395b9 |
void drawChunks(double x, double range, double t) {
|
|
|
3395b9 |
range += 5*2;
|
|
|
3395b9 |
int i0 = (int)floor((x - 0.5*range)/CHUNK_WIDTH) - chunks->index;
|
|
|
3395b9 |
int i1 = (int)floor((x + 0.5*range)/CHUNK_WIDTH) - chunks->index + 1;
|
|
|
3395b9 |
if (i0 < 0) i0 = 0;
|
|
|
3395b9 |
if (i1 > COUNT) i1 = COUNT;
|
|
|
3395b9 |
|
|
|
3395b9 |
for(int i = i0; i < i1; ++i) chunkDraw(&chunks[i], t, FLAG_GROUND);
|
|
|
3395b9 |
//for(int i = i0; i < i1; ++i) chunkDraw(&chunks[i], t, FLAG_LEAVES);
|
|
|
3395b9 |
for(int i = i0; i < i1; ++i) chunkDraw(&chunks[i], t, FLAG_BRANCHES);
|
|
|
3395b9 |
}
|
|
|
dab153 |
|
|
|
dab153 |
|
|
|
3395b9 |
double groundLevel(double x) {
|
|
|
3395b9 |
int i = (int)floor(x/CHUNK_WIDTH) - chunks->index;
|
|
|
3395b9 |
if (i < 0) return chunks->y;
|
|
|
3395b9 |
if (i > COUNT-1) return chunks[COUNT-1].y + chunks[COUNT-1].dy;
|
|
|
3395b9 |
return chunkGroundLevel(&chunks[i], x);
|
|
|
dab153 |
}
|
|
|
dab153 |
|
|
|
dab153 |
|
|
|
dab153 |
void init() {
|
|
|
3395b9 |
initChunks();
|
|
|
dab153 |
}
|
|
|
dab153 |
|
|
|
dab153 |
|
|
|
dab153 |
void draw() {
|
|
|
de7236 |
double dt = windowGetFrameTime();
|
|
|
3395b9 |
t += dt;
|
|
|
de7236 |
int w = windowGetWidth();
|
|
|
de7236 |
int h = windowGetHeight();
|
|
|
3395b9 |
double range = w;
|
|
|
3395b9 |
|
|
|
3395b9 |
double gy = groundLevel(x);
|
|
|
3395b9 |
vy = 0.5*(gy - y + 20);
|
|
|
3395b9 |
x += dt*vx;
|
|
|
3395b9 |
y += dt*vy;
|
|
|
3395b9 |
|
|
|
dab153 |
|
|
|
3395b9 |
if (keyWentDown("space")) initChunks();
|
|
|
3395b9 |
if (keyWentDown("right")) x += 10;
|
|
|
3395b9 |
if (keyWentDown("left")) x -= 10;
|
|
|
3395b9 |
if (keyWentDown("up")) y += 10;
|
|
|
3395b9 |
if (keyWentDown("down")) y -= 10;
|
|
|
dab153 |
|
|
|
dab153 |
saveState();
|
|
|
dab153 |
translate(0.5*w, 0.5*h);
|
|
|
dab153 |
zoom(10);
|
|
|
dab153 |
scale(1, -1);
|
|
|
3395b9 |
translate(-x, -y);
|
|
|
3395b9 |
range /= 10;
|
|
|
dab153 |
|
|
|
3395b9 |
prepareChunks(x);
|
|
|
3395b9 |
drawChunks(x, range, t);
|
|
|
dab153 |
|
|
|
dab153 |
restoreState();
|
|
|
dab153 |
}
|
|
|
dab153 |
|
|
|
dab153 |
|
|
|
dab153 |
int main() {
|
|
|
de7236 |
windowSetVariableFrameRate();
|
|
|
de7236 |
windowSetResizable(TRUE);
|
|
|
de7236 |
windowSetInit(&init);
|
|
|
de7236 |
windowSetDraw(&draw);
|
|
|
de7236 |
windowRun();
|
|
|
dab153 |
return 0;
|
|
|
dab153 |
}
|
|
|
dab153 |
|