Nimbin[12]?SDL & Graphics / mapcreator / src/cursor/animated_cursor.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/cursor/animated_cursor.hpp 1.9 KB · 75 lines raw
// g++ -std=c++23 -O2 -Wall -Wextra animated_cursor.hpp.cpp  $(pkg-config --cflags --libs sdl3 sdl3-image) -o cursor_particles && ./cursor_particles

#pragma once

#include <SDL3/SDL.h>
#include <SDL3_image/SDL_image.h>
#include <vector>
#include <algorithm>
#include <random>
#include <cmath>



enum class CursorState : uint32_t {
    NORMAL,
    ANIMATED,
    HIDDEN
};

struct Pixel {
    int srcX, srcY;
    float currentAngle;  // Current angle in sphere
    float currentRadius; // Current radius in sphere
    float targetAngle;   // Target angle for movement
    float targetRadius;  // Target radius for movement
    float angleSpeed;    // Speed of angle change
    float radiusSpeed;   // Speed of radius change
    SDL_Color originalColor;
    SDL_Color shuffledColor;
};

class AnimatedCursor {
private:
    SDL_Texture* spriteTexture;
    SDL_Surface* spriteSurface;

    std::vector<Pixel> pixels;
    std::vector<std::vector<Pixel>> allFrames; // Store all 8 frames
    bool isReversing;
    float reverseProgress;
    float time; // For continuous animation
    float clickX, clickY; // Position where click happened
    int currentFrame; // 0-7 for the 8 frames
    bool isPlayingAnimation; // True when playing through frames

    const int FRAME_WIDTH = 64;  // 512 / 8 frames
    const int FRAME_HEIGHT = 64;
    const int SPHERE_RADIUS = 16; // 32px diameter sphere
    const size_t TOTAL_FRAMES = 8;
    const float ANIMATION_DURATION = 1.2f; // Total time for unshuffle + frame play

public:
    CursorState  state = CursorState::NORMAL;
    float mouseX, mouseY; // Sphere follows cursor
    AnimatedCursor();

    ~AnimatedCursor();

    bool Init(SDL_Renderer* renderer, const char* filename);

    void set_State(const CursorState& state_new);

    void frame_ExtractFirst();

    void state_GenerateShuffle();

    void Render(SDL_Renderer* renderer);

    void Update(float deltaTime, float mx, float my);

    void mouse_OnClick();


    void Cleanup();
};