demos: Add basic DPI handling.

This commit is contained in:
Henri Verbeet
2025-03-17 16:52:55 +01:00
parent 21e08955d3
commit 110edf32d0
Notes: Henri Verbeet 2025-03-19 16:02:33 +01:00
Approved-by: Henri Verbeet (@hverbeet)
Merge-Request: https://gitlab.winehq.org/wine/vkd3d/-/merge_requests/1432
4 changed files with 39 additions and 0 deletions

View File

@ -31,6 +31,8 @@ struct demo
void *user_data; void *user_data;
void (*idle_func)(struct demo *demo, void *user_data); void (*idle_func)(struct demo *demo, void *user_data);
UINT (*GetDpiForSystem)(void);
}; };
struct demo_window struct demo_window
@ -48,6 +50,11 @@ struct demo_swapchain
IDXGISwapChain3 *swapchain; IDXGISwapChain3 *swapchain;
}; };
static inline void demo_get_dpi(struct demo *demo, double *dpi_x, double *dpi_y)
{
*dpi_x = *dpi_y = demo->GetDpiForSystem();
}
static inline struct demo_window *demo_window_create(struct demo *demo, const char *title, static inline struct demo_window *demo_window_create(struct demo *demo, const char *title,
unsigned int width, unsigned int height, void *user_data) unsigned int width, unsigned int height, void *user_data)
{ {
@ -191,6 +198,11 @@ static inline void demo_process_events(struct demo *demo)
} }
} }
static inline UINT demo_GetDpiForSystem(void)
{
return 96;
}
static inline bool demo_init(struct demo *demo, void *user_data) static inline bool demo_init(struct demo *demo, void *user_data)
{ {
WNDCLASSEXW wc; WNDCLASSEXW wc;
@ -215,6 +227,11 @@ static inline bool demo_init(struct demo *demo, void *user_data)
demo->user_data = user_data; demo->user_data = user_data;
demo->idle_func = NULL; demo->idle_func = NULL;
if ((demo->GetDpiForSystem = (void *)GetProcAddress(GetModuleHandleA("user32"), "GetDpiForSystem")))
SetProcessDPIAware();
else
demo->GetDpiForSystem = demo_GetDpiForSystem;
return true; return true;
} }

View File

@ -198,6 +198,20 @@ static inline xcb_screen_t *demo_get_screen(xcb_connection_t *c, int idx)
return NULL; return NULL;
} }
static inline void demo_get_dpi(struct demo *demo, double *dpi_x, double *dpi_y)
{
xcb_screen_t *screen;
if (!(screen = demo_get_screen(demo->connection, demo->screen)))
{
*dpi_x = *dpi_y = 96.0;
return;
}
*dpi_x = screen->width_in_pixels * 25.4 / screen->width_in_millimeters;
*dpi_y = screen->height_in_pixels * 25.4 / screen->height_in_millimeters;
}
static inline struct demo_window *demo_window_create(struct demo *demo, const char *title, static inline struct demo_window *demo_window_create(struct demo *demo, const char *title,
unsigned int width, unsigned int height, void *user_data) unsigned int width, unsigned int height, void *user_data)
{ {

View File

@ -849,12 +849,16 @@ static int cxg_main(void)
{ {
unsigned int width = 300, height = 300; unsigned int width = 300, height = 300;
struct cx_gears cxg; struct cx_gears cxg;
double dpi_x, dpi_y;
memset(&cxg, 0, sizeof(cxg)); memset(&cxg, 0, sizeof(cxg));
if (!demo_init(&cxg.demo, &cxg)) if (!demo_init(&cxg.demo, &cxg))
return EXIT_FAILURE; return EXIT_FAILURE;
demo_set_idle_func(&cxg.demo, cxg_idle); demo_set_idle_func(&cxg.demo, cxg_idle);
demo_get_dpi(&cxg.demo, &dpi_x, &dpi_y);
width *= dpi_x / 96.0;
height *= dpi_y / 96.0;
cxg.window = demo_window_create(&cxg.demo, "Vkd3d Gears", width, height, &cxg); cxg.window = demo_window_create(&cxg.demo, "Vkd3d Gears", width, height, &cxg);
demo_window_set_key_press_func(cxg.window, cxg_key_press); demo_window_set_key_press_func(cxg.window, cxg_key_press);
demo_window_set_expose_func(cxg.window, cxg_expose); demo_window_set_expose_func(cxg.window, cxg_expose);

View File

@ -368,12 +368,16 @@ static int cxt_main(void)
{ {
unsigned int width = 640, height = 480; unsigned int width = 640, height = 480;
struct cx_triangle cxt; struct cx_triangle cxt;
double dpi_x, dpi_y;
memset(&cxt, 0, sizeof(cxt)); memset(&cxt, 0, sizeof(cxt));
if (!demo_init(&cxt.demo, NULL)) if (!demo_init(&cxt.demo, NULL))
return EXIT_FAILURE; return EXIT_FAILURE;
demo_get_dpi(&cxt.demo, &dpi_x, &dpi_y);
width *= dpi_x / 96.0;
height *= dpi_y / 96.0;
cxt.window = demo_window_create(&cxt.demo, "Vkd3d Triangle", width, height, &cxt); cxt.window = demo_window_create(&cxt.demo, "Vkd3d Triangle", width, height, &cxt);
demo_window_set_expose_func(cxt.window, cxt_render_frame); demo_window_set_expose_func(cxt.window, cxt_render_frame);
demo_window_set_key_press_func(cxt.window, cxt_key_press); demo_window_set_key_press_func(cxt.window, cxt_key_press);