src/sprite.c

Fri, 25 Jul 2025 18:54:51 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 25 Jul 2025 18:54:51 +0200
changeset 224
f72b80448413
parent 223
4f32c7755138
child 226
18327d2df79d
permissions
-rw-r--r--

fix broken nullptr handling in asc_shader_use() - fixes #696

/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * Copyright 2025 Mike Becker. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "ascension/sprite.h"

#include "ascension/context.h"
#include "ascension/glcontext.h"
#include "ascension/error.h"
#include "ascension/mesh.h"
#include "ascension/shader.h"
#include "ascension/constants.h"


typedef struct asc_sprite_shader_s {
    AscShaderProgram program;
    asc_uniform_loc tex;
} AscSpriteShader;

static void asc_sprite_shader_init(AscShaderProgram *p, cx_attr_unused int flags) {
    asc_shader_init_uniform_loc_nice(p, AscSpriteShader, tex);
}

static AscShaderProgram *asc_sprite_shader_create(int rect) {
    return asc_shader_create((AscShaderCodes) {
        .vtx = {.source_file = "sprite_vtx.glsl"},
        .frag = {
            .source_file = "sprite_frag.glsl",
            .preamble_code = (const char*[]){"#define USE_RECT"},
            .preamble_code_flags = (uint64_t) rect
        },
    }, sizeof(AscSpriteShader), asc_sprite_shader_init, 0);
}

static void asc_sprite_destroy(AscSceneNode *node) {
    asc_ptr_cast(AscSprite, sprite, node);
    asc_mesh_destroy(&sprite->mesh);
    sprite->texture->refcount--;
}

static void asc_sprite_update(AscSceneNode *node) {
    asc_ptr_cast(AscSprite, sprite, node);

    // calculate texture parameters
    asc_vec2f uv_scale;
    if (sprite->texture_scale_mode == ASC_TEXTURE_SCALE_REPEAT) {
        uv_scale = asc_texture_calculate_uv_scale(sprite->texture,
            ASC_VEC2U(sprite->width, sprite->height), sprite->texture_scale);
    } else {
        uv_scale = sprite->texture_scale;
    }

    // update mesh
    asc_mesh_plane_2d(&sprite->mesh,
        .size = ASC_VEC2F(sprite->width, sprite->height),
        .uv_scale = uv_scale
    );
}

void asc_sprite_draw(const AscCamera *camera, const AscSceneNode *node) {
    asc_cptr_cast(AscSprite, sprite, node);

    // Activate shader
    // TODO: scene should know which shader we are going to activate s.t. it can pre-sort nodes
    const AscShaderProgram *shader =
        asc_texture_is_uv_normalized(sprite->texture)
                ? asc_shader_lookup_or_create(ASC_SHADER_SPRITE_UV, asc_sprite_shader_create, 0)
                : asc_shader_lookup_or_create(ASC_SHADER_SPRITE_RECT, asc_sprite_shader_create, 1);
    if (asc_shader_use(shader, camera)) return;
    asc_cptr_cast(AscSpriteShader, sprite_shader, shader);

    // Upload model matrix
    asc_shader_upload_model_matrix(shader, node);

    // Bind texture
    asc_texture_bind(sprite->texture, sprite_shader->tex, 0);

    // Draw mesh
    asc_mesh_draw_triangle_strip(&sprite->mesh);
}

AscSceneNode *asc_sprite_create(struct asc_sprite_create_args args) {
    AscSprite *sprite = cxZallocDefault(sizeof(AscSprite));

    // sprite parameters
    args.texture->refcount++;
    sprite->texture = args.texture;
    sprite->width = args.width;
    sprite->height = args.height;
    sprite->texture_scale_mode = args.texture_scale_mode;
    sprite->texture_scale.u = ASC_NONZERO_OR(1.f, args.texture_scale_x);
    sprite->texture_scale.v = ASC_NONZERO_OR(1.f, args.texture_scale_y);

    // basic node parameters
    asc_ptr_cast(AscSceneNode, node, sprite);
    asc_scene_node_init(node,
        ASC_SCENE_NODE_FUNCS(asc_sprite),
        .name = args.name,
        .render_group = args.opaque
                             ? ASC_RENDER_GROUP_2D_OPAQUE
                             : ASC_RENDER_GROUP_2D_BLEND,
        .pos2d = ASC_VEC2I(args.x, args.y),
        .origin2d = ASC_VEC2I(args.origin_x, args.origin_y)
    );

    return node;
}

void asc_sprite_set_size(AscSprite *sprite, asc_vec2u size) {
    sprite->width = size.width;
    sprite->height = size.height;
    asc_scene_node_update((AscSceneNode *) sprite);
}

mercurial