Work around for multiple displays with respective scales

This should resolve Genymobile/scrcpy#15
This commit is contained in:
Shaoxing Wang 2018-08-26 19:32:32 +08:00
parent fdbb725436
commit 48e10b9daa
3 changed files with 25 additions and 0 deletions

View file

@ -84,6 +84,17 @@ static SDL_bool event_loop(void) {
case SDL_WINDOWEVENT_EXPOSED:
case SDL_WINDOWEVENT_SIZE_CHANGED:
screen_render(&screen);
int scale_x, scale_y;
get_window_scale(SDL_GetWindowFromID(event.button.windowID), &scale_x, &scale_y);
if (scale_x != screen.scale_x || scale_y != screen.scale_y) {
int logical_width, logical_height;
SDL_RenderGetLogicalSize(screen.renderer, &logical_width, &logical_height);
logical_width *= (float)scale_x / screen.scale_x;
logical_height *= (float)scale_y / screen.scale_y;
screen.scale_x = scale_x;
screen.scale_y = scale_y;
SDL_RenderSetLogicalSize(screen.renderer, logical_width, logical_height);
}
break;
}
break;

View file

@ -65,6 +65,15 @@ static void set_window_size(struct screen *screen, struct size new_size) {
}
}
void get_window_scale(SDL_Window *window, int *scale_x, int *scale_y) {
int win_w, win_h;
SDL_GetWindowSize(window, &win_w, &win_h);
int output_w, output_h;
SDL_GL_GetDrawableSize(window, &output_w, &output_h);
*scale_x = output_w / win_w;
*scale_y = output_h / win_h;
}
// get the preferred display bounds (i.e. the screen bounds with some margins)
static SDL_bool get_preferred_display_bounds(struct size *bounds) {
SDL_Rect rect;
@ -168,6 +177,7 @@ SDL_bool screen_init_rendering(struct screen *screen, const char *device_name, s
screen_destroy(screen);
return SDL_FALSE;
}
get_window_scale(screen->window, &screen->scale_x, &screen->scale_y);
SDL_Surface *icon = read_xpm(icon_xpm);
if (!icon) {

View file

@ -12,6 +12,8 @@ struct screen {
SDL_Renderer *renderer;
SDL_Texture *texture;
struct size frame_size;
int scale_x;
int scale_y;
//used only in fullscreen mode to know the windowed window size
struct size windowed_window_size;
SDL_bool has_frame;
@ -66,4 +68,6 @@ void screen_resize_to_fit(struct screen *screen);
// resize window to 1:1 (pixel-perfect)
void screen_resize_to_pixel_perfect(struct screen *screen);
void get_window_scale(SDL_Window *window, int *scale_x, int *scale_y);
#endif