aboutsummaryrefslogtreecommitdiff
path: root/include/Gif.hpp
blob: 8f5d4e741413c0ed6d38d3099d75be8197972470 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#pragma once

#include "StringView.hpp"
#include <SFML/Graphics/RenderTarget.hpp>
#include <SFML/Graphics/Texture.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/System/Clock.hpp>
#include <boost/filesystem/path.hpp>
#include <stdexcept>
extern "C"
{
#include <libnsgif.h>
}

namespace dchat
{
    class GifLoadException : public std::runtime_error
    {
    public:
        GifLoadException(const std::string &errMsg) : std::runtime_error(errMsg) {}
    };
    
    class Gif
    {
    public:
        // Throws GifLoadException on error
        Gif(const boost::filesystem::path &filepath);
        Gif(StringView &&fileContent);
        ~Gif();
        
        sf::Vector2u getSize() const;
        
        void setPosition(const sf::Vector2f &position);
        void setScale(const sf::Vector2f &scale);
        void draw(sf::RenderTarget &target, const sf::RenderStates &renderStates = sf::RenderStates::Default);
        
        static bool isDataGif(const StringView &data);
    private:
        void init();
    private:
        gif_animation gif;
        StringView fileContent;
        unsigned int currentFrame;
        sf::Sprite sprite;
        sf::Texture texture;
        double timeElapsedCs;
        sf::Clock frameTimer;
    };
}