diff --git a/src/array.c b/src/array.c index 5f014e1..4706602 100644 --- a/src/array.c +++ b/src/array.c @@ -1,28 +1,44 @@ #include +#include #include #include "array.h" +struct Array +{ + int capacity; // current capacity i.e memory allocated + int length; // current length of array + size_t object_size; // size per element + char data[]; // actual data +}; + #define ARRAY_MIN_CAPACITY 2 -static bool array_reallocate(struct Array* array); +static struct Array* array_get_ptr(void* array_data); +static struct Array* array_reallocate(struct Array* array); +static void* array_top(struct Array* array); +static void* array_get(struct Array* array, unsigned int index); -struct Array* array_new_(size_t object_size, int capacity) +static struct Array* array_get_ptr(void* array_data) { - struct Array* newArray = malloc(sizeof(struct Array)); - - newArray->object_size = object_size; - newArray->length = 0; - newArray->capacity = capacity == 0 ? ARRAY_MIN_CAPACITY : capacity; - newArray->data = malloc(newArray->object_size * newArray->capacity); + return (struct Array*)((char*)array_data - offsetof(struct Array, data)); +} - return newArray; +void* array_new_(size_t object_size, int capacity) +{ + capacity = capacity == 0 ? ARRAY_MIN_CAPACITY : capacity; + struct Array* new_array = malloc(sizeof(*new_array) + (object_size * capacity)); + new_array->object_size = object_size; + new_array->length = 0; + new_array->capacity = capacity; + + return new_array->data; } -void array_free(struct Array* array) +void array_free(void* array) { - free(array->data); - free(array); + struct Array* array_ptr = array_get_ptr(array); + free(array_ptr); } void* array_get(struct Array* array, unsigned int index) @@ -35,85 +51,105 @@ void* array_top(struct Array* array) return array->data + (array->object_size * (array->length - 1)); } -void* array_add(struct Array* array) +void* array_grow_(void** array) { + struct Array* array_ptr = array_get_ptr(*array); /* if capacity is full, double size */ - if(++array->length > array->capacity) + if(++array_ptr->length > array_ptr->capacity) { - array->capacity = array->capacity << 1; /* LShift by 1 means (number * number) */ - char* new_data = realloc(array->data, array->object_size * array->capacity); + array_ptr->capacity = array_ptr->capacity << 1; /* LShift by 1 means (number * number) */ + char* new_data = realloc(array_ptr, sizeof(*array_ptr) + (array_ptr->object_size * array_ptr->capacity)); if(new_data) { - array->data = new_data; + array_ptr = (struct Array*)new_data; + *array = array_ptr->data; /* update the original pointer to the new address */ } else { - array->length--; - array->capacity = array->capacity >> 1; + array_ptr->length--; + array_ptr->capacity = array_ptr->capacity >> 1; /* TODO: Error handling here! */ } } - - /* return new location added */ - return array->data + (array->object_size * (array->length - 1)); + /* Return new pointer to data */ + return array_top(array_ptr); } -void array_reset(struct Array* array, unsigned int length) +int array_reset_(void** array, int length) { - if(array->data) free(array->data); - array->length = length; - array->capacity = length < ARRAY_MIN_CAPACITY ? ARRAY_MIN_CAPACITY : length; - array->data = malloc(array->object_size * array->capacity); + struct Array* array_ptr = array_get_ptr(*array); + size_t object_size = array_ptr->object_size; + int new_capacity = length < ARRAY_MIN_CAPACITY ? ARRAY_MIN_CAPACITY : length; + int new_length = new_capacity; + array_ptr = calloc(1, sizeof(*array_ptr) + (new_capacity * object_size)); + if(array_ptr) + { + array_ptr->length = new_length; + array_ptr->capacity = new_capacity; + array_ptr->object_size = object_size; + *array = array_ptr->data; + } + + return array_ptr ? 1 : 0; } -bool array_pop(struct Array* array) +int array_pop_(void** array) { - bool success = false; - if(array->length > 0) + struct Array* array_ptr = array_get_ptr(*array); + int success = 1; + if(array_ptr->length > 0) { - array->length--; - success = array_reallocate(array); + array_ptr->length--; + if(!(array_ptr = array_reallocate(array_ptr))) + success = 0; + else + *array = array_ptr->data; } return success; } -bool array_reallocate(struct Array* array) +struct Array* array_reallocate(struct Array* array) { - bool success = true; /* If capacity is too big i.e. 4 times larger than length, halve it */ if((array->length << 2) < array->capacity && array->capacity > ARRAY_MIN_CAPACITY) { array->capacity = array->capacity >> 1; - char* new_data = realloc(array->data, array->object_size * array->capacity); - if(new_data) - array->data = new_data; - else - success = false; + array = realloc(array, sizeof(*array) + (array->object_size * array->capacity)); + /* TODO: Maybe error handling here? */ } - return success; + return array; } -bool array_remove_at(struct Array* array, unsigned int index) +int array_remove_at_(void** array, int index) { - bool success = false; - if(array->length > 0) + struct Array* array_ptr = array_get_ptr(*array); + int success = 1; + if(array_ptr->length > 0 && index <= array_ptr->length && index >= 0) { - unsigned int next_index = index + 1; - if(next_index < array->length) + int next_index = index + 1; + if(next_index < array_ptr->length) { - char* current_location = array->data + (array->object_size * index); - char* location_after_obj = current_location + array->object_size; + char* current_location = array_ptr->data + (array_ptr->object_size * index); + char* location_after_obj = current_location + array_ptr->object_size; memmove(current_location, location_after_obj, - array->object_size * (array->length - next_index)); - array->length--; - success = array_reallocate(array); + array_ptr->object_size * (array_ptr->length - next_index)); + array_ptr->length--; + if(!(array_ptr = array_reallocate(array_ptr))) + success = 0; + else + *array = array_ptr->data; } else { - success = array_pop(array); + if(!array_pop(*array)) + success = 0; } } + else + { + success = 0; + } return success; } @@ -127,7 +163,40 @@ void* array_end(struct Array* array) return array->data + (array->object_size * array->length); } -void array_sort(struct Array* array, int (*compar)(const void*, const void*)) +int array_len(void* array) { - qsort(array->data, array->length, array->object_size, compar); + struct Array* array_ptr = array_get_ptr(array); + return array_ptr->length; +} + +int array_capacity(void* array) +{ + struct Array* array_ptr = array_get_ptr(array); + return array_ptr->capacity; +} + +void array_match_len_cap_(void** array) +{ + struct Array* array_ptr = array_get_ptr(*array); + array_ptr->length = array_ptr->capacity; +} + +void array_inc_cap_by_(void** array, int cap) +{ + struct Array* array_ptr = array_get_ptr(*array); + if(cap > 0) + { + array_ptr->capacity += cap; + char* new_data = realloc(array_ptr, sizeof(*array_ptr) + (array_ptr->object_size * array_ptr->capacity)); + if(new_data) + { + array_ptr = (struct Array*)new_data; + *array = array_ptr->data; /* update the original pointer to the new address */ + } + else + { + array_ptr->capacity = array_ptr->capacity -= cap; + /* TODO: Error handling here! */ + } + } } diff --git a/src/array.h b/src/array.h index 876c8c0..6263a60 100644 --- a/src/array.h +++ b/src/array.h @@ -2,47 +2,34 @@ #define ARRAY_H #include -#include -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 -}; - - -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(struct Array* array); - -// All the macros with _val return by value while the function returns pointer -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(struct Array* array); -#define array_top_val(array, type) (*((type*) array_top(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(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(struct Array* array); // Remove object with highest index -bool array_remove_at(struct Array* array, unsigned int index); - -void* array_begin(struct Array* array); -void* array_end(struct Array* array); - -void array_sort(struct Array* array, int (*compar)(const void*, const void*)); // Sort array by providing a comparator function - +/* Private use */ +void* array_new_(size_t object_size, int capacity); +void* array_grow_(void** array); +int array_reset_(void** array, int length); // Resize to length objects whose data is unspecified +int array_pop_(void** array); // Remove object with highest index +int array_remove_at_(void** array, int index); +void array_match_len_cap_(void** array); +void array_inc_cap_by_(void** array, int cap); + +/* Public Api */ +#define array_new(type) (type*) array_new_(sizeof(type), 0); // Use this for array creation +#define array_new_cap(type, capacity) (type*) array_new_(sizeof(type), capacity); // Use this for array with specific capacity +#define array_clear(array) array_reset(array, 0) +#define array_grow(array, type) (type*) array_grow_((void**)&array) +#define array_push(array, value, type) {type* new_val = array_grow(array, type); \ + *new_val = value;} +#define array_get_last(array, type) (type*) (&array[array_len(array)]) +#define array_set_last(array, val, type) {type* last = array_get_last(array, type); \ + *last = val} +#define array_pop(array) array_pop_((void**)&array) +#define array_remove_at(array, index) array_remove_at_((void**)&array, index); +#define array_reset(array, length) array_reset_((void**)&array, length); +#define array_match_len_cap(array) array_match_len_cap_((void**)&array); +#define array_inc_cap_by(array, cap) array_inc_cap_by_((void**)&array, cap); +int array_len(void* array); +int array_capacity(void* array); +void array_free(void* array); +/* TODO function to increase capacity of array by certain number similar to reserve in stl vector? */ #endif diff --git a/src/entity.c b/src/entity.c index 83bde72..55dcf17 100644 --- a/src/entity.c +++ b/src/entity.c @@ -7,8 +7,8 @@ #include #include -static struct Array* entity_list; -static struct Array* empty_indices; +static struct Entity* entity_list; +static int* empty_indices; void entity_initialize(void) @@ -19,7 +19,7 @@ void entity_initialize(void) void entity_cleanup(void) { - for(int i = 0; i < (int)entity_list->length; i++) + for(int i = 0; i < array_len(entity_list); i++) entity_remove(i); array_free(entity_list); @@ -28,7 +28,7 @@ void entity_cleanup(void) void entity_remove(int index) { - struct Entity* entity = array_get(entity_list, index); + struct Entity* entity = &entity_list[index]; for(int i = 0; i < MAX_COMPONENTS; i++) { @@ -54,16 +54,17 @@ struct Entity* entity_create(const char* name, const char* tag) { struct Entity* new_entity = NULL; int index = -1; - if(empty_indices->length > 0) + int empty_len = array_len(empty_indices); + if(empty_len > 0) { - index = array_get_last_val(empty_indices, int); + index = empty_indices[empty_len - 1]; array_pop(empty_indices); - new_entity = array_get(entity_list, index); + new_entity = &entity_list[index]; } else { - new_entity = array_add(entity_list); - index = entity_list->length - 1; + new_entity = array_grow(entity_list, struct Entity); + index = array_len(entity_list) - 1; } if(new_entity->name) free(new_entity->name); @@ -82,8 +83,8 @@ struct Entity* entity_create(const char* name, const char* tag) struct Entity* entity_get(int index) { struct Entity* entity = NULL; - if(index >= 0 && index < (int)entity_list->length) - entity = array_get(entity_list, index); + if(index >= 0 && index < array_len(entity_list)) + entity = &entity_list[index]; else log_error("entity:get", "Invalid index '%d'", index); return entity; @@ -93,9 +94,9 @@ struct Entity* entity_find(const char* name) { /* Bruteforce search all entities and return the first match */ struct Entity* entity = NULL; - for(int i = 0; i < (int)entity_list->length; i++) + for(int i = 0; i < array_len(entity_list); i++) { - struct Entity* curr_ent = array_get(entity_list, i); + struct Entity* curr_ent = &entity_list[i]; if(strcmp(curr_ent->name, name) == 0) { entity = curr_ent; diff --git a/src/game.c b/src/game.c index 049890c..5f60276 100644 --- a/src/game.c +++ b/src/game.c @@ -1,4 +1,6 @@ #include +#include +#include #include "GLFW/glfw3.h" #include "game.h" @@ -9,6 +11,7 @@ #include "file_io.h" #include "shader.h" #include "entity.h" +#include "array.h" void run(void); void update(void); @@ -34,7 +37,7 @@ void game_init(void) input_map_create("Test", keys3, 1); input_map_create("Test2", keys4, 1); - int shader = shader_create("phong.vert", "phong.frag"); + int shader = shader_create("phong.vert", "phong.frag"); run(); } diff --git a/src/geometry.c b/src/geometry.c index ef8f06d..bf21a14 100644 --- a/src/geometry.c +++ b/src/geometry.c @@ -17,28 +17,28 @@ struct Geometry { - 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; + 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; + vec3* vertices; + vec3* vertex_colors; + vec3* normals; + vec2* uvs; + uint* indices; /* BoundingBox boundingBox; */ /* BoundingSphere boundingSphere; */ }; /* Data */ -static struct Array* geometry_list; -static struct Array* empty_indices; +static struct Geometry* geometry_list; +static int* empty_indices; /* Function definitions */ bool load_from_file(struct Geometry* geometry, const char* filename); @@ -53,9 +53,9 @@ void geom_initialize(void) int geom_find(const char* filename) { int index = -1; - for(int i = 0; i < (int)geometry_list->length; i++) + for(int i = 0; i < array_len(geometry_list); i++) { - struct Geometry* geometry = array_get(geometry_list, i); + struct Geometry* geometry = &geometry_list[i]; if(strcmp(geometry->filename, filename) == 0) { index = i; @@ -74,20 +74,20 @@ int geom_create(const char* name) /* add new geometry object or overwrite existing one */ struct Geometry* new_geo = NULL; int index = -1; - if(empty_indices->length != 0) + int empty_len = array_len(empty_indices); + if(empty_len > 0) { - index = array_get_last_val(empty_indices, int); + index = empty_indices[empty_len - 1]; array_pop(empty_indices); - new_geo = array_get(geometry_list, index); + new_geo = &geometry_list[index]; } else { - new_geo = array_add(geometry_list); - index = geometry_list->length - 1; + new_geo = array_grow(geometry_list, struct Geometry); + index = array_len(geometry_list) - 1; } assert(new_geo); - if(load_from_file(new_geo, name)) { create_vao(new_geo); @@ -102,17 +102,16 @@ int geom_create(const char* name) } else { - struct Geometry* raw_geom_array = array_get_raw(geometry_list, struct Geometry); - raw_geom_array[index].ref_count++; + geometry_list[index].ref_count++; } return index; } void geom_remove(int index) { - if(index >= 0 && index < (int)geometry_list->length) + if(index >= 0 && index < array_len(geometry_list)) { - struct Geometry* geometry = array_get(geometry_list, index); + struct Geometry* geometry = &geometry_list[index]; array_free(geometry->indices); array_free(geometry->vertices); array_free(geometry->uvs); @@ -125,7 +124,7 @@ void geom_remove(int index) void geom_cleanup(void) { - for(uint i = 0; i < geometry_list->length; i++) + for(int i = 0; i < array_len(geometry_list); i++) geom_remove(i); array_free(geometry_list); @@ -161,16 +160,16 @@ bool load_from_file(struct Geometry* geometry, const char* filename) uint32 uvs_count = header[3]; // Indices geometry->indices = array_new_cap(uint, indices_count); - fread(geometry->indices->data, INDEX_SIZE, indices_count, file); + fread(geometry->indices, INDEX_SIZE, indices_count, file); // Vertices - geometry->vertices = array_new_cap(uint, vertices_count); - fread(geometry->vertices->data, VEC3_SIZE, vertices_count, file); + geometry->vertices = array_new_cap(vec3, vertices_count); + fread(geometry->vertices, VEC3_SIZE, vertices_count, file); // Normals - geometry->normals = array_new_cap(uint, normals_count); - fread(&geometry->normals->data, VEC3_SIZE, normals_count, file); + geometry->normals = array_new_cap(vec3, normals_count); + fread(geometry->normals, VEC3_SIZE, normals_count, file); // Uvs - geometry->uvs = array_new_cap(uint, uvs_count); - fread(&geometry->uvs->data, VEC2_SIZE, uvs_count, file); + geometry->uvs = array_new_cap(vec2, uvs_count); + fread(geometry->uvs, VEC2_SIZE, uvs_count, file); } fclose(file); geometry->filename = str_new(filename); @@ -195,59 +194,59 @@ void create_vao(struct Geometry* geometry) glGenBuffers(1, &geometry->vertex_vbo); glBindBuffer(GL_ARRAY_BUFFER, geometry->vertex_vbo); glBufferData(GL_ARRAY_BUFFER, - geometry->vertices->length * sizeof(vec3), - geometry->vertices->data, + array_len(geometry->vertices) * sizeof(vec3), + geometry->vertices, GL_STATIC_DRAW); renderer_check_glerror("Geometry::create_vbo::vertex"); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); - if(geometry->normals->length > 0) + if(array_len(geometry->normals) > 0) { glGenBuffers(1, &geometry->normal_vbo); glBindBuffer(GL_ARRAY_BUFFER, geometry->normal_vbo); glBufferData(GL_ARRAY_BUFFER, - geometry->normals->length * sizeof(vec3), - geometry->normals->data, + array_len(geometry->normals) * sizeof(vec3), + geometry->normals, GL_STATIC_DRAW); renderer_check_glerror("Geometry::create_vbo::normal"); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 3, GL_FLOAT, GL_TRUE, 0, 0); } - if(geometry->uvs->length > 0) + if(array_len(geometry->uvs) > 0) { glGenBuffers(1, &geometry->uv_vbo); glBindBuffer(GL_ARRAY_BUFFER, geometry->uv_vbo); glBufferData(GL_ARRAY_BUFFER, - geometry->uvs->length * sizeof(vec2), - geometry->uvs->data, + array_len(geometry->uvs) * sizeof(vec2), + geometry->uvs, GL_STATIC_DRAW); renderer_check_glerror("Geometry::create_vbo::uv"); glEnableVertexAttribArray(2); glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, 0); } - if(geometry->vertex_colors->length > 0) + if(array_len(geometry->vertex_colors) > 0) { glGenBuffers(1, &geometry->color_vbo); glBindBuffer(GL_ARRAY_BUFFER, geometry->color_vbo); glBufferData(GL_ARRAY_BUFFER, - geometry->vertex_colors->length * sizeof(vec3), - geometry->vertex_colors->data, + array_len(geometry->vertex_colors) * sizeof(vec3), + geometry->vertex_colors, GL_STATIC_DRAW); renderer_check_glerror("Geometry::create_vbo::color"); glEnableVertexAttribArray(3); glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 0, 0); } - if(geometry->indices->length > 0) + if(array_len(geometry->indices) > 0) { glGenBuffers(1, &geometry->index_vbo); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, geometry->index_vbo); glBufferData(GL_ELEMENT_ARRAY_BUFFER, - geometry->indices->length * sizeof(GLuint), - geometry->indices->data, + array_len(geometry->indices) * sizeof(GLuint), + geometry->indices, GL_STATIC_DRAW); geometry->draw_indexed = true; } diff --git a/src/input.c b/src/input.c index a63f993..4dca4f7 100644 --- a/src/input.c +++ b/src/input.c @@ -11,12 +11,19 @@ #define KS_INACTIVE -1; /* state for input map is set to KS_INACTIVE(KeyState_Inactive) when the key is neither pressed nor released */ +struct Input_Map +{ + const char* name; + int* keys; + int state; +}; + static void input_on_key(GLFWwindow* window, int key, int scancode, int action, int mods); static void input_on_mousebutton(GLFWwindow* window, int button, int action, int mods); static void input_on_cursor_move(GLFWwindow* window, double xpos, double ypos); static int map_find(const char* name); -static struct Array* input_map_list; +static struct Input_Map* input_map_list; void input_init(GLFWwindow* window) { @@ -29,9 +36,9 @@ void input_init(GLFWwindow* window) void input_cleanup(void) { - for(unsigned int i = 0; i < input_map_list->length; i++) + for(int i = 0; i < array_len(input_map_list); i++) { - struct Input_Map* map = array_get(input_map_list, i); + struct Input_Map* map = &input_map_list[i]; array_free(map->keys); } array_free(input_map_list); @@ -51,13 +58,12 @@ void input_cursor_pos_get(double* xpos, double* ypos) static void input_on_key(GLFWwindow* window, int key, int scancode, int action, int mods) { - for(unsigned int i = 0; i < input_map_list->length; i++) + for(int i = 0; i < array_len(input_map_list); i++) { - struct Input_Map* map = array_get(input_map_list, i); - for(unsigned int j = 0; j < map->keys->length; j++) + struct Input_Map* map = &input_map_list[i]; + for(int j = 0; j < array_len(map->keys); j++) { - int map_key = array_get_val(map->keys, int, j); - if(map_key == key) + if(map->keys[i] == key) { map->state = action; break; @@ -88,9 +94,9 @@ void input_cursor_mode_set(enum Cursor_Mode mode) 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++) + for(int i = 0; i < array_len(input_map_list); i++) { - struct Input_Map* map = array_get(input_map_list, i); + struct Input_Map* map = &input_map_list[i]; if(strcmp(map->name, map_name) == 0) { current_state = map->state; @@ -118,22 +124,21 @@ void input_map_create(const char* name, int* keys, size_t num_keys) { assert(name && keys && num_keys > 0); - struct Input_Map* new_map = array_add(input_map_list); + struct Input_Map* new_map = array_grow(input_map_list, struct Input_Map); new_map->name = name; new_map->keys = array_new(int); new_map->state = KS_INACTIVE; for(size_t i = 0; i < num_keys; i++) { - int* key = array_add(new_map->keys); - *key = keys[i]; + array_push(new_map->keys, keys[i], int); } } void input_update(void) { - for(unsigned int i = 0; i < input_map_list->length; i++) + for(int i = 0; i < array_len(input_map_list); i++) { - struct Input_Map* map = array_get(input_map_list, i); + struct Input_Map* map = &input_map_list[i]; if(map->state == GLFW_RELEASE) map->state = KS_INACTIVE; } @@ -146,7 +151,7 @@ bool input_map_remove(const char* name) int index = map_find(name); if(index > -1) { - array_remove_at(input_map_list, (unsigned int)index); + array_remove_at(input_map_list, (int)index); success = true; } if(!success) log_error("input:map_remove", "Map %s not found", name); @@ -161,17 +166,14 @@ bool input_map_keys_set(const char* name, int* keys, int num_keys) int index = map_find(name); if(index > -1) { - struct Input_Map* map = array_get(input_map_list, (unsigned int)index); + struct Input_Map* map = &input_map_list[index]; array_reset(map->keys, num_keys); for(int i = 0; i < num_keys; i++) - { - int* key = array_get(map->keys, (unsigned int)i); - *key = keys[i]; - } + map->keys[i] = keys[i]; success = true; } - if(!success) log_error("input:map_keys_set", "Map %s not found", name); - + if(!success) + log_error("input:map_keys_set", "Map %s not found", name); return success; } @@ -182,7 +184,7 @@ bool input_map_name_set(const char* name, const char* new_name) int index = map_find(name); if(index > -1) { - struct Input_Map* map = array_get(input_map_list, (unsigned int)index); + struct Input_Map* map = &input_map_list[index]; map->name = new_name; success = true; } @@ -193,9 +195,9 @@ bool input_map_name_set(const char* name, const char* new_name) static int map_find(const char* name) { int index = -1; - for(unsigned int i = 0; i < input_map_list->length; i++) + for(int i = 0; i < array_len(input_map_list); i++) { - struct Input_Map* map = array_get(input_map_list, i); + struct Input_Map* map = &input_map_list[i]; if(strcmp(name, map->name) == 0) { index = i; diff --git a/src/input.h b/src/input.h index 3311ac8..bee6868 100644 --- a/src/input.h +++ b/src/input.h @@ -5,7 +5,6 @@ #include typedef struct GLFWwindow GLFWwindow; -struct Array; enum Cursor_Mode { @@ -15,13 +14,6 @@ enum Cursor_Mode }; -struct 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); diff --git a/src/shader.c b/src/shader.c index 18a8706..5a3dce8 100644 --- a/src/shader.c +++ b/src/shader.c @@ -28,8 +28,8 @@ const int NORMAL_LOC = 1; const int UV_LOC = 2; const int COLOR_LOC = 3; -static struct Array* shader_list; -static struct Array* empty_indices; +static struct Shader_Object* shader_list; +static int* empty_indices; void debug_print_shader(const char* shaderText) { @@ -201,16 +201,17 @@ int shader_create(const char* vert_shader_name, const char* frag_shader_name) /* add new object or overwrite existing one */ Shader_Object* new_object = NULL; int index = -1; - if(empty_indices->length != 0) + int empty_len = array_len(empty_indices); + if(empty_len != 0) { - index = array_get_last_val(empty_indices, int); + index = empty_indices[empty_len - 1]; array_pop(empty_indices); - new_object = array_get(shader_list, index); + new_object = &shader_list[index]; } else { - new_object = array_add(shader_list); - index = shader_list->length - 1; + new_object = array_grow(shader_list, struct Shader_Object); + index = array_len(shader_list) - 1; } assert(new_object); new_object->vertex_shader = vert_shader; @@ -226,8 +227,7 @@ int shader_create(const char* vert_shader_name, const char* frag_shader_name) void shader_bind(const int shader_index) { - Shader_Object* shader_object = array_get(shader_list, shader_index); - glUseProgram(shader_object->program); + glUseProgram(shader_list[shader_index].program); } void shader_unbind(void) @@ -237,9 +237,7 @@ void shader_unbind(void) int get_uniform_location(const int shader_index, const char* name) { - Shader_Object shader_object = array_get_val(shader_list, Shader_Object, shader_index); - GLint handle = glGetUniformLocation(shader_object.program, name); - + GLint handle = glGetUniformLocation(shader_list[shader_index].program, name); if(handle == -1) log_error("shader:get_uniform_location", "Invalid uniform %s", name); @@ -290,7 +288,7 @@ void setUniformMat4(const int shader_index, const char* name, const mat4 value) void shader_remove(const int shader_index) { - Shader_Object* shader_object = array_get(shader_list, shader_index); + Shader_Object* shader_object = &shader_list[shader_index]; glDeleteProgram(shader_object->program); glDeleteShader(shader_object->vertex_shader); glDeleteShader(shader_object->fragment_shader); @@ -300,7 +298,7 @@ void shader_remove(const int shader_index) void shader_cleanup(void) { - for(int i = 0; i < (int)shader_list->length; i++) + for(int i = 0; i < array_len(shader_list); i++) shader_remove(i); array_free(shader_list); diff --git a/src/string_utils.c b/src/string_utils.c index cd5def9..ab94f6f 100644 --- a/src/string_utils.c +++ b/src/string_utils.c @@ -28,42 +28,42 @@ char* str_concat(char* string, const char* str_to_concat) char* str_replace(char* string, const char* pattern, const char* replacement) { - struct Array* indices = array_new(unsigned int); + int* indices = array_new(int); size_t string_len = strlen(string); /* Calculate size of new string and allocate new memory */ size_t pattern_len = strlen(pattern); size_t replacement_len = strlen(replacement); - bool done = false; + int done = 0; char* remaining_string = string; while(!done) { char* location = strstr(remaining_string, pattern); if(location) { - unsigned int index = location - string; - unsigned int* new_index = array_add(indices); - *new_index = index; + int index = location - string; + array_push(indices, index, int); remaining_string = location + pattern_len; /* Find the next occurance in the remaining string */ } else { - done = true; + done = 1; } } - - if(indices->length > 0) + + int num_indices = array_len(indices); + if(num_indices > 0) { - size_t string_len_without_pattern = string_len - (pattern_len * indices->length); - size_t new_string_len = string_len_without_pattern + (replacement_len * indices->length); + size_t string_len_without_pattern = string_len - (pattern_len * num_indices); + size_t new_string_len = string_len_without_pattern + (replacement_len * num_indices); char* new_string = malloc(new_string_len); if(new_string) { - done = false; - unsigned int count = 0; - unsigned int index = array_get_val(indices, unsigned int, count); + done = 0; + int count = 0; + int index = indices[count]; unsigned int prev_index = 0; while(!done) { @@ -76,15 +76,15 @@ char* str_replace(char* string, const char* pattern, const char* replacement) count++; prev_index = index + pattern_len; - if(count == indices->length) + if(count == array_len(indices)) { - done = true; + done = 1; source_beg = string + prev_index; strcat(new_string, source_beg); } else { - index = array_get_val(indices, unsigned int, count); + index = indices[count]; } } free(string); diff --git a/src/transform.c b/src/transform.c new file mode 100644 index 0000000..857beb4 --- /dev/null +++ b/src/transform.c @@ -0,0 +1,3 @@ +#include "transform.h" + + diff --git a/src/transform.h b/src/transform.h new file mode 100644 index 0000000..3c08b89 --- /dev/null +++ b/src/transform.h @@ -0,0 +1,17 @@ +#ifndef _transform_H +#define _transform_H + +#include "linmath.h" + +struct Transform +{ + vec3 position; + vec3 scale; + quat rotation; +}; + +void transform_initialize(void); +void transform_cleanup(void); +int transform_create(int node); + +#endif