Skip to content

Commit

Permalink
Store rotated content size
Browse files Browse the repository at this point in the history
This avoids to compute it every time from the frame size.
  • Loading branch information
rom1v committed Apr 8, 2020
1 parent c1ebea2 commit f3fba3c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
22 changes: 9 additions & 13 deletions app/src/screen.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ screen_init_rendering(struct screen *screen, const char *window_title,
if (rotation) {
LOGI("Initial display rotation set to %u", rotation);
}
struct size content_size =
get_rotated_size(frame_size, screen->rotation);
struct size content_size = get_rotated_size(frame_size, screen->rotation);
screen->content_size = content_size;

struct size window_size =
get_initial_optimal_size(content_size, window_width, window_height);
Expand Down Expand Up @@ -280,8 +280,7 @@ screen_set_rotation(struct screen *screen, unsigned rotation) {
return;
}

struct size old_content_size =
get_rotated_size(screen->frame_size, screen->rotation);
struct size old_content_size = screen->content_size;
struct size new_content_size =
get_rotated_size(screen->frame_size, rotation);

Expand All @@ -302,6 +301,7 @@ screen_set_rotation(struct screen *screen, unsigned rotation) {
target_size = get_optimal_size(target_size, new_content_size);
set_window_size(screen, target_size);

screen->content_size = new_content_size;
screen->rotation = rotation;
LOGI("Display rotation set to %u", rotation);

Expand All @@ -325,8 +325,7 @@ prepare_for_frame(struct screen *screen, struct size new_frame_size) {
// frame dimension changed, destroy texture
SDL_DestroyTexture(screen->texture);

struct size content_size =
get_rotated_size(screen->frame_size, screen->rotation);
struct size content_size = screen->content_size;
struct size windowed_size = get_windowed_window_size(screen);
struct size target_size = {
(uint32_t) windowed_size.width * new_content_size.width
Expand All @@ -338,6 +337,7 @@ prepare_for_frame(struct screen *screen, struct size new_frame_size) {
set_window_size(screen, target_size);

screen->frame_size = new_frame_size;
screen->content_size = new_content_size;

LOGI("New texture: %" PRIu16 "x%" PRIu16,
screen->frame_size.width, screen->frame_size.height);
Expand Down Expand Up @@ -390,8 +390,7 @@ screen_render(struct screen *screen) {
SDL_Rect *dstrect = NULL;
SDL_Rect rect;
if (screen->rotation & 1) {
struct size size =
get_rotated_size(screen->frame_size, screen->rotation);
struct size size = screen->content_size;
rect.x = (size.width - size.height) / 2;
rect.y = (size.height - size.width) / 2;
rect.w = size.height;
Expand Down Expand Up @@ -431,10 +430,8 @@ screen_resize_to_fit(struct screen *screen) {
screen->maximized = false;
}

struct size content_size =
get_rotated_size(screen->frame_size, screen->rotation);
struct size optimal_size =
get_optimal_window_size(screen, content_size);
get_optimal_window_size(screen, screen->content_size);
SDL_SetWindowSize(screen->window, optimal_size.width, optimal_size.height);
LOGD("Resized to optimal size");
}
Expand All @@ -450,8 +447,7 @@ screen_resize_to_pixel_perfect(struct screen *screen) {
screen->maximized = false;
}

struct size content_size =
get_rotated_size(screen->frame_size, screen->rotation);
struct size content_size = screen->content_size;
SDL_SetWindowSize(screen->window, content_size.width, content_size.height);
LOGD("Resized to pixel-perfect");
}
Expand Down
7 changes: 6 additions & 1 deletion app/src/screen.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ struct screen {
SDL_Renderer *renderer;
SDL_Texture *texture;
struct size frame_size;
struct size content_size; // rotated frame_size
// The window size the last time it was not maximized or fullscreen.
struct size windowed_window_size;
// Since we receive the event SIZE_CHANGED before MAXIMIZED, we must be
Expand All @@ -35,7 +36,11 @@ struct screen {
.renderer = NULL, \
.texture = NULL, \
.frame_size = { \
.width = 0, \
.width = 0, \
.height = 0, \
}, \
.content_size = { \
.width = 0, \
.height = 0, \
}, \
.windowed_window_size = { \
Expand Down

0 comments on commit f3fba3c

Please sign in to comment.