Skip to content

Commit

Permalink
More updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chukobyte committed Mar 24, 2024
1 parent 393f8bb commit 853254a
Show file tree
Hide file tree
Showing 22 changed files with 1,825 additions and 55 deletions.
57 changes: 11 additions & 46 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,56 +16,21 @@ else ()
endif ()

add_library(${PROJECT_NAME} STATIC
seika/input/input.c
seika/seika.c
seika/platform.c
seika/string.c
seika/math/math.c
seika/memory.c

# seika/seika.c
# seika/asset/asset_manager.c
# seika/asset/asset_file_loader.c
# seika/memory/se_mem.c
# seika/math/se_curve_float.c
# seika/math/se_math.c
# seika/input/input.c
# seika/input/input_action.c
# seika/input/mouse.c
# seika/networking/se_network.c
# seika/networking/se_network_socket.c
# seika/rendering/font.c
# seika/rendering/frame_buffer.c
# seika/rendering/renderer.c
# seika/rendering/render_context.c
# seika/rendering/shader/shader.c
# seika/rendering/shader/shader_cache.c
# seika/rendering/shader/shader_file_parser.c
# seika/rendering/shader/shader_instance.c
# seika/rendering/texture.c
# seika/audio/audio_manager.c
# seika/audio/audio.c
# seika/thread/se_pthread.c
# seika/thread/se_thread_pool.c
# seika/utils/command_line_args_util.c
# seika/utils/logger.c
# seika/utils/se_file_system_utils.c
# seika/utils/se_platform.c
# seika/utils/se_string_util.c
# seika/utils/observer.c
# seika/data_structures/ska_array2d.c
# seika/data_structures/ska_linked_list.c
# seika/data_structures/se_array_utils.c
# seika/data_structures/ska_array_list.c
# seika/data_structures/se_queue.c
# seika/data_structures/se_hash_map.c
# seika/data_structures/se_hash_map_string.c
# seika/data_structures/se_spatial_hash_map.c
# seika/data_structures/se_tile_map.c
# seika/ecs/ecs.c
# seika/ecs/component.c
# seika/ecs/entity.c
# seika/ecs/ec_system.c
seika/input/input.c
seika/math/math.c
seika/data_structures/array2d.c
seika/data_structures/array_list.c
seika/data_structures/array_utils.c
seika/data_structures/hash_map.c
seika/data_structures/hash_map_string.c
seika/data_structures/linked_list.c
seika/data_structures/queue.c
seika/data_structures/spatial_hash_map.c
seika/data_structures/tile_map.c
)

target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
Expand Down
87 changes: 87 additions & 0 deletions seika/data_structures/array2d.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#include "array2d.h"

#include <string.h>

#include "seika/memory.h"
#include "seika/assert.h"

#define SKA_ARRAY2D_IS_VALID_COORD(ARRAY2D, ROW, COL) (!((int32)ROW >= ARRAY2D->size.w || (int32)COL >= ARRAY2D->size.h))

SkaArray2D* ska_array2d_create(size_t rows, size_t cols, size_t elementSize) {
SkaArray2D* newArray = SKA_MEM_ALLOCATE(SkaArray2D);
newArray->data = SKA_MEM_ALLOCATE_SIZE(cols * sizeof(void*));
for (size_t i = 0; i < cols; i++) {
newArray->data[i] = SKA_MEM_ALLOCATE_SIZE(rows * elementSize);
}
newArray->size = (SkaSize2Di){ .w = (int32)rows, .h = (int32)cols };
newArray->elementSize = elementSize;
return newArray;
}

void ska_array2d_destroy(SkaArray2D* array2d) {
for (int32 i = 0; i < array2d->size.h; i++) {
SKA_MEM_FREE(array2d->data[i]);
}
SKA_MEM_FREE(array2d->data);
SKA_MEM_FREE(array2d);
}

void* ska_array2d_get(SkaArray2D* array2d, size_t x, size_t y) {
if (!SKA_ARRAY2D_IS_VALID_COORD(array2d, x, y)) {
return NULL;
}
return (void*)((char*)array2d->data[y] + x * array2d->elementSize);
}

bool ska_array2d_set(SkaArray2D* array2d, size_t x, size_t y, void* newValue) {
if (!SKA_ARRAY2D_IS_VALID_COORD(array2d, x, y)) {
return false;
}
memcpy((void*)((char*)array2d->data[y] + x * array2d->elementSize), newValue, array2d->elementSize);
return true;
}

void ska_array2d_resize(SkaArray2D* array2d, size_t newX, size_t newY) {
// Reallocate memory for data array
const size_t width = SKA_MATH_MAX(newX, 1);
const size_t height = SKA_MATH_MAX(newY, 1);

array2d->data = ska_mem_reallocate(array2d->data, height * sizeof(void*));
for (size_t i = 0; i < height; i++) {
if (i < array2d->size.h) {
// Resize existing rows
array2d->data[i] = ska_mem_reallocate(array2d->data[i], width * array2d->elementSize);
} else {
// Allocate memory for new rows
array2d->data[i] = SKA_MEM_ALLOCATE_SIZE(width * array2d->elementSize);
}
}

array2d->size = (SkaSize2Di){ .w = (int32)newX, .h = (int32)newY };

if (newX == 0 && newY == 0) {
ska_array2d_reset(array2d);
}
}

void ska_array2d_clear(SkaArray2D* array2d) {
ska_array2d_resize(array2d, 0, 0);
}

void ska_array2d_reset(SkaArray2D* array2d) {
ska_array2d_reset_default(array2d, NULL);
}

void ska_array2d_reset_default(SkaArray2D* array2d, void* defaultValue) {
if (defaultValue) {
for (int32 y = 0; y < array2d->size.h; y++) {
for (int32 x = 0; x < array2d->size.w; x++) {
ska_array2d_set(array2d, x, y, defaultValue);
}
}
} else {
for (int32 i = 0; i < array2d->size.h; i++) {
memset(array2d->data[i], 0, array2d->size.w * array2d->elementSize);
}
}
}
20 changes: 20 additions & 0 deletions seika/data_structures/array2d.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include "seika/math/math.h"

typedef struct SkaArray2D {
void** data;
SkaSize2Di size; // w = rows, h = columns
size_t elementSize;
} SkaArray2D;

SkaArray2D* ska_array2d_create(size_t rows, size_t cols, size_t elementSize);
void ska_array2d_destroy(SkaArray2D* array2d);
void* ska_array2d_get(SkaArray2D* array2d, size_t x, size_t y);
bool ska_array2d_set(SkaArray2D* array2d, size_t x, size_t y, void* newValue);
void ska_array2d_resize(SkaArray2D* array2d, size_t newX, size_t newY);
void ska_array2d_clear(SkaArray2D* array2d);
// Maintains elements in the arrays but zeroes out data
void ska_array2d_reset(SkaArray2D* array2d);
// Maintains elements in the arrays but copy memory from the passed in default value
void ska_array2d_reset_default(SkaArray2D* array2d, void* defaultValue);
77 changes: 77 additions & 0 deletions seika/data_structures/array_list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include "array_list.h"

#include <string.h>

#include "seika/memory.h"
#include "seika/assert.h"

#define SKA_ARRAY_LIST_DEFAULT_CAPACITY 16

SkaArrayList* ska_array_list_create_default_capacity(size_t valueSize) {
return ska_array_list_create(valueSize, SKA_ARRAY_LIST_DEFAULT_CAPACITY);
}

SkaArrayList* ska_array_list_create(size_t valueSize, size_t initialCapacity) {
SkaArrayList* newList = SKA_MEM_ALLOCATE(SkaArrayList);
newList->data = SKA_MEM_ALLOCATE_SIZE(initialCapacity * valueSize);
newList->valueSize = valueSize;
newList->capacity = initialCapacity;
newList->initialCapacity = initialCapacity;
return newList;
}

void ska_array_list_destroy(SkaArrayList* list) {
SKA_MEM_FREE(list->data);
SKA_MEM_FREE(list);
}

void ska_array_list_push_back(SkaArrayList* list, const void* value) {
if (list->size >= list->capacity) {
list->capacity *= 2;
list->data = ska_mem_reallocate(list->data, list->capacity * list->valueSize);
}
memcpy((char*)list->data + list->size * list->valueSize, value, list->valueSize);
list->size++;
}

void* ska_array_list_get(SkaArrayList* list, size_t index) {
SKA_ASSERT_FMT(index < list->size, "Attempting to access out of bounds index '%d", index);
return (char*)list->data + index * list->valueSize;
}

bool ska_array_list_remove(SkaArrayList* list, const void* value) {
size_t index = 0;
while (index < list->size) {
if (memcmp((char*)list->data + index * list->valueSize, value, list->valueSize) == 0) {
// Found the element, remove it
for (size_t i = index + 1; i < list->size; ++i) {
memcpy((char*)list->data + (i - 1) * list->valueSize, (char*)list->data + i * list->valueSize, list->valueSize);
}
list->size--;
return true;
} else {
index++;
}
}
return false;
}

bool ska_array_list_remove_by_index(SkaArrayList* list, size_t index) {
if (index < list->size) {
// Shift elements after the removed element
for (size_t i = index + 1; i < list->size; ++i) {
memcpy((char*)list->data + (i - 1) * list->valueSize, (char*)list->data + i * list->valueSize, list->valueSize);
}
list->size--;
return true;
}
return false;
}

bool ska_array_list_is_empty(SkaArrayList *list) {
return list->size == 0;
}

void ska_array_list_clear(SkaArrayList *list) {
list->size = 0;
}
33 changes: 33 additions & 0 deletions seika/data_structures/array_list.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#include <stddef.h>
#include <stdbool.h>

#define SKA_ARRAY_LIST_GET_ARRAY(LIST, TYPE) ((TYPE*)((LIST)->data))
#define SKA_ARRAY_LIST_FOR_EACH(LIST, TYPE, VALUE) for(TYPE* VALUE = SKA_ARRAY_LIST_GET_ARRAY(LIST, TYPE), *VALUE##_end = VALUE + (LIST)->size; VALUE < VALUE##_end; VALUE++)

// Generic array list, use 'ska_array_list_create' to allocate on the heap
typedef struct SkaArrayList {
void* data;
size_t valueSize; // Size of each element of the data
size_t size; // How many elements are in the array
size_t capacity; // Max elements allowed in array before resizing
size_t initialCapacity; // Min capacity for the array list
} SkaArrayList;

// Will create a new array list with all it's element values set to the passed in 'valueSize'
SkaArrayList* ska_array_list_create_default_capacity(size_t valueSize);
SkaArrayList* ska_array_list_create(size_t valueSize, size_t initialCapacity);
// Will complete destroy the array list freeing up all its memory
void ska_array_list_destroy(SkaArrayList* list);
// Inserts an item at the end of the list
void ska_array_list_push_back(SkaArrayList* list, const void* value);
// Returns an item from the list from specified index
void* ska_array_list_get(SkaArrayList* list, size_t index);
// Removes the first item from the list who is equal to the passed in value
bool ska_array_list_remove(SkaArrayList* list, const void* value);
bool ska_array_list_remove_by_index(SkaArrayList* list, size_t index);
// Returns true if the array list size == 0
bool ska_array_list_is_empty(SkaArrayList *list);
// Will remove all items from the array list
void ska_array_list_clear(SkaArrayList *list);
32 changes: 32 additions & 0 deletions seika/data_structures/array_utils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "array_utils.h"

static inline void ska_array_utils_swap(int32* xp, int32* yp) {
int32 temp = *xp;
*xp = *yp;
*yp = temp;
}

// Function to perform Selection Sort
void ska_array_utils_selection_sort_int(int32 arr[], int32 arraySize) {
int32 min_idx;

// One by one move boundary of unsorted subarray
for (int32 i = 0; i < arraySize - 1; i++) {

// Find the minimum element in unsorted array
min_idx = i;
for (int32 j = i + 1; j < arraySize; j++) {
if (arr[j] < arr[min_idx]) {
min_idx = j;
}
}

// Swap the found minimum element
// with the first element
ska_array_utils_swap(&arr[min_idx], &arr[i]);
}
}

void ska_array_utils_remove_item_uint32(uint32 array[], size_t* size, uint32 item, uint32 emptyValue) {
SKA_ARRAY_UTILS_REMOVE_ARRAY_ITEM(array, *size, item, emptyValue);
}
26 changes: 26 additions & 0 deletions seika/data_structures/array_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include <stddef.h>

#include "seika/defines.h"

#define SKA_ARRAY_UTILS_REMOVE_ARRAY_ITEM(ARRAY, ARRAY_SIZE, ELEMENT, EMPTY_VALUE) \
{ \
const size_t temp_arac_size = (ARRAY_SIZE); \
for (size_t temp_arac_loop_index = 0; temp_arac_loop_index < temp_arac_size; temp_arac_loop_index++) { \
if (ARRAY[temp_arac_loop_index] == ELEMENT) { \
ARRAY[temp_arac_loop_index] = ARRAY[temp_arac_loop_index + 1]; \
ARRAY[temp_arac_loop_index + 1] = EMPTY_VALUE; \
(ARRAY_SIZE)--; \
} \
if (ARRAY[temp_arac_loop_index] == EMPTY_VALUE && temp_arac_loop_index + 1 < temp_arac_size) { \
ARRAY[temp_arac_loop_index] = ARRAY[temp_arac_loop_index + 1]; \
ARRAY[temp_arac_loop_index + 1] = EMPTY_VALUE; \
} \
} \
}

// Function to perform Selection Sort
void ska_array_utils_selection_sort_int(int32 arr[], int32 arraySize);

void ska_array_utils_remove_item_uint32(uint32 array[], size_t* size, uint32 item, uint32 emptyValue);
Loading

0 comments on commit 853254a

Please sign in to comment.