Working n Windows

This commit is contained in:
Carlos Aragones 2020-09-21 12:27:22 +02:00
parent cfb114007e
commit f66e92c8da
3 changed files with 276 additions and 134 deletions

View File

@ -139,7 +139,7 @@ if(NOT MSVC)
add_compile_options("$<$<CONFIG:Debug>:-g>")
add_compile_options("$<IF:$<CONFIG:Debug>,-O0,-O2>")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -pedantic -Wno-switch -Wno-unused-function -Wno-implicit-fallthrough")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -pedantic -Wno-switch -Wno-unused-function -Wno-implicit-fallthrough -Wno-cast-function-type")
set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS}")
set(CMAKE_OBJC_FLAGS "${CMAKE_C_FLAGS}")
set(CMAKE_OBJCXX_FLAGS "${CMAKE_CXX_FLAGS}")

View File

@ -38,6 +38,10 @@ void * mfb_get_user_data(struct mfb_window *window);
// Set viewport (useful when resize)
bool mfb_set_viewport(struct mfb_window *window, unsigned offset_x, unsigned offset_y, unsigned width, unsigned height);
// DPI
void mfb_get_monitor_dpi(struct mfb_window *window, float *dpi_x, float *dpi_y);
// Callbacks
void mfb_set_active_callback(struct mfb_window *window, mfb_active_func callback);
void mfb_set_resize_callback(struct mfb_window *window, mfb_resize_func callback);
void mfb_set_keyboard_callback(struct mfb_window *window, mfb_keyboard_func callback);
@ -46,6 +50,7 @@ void mfb_set_mouse_button_callback(struct mfb_window *window, mfb
void mfb_set_mouse_move_callback(struct mfb_window *window, mfb_mouse_move_func callback);
void mfb_set_mouse_scroll_callback(struct mfb_window *window, mfb_mouse_scroll_func callback);
// Getters
const char * mfb_get_key_name(mfb_key key);
bool mfb_is_window_active(struct mfb_window *window);
@ -58,9 +63,11 @@ float mfb_get_mouse_scroll_y(struct mfb_window *window); // M
const uint8_t * mfb_get_mouse_button_buffer(struct mfb_window *window); // One byte for every button. Press (1), Release 0. (up to 8 buttons)
const uint8_t * mfb_get_key_buffer(struct mfb_window *window); // One byte for every key. Press (1), Release 0.
// FPS
void mfb_set_target_fps(uint32_t fps);
bool mfb_wait_sync(struct mfb_window *window);
// Timer
struct mfb_timer * mfb_timer_create(void);
void mfb_timer_destroy(struct mfb_timer *tmr);
void mfb_timer_reset(struct mfb_timer *tmr);

View File

@ -12,6 +12,123 @@
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copied (and modified) from Windows Kit 10 to avoid setting _WIN32_WINNT to a higher version
typedef enum mfb_PROCESS_DPI_AWARENESS {
mfb_PROCESS_DPI_UNAWARE = 0,
mfb_PROCESS_SYSTEM_DPI_AWARE = 1,
mfb_PROCESS_PER_MONITOR_DPI_AWARE = 2
} mfb_PROCESS_DPI_AWARENESS;
typedef enum mfb_MONITOR_DPI_TYPE {
mfb_MDT_EFFECTIVE_DPI = 0,
mfb_MDT_ANGULAR_DPI = 1,
mfb_MDT_RAW_DPI = 2,
mfb_MDT_DEFAULT = mfb_MDT_EFFECTIVE_DPI
} mfb_MONITOR_DPI_TYPE;
#define mfb_DPI_AWARENESS_CONTEXT_UNAWARE ((HANDLE) -1)
#define mfb_DPI_AWARENESS_CONTEXT_SYSTEM_AWARE ((HANDLE) -2)
#define mfb_DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE ((HANDLE) -3)
#define mfb_DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 ((HANDLE) -4)
#define mfb_DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED ((HANDLE) -5)
// user32.dll
typedef BOOL(WINAPI *PFN_SetProcessDPIAware)(void);
typedef BOOL(WINAPI *PFN_SetProcessDpiAwarenessContext)(HANDLE);
HMODULE mfb_user32_dll = 0x0;
PFN_SetProcessDPIAware mfb_SetProcessDPIAware = 0x0;
PFN_SetProcessDpiAwarenessContext mfb_SetProcessDpiAwarenessContext = 0x0;
// shcore.dll
typedef HRESULT(WINAPI *PFN_SetProcessDpiAwareness)(mfb_PROCESS_DPI_AWARENESS);
typedef HRESULT(WINAPI *PFN_GetDpiForMonitor)(HMONITOR, mfb_MONITOR_DPI_TYPE, UINT *, UINT *);
HMODULE mfb_shcore_dll = 0x0;
PFN_SetProcessDpiAwareness mfb_SetProcessDpiAwareness = 0x0;
PFN_GetDpiForMonitor mfb_GetDpiForMonitor = 0x0;
//--
void
load_functions() {
if(mfb_user32_dll == 0x0) {
mfb_user32_dll = LoadLibraryA("user32.dll");
if (mfb_user32_dll != 0x0) {
mfb_SetProcessDPIAware = (PFN_SetProcessDPIAware) GetProcAddress(mfb_user32_dll, "SetProcessDPIAware");
mfb_SetProcessDpiAwarenessContext = (PFN_SetProcessDpiAwarenessContext) GetProcAddress(mfb_user32_dll, "SetProcessDpiAwarenessContext");
}
}
if(mfb_shcore_dll == 0x0) {
mfb_shcore_dll = LoadLibraryA("shcore.dll");
if (mfb_shcore_dll != 0x0) {
mfb_SetProcessDpiAwareness = (PFN_SetProcessDpiAwareness) GetProcAddress(mfb_shcore_dll, "SetProcessDpiAwareness");
mfb_GetDpiForMonitor = (PFN_GetDpiForMonitor) GetProcAddress(mfb_shcore_dll, "GetDpiForMonitor");
}
}
}
//--
void
dpi_aware() {
if (mfb_SetProcessDpiAwarenessContext != 0x0) {
mfb_SetProcessDpiAwarenessContext(mfb_DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
}
else if (mfb_SetProcessDpiAwareness != 0x0) {
mfb_SetProcessDpiAwareness(mfb_PROCESS_PER_MONITOR_DPI_AWARE);
}
else if (mfb_SetProcessDPIAware != 0x0) {
mfb_SetProcessDPIAware();
}
}
//--
void
get_monitor_dpi(HWND hWnd, float *dpi_x, float *dpi_y) {
UINT xdpi, ydpi;
if(mfb_GetDpiForMonitor != 0x0) {
HMONITOR monitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST);
mfb_GetDpiForMonitor(monitor, mfb_MDT_EFFECTIVE_DPI, &xdpi, &ydpi);
}
else {
const HDC dc = GetDC(hWnd);
xdpi = GetDeviceCaps(dc, LOGPIXELSX);
ydpi = GetDeviceCaps(dc, LOGPIXELSY);
ReleaseDC(NULL, dc);
}
if (dpi_x) {
*dpi_x = xdpi / (float) USER_DEFAULT_SCREEN_DPI;
if(*dpi_x == 0) {
*dpi_x = 1;
}
}
if (dpi_y) {
*dpi_y = ydpi / (float) USER_DEFAULT_SCREEN_DPI;
if (*dpi_y == 0) {
*dpi_y = 1;
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
mfb_get_monitor_dpi(struct mfb_window *window, float *dpi_x, float *dpi_y) {
HWND hWnd = 0x0;
if(window != 0x0) {
SWindowData *window_data = (SWindowData *) window;
SWindowData_Win *window_data_win = (SWindowData_Win *) window_data->specific;
hWnd = window_data_win->window;
}
get_monitor_dpi(hWnd, dpi_x, dpi_y);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
long s_window_style = WS_POPUP | WS_SYSMENU | WS_CAPTION;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -37,8 +154,7 @@ WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
#if !defined(USE_OPENGL_API)
case WM_PAINT:
{
if (window_data && window_data->draw_buffer && window_data_win)
{
if (window_data && window_data->draw_buffer && window_data_win) {
StretchDIBits(window_data_win->hdc, window_data->dst_offset_x, window_data->dst_offset_y, window_data->dst_width, window_data->dst_height, 0, 0, window_data->buffer_width, window_data->buffer_height, window_data->draw_buffer,
window_data_win->bitmapInfo, DIB_RGB_COLORS, SRCCOPY);
}
@ -176,16 +292,28 @@ WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
case WM_SIZE:
if (window_data) {
float scaleX, scaleY;
uint32_t width, height;
if(wParam == SIZE_MINIMIZED) {
return res;
}
get_monitor_dpi(hWnd, &scaleX, &scaleY);
window_data->window_width = LOWORD(lParam);
window_data->window_height = HIWORD(lParam);
resize_dst(window_data, LOWORD(lParam), HIWORD(lParam));
resize_dst(window_data, window_data->window_width, window_data->window_height);
#if !defined(USE_OPENGL_API)
BitBlt(window_data_win->hdc, 0, 0, window_data->window_width, window_data->window_height, 0, 0, 0, BLACKNESS);
#else
resize_GL(window_data);
#endif
kCall(resize_func, window_data->window_width, window_data->window_height);
if(window_data->window_width != 0 && window_data->window_height != 0) {
width = (uint32_t) (window_data->window_width / scaleX);
height = (uint32_t) (window_data->window_height / scaleY);
kCall(resize_func, width, height);
}
}
break;
@ -219,6 +347,8 @@ mfb_open_ex(const char *title, unsigned width, unsigned height, unsigned flags)
RECT rect = { 0 };
int x = 0, y = 0;
load_functions();
dpi_aware();
init_keycodes();
SWindowData *window_data = malloc(sizeof(SWindowData));
@ -287,8 +417,11 @@ mfb_open_ex(const char *title, unsigned width, unsigned height, unsigned flags)
}
}
else if (!(flags & WF_FULLSCREEN)) {
rect.right = width;
rect.bottom = height;
float dpi_x, dpi_y;
get_monitor_dpi(0, &dpi_x, &dpi_y);
rect.right = (LONG) (width * dpi_x);
rect.bottom = (LONG) (height * dpi_y);
AdjustWindowRect(&rect, s_window_style, 0);
@ -557,11 +690,7 @@ extern short int g_keycodes[512];
void
init_keycodes() {
// Clear keys
for (size_t i = 0; i < sizeof(g_keycodes) / sizeof(g_keycodes[0]); ++i)
g_keycodes[i] = 0;
if(g_keycodes[0x00B] != KB_KEY_0) {
g_keycodes[0x00B] = KB_KEY_0;
g_keycodes[0x002] = KB_KEY_1;
g_keycodes[0x003] = KB_KEY_2;
@ -683,6 +812,7 @@ init_keycodes() {
g_keycodes[0x11C] = KB_KEY_KP_ENTER;
g_keycodes[0x037] = KB_KEY_KP_MULTIPLY;
g_keycodes[0x04A] = KB_KEY_KP_SUBTRACT;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -716,6 +846,8 @@ translate_key(unsigned int wParam, unsigned long lParam) {
bool
mfb_set_viewport(struct mfb_window *window, unsigned offset_x, unsigned offset_y, unsigned width, unsigned height) {
SWindowData *window_data = (SWindowData *) window;
SWindowData_Win *window_data_win = 0x0;
float scaleX, scaleY;
if(window_data == 0x0) {
return false;
@ -728,11 +860,14 @@ mfb_set_viewport(struct mfb_window *window, unsigned offset_x, unsigned offset_y
return false;
}
window_data->dst_offset_x = offset_x;
window_data->dst_offset_y = offset_y;
window_data_win = (SWindowData_Win *) window_data->specific;
window_data->dst_width = width;
window_data->dst_height = height;
get_monitor_dpi(window_data_win->window, &scaleX, &scaleY);
window_data->dst_offset_x = (uint32_t) (offset_x * scaleX);
window_data->dst_offset_y = (uint32_t) (offset_y * scaleY);
window_data->dst_width = (uint32_t) (width * scaleX);
window_data->dst_height = (uint32_t) (height * scaleY);
calc_dst_factor(window_data, window_data->window_width, window_data->window_height);