polyadvent

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

commit 6e3f26b3f451dc4623d8e9d9b137f427a1b6f3e3
parent af347d213fc2deb9f7de4c660f26ad043de3b881
Author: William Casarin <jb55@jb55.com>
Date:   Sun, 26 Sep 2021 22:39:01 -0700

data_id -> lens

Diffstat:
Dsrc/data_id.h | 92-------------------------------------------------------------------------------
Asrc/lens.h | 59+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/shader.c | 2+-
Msrc/shader.h | 2+-
Msrc/ui.c | 2+-
Mtest/test_data_id.c | 2+-
6 files changed, 63 insertions(+), 96 deletions(-)

diff --git a/src/data_id.h b/src/data_id.h @@ -1,92 +0,0 @@ -#pragma once - -#include "resource.h" -#include <stddef.h> -#include <assert.h> -#include <string.h> - -enum data_id_type { - DATA_ID_RESOURCE = 1, - DATA_ID_RAWPTR, -}; - -enum data_id_datatype { - DATA_ID_FLOAT = 1, - DATA_ID_INT, - DATA_ID_VEC2, - DATA_ID_VEC3, - DATA_ID_VEC3P, - DATA_ID_MAT3, - DATA_ID_MAT4, - DATA_ID_MAT4P, -}; - -struct lens { - const char *name; - int offset; - enum data_id_datatype type; -}; - -static inline int data_id_get_ptr_data(void *ptr, enum data_id_datatype typ, void *out) -{ - assert(out); - assert(ptr); - switch (typ) { - case DATA_ID_FLOAT: - break; - case DATA_ID_INT: - *((int*)out) = *(int*)ptr; - break; - case DATA_ID_VEC2: - case DATA_ID_VEC3: - case DATA_ID_MAT3: - case DATA_ID_MAT4: - assert(0); - break; - case DATA_ID_MAT4P: - case DATA_ID_VEC3P: - *((float**)out) = *(float**)ptr; - break; - default: - assert(0); - } - - return 1; -} - - -static inline void *get_lens_offset_ptr(struct lens *lens, void *structure) -{ - return (void*)((unsigned char*)structure + lens->offset); -} - -static inline float *get_lens_ptr(struct lens *lens, void *structure) -{ - assert(lens->type == DATA_ID_MAT4P || lens->type == DATA_ID_VEC3P); - float **p = (float**)get_lens_offset_ptr(lens, structure); - return *p; -} - -static inline float *get_lens_float_array(struct lens *lens, void *structure) -{ - assert(lens->type == DATA_ID_MAT4 || - lens->type == DATA_ID_VEC3 || - lens->type == DATA_ID_VEC2 || - lens->type == DATA_ID_MAT3); - void *p = get_lens_offset_ptr(lens, structure); - return (float*)p; -} - -static inline float get_lens_float(struct lens *lens, void *structure) -{ - assert(lens->type == DATA_ID_FLOAT); - void *p = get_lens_offset_ptr(lens, structure); - return *(float*)p; -} - -static inline int get_lens_int(struct lens *lens, void *structure) -{ - assert(lens->type == DATA_ID_INT); - void *p = get_lens_offset_ptr(lens, structure); - return *(int*)p; -} diff --git a/src/lens.h b/src/lens.h @@ -0,0 +1,59 @@ +#pragma once + +#include "resource.h" +#include <stddef.h> +#include <assert.h> +#include <string.h> + +enum datatype { + DATA_ID_FLOAT = 1, + DATA_ID_INT, + DATA_ID_VEC2, + DATA_ID_VEC3, + DATA_ID_VEC3P, + DATA_ID_MAT3, + DATA_ID_MAT4, + DATA_ID_MAT4P, +}; + +struct lens { + const char *name; + int offset; + enum datatype type; +}; + +static inline void *get_lens_offset_ptr(struct lens *lens, void *structure) +{ + return (void*)((unsigned char*)structure + lens->offset); +} + +static inline float *get_lens_ptr(struct lens *lens, void *structure) +{ + assert(lens->type == DATA_ID_MAT4P || lens->type == DATA_ID_VEC3P); + float **p = (float**)get_lens_offset_ptr(lens, structure); + return *p; +} + +static inline float *get_lens_float_array(struct lens *lens, void *structure) +{ + assert(lens->type == DATA_ID_MAT4 || + lens->type == DATA_ID_VEC3 || + lens->type == DATA_ID_VEC2 || + lens->type == DATA_ID_MAT3); + void *p = get_lens_offset_ptr(lens, structure); + return (float*)p; +} + +static inline float get_lens_float(struct lens *lens, void *structure) +{ + assert(lens->type == DATA_ID_FLOAT); + void *p = get_lens_offset_ptr(lens, structure); + return *(float*)p; +} + +static inline int get_lens_int(struct lens *lens, void *structure) +{ + assert(lens->type == DATA_ID_INT); + void *p = get_lens_offset_ptr(lens, structure); + return *(int*)p; +} diff --git a/src/shader.c b/src/shader.c @@ -186,7 +186,7 @@ void add_uniform(struct gpu_program *program, check_gl(); } -static GLenum datatype_to_gl_type(enum data_id_datatype type) +static GLenum datatype_to_gl_type(enum datatype type) { switch (type) { case DATA_ID_INT: return GL_INT; diff --git a/src/shader.h b/src/shader.h @@ -4,7 +4,7 @@ #include <time.h> #include "gl.h" #include "vbo.h" -#include "data_id.h" +#include "lens.h" #define SHADER(f) "etc/shaders/" f diff --git a/src/ui.c b/src/ui.c @@ -5,7 +5,7 @@ #include "geometry.h" #include "util.h" #include "common.h" -#include "data_id.h" +#include "lens.h" // v1------v0 diff --git a/test/test_data_id.c b/test/test_data_id.c @@ -1,6 +1,6 @@ #include "resource.h" -#include "data_id.h" +#include "lens.h" #include <stdio.h> struct test_struct {