aboutsummaryrefslogtreecommitdiff
path: root/include/dchat/Gif.hpp
blob: b7d2efbba6979bb339d310533c6bf58cda5b138f (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
50
51
#pragma once

#include "StringView.hpp"
#include "Vec2.hpp"
#include "Color.hpp"
#include "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) {}
    };
    
    // Gif is in image format ARGB (32-bits in total)
    class Gif
    {
    public:
        // Throws GifLoadException on error
        Gif(const boost::filesystem::path &filepath);
        Gif(StringView fileContent);
        virtual ~Gif();
        
        Vec2u getSize() const;
        // Throws GifLoadException on error
        void update();
        
        static bool isDataGif(const StringView &data);
    protected:
        // Return false if texture creation failed
        virtual bool createTexture() = 0;
        // Size of texture data is same as the size that the texture was created with (also same size returned by @getSize function)
        virtual void updateTexture(void *textureData) = 0;
    private:
        void init();
    private:
        gif_animation gif;
        StringView fileContent;
        unsigned int currentFrame;
        double timeElapsedCs;
        Clock frameTimer;
        bool created;
    };
}