Added Entity creation and removal

dev
Shariq Shah 10 years ago
parent 636966616a
commit 4d2f59dcb0
  1. 4
      .dir-locals.el
  2. 1
      .gitignore
  3. 14
      src/components.h
  4. 170
      src/entity.c
  5. 29
      src/entity.h
  6. 4
      src/game.c

@ -1,4 +1,6 @@
((c-mode . ((c-mode .
((company-clang-arguments . ("-I/mnt/Dev/Projects/Symmetry/include/GLFW")) ((company-clang-arguments . ("-I/mnt/Dev/Projects/Symmetry/include/GLFW"))
(flycheck-clang-include-path . ("/mnt/Dev/Projects/Symmetry/include/GLFW"))) (flycheck-clang-include-path . ("/mnt/Dev/Projects/Symmetry/include/GLFW")))
)) )
(nil .
(toggle-truncate-lines t)))

1
.gitignore vendored

@ -4,3 +4,4 @@ build/*
libs/* libs/*
include/* include/*
orgfile.org orgfile.org
assets/*

@ -0,0 +1,14 @@
#ifndef components_H
#define components_H
typedef enum
{
C_TRANSFORM = 0,
C_MODEL,
C_CAMERA,
C_RIGIDBODY,
MAX_COMPONENTS
} Component;
#endif

@ -0,0 +1,170 @@
#include "entity.h"
#include "array.h"
#include "log.h"
#include "string_utils.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
static Array* entity_list;
static Array* empty_indices;
void entity_initialize(void)
{
entity_list = array_new(Entity);
empty_indices = array_new(int);
}
void entity_cleanup(void)
{
for(int i = 0; i < (int)entity_list->length; i++)
entity_remove(i);
array_free(entity_list);
array_free(empty_indices);
}
void entity_remove(int index)
{
Entity* entity = array_get(entity_list, index);
for(int i = 0; i < MAX_COMPONENTS; i++)
{
Component component = entity->components[i];
switch(component)
{
case C_TRANSFORM:
break;
case C_MODEL:
break;
case C_RIGIDBODY:
break;
case C_CAMERA:
break;
default:
/* Only called for MAX_COMPONENTS, do nothing */
break;
}
}
}
Entity* entity_create(const char* name, const char* tag)
{
Entity* new_entity = NULL;
int index = -1;
if(empty_indices->length > 0)
{
index = array_get_last_val(empty_indices, int);
array_pop(empty_indices);
new_entity = array_get(entity_list, index);
}
else
{
new_entity = array_add(entity_list);
index = entity_list->length - 1;
}
if(new_entity->name) free(new_entity->name);
if(new_entity->name) free(new_entity->tag);
new_entity->name = name ? str_new(name) : str_new("DEFAULT_NAME");
new_entity->tag = tag ? str_new(tag) : str_new("DEFAULT_TAG");
new_entity->node = index;
/* TODO: Add transform here by default maybe? */
return new_entity;
}
Entity* entity_get(int index)
{
Entity* entity = NULL;
if(index >= 0 && index < (int)entity_list->length)
entity = array_get(entity_list, index);
else
log_error("entity:get", "Invalid index '%d'", index);
return entity;
}
Entity* entity_find(const char* name)
{
/* Bruteforce search all entities and return the first match */
Entity* entity = NULL;
for(int i = 0; i < (int)entity_list->length; i++)
{
Entity* curr_ent = array_get(entity_list, i);
if(strcmp(curr_ent->name, name) == 0)
{
entity = curr_ent;
break;
}
}
return entity;
}
bool entity_component_remove(Entity* entity, Component component)
{
bool success = true;
assert(entity);
switch(component)
{
case C_TRANSFORM:
break;
case C_MODEL:
break;
case C_RIGIDBODY:
break;
case C_CAMERA:
break;
default:
log_error("entity:component_remove", "Invalid component type");
break;
}
if(success) entity->components[component] = -1;
return success;
}
void* entity_component_get(Entity* entity, Component component)
{
void* comp_obj = NULL;
assert(entity);
switch(component)
{
case C_TRANSFORM:
break;
case C_MODEL:
break;
case C_RIGIDBODY:
break;
case C_CAMERA:
break;
default:
log_error("entity:component_get", "Invalid component type");
break;
}
return comp_obj;
}
void* entity_component_add(Entity* entity, Component component)
{
void* new_comp = NULL;
assert(entity);
switch(component)
{
case C_TRANSFORM:
break;
case C_MODEL:
break;
case C_RIGIDBODY:
break;
case C_CAMERA:
break;
default:
log_error("entity:component_add", "Invalid component type");
break;
}
return new_comp;
}

@ -0,0 +1,29 @@
#ifndef entity_H
#define entity_H
#include "components.h"
#include "num_types.h"
#include <stdbool.h>
typedef int32 Node;
typedef struct
{
Node node;
char* name;
char* tag;
int components[MAX_COMPONENTS];
} Entity;
void entity_initialize(void);
void entity_cleanup(void);
void entity_remove(int index);
Entity* entity_create(const char* name, const char* tag);
Entity* entity_get(int index);
Entity* entity_find(const char* name);
bool entity_component_remove(Entity* entity, Component component);
void* entity_component_get(Entity* entity, Component component);
void* entity_component_add(Entity* entity, Component component);
#endif

@ -8,6 +8,7 @@
#include "log.h" #include "log.h"
#include "file_io.h" #include "file_io.h"
#include "shader.h" #include "shader.h"
#include "entity.h"
void run(void); void run(void);
void update(void); void update(void);
@ -21,6 +22,8 @@ void game_init(void)
renderer_init(window); renderer_init(window);
io_file_initialize("/mnt/Dev/Projects/Symmetry/assets/");/* TODO: Implement proper way of getting binary directory */ io_file_initialize("/mnt/Dev/Projects/Symmetry/assets/");/* TODO: Implement proper way of getting binary directory */
shader_initialize(); shader_initialize();
entity_initialize();
int keys[2] = {'W', GLFW_KEY_UP}; int keys[2] = {'W', GLFW_KEY_UP};
int keys2[2] = {'S', GLFW_KEY_DOWN}; int keys2[2] = {'S', GLFW_KEY_DOWN};
@ -58,6 +61,7 @@ void render(void)
void game_cleanup(void) void game_cleanup(void)
{ {
entity_cleanup();
input_cleanup(); input_cleanup();
renderer_cleanup(); renderer_cleanup();
io_file_cleanup(); io_file_cleanup();

Loading…
Cancel
Save