Nimbin[12]?SDL & Graphics / SDL_Game_CPP / src/character/animation.hpp

SDL_Game_CPP git · main

SDL game in C++

sdl game c++ · first commit 2026-03-24 · last commit 2026-03-24 (8 months ago) · synced 1 hour ago · upstream: codeberg.org/Studying_at_HsNR/SDL_Game_CPP

C++ 99.3%
git clone https://git.christianimmanuel.de/sdl-graphics/SDL_Game_CPP.gitwget https://git.christianimmanuel.de/sdl-graphics/SDL_Game_CPP/archive/SDL_Game_CPP.tar.gz
src/character/animation.hpp 1.8 KB · 59 lines raw
#pragma once


#include <SDL3/SDL.h>
#include <SDL3_image/SDL_image.h>

#include <vector>
#include <unordered_map>
#include <string>
#include <cmath>
#include <iostream>

#include "defaults/defaults.hpp"
#include "audio/audio.hpp"

class Game;

struct AnimationData {
    SDL_Texture* texture;
    SDL_Texture* txt_back;
    SDL_Surface* surface = nullptr;
    std::vector<SDL_FRect> frames;
    size_t current_frame = 0;
    float frame_time = 0.1f;
    float timer = 0.0f;
    bool has_audio = false;
    std::string audio_name;
    bool reverse = false;
    bool restart = false;
};

struct CharacterAnimation {
    std::unordered_map<WalkDirection, AnimationData> idle;
    std::unordered_map<WalkDirection, AnimationData> walk;
    std::unordered_map<WalkDirection, AnimationData> run;
    std::unordered_map<WalkDirection, AnimationData> collect;
    std::unordered_map<WalkDirection, AnimationData> cultivating;
    std::unordered_map<WalkDirection, AnimationData> planting;
    std::unordered_map<WalkDirection, AnimationData> build;
    std::unordered_map<WalkDirection, AnimationData> hit;
    std::unordered_map<WalkDirection, AnimationData> attack_sword;
    std::unordered_map<WalkDirection, AnimationData> hit_woodaxe_stone;
    std::unordered_map<WalkDirection, AnimationData> death;
    std::unordered_map<WalkDirection, AnimationData> craft;
    std::unordered_map<WalkDirection, AnimationData> block;
    std::unordered_map<WalkDirection, AnimationData> drinking_river;
    std::unordered_map<WalkDirection, AnimationData> eating;
};

namespace Animation {
    extern float animation_frame_time;
    extern Audio* audio;

    AnimationData
    load(SDL_Renderer* renderer, const std::string& path, float frame_width, float frame_height, std::string audio_name, bool reverse, bool restart);

    void
    update(AnimationData& anim, float delta_time);
}