--- a/src/sprite.c Tue Jun 10 19:29:07 2025 +0200 +++ b/src/sprite.c Wed Jun 11 23:38:55 2025 +0200 @@ -31,6 +31,7 @@ #include "ascension/glcontext.h" #include "ascension/error.h" #include "ascension/mesh.h" +#include "ascension/shader.h" #include "ascension/constants.h" #include <GL/glew.h> @@ -41,6 +42,8 @@ int tex; }; +typedef struct asc_sprite_shader_s AscSpriteShader; + static void *asc_sprite_shader_create(bool rect) { AscShaderCodes codes; if (asc_shader_load_code_files((AscShaderCodeInfo){ @@ -51,7 +54,7 @@ asc_error("Loading sprite shader failed."); return NULL; } - struct asc_sprite_shader_s *shader = asc_shader_create(codes, sizeof(*shader)); + AscSpriteShader *shader = asc_shader_create(codes, sizeof(*shader)); if (asc_has_error()) { asc_shader_free_codes(codes); return NULL; @@ -72,6 +75,14 @@ return asc_sprite_shader_create(false); } +static const AscSpriteShader *asc_sprite_shader_rect(void) { + return asc_shader_lookup_or_create(ASC_SHADER_SPRITE_RECT, asc_sprite_shader_rect_create); +} + +static const AscSpriteShader *asc_sprite_shader_uv(void) { + return asc_shader_lookup_or_create(ASC_SHADER_SPRITE_UV, asc_sprite_shader_uv_create); +} + static void asc_sprite_destroy(AscSceneNode *node) { AscSprite *sprite = (AscSprite *) node; asc_mesh_destroy(&sprite->mesh); @@ -113,8 +124,8 @@ AscSceneNode *node = (AscSceneNode *) sprite; asc_scene_node_name(node, args.name); node->render_group = args.opaque - ? ASC_RENDER_GROUP_SPRITE_OPAQUE_UV - : ASC_RENDER_GROUP_SPRITE_BLEND_UV; + ? ASC_RENDER_GROUP_SPRITE_OPAQUE + : ASC_RENDER_GROUP_SPRITE_BLEND; node->update_func = asc_sprite_update; node->destroy_func = asc_sprite_destroy; @@ -125,16 +136,13 @@ return node; } -const AscShaderProgram *asc_sprite_shader_rect(void) { - return asc_shader_lookup_or_create(ASC_SHADER_SPRITE_RECT, asc_sprite_shader_rect_create); -} - -const AscShaderProgram *asc_sprite_shader_uv(void) { - return asc_shader_lookup_or_create(ASC_SHADER_SPRITE_UV, asc_sprite_shader_uv_create); -} - -void asc_sprite_draw(const AscShaderProgram *program, const AscSprite *node) { - asc_ptr_cast(const struct asc_sprite_shader_s, shader, program); +void asc_sprite_draw(const AscCamera *camera, const AscSprite *node) { + // Activate shader + const AscSpriteShader *shader = + node->texture->target == GL_TEXTURE_RECTANGLE + ? asc_sprite_shader_rect() + : asc_sprite_shader_uv(); + asc_shader_use(&shader->program, camera); // Upload model matrix glUniformMatrix4fv(shader->program.model, 1,