Manually position and scale the content
Position and scale the content "manually" instead of relying on the renderer "logical size". This avoids possible rounding differences between the computed window size and the content size, causing one row or column of black pixels on the bottom or on the right. This will also enable to draw items at their expected size on the screen (unscaled).
This commit is contained in:
@@ -175,6 +175,43 @@ get_initial_optimal_size(struct size content_size, uint16_t req_width,
|
||||
return window_size;
|
||||
}
|
||||
|
||||
static void
|
||||
update_content_rect(struct screen *screen) {
|
||||
int dw;
|
||||
int dh;
|
||||
SDL_GL_GetDrawableSize(screen->window, &dw, &dh);
|
||||
|
||||
struct size content_size = screen->content_size;
|
||||
// The drawable size is the window size * the HiDPI scale
|
||||
struct size drawable_size = {dw, dh};
|
||||
|
||||
SDL_Rect *rect = &screen->rect;
|
||||
|
||||
if (is_optimal_size(drawable_size, content_size)) {
|
||||
rect->x = 0;
|
||||
rect->y = 0;
|
||||
rect->w = drawable_size.width;
|
||||
rect->h = drawable_size.height;
|
||||
return;
|
||||
}
|
||||
|
||||
bool keep_width = content_size.width * drawable_size.height
|
||||
> content_size.height * drawable_size.width;
|
||||
if (keep_width) {
|
||||
rect->x = 0;
|
||||
rect->w = drawable_size.width;
|
||||
rect->h = drawable_size.width * content_size.height
|
||||
/ content_size.width;
|
||||
rect->y = (drawable_size.height - rect->h) / 2;
|
||||
} else {
|
||||
rect->y = 0;
|
||||
rect->h = drawable_size.height;
|
||||
rect->w = drawable_size.height * content_size.width
|
||||
/ content_size.height;
|
||||
rect->x = (drawable_size.width - rect->w) / 2;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
screen_init(struct screen *screen) {
|
||||
*screen = (struct screen) SCREEN_INITIALIZER;
|
||||
@@ -264,13 +301,6 @@ screen_init_rendering(struct screen *screen, const char *window_title,
|
||||
const char *renderer_name = r ? NULL : renderer_info.name;
|
||||
LOGI("Renderer: %s", renderer_name ? renderer_name : "(unknown)");
|
||||
|
||||
if (SDL_RenderSetLogicalSize(screen->renderer, content_size.width,
|
||||
content_size.height)) {
|
||||
LOGE("Could not set renderer logical size: %s", SDL_GetError());
|
||||
screen_destroy(screen);
|
||||
return false;
|
||||
}
|
||||
|
||||
// starts with "opengl"
|
||||
screen->use_opengl = renderer_name && !strncmp(renderer_name, "opengl", 6);
|
||||
if (screen->use_opengl) {
|
||||
@@ -314,6 +344,8 @@ screen_init_rendering(struct screen *screen, const char *window_title,
|
||||
return false;
|
||||
}
|
||||
|
||||
update_content_rect(screen);
|
||||
|
||||
screen->windowed_window_size = window_size;
|
||||
|
||||
return true;
|
||||
@@ -348,13 +380,6 @@ screen_set_rotation(struct screen *screen, unsigned rotation) {
|
||||
struct size new_content_size =
|
||||
get_rotated_size(screen->frame_size, rotation);
|
||||
|
||||
if (SDL_RenderSetLogicalSize(screen->renderer,
|
||||
new_content_size.width,
|
||||
new_content_size.height)) {
|
||||
LOGE("Could not set renderer logical size: %s", SDL_GetError());
|
||||
return;
|
||||
}
|
||||
|
||||
struct size windowed_size = get_windowed_window_size(screen);
|
||||
struct size target_size = {
|
||||
.width = (uint32_t) windowed_size.width * new_content_size.width
|
||||
@@ -369,6 +394,7 @@ screen_set_rotation(struct screen *screen, unsigned rotation) {
|
||||
screen->rotation = rotation;
|
||||
LOGI("Display rotation set to %u", rotation);
|
||||
|
||||
update_content_rect(screen);
|
||||
screen_render(screen);
|
||||
}
|
||||
|
||||
@@ -377,18 +403,11 @@ static bool
|
||||
prepare_for_frame(struct screen *screen, struct size new_frame_size) {
|
||||
if (screen->frame_size.width != new_frame_size.width
|
||||
|| screen->frame_size.height != new_frame_size.height) {
|
||||
struct size new_content_size =
|
||||
get_rotated_size(new_frame_size, screen->rotation);
|
||||
if (SDL_RenderSetLogicalSize(screen->renderer,
|
||||
new_content_size.width,
|
||||
new_content_size.height)) {
|
||||
LOGE("Could not set renderer logical size: %s", SDL_GetError());
|
||||
return false;
|
||||
}
|
||||
|
||||
// frame dimension changed, destroy texture
|
||||
SDL_DestroyTexture(screen->texture);
|
||||
|
||||
struct size new_content_size =
|
||||
get_rotated_size(new_frame_size, screen->rotation);
|
||||
struct size content_size = screen->content_size;
|
||||
struct size windowed_size = get_windowed_window_size(screen);
|
||||
struct size target_size = {
|
||||
@@ -402,6 +421,7 @@ prepare_for_frame(struct screen *screen, struct size new_frame_size) {
|
||||
|
||||
screen->frame_size = new_frame_size;
|
||||
screen->content_size = new_content_size;
|
||||
update_content_rect(screen);
|
||||
|
||||
LOGI("New texture: %" PRIu16 "x%" PRIu16,
|
||||
screen->frame_size.width, screen->frame_size.height);
|
||||
@@ -451,7 +471,7 @@ void
|
||||
screen_render(struct screen *screen) {
|
||||
SDL_RenderClear(screen->renderer);
|
||||
if (screen->rotation == 0) {
|
||||
SDL_RenderCopy(screen->renderer, screen->texture, NULL, NULL);
|
||||
SDL_RenderCopy(screen->renderer, screen->texture, NULL, &screen->rect);
|
||||
} else {
|
||||
// rotation in RenderCopyEx() is clockwise, while screen->rotation is
|
||||
// counterclockwise (to be consistent with --lock-video-orientation)
|
||||
@@ -461,12 +481,14 @@ screen_render(struct screen *screen) {
|
||||
SDL_Rect *dstrect = NULL;
|
||||
SDL_Rect rect;
|
||||
if (screen->rotation & 1) {
|
||||
struct size size = screen->content_size;
|
||||
rect.x = (size.width - size.height) / 2;
|
||||
rect.y = (size.height - size.width) / 2;
|
||||
rect.w = size.height;
|
||||
rect.h = size.width;
|
||||
rect.x = screen->rect.x + (screen->rect.w - screen->rect.h) / 2;
|
||||
rect.y = screen->rect.y + (screen->rect.h - screen->rect.w) / 2;
|
||||
rect.w = screen->rect.h;
|
||||
rect.h = screen->rect.w;
|
||||
dstrect = ▭
|
||||
} else {
|
||||
assert(screen->rotation == 2);
|
||||
dstrect = &screen->rect;
|
||||
}
|
||||
|
||||
SDL_RenderCopyEx(screen->renderer, screen->texture, NULL, dstrect,
|
||||
@@ -487,6 +509,7 @@ screen_switch_fullscreen(struct screen *screen) {
|
||||
apply_windowed_size(screen);
|
||||
|
||||
LOGD("Switched to %s mode", screen->fullscreen ? "fullscreen" : "windowed");
|
||||
update_content_rect(screen);
|
||||
screen_render(screen);
|
||||
}
|
||||
|
||||
@@ -530,6 +553,7 @@ screen_handle_window_event(struct screen *screen,
|
||||
const SDL_WindowEvent *event) {
|
||||
switch (event->event) {
|
||||
case SDL_WINDOWEVENT_EXPOSED:
|
||||
update_content_rect(screen);
|
||||
screen_render(screen);
|
||||
break;
|
||||
case SDL_WINDOWEVENT_SIZE_CHANGED:
|
||||
@@ -545,6 +569,7 @@ screen_handle_window_event(struct screen *screen,
|
||||
// window is maximized or fullscreen is enabled.
|
||||
screen->windowed_window_size = get_window_size(screen->window);
|
||||
}
|
||||
update_content_rect(screen);
|
||||
screen_render(screen);
|
||||
break;
|
||||
case SDL_WINDOWEVENT_MAXIMIZED:
|
||||
@@ -574,6 +599,17 @@ screen_convert_to_frame_coords(struct screen *screen, int32_t x, int32_t y) {
|
||||
|
||||
int32_t w = screen->content_size.width;
|
||||
int32_t h = screen->content_size.height;
|
||||
|
||||
// take the HiDPI scaling (dw/ww and dh/wh) into account
|
||||
int ww, wh, dw, dh;
|
||||
SDL_GetWindowSize(screen->window, &ww, &wh);
|
||||
SDL_GL_GetDrawableSize(screen->window, &dw, &dh);
|
||||
|
||||
// scale (64 bits for intermediate multiplications)
|
||||
x = (int64_t) (x - screen->rect.x) * w * dw / (screen->rect.w * ww);
|
||||
y = (int64_t) (y - screen->rect.y) * h * dh / (screen->rect.h * wh);
|
||||
|
||||
// rotate
|
||||
struct point result;
|
||||
switch (rotation) {
|
||||
case 0:
|
||||
|
||||
Reference in New Issue
Block a user