prosperon/source/engine/texture.c

288 lines
5.7 KiB
C
Raw Normal View History

2021-11-30 21:29:18 -06:00
#include "texture.h"
2022-02-06 10:14:57 -06:00
#include "render.h"
2021-11-30 21:29:18 -06:00
#include <stdio.h>
#include <stb_image.h>
#include <stb_ds.h>
#include "log.h"
2022-02-06 10:14:57 -06:00
#include <math.h>
2022-07-05 15:12:48 -05:00
#include "util.h"
2023-05-04 17:07:00 -05:00
#include "sokol/sokol_gfx.h"
2021-11-30 21:29:18 -06:00
2023-01-18 14:43:07 -06:00
struct glrect ST_UNIT = { 0.f, 1.f, 0.f, 1.f };
2021-11-30 21:29:18 -06:00
static struct {
char *key;
struct Texture *value;
} *texhash = NULL;
2022-11-20 14:37:17 -06:00
struct Texture *tex_default;
2023-04-28 12:49:18 -05:00
struct Texture *texture_notex()
{
return texture_pullfromfile("./icons/no_tex.png");
}
2023-02-08 15:30:12 -06:00
/* If an empty string or null is put for path, loads default texture */
2022-01-19 16:43:21 -06:00
struct Texture *texture_pullfromfile(const char *path)
2021-11-30 21:29:18 -06:00
{
2023-04-28 12:49:18 -05:00
if (!path) return texture_notex();
2021-11-30 21:29:18 -06:00
int index = shgeti(texhash, path);
if (index != -1)
return texhash[index].value;
2023-02-28 09:40:53 -06:00
YughInfo("Loading texture %s.", path);
2022-01-19 16:43:21 -06:00
struct Texture *tex = calloc(1, sizeof(*tex));
2023-05-01 20:58:10 -05:00
tex->opts.sprite = 1;
tex->opts.mips = 0;
2021-11-30 21:29:18 -06:00
tex->opts.gamma = 0;
2023-05-01 20:58:10 -05:00
2022-01-19 16:43:21 -06:00
int n;
unsigned char *data = stbi_load(path, &tex->width, &tex->height, &n, 4);
2021-11-30 21:29:18 -06:00
2023-02-08 15:30:12 -06:00
if (data == NULL) {
YughError("STBI failed to load file %s with message: %s\nOpening default instead.", path, stbi_failure_reason());
2023-04-28 12:49:18 -05:00
return texture_notex();
}
2023-03-24 14:01:01 -05:00
tex->data = data;
2021-11-30 21:29:18 -06:00
2023-05-04 17:07:00 -05:00
int filter;
if (tex->opts.sprite) {
if (tex->opts.mips)
filter = SG_FILTER_NEAREST_MIPMAP_NEAREST;
else
filter = SG_FILTER_NEAREST;
} else {
if (tex->opts.mips)
filter = SG_FILTER_LINEAR_MIPMAP_LINEAR;
else
filter = SG_FILTER_LINEAR;
}
tex->id = sg_make_image(&(sg_image_desc){
.type = SG_IMAGETYPE_2D,
.width = tex->width,
.height = tex->height,
.usage = SG_USAGE_IMMUTABLE,
.min_filter = filter,
.mag_filter = filter,
.data.subimage[0][0] = {
.ptr = data,
.size = tex->width*tex->height*4
}
});
2023-01-02 07:55:26 -06:00
if (shlen(texhash) == 0)
sh_new_arena(texhash);
2022-08-24 12:24:21 -05:00
shput(texhash, path, tex);
2021-11-30 21:29:18 -06:00
return tex;
}
2023-05-01 20:58:10 -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) {
for (int i = 0; i < shlen(texhash); i++) {
if (tex == texhash[i].value) {
YughInfo("Found key %s", texhash[i].key);
2022-08-24 12:24:21 -05:00
return texhash[i].key;
}
2022-08-24 12:24:21 -05:00
}
return NULL;
}
2022-01-19 16:43:21 -06:00
struct Texture *texture_loadfromfile(const char *path)
2021-11-30 21:29:18 -06:00
{
2022-01-19 16:43:21 -06:00
struct Texture *new = texture_pullfromfile(path);
2023-05-04 17:07:00 -05:00
/*
if (new->id == 0) {
glGenTextures(1, &new->id);
2022-06-30 16:38:51 -05:00
2023-01-02 07:55:26 -06:00
//tex_gpu_load(new);
YughInfo("Loaded texture path %s", path);
}
2023-05-04 17:07:00 -05:00
*/
2022-01-19 16:43:21 -06:00
return new;
}
void tex_gpu_reload(struct Texture *tex)
{
tex_gpu_free(tex);
2021-11-30 21:29:18 -06:00
2023-01-02 07:55:26 -06:00
//tex_gpu_load(tex);
2021-11-30 21:29:18 -06:00
}
2023-01-16 02:16:39 -06:00
void anim_calc(struct anim2d *anim)
2021-11-30 21:29:18 -06:00
{
2023-01-16 02:16:39 -06:00
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-01-15 09:53:50 -06:00
void anim_incr(struct anim2d *anim)
2021-11-30 21:29:18 -06:00
{
2023-01-02 07:55:26 -06:00
anim->frame = (anim->frame + 1) % arrlen(anim->anim->st_frames);
2023-01-16 02:16:39 -06:00
if (!anim->anim->loop && anim->frame == arrlen(anim->anim->st_frames))
anim_pause(anim);
anim_calc(anim);
2021-11-30 21:29:18 -06:00
}
2023-01-15 09:53:50 -06:00
void anim_decr(struct anim2d *anim)
2021-11-30 21:29:18 -06:00
{
2023-01-02 07:55:26 -06:00
anim->frame = (anim->frame + arrlen(anim->anim->st_frames) - 1) % arrlen(anim->anim->st_frames);
2023-01-16 02:16:39 -06:00
anim_calc(anim);
2023-01-02 07:55:26 -06:00
}
2023-01-15 09:53:50 -06:00
struct glrect anim_get_rect(struct anim2d *anim)
2023-01-02 07:55:26 -06:00
{
return anim->anim->st_frames[anim->frame];
2021-11-30 21:29:18 -06:00
}
2023-01-15 09:53:50 -06:00
void anim_setframe(struct anim2d *anim, int frame)
2022-08-25 15:48:15 -05:00
{
anim->frame = frame;
2023-01-16 02:16:39 -06:00
anim_calc(anim);
2022-08-25 15:48:15 -05:00
}
2023-01-18 17:15:36 -06: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;
return anim;
}
2023-01-16 02:16:39 -06:00
void texanim_fromframes(struct TexAnim *anim, int frames)
2021-11-30 21:29:18 -06:00
{
2023-02-08 15:30:12 -06:00
if (anim->st_frames) {
free(anim->st_frames);
}
2021-11-30 21:29:18 -06:00
2023-01-18 14:43:07 -06:00
arrsetlen(anim->st_frames, frames);
2021-11-30 21:29:18 -06:00
2023-01-16 02:16:39 -06:00
float width = (float)1/frames;
2022-01-19 16:43:21 -06:00
2023-01-16 02:16:39 -06: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;
}
}
2022-01-19 16:43:21 -06:00
void tex_gpu_free(struct Texture *tex)
2021-11-30 21:29:18 -06:00
{
2023-05-04 17:07:00 -05:00
/*
2021-11-30 21:29:18 -06:00
if (tex->id != 0) {
glDeleteTextures(1, &tex->id);
tex->id = 0;
}
2023-05-04 17:07:00 -05:00
*/
2021-11-30 21:29:18 -06:00
}
2023-01-02 07:55:26 -06:00
int anim_frames(struct TexAnim *a)
2021-11-30 21:29:18 -06:00
{
2023-01-02 07:55:26 -06:00
return arrlen(a->st_frames);
2021-11-30 21:29:18 -06:00
}
2022-12-29 11:27:41 -06:00
struct glrect tex_get_rect(struct Texture *tex)
{
2022-12-30 13:31:06 -06:00
return ST_UNIT;
2022-12-29 11:27:41 -06:00
}
2023-02-13 08:30:35 -06:00
cpVect tex_get_dimensions(struct Texture *tex)
{
if (!tex) return cpvzero;
cpVect d;
d.x = tex->width;
d.y = tex->height;
return d;
}
2021-11-30 21:29:18 -06:00
void tex_bind(struct Texture *tex)
{
2023-05-04 17:07:00 -05:00
/* glActiveTexture(GL_TEXTURE0);
2021-11-30 21:29:18 -06:00
glBindTexture(GL_TEXTURE_2D, tex->id);
glBindTexture(GL_TEXTURE_2D_ARRAY, tex->id);
2023-05-04 17:07:00 -05:00
*/
2021-11-30 21:29:18 -06:00
}
2023-01-16 02:16:39 -06:00
/********************** ANIM2D ****************/
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-15 09:53:50 -06:00
void anim_play(struct anim2d *anim)
2021-11-30 21:29:18 -06:00
{
if (anim->playing)
return;
2023-01-02 07:55:26 -06:00
if (anim->frame == anim_frames(anim->anim))
2021-11-30 21:29:18 -06:00
anim->frame = 0;
anim->playing = 1;
2022-08-22 08:55:54 -05:00
if (anim->timer == NULL)
2023-04-19 15:16:35 -05:00
anim->timer = id2timer(timer_make(1.f / anim->anim->ms, anim_incr, anim, 0));
2022-08-22 08:55:54 -05:00
else
2023-01-02 07:55:26 -06:00
timerr_settime(anim->timer, 1.f/anim->anim->ms);
2022-08-22 08:55:54 -05:00
timer_start(anim->timer);
2021-11-30 21:29:18 -06:00
}
2023-01-15 09:53:50 -06:00
void anim_stop(struct anim2d *anim)
2021-11-30 21:29:18 -06:00
{
if (!anim->playing)
return;
anim->playing = 0;
anim->frame = 0;
anim->pausetime = 0;
2022-08-22 08:55:54 -05:00
timer_stop(anim->timer);
2021-11-30 21:29:18 -06:00
}
2023-01-15 09:53:50 -06:00
void anim_pause(struct anim2d *anim)
2021-11-30 21:29:18 -06:00
{
if (!anim->playing)
return;
anim->playing = 0;
2022-08-22 08:55:54 -05:00
timer_pause(anim->timer);
2021-11-30 21:29:18 -06:00
}
2023-01-15 09:53:50 -06:00
void anim_fwd(struct anim2d *anim)
2021-11-30 21:29:18 -06:00
{
anim_incr(anim);
}
2023-01-15 09:53:50 -06:00
void anim_bkwd(struct anim2d *anim)
2021-11-30 21:29:18 -06:00
{
anim_decr(anim);
}
2022-12-24 13:18:06 -06:00
2022-12-30 13:31:06 -06:00
float st_s_w(struct glrect st)
{
return (st.s1 - st.s0);
}
2022-12-24 13:18:06 -06:00
2022-12-30 13:31:06 -06:00
float st_s_h(struct glrect st)
{
return (st.t1 - st.t0);
}