Files
2026-09-25 06:45:32 +02:00

89 lines
2.6 KiB
C++

#include "window.h"
namespace ui
{
Window::Window(const char *title, int width, int height)
{
this->win = SDL_CreateWindow(title, width, height, 0);
this->context = new Context(this->win);
windows.push_back({SDL_GetWindowID(this->win), this});
float x, y;
SDL_GetMouseState(&x, &y);
input.mouse.x = int(x);
input.mouse.y = int(y);
}
Window::~Window()
{
for (size_t i=0; i<windows.size(); ++i) { if (windows[i].second==this) std::swap(windows[i], windows.back()); windows.pop_back(); break; }
delete this->context;
SDL_DestroyWindow(this->win);
}
bool Window::HandleEvents(SDL_Event *e)
{
switch (e->type) {
case SDL_EVENT_WINDOW_EXPOSED:
case SDL_EVENT_WINDOW_SHOWN:
this->Update();
break;
case SDL_EVENT_MOUSE_MOTION:
input.mouse.x = e->motion.x;
input.mouse.y = e->motion.y;
this->Update();
break;
case SDL_EVENT_MOUSE_BUTTON_DOWN:
input.mouse.buttons[e->button.button-1] = mbsDown;
this->Update();
input.mouse.buttons[e->button.button-1] = mbsPressed;
break;
case SDL_EVENT_MOUSE_BUTTON_UP:
input.mouse.buttons[e->button.button-1] = mbsUp;
this->Update();
input.mouse.buttons[e->button.button-1] = mbsReleased;
break;
case SDL_EVENT_MOUSE_WHEEL:
input.mouse.wheel = e->wheel.integer_y;
this->Update();
input.mouse.wheel = 0;
break;
case SDL_EVENT_KEY_DOWN:
input.keyboard.keyDown = e->key.scancode;
this->Update();
input.keyboard.keyDown = 0;
break;
case SDL_EVENT_KEY_UP:
input.keyboard.keyUp = e->key.scancode;
this->Update();
input.keyboard.keyUp = 0;
break;
default: break;
}
}
void Window::Update()
{
this->context->Clear(0xff1e1e1e);
}
/* MÈTODES ESTATICS */
/* ================ */
std::vector<std::pair<uint32_t, Window*>> Window::windows;
void Window::DistributeEvent(SDL_Event *e)
{
uint32_t id = SDL_GetWindowID(SDL_GetWindowFromEvent(e));
for (auto window : Window::windows) if (window.first == id) window.second->HandleEvents(e);
}
}