summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcitrons <citrons@mondecitronne.com>2023-10-14 00:20:50 -0500
committercitrons <citrons@mondecitronne.com>2023-10-14 00:20:50 -0500
commit0928b5cbbde02d4a6c58ca940040ae4a1c2ac2e4 (patch)
treec5725ba5dc07a810ed1e0e743658a1fd0d2c6e20
parent55ea3a406e7232dc5848101dd806819e59df4cac (diff)
give the terrain a texture
-rw-r--r--random.c17
-rw-r--r--random.h1
-rw-r--r--world.c16
3 files changed, 28 insertions, 6 deletions
diff --git a/random.c b/random.c
index c53d378..90dc3b5 100644
--- a/random.c
+++ b/random.c
@@ -2,14 +2,25 @@
static Uint64 random = 0xbee;
+Uint64 hash(Uint64 n) {
+ Uint64 h = 0;
+ for (int i = 0; i < 8; i++) {
+ h += (n >> i * 8) & 0xFF;
+ h += h << 10;
+ h ^= h >> 6;
+ }
+ h += h << 3;
+ h ^= h >> 11;
+ h += h << 15;
+ return h;
+}
+
void seed_rand(Uint64 seed) {
random ^= seed;
}
Uint64 get_rand() {
- random ^= random >> 7;
- random ^= random << 9;
- random ^= random >> 13;
+ random = hash(random);
return random;
}
diff --git a/random.h b/random.h
index 18a032b..ed07bcd 100644
--- a/random.h
+++ b/random.h
@@ -1,6 +1,7 @@
#ifndef RANDOM_H
#define RANDOM_H
+Uint64 hash(Uint64 i);
void seed_rand(Uint64 seed);
Uint64 get_rand(void);
int rand_int(void);
diff --git a/world.c b/world.c
index 2ab924e..8899ff6 100644
--- a/world.c
+++ b/world.c
@@ -12,7 +12,7 @@ SDL_Color tile_colors[] = {
[TILE_BLUE] = {0x00, 0x00, 0xFF, 0xFF},
[TILE_LIGHT] = {0xFF, 0xFF, 0xFF, 0xFF},
[TILE_EMPTY] = {0x00, 0x00, 0x00, 0xFF},
- [TILE_WALL] = {0x50, 0x50, 0x50, 0xFF},
+ [TILE_WALL] = {0x40, 0x40, 0x40, 0xFF},
[TILE_BLOCK_WHITE] = {0x80, 0x80, 0x80, 0xFF},
};
@@ -79,14 +79,24 @@ SDL_Texture *render_chunk(SDL_Renderer *rend, chunk *c) {
SDL_assert(c->tex);
c->dirty = SDL_TRUE;
}
+
if (c->dirty) {
int pitch;
void *out;
SDL_assert(SDL_LockTexture(c->tex, NULL, &out, &pitch) >= 0);
SDL_Color *pixels = out;
- for (int i = 0; i < CHUNK_DIM * CHUNK_DIM; i++)
- pixels[i] = tile_colors[c->tiles[i]];
+ for (int i = 0; i < CHUNK_DIM * CHUNK_DIM; i++) {
+ tile t = c->tiles[i];
+ SDL_Color color = tile_colors[t];
+ if (t == TILE_WALL) {
+ int variance = hash(i) % 5;
+ color.r += variance;
+ color.g += variance;
+ color.b += variance;
+ }
+ pixels[i] = color;
+ }
SDL_UnlockTexture(c->tex);
c->dirty = SDL_FALSE;