prosperon/source/engine/texture.c

282 lines
5.8 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-01-13 22:08:39 -06:00
#include "parson.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));
2023-01-13 22:08:39 -06:00
/* Find texture's asset; otherwise load default asset */
JSON_Value *rv = json_parse_file("texture.asset");
JSON_Object *ro = json_value_get_object(rv);
tex->opts.sprite = json_object_get_boolean(ro, "sprite");
tex->opts.mips = json_object_get_boolean(ro, "mips");
json_value_free(rv);
2021-11-30 21:29:18 -06:00
tex->opts.gamma = 0;
2023-01-16 02:16:39 -06:00
tex->anim.ms = 2;
tex->anim.tex = tex;
texanim_fromframes(&tex->anim, 2);
tex->anim.loop = 1;
2021-11-30 21:29:18 -06:00
2022-01-19 16:43:21 -06:00
int n;
2023-01-02 07:55:26 -06:00
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
2023-01-02 07:55:26 -06:00
glGenTextures(1, &tex->id);
glBindTexture(GL_TEXTURE_2D, tex->id);
GLenum fmt;
switch (n) {
case 1:
fmt = GL_RED;
break;
case 2:
fmt = GL_RG;
break;
case 3:
fmt = GL_RGB;
break;
case 4:
fmt = GL_RGBA;
break;
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex->width, tex->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
2023-01-02 07:55:26 -06:00
2023-01-13 22:08:39 -06:00
if (tex->opts.mips)
glGenerateMipmap(GL_TEXTURE_2D);
2023-01-02 07:55:26 -06:00
if (tex->opts.sprite) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
} else {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
stbi_image_free(data);
2022-01-19 16:43:21 -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;
}
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
2023-01-02 07:55:26 -06:00
//tex_gpu_load(new);
YughInfo("Loaded texture path %s", path);
}
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-16 02:16:39 -06:00
void texanim_fromframes(struct TexAnim *anim, int frames)
2021-11-30 21:29:18 -06:00
{
2023-01-16 02:16:39 -06:00
if (anim->st_frames) free(anim->st_frames);
2021-11-30 21:29:18 -06:00
2023-01-16 02:16:39 -06:00
anim->st_frames = calloc(frames, sizeof(*anim->st_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
{
if (tex->id != 0) {
glDeleteTextures(1, &tex->id);
tex->id = 0;
}
}
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
}
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);
}
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-01-16 02:16:39 -06:00
anim->timer = timer_make(1.f / anim->anim->ms, anim_incr, anim);
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);
}