aboutsummaryrefslogtreecommitdiff
path: root/src/graphics/Sprite.cpp
blob: 461b14c6beff5bb8848bc189e88d8ba885937948 (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
#include "../../include/mglpp/graphics/Sprite.hpp"
#include "../../include/mglpp/graphics/Texture.hpp"

extern "C" {
#include <mgl/mgl.h>
}

namespace mgl {
    Sprite::Sprite() : Sprite(nullptr, vec2f(0.0f, 0.0f)) {}

    Sprite::Sprite(Texture *texture, vec2f position) : texture(texture) {
        mgl_sprite_init(&sprite, texture ? texture->internal_texture() : nullptr);
        set_position(position);
    }

    Sprite::~Sprite() {
        
    }

    void Sprite::set_texture(Texture *texture) {
        this->texture = texture;
        mgl_sprite_set_texture(&sprite, texture ? texture->internal_texture() : nullptr);
    }

    void Sprite::set_position(vec2f position) {
        mgl_sprite_set_position(&sprite, {position.x, position.y});
    }

    void Sprite::set_color(Color color) {
        mgl_sprite_set_color(&sprite, {color.r, color.g, color.b, color.a});
    }

    vec2f Sprite::get_position() const {
        return { sprite.position.x, sprite.position.y };
    }

    vec2f Sprite::get_size() const {
        if(texture)
            return get_scale() * mgl::vec2f(texture->get_size().x, texture->get_size().y);
        else
            return { 0.0f, 0.0f };
    }

    void Sprite::set_scale(vec2f scale) {
        sprite.scale = { scale.x, scale.y };
    }

    void Sprite::set_scale(float scale) {
        sprite.scale = { scale, scale };
    }

    void Sprite::set_size(vec2f size) {
        if(texture)
            set_scale({ size.x / (float)texture->get_size().x, size.y / (float)texture->get_size().y });
        else
            set_scale({ 0.0f, 0.0f });
    }

    void Sprite::set_rotation(float degrees) {
        mgl_sprite_set_rotation(&sprite, degrees);
    }

    void Sprite::set_origin(vec2f origin) {
        mgl_sprite_set_origin(&sprite, *(mgl_vec2f*)&origin);
    }

    vec2f Sprite::get_scale() const {
        return { sprite.scale.x, sprite.scale.y };
    }

    const Texture* Sprite::get_texture() const {
        return texture;
    }

    void Sprite::draw(Window&) {
        mgl_sprite_draw(mgl_get_context(), &sprite);
    }
}