diff options
Diffstat (limited to 'src/core')
| -rw-r--r-- | src/core/CMakeLists.txt | 18 | ||||
| -rw-r--r-- | src/core/include/engine/core/dltools.h | 23 | ||||
| -rw-r--r-- | src/core/include/engine/core/logging.h | 56 | ||||
| -rw-r--r-- | src/core/include/engine/core/memory.h | 31 | ||||
| -rw-r--r-- | src/core/include/engine/core/platform.h | 44 | ||||
| -rw-r--r-- | src/core/include/engine/core/state.h | 41 | ||||
| -rw-r--r-- | src/core/include/engine/core/types.h | 41 | ||||
| -rw-r--r-- | src/core/include/engine/engine.h | 57 | ||||
| -rw-r--r-- | src/core/src/dltools.c | 58 | ||||
| -rw-r--r-- | src/core/src/logging.c | 82 | ||||
| -rw-r--r-- | src/core/src/loop.c | 344 | ||||
| -rw-r--r-- | src/core/src/memory.c | 61 | ||||
| -rw-r--r-- | src/core/src/state.c | 251 |
13 files changed, 0 insertions, 1107 deletions
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt deleted file mode 100644 index a979cad..0000000 --- a/src/core/CMakeLists.txt +++ /dev/null @@ -1,18 +0,0 @@ -add_library(daw_core - src/dltools.c - src/logging.c - src/loop.c - src/memory.c - src/state.c - ${GLAD_HEADER} - ) - -#set_property(SOURCE src/loop.c APPEND PROPERTY OBJECT_DEPENDS ${GLAD_HEADER}) - -target_compile_options(daw_core PUBLIC ${BUILD_OPTS}) -target_include_directories(daw_core PRIVATE ${DAW_INCLUDE_DIRS}) -target_link_libraries(daw_core PRIVATE cglm) - -target_compile_definitions(daw_core PUBLIC - $<$<BOOL:${DAW_BUILD_HOTRELOAD}>:DAW_BUILD_HOTRELOAD> -) diff --git a/src/core/include/engine/core/dltools.h b/src/core/include/engine/core/dltools.h deleted file mode 100644 index d9c74ee..0000000 --- a/src/core/include/engine/core/dltools.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef DLTOOLS_H -#define DLTOOLS_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include <stdbool.h> - -/* Utility functions for handling runtime linked shared libraries */ -bool dynamic_library_close(void* shared_library); -void* dynamic_library_open(const char* library_path); -void* dynamic_library_reload(void* shared_library, const char* library_path); - -/* Returns the address of symbol in the provided shared_library handle. - * NULL on error*/ -void* dynamic_library_get_symbol(void* shared_library, const char* symbol); -char* dynamic_library_get_error(void); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/src/core/include/engine/core/logging.h b/src/core/include/engine/core/logging.h deleted file mode 100644 index cd55442..0000000 --- a/src/core/include/engine/core/logging.h +++ /dev/null @@ -1,56 +0,0 @@ -#ifndef LOGGING_H -#define LOGGING_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include <math.h> -#include <stdarg.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -#include <engine/core/types.h> - -#if defined __linux__ || defined __APPLE__ -#define TERM_COLOR_RESET "\033[0m" -#define TERM_COLOR_RED "\033[31m" -#define TERM_COLOR_GREEN "\033[32m" -#define TERM_COLOR_YELLOW "\033[33m" -#define TERM_COLOR_BLUE "\033[34m" -#define TERM_COLOR_PURPLE "\033[35m" -#else -#define TERM_COLOR_RESET -#define TERM_COLOR_RED -#define TERM_COLOR_GREEN -#define TERM_COLOR_YELLOW -#define TERM_COLOR_BLUE -#define TERM_COLOR_PURPLE -#endif - -#define STR(a) (#a) - -void _log(FILE* stream, const char* prefix, const char* fmt, va_list ap); - -void LOG(const char* fmt, ...); - -void INFO_(const char* fmt, ...); -void INFO(const char* fmt, ...); - -#ifdef _DEBUG -#define DEBUG(fmt, ...) __DEBUG(__FILE__, __LINE__, __func__, fmt, __VA_ARGS__) -void __DEBUG(const char* file, const i32 line, const char* func, - const char* fmt, ...); -#else -#define DEBUG(...) -#endif - -void WARN(const char* fmt, ...); - -void ERROR(const char* fmt, ...); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/src/core/include/engine/core/memory.h b/src/core/include/engine/core/memory.h deleted file mode 100644 index d04d58e..0000000 --- a/src/core/include/engine/core/memory.h +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef MEMORY_H -#define MEMORY_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include <engine/core/types.h> - -typedef struct memory { - void* data; - usize size; - usize pos; - usize free; -} memory; - -memory* memory_new(usize max_size); - -/* Returns a pointer to the allocated data */ -void* memory_allocate(memory* mem, usize size); - -memory memory_init(void* data, usize size); - -void memory_free(memory* mem, usize size); - -void memory_clear(memory* mem); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/src/core/include/engine/core/platform.h b/src/core/include/engine/core/platform.h deleted file mode 100644 index de39f71..0000000 --- a/src/core/include/engine/core/platform.h +++ /dev/null @@ -1,44 +0,0 @@ -#ifndef ENGINE_CORE_PLATFORM_H -#define ENGINE_CORE_PLATFORM_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include <cglm/ivec2.h> - -#include <engine/core/types.h> -#include <engine/core/memory.h> -#include <engine/rendering/window.h> -#include <engine/resources.h> - -#define NUM_GLOBAL_BINDINGS 1 - -/* Defines the internally used state of the engine. - * A single instance is created during `engine_init`, and used as a global - * variable (yeah, im sorry). Due to this design flaw the engine as a whole is - * not quite thread safe. - */ -typedef struct Instance { - - Window* window; - bool quit; - - u64 frame; - u16 fps_target; - - /* TODO: Move input ctx/bindings to window */ - /* TODO: Move cam to window->renderer */ - Camera *cam; - - /* Global resources that live from engine_init to engine_free */ - Resources* resources; - - memory* mem; - -} Instance; - -#ifdef __cplusplus -} -#endif -#endif diff --git a/src/core/include/engine/core/state.h b/src/core/include/engine/core/state.h deleted file mode 100644 index a6a5e84..0000000 --- a/src/core/include/engine/core/state.h +++ /dev/null @@ -1,41 +0,0 @@ -#ifndef STATE_H -#define STATE_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include <engine/core/memory.h> - -typedef enum StateType { - STATE_null, -#define State(name) STATE_##name, -#include <states/list_of_states.h> -#undef State - STATE_quit, -} StateType; - -extern const char* StateTypeStr[]; - -StateType (*State_updateFunc(StateType type))(f64, void*); - -void State_init(StateType type, memory* mem, void* arg); -void* State_free(StateType type, memory* mem); -StateType State_update(StateType type, f64 dt, memory* mem); - -/* Reloads shared object file associated with state */ -#ifdef DAW_BUILD_HOTRELOAD -#include <engine/ctrl/input.h> -bool State_reload(StateType type, i_ctx** ctx, usize ctx_len); -bool state_refresh_input_ctx(void* lib, i_ctx** ctx, usize ctx_len); - -#else -#define State_reload(_, _0, _1) true -#define state_refresh_input_ctx(_0, _1, _2) true - -#endif - -#ifdef __cplusplus -} -#endif -#endif diff --git a/src/core/include/engine/core/types.h b/src/core/include/engine/core/types.h deleted file mode 100644 index a7d794d..0000000 --- a/src/core/include/engine/core/types.h +++ /dev/null @@ -1,41 +0,0 @@ -#ifndef ENGINE_TYPES_H -#define ENGINE_TYPES_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include <stdbool.h> -#include <stdint.h> - -/* Signed */ -typedef int8_t i8; -typedef int16_t i16; -typedef int32_t i32; -typedef int64_t i64; - -/* Unsigned */ -typedef uint8_t u8; -typedef uint16_t u16; -typedef uint32_t u32; -typedef uint64_t u64; - -/* floating points */ -typedef float f32; -typedef double f64; - -/* sizes */ -#if __x86_64__ || __ppc64__ || _WIN64 -typedef u64 usize; -typedef i64 isize; -#else -typedef u32 usize; -typedef i32 isize; -#endif - -typedef bool(predicate_t)(const void*); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/src/core/include/engine/engine.h b/src/core/include/engine/engine.h deleted file mode 100644 index aa4eff6..0000000 --- a/src/core/include/engine/engine.h +++ /dev/null @@ -1,57 +0,0 @@ -#ifndef ENGINE_ENGINE_H -#define ENGINE_ENGINE_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include <stdbool.h> - -/* TODO: Cleanup the includes, ideally this header file should only include all - * "public-facing" headers. - */ - -#include <engine/core/types.h> -#include <engine/core/logging.h> -#include <engine/core/memory.h> -#include <engine/core/state.h> -#include <engine/ctrl/input.h> -#include <engine/utils/stack.h> - -typedef struct { - u32 texture_id; - i32 x, y, w, h; -} RenderUnit; - -#include <engine/rendering/window.h> -#include <engine/core/platform.h> - -#include <cglm/ivec2.h> - -/* Essential functions */ -Instance* engine_init(const char* windowtitle, i32 windowWidth, i32 windowHeight, - const u32 flags, - const usize initial_memory); - -i32 engine_run(Instance* p, StateType initial_state, void* state_arg); - -void engine_stop(Instance* p); - -/* Utility functions */ -void engine_fps_max(Instance* p, u16 cap); - -void render_set_zoom(f32 new_zoom); -void render_adjust_zoom(f32 diff); -void render_add_unit(RenderUnit* u); - -/* move this */ -void delay(uint64_t ms); - -// file operations -usize f_get_sz(FILE* f); - - -#ifdef __cplusplus -} -#endif -#endif diff --git a/src/core/src/dltools.c b/src/core/src/dltools.c deleted file mode 100644 index d27c4ff..0000000 --- a/src/core/src/dltools.c +++ /dev/null @@ -1,58 +0,0 @@ -#include <stdlib.h> -#include <stdbool.h> - -#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) -/* include winapi */ -#else -#include <dlfcn.h> -#endif - -#include <engine/core/dltools.h> -#include <engine/core/logging.h> - -bool dynamic_library_close(void* shared_library) { -#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) - return true; -#else - return dlclose(shared_library) == 0; -#endif -} - -void* dynamic_library_open(const char* library_path) { -#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) - return NULL; -#else - return dlopen(library_path, RTLD_NOW); -#endif -} - -char* dynamic_library_get_error(void) { -#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) - return "unsupported on windows"; -#else - return dlerror(); -#endif -} - -void* dynamic_library_reload(void* shared_library, const char* library_path) { - void* library_address = NULL; - if (!dynamic_library_close(shared_library)) { - ERROR("Failed to close shared library: %s", dynamic_library_get_error()); - ERROR("Reloading dynamic library failed."); - return library_address; - } - if ((library_address = dynamic_library_open(library_path)) == NULL) { - ERROR("Failed to open shared library: %s", dynamic_library_get_error()); - ERROR("Reloading dynamic library %s failed.", library_path); - } - return library_address; -} - -void* dynamic_library_get_symbol(void* restrict shared_library, - const char* symbol) { -#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) - return NULL; -#else - return dlsym(shared_library, symbol); -#endif -} diff --git a/src/core/src/logging.c b/src/core/src/logging.c deleted file mode 100644 index 94782ac..0000000 --- a/src/core/src/logging.c +++ /dev/null @@ -1,82 +0,0 @@ -#include <engine/core/logging.h> -#include <engine/core/types.h> -#include <stdlib.h> - -char* itoa(i32 x) { - const i32 size = (((i32)ceil(log10((f64)x))) + 1) * sizeof(char); - char* retval = (char*)malloc(size); - if (retval == NULL) { - perror("Failed to allocate memory for itoa"); - exit(EXIT_FAILURE); - } - sprintf(retval, "%d", x); - return retval; -} - -void _log(FILE* stream, const char* prefix, const char* fmt, va_list ap) { - if (stream == NULL) { - fprintf(stderr, "_log got NULL in stream argument\n"); - exit(EXIT_FAILURE); - } - fputs(prefix, stream); - vfprintf(stream, fmt, ap); -} - -void LOG(const char* fmt, ...) { - va_list ap; - va_start(ap, fmt); - _log(stdout, "[" TERM_COLOR_BLUE "LOG" TERM_COLOR_RESET "] ", fmt, ap); - va_end(ap); - puts(""); -} - -void INFO_(const char* fmt, ...) { - va_list ap; - va_start(ap, fmt); - _log(stdout, "[" TERM_COLOR_GREEN "INFO" TERM_COLOR_RESET "] ", fmt, ap); - va_end(ap); -} - -void INFO(const char* fmt, ...) { - va_list ap; - va_start(ap, fmt); - _log(stdout, "[" TERM_COLOR_GREEN "INFO" TERM_COLOR_RESET "] ", fmt, ap); - va_end(ap); - puts(""); -} - -void __DEBUG(const char* file, const i32 line, const char* func, - const char* fmt, ...) { - va_list ap; - - const usize prefix_len = 1024; - - char* prefix = malloc(sizeof(char) * 1024); - - snprintf(prefix, prefix_len, - "[" TERM_COLOR_YELLOW "DEBUG" TERM_COLOR_RESET "] " - "%s:%d <%s> ", - file, line, func); - - va_start(ap, fmt); - _log(stdout, prefix, fmt, ap); - va_end(ap); - - free(prefix); -} - -void WARN(const char* fmt, ...) { - va_list ap; - va_start(ap, fmt); - _log(stderr, "[" TERM_COLOR_PURPLE "WARN" TERM_COLOR_RESET "] ", fmt, ap); - va_end(ap); - puts(""); -} - -void ERROR(const char* fmt, ...) { - va_list ap; - va_start(ap, fmt); - _log(stderr, "[" TERM_COLOR_RED "ERROR" TERM_COLOR_RESET "] ", fmt, ap); - va_end(ap); - puts(""); -} diff --git a/src/core/src/loop.c b/src/core/src/loop.c deleted file mode 100644 index 4e9df36..0000000 --- a/src/core/src/loop.c +++ /dev/null @@ -1,344 +0,0 @@ -#include <time.h> -#include <errno.h> -#include <stdlib.h> -#include <string.h> -#include <stdbool.h> - -#include <cglm/ivec2.h> -#include <cglm/cam.h> - -#define STB_IMAGE_IMPLEMENTATION -#include <stb/stb_image.h> - -#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) -/* include winapi */ -#include <Windows.h> -#elif defined(__APPLE__) -/* mac includes */ -#elif defined(__linux) || defined(__linux__) || defined(linux) - -#include <unistd.h> -#include <sys/sysinfo.h> - -#endif - -#include <engine/configure.h> - -#include <engine/core/state.h> -#include <engine/engine.h> -#include <engine/utils/btree.h> -#include <engine/utils/hashmap.h> -#include <engine/utils/list.h> - -#include <engine/resources.h> - -#include <engine/rendering/window.h> -#include <engine/rendering/rendering.h> -#include <engine/rendering/platform_glfw.h> - - -#define DEFAULT_NUM_PROCS 8 - -Instance* GLOBAL_PLATFORM = NULL; - -static Camera default_camera = { - .pos = {3, 0, 0}, - .dir = {1, 1, 1}, -}; - -input_callback_t* callbacks[128]; -usize callbacks_len; - -i32 nproc(void) { - return get_nprocs(); -} - -void delay( uint64_t us ) -{ -#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) - Sleep( ms ); -#else - struct timespec ts = { - .tv_sec = (i64)us / 1000000, - .tv_nsec = ((i64)us * 1000) % 1000000000 - }; - struct timespec rem = {0,0}; - int err = 0; - while((err = clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, &rem))) { - switch (err) { - case EINTR: - WARN("clock_nanosleep didn't sleep full duration: interrupted."); - break; - case EINVAL: - WARN("clock_nanosleep didn't sleep full duration: invalid argument."); - break; - case ENOTSUP: - WARN("clock_nanosleep didn't sleep full duration: unsupported clock."); - break; - default: - WARN("clock_nanosleep didn't sleep full duration!"); - break; - } - ts = rem; - rem = (struct timespec){0,0}; - } -#endif -} - -i32 cmp_int(const void* a, const void* b) { - const i32* x = a; - const i32* y = b; - - return *x - *y; -} - -/* Creates the window, initializes IO, Rendering, Fonts and engine-specific - * resources. */ -Instance* engine_init(const char* windowtitle, i32 windowWidth, i32 windowHeight, - const u32 flags, - const usize initial_memory) { - - INFO("Engine version " ENGINE_VERSION); - - -#if defined(__linux) || defined(__linux__) || defined(linux) - { - pid_t pid = getpid(); - INFO("Starting with pid %lu", pid); - } -#endif - ivec2 windowsize = {windowWidth, windowHeight}; - - Instance* p = (Instance*)calloc(1, sizeof(Instance)); - Window* w = (Window*)calloc(1, sizeof(Window)); - - /* initialize resources */ - Resources* resources = calloc(1, sizeof(Resources)); - // TODO: Initialize them :) - - w = Window_new(windowtitle, - WINDOW_FRAMEWORK_GLFW, WINDOW_RENDERER_OPENGL, - (ivec2){windowsize[0], windowsize[1]}, flags); - - p->window = w; - p->quit = false; - - p->resources = resources; - - p->frame = 0; -#ifdef _DEBUG - engine_fps_max(p, 30); -#else - engine_fps_max(p, 300); -#endif - - p->mem = memory_new(initial_memory); - - p->cam = &default_camera; - - glm_ortho_default(45.f, p->cam->per); - - // TODO: Add global bindings - - INFO("Available cores: %d", nproc()); - - GLOBAL_PLATFORM = p; - -#ifdef DAW_BUILD_HOTRELOAD - -#define State(name) \ - if (!State_reload(STATE_##name, p->window->bindings, p->window->bindings_len)) { \ - ERROR("Failed to reload shared object file for state %s", #name); \ - }; - -#include <states/list_of_states.h> -#undef State - -#endif - - return p; -} - -i32 engine_run(Instance* p, StateType initial_state, void* state_arg) { - - if (p == NULL) { - ERROR("Platform is uninitialized.\n"); - INFO("initialize with `engine_init`"); - return -1; - } - - memory* mem = p->mem; - - StateType state = initial_state; - - { - u64 state_init_time = get_time(); - State_init(state, mem, state_arg); - INFO("Initializing state \"%s\" took %.1fns", StateTypeStr[state], - (get_time() - state_init_time)); - } - - u64 frame_end = get_time(); - - // Update ticks - u64 ticks = 0; - - StateType (*update_func)(f64, void*) = State_updateFunc(state); - - u64 last_fps_measurement = frame_end; - u64 last_fps_ticks = 0; - - /* The target frametime measured in μs */ - const u32 fps_cap = p->fps_target > 0 ? 1000000 / p->fps_target : 0; - - /* Main loop */ - do { - /* frame_start is μs since engine was initialized */ - const u64 frame_start = get_time(); - - /* dt measured in μs */ - const u64 dt = frame_start - frame_end; - - /* Delta is relative to FPS cap */ - //const f64 delta = (f64)dt / (f64)fps_cap; - - /* Events */ - Platform_GLFW.window_poll(); - i_flush_bindings(dt, callbacks_len, callbacks, mem->data); - - /* Update */ - StateType next_state; - next_state = update_func(dt, (void*)(mem->data)); - - if (next_state != STATE_null) { - if (next_state == STATE_quit) break; - - drawcall_reset(); - - void* retval = State_free(state, mem); - memory_clear(mem); - - i_ctx_reset(); - - // Reset camera to default camera - p->cam = &default_camera; - - state = next_state; - update_func = State_updateFunc(state); - { - u64 state_init_time = get_time(); - State_init(state, mem, retval); - INFO("Initializing state \"%s\" took %.1fns", StateTypeStr[state], - (get_time() - state_init_time)); - } - } else { - - /* Render */ - const u64 rendertime_begin = get_time(); - render_begin(p->window); - render_present(p->window); - const u64 rendertime_dt = get_time() - rendertime_begin; - - /* Regulate FPS */ - frame_end = get_time(); - const i64 fps_diff = fps_cap - (i64)(frame_end - frame_start); - - if (fps_diff > 0) { - delay((u64)fps_diff); - } - - frame_end = frame_start; - - /* Print stats */ - const u64 dt_measurement = frame_end - last_fps_measurement; - - /* only make measurements once a second */ - if (dt_measurement > 1000000) { - - printf(" FPS: %.1f" - "\tticks: %lu" - "\tframetime: %lu.%03lums" - "\trendertime: %lu.%03lums" - "\n" - , ((f64)(ticks - last_fps_ticks)) / ((f64)dt_measurement / 1000000.0) - , ticks - , dt / 1000 , dt % 1000 - , rendertime_dt / 1000 , rendertime_dt % 1000 - ); - - last_fps_measurement = frame_end; - last_fps_ticks = ticks; - } - } - - ticks++; - } while( - !Platform_GLFW.window_should_close(p->window) - && state != STATE_quit - ); - - return 0; -} - -void engine_stop(Instance* p) { - if (p == NULL) return; - - //{ /* Deallocate resources */ - // struct Resources* r = (struct Resources*)p->data; - // if (r != NULL) { - // /* Destroy textures */ - // for (usize i = 0; i < r->textures_len; i++) { - // if (r->textures[i] != NULL) { - // r->textures[i] = NULL; - // } - // } - // free(r->textures); - - // /* Destroy Fonts */ - // } - //} - - Platform_GLFW.window_destroy(p->window); - free(p->resources); -} - -/* Set the maximum framerate */ -void engine_fps_max(Instance* p, u16 cap) { - LOG("Setting max fps to %llu", cap); - p->fps_target = cap; -} - -usize f_get_sz(FILE* f) { - if (f == NULL) { - ERROR("File was null!"); - return 0; - } - - const isize pos = ftell(f); - if (pos == -1) { - ERROR("Failed to determine file size (%d): %s", errno, strerror(errno)); - return 0; - } - - const i32 err = fseek(f, 0, SEEK_END); - if (err != 0) { - if (err == ESPIPE) { - ERROR("File is a pipe"); - } else { - ERROR("Failed to determine file size!"); - } - return 0; - } - - const isize size = ftell(f); - - if (size == -1) { - ERROR("Failed to determine file size (%d): %s", errno, strerror(errno)); - return 0; - } - - // Reset the position to the position prior to calling f_get_sz - fseek(f, pos, SEEK_SET); - - return (usize)size; -} diff --git a/src/core/src/memory.c b/src/core/src/memory.c deleted file mode 100644 index 7a51a0d..0000000 --- a/src/core/src/memory.c +++ /dev/null @@ -1,61 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -#include <engine/core/logging.h> -#include <engine/core/memory.h> - -memory* memory_new(usize max_size) { - memory* m = malloc(sizeof(memory)); - m->data = malloc(max_size); - m->size = max_size; - m->pos = 0; - m->free = max_size; - - memset(m->data, 0, max_size); - - return m; -} - -/* Returns a pointer to the allocated data */ -void* memory_allocate(memory* mem, usize size) { - void* data = NULL; - - if (mem->pos + size <= mem->size) { - data = (void*)((usize)mem->data + mem->pos); - mem->pos += size; - mem->free -= size; - } else { - ERROR("Trying to allocate %lu in a %lu sized memory block", size, - mem->size); - ERROR("No more room!"); - exit(EXIT_FAILURE); - } - - return data; -} - -memory memory_init(void* data, usize size) { - memory m = {0}; - m.data = data; - m.size = size; - m.free = 0; - return m; -} - -void memory_free(memory* mem, usize size) { - if (size > mem->pos) { - perror("Freeing too much memory!"); - exit(EXIT_FAILURE); - } else { - mem->pos -= size; - mem->free += size; - } -} - -void memory_clear(memory* mem) { - mem->pos = 0; - mem->free = mem->size; - /* Reset the memory? */ - memset(mem->data, 0, mem->size); -} diff --git a/src/core/src/state.c b/src/core/src/state.c deleted file mode 100644 index a206cbb..0000000 --- a/src/core/src/state.c +++ /dev/null @@ -1,251 +0,0 @@ -#include <string.h> - -#include <engine/core/dltools.h> -#include <engine/core/logging.h> -#include <engine/core/state.h> -#include <engine/ctrl/input.h> - -//typedef void state_init_t(void*,void*); -//typedef void* (state_free_t(void*)); -typedef StateType state_update_t(f64, void*); - -const char* StateTypeStr[] = { - "null", -#define State(name) #name, -#include <states/list_of_states.h> -#undef State - "quit", -}; - -// Setup API for states -#define State(name) \ - typedef struct name##_state name##_state; \ - typedef void(state_##name##_init_t)(name##_state*,void*); \ - typedef void*(state_##name##_free_t)(name##_state*); \ - typedef StateType(state_##name##_update_t)(name##_state*); -#include <states/list_of_states.h> -#undef State - -#ifdef DAW_BUILD_HOTRELOAD - -// When hotreloading is enabled, we want to assign state function pointers -// dynamically. -#define State(name) \ - state_##name##_init_t* name##_init = NULL; \ - state_##name##_free_t* name##_free = NULL; \ - state_##name##_update_t* name##_update = NULL; \ - \ - void* libstate_##name = NULL; \ - const char* libstate_##name##_str = "lib" #name ".so"; -#else - -// Otherwise we just declare them. -#define State(name) \ - extern state_##name##_init_t name##_init; \ - extern state_##name##_free_t name##_free; \ - extern state_##name##_update_t name##_update; -#endif - -#include <states/list_of_states.h> -#undef State - -#include <states/all_states.h> - -void State_init(StateType type, memory* mem, void* arg) { - switch (type) { -#define State(name) \ - case (STATE_##name): { \ - name##_init(memory_allocate(mem, sizeof(name##_state)), arg); \ - break; \ - } -#include <states/list_of_states.h> -#undef State - case STATE_null: - case STATE_quit: - DEBUG("Got %s state.\n", StateTypeStr[type]); - break; - default: - exit(EXIT_FAILURE); - } -} - -void* State_free(StateType type, memory* mem) { - void* state_retval = NULL; - switch (type) { -#define State(name) \ - case (STATE_##name): { \ - state_retval = name##_free(mem->data); \ - break; \ - } -#include <states/list_of_states.h> -#undef State - case STATE_null: - case STATE_quit: - DEBUG("Got %s state.\n", StateTypeStr[type]); - break; - default: - exit(EXIT_FAILURE); - } - memory_clear(mem); - return state_retval; -} - -/* Returns the update function of a given state type */ -StateType (*State_updateFunc(StateType type))(f64, void*) { - switch (type) { -#ifdef DAW_BUILD_HOTRELOAD -#define State(name) \ - case (STATE_##name): { \ - return (state_update_t*)name##_update; \ - break; \ - } -#else -#define State(name) \ - case (STATE_##name): { \ - return (state_update_t*)&name##_update; \ - break; \ - } -#endif -#include <states/list_of_states.h> -#undef State - case STATE_null: - case STATE_quit: - return NULL; // DEBUG("Got %s state.\n", StateTypeStr[type]); - break; - default: - exit(EXIT_FAILURE); - } - return NULL; -} - -StateType State_update(StateType type, f64 dt, memory* mem) { - StateType next_state = STATE_null; - switch (type) { -#define State(name) \ - case (STATE_##name): { \ - next_state = name##_update(mem->data); \ - break; \ - } -#include <states/list_of_states.h> -#undef State - case STATE_null: - case STATE_quit: - DEBUG("Got %s state.\n", StateTypeStr[type]); - break; - default: - exit(EXIT_FAILURE); - } - return next_state; -} - -#ifdef DAW_BUILD_HOTRELOAD -bool State_reload(StateType type, i_ctx** ctx, usize ctx_len) { - void* libptr = NULL; - - switch (type) { -#define State(name) \ - case (STATE_##name): { \ - if (libstate_##name == NULL) { \ - libstate_##name = dynamic_library_open(libstate_##name##_str); \ - } else { \ - libstate_##name = \ - dynamic_library_reload(libstate_##name, libstate_##name##_str); \ - } \ - if (libstate_##name == NULL) { \ - ERROR("Failed loading shared object: %s (%s)", libstate_##name##_str, \ - dynamic_library_get_error()); \ - return false; \ - } \ - \ - name##_init = (state_##name##_init_t*)dynamic_library_get_symbol( \ - libstate_##name, STR(name##_init)); \ - name##_free = (state_##name##_free_t*)dynamic_library_get_symbol( \ - libstate_##name, STR(name##_free)); \ - name##_update = (state_##name##_update_t*)dynamic_library_get_symbol( \ - libstate_##name, STR(name##_update)); \ - libptr = libstate_##name; \ - break; \ - } -#include <states/list_of_states.h> -#undef State - case STATE_null: - case STATE_quit: - ERROR("Invalid state"); - DEBUG("Got %s state.\n", StateTypeStr[type]); - return false; - default: - exit(EXIT_FAILURE); - } - return state_refresh_input_ctx(libptr, ctx, ctx_len); -} - - -bool state_refresh_input_ctx(void* lib, i_ctx** ctx, usize ctx_len) { - if (ctx == NULL) return true; - if (ctx_len > 0 && ctx[0] == NULL) return false; - if (lib == NULL) return false; - - for (usize c = 0; c < ctx_len; c++) { - LOG("ctx[%d]->len = %d", c, ctx[c]->len); - for (isize b = 0; b < ctx[c]->len; b++) { - switch (ctx[c]->bindings[b].action.type) { - case InputType_error: - break; - case InputType_action: - if (strcmp("NULL", ctx[c]->bindings[b].action.action.callback_str) != - 0) { - - ctx[c]->bindings[b].action.action.callback = - (input_callback_t*)dynamic_library_get_symbol( - lib, ctx[c]->bindings[b].action.action.callback_str); - - if (ctx[c]->bindings[b].action.action.callback == NULL) { - ERROR("Failed to get binding for %s: %s", - ctx[c]->bindings[b].action.action.callback_str, - dynamic_library_get_error()); - return false; - } - } - break; - case InputType_state: - if (strcmp("NULL", ctx[c]->bindings[b].action.state.activate_str) != - 0) { - - ctx[c]->bindings[b].action.state.activate = - (input_callback_t*)dynamic_library_get_symbol( - lib, ctx[c]->bindings[b].action.state.activate_str); - - if (ctx[c]->bindings[b].action.state.activate == NULL) { - ERROR("Failed to get binding for %s: %s", - ctx[c]->bindings[b].action.state.activate_str, - dynamic_library_get_error()); - return false; - } - } - - if (strcmp("NULL", ctx[c]->bindings[b].action.state.deactivate_str) != - 0) { - - ctx[c]->bindings[b].action.state.deactivate = - (input_callback_t*)dynamic_library_get_symbol( - lib, ctx[c]->bindings[b].action.state.deactivate_str); - - if (ctx[c]->bindings[b].action.state.deactivate == NULL) { - ERROR("Failed to get binding for %s: %s", - ctx[c]->bindings[b].action.state.deactivate_str, - dynamic_library_get_error()); - return false; - } - } - break; - case InputType_range: - default: - break; - } - } - } - - return true; -} - -#endif |
