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"
|
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
|
|
|
|
2022-11-17 16:48:20 -06:00
|
|
|
struct sprite *sprites;
|
2021-11-30 21:29:18 -06:00
|
|
|
|
|
|
|
static uint32_t quadVAO;
|
|
|
|
|
2022-11-19 17:13:57 -06:00
|
|
|
struct sprite *make_sprite(struct gameobject *go)
|
2021-11-30 21:29:18 -06:00
|
|
|
{
|
2022-08-26 11:38:35 -05:00
|
|
|
if (arrcap(sprites) == 0)
|
|
|
|
arrsetcap(sprites, 100);
|
|
|
|
|
2022-11-17 16:48:20 -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},
|
|
|
|
.tex = texture_loadfromfile("ph.png"),
|
|
|
|
.index = arrlen(sprites) };
|
|
|
|
sprite_init(&sprite, go);
|
2022-11-19 17:13:57 -06:00
|
|
|
arrput(sprites, sprite);
|
|
|
|
|
2022-08-26 09:19:17 -05:00
|
|
|
return &arrlast(sprites);
|
2022-08-28 22:34:33 -05:00
|
|
|
}
|
|
|
|
|
2022-11-19 17:13:57 -06:00
|
|
|
void sprite_init(struct sprite *sprite, struct gameobject *go)
|
2022-08-28 22:34:33 -05:00
|
|
|
{
|
2022-08-26 11:38:35 -05:00
|
|
|
sprite->go = go;
|
2022-11-19 17:13:57 -06:00
|
|
|
|
|
|
|
YughInfo("Added sprite address %p to sprite array %p.", sprite, &arrlast(sprites));
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2022-11-17 16:48:20 -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) {
|
2022-11-17 16:48:20 -06:00
|
|
|
//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);
|
2022-11-17 16:48:20 -06:00
|
|
|
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-11-17 16:48:20 -06:00
|
|
|
void sprite_delete(struct sprite *sprite)
|
2022-08-28 22:34:33 -05:00
|
|
|
{
|
2022-11-19 17:13:57 -06:00
|
|
|
YughInfo("Attempting to delete sprite, address is %p.", sprite);
|
|
|
|
YughInfo("Number of sprites is %d.", arrlen(sprites));
|
|
|
|
for (int i = 0; i < arrlen(sprites); i++) {
|
|
|
|
YughInfo("Address of try sprite is %p.", &sprites[i]);
|
2022-08-28 22:34:33 -05:00
|
|
|
if (&sprites[i] == sprite) {
|
2022-11-19 17:13:57 -06:00
|
|
|
YughInfo("Deleted a sprite.");
|
2022-08-28 22:34:33 -05:00
|
|
|
arrdel(sprites, i);
|
|
|
|
return;
|
|
|
|
}
|
2022-11-19 17:13:57 -06:00
|
|
|
}
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2022-07-01 11:14:43 -05:00
|
|
|
void sprite_draw_all()
|
|
|
|
{
|
2022-08-26 09:19:17 -05:00
|
|
|
//shader_use(spriteShader);
|
2022-08-26 11:38:35 -05:00
|
|
|
for (int i = 0; i < arrlen(sprites); i++)
|
|
|
|
sprite_draw(&sprites[i]);
|
2022-07-01 11:14:43 -05:00
|
|
|
}
|
|
|
|
|
2022-11-17 16:48:20 -06: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);
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2022-11-17 16:48:20 -06:00
|
|
|
void sprite_loadanim(struct sprite *sprite, const char *path, struct Anim2D anim)
|
2021-11-30 21:29:18 -06:00
|
|
|
{
|
2022-01-19 16:43:21 -06:00
|
|
|
sprite->tex = texture_loadfromfile(path);
|
2021-11-30 21:29:18 -06:00
|
|
|
sprite->anim = anim;
|
2022-02-06 10:14:57 -06:00
|
|
|
sprite->anim.timer = timer_make(sprite->anim.ms, &incrementAnimFrame, sprite);
|
2021-11-30 21:29:18 -06:00
|
|
|
/*
|
2022-07-01 11:14:43 -05:00
|
|
|
sprite->tex = texture_loadanimfromfile(sprite->tex, path, sprite->anim.frames, sprite->anim.dimensions);
|
2021-11-30 21:29:18 -06:00
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2022-11-17 16:48:20 -06:00
|
|
|
void sprite_settex(struct sprite *sprite, struct Texture *tex)
|
2022-01-19 16:43:21 -06:00
|
|
|
{
|
|
|
|
sprite->tex = tex;
|
|
|
|
}
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2022-11-17 16:48:20 -06:00
|
|
|
unsigned int incrementAnimFrame(unsigned int interval, struct sprite *sprite)
|
2021-11-30 21:29:18 -06:00
|
|
|
{
|
|
|
|
sprite->anim.frame = (sprite->anim.frame + 1) % sprite->anim.frames;
|
|
|
|
return interval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: This should be done once for all sprites
|
|
|
|
void sprite_initialize()
|
|
|
|
{
|
|
|
|
uint32_t VBO;
|
|
|
|
float vertices[] = {
|
|
|
|
// pos
|
|
|
|
0.f, 0.f,
|
|
|
|
1.0f, 0.0f,
|
|
|
|
0.f, 1.f,
|
|
|
|
1.f, 1.f
|
|
|
|
};
|
|
|
|
|
|
|
|
glGenVertexArrays(1, &quadVAO);
|
|
|
|
glGenBuffers(1, &VBO);
|
|
|
|
|
|
|
|
glBindVertexArray(quadVAO);
|
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
2022-06-30 10:31:23 -05:00
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), &vertices, GL_STATIC_DRAW);
|
2021-11-30 21:29:18 -06:00
|
|
|
|
|
|
|
|
|
|
|
glEnableVertexAttribArray(0);
|
|
|
|
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
|
|
|
|
}
|
|
|
|
|
2022-12-24 13:18:06 -06:00
|
|
|
void tex_draw(struct Texture *tex, float pos[2], float angle, float size[2], float offset[2]) {
|
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));
|
|
|
|
|
2022-12-24 13:18:06 -06:00
|
|
|
mfloat_t t_scale[2] = { size[0] * tex->width, size[1] * tex->height };
|
|
|
|
mfloat_t t_offset[2] = { offset[0] * t_scale[0], offset[1] * t_scale[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
|
|
|
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
|
|
|
|
|
|
|
//tex_bind(sprite->tex);
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
2022-12-24 13:18:06 -06:00
|
|
|
glBindTexture(GL_TEXTURE_2D, tex->id);
|
2021-11-30 21:29:18 -06:00
|
|
|
|
|
|
|
glBindVertexArray(quadVAO);
|
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
2022-12-24 13:18:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void sprite_draw(struct sprite *sprite)
|
|
|
|
{
|
|
|
|
if (sprite->tex) {
|
|
|
|
cpVect cpos = cpBodyGetPosition(sprite->go->body);
|
|
|
|
float pos[2] = {cpos.x, cpos.y};
|
|
|
|
|
|
|
|
float size[2] = { sprite->size[0] * sprite->go->scale, sprite->size[1] * sprite->go->scale };
|
|
|
|
|
|
|
|
tex_draw(sprite->tex, pos, cpBodyGetAngle(sprite->go->body), size, sprite->pos);
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 };
|
|
|
|
tex_draw(tex, pos, 0.f, size, offset);
|
|
|
|
}
|
|
|
|
|
2022-11-17 16:48:20 -06:00
|
|
|
void spriteanim_draw(struct sprite *sprite)
|
2021-11-30 21:29:18 -06:00
|
|
|
{
|
|
|
|
shader_use(animSpriteShader);
|
|
|
|
|
|
|
|
mfloat_t model[16] = { 0.f };
|
|
|
|
memcpy(model, UNITMAT4, sizeof(UNITMAT4));
|
|
|
|
|
|
|
|
mat4_translate_vec2(model, sprite->pos);
|
|
|
|
mfloat_t msize[2] =
|
|
|
|
{ sprite->size[0] * sprite->anim.dimensions[0],
|
2022-07-02 03:40:50 -05:00
|
|
|
sprite->size[1] * sprite->anim.dimensions[1] };
|
2021-11-30 21:29:18 -06:00
|
|
|
mat4_scale_vec2(model, msize);
|
|
|
|
|
|
|
|
shader_setmat4(animSpriteShader, "model", model);
|
|
|
|
shader_setvec3(animSpriteShader, "spriteColor", sprite->color);
|
|
|
|
shader_setfloat(animSpriteShader, "frame", sprite->anim.frame);
|
|
|
|
|
|
|
|
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
|
|
glBindTexture(GL_TEXTURE_2D_ARRAY, sprite->tex->id);
|
|
|
|
|
|
|
|
glBindVertexArray(quadVAO);
|
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
2022-07-02 03:40:50 -05:00
|
|
|
glBindVertexArray(quadVAO);
|
2021-11-30 21:29:18 -06:00
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
|
|
|
}
|
2022-01-19 16:43:21 -06:00
|
|
|
|