Started turning Window to a class.

This commit is contained in:
Relintai 2023-12-19 00:21:23 +01:00
parent 8be54dfe27
commit 666a5d7ed8
6 changed files with 1288 additions and 1375 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,99 +0,0 @@
// -----------------------------------------------------------------------------
// window framework
// - rlyeh, public domain
//
// @todo: window_cursor(ico);
// @todo: if WINDOW_PORTRAIT && exist portrait monitor, use that instead of primary one
// @todo: WINDOW_TRAY
enum WINDOW_FLAGS {
WINDOW_MSAA2 = 0x02,
WINDOW_MSAA4 = 0x04,
WINDOW_MSAA8 = 0x08,
WINDOW_SQUARE = 0x20,
WINDOW_PORTRAIT = 0x40,
WINDOW_LANDSCAPE = 0x80,
WINDOW_ASPECT = 0x100, // keep aspect
WINDOW_FIXED = 0x200, // disable resizing
WINDOW_TRANSPARENT = 0x400,
WINDOW_BORDERLESS = 0x800,
WINDOW_VSYNC = 0,
WINDOW_VSYNC_ADAPTIVE = 0x1000,
WINDOW_VSYNC_DISABLED = 0x2000,
};
API bool window_create(float scale, unsigned flags);
API bool window_create_from_handle(void *handle, float scale, unsigned flags);
API void window_reload();
API int window_frame_begin();
API void window_frame_end();
API void window_frame_swap();
API int window_swap(); // single function that combines above functions (desktop only)
API void window_loop(void (*function)(void* loopArg), void* loopArg ); // run main loop function continuously (emscripten only)
API void window_loop_exit(); // exit from main loop function (emscripten only)
API void window_title(const char *title);
API void window_color(unsigned color);
API vec2 window_canvas();
API void* window_handle();
API char* window_stats();
API uint64_t window_frame();
API int window_width();
API int window_height();
API double window_time();
API double window_delta();
// API bool window_hook(void (*func)(), void* userdata); // deprecated
// API void window_unhook(void (*func)()); // deprecated
API void window_focus(); // window attribute api using haz catz language for now
API int window_has_focus();
API void window_fullscreen(int enabled);
API int window_has_fullscreen();
API void window_cursor(int visible);
API int window_has_cursor();
API void window_pause(int paused);
API int window_has_pause();
API void window_visible(int visible);
API int window_has_visible();
API void window_maximize(int enabled);
API int window_has_maximize();
API void window_transparent(int enabled);
API int window_has_transparent();
API void window_icon(const char *file_icon);
API int window_has_icon();
API double window_aspect();
API void window_aspect_lock(unsigned numer, unsigned denom);
API void window_aspect_unlock();
API double window_fps();
API double window_fps_target();
API void window_fps_lock(float fps);
API void window_fps_unlock();
API void window_screenshot(const char* outfile_png); // , bool record_cursor
API int window_record(const char *outfile_mp4); // , bool record_cursor
API vec2 window_dpi();
enum CURSOR_SHAPES {
CURSOR_NONE,
CURSOR_HW_ARROW, // default
CURSOR_HW_IBEAM, // i-beam text cursor
CURSOR_HW_HDRAG, // horizontal drag/resize
CURSOR_HW_VDRAG, // vertical drag/resize
CURSOR_HW_HAND, // hand, clickable
CURSOR_HW_CROSS, // crosshair
CURSOR_SW_AUTO, // software cursor, ui driven. note: this is the only icon that may be recorded or snapshotted
};
API void window_cursor_shape(unsigned shape);
API const char *window_clipboard();
API void window_setclipboard(const char *text);

1153
sfw/application/window.cpp Normal file

File diff suppressed because it is too large Load Diff

135
sfw/application/window.h Normal file
View File

@ -0,0 +1,135 @@
// -----------------------------------------------------------------------------
// window framework
// - rlyeh, public domain
//
// @todo: cursor(ico);
// @todo: if WINDOW_PORTRAIT && exist portrait monitor, use that instead of primary one
// @todo: WINDOW_TRAY
#include "int_types.h"
#include "vector2.h"
class Window {
public:
enum WINDOW_FLAGS {
WINDOW_MSAA2 = 0x02,
WINDOW_MSAA4 = 0x04,
WINDOW_MSAA8 = 0x08,
WINDOW_SQUARE = 0x20,
WINDOW_PORTRAIT = 0x40,
WINDOW_LANDSCAPE = 0x80,
WINDOW_ASPECT = 0x100, // keep aspect
WINDOW_FIXED = 0x200, // disable resizing
WINDOW_TRANSPARENT = 0x400,
WINDOW_BORDERLESS = 0x800,
WINDOW_VSYNC = 0,
WINDOW_VSYNC_ADAPTIVE = 0x1000,
WINDOW_VSYNC_DISABLED = 0x2000,
};
bool create(float scale, unsigned flags);
bool create_from_handle(void *handle, float scale, unsigned flags);
void reload();
int frame_begin();
void frame_end();
void frame_swap();
int swap(); // single function that combines above functions (desktop only)
void loop(void (*function)(void *loopArg), void *loopArg); // run main loop function continuously (emscripten only)
void loop_exit(); // exit from main loop function (emscripten only)
void title(const char *title);
void color(unsigned color);
Vector2 canvas();
void *handle();
char *stats();
uint64_t frame();
int width();
int height();
double time();
double delta();
// bool hook(void (*func)(), void* userdata); // deprecated
// void unhook(void (*func)()); // deprecated
void focus(); // window attribute using haz catz language for now
int has_focus();
void fullscreen(int enabled);
int has_fullscreen();
void cursor(int visible);
int has_cursor();
void pause(int paused);
int has_pause();
void visible(int visible);
int has_visible();
void maximize(int enabled);
int has_maximize();
void transparent(int enabled);
int has_transparent();
void icon(const char *file_icon);
int has_icon();
double aspect();
void aspect_lock(unsigned numer, unsigned denom);
void aspect_unlock();
double fps();
double fps_target();
void fps_lock(float fps);
void fps_unlock();
void screenshot(const char *outfile_png); // , bool record_cursor
int record(const char *outfile_mp4); // , bool record_cursor
Vector2 dpi();
enum CURSOR_SHAPES {
CURSOR_NONE,
CURSOR_HW_ARROW, // default
CURSOR_HW_IBEAM, // i-beam text cursor
CURSOR_HW_HDRAG, // horizontal drag/resize
CURSOR_HW_VDRAG, // vertical drag/resize
CURSOR_HW_HAND, // hand, clickable
CURSOR_HW_CROSS, // crosshair
CURSOR_SW_AUTO, // software cursor, ui driven. note: this is the only icon that may be recorded or snapshotted
};
void cursor_shape(unsigned shape);
const char *clipboard();
void setclipboard(const char *text);
static Window *get_singleton();
Window();
~Window();
protected:
static Window *_singleton;
struct GLFWwindow;
GLFWwindow *window;
int w;
int h;
int xpos;
int ypos;
int paused;
int fullscreen;
int xprev;
int yprev;
int wprev;
int hprev;
uint64_t frame_count;
double t;
double dt;
double fps;
double hz;
char title[128];
int locked_aspect_ratio;
Vector4 winbgcolor;
};

View File

@ -1,170 +0,0 @@
// ----------------------------------------------------------------------------
// time
#if 0
uint64_t time_gpu() {
GLint64 t = 123456789;
glGetInteger64v(GL_TIMESTAMP, &t);
return (uint64_t)t;
}
#endif
uint64_t date() {
time_t epoch = time(0);
struct tm *ti = localtime(&epoch);
return atoi64(va("%04d%02d%02d%02d%02d%02d",ti->tm_year+1900,ti->tm_mon+1,ti->tm_mday,ti->tm_hour,ti->tm_min,ti->tm_sec));
}
char *date_string() {
time_t epoch = time(0);
struct tm *ti = localtime(&epoch);
return va("%04d-%02d-%02d %02d:%02d:%02d",ti->tm_year+1900,ti->tm_mon+1,ti->tm_mday,ti->tm_hour,ti->tm_min,ti->tm_sec);
}
uint64_t date_epoch() {
time_t epoch = time(0);
return epoch;
}
#if 0
double time_ss() {
return glfwGetTime();
}
double time_ms() {
return glfwGetTime() * 1000.0;
}
uint64_t time_us() {
return (uint64_t)(glfwGetTime() * 1000000.0); // @fixme: use a high resolution timer instead, or time_gpu below
}
uint64_t sleep_us(uint64_t us) { // @fixme: use a high resolution sleeper instead
return sleep_ms( us / 1000.0 );
}
double sleep_ms(double ms) {
double now = time_ms();
if( ms <= 0 ) {
#if is(win32)
Sleep(0); // yield
#else
usleep(0);
#endif
} else {
#if is(win32)
Sleep(ms);
#else
usleep(ms * 1000);
#endif
}
return time_ms() - now;
}
double sleep_ss(double ss) {
return sleep_ms( ss * 1000 ) / 1000.0;
}
#endif
// high-perf functions
#define TIMER_E3 1000ULL
#define TIMER_E6 1000000ULL
#define TIMER_E9 1000000000ULL
#ifdef CLOCK_MONOTONIC_RAW
#define TIME_MONOTONIC CLOCK_MONOTONIC_RAW
#elif defined CLOCK_MONOTONIC
#define TIME_MONOTONIC CLOCK_MONOTONIC
#else
// #define TIME_MONOTONIC CLOCK_REALTIME // untested
#endif
static uint64_t nanotimer(uint64_t *out_freq) {
if( out_freq ) {
#if is(win32)
LARGE_INTEGER li;
QueryPerformanceFrequency(&li);
*out_freq = li.QuadPart;
//#elif is(ANDROID)
// *out_freq = CLOCKS_PER_SEC;
#elif defined TIME_MONOTONIC
*out_freq = TIMER_E9;
#else
*out_freq = TIMER_E6;
#endif
}
#if is(win32)
LARGE_INTEGER li;
QueryPerformanceCounter(&li);
return (uint64_t)li.QuadPart;
//#elif is(ANDROID)
// return (uint64_t)clock();
#elif defined TIME_MONOTONIC
struct timespec ts;
clock_gettime(TIME_MONOTONIC, &ts);
return (TIMER_E9 * (uint64_t)ts.tv_sec) + ts.tv_nsec;
#else
struct timeval tv;
gettimeofday(&tv, NULL);
return (TIMER_E6 * (uint64_t)tv.tv_sec) + tv.tv_usec;
#endif
}
uint64_t time_ns() {
static __thread uint64_t epoch = 0;
static __thread uint64_t freq = 0;
if( !freq ) {
epoch = nanotimer(&freq);
}
uint64_t a = nanotimer(NULL) - epoch;
uint64_t b = TIMER_E9;
uint64_t c = freq;
// Computes (a*b)/c without overflow, as long as both (a*b) and the overall result fit into 64-bits.
// [ref] https://github.com/rust-lang/rust/blob/3809bbf47c8557bd149b3e52ceb47434ca8378d5/src/libstd/sys_common/mod.rs#L124
uint64_t q = a / c;
uint64_t r = a % c;
return q * b + r * b / c;
}
uint64_t time_us() {
return time_ns() / TIMER_E3;
}
uint64_t time_ms() {
return time_ns() / TIMER_E6;
}
double time_ss() {
return time_ns() / 1e9; // TIMER_E9;
}
double time_mm() {
return time_ss() / 60;
}
double time_hh() {
return time_mm() / 60;
}
void sleep_ns( double ns ) {
#if is(win32)
if( ns >= 100 ) {
LARGE_INTEGER li; // Windows sleep in 100ns units
HANDLE timer = CreateWaitableTimer(NULL, TRUE, NULL);
li.QuadPart = (LONGLONG)(__int64)(-ns/100); // Negative for relative time
SetWaitableTimer(timer, &li, 0, NULL, NULL, FALSE);
WaitForSingleObject(timer, INFINITE);
CloseHandle(timer);
#else
if( ns > 0 ) {
struct timespec wait = {0};
wait.tv_sec = ns / 1e9;
wait.tv_nsec = ns - wait.tv_sec * 1e9;
nanosleep(&wait, NULL);
#endif
} else {
#if is(win32)
Sleep(0); // yield, Sleep(0), SwitchToThread
#else
usleep(0);
#endif
}
}
void sleep_us( double us ) {
sleep_ns(us * 1e3);
}
void sleep_ms( double ms ) {
sleep_ns(ms * 1e6);
}
void sleep_ss( double ss ) {
sleep_ns(ss * 1e9);
}

View File

@ -1,21 +0,0 @@
// -----------------------------------------------------------------------------
// time framework utils
// - rlyeh, public domain.
API uint64_t date(); // YYYYMMDDhhmmss
API uint64_t date_epoch(); // linux epoch
API char* date_string(); // "YYYY-MM-DD hh:mm:ss"
API double time_hh();
API double time_mm();
API double time_ss();
API uint64_t time_ms();
API uint64_t time_us();
API uint64_t time_ns();
API void sleep_ss(double ss);
API void sleep_ms(double ms);
API void sleep_us(double us);
API void sleep_ns(double us);
API unsigned timer(unsigned ms, unsigned (*callback)(unsigned ms, void *arg), void *arg);
API void timer_destroy(unsigned timer_handle);