-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.cpp
170 lines (148 loc) · 6.24 KB
/
application.cpp
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
//
// Created by piotr on 05.02.25.
//
/*------- include files:
-------------------------------------------------------------------*/
#include "application.h"
#include "window.h"
#include "sdl_event.h"
#include "toolbox/all.h"
#include <SDL3/SDL.h>
#include <SDL3_ttf/SDL_ttf.h>
#include "event/event_controller.h"
namespace Witcher {
using namespace bee;
Application::Application(
std::string app_name,
std::string app_version,
std::string app_identifier,
std::string app_creator,
std::string app_copyright
) :
name_{std::move(app_name)},
version_{std::move(app_version)},
identifier_{std::move(app_identifier)},
creator_{std::move(app_creator)},
copyright_{std::move(app_copyright)}
{
if (!SDL_SetAppMetadata(name_.c_str(), version_.c_str(), identifier_.c_str()))
box::println_error("Failed to set application metadata: {}", SDL_GetError());
if (!creator_.empty())
if (!SDL_SetAppMetadataProperty(SDL_PROP_APP_METADATA_CREATOR_STRING, creator_.c_str()))
box::println_error("Failed to set app metadata creator: {}\n", SDL_GetError());
if (!copyright_.empty())
if (SDL_SetAppMetadataProperty(SDL_PROP_APP_METADATA_COPYRIGHT_STRING, copyright_.c_str()))
box::println_error("Failed to set app metadata copyright: {}\n", SDL_GetError());
if (!SDL_Init(SDL_INIT_VIDEO|SDL_INIT_EVENTS)) {
box::println_error("Failed to initialize SDL - {}\n", SDL_GetError());
exit(1);
}
if (!TTF_Init()) {
box::println_error("Failed to initialize TTF - {}\n", SDL_GetError());
exit(2);
}
box::println("Application::Application");
}
Application::~Application() {
delete window_;
window_ = nullptr;
TTF_Quit();
SDL_Quit();
}
void Application::run() noexcept {
window_->prepare();
window_->show();
// window_->update_geometry();
main_loop();
}
/****************************************************************
* *
* m a i n _ l o o p *
* *
****************************************************************/
void Application::main_loop() noexcept {
bool quit{};
while (!quit) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
// Window Events --------------------------------
case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
// box::print("{}\n", sdl_event::to_string(event.type));
break;
case SDL_EVENT_QUIT:
// box::print("{}\n", sdl_event::to_string(event.type));
if (!can_exit())
continue;
quit = true;
break;
case SDL_EVENT_WINDOW_MINIMIZED:
// box::print("{}\n", sdl_event::to_string(event.type));
window_->set_visible(false);
break;
case SDL_EVENT_WINDOW_RESTORED:
// box::print("{}\n", sdl_event::to_string(event.type));
window_->set_visible(true);
break;
case SDL_EVENT_WINDOW_MOVED:
break;
case SDL_EVENT_WINDOW_RESIZED:
// box::print("{}\n", sdl_event::to_string(event.type));
window_->update_geometry();
break;
// Mouse Events ---------------------------------
case SDL_EVENT_MOUSE_BUTTON_DOWN: {
// box::print("{}\n", sdl_event::to_string(event.type));
MouseEvent mouse_event{event.button};
if (auto const widget = window_->contains_point(mouse_event.pos())) {
if (mouse_event.clicks() == 1) widget->mouse_down(mouse_event);
else if (mouse_event.clicks() == 2) widget->mouse_double_down(mouse_event);
}
break;
}
case SDL_EVENT_MOUSE_BUTTON_UP: {
MouseEvent mouse_event{event.button};
if (auto const widget = window_->contains_point(mouse_event.pos())) {
if (mouse_event.clicks() == 1) widget->mouse_up(mouse_event);
else if (mouse_event.clicks() == 2) widget->mouse_double_up(mouse_event);
}
break;
}
case SDL_EVENT_MOUSE_MOTION: {
SDL_MouseMotionEvent const e = event.motion;
EventController::self().send(UserEvent::MouseMove, std::pair{e.x, e.y});
break;
}
default: {}
}
}
update();
}
}
void Application::update() {
while (SDL_GetTicks() < (tick_counter_ + DEADLINE)) {}
tick_counter_ = SDL_GetTicks();
window_->draw();
}
bool Application::can_exit() const noexcept {
return window_->can_close();
}
void Application::displays() {
box::println("Available displays:");
int count = 0;
SDL_DisplayID* ptr = SDL_GetDisplays(&count);
for (int i = 0; i < count; i++) {
auto id = ptr[i];
auto const name = std::string(SDL_GetDisplayName(id)).c_str();
SDL_Rect rect;
SDL_GetDisplayBounds(id, &rect);
SDL_Rect available;
SDL_GetDisplayUsableBounds(id, &available);
box::print("\t id: {}, name: {}, bounds: ({}, {}, {}, {}), usable: ({}, {}, {}, {})\n",
id, name,
rect.x, rect.y, rect.w, rect.h,
available.x, available.y, available.w, available.h);
}
SDL_free(ptr);
}
}