summaryrefslogtreecommitdiffhomepage
path: root/shaders/tile.cpp
blob: 2fb37cd8fe3963eeb28d0cf6eab81829f9a78d18 (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
#include "shaders/tile.hpp"
#include "loader.hpp"
#include "compat/assert.hpp"
#include <Magnum/Math/Vector4.h>
#include <Magnum/GL/Context.h>
#include <Magnum/GL/Shader.h>
#include <Magnum/GL/Version.h>

namespace floormat {

tile_shader::tile_shader()
{
    using V = GL::Version;
    constexpr V min_version = GL::Version::GL430;
    const auto version = GL::Context::current().supportedVersion({ V::GL460, V::GL450, V::GL440, V::GL430, });

    if (version < min_version)
        fm_abort("floormat requires OpenGL version %d, only %d is supported", (int)min_version, (int)version);

    GL::Shader vert{version, GL::Shader::Type::Vertex};
    GL::Shader frag{version, GL::Shader::Type::Fragment};

    vert.addSource(loader.shader("shaders/tile.vert"));
    frag.addSource(loader.shader("shaders/tile.frag"));
    CORRADE_INTERNAL_ASSERT_OUTPUT(vert.compile());
    CORRADE_INTERNAL_ASSERT_OUTPUT(frag.compile());
    attachShaders({vert, frag});
    CORRADE_INTERNAL_ASSERT_OUTPUT(link());

    set_scale({640, 480});
    set_camera_offset({0, 0});
    set_tint({1, 1, 1, 1});
}

tile_shader::~tile_shader() = default;

tile_shader& tile_shader::set_scale(const Vector2& scale)
{
    if (scale != _scale)
        setUniform(ScaleUniform, 1.f/(_scale = scale));
    return *this;
}

tile_shader& tile_shader::set_camera_offset(Vector2d camera_offset)
{
    _camera_offset = camera_offset;
    return *this;
}

tile_shader& tile_shader::set_tint(const Vector4& tint)
{
    if (tint != _tint)
        setUniform(TintUniform, _tint = tint);
    return *this;
}

void tile_shader::_draw()
{
    if (const auto offset = Vector2{(float)_camera_offset[0], (float)_camera_offset[1]};
        offset != _real_camera_offset)
    {
        fm_assert(offset[0] < 1 << 24 && offset[1] < 1 << 24);
        _real_camera_offset = offset;
        setUniform(OffsetUniform, offset);
    }
}

} // namespace floormat