prosperon/source/engine/texture.c

264 lines
5.3 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"
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;
2022-01-19 16:43:21 -06:00
struct Texture *texture_pullfromfile(const char *path)
2021-11-30 21:29:18 -06:00
{
int index = shgeti(texhash, path);
if (index != -1)
return texhash[index].value;
2022-01-19 16:43:21 -06:00
struct Texture *tex = calloc(1, sizeof(*tex));
2021-11-30 21:29:18 -06:00
tex->flipy = 0;
tex->opts.sprite = 1;
tex->opts.gamma = 0;
tex->anim.frames = 1;
tex->anim.ms = 1;
2022-01-19 16:43:21 -06:00
int n;
2022-12-28 16:50:54 -06:00
stbi_set_flip_vertically_on_load(0);
2022-01-19 16:43:21 -06:00
unsigned char *data = stbi_load(path, &tex->width, &tex->height, &n, 4);
2021-11-30 21:29:18 -06:00
while (data == NULL) {
YughError("STBI failed to load file %s with message: %s", path, stbi_failure_reason());
return NULL;
}
2021-11-30 21:29:18 -06:00
2022-01-19 16:43:21 -06:00
tex->data = data;
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
tex->id = 0;
2021-11-30 21:29:18 -06:00
return tex;
}
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);
2022-09-07 10:34:45 -05:00
if (new == NULL) {
2022-11-20 14:37:17 -06:00
YughError("Texture %s not loaded! Loading the default instead ...", path);
new = texture_pullfromfile("./ph.png");
2022-09-07 10:34:45 -05:00
}
if (new->id == 0) {
glGenTextures(1, &new->id);
2022-06-30 16:38:51 -05:00
tex_gpu_load(new);
YughInfo("Loaded texture path %s", path);
}
2022-01-19 16:43:21 -06:00
return new;
}
void tex_pull(struct Texture *tex)
{
2022-08-25 15:48:15 -05:00
if (tex->data != NULL)
tex_flush(tex);
2022-02-06 10:14:57 -06:00
int n;
2022-08-24 12:24:21 -05:00
char *path = tex_get_path(tex);
2022-01-19 16:43:21 -06:00
stbi_set_flip_vertically_on_load(0);
2022-08-24 12:24:21 -05:00
tex->data = stbi_load(path, &tex->width, &tex->height, &n, 4);
2022-01-19 16:43:21 -06:00
if (tex->data == NULL)
YughError("STBI failed to load file %s with message: %s", path, stbi_failure_reason());
2022-01-19 16:43:21 -06:00
}
void tex_flush(struct Texture *tex)
{
free(tex->data);
}
void tex_gpu_reload(struct Texture *tex)
{
tex_gpu_free(tex);
2021-11-30 21:29:18 -06:00
tex_gpu_load(tex);
}
2022-01-19 16:43:21 -06:00
void tex_free(struct Texture *tex)
{
free(tex->data);
2022-08-24 12:24:21 -05:00
//free(tex->path);
2022-01-19 16:43:21 -06:00
free(tex);
}
2021-11-30 21:29:18 -06:00
void tex_gpu_load(struct Texture *tex)
{
glBindTexture(GL_TEXTURE_2D, tex->id);
2022-06-30 16:38:51 -05:00
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex->width, tex->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex->data);
2021-11-30 21:29:18 -06:00
glGenerateMipmap(GL_TEXTURE_2D);
if (tex->opts.sprite) {
2022-06-30 16:38:51 -05:00
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2021-11-30 21:29:18 -06:00
} else {
2022-06-30 16:38:51 -05:00
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2021-11-30 21:29:18 -06:00
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
}
2022-08-22 08:55:54 -05:00
void tex_incr_anim(struct TexAnimation *tex_anim)
2021-11-30 21:29:18 -06:00
{
anim_incr(tex_anim);
2022-08-22 08:55:54 -05:00
if (!tex_anim->tex->anim.loop && tex_anim->frame == tex_anim->tex->anim.frames)
2021-11-30 21:29:18 -06:00
anim_pause(tex_anim);
}
void anim_incr(struct TexAnimation *anim)
{
anim->frame = (anim->frame + 1) % anim->tex->anim.frames;
tex_anim_calc_uv(anim);
}
void anim_decr(struct TexAnimation *anim)
{
2022-08-22 08:55:54 -05:00
anim->frame = (anim->frame + anim->tex->anim.frames - 1) % anim->tex->anim.frames;
2021-11-30 21:29:18 -06:00
tex_anim_calc_uv(anim);
}
2022-08-25 15:48:15 -05:00
void anim_setframe(struct TexAnimation *anim, int frame)
{
anim->frame = frame;
tex_anim_calc_uv(anim);
}
2021-11-30 21:29:18 -06:00
void tex_anim_set(struct TexAnimation *anim)
{
if (anim->playing) {
2022-02-06 10:14:57 -06:00
timer_remove(anim->timer);
2022-08-22 08:55:54 -05:00
anim->timer = timer_make(1.f / anim->tex->anim.ms, tex_incr_anim, anim);
2021-11-30 21:29:18 -06:00
}
tex_anim_calc_uv(anim);
}
2022-01-19 16:43:21 -06:00
void tex_gpu_free(struct Texture *tex)
2021-11-30 21:29:18 -06:00
{
if (tex->id != 0) {
glDeleteTextures(1, &tex->id);
tex->id = 0;
}
}
void tex_anim_calc_uv(struct TexAnimation *anim)
{
2022-12-30 13:31:06 -06:00
struct glrect st;
float w = 1.f / anim->tex->anim.frames;
st.s0 = w * (anim->frame);
st.s1 = st.s0 + w;
st.t0 = 0.f;
st.t1 = 1.f;
anim->st = st;
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
}
2021-11-30 21:29:18 -06:00
void tex_bind(struct Texture *tex)
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex->id);
glBindTexture(GL_TEXTURE_2D_ARRAY, tex->id);
}
void anim_play(struct TexAnimation *anim)
{
if (anim->playing)
return;
if (anim->frame == anim->tex->anim.frames)
anim->frame = 0;
anim->playing = 1;
2022-08-22 08:55:54 -05:00
if (anim->timer == NULL)
anim->timer = timer_make(1.f / anim->tex->anim.ms, tex_incr_anim, anim);
else
timerr_settime(anim->timer, 1.f/anim->tex->anim.ms);
2022-08-22 08:55:54 -05:00
timer_start(anim->timer);
2021-11-30 21:29:18 -06:00
}
void anim_stop(struct TexAnimation *anim)
{
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
tex_anim_calc_uv(anim);
}
void anim_pause(struct TexAnimation *anim)
{
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
}
void anim_fwd(struct TexAnimation *anim)
{
anim_incr(anim);
}
void anim_bkwd(struct TexAnimation *anim)
{
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);
}