Added Flecs, license comments and created the project outline.

This commit is contained in:
Lorenzo Torres 2025-11-01 17:32:01 +01:00
parent d716b6cea7
commit 7be714b81f
16 changed files with 127682 additions and 5 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 151 B

View file

@ -1,6 +1,8 @@
# SPDX-License-Identifier: BSD-3-Clause
CC := cc
CFLAGS := -Wall -Wextra -std=c99 -pedantic -ggdb -O2
LIBS :=
LIBS := -lm
# if this is set to gl, it will use OpenGL, otherwise it will use Vulkan
BACKEND := gl

91278
flecs.c Normal file

File diff suppressed because it is too large Load diff

36107
flecs.h Normal file

File diff suppressed because it is too large Load diff

View file

@ -1,6 +1,5 @@
/**
* SPDX-License-Identifier: (WTFPL OR CC0-1.0) AND Apache-2.0
*/
// SPDX-License-Identifier: (WTFPL OR CC0-1.0) AND Apache-2.0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

View file

@ -1,3 +1,5 @@
// SPDX-License-Identifier: BSD-3-Clause
#include "gl.h"
#define RGFW_IMPLEMENTATION
#define RGFW_OPENGL

179
linear.c Normal file
View file

@ -0,0 +1,179 @@
// SPDX-License-Identifier: BSD-3-Clause
#include "linear.h"
float vec2_dot(vec2 a, vec2 b)
{
float result = 0.0f;
for (int i=0; i<2; i++) {
result += a[i] * b[i];
}
return result;
}
float vec3_dot(vec3 a, vec3 b)
{
float result = 0.0f;
for (int i=0; i<3; i++) {
result += a[i] * b[i];
}
return result;
}
float vec4_dot(vec4 a, vec4 b)
{
float result = 0.0f;
for (int i=0; i<4; i++) {
result += a[i] * b[i];
}
return result;
}
void vec3_cross(vec3 dest, vec3 a, vec3 b)
{
vec3 res = {0};
res[0] = a[1] * b[2] - a[2] * b[1];
res[1] = a[2] * b[0] - a[0] * b[2];
res[2] = a[0] * b[1] - a[1] * b[0];
memcpy(dest, res, sizeof(vec3));
}
void mat4_perspective(mat4 dest, float fov, float aspect, float near, float far)
{
mat4 perspective = {
{ 1.0f/(aspect*tan(fov/2.0f)), 0.0f, 0.0f, 0.0f },
{ 0.0f, 1.0f/tan(fov/2.0f), 0.0f, 0.0f },
{ 0.0f, 0.0f, -((far+near)/(far-near)), -((2*far*near)/(far-near)) },
{ 0.0f, 0.0f, -1.0f, 0.0f }
};
memcpy(dest, perspective, sizeof(mat4));
}
void mat4_lookat(mat4 dest, vec3 eye, vec3 target, vec3 up)
{
vec3 target_cpy = {0};
memcpy(target_cpy, target, sizeof(vec3));
vec3_sub(target_cpy, target_cpy, eye);
vec3 zaxis = {0};
vec3_normalize(zaxis, target_cpy);
vec3 zaxis_cpy = {0};
memcpy(zaxis_cpy, zaxis, sizeof(vec3));
vec3_cross(zaxis_cpy, zaxis, up);
vec3 xaxis = {0};
vec3_normalize(xaxis, zaxis_cpy);
vec3 yaxis = {0};
vec3_cross(yaxis, xaxis, zaxis);
zaxis[0] = -zaxis[0];
zaxis[1] = -zaxis[1];
zaxis[2] = -zaxis[2];
mat4 view = {
{ xaxis[0], xaxis[1], xaxis[2], -vec3_dot(xaxis, eye) },
{ yaxis[0], yaxis[1], yaxis[2], -vec3_dot(yaxis, eye) },
{ zaxis[0], zaxis[1], zaxis[2], -vec3_dot(zaxis, eye) },
{ 0.0f, 0.0f, 0.0f, 1.0f }
};
memcpy(dest, view, sizeof(mat4));
}
void vec2_sub(vec2 dest, vec2 a, vec2 b)
{
vec2 res = {0};
for (int i=0; i<2; i++) res[i] = a[i] - b[i];
memcpy(dest, res, sizeof(vec2));
}
void vec3_sub(vec3 dest, vec3 a, vec3 b)
{
vec3 res = {0};
for (int i=0; i<3; i++) res[i] = a[i] - b[i];
memcpy(dest, res, sizeof(vec3));
}
void vec4_sub(vec4 dest, vec4 a, vec4 b)
{
vec4 res = {0};
for (int i=0; i<4; i++) res[i] = a[i] - b[i];
memcpy(dest, res, sizeof(vec4));
}
void vec2_add(vec2 dest, vec2 a, vec2 b)
{
vec2 res = {0};
for (int i=0; i<2; i++) res[i] = a[i] + b[i];
memcpy(dest, res, sizeof(vec2));
}
void vec3_add(vec3 dest, vec3 a, vec3 b)
{
vec3 res = {0};
for (int i=0; i<3; i++) res[i] = a[i] + b[i];
memcpy(dest, res, sizeof(vec3));
}
void vec4_add(vec4 dest, vec4 a, vec4 b)
{
vec4 res = {0};
for (int i=0; i<4; i++) res[i] = a[i] + b[i];
memcpy(dest, res, sizeof(vec4));
}
void vec2_normalize(vec2 dest, vec2 a)
{
vec2 res = {0};
memcpy(res, a, sizeof(vec2));
float w = sqrt(a[0] * a[0] + a[1] * a[1]);
res[0] /= w;
res[1] /= w;
memcpy(dest, res, sizeof(vec2));
}
void vec3_normalize(vec3 dest, vec3 a)
{
vec3 res = {0};
memcpy(res, a, sizeof(vec3));
float w = sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2]);
res[0] /= w;
res[1] /= w;
res[2] /= w;
memcpy(dest, res, sizeof(vec3));
}
void vec4_normalize(vec4 dest, vec4 a)
{
vec4 res = {0};
memcpy(res, a, sizeof(vec4));
float w = sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2]);
res[0] /= w;
res[1] /= w;
res[2] /= w;
res[3] /= w;
memcpy(dest, res, sizeof(vec4));
}
void vec2_scale(vec2 dest, vec2 a, float scale)
{
memcpy(dest, a, sizeof(vec2));
for (int i=0; i<2; i++) dest[i] *= scale;
}
void vec3_scale(vec3 dest, vec3 a, float scale)
{
memcpy(dest, a, sizeof(vec3));
for (int i=0; i<3; i++) dest[i] *= scale;
}
void vec4_scale(vec4 dest, vec4 a, float scale)
{
memcpy(dest, a, sizeof(vec4));
for (int i=0; i<4; i++) dest[i] *= scale;
}

47
linear.h Normal file
View file

@ -0,0 +1,47 @@
// SPDX-License-Identifier: BSD-3-Clause
#ifndef LINEAR_H
#define LINEAR_H
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "types.h"
#define PI 3.14159265358979323846
#define RAD(deg) deg * (PI / 180.0f)
#define DEG(rad) rad * (180.0f / PI)
typedef f32 vec2[2];
typedef f32 vec3[3];
typedef f32 vec4[4];
typedef f32 mat2[2][2];
typedef f32 mat3[3][3];
typedef f32 mat4[4][4];
f32 vec2_dot(vec2 a, vec2 b);
f32 vec3_dot(vec3 a, vec3 b);
f32 vec4_dot(vec4 a, vec4 b);
void vec2_sub(vec2 dest, vec2 a, vec2 b);
void vec3_sub(vec3 dest, vec3 a, vec3 b);
void vec4_sub(vec4 dest, vec4 a, vec4 b);
void vec2_add(vec2 dest, vec2 a, vec2 b);
void vec3_add(vec3 dest, vec3 a, vec3 b);
void vec4_add(vec4 dest, vec4 a, vec4 b);
void vec3_cross(vec3 dest, vec3 a, vec3 b);
void vec2_normalize(vec2 dest, vec2 a);
void vec3_normalize(vec3 dest, vec3 a);
void vec4_normalize(vec4 dest, vec4 a);
void vec2_scale(vec2 dest, vec2 a, f32 scale);
void vec3_scale(vec3 dest, vec3 a, f32 scale);
void vec4_scale(vec4 dest, vec4 a, f32 scale);
void mat4_perspective(mat4 dest, f32 fov, f32 aspect, f32 near, f32 far);
void mat4_lookat(mat4 dest, vec3 eye, vec3 target, vec3 up);
#endif

View file

@ -1,6 +1,8 @@
# SPDX-License-Identifier: BSD-3-Clause
include config.mk
SRC:=topaz.c
SRC:=topaz.c linear.c flecs.c
ifeq (${BACKEND},gl)
SRC += gl/gl.c gl/platform.c

View file

@ -1,3 +1,5 @@
// SPDX-License-Identifier: BSD-3-Clause
#ifndef PLATFORM_H
#define PLATFORM_H

16
rendering/renderer.h Normal file
View file

@ -0,0 +1,16 @@
// SPDX-License-Identifier: BSD-3-Clause
#ifndef RENDERER_H
#define RENDERER_H
#include "../types.h"
struct mesh {
usize index;
usize size;
};
struct mesh renderer_build_chunk_mesh();
void renderer_draw_mesh(struct mesh);
void renderer_draw_chunk(struct mesh);
#endif

View file

@ -1,3 +1,5 @@
// SPDX-License-Identifier: BSD-3-Clause
#include <stdio.h>
#include "platform.h"

View file

@ -1,3 +1,5 @@
// SPDX-License-Identifier: BSD-3-Clause
#ifndef TYPES_H
#define TYPES_H
@ -16,4 +18,7 @@ typedef int64_t i64;
typedef size_t usize;
typedef float f32;
typedef double f64;
#endif

View file

@ -1,3 +1,5 @@
// SPDX-License-Identifier: BSD-3-Clause
#ifndef UTILS_H
#define UTILS_H

13
world/block.h Normal file
View file

@ -0,0 +1,13 @@
#ifdef BLOCK_H
#define BLOCK_H
#include "../types.h"
typedef u16 block_id;
#define BLOCK_AIR_ID 0
#define BLOCK_STONE_ID 1
#define BLOCK_DIRT_ID 1
#define BLOCK_GRASS_ID 1
#endif

21
world/chunk.h Normal file
View file

@ -0,0 +1,21 @@
#ifndef CHUNK_H
#define CHUNK_H
#include "block.h"
#define CHUNK_SIZE 16
#define CHUNK_INDEX(x, y, z) (CHUNK_SIZE * CHUNK_SIZE * (x) + CHUNK_SIZE * (y) + (z))
typedef usize chunk_position[2];
/*
* Chunk are a group of blocks with
* size 16x16x16. The world is composed
* of infinite chunks in each axis.
*/
struct chunk {
block_id blocks[CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE];
chunk_position position;
};
#endif