114 lines
3.4 KiB
C
114 lines
3.4 KiB
C
#pragma once
|
|
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <sys/stat.h>
|
|
|
|
#include "pxl8_types.h"
|
|
|
|
typedef struct {
|
|
const u8* bytes;
|
|
u32 offset;
|
|
u32 size;
|
|
} pxl8_stream;
|
|
|
|
static inline pxl8_stream pxl8_stream_create(const u8* bytes, u32 size) {
|
|
return (pxl8_stream){
|
|
.bytes = bytes,
|
|
.offset = 0,
|
|
.size = size
|
|
};
|
|
}
|
|
|
|
static inline bool pxl8_stream_can_read(const pxl8_stream* stream, u32 count) {
|
|
return stream->offset + count <= stream->size;
|
|
}
|
|
|
|
static inline void pxl8_stream_seek(pxl8_stream* stream, u32 offset) {
|
|
stream->offset = offset;
|
|
}
|
|
|
|
static inline u32 pxl8_stream_position(const pxl8_stream* stream) {
|
|
return stream->offset;
|
|
}
|
|
|
|
static inline u8 pxl8_read_u8(pxl8_stream* stream) {
|
|
return stream->bytes[stream->offset++];
|
|
}
|
|
|
|
static inline u16 pxl8_read_u16(pxl8_stream* stream) {
|
|
u16 val = (u16)stream->bytes[stream->offset] | ((u16)stream->bytes[stream->offset + 1] << 8);
|
|
stream->offset += 2;
|
|
return val;
|
|
}
|
|
|
|
static inline u32 pxl8_read_u32(pxl8_stream* stream) {
|
|
u32 val = (u32)stream->bytes[stream->offset] |
|
|
((u32)stream->bytes[stream->offset + 1] << 8) |
|
|
((u32)stream->bytes[stream->offset + 2] << 16) |
|
|
((u32)stream->bytes[stream->offset + 3] << 24);
|
|
stream->offset += 4;
|
|
return val;
|
|
}
|
|
|
|
static inline i16 pxl8_read_i16(pxl8_stream* stream) {
|
|
return (i16)pxl8_read_u16(stream);
|
|
}
|
|
|
|
static inline i32 pxl8_read_i32(pxl8_stream* stream) {
|
|
return (i32)pxl8_read_u32(stream);
|
|
}
|
|
|
|
static inline f32 pxl8_read_f32(pxl8_stream* stream) {
|
|
u32 val = pxl8_read_u32(stream);
|
|
f32 result;
|
|
memcpy(&result, &val, sizeof(f32));
|
|
return result;
|
|
}
|
|
|
|
static inline void pxl8_read_bytes(pxl8_stream* stream, void* dest, u32 count) {
|
|
for (u32 i = 0; i < count; i++) {
|
|
((u8*)dest)[i] = stream->bytes[stream->offset++];
|
|
}
|
|
}
|
|
|
|
static inline void pxl8_skip_bytes(pxl8_stream* stream, u32 count) {
|
|
stream->offset += count;
|
|
}
|
|
|
|
static inline const u8* pxl8_read_ptr(pxl8_stream* stream, u32 count) {
|
|
const u8* ptr = &stream->bytes[stream->offset];
|
|
stream->offset += count;
|
|
return ptr;
|
|
}
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
pxl8_result pxl8_io_create_directory(const char* path);
|
|
bool pxl8_io_file_exists(const char* path);
|
|
void pxl8_io_free_binary_data(u8* data);
|
|
void pxl8_io_free_file_content(char* content);
|
|
f64 pxl8_io_get_file_modified_time(const char* path);
|
|
pxl8_result pxl8_io_read_binary_file(const char* path, u8** data, size_t* size);
|
|
pxl8_result pxl8_io_read_file(const char* path, char** content, size_t* size);
|
|
pxl8_result pxl8_io_write_binary_file(const char* path, const u8* data, size_t size);
|
|
pxl8_result pxl8_io_write_file(const char* path, const char* content, size_t size);
|
|
|
|
bool pxl8_key_down(const pxl8_input_state* input, const char* key_name);
|
|
bool pxl8_key_pressed(const pxl8_input_state* input, const char* key_name);
|
|
bool pxl8_key_released(const pxl8_input_state* input, const char* key_name);
|
|
|
|
bool pxl8_mouse_pressed(const pxl8_input_state* input, i32 button);
|
|
bool pxl8_mouse_released(const pxl8_input_state* input, i32 button);
|
|
i32 pxl8_mouse_wheel_x(const pxl8_input_state* input);
|
|
i32 pxl8_mouse_wheel_y(const pxl8_input_state* input);
|
|
i32 pxl8_mouse_x(const pxl8_input_state* input);
|
|
i32 pxl8_mouse_y(const pxl8_input_state* input);
|
|
i32 pxl8_mouse_dx(const pxl8_input_state* input);
|
|
i32 pxl8_mouse_dy(const pxl8_input_state* input);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|