Nimbin[12]?SDL & Graphics / mapcreator / src/character/animation.cpp

mapcreator git · master

SDL3 2.5D game and engine using assets, with map editor

sdl3 c++ game engine map-editor cmake · first commit 2025-10-31 · last commit 2026-09-07 (1 month ago) · synced 3 days ago · upstream: git.ide3.de/hsnr/sdl-spieleentwicklung/map_creator

C++ 99.3%
git clone https://git.christianimmanuel.de/sdl-graphics/mapcreator.gitwget https://git.christianimmanuel.de/sdl-graphics/mapcreator/archive/mapcreator.tar.gz
src/character/animation.cpp 2.6 KB · 88 lines raw
#include "character/animation.hpp"
#include "game/game.hpp"
#include "audio/audio.hpp"


namespace Animation {

    float animation_frame_time = 0.1f;
    Audio* audio = nullptr;

    AnimationData
    load(SDL_Renderer* renderer,
                            const std::string& path,
                            float frame_width,
                            float frame_height,
                            std::string audio_name_in,
                            bool re, bool rs) {
        SDL_Surface* surf = IMG_Load(path.c_str());
        SDL_Texture* tex = SDL_CreateTextureFromSurface(renderer, surf);
        SDL_SetTextureScaleMode(tex, SDL_SCALEMODE_NEAREST);
        if (!tex) {
            std::cerr << "Failed to load texture: " << path << '\n';
            return {};
        }

        float tex_w = 0.0f;
        float tex_h = 0.0f;
        if (!SDL_GetTextureSize(tex, &tex_w, &tex_h)) {
            std::cerr << "Failed to get texture size: " << SDL_GetError() << '\n';
            SDL_DestroyTexture(tex);
            return {};
        }

        const float frame_count = static_cast<int>(tex_w / frame_width);

        AnimationData anim;
        anim.texture = tex;
        anim.surface = surf;
        anim.frames.reserve(frame_count);

        for (int i = 0; i < frame_count; ++i) {
            anim.frames.push_back({
                SDL_FRect{ i * frame_width, 0, frame_width, frame_height }
            });
        }

        // Audio
        anim.audio_name = audio_name_in;
        if (anim.audio_name != "None") {
            anim.has_audio = true;
            std::string p = "music/" + anim.audio_name + ".wav";
            audio->RegisterSound(anim.audio_name.c_str(), p.c_str());
        }

        anim.reverse = re; anim.restart = rs;

        return anim;
    }


    void
    update(AnimationData& anim, float delta_time) {
        anim.timer += delta_time;
        if (anim.timer >= animation_frame_time) {
            anim.timer = 0.0f;

            if (!anim.reverse) {
                anim.current_frame =
                    (anim.current_frame + 1) % anim.frames.size();
            } else {
                if (anim.current_frame == 0) {
                    anim.current_frame = anim.frames.size() - 1;
                } else {
                    anim.current_frame--;
                }
            }

            if (anim.current_frame == 0 && anim.restart) {
                    audio->Stop(anim.audio_name.c_str());
            }
            if (anim.has_audio) {
                if (!audio->is_Playing(anim.audio_name.c_str())) {
                    audio->Play(anim.audio_name.c_str());
                }
            }
        }
    }
}