aboutsummaryrefslogtreecommitdiff
path: root/src/graphics/Texture.cpp
blob: 3b1f410a5cf8440de02a92b7b394bb2bb31e485f (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "../../include/mglpp/graphics/Texture.hpp"
#include "../../include/mglpp/graphics/Image.hpp"
#include <string.h>

namespace mgl {
    Texture::Texture() {
        memset(&texture, 0, sizeof(mgl_texture));
    }

    Texture::Texture(unsigned int gl_texture_id, mgl_texture_format format, const ReferenceOptions reference_options) {
        memset(&texture, 0, sizeof(mgl_texture));
        const mgl_texture_reference_options texture_reference_options = {
            reference_options.pixel_coordinates
        };
        mgl_texture_init_reference_existing_gl_texture(&texture, gl_texture_id, format, &texture_reference_options);
    }

    Texture::Texture(Texture &&other) {
        memcpy(&texture, &other.texture, sizeof(mgl_texture));
        owned = other.owned;

        other.texture.id = 0;
        other.owned = false;
    }

    Texture& Texture::operator=(Texture &&other) {
        if(owned)
            mgl_texture_unload(&texture);

        memcpy(&texture, &other.texture, sizeof(mgl_texture));
        owned = other.owned;

        other.texture.id = 0;
        other.owned = false;
        return *this;
    }

    Texture::~Texture() {
        if(owned)
            mgl_texture_unload(&texture);
    }

    // static
    Texture Texture::reference(mgl_texture ref) {
        Texture instance;
        instance.texture = ref;
        instance.owned = false;
        return instance;
    }

    bool Texture::load_from_file(const char *filepath, const LoadOptions load_options) {
        if(texture.id)
            clear();

        if(mgl_texture_init(&texture) != 0)
            return false;

        const mgl_texture_load_options texture_load_options = {
            load_options.compressed,
            load_options.pixel_coordinates,
            load_options.mipmap
        };
        
        if(mgl_texture_load_from_file(&texture, filepath, &texture_load_options) == 0) {
            return true;
        } else {
            clear();
            return false;
        }
    }

    bool Texture::load_from_image(Image &image, const LoadOptions load_options) {
        if(texture.id)
            clear();

        if(mgl_texture_init(&texture) != 0)
            return false;

        const mgl_texture_load_options texture_load_options = {
            load_options.compressed,
            load_options.pixel_coordinates,
            load_options.mipmap
        };
        
        if(mgl_texture_load_from_image(&texture, image.internal_image(), &texture_load_options) == 0) {
            return true;
        } else {
            clear();
            return false;
        }
    }

    bool Texture::load_from_memory(const unsigned char *data, int width, int height, mgl_image_format format, LoadOptions load_options) {
        if(texture.id)
            clear();

        if(mgl_texture_init(&texture) != 0)
            return false;

        const mgl_texture_load_options texture_load_options = {
            load_options.compressed,
            load_options.pixel_coordinates,
            load_options.mipmap
        };
        
        if(mgl_texture_load_from_memory(&texture, data, width, height, format, &texture_load_options) == 0) {
            return true;
        } else {
            clear();
            return false;
        }
    }

    void Texture::clear() {
        if(owned)
            mgl_texture_unload(&texture);
        memset(&texture, 0, sizeof(mgl_texture));
        owned = true;
    }

    vec2i Texture::get_size() const {
        return { texture.width, texture.height };
    }

    bool Texture::is_valid() const {
        return texture.id != 0;
    }

    mgl_texture* Texture::internal_texture() {
        return &texture;
    }
}