2021-11-30 21:29:18 -06:00
|
|
|
#include "texture.h"
|
|
|
|
|
|
|
|
#include "log.h"
|
2023-05-12 13:22:05 -05:00
|
|
|
#include "render.h"
|
2023-05-04 17:07:00 -05:00
|
|
|
#include "sokol/sokol_gfx.h"
|
2023-05-12 13:22:05 -05:00
|
|
|
#include <math.h>
|
|
|
|
#include <stb_ds.h>
|
|
|
|
#include <stb_image.h>
|
2023-05-12 22:21:11 -05:00
|
|
|
|
2023-09-18 12:35:40 -05:00
|
|
|
#include "resources.h"
|
|
|
|
|
2023-05-12 22:21:11 -05:00
|
|
|
#define STB_IMAGE_RESIZE_IMPLEMENTATION
|
|
|
|
#include "stb_image_resize.h"
|
2023-09-12 23:32:14 -05:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
#include <stdio.h>
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-08-31 13:00:33 -05:00
|
|
|
#define QOI_IMPLEMENTATION
|
|
|
|
#include "qoi.h"
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
struct glrect ST_UNIT = {0.f, 1.f, 0.f, 1.f};
|
2023-01-18 14:43:07 -06:00
|
|
|
|
2021-11-30 21:29:18 -06:00
|
|
|
static struct {
|
2023-05-12 13:22:05 -05:00
|
|
|
char *key;
|
|
|
|
struct Texture *value;
|
2021-11-30 21:29:18 -06:00
|
|
|
} *texhash = NULL;
|
|
|
|
|
2022-11-20 14:37:17 -06:00
|
|
|
struct Texture *tex_default;
|
2023-04-28 12:49:18 -05:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
struct Texture *texture_notex() {
|
2023-10-02 07:58:17 -05:00
|
|
|
return texture_pullfromfile("icons/no_tex.gif");
|
2023-04-28 12:49:18 -05:00
|
|
|
}
|
|
|
|
|
2023-05-24 20:45:50 -05:00
|
|
|
unsigned int next_pow2(unsigned int v)
|
|
|
|
{
|
|
|
|
v--;
|
|
|
|
v |= v >> 1;
|
|
|
|
v |= v >> 2;
|
|
|
|
v |= v >> 4;
|
|
|
|
v |= v >> 8;
|
|
|
|
v |= v >> 16;
|
|
|
|
v++;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2023-05-12 22:21:11 -05:00
|
|
|
int mip_levels(int width, int height)
|
|
|
|
{
|
2023-05-24 20:45:50 -05:00
|
|
|
int levels = 0;
|
|
|
|
|
|
|
|
while (width > 1 || height > 1)
|
|
|
|
{
|
|
|
|
width >>= 1;
|
|
|
|
height >>= 1;
|
|
|
|
levels++;
|
|
|
|
}
|
|
|
|
return levels;
|
|
|
|
}
|
|
|
|
|
|
|
|
int mip_wh(int w, int h, int *mw, int *mh, int lvl)
|
|
|
|
{
|
|
|
|
w >>= lvl;
|
|
|
|
h >>= lvl;
|
|
|
|
|
|
|
|
if (w == 0 && h == 0)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
*mw = w ? w : 1;
|
|
|
|
*mh = h ? h : 1;
|
|
|
|
|
|
|
|
return 0;
|
2023-05-12 22:21:11 -05:00
|
|
|
}
|
|
|
|
|
2023-09-24 11:26:44 -05:00
|
|
|
int gif_nframes(const char *path)
|
|
|
|
{
|
2023-09-25 08:21:02 -05:00
|
|
|
struct Texture *t = texture_pullfromfile(path);
|
|
|
|
return t->frames;
|
2023-09-24 11:26:44 -05:00
|
|
|
}
|
|
|
|
|
2023-02-08 15:30:12 -06:00
|
|
|
/* If an empty string or null is put for path, loads default texture */
|
2023-05-12 13:22:05 -05:00
|
|
|
struct Texture *texture_pullfromfile(const char *path) {
|
|
|
|
if (!path) return texture_notex();
|
|
|
|
|
|
|
|
int index = shgeti(texhash, path);
|
|
|
|
if (index != -1)
|
|
|
|
return texhash[index].value;
|
|
|
|
|
2023-10-02 07:58:17 -05:00
|
|
|
long rawlen;
|
|
|
|
unsigned char *raw = slurp_file(path, &rawlen);
|
|
|
|
|
|
|
|
if (!raw) return texture_notex();
|
|
|
|
|
|
|
|
unsigned char *data;
|
2023-08-31 13:00:33 -05:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
struct Texture *tex = calloc(1, sizeof(*tex));
|
|
|
|
tex->opts.sprite = 1;
|
|
|
|
tex->opts.mips = 0;
|
|
|
|
tex->opts.gamma = 0;
|
2023-06-06 15:49:55 -05:00
|
|
|
tex->opts.wrapx = 1;
|
|
|
|
tex->opts.wrapy = 1;
|
2023-05-12 13:22:05 -05:00
|
|
|
|
|
|
|
int n;
|
2023-08-31 13:00:33 -05:00
|
|
|
|
2023-09-03 12:50:02 -05:00
|
|
|
char *ext = strrchr(path, '.');
|
2023-09-24 11:26:44 -05:00
|
|
|
|
|
|
|
if (!strcmp(ext, ".qoi")) {
|
2023-08-31 13:00:33 -05:00
|
|
|
qoi_desc qoi;
|
2023-09-18 10:45:51 -05:00
|
|
|
data = qoi_decode(raw, rawlen, &qoi, 4);
|
2023-08-31 13:00:33 -05:00
|
|
|
tex->width = qoi.width;
|
|
|
|
tex->height = qoi.height;
|
|
|
|
n = qoi.channels;
|
2023-09-24 11:26:44 -05:00
|
|
|
} else if (!strcmp(ext, ".gif")) {
|
|
|
|
int *delays;
|
2023-09-25 08:21:02 -05:00
|
|
|
data = stbi_load_gif_from_memory(raw, rawlen, &delays, &tex->width, &tex->height, &tex->frames, &n, 4);
|
|
|
|
tex->height *= tex->frames;
|
2023-08-31 13:00:33 -05:00
|
|
|
} else {
|
2023-09-18 10:45:51 -05:00
|
|
|
data = stbi_load_from_memory(raw, rawlen, &tex->width, &tex->height, &n, 4);
|
2023-08-31 13:00:33 -05:00
|
|
|
}
|
2023-09-18 10:45:51 -05:00
|
|
|
free(raw);
|
2023-05-12 13:22:05 -05:00
|
|
|
|
|
|
|
if (data == NULL) {
|
|
|
|
YughError("STBI failed to load file %s with message: %s\nOpening default instead.", path, stbi_failure_reason());
|
|
|
|
return texture_notex();
|
|
|
|
}
|
2023-05-24 20:45:50 -05:00
|
|
|
|
|
|
|
unsigned int nw = next_pow2(tex->width);
|
|
|
|
unsigned int nh = next_pow2(tex->height);
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
tex->data = data;
|
|
|
|
|
|
|
|
int filter;
|
|
|
|
if (tex->opts.sprite) {
|
|
|
|
filter = SG_FILTER_NEAREST;
|
|
|
|
} else {
|
|
|
|
filter = SG_FILTER_LINEAR;
|
|
|
|
}
|
2023-05-12 22:21:11 -05:00
|
|
|
|
|
|
|
sg_image_data sg_img_data;
|
|
|
|
|
|
|
|
int mips = mip_levels(tex->width, tex->height)+1;
|
2023-05-24 20:45:50 -05:00
|
|
|
|
|
|
|
YughInfo("Has %d mip levels, from wxh %dx%d, pow2 is %ux%u.", mips, tex->width, tex->height,nw,nh);
|
2023-05-12 22:21:11 -05:00
|
|
|
|
|
|
|
int mipw, miph;
|
|
|
|
mipw = tex->width;
|
|
|
|
miph = tex->height;
|
|
|
|
|
|
|
|
sg_img_data.subimage[0][0] = (sg_range){ .ptr = data, .size = mipw*miph*4 };
|
|
|
|
|
|
|
|
unsigned char *mipdata[mips];
|
|
|
|
mipdata[0] = data;
|
|
|
|
|
|
|
|
for (int i = 1; i < mips; i++) {
|
2023-05-24 20:45:50 -05:00
|
|
|
int w, h, mipw, miph;
|
|
|
|
mip_wh(tex->width, tex->height, &mipw, &miph, i-1); /* mipw miph are previous iteration */
|
|
|
|
mip_wh(tex->width, tex->height, &w, &h, i);
|
2023-05-12 22:21:11 -05:00
|
|
|
mipdata[i] = malloc(w * h * 4);
|
|
|
|
stbir_resize_uint8(mipdata[i-1], mipw, miph, 0, mipdata[i], w, h, 0, 4);
|
|
|
|
sg_img_data.subimage[0][i] = (sg_range){ .ptr = mipdata[i], .size = w*h*4 };
|
|
|
|
|
|
|
|
mipw = w;
|
|
|
|
miph = h;
|
|
|
|
}
|
2023-05-12 13:22:05 -05:00
|
|
|
|
|
|
|
tex->id = sg_make_image(&(sg_image_desc){
|
2023-05-04 17:07:00 -05:00
|
|
|
.type = SG_IMAGETYPE_2D,
|
|
|
|
.width = tex->width,
|
|
|
|
.height = tex->height,
|
|
|
|
.usage = SG_USAGE_IMMUTABLE,
|
2023-05-12 22:21:11 -05:00
|
|
|
.num_mipmaps = mips,
|
|
|
|
.data = sg_img_data
|
|
|
|
});
|
2023-01-02 07:55:26 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
if (shlen(texhash) == 0)
|
|
|
|
sh_new_arena(texhash);
|
2022-11-17 16:48:20 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
shput(texhash, path, tex);
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-10-03 17:16:38 -05:00
|
|
|
for (int i = 1; i < mips; i++)
|
|
|
|
free(mipdata[i]);
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
return tex;
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
void texture_sync(const char *path) {
|
2023-05-04 17:07:00 -05:00
|
|
|
YughWarn("Need to implement texture sync.");
|
2023-05-01 20:58:10 -05:00
|
|
|
}
|
|
|
|
|
2022-08-24 12:24:21 -05:00
|
|
|
char *tex_get_path(struct Texture *tex) {
|
2023-05-12 13:22:05 -05:00
|
|
|
for (int i = 0; i < shlen(texhash); i++) {
|
|
|
|
if (tex == texhash[i].value) {
|
|
|
|
YughInfo("Found key %s", texhash[i].key);
|
|
|
|
return texhash[i].key;
|
2022-08-24 12:24:21 -05:00
|
|
|
}
|
2023-05-12 13:22:05 -05:00
|
|
|
}
|
2022-08-24 12:24:21 -05:00
|
|
|
|
2023-05-29 10:47:30 -05:00
|
|
|
return "";
|
2022-08-24 12:24:21 -05:00
|
|
|
}
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
struct Texture *texture_loadfromfile(const char *path) {
|
|
|
|
struct Texture *new = texture_pullfromfile(path);
|
|
|
|
/*
|
|
|
|
if (new->id == 0) {
|
|
|
|
glGenTextures(1, &new->id);
|
2022-06-30 16:38:51 -05:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
//tex_gpu_load(new);
|
2022-11-17 16:48:20 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
YughInfo("Loaded texture path %s", path);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
return new;
|
2022-01-19 16:43:21 -06:00
|
|
|
}
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
void tex_gpu_reload(struct Texture *tex) {
|
|
|
|
tex_gpu_free(tex);
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
// tex_gpu_load(tex);
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
void anim_calc(struct anim2d *anim) {
|
|
|
|
anim->size[0] = anim->anim->tex->width * st_s_w(anim->anim->st_frames[anim->frame]);
|
|
|
|
anim->size[1] = anim->anim->tex->height * st_s_h(anim->anim->st_frames[anim->frame]);
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
void anim_incr(struct anim2d *anim) {
|
|
|
|
anim->frame = (anim->frame + 1) % arrlen(anim->anim->st_frames);
|
2023-01-16 02:16:39 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
if (!anim->anim->loop && anim->frame == arrlen(anim->anim->st_frames))
|
|
|
|
anim_pause(anim);
|
2023-01-16 02:16:39 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
anim_calc(anim);
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
void anim_decr(struct anim2d *anim) {
|
|
|
|
anim->frame = (anim->frame + arrlen(anim->anim->st_frames) - 1) % arrlen(anim->anim->st_frames);
|
|
|
|
anim_calc(anim);
|
2023-01-02 07:55:26 -06:00
|
|
|
}
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
struct glrect anim_get_rect(struct anim2d *anim) {
|
|
|
|
return anim->anim->st_frames[anim->frame];
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
void anim_setframe(struct anim2d *anim, int frame) {
|
|
|
|
anim->frame = frame;
|
|
|
|
anim_calc(anim);
|
2022-08-25 15:48:15 -05:00
|
|
|
}
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
struct TexAnim *anim2d_from_tex(const char *path, int frames, int fps) {
|
|
|
|
struct TexAnim *anim = malloc(sizeof(*anim));
|
|
|
|
anim->tex = texture_loadfromfile(path);
|
|
|
|
texanim_fromframes(anim, frames);
|
|
|
|
anim->ms = (float)1 / fps;
|
2023-01-18 17:15:36 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
return anim;
|
2023-01-18 17:15:36 -06:00
|
|
|
}
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
void texanim_fromframes(struct TexAnim *anim, int frames) {
|
|
|
|
if (anim->st_frames) {
|
|
|
|
free(anim->st_frames);
|
|
|
|
}
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
arrsetlen(anim->st_frames, frames);
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
float width = (float)1 / frames;
|
2022-01-19 16:43:21 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
for (int i = 0; i < frames; i++) {
|
|
|
|
anim->st_frames[i].s0 = width * i;
|
|
|
|
anim->st_frames[i].s1 = width * (i + 1);
|
|
|
|
anim->st_frames[i].t0 = 0.f;
|
|
|
|
anim->st_frames[i].t1 = 1.f;
|
|
|
|
}
|
2023-01-16 02:16:39 -06:00
|
|
|
}
|
2022-01-19 16:43:21 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
void tex_gpu_free(struct Texture *tex) {
|
|
|
|
/*
|
|
|
|
if (tex->id != 0) {
|
|
|
|
glDeleteTextures(1, &tex->id);
|
|
|
|
tex->id = 0;
|
|
|
|
}
|
|
|
|
*/
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
int anim_frames(struct TexAnim *a) {
|
|
|
|
return arrlen(a->st_frames);
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
struct glrect tex_get_rect(struct Texture *tex) {
|
|
|
|
return ST_UNIT;
|
2022-12-29 11:27:41 -06:00
|
|
|
}
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
cpVect tex_get_dimensions(struct Texture *tex) {
|
2023-02-13 08:30:35 -06:00
|
|
|
if (!tex) return cpvzero;
|
|
|
|
cpVect d;
|
|
|
|
d.x = tex->width;
|
|
|
|
d.y = tex->height;
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
void tex_bind(struct Texture *tex) {
|
|
|
|
/* glActiveTexture(GL_TEXTURE0);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, tex->id);
|
|
|
|
glBindTexture(GL_TEXTURE_2D_ARRAY, tex->id);
|
|
|
|
*/
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2023-01-16 02:16:39 -06:00
|
|
|
/********************** ANIM2D ****************/
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
void anim_load(struct anim2d *anim, const char *path) {
|
|
|
|
anim->anim = &texture_pullfromfile(path)->anim;
|
|
|
|
anim->anim->tex->opts.animation = 1;
|
|
|
|
anim_stop(anim);
|
|
|
|
anim_play(anim);
|
2023-01-16 02:16:39 -06:00
|
|
|
}
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
void anim_play(struct anim2d *anim) {
|
|
|
|
if (anim->playing)
|
|
|
|
return;
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
if (anim->frame == anim_frames(anim->anim))
|
|
|
|
anim->frame = 0;
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
anim->playing = 1;
|
2022-08-22 08:55:54 -05:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
if (anim->timer == NULL)
|
2023-09-13 07:31:00 -05:00
|
|
|
anim->timer = id2timer(timer_make(1.f / anim->anim->ms, anim_incr, anim, 0, 0));
|
2023-05-12 13:22:05 -05:00
|
|
|
else
|
|
|
|
timerr_settime(anim->timer, 1.f / anim->anim->ms);
|
2022-08-22 08:55:54 -05:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
timer_start(anim->timer);
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
void anim_stop(struct anim2d *anim) {
|
|
|
|
if (!anim->playing)
|
|
|
|
return;
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
anim->playing = 0;
|
|
|
|
anim->frame = 0;
|
|
|
|
anim->pausetime = 0;
|
|
|
|
timer_stop(anim->timer);
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
void anim_pause(struct anim2d *anim) {
|
|
|
|
if (!anim->playing)
|
|
|
|
return;
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
anim->playing = 0;
|
|
|
|
timer_pause(anim->timer);
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
void anim_fwd(struct anim2d *anim) {
|
|
|
|
anim_incr(anim);
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
void anim_bkwd(struct anim2d *anim) {
|
|
|
|
anim_decr(anim);
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
2022-12-24 13:18:06 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
float st_s_w(struct glrect st) {
|
|
|
|
return (st.s1 - st.s0);
|
2022-12-30 13:31:06 -06:00
|
|
|
}
|
2022-12-24 13:18:06 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
float st_s_h(struct glrect st) {
|
|
|
|
return (st.t1 - st.t0);
|
2023-02-02 17:52:15 -06:00
|
|
|
}
|