2021-11-30 21:29:18 -06:00
|
|
|
#include "sprite.h"
|
|
|
|
|
|
|
|
#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"
|
2023-11-09 16:44:33 -06:00
|
|
|
#include "HandmadeMath.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"
|
|
|
|
|
2024-04-30 10:32:27 -05:00
|
|
|
sg_pipeline pip_sprite;
|
2024-04-21 10:05:18 -05:00
|
|
|
sg_bindings bind_sprite;
|
2023-05-12 13:22:05 -05:00
|
|
|
|
2023-06-05 10:32:45 -05:00
|
|
|
static sg_shader slice9_shader;
|
|
|
|
static sg_pipeline slice9_pipe;
|
|
|
|
static sg_bindings slice9_bind;
|
2024-04-23 15:58:08 -05:00
|
|
|
|
2023-06-05 10:32:45 -05:00
|
|
|
struct slice9_vert {
|
|
|
|
HMM_Vec2 pos;
|
|
|
|
struct uv_n uv;
|
|
|
|
unsigned short border[4];
|
|
|
|
HMM_Vec2 scale;
|
|
|
|
struct rgba color;
|
|
|
|
};
|
|
|
|
|
2024-03-13 03:51:44 -05:00
|
|
|
sprite *sprite_make()
|
|
|
|
{
|
|
|
|
sprite *sp = calloc(sizeof(*sp), 1);
|
2024-04-23 15:58:08 -05:00
|
|
|
sp->pos = v2zero;
|
|
|
|
sp->scale = v2one;
|
2024-03-13 03:51:44 -05:00
|
|
|
sp->angle = 0;
|
|
|
|
sp->color = color_white;
|
|
|
|
sp->emissive = color_clear;
|
2024-04-23 15:58:08 -05:00
|
|
|
sp->spritesize = v2one;
|
|
|
|
sp->spriteoffset = v2zero;
|
2024-03-13 03:51:44 -05:00
|
|
|
return sp;
|
2023-01-13 08:05:36 -06:00
|
|
|
}
|
|
|
|
|
2024-04-21 10:05:18 -05:00
|
|
|
void sprite_free(sprite *sprite) { free(sprite); }
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2024-04-21 10:05:18 -05:00
|
|
|
static texture *loadedtex;
|
2024-04-23 15:58:08 -05:00
|
|
|
static int sprite_count = 0;
|
2023-05-07 19:47:49 -05:00
|
|
|
|
2023-05-12 13:22:05 -05:00
|
|
|
void sprite_initialize() {
|
|
|
|
pip_sprite = sg_make_pipeline(&(sg_pipeline_desc){
|
2024-04-30 10:32:27 -05:00
|
|
|
.shader = sg_make_shader(sprite_shader_desc(sg_query_backend())),
|
2024-04-21 10:05:18 -05:00
|
|
|
.layout = {
|
|
|
|
.attrs = {
|
2024-04-23 15:58:08 -05:00
|
|
|
[0].format = SG_VERTEXFORMAT_FLOAT2
|
|
|
|
},
|
|
|
|
},
|
2024-04-21 10:05:18 -05:00
|
|
|
.primitive_type = SG_PRIMITIVETYPE_TRIANGLE_STRIP,
|
|
|
|
.label = "sprite pipeline",
|
|
|
|
.colors[0].blend = blend_trans,
|
2023-05-24 20:45:50 -05:00
|
|
|
});
|
2023-05-04 17:07:00 -05:00
|
|
|
|
2024-04-23 15:58:08 -05:00
|
|
|
bind_sprite.vertex_buffers[0] = sprite_quad;
|
|
|
|
bind_sprite.fs.samplers[0] = std_sampler;
|
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,
|
2024-03-26 07:53:36 -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,
|
|
|
|
});
|
|
|
|
|
2024-04-23 15:58:08 -05:00
|
|
|
slice9_bind.vertex_buffers[0] = sprite_quad;
|
2022-12-29 11:27:41 -06:00
|
|
|
}
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2024-04-21 10:05:18 -05:00
|
|
|
void sprite_pipe()
|
|
|
|
{
|
|
|
|
sg_apply_pipeline(pip_sprite);
|
2024-04-23 15:58:08 -05:00
|
|
|
sg_apply_uniforms(SG_SHADERSTAGE_VS, SLOT_vp, SG_RANGE_REF(useproj));
|
2024-04-21 10:05:18 -05:00
|
|
|
}
|
|
|
|
|
2024-03-13 03:51:44 -05:00
|
|
|
transform2d sprite2t(sprite *s)
|
|
|
|
{
|
|
|
|
return (transform2d){
|
|
|
|
.pos = s->pos,
|
2024-04-23 15:58:08 -05:00
|
|
|
.scale = HMM_MulV2(s->scale, (HMM_Vec2){loadedtex->width, loadedtex->height}),
|
2024-04-03 08:37:29 -05:00
|
|
|
.angle = HMM_TurnToRad*s->angle
|
2024-03-13 03:51:44 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-04-21 10:05:18 -05:00
|
|
|
void sprite_tex(texture *t)
|
|
|
|
{
|
|
|
|
loadedtex = t;
|
|
|
|
bind_sprite.fs.images[0] = t->id;
|
|
|
|
}
|
|
|
|
|
2024-04-30 10:32:27 -05:00
|
|
|
void sprite_setpipe(sg_pipeline p)
|
|
|
|
{
|
|
|
|
pip_sprite = p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tex_draw(texture *tex, gameobject *go)
|
|
|
|
{
|
|
|
|
HMM_Mat4 m = transform2d2mat4(go2t(go));
|
|
|
|
|
|
|
|
sg_apply_uniforms(SG_SHADERSTAGE_VS, 1, SG_RANGE_REF(m.e));
|
|
|
|
|
|
|
|
sg_bindings bind = {0};
|
|
|
|
bind.vertex_buffers[0] = sprite_quad;
|
|
|
|
bind.fs.images[0] = tex->id;
|
|
|
|
bind.fs.samplers[0] = std_sampler;
|
|
|
|
sg_apply_bindings(&bind);
|
|
|
|
sg_draw(0,4,1);
|
|
|
|
}
|
|
|
|
|
2024-04-21 10:05:18 -05:00
|
|
|
void sprite_draw(struct sprite *sprite, gameobject *go) {
|
2024-04-23 15:58:08 -05:00
|
|
|
HMM_Mat4 m = transform2d2mat4(go2t(go));
|
|
|
|
HMM_Mat4 sm = transform2d2mat4(sprite2t(sprite));
|
|
|
|
struct spriteuni spv;
|
|
|
|
rgba2floats(&spv.color.e, sprite->color);
|
|
|
|
rgba2floats(spv.emissive.e, sprite->emissive);
|
|
|
|
spv.size = sprite->spritesize;
|
|
|
|
spv.offset = sprite->spriteoffset;
|
|
|
|
spv.model = HMM_MulM4(m,sm);
|
2024-04-30 10:32:27 -05:00
|
|
|
|
|
|
|
sg_bindings bind = {0};
|
|
|
|
bind.vertex_buffers[0] = sprite_quad;
|
|
|
|
bind.fs.images[0] = loadedtex->id;
|
|
|
|
bind.fs.samplers[0] = std_sampler;
|
2024-04-23 15:58:08 -05:00
|
|
|
sg_draw(0,4,1);
|
2023-01-18 17:15:36 -06:00
|
|
|
}
|
|
|
|
|
2024-03-26 07:53:36 -05:00
|
|
|
void gui_draw_img(texture *tex, transform2d t, int wrap, HMM_Vec2 wrapoffset, float wrapscale, struct rgba color) {
|
2024-03-27 15:00:59 -05:00
|
|
|
sg_apply_pipeline(pip_sprite);
|
2024-04-23 15:58:08 -05:00
|
|
|
sg_apply_uniforms(SG_SHADERSTAGE_VS, SLOT_vp, SG_RANGE_REF(useproj));
|
2024-04-21 10:05:18 -05:00
|
|
|
sprite_tex(tex);
|
2024-04-28 13:33:37 -05:00
|
|
|
//tex_draw(transform2d2mat(t), ST_UNIT, color, wrap, wrapoffset, (HMM_Vec2){wrapscale,wrapscale}, (struct rgba){0,0,0,0});
|
2022-12-24 13:18:06 -06:00
|
|
|
}
|
2024-03-27 15:00:59 -05:00
|
|
|
|
|
|
|
void slice9_draw(texture *tex, transform2d *t, HMM_Vec4 border, struct rgba color)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|