aboutsummaryrefslogtreecommitdiff
path: root/src/window/Window.cpp
blob: 512991a398c2024d1a842cf7459170e4ed16f3c2 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "../../include/mglpp/window/Window.hpp"
#include "../../include/mglpp/window/Event.hpp"
#include "../../include/mglpp/graphics/Drawable.hpp"
#include "../../include/mglpp/graphics/Shader.hpp"

extern "C" {
#include <mgl/graphics/vertex.h>
#include <mgl/window/event.h>
#include <mgl/mgl.h>
}

namespace mgl {
    static bool clipboard_callback_interface(const unsigned char *data, size_t size, mgl_clipboard_type clipboard_type, void *userdata) {
        ClipboardCallback *clipboard_callback = (ClipboardCallback*)userdata;
        return (*clipboard_callback)(data, size, clipboard_type);
    }

    Window::Window() {
        window.window = 0;
    }

    Window::~Window() {
        if(window.window)
            mgl_window_deinit(&window);
    }

    bool Window::create(const char *title, CreateParams create_params) {
        if(window.window)
            return false;
        return mgl_window_create(&window, title, (mgl_window_create_params*)&create_params) == 0;
    }

    bool Window::create(WindowHandle existing_window) {
        if(window.window)
            return false;
        return mgl_window_init_from_existing_window(&window, existing_window) == 0;
    }

    bool Window::poll_event(Event &event) {
        return mgl_window_poll_event(&window, (mgl_event*)&event);
    }

    void Window::clear(Color color) {
        mgl_window_clear(&window, mgl_color{color.r, color.g, color.b, color.a});
    }

    void Window::draw(Drawable &drawable, Shader *shader) {
        // TODO: Make the opengl context active for this thread and window, if it already isn't
        if(shader)
            Shader::use(shader);
        drawable.draw(*this);
        if(shader)
            Shader::use(nullptr);
    }
    
    void Window::draw(const Vertex *vertices, size_t vertex_count, PrimitiveType primitive_type, Shader *shader) {
        // TODO: Make the opengl context active for this thread and window, if it already isn't
        if(shader)
            Shader::use(shader);

        mgl_context *context = mgl_get_context();
        mgl_vertices_draw(context, (const mgl_vertex*)vertices, vertex_count, (mgl_primitive_type)primitive_type, { 0.0f, 0.0f });

        if(shader)
            Shader::use(nullptr);
    }

    void Window::display() {
        mgl_window_display(&window);
    }

    void Window::set_visible(bool visible) {
        mgl_window_set_visible(&window, visible);
    }

    bool Window::is_open() const {
        return mgl_window_is_open(&window);
    }

    bool Window::has_focus() const {
        return mgl_window_has_focus(&window);
    }

    void Window::close() {
        mgl_window_close(&window);
    }

    void Window::set_title(const char *title) {
        mgl_window_set_title(&window, title);
    }

    void Window::set_framerate_limit(unsigned int fps) {
        mgl_window_set_framerate_limit(&window, fps);
    }

    void Window::set_vsync_enabled(bool enabled) {
        mgl_window_set_vsync_enabled(&window, enabled);
    }

    bool Window::is_vsync_enabled() const {
        return mgl_window_is_vsync_enabled(&window);
    }

    void Window::set_fullscreen(bool fullscreen) {
        mgl_window_set_fullscreen(&window, fullscreen);
    }

    bool Window::is_fullscreen() const {
        return mgl_window_is_fullscreen(&window);
    }

    void Window::set_key_repeat_enabled(bool enabled) {
        mgl_window_set_key_repeat_enabled(&window, enabled);
    }

    void Window::set_cursor_visible(bool visible) {
        mgl_window_set_cursor_visible(&window, visible);
    }

    vec2i Window::get_size() const {
        return { window.size.x, window.size.y };
    }

    void Window::set_size(mgl::vec2i size) {
        mgl_window_set_size(&window, { size.x, size.y });
    }

    void Window::set_position(mgl::vec2i position) {
        mgl_window_set_position(&window, { position.x, position.y });
    }

    void Window::set_size_limits(mgl::vec2i minimum, mgl::vec2i maximum) {
        mgl_window_set_size_limits(&window, { minimum.x, minimum.y }, { maximum.x, maximum.y });
    }

    vec2i Window::get_mouse_position() const {
        return { window.cursor_position.x, window.cursor_position.y };
    }

    void Window::set_view(const View &new_view) {
        mgl_window_set_view(&window, (mgl_view*)&new_view);
    }

    View Window::get_view() {
        View view;
        mgl_window_get_view(&window, (mgl_view*)&view);
        return view;
    }

    bool Window::is_key_pressed(Keyboard::Key key) const {
        return mgl_window_is_key_pressed(&window, (mgl_key)key);
    }

    bool Window::is_mouse_button_pressed(Mouse::Button button) const {
        return mgl_window_is_mouse_button_pressed(&window, (mgl_mouse_button)button);
    }

    void Window::set_clipboard(const std::string &str) {
        mgl_window_set_clipboard(&window, str.c_str(), str.size());
    }

    bool Window::get_clipboard(ClipboardCallback callback) {
        return mgl_window_get_clipboard(&window, clipboard_callback_interface, &callback, MGL_CLIPBOARD_TYPE_ALL);
    }

    std::string Window::get_clipboard_string() {
        std::string result;
        char *str = nullptr;
        size_t size = 0;
        if(mgl_window_get_clipboard_string(&window, &str, &size)) {
            result.assign(str, size);
            free(str);
        }
        return result;
    }

    WindowHandle Window::get_system_handle() const {
        return window.window;
    }
}