ecs updates, still no compile
This commit is contained in:
parent
68746a654f
commit
6325c4e4a7
1895
include/stb_ds.h
1895
include/stb_ds.h
File diff suppressed because it is too large
Load Diff
@ -3,8 +3,8 @@
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <stb_ds.h>
|
||||
#include <cglm/cglm.h>
|
||||
|
||||
typedef uint8_t u8;
|
||||
|
@ -13,6 +13,11 @@ _renderer_viewport_set(Renderer* self, ivec4 viewport)
|
||||
void
|
||||
renderer_init(Renderer* self, Window* window, CameraType type)
|
||||
{
|
||||
ivec2 windowSize;
|
||||
vec2 size;
|
||||
|
||||
window_size_get(window, windowSize);
|
||||
|
||||
self->window = window;
|
||||
|
||||
self->glContext = SDL_GL_CreateContext(self->window->sdl);
|
||||
@ -23,10 +28,13 @@ renderer_init(Renderer* self, Window* window, CameraType type)
|
||||
vbo_init(&self->vbo, GL_ARRAY_BUFFER, true);
|
||||
vbo_init(&self->ebo, GL_ELEMENT_ARRAY_BUFFER, true);
|
||||
|
||||
size[0] = (f32)windowSize[0];
|
||||
size[1] = (f32)windowSize[1];
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case CAMERA_ORTHOGRAPHIC:
|
||||
camera_orthographic_init(&self->camera, self->window->size);
|
||||
camera_orthographic_init(&self->camera, size);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
@ -16,7 +16,7 @@ typedef struct Renderer
|
||||
VAO vao; // Vertex Array Object
|
||||
} Renderer;
|
||||
|
||||
void renderer_init(Renderer* self, Window* window);
|
||||
void renderer_init(Renderer* self, Window* window, CameraType type);
|
||||
void renderer_viewport_window_set(Renderer* self);
|
||||
void renderer_clear_color_set(Renderer* self, vec4 color);
|
||||
void renderer_clear(Renderer* self);
|
||||
|
186
src/engine/vector.c
Normal file
186
src/engine/vector.c
Normal file
@ -0,0 +1,186 @@
|
||||
#include "vector.h"
|
||||
|
||||
static void _vector_realloc(Vector* self);
|
||||
|
||||
/* Reallocates the vector to a specified size. */
|
||||
static void
|
||||
_vector_realloc(Vector* self)
|
||||
{
|
||||
if (self->count == 0)
|
||||
{
|
||||
self->data = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
self->data = realloc(self->data, vector_size_get(self));
|
||||
}
|
||||
|
||||
/* Pushes all elements past an index forward (<-). */
|
||||
static void
|
||||
vector_forward(Vector* self, s32 index)
|
||||
{
|
||||
s32 length;
|
||||
|
||||
length = ((self->count - 1) - index);
|
||||
|
||||
for (s32 i = 0; i < length; i++)
|
||||
{
|
||||
size_t* data;
|
||||
size_t* nextData;
|
||||
s32 currentIndex;
|
||||
|
||||
currentIndex = index + i;
|
||||
|
||||
data = (size_t*)self->data + (currentIndex * self->itemSize);
|
||||
nextData = data + self->itemSize;
|
||||
|
||||
memcpy((void*)data, (void*)nextData, self->itemSize);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* Pushes all elements past an index backward (->).*/
|
||||
static void
|
||||
vector_backward(Vector* self, s32 index)
|
||||
{
|
||||
s32 length;
|
||||
|
||||
length = ((self->count - 1) - index);
|
||||
|
||||
for (s32 i = 0; i < length; i++)
|
||||
{
|
||||
size_t* data;
|
||||
size_t* prevData;
|
||||
s32 index;
|
||||
|
||||
index = (self->count - 1) - i;
|
||||
|
||||
data = (size_t*)self->data + (index * self->itemSize);
|
||||
prevData = (size_t*)data - self->itemSize;
|
||||
|
||||
memcpy((void*)data, (void*)prevData, self->itemSize);
|
||||
}
|
||||
}
|
||||
|
||||
/* Returns total size of vector. */
|
||||
size_t
|
||||
vector_size_get(Vector* self)
|
||||
{
|
||||
return self->count * self->itemSize;
|
||||
}
|
||||
|
||||
/* Initializes a vector, given a size of an element. */
|
||||
void
|
||||
vector_init(Vector* self, size_t size)
|
||||
{
|
||||
memset(self, '\0', sizeof(Vector));
|
||||
|
||||
self->itemSize = size;
|
||||
self->count = 0;
|
||||
|
||||
_vector_realloc(self);
|
||||
|
||||
memset(self->data, '\0', vector_size_get(self));
|
||||
}
|
||||
|
||||
/* Frees a vector. */
|
||||
void
|
||||
vector_free(Vector* self)
|
||||
{
|
||||
if (self->data)
|
||||
free(self->data);
|
||||
|
||||
self->count = 0;
|
||||
}
|
||||
|
||||
/* Adds an element to the end of the vector. */
|
||||
void*
|
||||
vector_push(Vector* self, void* data)
|
||||
{
|
||||
size_t* pointer;
|
||||
|
||||
self->count++;
|
||||
|
||||
_vector_realloc(self);
|
||||
|
||||
pointer = (size_t*)self->data + (vector_size_get(self) - self->itemSize);
|
||||
|
||||
if (data)
|
||||
memcpy((void*)pointer, data, self->itemSize);
|
||||
else
|
||||
memset((void*)pointer, '\0', self->itemSize);
|
||||
|
||||
return pointer;
|
||||
}
|
||||
|
||||
/* Removes the last element of the vector. */
|
||||
void
|
||||
vector_pop(Vector* self)
|
||||
{
|
||||
self->count--;
|
||||
|
||||
_vector_realloc(self);
|
||||
}
|
||||
|
||||
/* Removes all elements of a vector (but does not dealloc the data). */
|
||||
void
|
||||
vector_clear(Vector* self)
|
||||
{
|
||||
self->count = 0;
|
||||
|
||||
_vector_realloc(self);
|
||||
}
|
||||
|
||||
/* Inserts an element at the specified index, putting successive elements back. */
|
||||
void*
|
||||
vector_insert(Vector* self, void* data, s32 index)
|
||||
{
|
||||
size_t* pointer;
|
||||
|
||||
self->count++;
|
||||
|
||||
_vector_realloc(self);
|
||||
|
||||
vector_backward(self, index);
|
||||
|
||||
pointer = (size_t*)self->data + (index * self->itemSize);
|
||||
|
||||
memcpy((void*)pointer, data, self->itemSize);
|
||||
|
||||
return pointer;
|
||||
}
|
||||
|
||||
/* Removes an element in a vector at the specified index, bringing successive elements forward. */
|
||||
void
|
||||
vector_remove(Vector* self, s32 index)
|
||||
{
|
||||
vector_forward(self, index);
|
||||
|
||||
vector_pop(self);
|
||||
}
|
||||
|
||||
/* Returns the address of the element at the specified index. */
|
||||
void*
|
||||
vector_get(Vector* self, s32 index)
|
||||
{
|
||||
return (void*)((size_t*)self->data + (index * self->itemSize));
|
||||
}
|
||||
|
||||
/* Returns the index of an element, given it is valid. */
|
||||
s32
|
||||
vector_index_get(Vector* self, void* data)
|
||||
{
|
||||
s32 index;
|
||||
|
||||
index = ((size_t*)data - (size_t*)self->data) / self->itemSize;
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
/* Removes an element within a vector, given its data, provided it is valid. */
|
||||
void
|
||||
vector_remove_from_data(Vector* self, void* data)
|
||||
{
|
||||
vector_remove(self, vector_index_get(self, data));
|
||||
data = NULL;
|
||||
}
|
24
src/engine/vector.h
Normal file
24
src/engine/vector.h
Normal file
@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include "../COMMON.h"
|
||||
|
||||
typedef struct Vector
|
||||
{
|
||||
u32 count;
|
||||
u32 limit;
|
||||
size_t itemSize;
|
||||
void* data;
|
||||
} Vector;
|
||||
|
||||
s32 vector_index_get(Vector* self, void* data);
|
||||
size_t vector_size_get(Vector* self);
|
||||
void vector_free(Vector* self);
|
||||
void vector_init(Vector* self, size_t size);
|
||||
void vector_pop(Vector* self);
|
||||
void vector_remove(Vector* self, s32 index);
|
||||
void vector_clear(Vector* self);
|
||||
void vector_remove_from_data(Vector* self, void* data);
|
||||
void* vector_get(Vector* self, s32 index);
|
||||
void* vector_insert(Vector* self, void* data, s32 index);
|
||||
void* vector_push(Vector* self, void* data);
|
||||
void vector_print(Vector* self);
|
@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "../../COMMON.h"
|
||||
#include "../../engine/vector.h"
|
||||
|
||||
#define ECS_FUNCTION_COUNT ECS_FUNCTION_DRAW + 1
|
||||
typedef enum ECSFunctionType
|
||||
@ -17,11 +17,11 @@ typedef enum ECSComponentType
|
||||
ECS_COMPONENT_SPRITE
|
||||
} ECSComponentType;
|
||||
|
||||
typedef struct ECS;
|
||||
typedef struct Game;
|
||||
typedef struct ECS ECS;
|
||||
typedef struct Game Game;
|
||||
|
||||
typedef void (*ECSRegister)(ECS);
|
||||
typedef void (*ECSFunction)(ECS, void*);
|
||||
typedef void (*ECSRegister)(ECS*);
|
||||
typedef void (*ECSFunction)(void*, ECS*);
|
||||
|
||||
typedef struct ECSsystem
|
||||
{
|
||||
@ -33,14 +33,15 @@ typedef struct ECSComponentInfo
|
||||
ECSSystem system;
|
||||
ECSComponentType type;
|
||||
size_t size;
|
||||
};
|
||||
} ECSComponentInfo;
|
||||
|
||||
typedef struct ECSComponentList
|
||||
{
|
||||
ECSSustem system;
|
||||
ECS* ecs;
|
||||
ECSSystem system;
|
||||
ECSComponentType type;
|
||||
void* components; /* dynamic array */
|
||||
};
|
||||
Vector components; /* whatever type is being used */
|
||||
} ECSComponentList;
|
||||
|
||||
typedef struct ECS
|
||||
{
|
||||
|
@ -1,10 +1,51 @@
|
||||
#include "component_sprite.h"
|
||||
|
||||
static void _tick(ComponentSprite* self, ECS* ecs);
|
||||
static void _draw(ComponentSprite* self, ECS* ecs);
|
||||
|
||||
const ECSComponentInfo COMPONENT_SPRITE_INFO =
|
||||
{
|
||||
.system =
|
||||
{
|
||||
.functions =
|
||||
{
|
||||
NULL,
|
||||
NULL,
|
||||
(ECSFunction)_tick,
|
||||
(ECSFunction)_draw
|
||||
}
|
||||
},
|
||||
.type = ECS_COMPONENT_SPRITE,
|
||||
.size = sizeof(ComponentSprite)
|
||||
};
|
||||
|
||||
/* Ticks sprite component. */
|
||||
static void
|
||||
_tick(ComponentSprite* self, ECS* ecs)
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
/* Draws sprite component. */
|
||||
static void
|
||||
_draw(ComponentSprite* self, ECS* ecs)
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
/* Initializes sprite component. */
|
||||
void
|
||||
component_sprite_init
|
||||
(
|
||||
|
||||
ComponentSprite* self
|
||||
|
||||
|
||||
|
||||
@ -22,26 +63,4 @@ component_sprite_init
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
/* Ticks sprite component. */
|
||||
void
|
||||
component_sprite_tick(ComponentSprite* self, ECS* ecs)
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
/* Draws sprite component. */
|
||||
void
|
||||
component_sprite_draw(ComponentSprite* self, ECS* ecs)
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -2,6 +2,8 @@
|
||||
|
||||
#include "../../GAME_COMMON.h"
|
||||
|
||||
extern ECSComponentInfo COMPONENT_SPRITE_INFO;
|
||||
|
||||
typedef struct ComponentSprite
|
||||
{
|
||||
u32 id;
|
||||
@ -13,12 +15,4 @@ typedef struct ComponentSprite
|
||||
f32 rotation;
|
||||
} ComponentSprite;
|
||||
|
||||
static const ECSComponentInfo COMPONENT_SPRITE_INFO =
|
||||
{
|
||||
.system =
|
||||
{
|
||||
.functions = { NULL, NULL, NULL, NULL}
|
||||
},
|
||||
.type = ECS_COMPONENT_SPRITE,
|
||||
.size = sizeof(ComponentSprite)
|
||||
};
|
||||
void component_sprite_init(ComponentSprite* self);
|
||||
|
@ -9,15 +9,11 @@ _ecs_function(ECS* self, ECSFunctionType type)
|
||||
{
|
||||
for (s32 i = 0; i < ECS_COMPONENT_COUNT; i++)
|
||||
{
|
||||
ECSFunction function;
|
||||
struct ECSComponentList* list;
|
||||
ECSComponentList* list;
|
||||
|
||||
list = &game.ecs.lists[i];
|
||||
list = &self->lists[i];
|
||||
|
||||
function = list->system.functions[type];
|
||||
|
||||
if (function)
|
||||
ecs_component_function(list, function);
|
||||
ecs_component_list_function(list, type);
|
||||
}
|
||||
}
|
||||
|
||||
@ -40,11 +36,25 @@ void
|
||||
ecs_init(ECS* self, Game* game)
|
||||
{
|
||||
self->game = game;
|
||||
|
||||
for (s32 i = 0 ; i < ECS_COMPONENT_COUNT; i++)
|
||||
ecs_component_list_init(&self->lists[i], self, ECS_COMPONENT_INFO[i]);
|
||||
}
|
||||
|
||||
/* Clears ECS. */
|
||||
void
|
||||
ecs_clear(ECS* self)
|
||||
{
|
||||
for (s32 i = 0 ; i < ECS_COMPONENT_COUNT; i++)
|
||||
ecs_component_list_clear(&self->lists[i]);
|
||||
}
|
||||
|
||||
/* Frees ECS. */
|
||||
void
|
||||
ecs_free(ECS* self)
|
||||
{
|
||||
for (s32 i = 0 ; i < ECS_COMPONENT_COUNT; i++)
|
||||
ecs_component_list_free(&self->lists[i]);
|
||||
|
||||
memset(self, '\0', sizeof(ECS));
|
||||
}
|
||||
|
15
src/game/ecs/ecs.h
Normal file
15
src/game/ecs/ecs.h
Normal file
@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include "ecs_component_list.h"
|
||||
#include "component/component_sprite.h"
|
||||
|
||||
static const ECSComponentInfo ECS_COMPONENT_INFO[ECS_COMPONENT_COUNT] =
|
||||
{
|
||||
COMPONENT_SPRITE_INFO
|
||||
};
|
||||
|
||||
void ecs_tick(ECS* self);
|
||||
void ecs_draw(ECS* self);
|
||||
void ecs_init(ECS* self, Game* game);
|
||||
void ecs_clear(ECS* self);
|
||||
void ecs_free(ECS* self);
|
@ -1,10 +0,0 @@
|
||||
#include "ecs_component.h"
|
||||
|
||||
/* Executes a function on a component. */
|
||||
void
|
||||
ecs_component_function(ECSComponentList* self, ECSFunction function)
|
||||
{
|
||||
|
||||
|
||||
|
||||
}
|
42
src/game/ecs/ecs_component_list.c
Normal file
42
src/game/ecs/ecs_component_list.c
Normal file
@ -0,0 +1,42 @@
|
||||
#include "ecs_component_list.h"
|
||||
|
||||
/* From ECSComponentInfo, initialize a component list. */
|
||||
void
|
||||
ecs_component_list_init(ECSComponentList* self, ECS* ecs, ECSComponentInfo info)
|
||||
{
|
||||
self->ecs = ecs;
|
||||
self->type = info.type;
|
||||
self->system = info.system;
|
||||
vector_init(&self->vector, info.size);
|
||||
}
|
||||
|
||||
/* Executes a function on a component list. */
|
||||
/* Exits if component list has a NULL function. */
|
||||
void
|
||||
ecs_component_list_function(ECSComponentList* self, ECSFunctionType type)
|
||||
{
|
||||
ECSFunction function;
|
||||
|
||||
function = self->system.functions[type];
|
||||
|
||||
if (!function)
|
||||
return;
|
||||
|
||||
for (s32 i = 0; i < self->components.count; i++)
|
||||
function(vector_get(i), self->ecs);
|
||||
}
|
||||
|
||||
/* Clears all elements in a component list. */
|
||||
void
|
||||
ecs_component_list_clear(ECSComponentList* self)
|
||||
{
|
||||
vector_clear(&self->components);
|
||||
}
|
||||
|
||||
/* Frees a component list. */
|
||||
void
|
||||
ecs_component_list_free(ECSComponentList* self)
|
||||
{
|
||||
vector_free(&self->components);
|
||||
memset(self, '\0', sizeof(ECSComponentList));
|
||||
}
|
8
src/game/ecs/ecs_component_list.h
Normal file
8
src/game/ecs/ecs_component_list.h
Normal file
@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include "ECS_COMMON.h"
|
||||
|
||||
void ecs_component_list_init(ECSComponentList* self, ECS* ecs, ECSComponentInfo info);
|
||||
void ecs_component_list_function(ECSComponentList* self, ECSFunctionType type);
|
||||
void ecs_component_list_clear(ECSComponentList* self);
|
||||
void ecs_component_list_free(ECSComponentList* self);
|
Loading…
Reference in New Issue
Block a user