Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

this fixes issue116 and explicitly maintains the aspect ratio in the … #201

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions examples/player_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,38 @@ void player_example_handle_event(player_example *player, SDL_Event *event) {
break;
}
case SDLK_f: {

// store the original aspect ratio for reference
int width = event->window.data1;
int height = event->window.data2;
const float ASPECT_RATIO = (float)width/(float)height;
float aspectRatio;

player->fullscreen = !player->fullscreen;
SDL_SetWindowFullscreen(player->screen,
player->fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
SDL_SetWindowFullscreen(player->screen,
player->fullscreen ? SDL_WINDOW_FULLSCREEN : 0);
img_to_rgb(player, player->texture, &player->img, player->plane_mask);
player_example_display_frame(player);

// calculate the aspectRatio after the player->screen has been modified
width = player->width;
height = player->height;
aspectRatio = (float)width/(float)height;

// compare and rectify, to maintain the original aspect ratio
if(aspectRatio != ASPECT_RATIO) {
if(aspectRatio > ASPECT_RATIO) {
height = (1.f / ASPECT_RATIO) * width;
}
else {
width = ASPECT_RATIO * height;
}
printf("Setting window size to %d, %d, aspect ratio: %f\n",
width, height, (float)width/(float)height);
}
player->width = width;
player->height = height;

player_example_display_frame(player);
break;
}
default: break;
Expand All @@ -294,7 +321,7 @@ void player_example_handle_event(player_example *player, SDL_Event *event) {
case SDL_WINDOWEVENT: {
switch (event->window.event) {
case SDL_WINDOWEVENT_RESIZED: {
player_example_display_frame(player);
player_example_display_frame(player);
break;
}
default: break;
Expand Down