aboutsummaryrefslogtreecommitdiff
path: root/src/client/render/texture.hh
blob: bc3d00886ebd93bb7fb56b020da8a4ae9e759680 (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
#ifndef CLIENT_RENDER_TEXTURE_HH_
#define CLIENT_RENDER_TEXTURE_HH_

#include <stdexcept>
#include <string>

#include "client/render/lib/stb_image/stb_image.hh"

namespace client {
namespace render {

// Automatic freeing of a texture from stb image.
struct texture {
    unsigned char* image;
    int width;
    int height;
    int channels;

    texture(const std::string& dir, const bool flip = false) {
        stbi_set_flip_vertically_on_load(flip);
        this->image = stbi_load(dir.c_str(), &this->width, &this->height,
                                &this->channels, 0);

        if (this->image == nullptr) {
            throw std::runtime_error("stbi failed to load texture \"" + dir +
                                     '\"');
        }
    }
    texture(const texture&) = delete;
    texture(texture&&) = delete;
    ~texture() noexcept { stbi_image_free(image); }
};

} // namespace render
} // namespace client

#endif