polyadvent

A game engine from scratch in C
git clone git://jb55.com/polyadvent
Log | Files | Refs | README

commit 219710e9db514203b1e080ddca5f4de8dce1d110
parent 6f28e98dcd3df22ef1bfb2428a308798123cca88
Author: William Casarin <jb55@jb55.com>
Date:   Sun, 29 Apr 2018 12:26:37 -0700

wip

Diffstat:
MMakefile | 1+
Msrc/common.h | 5+++++
Msrc/main.c | 9+++++++--
Msrc/poisson.c | 1+
Msrc/poisson.h | 4+---
Asrc/uniform.c | 17+++++++++++++++++
Asrc/uniform.h | 9+++++++++
Msrc/update.c | 15+++++++++------
8 files changed, 50 insertions(+), 11 deletions(-)

diff --git a/Makefile b/Makefile @@ -19,6 +19,7 @@ OBJS += $(SRC)/event.o OBJS += $(SRC)/file.o OBJS += $(SRC)/perlin.o OBJS += $(SRC)/poisson.o +OBJS += $(SRC)/uniform.o OBJS += $(SRC)/game.o OBJS += $(SRC)/mat4/mat4.o OBJS += $(SRC)/vec3/vec3.o diff --git a/src/common.h b/src/common.h @@ -21,4 +21,9 @@ typedef signed short s16; typedef unsigned int u32; typedef signed int s32; +struct point { + double x, y; +}; + + #endif /* POLYADVENT_COMMON_H */ diff --git a/src/main.c b/src/main.c @@ -15,6 +15,7 @@ #include <assert.h> #include <time.h> #include "poisson.h" +#include "uniform.h" int main(void) @@ -47,9 +48,13 @@ int main(void) const double size = 200; const double pdist = 1.7; /* printf("samples seed %d\n", seed); */ + const int n_samples = (int)(size * size); + struct point *samples = uniform_samples((int)(size*size), size); + terrain.n_samples = n_samples; + /* struct point *samples = poisson_disk_samples(pdist, size, 30, &terrain.n_samples); */ - struct point *samples = load_samples(NULL, &terrain.n_samples); - /* draw_samples(samples, pdist, terrain.n_samples, size); */ + /* struct point *samples = load_samples(NULL, &terrain.n_samples); */ + draw_samples(samples, pdist, terrain.n_samples, size); /* save_samples(samples, seed, terrain.n_samples); */ terrain.settings = (struct perlin_settings){ diff --git a/src/poisson.c b/src/poisson.c @@ -2,6 +2,7 @@ #include <stdlib.h> #include <math.h> #include "util.h" +#include "common.h" #include "poisson.h" diff --git a/src/poisson.h b/src/poisson.h @@ -2,9 +2,7 @@ #ifndef POLYADVENT_POISSON_H #define POLYADVENT_POISSON_H -struct point { - double x, y; -}; +struct point; void draw_samples(struct point *samples, double point_dist, const int nsamples, const int size); diff --git a/src/uniform.c b/src/uniform.c @@ -0,0 +1,17 @@ + +#include "common.h" +#include "util.h" + +struct point * +uniform_samples(const int count, double size) { + struct point *samples = malloc(sizeof(*samples) * count); + + for (int i = 0; i < count; ++i) { + samples[i].x = rand_0to1() * size; + samples[i].y = rand_0to1() * size; + } + + return samples; +} + + diff --git a/src/uniform.h b/src/uniform.h @@ -0,0 +1,9 @@ + + +#ifndef POLYADVENT_UNIFORM_H +#define POLYADVENT_UNIFORM_H + +struct point * uniform_samples(const int count, double size); + + +#endif /* POLYADVENT_UNIFORM_H */ diff --git a/src/update.c b/src/update.c @@ -54,8 +54,8 @@ void update (struct game *game, u32 dt) { static int last_input = 0; static int last_gen_time = 50; static float n = 1; - static double offset = 0.5; static int first = 1; + static double last_ox, last_oy, last_oz; struct resources *res = &game->test_resources; static int stopped = 0; struct perlin_settings *ts = &game->terrain->settings; @@ -105,7 +105,12 @@ void update (struct game *game, u32 dt) { } else { passed = 0; - if (!stopped) { + double ox = tnode[12]; + double oy = tnode[13]; + + bool changed = last_ox != ox || last_oy != oy || last_oz != tnode[14]; + + if (!stopped && changed) { for (int i = 12; i < 14; ++i) tnode[i] = max(tnode[i], 0); @@ -113,13 +118,11 @@ void update (struct game *game, u32 dt) { double scale = tnode[14] * 0.01; if (scale == 0) scale = 1.0; - double ox = tnode[12]; - double oy = tnode[13]; printf("terrain %f %f %f\n", tnode[12], tnode[13], tnode[14]); - ts->ox = ox; - ts->oy = oy; + last_ox = ts->ox = ox; + last_oy = ts->oy = oy; ts->scale = scale; /* ts.o1s = fabs(sin(1/n) * 0.25); */