hidpi test
This commit is contained in:
parent
f5530f5195
commit
277d40701c
@ -35,6 +35,21 @@ static struct input_manager input_manager = {
|
|||||||
.screen = &screen,
|
.screen = &screen,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static void hidpi_fix_coordinates(Sint32 *x, Sint32 *y) {
|
||||||
|
struct screen_sizes sizes = screen_get_sizes(&screen);
|
||||||
|
Uint16 ww = sizes.window_size.width;
|
||||||
|
Uint16 wh = sizes.window_size.height;
|
||||||
|
Uint16 dw = sizes.drawable_size.width;
|
||||||
|
Uint16 dh = sizes.drawable_size.height;
|
||||||
|
printf("window=%dx%d; drawable=%dx%d\n", (int) ww, (int) wh, (int) dw, (int) dh);
|
||||||
|
if (dw && dw != ww) {
|
||||||
|
*x = ((Sint64) *x) * ww / dw;
|
||||||
|
}
|
||||||
|
if (dh && dh != wh) {
|
||||||
|
*y = ((Sint64) *y) * wh / dh;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static void event_loop(void) {
|
static void event_loop(void) {
|
||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
while (SDL_WaitEvent(&event)) {
|
while (SDL_WaitEvent(&event)) {
|
||||||
@ -72,14 +87,17 @@ static void event_loop(void) {
|
|||||||
input_manager_process_key(&input_manager, &event.key);
|
input_manager_process_key(&input_manager, &event.key);
|
||||||
break;
|
break;
|
||||||
case SDL_MOUSEMOTION:
|
case SDL_MOUSEMOTION:
|
||||||
|
hidpi_fix_coordinates(&event.motion.x, &event.motion.y);
|
||||||
input_manager_process_mouse_motion(&input_manager, &event.motion);
|
input_manager_process_mouse_motion(&input_manager, &event.motion);
|
||||||
break;
|
break;
|
||||||
case SDL_MOUSEWHEEL: {
|
case SDL_MOUSEWHEEL: {
|
||||||
|
hidpi_fix_coordinates(&event.wheel.x, &event.wheel.y);
|
||||||
input_manager_process_mouse_wheel(&input_manager, &event.wheel);
|
input_manager_process_mouse_wheel(&input_manager, &event.wheel);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case SDL_MOUSEBUTTONDOWN:
|
case SDL_MOUSEBUTTONDOWN:
|
||||||
case SDL_MOUSEBUTTONUP: {
|
case SDL_MOUSEBUTTONUP: {
|
||||||
|
hidpi_fix_coordinates(&event.button.y, &event.button.y);
|
||||||
input_manager_process_mouse_button(&input_manager, &event.button);
|
input_manager_process_mouse_button(&input_manager, &event.button);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -45,6 +45,27 @@ static struct size get_native_window_size(SDL_Window *window) {
|
|||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get the size of the window underlying drawable in pixels
|
||||||
|
// may differ from get_native_window_size() if hi-dpi is enabled
|
||||||
|
static struct size get_native_drawable_size(SDL_Window *window) {
|
||||||
|
int width;
|
||||||
|
int height;
|
||||||
|
SDL_GL_GetDrawableSize(window, &width, &height);
|
||||||
|
|
||||||
|
struct size size;
|
||||||
|
size.width = width;
|
||||||
|
size.height = height;
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
// return both the native window size and native drawable size
|
||||||
|
struct screen_sizes screen_get_sizes(const struct screen *screen) {
|
||||||
|
struct screen_sizes sizes;
|
||||||
|
sizes.window_size = get_native_window_size(screen->window);
|
||||||
|
sizes.drawable_size = get_native_drawable_size(screen->window);
|
||||||
|
return sizes;
|
||||||
|
}
|
||||||
|
|
||||||
// get the windowed window size
|
// get the windowed window size
|
||||||
static struct size get_window_size(const struct screen *screen) {
|
static struct size get_window_size(const struct screen *screen) {
|
||||||
if (screen->fullscreen) {
|
if (screen->fullscreen) {
|
||||||
@ -141,7 +162,8 @@ SDL_bool screen_init_rendering(struct screen *screen, const char *device_name, s
|
|||||||
|
|
||||||
struct size window_size = get_initial_optimal_size(frame_size);
|
struct size window_size = get_initial_optimal_size(frame_size);
|
||||||
screen->window = SDL_CreateWindow(device_name, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
|
screen->window = SDL_CreateWindow(device_name, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
|
||||||
window_size.width, window_size.height, SDL_WINDOW_HIDDEN | SDL_WINDOW_RESIZABLE);
|
window_size.width, window_size.height,
|
||||||
|
SDL_WINDOW_HIDDEN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
|
||||||
if (!screen->window) {
|
if (!screen->window) {
|
||||||
LOGC("Could not create window: %s", SDL_GetError());
|
LOGC("Could not create window: %s", SDL_GetError());
|
||||||
return SDL_FALSE;
|
return SDL_FALSE;
|
||||||
|
@ -34,6 +34,15 @@ struct screen {
|
|||||||
.fullscreen = SDL_FALSE, \
|
.fullscreen = SDL_FALSE, \
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// the window and drawable size may differ if hi-dpi is enabled
|
||||||
|
struct screen_sizes {
|
||||||
|
// the size of the window client area, as reported by SDL_GetWindowSize()
|
||||||
|
struct size window_size;
|
||||||
|
// the size of the window underlying drawable, as reported by
|
||||||
|
// SDL_GL_GetDrawableSize()
|
||||||
|
struct size drawable_size;
|
||||||
|
};
|
||||||
|
|
||||||
// init SDL and set appropriate hints
|
// init SDL and set appropriate hints
|
||||||
SDL_bool sdl_init_and_configure(void);
|
SDL_bool sdl_init_and_configure(void);
|
||||||
|
|
||||||
@ -66,4 +75,6 @@ void screen_resize_to_fit(struct screen *screen);
|
|||||||
// resize window to 1:1 (pixel-perfect)
|
// resize window to 1:1 (pixel-perfect)
|
||||||
void screen_resize_to_pixel_perfect(struct screen *screen);
|
void screen_resize_to_pixel_perfect(struct screen *screen);
|
||||||
|
|
||||||
|
struct screen_sizes screen_get_sizes(const struct screen *screen);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user