prosperon/source/engine/sprite.c

225 lines
5.6 KiB
C
Raw Normal View History

2021-11-30 21:29:18 -06:00
#include "sprite.h"
2022-02-06 10:14:57 -06:00
#include "timer.h"
2021-11-30 21:29:18 -06:00
#include "render.h"
#include "openglrender.h"
#include "texture.h"
#include "shader.h"
#include "datastream.h"
#include "gameobject.h"
#include <string.h>
2022-08-26 09:19:17 -05:00
#include "stb_ds.h"
2022-11-19 17:13:57 -06:00
#include "log.h"
2022-12-29 11:27:41 -06:00
#include "font.h"
2021-11-30 21:29:18 -06:00
2022-02-06 10:14:57 -06:00
struct TextureOptions TEX_SPRITE = { 1, 0, 0 };
2021-11-30 21:29:18 -06:00
static struct sprite *sprites;
static int first = -1;
2021-11-30 21:29:18 -06:00
2022-12-29 11:27:41 -06:00
static uint32_t VBO;
2021-11-30 21:29:18 -06:00
2023-01-12 17:41:54 -06:00
int make_sprite(int go)
2021-11-30 21:29:18 -06:00
{
struct sprite sprite = {
2022-08-26 09:19:17 -05:00
.color = {1.f, 1.f, 1.f},
.size = {1.f, 1.f},
2023-01-12 17:41:54 -06:00
.tex = texture_loadfromfile("ph.png"),
.go = go,
2023-01-17 13:04:08 -06:00
.next = -1,
2023-02-10 14:31:59 -06:00
.layer = 0,
2023-01-17 13:04:08 -06:00
.enabled = 1 };
2022-11-19 17:13:57 -06:00
2023-01-12 17:41:54 -06:00
int ret;
if (first<0) {
2023-01-12 17:41:54 -06:00
arrput(sprites, sprite);
arrlast(sprites).id = arrlen(sprites)-1;
return arrlen(sprites)-1;
} else {
int slot = first;
first = id2sprite(first)->next;
*id2sprite(slot) = sprite;
2023-01-12 17:41:54 -06:00
return slot;
}
2022-08-28 22:34:33 -05:00
}
void sprite_delete(int id)
{
struct sprite *sp = id2sprite(id);
sp->go = -1;
sp->next = first;
first = id;
}
2023-01-17 13:04:08 -06:00
void sprite_enabled(int id, int e)
{
sprites[id].enabled = e;
}
2023-01-12 17:41:54 -06:00
struct sprite *id2sprite(int id) {
2023-01-19 18:30:23 -06:00
if (id < 0) return NULL;
2023-01-12 17:41:54 -06:00
return &sprites[id];
2021-11-30 21:29:18 -06:00
}
void sprite_io(struct sprite *sprite, FILE *f, int read)
2021-11-30 21:29:18 -06:00
{
2022-08-28 22:34:33 -05:00
char path[100];
if (read) {
//fscanf(f, "%s", &path);
for (int i = 0; i < 100; i++) {
path[i] = fgetc(f);
if (path[i] == '\0') break;
}
2022-08-28 22:34:33 -05:00
fread(sprite, sizeof(*sprite), 1, f);
sprite_loadtex(sprite, path);
} else {
fputs(tex_get_path(sprite->tex), f);
fputc('\0', f);
2022-08-28 22:34:33 -05:00
fwrite(sprite, sizeof(*sprite), 1, f);
}
}
2022-08-26 11:38:35 -05:00
2022-07-01 11:14:43 -05:00
void sprite_draw_all()
{
2023-02-10 14:31:59 -06:00
static struct sprite **layers[5];
for (int i = 0; i < 5; i++)
arrfree(layers[i]);
2023-01-12 17:41:54 -06:00
for (int i = 0; i < arrlen(sprites); i++) {
2023-02-10 14:31:59 -06:00
if (sprites[i].go >= 0 && sprites[i].enabled) arrpush(layers[sprites[i].layer], &sprites[i]);
2023-01-12 17:41:54 -06:00
}
2023-02-10 14:31:59 -06:00
for (int i = 4; i >= 0; i--)
for (int j = 0; j < arrlen(layers[i]); j++)
sprite_draw(layers[i][j]);
2022-07-01 11:14:43 -05:00
}
void sprite_loadtex(struct sprite *sprite, const char *path)
2021-11-30 21:29:18 -06:00
{
2022-01-19 16:43:21 -06:00
sprite->tex = texture_loadfromfile(path);
2023-01-18 14:43:07 -06:00
sprite_setframe(sprite, &ST_UNIT);
2021-11-30 21:29:18 -06:00
}
void sprite_settex(struct sprite *sprite, struct Texture *tex)
2022-01-19 16:43:21 -06:00
{
sprite->tex = tex;
2023-01-18 14:43:07 -06:00
sprite_setframe(sprite, &ST_UNIT);
2022-01-19 16:43:21 -06:00
}
2021-11-30 21:29:18 -06:00
2022-12-29 17:44:19 -06:00
static uint32_t VAO = 0;
2021-11-30 21:29:18 -06:00
void sprite_initialize()
{
glGenBuffers(1, &VBO);
2022-12-29 17:44:19 -06:00
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(0);
2022-12-29 11:27:41 -06:00
}
2021-11-30 21:29:18 -06:00
2022-12-29 11:27:41 -06:00
void tex_draw(struct Texture *tex, float pos[2], float angle, float size[2], float offset[2], struct glrect r) {
2021-11-30 21:29:18 -06:00
mfloat_t model[16] = { 0.f };
mfloat_t r_model[16] = { 0.f };
mfloat_t s_model[16] = { 0.f };
memcpy(model, UNITMAT4, sizeof(UNITMAT4));
memcpy(r_model, UNITMAT4, sizeof(UNITMAT4));
memcpy(s_model, UNITMAT4, sizeof(UNITMAT4));
mfloat_t t_scale[2] = { tex->width * st_s_w(r), tex->height * st_s_h(r) };
2023-01-19 10:44:29 -06:00
mfloat_t t_offset[2] = { offset[0] * t_scale[0] * size[0], offset[1] * t_scale[1] * size[1]};
2021-11-30 21:29:18 -06:00
2022-12-24 13:18:06 -06:00
mat4_translate_vec2(model, t_offset);
2021-11-30 21:29:18 -06:00
2023-02-20 16:28:07 -06:00
mat4_scale_vec2(model, t_scale);
2022-12-24 13:18:06 -06:00
mat4_rotation_z(r_model, angle);
2021-11-30 21:29:18 -06:00
2022-12-24 13:18:06 -06:00
mat4_multiply(model, r_model, model);
2021-11-30 21:29:18 -06:00
2022-12-24 13:18:06 -06:00
mat4_translate_vec2(model, pos);
2021-11-30 21:29:18 -06:00
2022-12-24 13:18:06 -06:00
float white[3] = { 1.f, 1.f, 1.f };
2021-11-30 21:29:18 -06:00
shader_setmat4(spriteShader, "model", model);
2022-12-24 13:18:06 -06:00
shader_setvec3(spriteShader, "spriteColor", white);
2021-11-30 21:29:18 -06:00
glActiveTexture(GL_TEXTURE0);
2022-12-24 13:18:06 -06:00
glBindTexture(GL_TEXTURE_2D, tex->id);
2022-12-29 11:27:41 -06:00
glBindBuffer(GL_ARRAY_BUFFER, VBO);
float vertices[] = {
0.f, 0.f, r.s0, r.t1,
size[0], 0.f, r.s1, r.t1,
0.f, size[1], r.s0, r.t0,
size[0], size[1], r.s1, r.t0
2022-12-29 11:27:41 -06:00
};
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STREAM_DRAW);
2021-11-30 21:29:18 -06:00
2022-12-29 17:44:19 -06:00
glBindVertexArray(VAO);
2021-11-30 21:29:18 -06:00
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
2022-12-29 17:44:19 -06:00
glBindVertexArray(0);
2022-12-24 13:18:06 -06:00
}
void sprite_draw(struct sprite *sprite)
{
2023-01-12 17:41:54 -06:00
struct gameobject *go = id2go(sprite->go);
2022-12-24 13:18:06 -06:00
if (sprite->tex) {
2023-01-12 17:41:54 -06:00
cpVect cpos = cpBodyGetPosition(go->body);
2022-12-24 13:18:06 -06:00
float pos[2] = {cpos.x, cpos.y};
2023-01-16 02:16:39 -06:00
float size[2] = { sprite->size[0] * go->scale * go->flipx, sprite->size[1] * go->scale * go->flipy };
2023-01-18 17:15:36 -06:00
tex_draw(sprite->tex, pos, cpBodyGetAngle(go->body), size, sprite->pos, *sprite->frame);
2021-11-30 21:29:18 -06:00
}
}
2023-01-18 17:15:36 -06:00
void sprite_setanim(struct sprite *sprite, struct TexAnim *anim, int frame)
{
2023-01-19 18:30:23 -06:00
if (!sprite) return;
2023-01-18 17:15:36 -06:00
sprite->tex = anim->tex;
sprite->frame = &anim->st_frames[frame];
}
2022-12-24 13:18:06 -06:00
void gui_draw_img(const char *img, float x, float y) {
shader_use(spriteShader);
struct Texture *tex = texture_loadfromfile(img);
float pos[2] = {x, y};
float size[2] = {1.f, 1.f};
float offset[2] = { 0.f, 0.f };
2022-12-29 11:27:41 -06:00
tex_draw(tex, pos, 0.f, size, offset, tex_get_rect(tex));
2022-12-24 13:18:06 -06:00
}
2023-01-18 14:43:07 -06:00
void sprite_setframe(struct sprite *sprite, struct glrect *frame)
{
sprite->frame = frame;
}
2022-06-30 10:31:23 -05:00
void video_draw(struct datastream *stream, mfloat_t position[2], mfloat_t size[2], float rotate, mfloat_t color[3])
2021-11-30 21:29:18 -06:00
{
2022-07-02 03:40:50 -05:00
shader_use(vid_shader);
2021-11-30 21:29:18 -06:00
static mfloat_t model[16];
memcpy(model, UNITMAT4, sizeof(UNITMAT4));
mat4_translate_vec2(model, position);
mat4_scale_vec2(model, size);
2022-07-02 03:40:50 -05:00
shader_setmat4(vid_shader, "model", model);
shader_setvec3(vid_shader, "spriteColor", color);
2021-11-30 21:29:18 -06:00
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, stream->texture_y);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, stream->texture_cb);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, stream->texture_cr);
// TODO: video bind VAO
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}