aboutsummaryrefslogtreecommitdiffstats
path: root/include/components/texture.hpp
blob: ea08c771c80d6ae5923ae4f9016f47c7eb783755 (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
#ifndef COMPONENTS_TEXTURE_HPP
#define COMPONENTS_TEXTURE_HPP

#include "components/point.hpp"
#include "window.hpp"

#include <string>

class Texture
{
public:
    Texture(std::string path) {
        tex = sdl2LoadTexture(path.c_str());
        if (tex)
            SDL_QueryTexture(tex, nullptr, nullptr, &w, &h);
    }

    ~Texture() {
        if (tex != nullptr)
            SDL_DestroyTexture(tex);
    }

    void operator()(SDL_Renderer *rend, Point p) const noexcept {
        const int x = static_cast<int>(p.x);
        const int y = static_cast<int>(p.y);
        SDL_Rect rect {x, y, w, h};

        SDL_RenderCopy(rend, tex, nullptr, &rect);
    }

    Point dim() const noexcept {
        return Point(static_cast<float>(w), static_cast<float>(h));
    }

private:
    SDL_Texture *tex;
    int w, h;
};

#endif // COMPONENTS_TEXTURE_HPP