aboutsummaryrefslogtreecommitdiff
path: root/tests/main.cpp
blob: f3ad539bd7844ea0d1c2fda9dc349d1fc9aa08d9 (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
#include <stdio.h>
#include <mglpp/mglpp.hpp>
#include <mglpp/window/Window.hpp>
#include <mglpp/window/Event.hpp>
#include <mglpp/graphics/Texture.hpp>
#include <mglpp/graphics/Sprite.hpp>
#include <mglpp/graphics/Font.hpp>
#include <mglpp/graphics/Text.hpp>
#include <mglpp/graphics/Rectangle.hpp>
#include <mglpp/graphics/VertexBuffer.hpp>
#include <mglpp/graphics/Vertex.hpp>
#include <mglpp/graphics/Shader.hpp>
#include <mglpp/system/Clock.hpp>
#include <mglpp/system/MemoryMappedFile.hpp>

struct Delegate {
    Delegate() {}

    void draw() {
        mgl::Rectangle rect(window->get_mouse_position().to_vec2f(), { 100.0f, 500.0f });
        rect.set_color({255, 0, 0, 255});
        window->draw(rect);

        shader_program->set_uniform("resolution", mgl::vec2f(texture->get_size().x, texture->get_size().y));

        mgl::Sprite sprite(texture, { 100.0f - 10.0f, 0.0f });
        sprite.set_color({255, 255, 255, 128});
        window->draw(sprite, shader_program);

        mgl::Text text("hello world!\nelapsed time: " + std::to_string(clock.get_elapsed_time_seconds()), { 0.0f, 0.0f }, *font);
        window->draw(text);

        vertex_buffer->set_position({ (float)window->get_mouse_position().x + 100, (float)window->get_mouse_position().y });
        window->draw(*vertex_buffer);
    }

    mgl::Window *window;
    mgl::Texture *texture;
    mgl::Font *font;
    mgl::VertexBuffer *vertex_buffer;
    mgl::Shader *shader_program;
    mgl::Clock clock;
};

int main(int argc, char **argv) {
    mgl::Init init;

    mgl::Window::CreateParams window_create_params;
    window_create_params.size = { 1280, 720 };
    mgl::Window window;
    if(!window.create("mglpp", window_create_params))
        return 1;

    mgl::Texture texture;
    if(!texture.load_from_file("depends/mgl/tests/X11.jpg"))
        return 1;

    mgl::MemoryMappedFile font_file;
    if(!font_file.load("/usr/share/fonts/noto/NotoSans-Regular.ttf", mgl::MemoryMappedFile::LoadOptions{true, false}))
        return 1;

    mgl::Font font;
    if(!font.load_from_file(font_file, 32))
        return 1;

    mgl::Shader shader;
    if(!shader.load_from_file("depends/mgl/tests/circle_mask.glsl", mgl::Shader::Fragment))
        return 1;

    mgl::Texture font_texture = font.get_texture();
    mgl::Vertex vertices[4] = {
        {
            {0.0f, 0.0f},
            {0.0f, 0.0f},
            {255, 0, 0, 100}
        },
        {
            {font_texture.get_size().x, 0.0f},
            {1.0f, 0.0f},
            {0, 255, 0, 100}
        },
        {
            {font_texture.get_size().x, font_texture.get_size().y},
            {1.0f, 1.0f},
            {0, 0, 255, 100}
        },
        {
            {0.0f, font_texture.get_size().y},
            {0.0f, 1.0f},
            {255, 0, 255, 100},
        }
    };

    mgl::VertexBuffer vertex_buffer;
    if(!vertex_buffer.update(vertices, 4, mgl::PrimitiveType::Quads, mgl::VertexBuffer::Static))
        return 1;

    vertex_buffer.set_texture(&font_texture);

    Delegate delegate;
    delegate.window = &window;
    delegate.texture = &texture;
    delegate.font = &font;
    delegate.vertex_buffer = &vertex_buffer;
    delegate.shader_program = &shader;

    mgl::Event event;
    while(window.is_open()) {
        if(window.poll_event(event)) {

        }

        window.clear(mgl::Color(0, 0, 0, 255));
        delegate.draw();
        window.display();
    }

    return 0;
}