Skip to content

Commit 92b4faf

Browse files
committed
Enable HiDPI mode for SDL and ensure that we use nearest-neighbor scaling
Avoids blurriness on macOS hosts with Retina displays.
1 parent 873f863 commit 92b4faf

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

devices/video/display_sdl.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class Display::Impl {
2929
uint32_t disp_wnd_id = 0;
3030
SDL_Window* display_wnd = 0;
3131
SDL_Renderer* renderer = 0;
32+
double renderer_scale_x; // scaling factor from guest OS to host OS
33+
double renderer_scale_y;
3234
SDL_Texture* disp_texture = 0;
3335
SDL_Texture* cursor_texture = 0;
3436
SDL_Rect cursor_rect; // destination rectangle for cursor drawing
@@ -61,7 +63,7 @@ bool Display::configure(int width, int height) {
6163
SDL_WINDOWPOS_UNDEFINED,
6264
SDL_WINDOWPOS_UNDEFINED,
6365
width, height,
64-
SDL_WINDOW_OPENGL
66+
SDL_WINDOW_OPENGL | SDL_WINDOW_ALLOW_HIGHDPI
6567
);
6668

6769
impl->disp_wnd_id = SDL_GetWindowID(impl->display_wnd);
@@ -72,6 +74,11 @@ bool Display::configure(int width, int height) {
7274
if (impl->renderer == NULL)
7375
ABORT_F("Display: SDL_CreateRenderer failed with %s", SDL_GetError());
7476

77+
int drawable_width, drawable_height;
78+
SDL_GetRendererOutputSize(impl->renderer, &drawable_width, &drawable_height);
79+
impl->renderer_scale_x = static_cast<double>(drawable_width) / width;
80+
impl->renderer_scale_y = static_cast<float>(drawable_height) / height;
81+
7582
is_initialization = true;
7683
} else { // resize display window
7784
SDL_SetWindowSize(impl->display_wnd, width, height);
@@ -133,8 +140,8 @@ void Display::update(std::function<void(uint8_t *dst_buf, int dst_pitch)> conver
133140

134141
// draw HW cursor if enabled
135142
if (draw_hw_cursor) {
136-
impl->cursor_rect.x = cursor_x;
137-
impl->cursor_rect.y = cursor_y;
143+
impl->cursor_rect.x = cursor_x * impl->renderer_scale_x;
144+
impl->cursor_rect.y = cursor_y * impl->renderer_scale_y;
138145
SDL_RenderCopy(impl->renderer, impl->cursor_texture, NULL, &impl->cursor_rect);
139146
}
140147

@@ -170,6 +177,6 @@ void Display::setup_hw_cursor(std::function<void(uint8_t *dst_buf, int dst_pitch
170177

171178
impl->cursor_rect.x = 0;
172179
impl->cursor_rect.y = 0;
173-
impl->cursor_rect.w = cursor_width;
174-
impl->cursor_rect.h = cursor_height;
180+
impl->cursor_rect.w = cursor_width * impl->renderer_scale_x;
181+
impl->cursor_rect.h = cursor_height * impl->renderer_scale_y;
175182
}

main_sdl.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ bool init() {
4343
}
4444
}
4545

46+
// Make sure we always use nearest-neighbor to scale window contents.
47+
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0");
48+
4649
#ifdef __APPLE__
4750
remap_appkit_menu_shortcuts();
4851
#endif

0 commit comments

Comments
 (0)