summaryrefslogtreecommitdiffstats
path: root/game/client/texture_gui.cc
blob: 265f6c2003a47b2e431161bfd6744ef4d513194f (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
#include "client/pch.hh"

#include "client/texture_gui.hh"

#include "core/image.hh"
#include "core/resource.hh"

static emhash8::HashMap<std::string, resource_ptr<TextureGUI>> resource_map;

template<>
resource_ptr<TextureGUI> resource::load<TextureGUI>(const char* name, unsigned int flags)
{
    auto it = resource_map.find(name);

    if(it != resource_map.cend()) {
        // Return an existing resource
        return it->second;
    }

    unsigned int image_load_flags = 0U;

    if(flags & TEXTURE_GUI_LOAD_VFLIP) {
        image_load_flags |= IMAGE_LOAD_FLIP;
    }

    if(flags & TEXTURE_GUI_LOAD_GRAYSCALE) {
        image_load_flags |= IMAGE_LOAD_GRAY;
    }

    if(auto image = resource::load<Image>(name, image_load_flags)) {
        GLuint gl_texture;

        glGenTextures(1, &gl_texture);
        glBindTexture(GL_TEXTURE_2D, gl_texture);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image->size.x, image->size.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);

        if(flags & TEXTURE_GUI_LOAD_CLAMP_S) {
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        } else {
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
        }

        if(flags & TEXTURE_GUI_LOAD_CLAMP_T) {
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        } else {
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
        }

        if(flags & TEXTURE_GUI_LOAD_LINEAR_MAG) {
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        } else {
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        }

        if(flags & TEXTURE_GUI_LOAD_LINEAR_MIN) {
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        } else {
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        }

        auto new_resource = std::make_shared<TextureGUI>();
        new_resource->handle = static_cast<ImTextureID>(gl_texture);
        new_resource->size.x = image->size.x;
        new_resource->size.y = image->size.y;

        return resource_map.insert_or_assign(name, new_resource).first->second;
    }

    return nullptr;
}

template<>
void resource::hard_cleanup<TextureGUI>(void)
{
    for(const auto& it : resource_map) {
        if(it.second.use_count() > 1L) {
            spdlog::warn("resource: zombie resource [TextureGUI] {} [use_count={}]", it.first, it.second.use_count());
        } else {
            spdlog::debug("resource: releasing [TextureGUI] {}", it.first);
        }

        auto gl_texture = static_cast<GLuint>(it.second->handle);

        glDeleteTextures(1, &gl_texture);
    }

    resource_map.clear();
}

template<>
void resource::soft_cleanup<TextureGUI>(void)
{
    auto iter = resource_map.cbegin();

    while(iter != resource_map.cend()) {
        if(iter->second.use_count() == 1L) {
            spdlog::debug("resource: releasing [TextureGUI] {}", iter->first);

            auto gl_texture = static_cast<GLuint>(iter->second->handle);

            glDeleteTextures(1, &gl_texture);

            iter = resource_map.erase(iter);

            continue;
        }

        iter = std::next(iter);
    }
}