summaryrefslogtreecommitdiffhomepage
path: root/main/app.cpp
blob: 8483197817aba506c04f3c24205977bc505b7b17 (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
#include <cstddef>
#include "app.hpp"
#include "compat/fpu.hpp"
#include <Magnum/GL/DefaultFramebuffer.h>
#include <Magnum/ImGuiIntegration/Context.h>
#include <Magnum/ImGuiIntegration/Context.hpp>
#include <SDL_events.h>
#include <SDL_video.h>

namespace floormat {

app::app(const Arguments& arguments):
      Platform::Application{
          arguments,
          Configuration{}
              .setTitle("Test")
              .setSize({1024, 768}, dpi_policy::Physical)
              .setWindowFlags(Configuration::WindowFlag::Resizable),
          GLConfiguration{}
              .setSampleCount(4)
              .setFlags(GLConfiguration::Flag::GpuValidation)
      }
{
    if (!setSwapInterval(-1))
        (void)setSwapInterval(1);
    set_fp_mask();
    reset_camera_offset();
    update_window_scale(windowSize());
    setMinimalLoopPeriod(5);
    _imgui = ImGuiIntegration::Context(Vector2{windowSize()}, windowSize(), framebufferSize());
    SDL_MaximizeWindow(window());
    {
        auto c = _world[chunk_coords{0, 0}];
        make_test_chunk(*c);
    }
    timeline.start();
}

void app::viewportEvent(Platform::Sdl2Application::ViewportEvent& event)
{
    update_window_scale(event.windowSize());
    GL::defaultFramebuffer.setViewport({{}, event.windowSize()});
    _imgui.relayout(Vector2{event.windowSize()}, event.windowSize(), event.framebufferSize());
}


void app::mousePressEvent(Platform::Sdl2Application::MouseEvent& event)
{
    if (_imgui.handleMousePressEvent(event))
        return event.setAccepted();
    {
        if (_cursor_tile)
        {
            const auto& tile = *_cursor_tile;
            int button;
            switch (event.button())
            {
            case MouseEvent::Button::Left:   button = 0; break;
            case MouseEvent::Button::Right:  button = 1; break;
            case MouseEvent::Button::Middle: button = 2; break;
            case MouseEvent::Button::X1:     button = 5; break;
            case MouseEvent::Button::X2:     button = 6; break;
            default: button = -1; break;
            }
            do_mouse_click(tile, button);
        }
    }
}

void app::mouseReleaseEvent(Platform::Sdl2Application::MouseEvent& event)
{
    if (_imgui.handleMouseReleaseEvent(event))
        return event.setAccepted();
#if 0
    using Button = Platform::Sdl2Application::MouseEvent::Button;
    if (event.button() == Button::Left)
    {
    }
#endif
}

void app::mouseMoveEvent(Platform::Sdl2Application::MouseMoveEvent& event)
{
    _cursor_in_imgui = _imgui.handleMouseMoveEvent(event);
    _cursor_pixel = event.position();
    recalc_cursor_tile();
}

void app::mouseScrollEvent(Platform::Sdl2Application::MouseScrollEvent& event)
{
    if (_imgui.handleMouseScrollEvent(event))
        return event.setAccepted();
}

void app::textInputEvent(Platform::Sdl2Application::TextInputEvent& event)
{
    if (_imgui.handleTextInputEvent(event))
        return keys = {}, event.setAccepted();
}

void app::keyPressEvent(Platform::Sdl2Application::KeyEvent& event)
{
    if (_imgui.handleKeyPressEvent(event))
        return event.setAccepted();
    do_key(event.key(), event.modifiers(), true, event.isRepeated());
}

void app::keyReleaseEvent(Platform::Sdl2Application::KeyEvent& event)
{
    if (_imgui.handleKeyReleaseEvent(event))
        return keys = {}, event.setAccepted();
    do_key(event.key(), event.modifiers(), false, false);
}

void app::anyEvent(SDL_Event& event)
{
    if (event.type == SDL_WINDOWEVENT)
        switch (event.window.event)
        {
        case SDL_WINDOWEVENT_FOCUS_LOST:
            return event_focus_out();
        case SDL_WINDOWEVENT_FOCUS_GAINED:
            return event_focus_in();
        case SDL_WINDOWEVENT_LEAVE:
            return event_mouse_leave();
        case SDL_WINDOWEVENT_ENTER:
            return event_mouse_enter();
        default:
            std::fputs("", stdout); break; // put breakpoint here
        }
}

void app::event_focus_out()
{
    _cursor_pixel = std::nullopt;
    _cursor_tile = std::nullopt;
}

void app::event_focus_in()
{
}

void app::event_mouse_leave()
{
    _cursor_pixel = std::nullopt;
    _cursor_tile = std::nullopt;
}

void app::event_mouse_enter()
{
}

} // namespace floormat

MAGNUM_APPLICATION_MAIN(floormat::app)

#ifdef _MSC_VER
#include <cstdlib> // for __arg{c,v}
#ifdef __clang__
#    pragma clang diagnostic push
#    pragma clang diagnostic ignored "-Wmain"
#endif
extern "C" int __stdcall WinMain(void*, void*, void*, int);

extern "C" int __stdcall WinMain(void*, void*, void*, int)
{
    return main(__argc, __argv);
}
#ifdef __clang__
#    pragma clang diagnostic pop
#endif
#endif