Removed typedefs and changed struct/enum usage

dev
Shariq Shah 10 years ago
parent 4d2f59dcb0
commit 4a8c3133bb
  1. 28
      src/array.c
  2. 27
      src/array.h
  3. 5
      src/components.h
  4. 30
      src/entity.c
  5. 22
      src/entity.h
  6. 55
      src/geometry.c
  7. 23
      src/input.c
  8. 21
      src/input.h
  9. 11
      src/shader.c
  10. 2
      src/string_utils.c

@ -5,11 +5,11 @@
#define ARRAY_MIN_CAPACITY 2
static bool array_reallocate(Array* array);
static bool array_reallocate(struct Array* array);
Array* array_new_(size_t object_size, int capacity)
struct Array* array_new_(size_t object_size, int capacity)
{
Array* newArray = malloc(sizeof(Array));
struct Array* newArray = malloc(sizeof(struct Array));
newArray->object_size = object_size;
newArray->length = 0;
@ -19,23 +19,23 @@ Array* array_new_(size_t object_size, int capacity)
return newArray;
}
void array_free(Array* array)
void array_free(struct Array* array)
{
free(array->data);
free(array);
}
void* array_get(Array* array, unsigned int index)
void* array_get(struct Array* array, unsigned int index)
{
return array->data + (array->object_size * index);
}
void* array_top(Array* array)
void* array_top(struct Array* array)
{
return array->data + (array->object_size * (array->length - 1));
}
void* array_add(Array* array)
void* array_add(struct Array* array)
{
/* if capacity is full, double size */
if(++array->length > array->capacity)
@ -58,7 +58,7 @@ void* array_add(Array* array)
return array->data + (array->object_size * (array->length - 1));
}
void array_reset(Array* array, unsigned int length)
void array_reset(struct Array* array, unsigned int length)
{
if(array->data) free(array->data);
array->length = length;
@ -66,7 +66,7 @@ void array_reset(Array* array, unsigned int length)
array->data = malloc(array->object_size * array->capacity);
}
bool array_pop(Array* array)
bool array_pop(struct Array* array)
{
bool success = false;
if(array->length > 0)
@ -77,7 +77,7 @@ bool array_pop(Array* array)
return success;
}
bool array_reallocate(Array* array)
bool array_reallocate(struct Array* array)
{
bool success = true;
/* If capacity is too big i.e. 4 times larger than length, halve it */
@ -93,7 +93,7 @@ bool array_reallocate(Array* array)
return success;
}
bool array_remove_at(Array* array, unsigned int index)
bool array_remove_at(struct Array* array, unsigned int index)
{
bool success = false;
if(array->length > 0)
@ -117,17 +117,17 @@ bool array_remove_at(Array* array, unsigned int index)
return success;
}
void* array_begin(Array* array)
void* array_begin(struct Array* array)
{
return array->data;
}
void* array_end(Array* array)
void* array_end(struct Array* array)
{
return array->data + (array->object_size * array->length);
}
void array_sort(Array* array, int (*compar)(const void*, const void*))
void array_sort(struct Array* array, int (*compar)(const void*, const void*))
{
qsort(array->data, array->length, array->object_size, compar);
}

@ -4,44 +4,45 @@
#include <stddef.h>
#include <stdbool.h>
typedef struct
struct Array
{
char* data; // actual data
unsigned int capacity; // current capacity i.e memory allocated
unsigned int length; // current length of array
size_t object_size; // size per element
} Array;
};
Array* array_new_(size_t object_size, int capacity);
struct Array* array_new_(size_t object_size, int capacity);
#define array_new(type) array_new_(sizeof(type), 0); // Use this for array creation
#define array_new_cap(type, capacity) array_new_(sizeof(type), capacity); // Use this for array with specific capacity
void array_free(Array* array);
void array_free(struct Array* array);
// All the macros with _val return by value while the function returns pointer
void* array_get(Array* array, unsigned int index);
void* array_get(struct Array* array, unsigned int index);
#define array_get_val(array, type, index) (*((type*) array_get(array, index)))
#define array_get_last_val(array, type) array_get_val(array, type, array->length - 1)
#define array_get_raw(array, type) (type*) array->data
void* array_top(Array* array);
void* array_top(struct Array* array);
#define array_top_val(array, type) (*((type*) array_top(array)))
void* array_add(Array* array);
void* array_add(struct Array* array);
#define array_add_val(array, type) (*((type*) array_add(array)))
#define array_push(array, value, type) \
type* new_val = array_add(array); \
*new_val = value;
void array_reset(Array* array, unsigned int length); // Resize to length objects whose data is unspecified
void array_reset(struct Array* array, unsigned int length); // Resize to length objects whose data is unspecified
#define array_clear(array) array_reset(array, 0)
bool array_pop(Array* array); // Remove object with highest index
bool array_remove_at(Array* array, unsigned int index);
bool array_pop(struct Array* array); // Remove object with highest index
bool array_remove_at(struct Array* array, unsigned int index);
void* array_begin(Array* array);
void* array_end(Array* array);
void* array_begin(struct Array* array);
void* array_end(struct Array* array);
void array_sort(Array* array, int (*compar)(const void*, const void*)); // Sort array by providing a comparator function
void array_sort(struct Array* array, int (*compar)(const void*, const void*)); // Sort array by providing a comparator function
#endif

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

@ -7,13 +7,13 @@
#include <string.h>
#include <assert.h>
static Array* entity_list;
static Array* empty_indices;
static struct Array* entity_list;
static struct Array* empty_indices;
void entity_initialize(void)
{
entity_list = array_new(Entity);
entity_list = array_new(struct Entity);
empty_indices = array_new(int);
}
@ -28,11 +28,11 @@ void entity_cleanup(void)
void entity_remove(int index)
{
Entity* entity = array_get(entity_list, index);
struct Entity* entity = array_get(entity_list, index);
for(int i = 0; i < MAX_COMPONENTS; i++)
{
Component component = entity->components[i];
enum Component component = entity->components[i];
switch(component)
{
case C_TRANSFORM:
@ -50,9 +50,9 @@ void entity_remove(int index)
}
}
Entity* entity_create(const char* name, const char* tag)
struct Entity* entity_create(const char* name, const char* tag)
{
Entity* new_entity = NULL;
struct Entity* new_entity = NULL;
int index = -1;
if(empty_indices->length > 0)
{
@ -79,9 +79,9 @@ Entity* entity_create(const char* name, const char* tag)
}
Entity* entity_get(int index)
struct Entity* entity_get(int index)
{
Entity* entity = NULL;
struct Entity* entity = NULL;
if(index >= 0 && index < (int)entity_list->length)
entity = array_get(entity_list, index);
else
@ -89,13 +89,13 @@ Entity* entity_get(int index)
return entity;
}
Entity* entity_find(const char* name)
struct Entity* entity_find(const char* name)
{
/* Bruteforce search all entities and return the first match */
Entity* entity = NULL;
struct Entity* entity = NULL;
for(int i = 0; i < (int)entity_list->length; i++)
{
Entity* curr_ent = array_get(entity_list, i);
struct Entity* curr_ent = array_get(entity_list, i);
if(strcmp(curr_ent->name, name) == 0)
{
entity = curr_ent;
@ -105,7 +105,7 @@ Entity* entity_find(const char* name)
return entity;
}
bool entity_component_remove(Entity* entity, Component component)
bool entity_component_remove(struct Entity* entity, enum Component component)
{
bool success = true;
assert(entity);
@ -127,7 +127,7 @@ bool entity_component_remove(Entity* entity, Component component)
return success;
}
void* entity_component_get(Entity* entity, Component component)
void* entity_component_get(struct Entity* entity, enum Component component)
{
void* comp_obj = NULL;
assert(entity);
@ -148,7 +148,7 @@ void* entity_component_get(Entity* entity, Component component)
return comp_obj;
}
void* entity_component_add(Entity* entity, Component component)
void* entity_component_add(struct Entity* entity, enum Component component)
{
void* new_comp = NULL;
assert(entity);

@ -7,23 +7,23 @@
typedef int32 Node;
typedef struct
struct Entity
{
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);
void entity_initialize(void);
void entity_cleanup(void);
void entity_remove(int index);
struct Entity* entity_create(const char* name, const char* tag);
struct Entity* entity_get(int index);
struct Entity* entity_find(const char* name);
bool entity_component_remove(struct Entity* entity, enum Component component);
void* entity_component_get(struct Entity* entity, enum Component component);
void* entity_component_add(struct Entity* entity, enum Component component);
#endif

@ -15,37 +15,38 @@
#include <string.h>
#include <assert.h>
typedef struct
struct Geometry
{
char* filename;
bool draw_indexed;
Array* vertices;
Array* vertex_colors;
Array* normals;
Array* uvs;
Array* indices;
uint vao;
uint vertex_vbo;
uint uv_vbo;
uint normal_vbo;
uint color_vbo;
uint index_vbo;
uint ref_count;
char* filename;
bool draw_indexed;
uint vao;
uint vertex_vbo;
uint uv_vbo;
uint normal_vbo;
uint color_vbo;
uint index_vbo;
uint ref_count;
struct Array* vertices;
struct Array* vertex_colors;
struct Array* normals;
struct Array* uvs;
struct Array* indices;
/* BoundingBox boundingBox; */
/* BoundingSphere boundingSphere; */
} Geometry;
};
/* Data */
static Array* geometry_list;
static Array* empty_indices;
static struct Array* geometry_list;
static struct Array* empty_indices;
/* Function definitions */
bool load_from_file(Geometry* geometry, const char* filename);
void create_vao(Geometry* geometry);
bool load_from_file(struct Geometry* geometry, const char* filename);
void create_vao(struct Geometry* geometry);
void geom_initialize(void)
{
geometry_list = array_new(Geometry);
geometry_list = array_new(struct Geometry);
empty_indices = array_new(int);
}
@ -54,7 +55,7 @@ int geom_find(const char* filename)
int index = -1;
for(int i = 0; i < (int)geometry_list->length; i++)
{
Geometry* geometry = array_get(geometry_list, i);
struct Geometry* geometry = array_get(geometry_list, i);
if(strcmp(geometry->filename, filename) == 0)
{
index = i;
@ -71,7 +72,7 @@ int geom_create(const char* name)
if(index == -1)
{
/* add new geometry object or overwrite existing one */
Geometry* new_geo = NULL;
struct Geometry* new_geo = NULL;
int index = -1;
if(empty_indices->length != 0)
{
@ -101,7 +102,7 @@ int geom_create(const char* name)
}
else
{
Geometry* raw_geom_array = array_get_raw(geometry_list, Geometry);
struct Geometry* raw_geom_array = array_get_raw(geometry_list, struct Geometry);
raw_geom_array[index].ref_count++;
}
return index;
@ -111,7 +112,7 @@ void geom_remove(int index)
{
if(index >= 0 && index < (int)geometry_list->length)
{
Geometry* geometry = array_get(geometry_list, index);
struct Geometry* geometry = array_get(geometry_list, index);
array_free(geometry->indices);
array_free(geometry->vertices);
array_free(geometry->uvs);
@ -131,7 +132,7 @@ void geom_cleanup(void)
array_free(empty_indices);
}
bool load_from_file(Geometry* geometry, const char* filename)
bool load_from_file(struct Geometry* geometry, const char* filename)
{
assert(filename);
bool success = true;
@ -184,7 +185,7 @@ bool load_from_file(Geometry* geometry, const char* filename)
return success;
}
void create_vao(Geometry* geometry)
void create_vao(struct Geometry* geometry)
{
// TODO : Add support for different model formats and interleaving VBO
assert(geometry);

@ -2,6 +2,7 @@
#include <stdlib.h>
#include <string.h>
#include "input.h"
#include "array.h"
#include "GLFW/glfw3.h"
#include "window_system.h"
@ -15,7 +16,7 @@ static void input_on_mousebutton(GLFWwindow* window, int button, int action, int
static void input_on_cursor_move(GLFWwindow* window, double xpos, double ypos);
static int map_find(const char* name);
static Array* input_map_list;
static struct Array* input_map_list;
void input_init(GLFWwindow* window)
{
@ -23,14 +24,14 @@ void input_init(GLFWwindow* window)
glfwSetKeyCallback(window, input_on_key);
glfwSetCursorPosCallback(window, input_on_cursor_move);
input_map_list = array_new(Input_Map);
input_map_list = array_new(struct Input_Map);
}
void input_cleanup(void)
{
for(unsigned int i = 0; i < input_map_list->length; i++)
{
Input_Map* map = array_get(input_map_list, i);
struct Input_Map* map = array_get(input_map_list, i);
array_free(map->keys);
}
array_free(input_map_list);
@ -52,7 +53,7 @@ static void input_on_key(GLFWwindow* window, int key, int scancode, int action,
{
for(unsigned int i = 0; i < input_map_list->length; i++)
{
Input_Map* map = array_get(input_map_list, i);
struct Input_Map* map = array_get(input_map_list, i);
for(unsigned int j = 0; j < map->keys->length; j++)
{
int map_key = array_get_val(map->keys, int, j);
@ -72,7 +73,7 @@ static void input_on_mousebutton(GLFWwindow* window, int button, int action, int
*/
}
void input_cursor_mode_set(Cursor_Mode mode)
void input_cursor_mode_set(enum Cursor_Mode mode)
{
GLFWwindow* window = window_get_active();
int cursor_mode = GLFW_CURSOR_NORMAL;
@ -89,7 +90,7 @@ bool input_map_state_get(const char* map_name, int state)
int current_state = KS_INACTIVE;
for(unsigned int i = 0; i < input_map_list->length; i++)
{
Input_Map* map = array_get(input_map_list, i);
struct Input_Map* map = array_get(input_map_list, i);
if(strcmp(map->name, map_name) == 0)
{
current_state = map->state;
@ -117,7 +118,7 @@ void input_map_create(const char* name, int* keys, size_t num_keys)
{
assert(name && keys && num_keys > 0);
Input_Map* new_map = array_add(input_map_list);
struct Input_Map* new_map = array_add(input_map_list);
new_map->name = name;
new_map->keys = array_new(int);
new_map->state = KS_INACTIVE;
@ -132,7 +133,7 @@ void input_update(void)
{
for(unsigned int i = 0; i < input_map_list->length; i++)
{
Input_Map* map = array_get(input_map_list, i);
struct Input_Map* map = array_get(input_map_list, i);
if(map->state == GLFW_RELEASE)
map->state = KS_INACTIVE;
}
@ -160,7 +161,7 @@ bool input_map_keys_set(const char* name, int* keys, int num_keys)
int index = map_find(name);
if(index > -1)
{
Input_Map* map = array_get(input_map_list, (unsigned int)index);
struct Input_Map* map = array_get(input_map_list, (unsigned int)index);
array_reset(map->keys, num_keys);
for(int i = 0; i < num_keys; i++)
{
@ -181,7 +182,7 @@ bool input_map_name_set(const char* name, const char* new_name)
int index = map_find(name);
if(index > -1)
{
Input_Map* map = array_get(input_map_list, (unsigned int)index);
struct Input_Map* map = array_get(input_map_list, (unsigned int)index);
map->name = new_name;
success = true;
}
@ -194,7 +195,7 @@ static int map_find(const char* name)
int index = -1;
for(unsigned int i = 0; i < input_map_list->length; i++)
{
Input_Map* map = array_get(input_map_list, i);
struct Input_Map* map = array_get(input_map_list, i);
if(strcmp(name, map->name) == 0)
{
index = i;

@ -1,32 +1,33 @@
#ifndef input_H
#define input_H
#include "array.h"
#include <stdbool.h>
#include <stdlib.h>
typedef struct GLFWwindow GLFWwindow;
struct Array;
typedef enum
enum Cursor_Mode
{
CM_NORMAL = 0,
CM_LOCKED,
CM_HIDDEN
} Cursor_Mode;
};
typedef struct
struct Input_Map
{
Array* keys;
const char* name;
int state;
} Input_Map;
struct Array* keys;
const char* name;
int state;
};
void input_init(GLFWwindow* window);
void input_cleanup(void);
bool input_mousebutton_state_get(int button, int state_type);
bool input_key_state_get(int key, int state_type);
void input_cursor_pos_get(double* xpos, double* ypos);
void input_cursor_mode_set(Cursor_Mode mode);
void input_cursor_mode_set(enum Cursor_Mode mode);
void input_update(void);
bool input_map_state_get(const char* map_name, int state);
void input_map_create(const char* name, int* keys, size_t num_keys);

@ -14,14 +14,13 @@
#include "GL/glew.h"
#include "GLFW/glfw3.h"
typedef struct
struct Shader_Object
{
unsigned int vertex_shader;
unsigned int fragment_shader;
unsigned int program;
} Shader_Object;
};
typedef struct Shader_Object Shader_Object;
// Constants for locations of attributes inside all shaders
const int POSITION_LOC = 0;
@ -29,8 +28,8 @@ const int NORMAL_LOC = 1;
const int UV_LOC = 2;
const int COLOR_LOC = 3;
static Array* shader_list;
static Array* empty_indices;
static struct Array* shader_list;
static struct Array* empty_indices;
void debug_print_shader(const char* shaderText)
{

@ -28,7 +28,7 @@ char* str_concat(char* string, const char* str_to_concat)
char* str_replace(char* string, const char* pattern, const char* replacement)
{
Array* indices = array_new(unsigned int);
struct Array* indices = array_new(unsigned int);
size_t string_len = strlen(string);
/* Calculate size of new string and allocate new memory */

Loading…
Cancel
Save