Add asset saving

This commit is contained in:
John Alanbrook 2022-08-24 17:24:21 +00:00
parent dc82da6982
commit 3040dc1f7f
6 changed files with 91 additions and 17 deletions

View file

@ -27,6 +27,8 @@
#include <stdio.h>
#include <stdlib.h>
#include "log.h"
#include "ftw.h"
#include <stb_ds.h>
@ -122,6 +124,45 @@ static void print_files_in_directory(const char *dirpath) {
static void get_all_files() { print_files_in_directory("."); }
size_t asset_side_size(struct fileasset *asset) {
if (asset->type == ASSET_TYPE_IMAGE)
return sizeof(struct Texture);
return 0;
}
void save_asset() {
if (selected_asset == NULL) {
YughWarn("No asset to save.", 0);
return;
}
if (selected_asset->type != ASSET_TYPE_IMAGE) return;
FILE *f = res_open(str_replace_ext(selected_asset->filename, EXT_ASSET), "w");
fwrite(selected_asset->data, asset_side_size(selected_asset), 1, f);
fclose(f);
}
void load_asset() {
if (selected_asset == NULL) {
YughWarn("No asset to load.", 0);
return;
}
if (selected_asset->type != ASSET_TYPE_IMAGE) return;
if (selected_asset->data == NULL)
selected_asset->data = malloc(asset_side_size(selected_asset));
FILE *f = res_open(str_replace_ext(selected_asset->filename, EXT_ASSET), "r");
if (f == NULL)
return;
fread(selected_asset->data, asset_side_size(selected_asset), 1, f);
fclose(f);
}
static int *compute_prefix_function(const char *str) {
int str_len = strlen(str);
int *pi = (int *)malloc(sizeof(int) * str_len);
@ -736,7 +777,13 @@ void editor_selectasset(struct fileasset *asset) {
fread(asset->data, 1, length, fasset);
fclose(fasset);
}
if (selected_asset != NULL)
save_asset();
selected_asset = asset;
load_asset();
}
void editor_selectasset_str(char *path) {
@ -892,7 +939,7 @@ void game_pause() { physOn = 0; }
void sprite_gui(struct mSprite *sprite) {
nuke_nel(2);
nk_labelf(ctx, NK_TEXT_LEFT, "Path %s", sprite->tex->path);
nk_labelf(ctx, NK_TEXT_LEFT, "Path %s", tex_get_path(sprite->tex));
if (nk_button_label(ctx, "Load texture") && selected_asset != NULL) {
@ -900,12 +947,12 @@ void sprite_gui(struct mSprite *sprite) {
}
if (sprite->tex != NULL) {
nk_labelf(ctx, NK_TEXT_LEFT, "%s", sprite->tex->path);
nk_labelf(ctx, NK_TEXT_LEFT, "%s", tex_get_path(sprite->tex));
nk_labelf(ctx, NK_TEXT_LEFT, "%dx%d", sprite->tex->width, sprite->tex->height);
if (nk_button_label(ctx, "Imgbutton"))
editor_selectasset_str(sprite->tex->path);
// if (ImGui::ImageButton ((void *) (intptr_t) sprite->tex->id, ImVec2(50,
// 50))) {
nk_layout_row_static(ctx, sprite->tex->height, sprite->tex->width, 1);
if (nk_button_image(ctx, nk_image_id(sprite->tex->id)))
editor_selectasset_str(tex_get_path(sprite->tex));
}
nk_property_float2(ctx, "Sprite Position", -1.f, sprite->pos, 0.f, 0.01f, 0.01f);
@ -925,3 +972,4 @@ void sprite_gui(struct mSprite *sprite) {
sprite->pos[1] = 0.f;
}
}

View file

@ -17,7 +17,7 @@ struct fileasset {
short filename_len;
bool searched;
short type;
void *data;
void *data; // Struct of the underlying asset - Texture struct, etc
};
typedef struct {

View file

@ -21,6 +21,7 @@ struct vec *prefabs = NULL;
const char *EXT_PREFAB = ".prefab";
const char *EXT_LEVEL = ".level";
const char *EXT_ASSET = ".asset";
int stemlen = 0;
static const char *cur_ext = NULL;
@ -100,6 +101,16 @@ void findPrefabs()
fill_extensions(prefabs, DATA_PATH, EXT_PREFAB);
}
char *str_replace_ext(const char *s, const char *newext) {
static char ret[256];
strncpy(ret, s, 256);
char *ext = strrchr(ret, '.');
strncpy(ext, newext, 10);
return ret;
}
FILE *path_open(const char *tag, const char *fmt, ...)
{
va_list args;

View file

@ -12,6 +12,7 @@ extern char *PREF_PATH;
extern const char *EXT_PREFAB;
extern const char *EXT_LEVEL;
extern const char *EXT_ASSET;
extern int stemlen;
void resources_init();
@ -21,6 +22,7 @@ void findPrefabs();
void fill_extensions(struct vec *vec, const char *path, const char *ext);
char *get_filename_from_path(char *path, int extension);
char *get_directory_from_path(char *path);
char *str_replace_ext(const char *s, const char *newext);
FILE *res_open(char *path, const char *tag);
FILE *path_open(const char *tag, const char *fmt, ...);
char *make_path(const char *file);

View file

@ -19,9 +19,11 @@ struct Texture *texture_pullfromfile(const char *path)
if (index != -1)
return texhash[index].value;
struct Texture *tex = calloc(1, sizeof(*tex));
tex->path = malloc(strlen(path) + 1);
/* tex->path = malloc(strlen(path) + 1);
strncpy(tex->path, path, strlen(path) + 1);
*/
tex->flipy = 0;
tex->opts.sprite = 1;
tex->opts.gamma = 0;
@ -34,17 +36,26 @@ struct Texture *texture_pullfromfile(const char *path)
if (stbi_failure_reason()) {
YughLog(0, 3, "STBI failed to load file %s with message: %s",
tex->path, stbi_failure_reason());
path, stbi_failure_reason());
}
tex->data = data;
shput(texhash, tex->path, tex);
shput(texhash, path, tex);
return tex;
}
char *tex_get_path(struct Texture *tex) {
for (int i = 0; i < shlen(texhash); i++) {
if (tex == texhash[i].value)
return texhash[i].key;
}
return NULL;
}
struct Texture *texture_loadfromfile(const char *path)
{
struct Texture *new = texture_pullfromfile(path);
@ -61,12 +72,12 @@ struct Texture *texture_loadfromfile(const char *path)
void tex_pull(struct Texture *tex)
{
int n;
char *path = tex_get_path(tex);
stbi_set_flip_vertically_on_load(0);
tex->data = stbi_load(tex->path, &tex->width, &tex->height, &n, 4);
tex->data = stbi_load(path, &tex->width, &tex->height, &n, 4);
if (stbi_failure_reason())
YughLog(0, 3, "STBI failed to load file %s with message: %s",
tex->path, stbi_failure_reason());
YughLog(0, 3, "STBI failed to load file %s with message: %s", path, stbi_failure_reason());
}
void tex_flush(struct Texture *tex)
@ -84,7 +95,7 @@ void tex_gpu_reload(struct Texture *tex)
void tex_free(struct Texture *tex)
{
free(tex->data);
free(tex->path);
//free(tex->path);
free(tex);
}

View file

@ -41,11 +41,11 @@ struct TextureOptions {
struct Texture {
int type;
unsigned int id;
char *path;
//char *path;
int width;
int height;
short flipy;
unsigned char *data;
unsigned char *data; // Pixel data of the texture, loaded in at runtime
struct TextureOptions opts;
struct TexAnim anim;
@ -63,6 +63,8 @@ void tex_bind(struct Texture *tex);
unsigned int powof2(unsigned int num);
int ispow2(int num);
char * tex_get_path(struct Texture *tex);
void anim_play(struct TexAnimation *anim);
void anim_stop(struct TexAnimation *anim);
void anim_pause(struct TexAnimation *anim);