2021-11-30 21:29:18 -06:00
|
|
|
#include "sprite.h"
|
|
|
|
|
|
|
|
#include "datastream.h"
|
2023-05-12 13:22:05 -05:00
|
|
|
#include "font.h"
|
2021-11-30 21:29:18 -06:00
|
|
|
#include "gameobject.h"
|
2022-11-19 17:13:57 -06:00
|
|
|
#include "log.h"
|
2023-05-12 13:22:05 -05:00
|
|
|
#include "render.h"
|
|
|
|
#include "stb_ds.h"
|
|
|
|
#include "texture.h"
|
|
|
|
#include "timer.h"
|
|
|
|
#include <string.h>
|
2023-05-31 14:52:30 -05:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <limits.h>
|
2023-11-09 16:44:33 -06:00
|
|
|
#include "HandmadeMath.h"
|
2023-11-28 17:17:40 -06:00
|
|
|
#include "freelist.h"
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-09-15 03:37:07 -05:00
|
|
|
#include "sprite.sglsl.h"
|
|
|
|
#include "9slice.sglsl.h"
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
struct TextureOptions TEX_SPRITE = {1, 0, 0};
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-11-28 17:17:40 -06:00
|
|
|
static struct sprite *sprites = NULL;
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-06-05 10:32:45 -05:00
|
|
|
static sg_shader shader_sprite;
|
2023-05-12 13:22:05 -05:00
|
|
|
static sg_pipeline pip_sprite;
|
|
|
|
static sg_bindings bind_sprite;
|
|
|
|
|
2023-05-31 14:52:30 -05:00
|
|
|
struct sprite_vert {
|
|
|
|
HMM_Vec2 pos;
|
2023-06-06 15:49:55 -05:00
|
|
|
HMM_Vec2 uv;
|
2023-05-31 14:52:30 -05:00
|
|
|
struct rgba color;
|
2023-11-21 01:07:50 -06:00
|
|
|
struct rgba emissive;
|
2023-05-31 14:52:30 -05:00
|
|
|
};
|
|
|
|
|
2023-06-07 08:41:09 -05:00
|
|
|
static int num_spriteverts = 5000;
|
|
|
|
|
2023-06-05 10:32:45 -05:00
|
|
|
static sg_shader slice9_shader;
|
|
|
|
static sg_pipeline slice9_pipe;
|
|
|
|
static sg_bindings slice9_bind;
|
|
|
|
static float slice9_points[8] = {
|
|
|
|
0.0, 0.0,
|
|
|
|
0.0, 1.0,
|
|
|
|
1.0, 0.0,
|
|
|
|
1.0, 1.0
|
|
|
|
};
|
|
|
|
struct slice9_vert {
|
|
|
|
HMM_Vec2 pos;
|
|
|
|
struct uv_n uv;
|
|
|
|
unsigned short border[4];
|
|
|
|
HMM_Vec2 scale;
|
|
|
|
struct rgba color;
|
|
|
|
};
|
|
|
|
|
2023-12-11 16:59:59 -06:00
|
|
|
int make_sprite(gameobject *go) {
|
2023-05-12 13:22:05 -05:00
|
|
|
struct sprite sprite = {
|
2023-11-30 10:47:59 -06:00
|
|
|
.t = t2d_unit,
|
2023-05-24 20:45:50 -05:00
|
|
|
.color = color_white,
|
2023-11-21 01:07:50 -06:00
|
|
|
.emissive = {0,0,0,0},
|
2023-12-18 17:12:05 -06:00
|
|
|
.tex = texture_pullfromfile(NULL),
|
2023-05-12 13:22:05 -05:00
|
|
|
.go = go,
|
2023-11-28 17:17:40 -06:00
|
|
|
.next = -1,
|
2023-12-27 14:16:43 -06:00
|
|
|
.enabled = 1,
|
|
|
|
.drawmode = DRAW_SIMPLE
|
|
|
|
};
|
2023-11-28 17:17:40 -06:00
|
|
|
int id;
|
|
|
|
freelist_grab(id, sprites);
|
|
|
|
sprites[id] = sprite;
|
|
|
|
return id;
|
2022-08-28 22:34:33 -05:00
|
|
|
}
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
void sprite_delete(int id) {
|
|
|
|
struct sprite *sp = id2sprite(id);
|
2023-12-11 16:59:59 -06:00
|
|
|
sp->go = NULL;
|
2023-09-14 17:37:04 -05:00
|
|
|
sp->enabled = 0;
|
2023-11-28 17:17:40 -06:00
|
|
|
freelist_kill(sprites,id);
|
2023-01-13 08:05:36 -06:00
|
|
|
}
|
|
|
|
|
2023-11-30 10:47:59 -06:00
|
|
|
void sprite_enabled(int id, int e) { sprites[id].enabled = e; }
|
2023-01-17 13:04:08 -06:00
|
|
|
|
2023-01-12 17:41:54 -06:00
|
|
|
struct sprite *id2sprite(int id) {
|
2023-05-12 13:22:05 -05:00
|
|
|
if (id < 0) return NULL;
|
|
|
|
return &sprites[id];
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2023-08-31 17:23:24 -05:00
|
|
|
static int sprite_count = 0;
|
2023-05-07 19:47:49 -05:00
|
|
|
|
2023-12-18 17:12:05 -06:00
|
|
|
void sprite_flush() { sprite_count = 0; }
|
2022-08-26 11:38:35 -05:00
|
|
|
|
2023-11-21 01:07:50 -06:00
|
|
|
int sprite_sort(int *a, int *b)
|
|
|
|
{
|
2023-12-11 08:36:45 -06:00
|
|
|
struct gameobject *goa = sprites[*a].go;
|
|
|
|
struct gameobject *gob = sprites[*b].go;
|
2023-11-21 01:07:50 -06:00
|
|
|
if (goa->drawlayer == gob->drawlayer) return 0;
|
|
|
|
if (goa->drawlayer > gob->drawlayer) return 1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
void sprite_draw_all() {
|
|
|
|
sg_apply_pipeline(pip_sprite);
|
2023-09-12 17:19:46 -05:00
|
|
|
sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, SG_RANGE_REF(projection));
|
2023-12-04 13:38:37 -06:00
|
|
|
int *layers = NULL;
|
2023-11-21 01:07:50 -06:00
|
|
|
if (layers) arrfree(layers);
|
2023-02-10 14:31:59 -06:00
|
|
|
|
2023-11-28 17:17:40 -06:00
|
|
|
for (int i = 0; i < freelist_len(sprites); i++)
|
2023-12-12 08:46:27 -06:00
|
|
|
if (sprites[i].next == -1 && sprites[i].go != NULL && sprites[i].enabled)
|
2023-11-28 17:17:40 -06:00
|
|
|
arrpush(layers, i);
|
2023-11-21 01:07:50 -06:00
|
|
|
|
2023-12-04 13:38:37 -06:00
|
|
|
if (!layers || arrlen(layers) == 0) return;
|
2023-11-21 01:07:50 -06:00
|
|
|
if (arrlen(layers) > 1)
|
|
|
|
qsort(layers, arrlen(layers), sizeof(*layers), sprite_sort);
|
2023-05-12 13:22:05 -05:00
|
|
|
|
2023-11-21 01:07:50 -06:00
|
|
|
for (int i = 0; i < arrlen(layers); i++)
|
|
|
|
sprite_draw(&sprites[layers[i]]);
|
2023-12-04 13:38:37 -06:00
|
|
|
|
|
|
|
arrfree(layers);
|
2022-07-01 11:14:43 -05:00
|
|
|
}
|
|
|
|
|
2023-12-04 13:38:37 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
void sprite_loadtex(struct sprite *sprite, const char *path, struct glrect frame) {
|
2023-11-28 17:17:40 -06:00
|
|
|
if (!sprite) {
|
|
|
|
YughWarn("NO SPRITE!");
|
|
|
|
return;
|
|
|
|
}
|
2023-12-18 17:12:05 -06:00
|
|
|
sprite->tex = texture_pullfromfile(path);
|
2023-05-12 13:22:05 -05:00
|
|
|
sprite_setframe(sprite, &frame);
|
2021-11-30 21:29:18 -06:00
|
|
|
}
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
void sprite_settex(struct sprite *sprite, struct Texture *tex) {
|
|
|
|
sprite->tex = tex;
|
|
|
|
sprite_setframe(sprite, &ST_UNIT);
|
2022-01-19 16:43:21 -06:00
|
|
|
}
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
void sprite_initialize() {
|
2023-11-28 17:17:40 -06:00
|
|
|
freelist_size(sprites, 500);
|
|
|
|
|
2023-09-15 03:37:07 -05:00
|
|
|
shader_sprite = sg_make_shader(sprite_shader_desc(sg_query_backend()));
|
2023-05-04 17:07:00 -05:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
pip_sprite = sg_make_pipeline(&(sg_pipeline_desc){
|
2023-05-04 17:07:00 -05:00
|
|
|
.shader = shader_sprite,
|
|
|
|
.layout = {
|
2023-05-12 13:22:05 -05:00
|
|
|
.attrs = {
|
2023-05-31 14:52:30 -05:00
|
|
|
[0].format = SG_VERTEXFORMAT_FLOAT2,
|
2023-06-06 15:49:55 -05:00
|
|
|
[1].format = SG_VERTEXFORMAT_FLOAT2,
|
2023-11-21 01:07:50 -06:00
|
|
|
[2].format = SG_VERTEXFORMAT_UBYTE4N,
|
|
|
|
[3].format = SG_VERTEXFORMAT_UBYTE4N}},
|
2023-05-04 17:07:00 -05:00
|
|
|
.primitive_type = SG_PRIMITIVETYPE_TRIANGLE_STRIP,
|
2023-05-24 20:45:50 -05:00
|
|
|
.label = "sprite pipeline",
|
2023-11-21 01:07:50 -06:00
|
|
|
.colors[0].blend = blend_trans,
|
2023-09-25 12:29:04 -05:00
|
|
|
.depth = {
|
2023-05-24 20:45:50 -05:00
|
|
|
.write_enabled = true,
|
2023-09-25 12:29:04 -05:00
|
|
|
.compare = SG_COMPAREFUNC_LESS_EQUAL,
|
|
|
|
.pixel_format = SG_PIXELFORMAT_DEPTH
|
2023-05-24 20:45:50 -05:00
|
|
|
}
|
|
|
|
});
|
2023-05-04 17:07:00 -05:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
bind_sprite.vertex_buffers[0] = sg_make_buffer(&(sg_buffer_desc){
|
2023-06-07 08:41:09 -05:00
|
|
|
.size = sizeof(struct sprite_vert) * num_spriteverts,
|
2023-05-04 17:07:00 -05:00
|
|
|
.type = SG_BUFFERTYPE_VERTEXBUFFER,
|
|
|
|
.usage = SG_USAGE_STREAM,
|
|
|
|
.label = "sprite vertex buffer",
|
2023-05-12 13:22:05 -05:00
|
|
|
});
|
2023-09-15 03:37:07 -05:00
|
|
|
bind_sprite.fs.samplers[0] = sg_make_sampler(&(sg_sampler_desc){});
|
2023-06-05 10:32:45 -05:00
|
|
|
|
2023-09-15 03:37:07 -05:00
|
|
|
slice9_shader = sg_make_shader(slice9_shader_desc(sg_query_backend()));
|
2023-06-05 10:32:45 -05:00
|
|
|
|
|
|
|
slice9_pipe = sg_make_pipeline(&(sg_pipeline_desc){
|
|
|
|
.shader = slice9_shader,
|
|
|
|
.layout = {
|
|
|
|
.attrs = {
|
|
|
|
[0].format = SG_VERTEXFORMAT_FLOAT2,
|
2023-09-18 21:55:37 -05:00
|
|
|
[1].format = SG_VERTEXFORMAT_FLOAT2,
|
|
|
|
[2].format = SG_VERTEXFORMAT_USHORT4N,
|
|
|
|
[3].format = SG_VERTEXFORMAT_FLOAT2,
|
|
|
|
[4].format = SG_VERTEXFORMAT_UBYTE4N
|
2023-06-05 10:32:45 -05:00
|
|
|
}},
|
|
|
|
.primitive_type = SG_PRIMITIVETYPE_TRIANGLE_STRIP,
|
|
|
|
});
|
|
|
|
|
|
|
|
slice9_bind.vertex_buffers[0] = sg_make_buffer(&(sg_buffer_desc){
|
|
|
|
.size = sizeof(struct slice9_vert) * 100,
|
|
|
|
.type = SG_BUFFERTYPE_VERTEXBUFFER,
|
|
|
|
.usage = SG_USAGE_STREAM,
|
|
|
|
});
|
2022-12-29 11:27:41 -06:00
|
|
|
}
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-12-27 14:16:43 -06:00
|
|
|
void tex_draw(struct Texture *tex, HMM_Mat3 m, struct glrect r, struct rgba color, int wrap, HMM_Vec2 wrapoffset, HMM_Vec2 wrapscale, struct rgba emissive) {
|
2023-05-31 14:52:30 -05:00
|
|
|
struct sprite_vert verts[4];
|
2023-12-04 13:38:37 -06:00
|
|
|
float w = tex->width*st_s_w(r);
|
|
|
|
float h = tex->height*st_s_h(r);
|
2023-05-31 14:52:30 -05:00
|
|
|
|
|
|
|
HMM_Vec2 sposes[4] = {
|
|
|
|
{0.0,0.0},
|
2023-12-04 13:38:37 -06:00
|
|
|
{w,0.0},
|
|
|
|
{0.0,h},
|
|
|
|
{w,h}
|
2023-05-31 17:33:04 -05:00
|
|
|
};
|
2022-12-29 11:27:41 -06:00
|
|
|
|
2023-05-31 14:52:30 -05:00
|
|
|
for (int i = 0; i < 4; i++) {
|
2023-11-21 01:07:50 -06:00
|
|
|
verts[i].pos = mat_t_pos(m, sposes[i]);
|
2023-05-31 14:52:30 -05:00
|
|
|
verts[i].color = color;
|
2023-11-21 01:07:50 -06:00
|
|
|
verts[i].emissive = emissive;
|
2023-05-31 14:52:30 -05:00
|
|
|
}
|
2023-05-07 19:47:49 -05:00
|
|
|
|
2023-12-27 14:16:43 -06:00
|
|
|
if (wrap) {
|
|
|
|
r.s1 *= wrapscale.x;
|
|
|
|
r.t1 *= wrapscale.y;
|
|
|
|
}
|
|
|
|
|
2023-12-04 13:38:37 -06:00
|
|
|
verts[0].uv.X = r.s0;
|
|
|
|
verts[0].uv.Y = r.t1;
|
|
|
|
verts[1].uv.X = r.s1;
|
|
|
|
verts[1].uv.Y = r.t1;
|
|
|
|
verts[2].uv.X = r.s0;
|
|
|
|
verts[2].uv.Y = r.t0;
|
|
|
|
verts[3].uv.X = r.s1;
|
|
|
|
verts[3].uv.Y = r.t0;
|
2023-05-31 17:33:04 -05:00
|
|
|
|
2023-09-15 03:37:07 -05:00
|
|
|
bind_sprite.fs.images[0] = tex->id;
|
|
|
|
|
2023-05-31 14:52:30 -05:00
|
|
|
sg_append_buffer(bind_sprite.vertex_buffers[0], SG_RANGE_REF(verts));
|
2023-05-12 13:22:05 -05:00
|
|
|
sg_apply_bindings(&bind_sprite);
|
2022-12-29 17:44:19 -06:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
sg_draw(sprite_count * 4, 4, 1);
|
|
|
|
sprite_count++;
|
2022-12-24 13:18:06 -06:00
|
|
|
}
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
void sprite_draw(struct sprite *sprite) {
|
2023-12-12 08:46:27 -06:00
|
|
|
if (!sprite->tex) return;
|
|
|
|
HMM_Mat3 m = t_go2world(sprite->go);
|
|
|
|
HMM_Mat3 sm = transform2d2mat(sprite->t);
|
|
|
|
|
2023-12-27 14:16:43 -06:00
|
|
|
tex_draw(sprite->tex, HMM_MulM3(m, sm), sprite->frame, sprite->color, sprite->drawmode, (HMM_Vec2){0,0}, sprite->t.scale, sprite->emissive);
|
2023-01-18 17:15:36 -06:00
|
|
|
}
|
|
|
|
|
2023-11-30 10:47:59 -06:00
|
|
|
void gui_draw_img(const char *img, transform2d t, int wrap, HMM_Vec2 wrapoffset, float wrapscale, struct rgba color) {
|
2023-05-12 13:22:05 -05:00
|
|
|
sg_apply_pipeline(pip_sprite);
|
2023-05-31 14:52:30 -05:00
|
|
|
sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, SG_RANGE_REF(hudproj));
|
2023-12-18 17:12:05 -06:00
|
|
|
struct Texture *tex = texture_pullfromfile(img);
|
2023-12-27 14:16:43 -06:00
|
|
|
tex_draw(tex, transform2d2mat(t), tex_get_rect(tex), color, wrap, wrapoffset, (HMM_Vec2){wrapscale,wrapscale}, (struct rgba){0,0,0,0});
|
2022-12-24 13:18:06 -06:00
|
|
|
}
|
|
|
|
|
2023-06-05 10:32:45 -05:00
|
|
|
void slice9_draw(const char *img, HMM_Vec2 pos, HMM_Vec2 dimensions, struct rgba color)
|
|
|
|
{
|
|
|
|
sg_apply_pipeline(slice9_pipe);
|
|
|
|
sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, SG_RANGE_REF(hudproj));
|
2023-12-18 17:12:05 -06:00
|
|
|
struct Texture *tex = texture_pullfromfile(img);
|
2023-06-05 10:32:45 -05:00
|
|
|
|
|
|
|
struct glrect r = tex_get_rect(tex);
|
|
|
|
|
|
|
|
struct slice9_vert verts[4];
|
|
|
|
|
|
|
|
HMM_Vec2 sposes[4] = {
|
|
|
|
{0.0,0.0},
|
|
|
|
{1.0,0.0},
|
|
|
|
{0.0,1.0},
|
|
|
|
{1.0,1.0},
|
|
|
|
};
|
|
|
|
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
verts[i].pos = HMM_MulV2(sposes[i], dimensions);
|
|
|
|
//verts[i].uv =z sposes[i];
|
|
|
|
verts[i].color = color;
|
|
|
|
}
|
|
|
|
|
|
|
|
verts[0].uv.u = r.s0 * USHRT_MAX;
|
|
|
|
verts[0].uv.v = r.t1 * USHRT_MAX;
|
|
|
|
verts[1].uv.u = r.s1 * USHRT_MAX;
|
|
|
|
verts[1].uv.v = r.t1 * USHRT_MAX;
|
|
|
|
verts[2].uv.u = r.s0 * USHRT_MAX;
|
|
|
|
verts[2].uv.v = r.t0 * USHRT_MAX;
|
|
|
|
verts[3].uv.u = r.s1 * USHRT_MAX;
|
|
|
|
verts[3].uv.v = r.t0 * USHRT_MAX;
|
|
|
|
|
2023-09-15 03:37:07 -05:00
|
|
|
bind_sprite.fs.images[0] = tex->id;
|
2023-06-05 10:32:45 -05:00
|
|
|
sg_append_buffer(bind_sprite.vertex_buffers[0], SG_RANGE_REF(verts));
|
|
|
|
sg_apply_bindings(&bind_sprite);
|
|
|
|
|
|
|
|
sg_draw(sprite_count * 4, 4, 1);
|
|
|
|
sprite_count++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
void sprite_setframe(struct sprite *sprite, struct glrect *frame) {
|
|
|
|
sprite->frame = *frame;
|
2023-01-18 14:43:07 -06:00
|
|
|
}
|