add save and bundle pxl8 with game for standalone game distribution
This commit is contained in:
parent
b1e8525c3e
commit
04d3af11a9
25 changed files with 1173 additions and 346 deletions
|
|
@ -3,6 +3,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "pxl8_cart.h"
|
||||
#include "pxl8_types.h"
|
||||
|
||||
static inline char pxl8_to_lower(char c) {
|
||||
|
|
@ -11,31 +12,48 @@ static inline char pxl8_to_lower(char c) {
|
|||
|
||||
pxl8_result pxl8_io_read_file(const char* path, char** content, size_t* size) {
|
||||
if (!path || !content || !size) return PXL8_ERROR_NULL_POINTER;
|
||||
|
||||
|
||||
pxl8_cart* cart = pxl8_cart_current();
|
||||
if (cart && pxl8_cart_is_packed(cart)) {
|
||||
u8* data = NULL;
|
||||
u32 cart_size = 0;
|
||||
pxl8_result result = pxl8_cart_read_file(cart, path, &data, &cart_size);
|
||||
if (result == PXL8_OK) {
|
||||
*content = realloc(data, cart_size + 1);
|
||||
if (!*content) {
|
||||
pxl8_cart_free_file(data);
|
||||
return PXL8_ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
(*content)[cart_size] = '\0';
|
||||
*size = cart_size;
|
||||
return PXL8_OK;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue