commit e800f1c961247191647953c45a18fbe63529edce
parent 4c1d97d987c0ab5942fa0f0c53fddfb45e299e3c
Author: Christian Ermann <christianermann@gmail.com>
Date: Fri, 29 Jul 2022 19:41:32 -0700
Use set-width type definitions
Diffstat:
32 files changed, 249 insertions(+), 203 deletions(-)
diff --git a/include/app.h b/include/app.h
@@ -1,15 +1,17 @@
#ifndef APP_H
#define APP_H
+#include "types.h"
+
#include "glad/glad.h"
#include "GLFW/glfw3.h"
typedef struct {
const char *title;
- int width;
- int height;
- int gl_major_version;
- int gl_minor_version;
+ u16 width;
+ u16 height;
+ u8 gl_major_version;
+ u8 gl_minor_version;
} AppInfo;
typedef struct App {
diff --git a/include/camera.h b/include/camera.h
@@ -4,6 +4,7 @@
#include "input.h"
#include "vec.h"
#include "transform.h"
+#include "types.h"
typedef struct {
Vec3 n;
@@ -13,17 +14,17 @@ typedef struct {
typedef struct {
Transform *transform;
- float fovy;
- float aspect;
- float near;
- float far;
+ f32 fovy;
+ f32 aspect;
+ f32 near;
+ f32 far;
Mat4 matrix;
- float near_h;
- float near_w;
- float far_h;
- float far_w;
+ f32 near_h;
+ f32 near_w;
+ f32 far_h;
+ f32 far_w;
Plane frustum[6];
} Camera;
@@ -37,6 +38,6 @@ void Camera_defaultSettings(Camera* camera);
void Camera_updateMatrix(Camera* camera);
void Camera_updateFrustum(Camera *camera);
-int Camera_sphereInFrustum(const Camera *camera, Vec3 p, float r);
+b8 Camera_sphereInFrustum(const Camera *camera, Vec3 p, f32 r);
#endif
diff --git a/include/chunk.h b/include/chunk.h
@@ -4,6 +4,7 @@
#include "mesh.h"
#include "sdf.h"
#include "threadpool.h"
+#include "types.h"
#include <pthread.h>
@@ -21,14 +22,14 @@ typedef struct {
UpdateArgs *update_args;
pthread_mutex_t mesh_mutex;
- int mesh_update_count;
+ u32 mesh_update_count;
} Chunk;
void Chunk_init(Chunk *c);
void Chunk_free(Chunk *c);
void Chunk_updateOrigin(Chunk *c, IVec3 origin);
-void Chunk_updateMesh(Chunk *c, SDF f, float isolevel, ThreadPool *pool);
+void Chunk_updateMesh(Chunk *c, SDF f, f32 isolevel, ThreadPool *pool);
void Chunk_drawMesh(Chunk *c);
diff --git a/include/chunk_manager.h b/include/chunk_manager.h
@@ -4,27 +4,32 @@
#include "camera.h"
#include "chunk.h"
#include "threadpool.h"
+#include "types.h"
typedef struct {
IVec3 origin;
- int radius;
+ i32 radius;
SDF f;
- float isolevel;
+ f32 isolevel;
- unsigned int chunk_count;
+ u32 chunk_count;
IVec3 *offsets;
Chunk *chunks;
- bool *is_new_offset;
- bool *is_old_chunk;
+ b8 *is_new_offset;
+ b8 *is_old_chunk;
ThreadPool *pool;
} ChunkManager;
-ChunkManager ChunkManager_create(const Vec3 target, int radius, SDF f,
- float isolevel);
+ChunkManager ChunkManager_create(
+ const Vec3 target,
+ i32 radius,
+ SDF f,
+ f32 isolevel
+);
void ChunkManager_free(ChunkManager *cm);
diff --git a/include/input.h b/include/input.h
@@ -1,17 +1,19 @@
#ifndef INPUT_H
#define INPUT_H
+#include "types.h"
+
#include "glad/glad.h"
#include "GLFW/glfw3.h"
typedef struct {
- float mouse_x;
- float mouse_y;
- float rotation_dx;
- float rotation_dy;
- float move_dx;
- float move_dy;
- float move_dz;
+ f32 mouse_x;
+ f32 mouse_y;
+ f32 rotation_dx;
+ f32 rotation_dy;
+ f32 move_dx;
+ f32 move_dy;
+ f32 move_dz;
} UserInput;
void UserInput_init(UserInput *input, GLFWwindow *window);
diff --git a/include/marching_cubes.h b/include/marching_cubes.h
@@ -3,18 +3,23 @@
#include "mesh.h"
#include "sdf.h"
+#include "types.h"
-int MC_index(const Vec3 corners[8], SDF f, float isolevel);
+u32 MC_index(const Vec3 corners[8], SDF f, f32 isolevel);
-int MC_vertices(
+u32 MC_vertices(
const Vec3 corners[8],
SDF f,
- float isolevel,
- int mc_index,
+ f32 isolevel,
+ u32 mc_index,
Vertex vertices[3]
);
-int MC_indices(int mc_index, unsigned int vertex_offset,
- const unsigned int edge_offsets[12], unsigned int indices[15]);
+u32 MC_indices(
+ u32 mc_index,
+ u32 vertex_offset,
+ const u32 edge_offsets[12],
+ u32 indices[15]
+);
#endif
diff --git a/include/memory.h b/include/memory.h
@@ -1,6 +1,6 @@
#pragma once
-#include <stdlib.h>
+#include "types.h"
typedef enum memory_tag {
MEMORY_TAG_UNKNOWN,
@@ -10,9 +10,9 @@ typedef enum memory_tag {
MEMORY_TAG_MAX_TAGS
} memory_tag;
-void *s_alloc(size_t size, memory_tag tag);
+void *s_alloc(u64 size, memory_tag tag);
-void s_free(void *mem, size_t size, memory_tag tag);
+void s_free(void *mem, u64 size, memory_tag tag);
-void s_zeroMemory(void *mem, size_t size);
+void s_zeroMemory(void *mem, u64 size);
diff --git a/include/mesh.h b/include/mesh.h
@@ -1,6 +1,7 @@
#ifndef MESH_H
#define MESH_H
+#include "types.h"
#include "vec.h"
#include "glad/glad.h"
@@ -11,22 +12,22 @@ typedef struct {
} Vertex;
typedef struct {
- unsigned int vertex_capacity;
- unsigned int vertex_count;
+ u32 vertex_capacity;
+ u32 vertex_count;
Vertex *vertices;
- unsigned int index_capacity;
- unsigned int index_count;
- unsigned int *indices;
+ u32 index_capacity;
+ u32 index_count;
+ u32 *indices;
- unsigned int vao;
- unsigned int vbo;
- unsigned int ebo;
+ u32 vao;
+ u32 vbo;
+ u32 ebo;
- bool buffers_mapped;
+ b8 buffers_mapped;
} Mesh;
-void Mesh_init(Mesh *m, unsigned int vertex_count, unsigned int index_count);
+void Mesh_init(Mesh *m, u32 vertex_count, u32 index_count);
void Mesh_free(Mesh *m);
diff --git a/include/perlin.h b/include/perlin.h
@@ -1,6 +1,8 @@
#ifndef PERLIN_H
#define PERLIN_H
-float perlin(float x, float y, float z);
+#include "types.h"
+
+f32 perlin(f32 x, f32 y, f32 z);
#endif
diff --git a/include/player.h b/include/player.h
@@ -3,10 +3,11 @@
#include "input.h"
#include "transform.h"
+#include "types.h"
typedef struct {
- float speed;
- float sensitivity;
+ f32 speed;
+ f32 sensitivity;
Transform *transform;
} Player;
diff --git a/include/save.h b/include/save.h
@@ -2,8 +2,9 @@
#define SAVE_H
#include "transform.h"
+#include "types.h"
-int save(const Transform *t);
-int load(Transform *t);
+b8 save(const Transform *t);
+b8 load(Transform *t);
#endif
diff --git a/include/sdf.h b/include/sdf.h
@@ -1,13 +1,14 @@
#ifndef SDF_H
#define SDF_H
+#include "types.h"
#include "vec.h"
-typedef float (*SDF)(const Vec3 p);
+typedef f32 (*SDF)(const Vec3 p);
-float perlinSDF(const Vec3 p);
-float caveSDF(const Vec3 p);
-float terrainSDF(const Vec3 p);
-float sphereSDF(const Vec3 p);
+f32 perlinSDF(const Vec3 p);
+f32 caveSDF(const Vec3 p);
+f32 terrainSDF(const Vec3 p);
+f32 sphereSDF(const Vec3 p);
#endif
diff --git a/include/shader.h b/include/shader.h
@@ -1,6 +1,7 @@
#ifndef SHADER_H
#define SHADER_H
+#include "types.h"
#include "vec.h"
#include "glad/glad.h"
@@ -14,8 +15,8 @@ void Shader_use(Shader *shader);
void Shader_reload(Shader *shader);
-void Shader_setInt(const Shader *shader, const char *name, int i);
-void Shader_setFloat(const Shader *shader, const char *name, float f);
+void Shader_setInt(const Shader *shader, const char *name, i32 i);
+void Shader_setFloat(const Shader *shader, const char *name, f32 f);
void Shader_setVec3(const Shader *shader, const char *name, Vec3 v);
void Shader_setMat4(const Shader *shader, const char *name, Mat4 m);
diff --git a/include/transform.h b/include/transform.h
@@ -1,12 +1,13 @@
#ifndef TRANSFORM_H
#define TRANSFORM_H
+#include "types.h"
#include "vec.h"
typedef struct {
+ f32 yaw;
+ f32 pitch;
Vec3 position;
- float yaw;
- float pitch;
Vec3 forward;
Vec3 up;
Vec3 right;
@@ -14,11 +15,11 @@ typedef struct {
void Transform_init(
Transform *t,
- float x,
- float y,
- float z,
- float yaw,
- float pitch
+ f32 x,
+ f32 y,
+ f32 z,
+ f32 yaw,
+ f32 pitch
);
void Transform_updateVectors(Transform *t);
diff --git a/include/vec.h b/include/vec.h
@@ -1,17 +1,17 @@
#ifndef VEC_H
#define VEC_H
-#include <stdbool.h>
+#include "types.h"
-float radians(float degrees);
+f32 radians(f32 degrees);
-typedef int IVec3[3];
+typedef i32 IVec3[3];
-bool IVec3_equal(const IVec3 a, const IVec3 b);
+b8 IVec3_equal(const IVec3 a, const IVec3 b);
-typedef float Vec3[3];
+typedef f32 Vec3[3];
-void Vec3_set(float x, float y, float z, Vec3 dst);
+void Vec3_set(f32 x, f32 y, f32 z, Vec3 dst);
// dst = src * scale
void Vec3_scale(const Vec3 src, float scale, Vec3 dst);
@@ -29,15 +29,15 @@ void Vec3_mul(const Vec3 src_a, const Vec3 src_b, Vec3 dst);
void Vec3_normalize(const Vec3 src, Vec3 dst);
// src_a * src_b
-float Vec3_dot(const Vec3 src_a, const Vec3 src_b);
+f32 Vec3_dot(const Vec3 src_a, const Vec3 src_b);
// ||src||^2
-float Vec3_magSquared(const Vec3 src);
+f32 Vec3_magSquared(const Vec3 src);
// ||src||
-float Vec3_mag(const Vec3 src);
+f32 Vec3_mag(const Vec3 src);
-typedef float Mat4[4][4];
+typedef f32 Mat4[4][4];
// dst = src_a * src_b
void Mat4_mul(const Mat4 src_a, const Mat4 src_b, Mat4 dst);
@@ -46,6 +46,6 @@ void Mat4_mul(const Mat4 src_a, const Mat4 src_b, Mat4 dst);
void Mat4_lookAt(const Vec3 camera, const Vec3 target, const Vec3 up, Mat4 dst);
// Computes the perspective projection matrix.
-void Mat4_perspective(float fov, float aspect, float near, float far, Mat4 dst);
+void Mat4_perspective(f32 fov, f32 aspect, f32 near, f32 far, Mat4 dst);
#endif
diff --git a/src/app.c b/src/app.c
@@ -53,7 +53,7 @@ App *App_make(const AppInfo *app_info)
return NULL;
}
- int width, height;
+ i32 width, height;
glfwGetFramebufferSize(app->window, &width, &height);
glViewport(0, 0, width, height);
diff --git a/src/camera.c b/src/camera.c
@@ -213,24 +213,25 @@ void Camera_updateFrustum(Camera *camera)
}
-static float planeSDF(const Plane *p, const Vec3 r)
+static f32 planeSDF(const Plane *p, const Vec3 r)
{
return Vec3_dot(p->n, r) - Vec3_dot(p->n, p->p);
}
-int Camera_sphereInFrustum(const Camera *camera, Vec3 p, float r)
+b8 Camera_sphereInFrustum(const Camera *camera, Vec3 p, f32 r)
{
- for (int i = 0; i < 6; i++)
+ for (i32 i = 0; i < 6; i++)
{
- float distance = planeSDF(&camera->frustum[i], p);
+ f32 distance = planeSDF(&camera->frustum[i], p);
if (distance < -r)
{
- return 0;
+ return false;
}
else if (distance < r)
{
- return 1;
+ return true;
}
}
- return 1;
+ return true;
}
+
diff --git a/src/chunk.c b/src/chunk.c
@@ -9,7 +9,7 @@
struct UpdateArgs {
Chunk *chunk;
SDF f;
- float isolevel;
+ f32 isolevel;
};
static UpdateArgs *UpdateArgs_make(Chunk *c)
@@ -51,7 +51,7 @@ static void worldOrigin(const IVec3 chunk_origin, Vec3 world_origin)
world_origin[2] = chunk_origin[2] * CHUNK_WIDTH;
}
-static void cubeCorners(const Vec3 origin, float width, Vec3 corners[8])
+static void cubeCorners(const Vec3 origin, f32 width, Vec3 corners[8])
{
corners[0][0] = origin[0];
corners[0][1] = origin[1];
@@ -86,7 +86,7 @@ static void cubeCorners(const Vec3 origin, float width, Vec3 corners[8])
corners[7][2] = origin[2] + width;
}
-const static unsigned int EDGE_OFFSETS[] = {
+const static u32 EDGE_OFFSETS[] = {
0,
4,
3 * CHUNK_PWIDTH,
@@ -101,7 +101,7 @@ const static unsigned int EDGE_OFFSETS[] = {
3 * CHUNK_PWIDTH + 2
};
-static void Chunk_updateMeshData(Chunk *c, SDF f, float isolevel)
+static void Chunk_updateMeshData(Chunk *c, SDF f, f32 isolevel)
{
c->mesh.vertex_count = 0;
c->mesh.index_count = 0;
@@ -109,11 +109,11 @@ static void Chunk_updateMeshData(Chunk *c, SDF f, float isolevel)
Vec3 mesh_origin;
worldOrigin(c->origin, mesh_origin);
- for (int i = 0; i < CHUNK_PWIDTH; i++)
+ for (u32 i = 0; i < CHUNK_PWIDTH; i++)
{
- for (int j = 0; j < CHUNK_PWIDTH; j++)
+ for (u32 j = 0; j < CHUNK_PWIDTH; j++)
{
- for (int k = 0; k < CHUNK_PWIDTH; k++)
+ for (u32 k = 0; k < CHUNK_PWIDTH; k++)
{
Vec3 cell_origin = {
mesh_origin[0] + k,
@@ -124,7 +124,7 @@ static void Chunk_updateMeshData(Chunk *c, SDF f, float isolevel)
Vec3 cell_corners[8];
cubeCorners(cell_origin, 1, cell_corners);
- int mc_index = MC_index(cell_corners, f, isolevel);
+ u32 mc_index = MC_index(cell_corners, f, isolevel);
if (i < CHUNK_WIDTH && j < CHUNK_WIDTH && k < CHUNK_WIDTH)
{
@@ -153,7 +153,7 @@ static void Chunk_updateMeshFunc(void *arg)
pthread_mutex_unlock(&args->chunk->mesh_mutex);
}
-void Chunk_updateMesh(Chunk *c, SDF f, float isolevel, ThreadPool *pool)
+void Chunk_updateMesh(Chunk *c, SDF f, f32 isolevel, ThreadPool *pool)
{
c->update_args->chunk = c;
c->update_args->f = f;
diff --git a/src/chunk_manager.c b/src/chunk_manager.c
@@ -6,11 +6,11 @@
static void ChunkManager_setChunkCount(ChunkManager *cm)
{
cm->chunk_count = 0;
- for (int x = -cm->radius; x <= cm->radius; x++)
+ for (i32 x = -cm->radius; x <= cm->radius; x++)
{
- for (int y = -cm->radius; y <= cm->radius; y++)
+ for (i32 y = -cm->radius; y <= cm->radius; y++)
{
- for (int z = -cm->radius; z <= cm->radius; z++)
+ for (i32 z = -cm->radius; z <= cm->radius; z++)
{
cm->chunk_count += 1;
}
@@ -21,11 +21,11 @@ static void ChunkManager_setChunkCount(ChunkManager *cm)
static void ChunkManager_createOffsets(ChunkManager *cm)
{
cm->offsets = malloc(sizeof *cm->offsets * cm->chunk_count);
- for (int i = 0, x = -cm->radius; x <= cm->radius; x++)
+ for (i32 i = 0, x = -cm->radius; x <= cm->radius; x++)
{
- for (int y = -cm->radius; y <= cm->radius; y++)
+ for (i32 y = -cm->radius; y <= cm->radius; y++)
{
- for (int z = -cm->radius; z <= cm->radius; z++, i++)
+ for (i32 z = -cm->radius; z <= cm->radius; z++, i++)
{
cm->offsets[i][0] = x;
cm->offsets[i][1] = y;
@@ -38,7 +38,7 @@ static void ChunkManager_createOffsets(ChunkManager *cm)
static void ChunkManager_createChunks(ChunkManager *cm)
{
cm->chunks = malloc(sizeof *cm->chunks * cm->chunk_count);
- for (int i = 0; i < cm->chunk_count; i++)
+ for (u32 i = 0; i < cm->chunk_count; i++)
{
Chunk_init(&cm->chunks[i]);
@@ -54,7 +54,7 @@ static void ChunkManager_createChunks(ChunkManager *cm)
static void ChunkManager_resetFlags(ChunkManager *cm)
{
- for (int i = 0; i < cm->chunk_count; i++)
+ for (u32 i = 0; i < cm->chunk_count; i++)
{
cm->is_new_offset[i] = true;
cm->is_old_chunk[i] = true;
@@ -63,14 +63,14 @@ static void ChunkManager_resetFlags(ChunkManager *cm)
static void ChunkManager_updateFlags(ChunkManager *cm)
{
- for (int i = 0; i < cm->chunk_count; i++)
+ for (u32 i = 0; i < cm->chunk_count; i++)
{
IVec3 chunk_origin = {
cm->origin[0] + cm->offsets[i][0],
cm->origin[1] + cm->offsets[i][1],
cm->origin[2] + cm->offsets[i][2]
};
- for (int j = 0; j < cm->chunk_count; j++)
+ for (u32 j = 0; j < cm->chunk_count; j++)
{
if (IVec3_equal(chunk_origin, cm->chunks[j].origin))
{
@@ -84,11 +84,11 @@ static void ChunkManager_updateFlags(ChunkManager *cm)
static void ChunkManager_updateChunkMeshes(ChunkManager *cm)
{
- for (int i = 0; i < cm->chunk_count; i++)
+ for (u32 i = 0; i < cm->chunk_count; i++)
{
if (cm->is_new_offset[i])
{
- for (int j = 0; j < cm->chunk_count; j++)
+ for (u32 j = 0; j < cm->chunk_count; j++)
{
if (cm->is_old_chunk[j])
{
@@ -119,9 +119,9 @@ static void ChunkManager_updateChunks(ChunkManager *cm)
static void ChunkManager_worldToChunk(const Vec3 src, IVec3 dst)
{
- dst[0] = (int)floorf(src[0] / CHUNK_WIDTH);
- dst[1] = (int)floorf(src[1] / CHUNK_WIDTH);
- dst[2] = (int)floorf(src[2] / CHUNK_WIDTH);
+ dst[0] = (i32)floorf(src[0] / CHUNK_WIDTH);
+ dst[1] = (i32)floorf(src[1] / CHUNK_WIDTH);
+ dst[2] = (i32)floorf(src[2] / CHUNK_WIDTH);
}
static void ChunkManager_chunkToWorld(const IVec3 src, Vec3 dst)
@@ -131,8 +131,12 @@ static void ChunkManager_chunkToWorld(const IVec3 src, Vec3 dst)
dst[2] = src[2] * CHUNK_WIDTH;
}
-ChunkManager ChunkManager_create(const Vec3 target, int radius, SDF f,
- float isolevel)
+ChunkManager ChunkManager_create(
+ const Vec3 target,
+ i32 radius,
+ SDF f,
+ f32 isolevel
+)
{
ChunkManager cm;
@@ -160,7 +164,7 @@ void ChunkManager_free(ChunkManager *cm)
ThreadPool_free(cm->pool);
- for (int i = 0; i < cm->chunk_count; i++)
+ for (u32 i = 0; i < cm->chunk_count; i++)
{
Chunk_free(&cm->chunks[i]);
}
@@ -187,7 +191,7 @@ void ChunkManager_recenter(ChunkManager *cm, const Vec3 target)
void ChunkManager_drawChunks(const ChunkManager *cm, const Camera *camera)
{
- for (int i = 0; i < cm->chunk_count; i++)
+ for (u32 i = 0; i < cm->chunk_count; i++)
{
Vec3 chunk_center;
ChunkManager_chunkToWorld(cm->chunks[i].origin, chunk_center);
@@ -195,7 +199,7 @@ void ChunkManager_drawChunks(const ChunkManager *cm, const Camera *camera)
chunk_center[1] += CHUNK_WIDTH / 2;
chunk_center[2] += CHUNK_WIDTH / 2;
- float chunk_radius = CHUNK_WIDTH * 0.866025f;
+ f32 chunk_radius = CHUNK_WIDTH * 0.866025f;
if (Camera_sphereInFrustum(camera, chunk_center, chunk_radius))
{
diff --git a/src/input.c b/src/input.c
@@ -2,7 +2,7 @@
void UserInput_init(UserInput *input, GLFWwindow *window)
{
- double mx, my;
+ f64 mx, my;
glfwGetCursorPos(window, &mx, &my);
input->rotation_dx = 0.0f;
input->mouse_x = mx;
@@ -16,7 +16,7 @@ void UserInput_init(UserInput *input, GLFWwindow *window)
void UserInput_update(UserInput *input, GLFWwindow *window)
{
- double mx, my;
+ f64 mx, my;
glfwGetCursorPos(window, &mx, &my);
input->rotation_dx = mx - input->mouse_x;
input->mouse_x = mx;
diff --git a/src/logger.c b/src/logger.c
@@ -1,4 +1,5 @@
#include "logger.h"
+#include "types.h"
#include <stdarg.h>
#include <stdio.h>
@@ -21,7 +22,7 @@ void log_msg(log_level level, const char *message, ...)
"[FATAL]: ",
};
const char *prefix = level_strings[level];
- int prefix_length = strlen(prefix);
+ u32 prefix_length = strlen(prefix);
strncpy(log_msg, prefix, prefix_length);
// Append message
diff --git a/src/main.c b/src/main.c
@@ -8,6 +8,7 @@
#include "transform.h"
#include "player.h"
#include "logger.h"
+#include "types.h"
#include "glad/glad.h"
#include "GLFW/glfw3.h"
@@ -65,7 +66,7 @@ int main(int argc, char** argv)
Shader_use(shader);
Shader_setInt(shader, "shade_normals", 0);
- int loop_count = 0;
+ i32 loop_count = 0;
while (!glfwWindowShouldClose(app->window))
{
diff --git a/src/marching_cubes.c b/src/marching_cubes.c
@@ -2,7 +2,7 @@
#include <math.h>
-const static int EDGE_TABLE[256] = {
+const static u32 EDGE_TABLE[256] = {
0x0 , 0x109, 0x203, 0x30a, 0x406, 0x50f, 0x605, 0x70c, 0x80c, 0x905,
0xa0f, 0xb06, 0xc0a, 0xd03, 0xe09, 0xf00, 0x190, 0x99 , 0x393, 0x29a,
0x596, 0x49f, 0x795, 0x69c, 0x99c, 0x895, 0xb9f, 0xa96, 0xd9a, 0xc93,
@@ -31,7 +31,7 @@ const static int EDGE_TABLE[256] = {
0x50f, 0x406, 0x30a, 0x203, 0x109, 0x0
};
-const static int TRI_TABLE[256][16] = {
+const static i32 TRI_TABLE[256][16] = {
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ 0, 8, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ 0, 1, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
@@ -290,9 +290,9 @@ const static int TRI_TABLE[256][16] = {
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}
};
-int MC_index(const Vec3 corners[8], SDF f, float isolevel)
+u32 MC_index(const Vec3 corners[8], SDF f, f32 isolevel)
{
- int mc_index = 0;
+ u32 mc_index = 0;
if (f(corners[0]) < isolevel) mc_index |= 1;
if (f(corners[1]) < isolevel) mc_index |= 2;
if (f(corners[2]) < isolevel) mc_index |= 4;
@@ -304,11 +304,16 @@ int MC_index(const Vec3 corners[8], SDF f, float isolevel)
return mc_index;
}
-static void MC_interpolateVertexPosition(const Vec3 a, const Vec3 b, SDF f,
- float isolevel, Vec3 dst)
+static void MC_interpolateVertexPosition(
+ const Vec3 a,
+ const Vec3 b,
+ SDF f,
+ f32 isolevel,
+ Vec3 dst
+)
{
- float fa = f(a);
- float fb = f(b);
+ f32 fa = f(a);
+ f32 fb = f(b);
if (fabsf(isolevel - fa) < 0.0001f || fabsf(fa - fb) < 0.0001f)
{
dst[0] = a[0];
@@ -323,7 +328,7 @@ static void MC_interpolateVertexPosition(const Vec3 a, const Vec3 b, SDF f,
dst[2] = b[2];
return;
}
- float t = (isolevel - fa) / (fb - fa);
+ f32 t = (isolevel - fa) / (fb - fa);
dst[0] = a[0] + t * (b[0] - a[0]);
dst[1] = a[1] + t * (b[1] - a[1]);
dst[2] = a[2] + t * (b[2] - a[2]);
@@ -334,7 +339,7 @@ static void MC_interpolateVertexPosition(const Vec3 a, const Vec3 b, SDF f,
// These normals could be computed in vertex shader if it had access to the SDF
static void MC_normal(const Vec3 p, SDF f, Vec3 dst)
{
- const float h = 0.01f;
+ const f32 h = 0.01f;
Vec3 x1 = { h, -h, -h };
Vec3 x2 = { -h, -h, h };
@@ -359,8 +364,13 @@ static void MC_normal(const Vec3 p, SDF f, Vec3 dst)
Vec3_normalize(x1, dst);
}
-int MC_vertices(const Vec3 corners[8], SDF f, float isolevel, int mc_index,
- Vertex vertices[3])
+u32 MC_vertices(
+ const Vec3 corners[8],
+ SDF f,
+ f32 isolevel,
+ u32 mc_index,
+ Vertex vertices[3]
+)
{
if (EDGE_TABLE[mc_index] & 1)
{
@@ -398,13 +408,16 @@ int MC_vertices(const Vec3 corners[8], SDF f, float isolevel, int mc_index,
return 3;
}
-int MC_indices(int mc_index, unsigned int vertex_offset,
- const unsigned int edge_offsets[12], unsigned int indices[15])
+u32 MC_indices(
+ u32 mc_index,
+ u32 vertex_offset,
+ const u32 edge_offsets[12],
+ u32 indices[15])
{
- int index_count = 0;
+ u32 index_count = 0;
while (TRI_TABLE[mc_index][index_count] != -1)
{
- int edge_id = TRI_TABLE[mc_index][index_count];
+ u32 edge_id = TRI_TABLE[mc_index][index_count];
indices[index_count] = vertex_offset + edge_offsets[edge_id];
index_count += 1;
}
diff --git a/src/memory.c b/src/memory.c
@@ -1,16 +1,17 @@
#include "memory.h"
#include "logger.h"
+#include <stdlib.h>
#include <string.h>
struct MemoryStats {
- size_t allocated;
- size_t allocated_tagged[MEMORY_TAG_MAX_TAGS];
+ u64 allocated;
+ u64 allocated_tagged[MEMORY_TAG_MAX_TAGS];
};
static struct MemoryStats stats;
-void *s_alloc(size_t size, memory_tag tag)
+void *s_alloc(u64 size, memory_tag tag)
{
if (tag == MEMORY_TAG_UNKNOWN)
{
@@ -22,13 +23,12 @@ void *s_alloc(size_t size, memory_tag tag)
void *mem = malloc(size);
-
s_zeroMemory(mem, size);
return mem;
}
-void s_free(void *mem, size_t size, memory_tag tag)
+void s_free(void *mem, u64 size, memory_tag tag)
{
if (tag == MEMORY_TAG_UNKNOWN)
{
@@ -41,7 +41,7 @@ void s_free(void *mem, size_t size, memory_tag tag)
free(mem);
}
-void s_zeroMemory(void *mem, size_t size)
+void s_zeroMemory(void *mem, u64 size)
{
memset(mem, 0, size);
}
diff --git a/src/mesh.c b/src/mesh.c
@@ -4,7 +4,7 @@
#include <stdlib.h>
-void Mesh_init(Mesh *m, unsigned int vertex_count, unsigned int index_count)
+void Mesh_init(Mesh *m, u32 vertex_count, u32 index_count)
{
m->vertex_capacity = vertex_count;
m->vertex_count = 0;
diff --git a/src/perlin.c b/src/perlin.c
@@ -2,7 +2,7 @@
#include <math.h>
-const static int p[512] = {
+const static u16 p[512] = {
151, 160, 137, 91, 90, 15, 131, 13, 201, 95, 96, 53, 194, 233, 7,
225, 140, 36, 103, 30, 69, 142, 8, 99, 37, 240, 21, 10, 23, 190,
6, 148, 247, 120, 234, 75, 0, 26, 197, 62, 94, 252, 219, 203, 117,
@@ -41,17 +41,17 @@ const static int p[512] = {
180
};
-static float fade(float t)
+static f32 fade(f32 t)
{
return t * t * t * (t * (t * 6 - 15) + 10);
}
-static float lerp(float t, float a, float b)
+static f32 lerp(f32 t, f32 a, f32 b)
{
return a + t * (b - a);
}
-const static float g[16][3] = {
+const static f32 g[16][3] = {
{ 1, 1, 0 },
{ -1, 1, 0 },
{ 1, -1, 0 },
@@ -70,34 +70,34 @@ const static float g[16][3] = {
{ 0, -1, -1 }
};
-static float grad(int hash, float x, float y, float z)
+static f32 grad(u16 hash, f32 x, f32 y, f32 z)
{
- int h = hash & 15;
+ u16 h = hash & 15;
return g[h][0] * x + g[h][1] * y + g[h][2] * z;
}
-float perlin(float x, float y, float z)
+f32 perlin(f32 x, f32 y, f32 z)
{
- int X = (int)floorf(x) & 255;
- int Y = (int)floorf(y) & 255;
- int Z = (int)floorf(z) & 255;
+ i32 X = (i32)floorf(x) & 255;
+ i32 Y = (i32)floorf(y) & 255;
+ i32 Z = (i32)floorf(z) & 255;
x -= floorf(x);
y -= floorf(y);
z -= floorf(z);
- float u = fade(x);
- float v = fade(y);
- float w = fade(z);
+ f32 u = fade(x);
+ f32 v = fade(y);
+ f32 w = fade(z);
- int a = p[p[p[X ] + Y ] + Z];
- int b = p[p[p[X + 1] + Y ] + Z];
- int c = p[p[p[X ] + Y + 1] + Z];
- int d = p[p[p[X + 1] + Y + 1] + Z];
- int e = p[p[p[X ] + Y ] + Z + 1];
- int f = p[p[p[X + 1] + Y ] + Z + 1];
- int g = p[p[p[X ] + Y + 1] + Z + 1];
- int h = p[p[p[X + 1] + Y + 1] + Z + 1];
+ i32 a = p[p[p[X ] + Y ] + Z];
+ i32 b = p[p[p[X + 1] + Y ] + Z];
+ i32 c = p[p[p[X ] + Y + 1] + Z];
+ i32 d = p[p[p[X + 1] + Y + 1] + Z];
+ i32 e = p[p[p[X ] + Y ] + Z + 1];
+ i32 f = p[p[p[X + 1] + Y ] + Z + 1];
+ i32 g = p[p[p[X ] + Y + 1] + Z + 1];
+ i32 h = p[p[p[X + 1] + Y + 1] + Z + 1];
return lerp(w, lerp(v, lerp(u, grad(a, x , y , z ),
grad(b, x - 1, y , z )),
diff --git a/src/player.c b/src/player.c
@@ -1,6 +1,6 @@
#include "player.h"
-static void Player_updateRotation(Player *player, float dx, float dy)
+static void Player_updateRotation(Player *player, f32 dx, f32 dy)
{
player->transform->yaw += dx * player->sensitivity;
@@ -15,7 +15,7 @@ static void Player_updateRotation(Player *player, float dx, float dy)
}
}
-static void Player_updatePosition(Player *player, float dx, float dy, float dz)
+static void Player_updatePosition(Player *player, f32 dx, f32 dy, f32 dz)
{
Vec3 movement_x;
Vec3_scale(player->transform->right, dx, movement_x);
@@ -56,3 +56,4 @@ void Player_move(Player *player, UserInput *input)
Transform_updateVectors(player->transform);
}
+
diff --git a/src/save.c b/src/save.c
@@ -2,23 +2,23 @@
#include <stdio.h>
-int save(const Transform *t)
+b8 save(const Transform *t)
{
FILE *fp;
fp = fopen("savedata", "w");
- if (!fp) return -1;
+ if (!fp) return false;
fwrite(t, 1, sizeof(Transform), fp);
fclose(fp);
- return 0;
+ return true;
}
-int load(Transform *t)
+b8 load(Transform *t)
{
FILE *fp;
fp = fopen("savedata", "r");
- if (!fp) return -1;
+ if (!fp) return false;
fread(t, sizeof(Transform), 1, fp);
fclose(fp);
- return 0;
+ return true;
}
diff --git a/src/sdf.c b/src/sdf.c
@@ -3,31 +3,31 @@
#include <math.h>
-float perlinSDF(const Vec3 p)
+f32 perlinSDF(const Vec3 p)
{
return perlin(p[0] * 0.01f, p[1] * 0.01f, p[2] * 0.01f)
+ 0.5f * perlin(p[0] * 0.05f, p[1] * 0.05f, p[2] * 0.05f)
+ 0.1f * perlin(p[0] * 0.1f, p[1] * 0.1f, p[2] * 0.1f);
}
-float caveSDF(const Vec3 p)
+f32 caveSDF(const Vec3 p)
{
- float o1 = perlin(p[0] * 0.01f, p[1] * 0.01f, p[2] * 0.01f);
+ f32 o1 = perlin(p[0] * 0.01f, p[1] * 0.01f, p[2] * 0.01f);
return o1;
}
-float terrainSDF(const Vec3 p)
+f32 terrainSDF(const Vec3 p)
{
- float o1 = 50.0f * perlin(p[0] * 0.005f, 0.0f, p[2] * 0.005f);
- float o2 = 2.0f * perlin(p[0] * 0.050f, 0.0f, p[2] * 0.050f);
- float o3 = 1.0f * perlin(p[0] * 0.100f, 0.0f, p[2] * 0.100f);
+ f32 o1 = 50.0f * perlin(p[0] * 0.005f, 0.0f, p[2] * 0.005f);
+ f32 o2 = 2.0f * perlin(p[0] * 0.050f, 0.0f, p[2] * 0.050f);
+ f32 o3 = 1.0f * perlin(p[0] * 0.100f, 0.0f, p[2] * 0.100f);
- float surface = p[1] - o1 - o2 - o3;
- float cave = perlinSDF(p);
+ f32 surface = p[1] - o1 - o2 - o3;
+ f32 cave = perlinSDF(p);
- float k = 32.0f;
- float h = 0.5f - 0.5f * (cave - surface) / k;
+ f32 k = 32.0f;
+ f32 h = 0.5f - 0.5f * (cave - surface) / k;
if (h < 0.0f)
{
h = 0.0f;
@@ -37,13 +37,13 @@ float terrainSDF(const Vec3 p)
h = 1.0f;
}
- float a = cave * (1.0f - h) + surface * h;
- float b = k * h * (1.0f - h);
+ f32 a = cave * (1.0f - h) + surface * h;
+ f32 b = k * h * (1.0f - h);
return a + b;
}
-float sphereSDF(const float p[3])
+f32 sphereSDF(const Vec3 p)
{
return sqrtf(p[0] * p[0] + p[1] * p[1] + p[2] * p[2]) - 25.0f;
}
diff --git a/src/shader.c b/src/shader.c
@@ -152,13 +152,13 @@ void Shader_reload(Shader* shader)
}
}
-void Shader_setInt(const Shader *shader, const char *name, int i)
+void Shader_setInt(const Shader *shader, const char *name, i32 i)
{
GLint uniform_loc = glGetUniformLocation(shader->program, name);
glUniform1i(uniform_loc, i);
}
-void Shader_setFloat(const Shader *shader, const char *name, float f)
+void Shader_setFloat(const Shader *shader, const char *name, f32 f)
{
GLint uniform_loc = glGetUniformLocation(shader->program, name);
glUniform1f(uniform_loc, f);
diff --git a/src/transform.c b/src/transform.c
@@ -6,11 +6,11 @@ const Vec3 WORLD_UP = { 0, 1, 0 };
void Transform_init(
Transform *t,
- float x,
- float y,
- float z,
- float yaw,
- float pitch
+ f32 x,
+ f32 y,
+ f32 z,
+ f32 yaw,
+ f32 pitch
)
{
t->yaw = yaw;
diff --git a/src/vec.c b/src/vec.c
@@ -2,24 +2,24 @@
#include <math.h>
-float radians(float degrees)
+f32 radians(f32 degrees)
{
return degrees * (M_PI / 180.0f);
}
-bool IVec3_equal(const IVec3 a, const IVec3 b)
+b8 IVec3_equal(const IVec3 a, const IVec3 b)
{
return (a[0] == b[0]) & (a[1] == b[1]) & (a[2] == b[2]);
}
-void Vec3_set(float x, float y, float z, Vec3 dst)
+void Vec3_set(f32 x, f32 y, f32 z, Vec3 dst)
{
dst[0] = x;
dst[1] = y;
dst[2] = z;
}
-void Vec3_scale(const Vec3 src, float scale, Vec3 dst)
+void Vec3_scale(const Vec3 src, f32 scale, Vec3 dst)
{
dst[0] = src[0] * scale;
dst[1] = src[1] * scale;
@@ -49,7 +49,7 @@ void Vec3_mul(const Vec3 src_a, const Vec3 src_b, Vec3 dst)
void Vec3_normalize(const Vec3 src, Vec3 dst)
{
- float m = Vec3_mag(src);
+ f32 m = Vec3_mag(src);
if (m > 0.0f)
{
dst[0] = src[0] / m;
@@ -58,17 +58,17 @@ void Vec3_normalize(const Vec3 src, Vec3 dst)
}
}
-float Vec3_dot(const Vec3 src_a, const Vec3 src_b)
+f32 Vec3_dot(const Vec3 src_a, const Vec3 src_b)
{
return src_a[0] * src_b[0] + src_a[1] * src_b[1] + src_a[2] * src_b[2];
}
-float Vec3_magSquared(const Vec3 src)
+f32 Vec3_magSquared(const Vec3 src)
{
return Vec3_dot(src, src);
}
-float Vec3_mag(const Vec3 src)
+f32 Vec3_mag(const Vec3 src)
{
return sqrtf(Vec3_magSquared(src));
}
@@ -139,9 +139,9 @@ void Mat4_lookAt(const Vec3 camera, const Vec3 target, const Vec3 up, Mat4 dst)
dst[3][3] = 1.0f;
}
-void Mat4_perspective(float fov, float aspect, float near, float far, Mat4 dst)
+void Mat4_perspective(f32 fov, f32 aspect, f32 near, f32 far, Mat4 dst)
{
- float tan_half_fov = tanf(fov * 0.5f);
+ f32 tan_half_fov = tanf(fov * 0.5f);
// x basis
dst[0][0] = 1.0f / (aspect * tan_half_fov);
@@ -167,3 +167,4 @@ void Mat4_perspective(float fov, float aspect, float near, float far, Mat4 dst)
dst[3][2] = -(2 * far * near) / (far - near);
dst[3][3] = 0.0f;
}
+