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

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.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);
}