#include "pxl8_io.h" #include #include #include "pxl8_types.h" static inline char pxl8_to_lower(char c) { return (c >= 'A' && c <= 'Z') ? c + 32 : c; } 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 = 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) { free(content); } } void pxl8_io_free_binary_data(u8* data) { if (data) { free(data); } } static i32 pxl8_key_code(const char* key_name) { if (!key_name || !key_name[0]) return 0; typedef struct { const char* name; i32 code; } KeyMapping; static const KeyMapping keys[] = { {"a", 4}, {"b", 5}, {"c", 6}, {"d", 7}, {"e", 8}, {"f", 9}, {"g", 10}, {"h", 11}, {"i", 12}, {"j", 13}, {"k", 14}, {"l", 15}, {"m", 16}, {"n", 17}, {"o", 18}, {"p", 19}, {"q", 20}, {"r", 21}, {"s", 22}, {"t", 23}, {"u", 24}, {"v", 25}, {"w", 26}, {"x", 27}, {"y", 28}, {"z", 29}, {"1", 30}, {"2", 31}, {"3", 32}, {"4", 33}, {"5", 34}, {"6", 35}, {"7", 36}, {"8", 37}, {"9", 38}, {"0", 39}, {"return", 40}, {"escape", 41}, {"backspace", 42}, {"tab", 43}, {"space", 44}, {"-", 45}, {"=", 46}, {"left", 80}, {"right", 79}, {"up", 82}, {"down", 81}, {"f1", 58}, {"f2", 59}, {"f3", 60}, {"f4", 61}, {"f5", 62}, {"f6", 63}, {"f7", 64}, {"f8", 65}, {"f9", 66}, {"f10", 67}, {"f11", 68}, {"f12", 69}, {NULL, 0} }; char lower_name[64]; size_t i; for (i = 0; i < sizeof(lower_name) - 1 && key_name[i]; i++) { lower_name[i] = pxl8_to_lower(key_name[i]); } lower_name[i] = '\0'; for (i = 0; keys[i].name; i++) { if (strcmp(lower_name, keys[i].name) == 0) { return keys[i].code; } } return 0; } 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; }