#include #include #include "pxl8_io.h" pxl8_result pxl8_io_read_file(const char* path, char** content, size_t* size) { if (!path || !content || !size) return PXL8_ERROR_NULL_POINTER; FILE* file = fopen(path, "rb"); if (!file) { return PXL8_ERROR_FILE_NOT_FOUND; } fseek(file, 0, SEEK_END); long file_size = ftell(file); fseek(file, 0, SEEK_SET); if (file_size < 0) { fclose(file); return PXL8_ERROR_SYSTEM_FAILURE; } *content = SDL_malloc(file_size + 1); if (!*content) { fclose(file); return PXL8_ERROR_OUT_OF_MEMORY; } size_t bytes_read = fread(*content, 1, file_size, file); (*content)[bytes_read] = '\0'; *size = bytes_read; fclose(file); return PXL8_OK; } pxl8_result pxl8_io_write_file(const char* path, const char* content, size_t size) { if (!path || !content) return PXL8_ERROR_NULL_POINTER; FILE* file = fopen(path, "wb"); if (!file) { return PXL8_ERROR_SYSTEM_FAILURE; } size_t bytes_written = fwrite(content, 1, size, file); fclose(file); return (bytes_written == size) ? PXL8_OK : PXL8_ERROR_SYSTEM_FAILURE; } pxl8_result pxl8_io_read_binary_file(const char* path, u8** data, size_t* size) { return pxl8_io_read_file(path, (char**)data, size); } pxl8_result pxl8_io_write_binary_file(const char* path, const u8* data, size_t size) { return pxl8_io_write_file(path, (const char*)data, size); } bool pxl8_io_file_exists(const char* path) { if (!path) return false; struct stat st; return stat(path, &st) == 0; } f64 pxl8_io_get_file_modified_time(const char* path) { if (!path) return 0.0; struct stat st; if (stat(path, &st) == 0) { return st.st_mtime; } return 0.0; } pxl8_result pxl8_io_create_directory(const char* path) { if (!path) return PXL8_ERROR_NULL_POINTER; #ifdef _WIN32 if (mkdir(path) != 0) { #else if (mkdir(path, 0755) != 0) { #endif return PXL8_ERROR_SYSTEM_FAILURE; } return PXL8_OK; } void pxl8_io_free_file_content(char* content) { if (content) { SDL_free(content); } } void pxl8_io_free_binary_data(u8* data) { if (data) { SDL_free(data); } } static i32 pxl8_key_code(const char* key_name) { if (!key_name || !key_name[0]) return 0; SDL_Scancode scancode = SDL_GetScancodeFromName(key_name); return (i32)scancode; } bool pxl8_key_down(const pxl8_input_state* input, const char* key_name) { if (!input) return false; i32 key = pxl8_key_code(key_name); if (key < 0 || key >= 256) return false; return input->keys_down[key]; } bool pxl8_key_pressed(const pxl8_input_state* input, const char* key_name) { if (!input) return false; i32 key = pxl8_key_code(key_name); if (key < 0 || key >= 256) return false; return input->keys_pressed[key]; } bool pxl8_key_released(const pxl8_input_state* input, const char* key_name) { if (!input) return false; i32 key = pxl8_key_code(key_name); if (key < 0 || key >= 256) return false; return input->keys_released[key]; } i32 pxl8_mouse_wheel_x(const pxl8_input_state* input) { if (!input) return 0; return input->mouse_wheel_x; } i32 pxl8_mouse_wheel_y(const pxl8_input_state* input) { if (!input) return 0; return input->mouse_wheel_y; } i32 pxl8_mouse_x(const pxl8_input_state* input) { if (!input) return 0; return input->mouse_x; } i32 pxl8_mouse_y(const pxl8_input_state* input) { if (!input) return 0; return input->mouse_y; }