-
Notifications
You must be signed in to change notification settings - Fork 0
Tiny Window
The module that interacts directly with the Operating System, built to support native Win32 and X11 for windows and linux. It consists of two main classes defined in tiny_window.hh
the tWindowManager and the tWindow, the prefix "t" stands for "tiny".
Ideally there will only be a single tWindowManager per session. This class is used to create and destroy windows and handle event callbacks for these windows. Here are some examples on how to use the WindowManager.
#include "MEM_guardedalloc.h"
#include "tiny_window.hh"
using namespace rose::tiny_window;
int main(void) {
tWindowManager *wm = MEM_new<tWindowManager>("application-name");
while(true) {
if(wm->HasEventsWaiting()) {
wm->Poll();
}
// Do stuff here ...
}
MEM_delete<tWindowManager>(wm);
return 0;
}
This will not do much though since we have no windows. In order to create a window it is as simple as calling a function.
#include "MEM_guardedalloc.h"
#include "tiny_window.hh"
using namespace rose::tiny_window;
int main(void) {
tWindowManager *wm = MEM_new<tWindowManager>("application-name");
WindowSetting settings;
settings.name = "window-title";
settings.width = 1920;
settings.height = 1080;
tWindow *win = wm->AddWindow(settings);
// Activate the window's (OpneGL/GLEW) graphics context.
win->MakeContextCurrent();
while(!win->ShouldClose()) {
if(wm->HasEventsWaiting()) {
wm->Poll();
}
// Do stuff here ...
win->SwapBuffers();
}
MEM_delete<tWindowManager>(wm);
return 0;
}
After creating a window manager you can handle window events by overriding the respective callbacks.
These are the callback types.
using MoveEventFn = std::function<void(tWindow *window, int x, int y)>;
using ResizeEventFn = std::function<void(tWindow *window, unsigned int cwidth, unsigned int cheight)>;
using MouseEventFn = std::function<void(tWindow *window, int x, int y, double time)>;
using ButtonDownEventFn = std::function<void(tWindow *window, int key, int x, int y, double time)>;
using ButtonUpEventFn = std::function<void(tWindow *window, int key, int x, int y, double time)>;
using KeyDownEventFn = std::function<void(tWindow *window, int key, bool repeat, char utf8[4], double time)>;
using KeyUpEventFn = std::function<void(tWindow *window, int key, double time)>;
using DestroyEventFn = std::function<void(tWindow *window)>;
And you can override them using these variables.
class tWindowManager {
...
public:
MoveEventFn MoveEvent;
ResizeEventFn ResizeEvent;
MouseEventFn MouseEvent;
ButtonDownEventFn ButtonDownEvent;
ButtonUpEventFn ButtonUpEvent;
KeyDownEventFn KeyDownEvent;
KeyUpEventFn KeyUpEvent;
DestroyEventFn DestroyEvent;
private:
...
};
Here is a quick example
/** Note that resize provides the window's client width and height, since these should be used with `glViewport`. */
wm->ResizeEvent = [=](tWindow *window, unsigned int cwidth, unsigned int cheight) -> void {
printf("Window %p was resized to %ux%u\n", window, cwidth, cheight);
};
/**
* For mouse and keyboards events a handy `time` argument is passed as well but do not compare it with `wm->GetElapsedTime()`
* You should only use it to compare elapsed time between events, its resolution is also much smaller as it only covers 1ms intervals.
*/
wm->KeyDownEventFn = [=](tWindow *window, int key, bool repeat, char utf8[4], double time) {
printf("Key pressed : %s\n", utf8);
};