aboutsummaryrefslogtreecommitdiff
path: root/include/dchat/Gif.hpp
blob: c870d1c5a3aad1d94de19b567b3c71cc69d3a54e (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(int width, int height) = 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;
    };
}