polyadvent

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

data_id.h (2011B)


      1 #pragma once
      2 
      3 #include "resource.h"
      4 #include <stddef.h>
      5 #include <assert.h>
      6 #include <string.h>
      7 
      8 enum data_id_type {
      9 	DATA_ID_RESOURCE = 1,
     10 	DATA_ID_RAWPTR,
     11 };
     12 
     13 enum data_id_datatype {
     14 	DATA_ID_FLOAT = 1,
     15 	DATA_ID_INT,
     16 	DATA_ID_VEC2,
     17 	DATA_ID_VEC3,
     18 	DATA_ID_VEC3P,
     19 	DATA_ID_MAT3,
     20 	DATA_ID_MAT4,
     21 	DATA_ID_MAT4P,
     22 };
     23 
     24 struct lens {
     25 	const char *name;
     26 	int offset;
     27 	enum data_id_datatype type;
     28 };
     29 
     30 static inline int data_id_get_ptr_data(void *ptr, enum data_id_datatype typ, void *out)
     31 {
     32     assert(out);
     33     assert(ptr);
     34     switch (typ) {
     35         case DATA_ID_FLOAT:
     36             break;
     37         case DATA_ID_INT:
     38             *((int*)out) = *(int*)ptr;
     39             break;
     40         case DATA_ID_VEC2:
     41         case DATA_ID_VEC3:
     42         case DATA_ID_MAT3:
     43         case DATA_ID_MAT4:
     44             assert(0);
     45             break;
     46         case DATA_ID_MAT4P:
     47         case DATA_ID_VEC3P:
     48             *((float**)out) = *(float**)ptr;
     49 	    break;
     50         default:
     51             assert(0);
     52     }
     53 
     54     return 1;
     55 }
     56 
     57 
     58 static inline void *get_lens_offset_ptr(struct lens *lens, void *structure)
     59 {
     60 	return (void*)((unsigned char*)structure + lens->offset);
     61 }
     62 
     63 static inline float *get_lens_ptr(struct lens *lens, void *structure)
     64 {
     65 	assert(lens->type == DATA_ID_MAT4P || lens->type == DATA_ID_VEC3P);
     66 	float **p = (float**)get_lens_offset_ptr(lens, structure);
     67 	return *p;
     68 }
     69 
     70 static inline float *get_lens_float_array(struct lens *lens, void *structure)
     71 {
     72 	assert(lens->type == DATA_ID_MAT4 ||
     73 	       lens->type == DATA_ID_VEC3 ||
     74 	       lens->type == DATA_ID_VEC2 ||
     75 	       lens->type == DATA_ID_MAT3);
     76 	void *p = get_lens_offset_ptr(lens, structure);
     77 	return (float*)p;
     78 }
     79 
     80 static inline float get_lens_float(struct lens *lens, void *structure)
     81 {
     82 	assert(lens->type == DATA_ID_FLOAT);
     83 	void *p = get_lens_offset_ptr(lens, structure);
     84 	return *(float*)p;
     85 }
     86 
     87 static inline int get_lens_int(struct lens *lens, void *structure)
     88 {
     89 	assert(lens->type == DATA_ID_INT);
     90 	void *p = get_lens_offset_ptr(lens, structure);
     91 	return *(int*)p;
     92 }