Skip to content

Commit

Permalink
Resolution fix (#371)
Browse files Browse the repository at this point in the history
Should fix #369.

---------

Co-authored-by: Richard Kettering <kettering.richard@gmail.com>
  • Loading branch information
DDR0 and rkettering authored Dec 11, 2023
1 parent 30ba105 commit 8d640b9
Show file tree
Hide file tree
Showing 13 changed files with 219 additions and 172 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ modules/*
!modules/gui
!modules/tbs
.vscode/
/external/*

#Ignore this, don't know why it's being created for me.
imgui.ini
Expand Down
6 changes: 5 additions & 1 deletion src/kre/WindowManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,11 @@ namespace KRE
void Window::enableResizeableWindow(bool en) {
is_resizeable_ = en;
}


//Use `graphics::GameScreen::get().setFullscreen(fullscreenCheckbox->checked()`,
//instead of this function, like `KRE::WindowManager::getMainWindow()->setFullscreenMode(fullscreenCheckbox->checked()`.
//This function is a lower-level function called from setFullscreen - this one
//*only* changes the window size, and doesn't recalculate the internal scaling.
void Window::setFullscreenMode(FullScreenMode mode)
{
bool modes_differ = fullscreen_mode_ != mode;
Expand Down
6 changes: 0 additions & 6 deletions src/kre/WindowManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@

namespace KRE
{
enum class FullScreenMode {
WINDOWED,
FULLSCREEN_WINDOWED,
FULLSCREEN_EXCLUSIVE,
};

struct WindowMode
{
int width;
Expand Down
6 changes: 6 additions & 0 deletions src/kre/WindowManagerFwd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@

namespace KRE
{
enum class FullScreenMode {
WINDOWED,
FULLSCREEN_WINDOWED,
FULLSCREEN_EXCLUSIVE,
};

class Surface;
typedef std::shared_ptr<Surface> SurfacePtr;

Expand Down
48 changes: 7 additions & 41 deletions src/level_runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ extern std::map<std::string, variant> g_user_info_registry;

PREF_STRING(editor_object, "", "Object to use for the editor");

extern bool g_desktop_fullscreen;
extern bool g_particle_editor;
extern int g_vsync;

Expand Down Expand Up @@ -1576,7 +1575,7 @@ bool LevelRunner::play_cycle()
pause_next = true;
}

} else if(key == SDLK_f && mod & KMOD_CTRL && !preferences::no_fullscreen_ever()) {
} else if(((key == SDLK_f && mod & KMOD_CTRL) || key == SDLK_F11) && !preferences::no_fullscreen_ever()) {

static int last_pushed = -1;

Expand All @@ -1592,45 +1591,12 @@ bool LevelRunner::play_cycle()
const int virtual_height = gs.getVirtualHeight();

last_pushed = nevent_frame;
LOG_DEBUG("ctrl-f pushed");
// XXX this changes if editor is active.
if(wnd->fullscreenMode() == KRE::FullScreenMode::WINDOWED) {

LOG_DEBUG("Enter full-screen mode");
wnd->setFullscreenMode(KRE::FullScreenMode::FULLSCREEN_WINDOWED);

if(preferences::auto_size_window() || g_desktop_fullscreen) {
SDL_DisplayMode dm;
if(SDL_GetDesktopDisplayMode(0, &dm) == 0) {
preferences::adjust_virtual_width_to_match_physical(dm.w, dm.h);
wnd->setWindowSize(dm.w, dm.h);
gs.setDimensions(dm.w, dm.h);
gs.setVirtualDimensions(preferences::requested_virtual_window_width(), preferences::requested_virtual_window_height());
}

}

} else {
LOG_DEBUG("Enter windowed mode");
wnd->setFullscreenMode(KRE::FullScreenMode::WINDOWED);

if(preferences::auto_size_window() || g_desktop_fullscreen) {
int width = 0, height = 0;

if(preferences::requested_window_width() > 0 && preferences::requested_window_height() > 0) {
width = preferences::requested_window_width();
height = preferences::requested_window_height();
} else {
auto_select_resolution(wnd, width, height, true, false);
}

preferences::adjust_virtual_width_to_match_physical(width, height);

wnd->setWindowSize(width, height);
gs.setDimensions(width, height);
gs.setVirtualDimensions(preferences::requested_virtual_window_width(), preferences::requested_virtual_window_height());
}
}
// This changes if editor is active.
gs.setFullscreen(
wnd->fullscreenMode() == KRE::FullScreenMode::WINDOWED
? KRE::FullScreenMode::FULLSCREEN_WINDOWED
: KRE::FullScreenMode::WINDOWED
);
} else if(key == SDLK_F7) {
if(formula_profiler::Manager::get()) {
if(formula_profiler::Manager::get()->is_profiling()) {
Expand Down
95 changes: 6 additions & 89 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,9 @@ namespace
PREF_INT(auto_update_timeout, 5000, "Timeout to use on auto updates (given in milliseconds)");

PREF_BOOL(resizeable, false, "Window is dynamically resizeable.");
PREF_INT(min_window_width, 934, "Minimum window width when auto-determining window size");
PREF_INT(min_window_height, 700, "Minimum window height when auto-determining window size");

PREF_INT(max_window_width, 10240, "Minimum window width when auto-determining window size");
PREF_INT(max_window_height, 7680, "Minimum window height when auto-determining window size");

PREF_BOOL(disable_global_alpha_filter, false, "Disables using alpha-colors.png to denote some special colors as 'alpha colors'");

PREF_INT(auto_size_ideal_width, 0, "");
PREF_INT(auto_size_ideal_height, 0, "");
PREF_BOOL(desktop_fullscreen_force, false, "(Windows) forces desktop fullscreen to actually use fullscreen rather than a borderless window the size of the desktop");
PREF_BOOL(msaa, false, "Use msaa");

Expand Down Expand Up @@ -355,84 +348,6 @@ namespace
// behavior of the module. The `frogatto` module does not use this.
PREF_BOOL(remember_me, true, "Remember me (my gamer account) when connecting to the server");

// Seemingly, this is to select the "next common resolution down" for windowed mode.
// Takes a window, two out params for the best common w/h which will fit in the screen at 2x (?), and "reduce" (?).
void auto_select_resolution(const KRE::WindowPtr& wm, int& width, int& height, bool reduce, bool isFullscreen)
{
auto mode = wm->getDisplaySize();
auto best_mode = mode;
bool found = false;

if(isFullscreen) {
LOG_INFO("RESOLUTION SET TO FULLSCREEN RESOLUTION " << mode.width << "x" << mode.height);

width = mode.width;
height = mode.height;

return;
}

LOG_INFO("TARGET RESOLUTION IS " << mode.width << "x" << mode.height);

const float MinReduction = reduce ? 0.9f : 2.0f;
for(auto& candidate_mode : wm->getWindowModes([](const KRE::WindowMode&){ return true; })) {
if(g_auto_size_ideal_width && g_auto_size_ideal_height) {
if(found && candidate_mode.width < best_mode.width) {
continue;
}

if(candidate_mode.width > mode.width * MinReduction) {
LOG_INFO("REJECTED MODE IS " << candidate_mode.width << "x" << candidate_mode.height
<< "; (width " << candidate_mode.width << " > " << mode.width * MinReduction << ")");
continue;
}

int h = (candidate_mode.width * g_auto_size_ideal_height) / g_auto_size_ideal_width;
if(h > mode.height * MinReduction) {
continue;
}

best_mode = candidate_mode;
best_mode.height = h;
found = true;

LOG_INFO("BETTER MODE IS " << best_mode.width << "x" << best_mode.height);

} else
if( candidate_mode.width < mode.width * MinReduction
&& candidate_mode.height < mode.height * MinReduction
&& ((candidate_mode.width >= best_mode.width
&& candidate_mode.height >= best_mode.height) || !found)
) {
found = true;
LOG_INFO("BETTER MODE IS " << candidate_mode.width << "x" << candidate_mode.height << " vs " << best_mode.width << "x" << best_mode.height);
best_mode = candidate_mode;
} else {
LOG_INFO("REJECTED MODE IS " << candidate_mode.width << "x" << candidate_mode.height);
}
}

if (best_mode.width < g_min_window_width ||
best_mode.height < g_min_window_height) {

best_mode.width = g_min_window_width;
best_mode.height = g_min_window_height;
}

if (best_mode.width > g_max_window_width) {
best_mode.width = g_max_window_width;
}

if (best_mode.height > g_max_window_height) {
best_mode.height = g_max_window_height;
}

LOG_INFO("CHOSEN MODE IS " << best_mode.width << "x" << best_mode.height);

width = best_mode.width;
height = best_mode.height;
}

extern int g_tile_scale;
extern int g_tile_size;

Expand Down Expand Up @@ -1025,15 +940,17 @@ int main(int argcount, char* argvec[])
KRE::WindowPtr main_wnd = wm.allocateWindow(built_hints);
main_wnd->setWindowTitle(module::get_module_pretty_name());

if(!g_desktop_fullscreen && //What the heck is this, where is it defined?
preferences::auto_size_window()
if(
!g_desktop_fullscreen && //Comes from a PREF_BOOL() somewhere.
preferences::auto_size_window()
&& preferences::requested_window_width() == 0
&& preferences::requested_window_height() == 0) {
&& preferences::requested_window_height() == 0
) {
int width = 0;
int height = 0;

bool isFullscreen = preferences::get_screen_mode() != preferences::ScreenMode::WINDOWED;
auto_select_resolution(main_wnd, width, height, true, isFullscreen);
graphics::GameScreen::autoSelectResolution(main_wnd, width, height, true, isFullscreen);

preferences::adjust_virtual_width_to_match_physical(width, height);

Expand Down
5 changes: 5 additions & 0 deletions src/preferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,11 @@ namespace preferences
return g_auto_size_window;
}

void set_auto_size_window(bool enabled)
{
g_auto_size_window = enabled;
}

int requested_window_width()
{
return requested_window_width_;
Expand Down
1 change: 1 addition & 0 deletions src/preferences.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ namespace preferences
extern bool compiling_tiles;

bool auto_size_window();
void set_auto_size_window(bool);
int requested_window_width();
int requested_window_height();
int requested_virtual_window_width();
Expand Down
Loading

0 comments on commit 8d640b9

Please sign in to comment.