Add asset saving
This commit is contained in:
parent
dc82da6982
commit
3040dc1f7f
|
@ -27,6 +27,8 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "log.h"
|
||||||
|
|
||||||
#include "ftw.h"
|
#include "ftw.h"
|
||||||
|
|
||||||
#include <stb_ds.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("."); }
|
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) {
|
static int *compute_prefix_function(const char *str) {
|
||||||
int str_len = strlen(str);
|
int str_len = strlen(str);
|
||||||
int *pi = (int *)malloc(sizeof(int) * str_len);
|
int *pi = (int *)malloc(sizeof(int) * str_len);
|
||||||
|
@ -736,7 +777,13 @@ void editor_selectasset(struct fileasset *asset) {
|
||||||
fread(asset->data, 1, length, fasset);
|
fread(asset->data, 1, length, fasset);
|
||||||
fclose(fasset);
|
fclose(fasset);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (selected_asset != NULL)
|
||||||
|
save_asset();
|
||||||
|
|
||||||
|
|
||||||
selected_asset = asset;
|
selected_asset = asset;
|
||||||
|
load_asset();
|
||||||
}
|
}
|
||||||
|
|
||||||
void editor_selectasset_str(char *path) {
|
void editor_selectasset_str(char *path) {
|
||||||
|
@ -892,7 +939,7 @@ void game_pause() { physOn = 0; }
|
||||||
|
|
||||||
void sprite_gui(struct mSprite *sprite) {
|
void sprite_gui(struct mSprite *sprite) {
|
||||||
nuke_nel(2);
|
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) {
|
if (nk_button_label(ctx, "Load texture") && selected_asset != NULL) {
|
||||||
|
@ -900,12 +947,12 @@ void sprite_gui(struct mSprite *sprite) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sprite->tex != NULL) {
|
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);
|
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);
|
nk_layout_row_static(ctx, sprite->tex->height, sprite->tex->width, 1);
|
||||||
// if (ImGui::ImageButton ((void *) (intptr_t) sprite->tex->id, ImVec2(50,
|
if (nk_button_image(ctx, nk_image_id(sprite->tex->id)))
|
||||||
// 50))) {
|
editor_selectasset_str(tex_get_path(sprite->tex));
|
||||||
}
|
}
|
||||||
nk_property_float2(ctx, "Sprite Position", -1.f, sprite->pos, 0.f, 0.01f, 0.01f);
|
nk_property_float2(ctx, "Sprite Position", -1.f, sprite->pos, 0.f, 0.01f, 0.01f);
|
||||||
|
|
||||||
|
@ -924,4 +971,5 @@ void sprite_gui(struct mSprite *sprite) {
|
||||||
sprite->pos[0] = -0.5f;
|
sprite->pos[0] = -0.5f;
|
||||||
sprite->pos[1] = 0.f;
|
sprite->pos[1] = 0.f;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,7 @@ struct fileasset {
|
||||||
short filename_len;
|
short filename_len;
|
||||||
bool searched;
|
bool searched;
|
||||||
short type;
|
short type;
|
||||||
void *data;
|
void *data; // Struct of the underlying asset - Texture struct, etc
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|
|
@ -21,6 +21,7 @@ struct vec *prefabs = NULL;
|
||||||
|
|
||||||
const char *EXT_PREFAB = ".prefab";
|
const char *EXT_PREFAB = ".prefab";
|
||||||
const char *EXT_LEVEL = ".level";
|
const char *EXT_LEVEL = ".level";
|
||||||
|
const char *EXT_ASSET = ".asset";
|
||||||
int stemlen = 0;
|
int stemlen = 0;
|
||||||
|
|
||||||
static const char *cur_ext = NULL;
|
static const char *cur_ext = NULL;
|
||||||
|
@ -100,6 +101,16 @@ void findPrefabs()
|
||||||
fill_extensions(prefabs, DATA_PATH, EXT_PREFAB);
|
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, ...)
|
FILE *path_open(const char *tag, const char *fmt, ...)
|
||||||
{
|
{
|
||||||
va_list args;
|
va_list args;
|
||||||
|
|
|
@ -12,6 +12,7 @@ extern char *PREF_PATH;
|
||||||
|
|
||||||
extern const char *EXT_PREFAB;
|
extern const char *EXT_PREFAB;
|
||||||
extern const char *EXT_LEVEL;
|
extern const char *EXT_LEVEL;
|
||||||
|
extern const char *EXT_ASSET;
|
||||||
extern int stemlen;
|
extern int stemlen;
|
||||||
|
|
||||||
void resources_init();
|
void resources_init();
|
||||||
|
@ -21,6 +22,7 @@ void findPrefabs();
|
||||||
void fill_extensions(struct vec *vec, const char *path, const char *ext);
|
void fill_extensions(struct vec *vec, const char *path, const char *ext);
|
||||||
char *get_filename_from_path(char *path, int extension);
|
char *get_filename_from_path(char *path, int extension);
|
||||||
char *get_directory_from_path(char *path);
|
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 *res_open(char *path, const char *tag);
|
||||||
FILE *path_open(const char *tag, const char *fmt, ...);
|
FILE *path_open(const char *tag, const char *fmt, ...);
|
||||||
char *make_path(const char *file);
|
char *make_path(const char *file);
|
||||||
|
|
|
@ -19,9 +19,11 @@ struct Texture *texture_pullfromfile(const char *path)
|
||||||
if (index != -1)
|
if (index != -1)
|
||||||
return texhash[index].value;
|
return texhash[index].value;
|
||||||
|
|
||||||
|
|
||||||
struct Texture *tex = calloc(1, sizeof(*tex));
|
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);
|
strncpy(tex->path, path, strlen(path) + 1);
|
||||||
|
*/
|
||||||
tex->flipy = 0;
|
tex->flipy = 0;
|
||||||
tex->opts.sprite = 1;
|
tex->opts.sprite = 1;
|
||||||
tex->opts.gamma = 0;
|
tex->opts.gamma = 0;
|
||||||
|
@ -34,17 +36,26 @@ struct Texture *texture_pullfromfile(const char *path)
|
||||||
|
|
||||||
if (stbi_failure_reason()) {
|
if (stbi_failure_reason()) {
|
||||||
YughLog(0, 3, "STBI failed to load file %s with message: %s",
|
YughLog(0, 3, "STBI failed to load file %s with message: %s",
|
||||||
tex->path, stbi_failure_reason());
|
path, stbi_failure_reason());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tex->data = data;
|
tex->data = data;
|
||||||
|
|
||||||
shput(texhash, tex->path, tex);
|
shput(texhash, path, tex);
|
||||||
|
|
||||||
return 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 *texture_loadfromfile(const char *path)
|
||||||
{
|
{
|
||||||
struct Texture *new = texture_pullfromfile(path);
|
struct Texture *new = texture_pullfromfile(path);
|
||||||
|
@ -61,12 +72,12 @@ struct Texture *texture_loadfromfile(const char *path)
|
||||||
void tex_pull(struct Texture *tex)
|
void tex_pull(struct Texture *tex)
|
||||||
{
|
{
|
||||||
int n;
|
int n;
|
||||||
|
char *path = tex_get_path(tex);
|
||||||
stbi_set_flip_vertically_on_load(0);
|
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())
|
if (stbi_failure_reason())
|
||||||
YughLog(0, 3, "STBI failed to load file %s with message: %s",
|
YughLog(0, 3, "STBI failed to load file %s with message: %s", path, stbi_failure_reason());
|
||||||
tex->path, stbi_failure_reason());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void tex_flush(struct Texture *tex)
|
void tex_flush(struct Texture *tex)
|
||||||
|
@ -84,7 +95,7 @@ void tex_gpu_reload(struct Texture *tex)
|
||||||
void tex_free(struct Texture *tex)
|
void tex_free(struct Texture *tex)
|
||||||
{
|
{
|
||||||
free(tex->data);
|
free(tex->data);
|
||||||
free(tex->path);
|
//free(tex->path);
|
||||||
free(tex);
|
free(tex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -41,11 +41,11 @@ struct TextureOptions {
|
||||||
struct Texture {
|
struct Texture {
|
||||||
int type;
|
int type;
|
||||||
unsigned int id;
|
unsigned int id;
|
||||||
char *path;
|
//char *path;
|
||||||
int width;
|
int width;
|
||||||
int height;
|
int height;
|
||||||
short flipy;
|
short flipy;
|
||||||
unsigned char *data;
|
unsigned char *data; // Pixel data of the texture, loaded in at runtime
|
||||||
|
|
||||||
struct TextureOptions opts;
|
struct TextureOptions opts;
|
||||||
struct TexAnim anim;
|
struct TexAnim anim;
|
||||||
|
@ -63,6 +63,8 @@ void tex_bind(struct Texture *tex);
|
||||||
unsigned int powof2(unsigned int num);
|
unsigned int powof2(unsigned int num);
|
||||||
int ispow2(int num);
|
int ispow2(int num);
|
||||||
|
|
||||||
|
char * tex_get_path(struct Texture *tex);
|
||||||
|
|
||||||
void anim_play(struct TexAnimation *anim);
|
void anim_play(struct TexAnimation *anim);
|
||||||
void anim_stop(struct TexAnimation *anim);
|
void anim_stop(struct TexAnimation *anim);
|
||||||
void anim_pause(struct TexAnimation *anim);
|
void anim_pause(struct TexAnimation *anim);
|
||||||
|
|
Loading…
Reference in a new issue