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

Window handling changes (zoom + fullscreen) #1406

Merged
merged 3 commits into from Sep 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/baseui.cpp
Expand Up @@ -33,9 +33,13 @@

std::shared_ptr<BaseUi> DisplayUi;

std::shared_ptr<BaseUi> BaseUi::CreateUi(long width, long height, bool fs_flag, bool /* zoom */) {
std::shared_ptr<BaseUi> BaseUi::CreateUi(long width, long height, bool fs_flag, int zoom) {
#if USE_SDL!=2
/* unused */
(void) zoom;
#endif
#if USE_SDL==2
return std::make_shared<Sdl2Ui>(width, height, fs_flag);
return std::make_shared<Sdl2Ui>(width, height, fs_flag, zoom);
#elif USE_SDL==1
return std::make_shared<SdlUi>(width, height, fs_flag);
#elif _3DS
Expand Down
8 changes: 4 additions & 4 deletions src/baseui.h
Expand Up @@ -48,9 +48,9 @@ class BaseUi {
* @param width display client width.
* @param height display client height.
* @param fullscreen start in fullscreen flag.
* @param zoom start with zoom flag.
* @param zoom initial magnification factor.
*/
static std::shared_ptr<BaseUi> CreateUi(long width, long height, bool fullscreen, bool zoom);
static std::shared_ptr<BaseUi> CreateUi(long width, long height, bool fullscreen, int zoom);

/**
* Begins a display mode change.
Expand Down Expand Up @@ -223,9 +223,9 @@ class BaseUi {
* Display mode data struct.
*/
struct DisplayMode {
DisplayMode() : effective(false), zoom(false), width(0), height(0), bpp(0), flags(0) {}
DisplayMode() : effective(false), zoom(0), width(0), height(0), bpp(0), flags(0) {}
bool effective;
bool zoom;
int zoom;
int width;
int height;
uint8_t bpp;
Expand Down
5 changes: 2 additions & 3 deletions src/options.h
Expand Up @@ -22,7 +22,6 @@
#define GAME_TITLE "EasyRPG Player"

/** Targeted screen default width. */
//#define SCREEN_TARGET_WIDTH 640
#define SCREEN_TARGET_WIDTH 320

/** Targeted screen default height. */
Expand All @@ -40,8 +39,8 @@
/** Run game in fullscreen mode. */
#define RUN_FULLSCREEN 0

/** Run game in zoom mode. */
#define RUN_ZOOM 1
/** Run game with this magnification factor. */
#define RUN_ZOOM 2

/**
* Pause the game process when the player window
Expand Down
51 changes: 39 additions & 12 deletions src/sdl2_ui.cpp
Expand Up @@ -79,9 +79,9 @@ static int FilterUntilFocus(const SDL_Event* evnt);
static Input::Keys::InputKey SdlJKey2InputKey(int button_index);
#endif

Sdl2Ui::Sdl2Ui(long width, long height, bool fs_flag) :
Sdl2Ui::Sdl2Ui(long width, long height, bool fullscreen, int zoom) :
BaseUi(),
zoom_available(true),
zoom_available(false),
toggle_fs_available(false),
mode_changing(false) {

Expand All @@ -105,11 +105,16 @@ Sdl2Ui::Sdl2Ui(long width, long height, bool fs_flag) :
sdl_window = NULL;

BeginDisplayModeChange();
if (!RequestVideoMode(width, height, fs_flag)) {
if (!RequestVideoMode(width, height, zoom)) {
Output::Error("No suitable video resolution found. Aborting.");
}
EndDisplayModeChange();

// Work around some SDL bugs, window properties are incorrect when started
// as full screen, e.g. height lacks title bar size, icon is not added, etc.
if (fullscreen)
ToggleFullscreen();

SetTitle(GAME_TITLE);

#if (defined(USE_JOYSTICK) && defined(SUPPORT_JOYSTICK)) || (defined(USE_JOYSTICK_AXIS) && defined(SUPPORT_JOYSTICK_AXIS)) || (defined(USE_JOYSTICK_HAT) && defined(SUPPORT_JOYSTICK_HAT))
Expand Down Expand Up @@ -163,18 +168,15 @@ void Sdl2Ui::Sleep(uint32_t time) {
#endif
}

bool Sdl2Ui::RequestVideoMode(int width, int height, bool fullscreen) {
bool Sdl2Ui::RequestVideoMode(int width, int height, int zoom) {
// SDL2 documentation says that resolution dependent code should not be used
// anymore. The library takes care of it now.
current_display_mode.width = width;
current_display_mode.height = height;
current_display_mode.bpp = 32;
if (fullscreen) {
current_display_mode.flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
}
toggle_fs_available = true;

current_display_mode.zoom = true;
current_display_mode.zoom = zoom;
#ifdef SUPPORT_ZOOM
zoom_available = true;
#else
Expand Down Expand Up @@ -223,9 +225,9 @@ bool Sdl2Ui::RefreshDisplayMode() {
int display_width = current_display_mode.width;
int display_height = current_display_mode.height;

if (zoom_available && current_display_mode.zoom) {
display_width *= 2;
display_height *= 2;
if (zoom_available) {
display_width *= current_display_mode.zoom;
display_height *= current_display_mode.zoom;
}

if (!sdl_window) {
Expand Down Expand Up @@ -259,6 +261,11 @@ bool Sdl2Ui::RefreshDisplayMode() {
sdl_renderer = SDL_CreateRenderer(sdl_window, -1, rendered_flag);
if (!sdl_renderer)
return false;

// Flush display
SDL_RenderClear(sdl_renderer);
SDL_RenderPresent(sdl_renderer);

SDL_RenderSetLogicalSize(sdl_renderer, SCREEN_TARGET_WIDTH, SCREEN_TARGET_HEIGHT);

uint32_t const texture_format =
Expand Down Expand Up @@ -357,7 +364,27 @@ void Sdl2Ui::ToggleZoom() {
}

if (zoom_available && mode_changing) {
current_display_mode.zoom = !current_display_mode.zoom;
// get current window size, calculate next bigger zoom factor
int w, h;
SDL_GetWindowSize(sdl_window, &w, &h);
last_display_mode.zoom = std::min(w / SCREEN_TARGET_WIDTH, h / SCREEN_TARGET_HEIGHT);
current_display_mode.zoom = last_display_mode.zoom + 1;

// get maximum usable window size
int display_index = SDL_GetWindowDisplayIndex(sdl_window);
SDL_Rect max_mode;
#if SDL_VERSION_ATLEAST(2, 0, 5)
// this takes account of the menu bar and dock on macOS and task bar on windows
SDL_GetDisplayUsableBounds(display_index, &max_mode);
#else
SDL_GetDisplayBounds(display_index, &max_mode);
#endif

// reset zoom, if it does not fit
if ((max_mode.h < SCREEN_TARGET_HEIGHT * current_display_mode.zoom) ||
(max_mode.w < SCREEN_TARGET_WIDTH * current_display_mode.zoom)) {
current_display_mode.zoom = 1;
}
}
EndDisplayModeChange();
}
Expand Down
5 changes: 3 additions & 2 deletions src/sdl2_ui.h
Expand Up @@ -46,8 +46,9 @@ class Sdl2Ui : public BaseUi {
* @param width window client width.
* @param height window client height.
* @param fullscreen start in fullscreen flag.
* @param zoom initial magnification factor.
*/
Sdl2Ui(long width, long height, bool fullscreen);
Sdl2Ui(long width, long height, bool fullscreen, int zoom);

/**
* Destructor.
Expand Down Expand Up @@ -125,7 +126,7 @@ class Sdl2Ui : public BaseUi {
bool zoom_available;
bool toggle_fs_available;

bool RequestVideoMode(int width, int height, bool fullscreen);
bool RequestVideoMode(int width, int height, int zoom);

/** Last display mode. */
DisplayMode last_display_mode;
Expand Down
28 changes: 16 additions & 12 deletions src/sdl_ui.cpp
Expand Up @@ -192,9 +192,10 @@ bool SdlUi::RequestVideoMode(int width, int height, bool fullscreen) {
// All modes available
if (modes == (SDL_Rect **)-1) {
// If we have a high res, turn zoom on
current_display_mode.zoom = (vinfo->current_h > height*2 && vinfo->current_w > width*2);
if (vinfo->current_h > height*2 && vinfo->current_w > width*2)
current_display_mode.zoom = 2;
#if defined(SUPPORT_ZOOM)
zoom_available = current_display_mode.zoom;
zoom_available = current_display_mode.zoom == 2;
#else
zoom_available = false;
#endif
Expand All @@ -211,8 +212,8 @@ bool SdlUi::RequestVideoMode(int width, int height, bool fullscreen) {
|| (modes[i]->h == height*2 && modes[i]->w == width*2)
#endif
) {
current_display_mode.zoom = ((modes[i]->w >> 1) == width);
zoom_available = current_display_mode.zoom;
current_display_mode.zoom = modes[i]->w / width;
zoom_available = current_display_mode.zoom == 2;
return true;
}
}
Expand Down Expand Up @@ -246,8 +247,8 @@ bool SdlUi::RequestVideoMode(int width, int height, bool fullscreen) {
if (modes == (SDL_Rect **)-1) {
// All modes available
current_display_mode.flags = flags;
current_display_mode.zoom = false;
zoom_available = current_display_mode.zoom;
current_display_mode.zoom = 1;
zoom_available = false;
return true;
}

Expand All @@ -263,8 +264,8 @@ bool SdlUi::RequestVideoMode(int width, int height, bool fullscreen) {
) {
current_display_mode.flags = flags;
// FIXME: we have to find a way to make zoom possible only in windowed mode
current_display_mode.zoom = ((modes[i]->w >> 1) == width);
zoom_available = current_display_mode.zoom;
current_display_mode.zoom = modes[i]->w / width;
zoom_available = current_display_mode.zoom == 2;
return true;
}
}
Expand Down Expand Up @@ -312,7 +313,7 @@ bool SdlUi::RefreshDisplayMode() {
int display_width = current_display_mode.width;
int display_height = current_display_mode.height;

if (zoom_available && current_display_mode.zoom) {
if (zoom_available && current_display_mode.zoom == 2) {
display_width *= 2;
display_height *= 2;
}
Expand Down Expand Up @@ -342,7 +343,7 @@ bool SdlUi::RefreshDisplayMode() {

Bitmap::SetFormat(Bitmap::ChooseFormat(format));

if (zoom_available && current_display_mode.zoom) {
if (zoom_available && current_display_mode.zoom == 2) {
// Create a non zoomed surface as drawing surface
main_surface = Bitmap::Create(current_display_mode.width,
current_display_mode.height,
Expand Down Expand Up @@ -380,7 +381,10 @@ void SdlUi::ToggleFullscreen() {
void SdlUi::ToggleZoom() {
BeginDisplayModeChange();
if (zoom_available && mode_changing) {
current_display_mode.zoom = !current_display_mode.zoom;
if(current_display_mode.zoom == 2)
current_display_mode.zoom = 1;
else
current_display_mode.zoom = 2;
}
EndDisplayModeChange();
}
Expand All @@ -398,7 +402,7 @@ void SdlUi::ProcessEvents() {
}

void SdlUi::UpdateDisplay() {
if (zoom_available && current_display_mode.zoom) {
if (zoom_available && current_display_mode.zoom == 2) {
// Blit drawing surface x2 scaled over window surface
Blit2X(*main_surface, sdl_surface);
}
Expand Down