harfang3d/harfang/platform/sdl/window_system.cpp

195 lines
4.7 KiB
C++
Raw Normal View History

2021-10-13 12:40:31 +00:00
// HARFANG(R) Copyright (C) 2021 Emmanuel Julien, NWNC HARFANG. Released under GPL/LGPL/Commercial Licence, see licence.txt for details.
#include "platform/window_system.h"
2022-09-26 10:07:03 +00:00
#include "foundation/format.h"
2021-10-13 12:40:31 +00:00
#include "foundation/log.h"
#include <SDL.h>
#include <iostream>
namespace hg {
//-- Monitor
2022-09-26 10:07:03 +00:00
struct Monitor {
2021-10-13 12:40:31 +00:00
std::string id;
iRect rect;
bool primary;
};
2022-09-26 10:07:03 +00:00
std::vector<Monitor *> GetMonitors() {
std::vector<Monitor *> monitors;
2021-10-13 12:40:31 +00:00
return monitors;
}
2022-09-26 10:07:03 +00:00
iRect GetMonitorRect(const Monitor *m) { return m->rect; }
2021-10-13 12:40:31 +00:00
2022-09-26 10:07:03 +00:00
bool IsPrimaryMonitor(const Monitor *m) { return m->primary; }
2021-10-13 12:40:31 +00:00
// TODO Return true if the monitor is connected.
2022-09-26 10:07:03 +00:00
bool IsMonitorConnected(const Monitor *monitor) { return true; }
2021-10-13 12:40:31 +00:00
// TODO Return monitor name.
2022-09-26 10:07:03 +00:00
std::string GetMonitorName(const Monitor *monitor) { return "TODO IN SDL"; }
2021-10-13 12:40:31 +00:00
// TODO Return monitor size in millimeters.
2022-09-26 10:07:03 +00:00
hg::iVec2 GetMonitorSizeMM(const Monitor *monitor) { return hg::iVec2(0, 0); }
2021-10-13 12:40:31 +00:00
// TODO Get the list of screen modes for a given monitor.
2022-09-26 10:07:03 +00:00
bool GetMonitorModes(const Monitor *monitor, std::vector<MonitorMode> &modes) { return true; }
2021-10-13 12:40:31 +00:00
//-- Window
//
2022-09-26 10:07:03 +00:00
struct Window {
2021-10-13 12:40:31 +00:00
SDL_Window *w{0};
bool is_foreign{false};
};
2022-09-26 10:07:03 +00:00
static Window *window_in_focus;
Signal<void(const Window *)> new_window_signal;
Signal<void(const Window *, bool)> window_focus_signal;
Signal<bool(const Window *)> close_window_signal;
Signal<void(const Window *)> destroy_window_signal;
2021-10-13 12:40:31 +00:00
2022-09-26 10:07:03 +00:00
void WindowSystemInit() {
SDL_Init(SDL_INIT_VIDEO);
2021-10-13 12:40:31 +00:00
2022-09-26 10:07:03 +00:00
// DISABLE ALL TEXT KEYBOARD
SDL_EventState(SDL_TEXTINPUT, SDL_DISABLE);
SDL_EventState(SDL_KEYDOWN, SDL_DISABLE);
SDL_EventState(SDL_KEYUP, SDL_DISABLE);
}
Window *NewWindow(int width, int height, int bpp, WindowVisibility visibility) {
Window *w = new Window();
2021-10-13 12:40:31 +00:00
2022-09-26 10:07:03 +00:00
int window_flag = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_SHOWN;
2021-10-13 12:40:31 +00:00
switch (visibility) {
2022-09-26 10:07:03 +00:00
case WV_Fullscreen:
2021-10-13 12:40:31 +00:00
window_flag |= SDL_WINDOW_FULLSCREEN;
break;
2022-09-26 10:07:03 +00:00
case WV_Undecorated:
2021-10-13 12:40:31 +00:00
window_flag |= SDL_WINDOW_BORDERLESS;
break;
default:
2022-09-26 10:07:03 +00:00
case WV_Windowed:
2021-10-13 12:40:31 +00:00
break;
}
2022-09-26 10:07:03 +00:00
w->w = SDL_CreateWindow(nullptr, 0, 0, width, height, window_flag);
2021-10-13 12:40:31 +00:00
UpdateWindow(w);
2022-09-26 10:07:03 +00:00
debug(format("NewWindow: %1").arg((void *)w->w));
2021-10-13 12:40:31 +00:00
return w;
}
2022-09-26 10:07:03 +00:00
Window *NewWindow(const char *title, int width, int height, int bpp, WindowVisibility visibility) {
auto win = NewWindow(width, height, bpp, visibility);
SetWindowTitle(win, title);
return win;
}
Window *NewWindowFrom(void *handle) {
Window *w = new Window();
2021-10-13 12:40:31 +00:00
2022-09-26 10:07:03 +00:00
w->is_foreign = true;
w->w = reinterpret_cast<SDL_Window *>(handle);
2021-10-13 12:40:31 +00:00
2022-09-26 10:07:03 +00:00
SDL_CreateWindowFrom(w->w);
2021-10-13 12:40:31 +00:00
return w;
}
// TODO Create a new fullscreen window on a specified monitor.
2022-10-21 11:14:26 +00:00
Window *NewFullscreenWindow(const Monitor *monitor, int mode_index, MonitorRotation rotation) {
return NewWindow(512, 512, 32, WV_Windowed);
}
Window * NewFullscreenWindow(const char *title, const Monitor *monitor, int mode_index, MonitorRotation rotation) {
return NewWindow(512, 512, 32, WV_Windowed);
}
2021-10-13 12:40:31 +00:00
2022-02-22 10:28:28 +00:00
void *GetDisplay() { return nullptr; }
2021-10-13 12:40:31 +00:00
static const char *canvas_name = "#canvas";
2022-09-26 10:07:03 +00:00
void *GetWindowHandle(const Window *w) {
return (void *)canvas_name;
// return reinterpret_cast<void *>(w->w);
}
2021-10-13 12:40:31 +00:00
2022-09-26 10:07:03 +00:00
Window *GetWindowInFocus() { return window_in_focus; }
2021-10-13 12:40:31 +00:00
2022-09-26 10:07:03 +00:00
bool DestroyWindow(Window *w) {
debug(format("DestroyWindow: %1").arg((void *)w->w));
window_focus_signal.Emit(w, false);
SDL_DestroyWindow(w->w);
w->w = 0;
delete w;
2021-10-13 12:40:31 +00:00
return true;
}
//
2022-09-26 10:07:03 +00:00
bool UpdateWindow(const Window *w) {
2021-10-13 12:40:31 +00:00
2022-09-26 10:07:03 +00:00
if (SDL_GetWindowFlags(w->w) & SDL_WINDOW_INPUT_FOCUS) {
2021-10-13 12:40:31 +00:00
// in focus
2022-09-26 10:07:03 +00:00
if (!(window_in_focus == w)) {
2021-10-13 12:40:31 +00:00
2022-09-26 10:07:03 +00:00
window_in_focus = (Window *)w;
window_focus_signal.Emit(w, true);
2021-10-13 12:40:31 +00:00
}
2022-09-26 10:07:03 +00:00
} else if (window_in_focus == w) {
2021-10-13 12:40:31 +00:00
// not in focus
2022-09-26 10:07:03 +00:00
window_in_focus = new Window(); // blank window
window_focus_signal.Emit(w, false);
2021-10-13 12:40:31 +00:00
}
return true;
}
//
2022-09-26 10:07:03 +00:00
bool GetWindowClientSize(const Window *w, int &width, int &height) {
if (!w)
return false;
SDL_GetWindowSize(w->w, &width, &height);
2021-10-13 12:40:31 +00:00
return true;
}
2022-09-26 10:07:03 +00:00
bool SetWindowClientSize(Window *w, int width, int height) {
if (!w)
return false;
SDL_SetWindowSize(w->w, width, height);
2021-10-13 12:40:31 +00:00
return true;
}
2022-09-26 10:07:03 +00:00
bool GetWindowTitle(const Window *w, std::string &title) { return true; }
2021-10-13 12:40:31 +00:00
2022-09-26 10:07:03 +00:00
bool SetWindowTitle(Window *w, const std::string &title) { return true; }
2021-10-13 12:40:31 +00:00
2022-09-26 10:07:03 +00:00
bool WindowHasFocus(const Window *w) { return true; }
2021-10-13 12:40:31 +00:00
2022-10-21 11:14:26 +00:00
bool SetWindowPos(Window *w, const hg::iVec2 &v) {
2022-09-26 10:07:03 +00:00
SDL_SetWindowPosition(w->w, v.x, v.y);
2021-10-13 12:40:31 +00:00
UpdateWindow(w); // process messages on the spot
return true;
}
2022-09-26 10:07:03 +00:00
hg::iVec2 GetWindowPos(const Window *w) {
2021-10-13 12:40:31 +00:00
UpdateWindow(w);
2022-09-26 10:07:03 +00:00
hg::iVec2 pos;
SDL_GetWindowPosition(w->w, &pos.x, &pos.y);
2021-10-13 12:40:31 +00:00
return pos;
}
2022-10-21 11:14:26 +00:00
hg::Vec2 GetWindowContentScale(const Window *window) {
puts(__FILE__ "GetWindowContentScale");
return { 1, 1 };
}
2021-10-13 12:40:31 +00:00
void ShowCursor() { SDL_ShowCursor(SDL_ENABLE); }
void HideCursor() { SDL_ShowCursor(SDL_DISABLE); }
2022-10-21 11:14:26 +00:00
void WindowSystemShutdown() { }
2021-10-13 12:40:31 +00:00
} // namespace hg