Fantasizing about C API and plugins

This commit is contained in:
2026-01-16 20:28:08 +01:00
parent 133994d2ef
commit 33a0b241ef
5 changed files with 232 additions and 9 deletions

138
include/api.h Normal file
View File

@@ -0,0 +1,138 @@
#ifndef _API_H
#define _API_H
#include <stdafx.h>
#include <vecmath.h>
typedef int Error;
#define ERROR_NONE 0
#define ERROR_OUT_OF_MEMORY 1
#define ERROR_OUT_OF_BLOCKS 2
#define ERROR_OUT_OF_MATERIALS 3
#define ERROR_OUT_OF_TEXTURES 4
typedef int Orientation;
#define ORIENTATION_NEGATIVE_X ~0
#define ORIENTATION_POSITIVE_X 0
#define ORIENTATION_NEGATIVE_Y ~1
#define ORIENTATION_POSITIVE_Y 1
#define ORIENTATION_NEGATIVE_Z ~2
#define ORIENTATION_POSITIVE_Z 2
#define ORIENTATION_INIT_SIGN_INDEX(sign, index) ((sign) > 0 ? (index) : ~(index))
#define ORIENTATION_GET_INDEX(orientation) ((orientation) & 0x4 ? ~(orientation) : (orientation))
#define ORIENTATION_GET_SIGN(orientation) ((oritentation) & 0x4 ? -1 : 1)
typedef int Transform;
#define TRANSFORM_IDENTITY 0
#define TRANSFORM_ROTATE_CW 1
#define TRANSFORM_ROTATE_CCW 2
#define TRANSFORM_ROTATE_180 3
#define TRANSFORM_MIRROR 4
#define TRANSFORM_MIRROR_ROTATE_CW 5
#define TRANSFORM_MIRROR_ROTATE_CCW 6
#define TRANSFORM_MIRROR_ROTATE_180 7
typedef unsigned short BlockId;
#define BLOCK_ID_AIR 0
typedef unsigned short MaterialId;
#define MATERIAL_ID_EMPTY 0
typedef unsigned short TextureId;
#define TEXTURE_ID_EMPTY_BASE_COLOR 0
#define TEXTURE_ID_EMPTY_EMISSIVE 1
#define TEXTURE_ID_EMPTY_NORMAL 2
#define TEXTURE_ID_EMPTY_OCCLUSION_ROUGHNESS_METALLIC 3
typedef int TextureUsage;
#define TEXTURE_USAGE_BASE_COLOR 0
#define TEXTURE_USAGE_NORMAL 1
#define TEXTURE_USAGE_OCCLUSION_ROUGHNESS_METALLIC 2
#define TEXTURE_USAGE_EMISSIVE 3
typedef struct String {
const char* ptr;
size_t len;
} String;
#define STRING_NULL (String){ .ptr = NULL, .len = 0 }
typedef struct BlockDefinitionWall {
MaterialId material;
Transform transform;
} BlockDefinitionWall;
typedef struct BlockDefinitionWalls {
BlockDefinitionWall negative_x;
BlockDefinitionWall positive_x;
BlockDefinitionWall negative_y;
BlockDefinitionWall positive_y;
BlockDefinitionWall negative_z;
BlockDefinitionWall positive_z;
} BlockDefinitionWalls;
typedef struct BlockDefinition {
BlockDefinitionWalls walls;
} BlockDefinition;
typedef struct MaterialDefinition {
vec3_t base_color;
vec3_t emissive;
float ior;
float metallic;
float normal_scale;
float occlusion_texture_strength;
float roughness;
TextureId base_color_texture;
TextureId emissive_texture;
TextureId normal_texture;
TextureId occlusion_roughness_metallic_texture;
} MaterialDefinition;
typedef struct TextureData {
TextureUsage usage;
unsigned int width;
unsigned int height;
const char *data;
} TextureData;
typedef struct TextureDataMutable {
TextureUsage usage;
unsigned int width;
unsigned int height;
char *data;
} TextureDataMutable;
Error BlockIdFromName(String name, bool *exists, BlockId *out);
Error BlockDefinitionFromId(BlockId id, const BlockDefinition **out);
Error BlockDefine(String name, const BlockDefinition *definition, BlockId *out);
Error MaterialIdFromName(String name, bool *exists, MaterialId *out);
Error MaterialDefinitionFromId(MaterialId, const MaterialDefinition **out);
Error MaterialDefine(String name, const MaterialDefinition *definition, MaterialId *out);
Error TextureIdFromName(String name, TextureUsage usage, bool *exists, TextureId *out);
Error TextureDefine(String name, const TextureData *texture_data, TextureId *out);
Error TextureResize(TextureDataMutable *texture_data, unsigned int width, unsigned int height);
// --- CALLBACKS ---
void PreInit();
void PostInit();
void Deinit();
void FilterBlock(BlockId id, String name, bool *exists, BlockDefinition *definition);
void FilterMaterial(MaterialId id, String name, bool *exists, MaterialDefinition *definition);
void FilterTexture(TextureId id, String name, bool *exists, TextureDataMutable *texture_data);
#endif

27
include/stdafx.h Normal file
View File

@@ -0,0 +1,27 @@
#ifndef _STDAFX_H
#define _STDAFX_H
typedef unsigned long size_t;
typedef long ssize_t;
typedef long ptrdiff_t;
typedef long intptr_t;
typedef unsigned long uintptr_t;
#define NULL ((void *)0)
#define bool _Bool
#define true 1
#define false 0
#define __bool_true_false_are_defined 1
typedef signed char int8_t;
typedef short int16_t;
typedef int int32_t;
typedef long int64_t;
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long uint64_t;
#endif

58
include/vecmath.h Normal file
View File

@@ -0,0 +1,58 @@
#ifndef _VECMATH_H
#define _VECMATH_H
typedef union vec2_t {
struct {
float x;
float y;
};
float v[2];
} vec2_t;
typedef union vec3_t {
struct {
float x;
float y;
float z;
};
float v[3];
} vec3_t;
typedef union vec4_t {
struct {
float x;
float y;
float z;
float w;
};
float v[4];
} vec4_t;
typedef union ivec2_t {
struct {
int x;
int y;
};
int v[2];
} vec2_t;
typedef union ivec3_t {
struct {
int x;
int y;
int z;
};
int v[3];
} vec3_t;
typedef union ivec4_t {
struct {
int x;
int y;
int z;
int w;
};
int v[4];
} vec4_t;
#endif

View File

@@ -141,7 +141,7 @@ pub fn deinit(self: *Textures, engine: *Engine, allocator: std.mem.Allocator) vo
/// Get the ID of a texture given its filename (as a string) and usage. Returns
/// `null` if such texture hasn't been loaded. When the filename is `null`,
/// returns an empty texture ID appropriate for given usage.
pub fn get(self: *const Textures, maybe_filename: []const u8, usage: Texture.Usage) ?Id {
pub fn get(self: *const Textures, maybe_filename: ?[]const u8, usage: Texture.Usage) ?Id {
if (maybe_filename) |filename| {
return self.map.get(.{
// If the atom doesn't exist, then the texture cannot possibly exist.

View File

@@ -51,14 +51,14 @@ pub const Orientation = enum(u3) {
};
pub const Transform = enum(u3) {
identity,
rotate_cw,
rotate_ccw,
rotate_180,
mirror,
mirror_rotate_cw,
mirror_rotate_ccw,
mirror_rotate_180,
identity = 0,
rotate_cw = 1,
rotate_ccw = 2,
rotate_180 = 3,
mirror = 4,
mirror_rotate_cw = 5,
mirror_rotate_ccw = 6,
mirror_rotate_180 = 7,
};
// ┌──────────────────── x