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

Fullscreen fix #367

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
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: 0 additions & 6 deletions src/kre/WindowManager.hpp
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
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
46 changes: 6 additions & 40 deletions src/level_runner.cpp
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 @@ -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
87 changes: 1 addition & 86 deletions src/main.cpp
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 @@ -1033,7 +948,7 @@ int main(int argcount, char* argvec[])
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
134 changes: 134 additions & 0 deletions src/screen_handling.cpp
Expand Up @@ -27,9 +27,19 @@

#include "asserts.hpp"
#include "screen_handling.hpp"
#include "preferences.hpp"

extern bool g_desktop_fullscreen; //test

namespace graphics
{
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_INT(auto_size_ideal_width, 0, "");
PREF_INT(auto_size_ideal_height, 0, "");

GameScreen::GameScreen()
: width_(0),
height_(0),
Expand Down Expand Up @@ -78,6 +88,128 @@ namespace graphics
cam_ = std::make_shared<KRE::Camera>("gs.cam", x_, virtual_width_, y_, virtual_height_);
}

//should use the fullscreen mode from preferences.hpp
void GameScreen::setFullscreen(KRE::FullScreenMode mode) {
auto wnd = KRE::WindowManager::getMainWindow();
auto& gs = graphics::GameScreen::get();

if(mode == KRE::FullScreenMode::FULLSCREEN_WINDOWED)
{
LOG_DEBUG("Entering 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("Entering 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 {
GameScreen::autoSelectResolution(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());
}
}
}


// 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 GameScreen::autoSelectResolution(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;
}

void GameScreen::setupForDraw(KRE::WindowPtr wnd)
{
last_cam_ = KRE::DisplayDevice::getCurrent()->setDefaultCamera(cam_);
Expand All @@ -102,4 +234,6 @@ namespace graphics
{
GameScreen::get().cleanupAfterDraw(wnd_);
}

extern bool g_desktop_fullscreen;
}
2 changes: 2 additions & 0 deletions src/screen_handling.hpp
Expand Up @@ -51,6 +51,8 @@ namespace graphics
void setLocation(int x, int y);
void setDimensions(int width, int height);
void setVirtualDimensions(int vwidth, int vheight);
void setFullscreen(KRE::FullScreenMode mode);
static void autoSelectResolution(KRE::WindowPtr wm, int& width, int& height, bool reduce, bool isFullscreen);

KRE::CameraPtr getCurrentCamera() const { return cam_; }

Expand Down
6 changes: 0 additions & 6 deletions src/utils.cpp
Expand Up @@ -50,12 +50,6 @@ void write_autosave()
sys::write_file(std::string(preferences::auto_save_file_path()) + ".stat", "1");
}

void toggle_fullscreen()
{
auto wnd = KRE::WindowManager::getMainWindow();
wnd->setFullscreenMode(wnd->fullscreenMode() == KRE::FullScreenMode::WINDOWED ? KRE::FullScreenMode::FULLSCREEN_WINDOWED : KRE::FullScreenMode::WINDOWED);
}

std::string get_http_datetime()
{
time_t rawtime;
Expand Down
1 change: 0 additions & 1 deletion src/utils.hpp
Expand Up @@ -32,7 +32,6 @@
std::string get_http_datetime();
int truncate_to_char(int value);
void write_autosave();
void toggle_fullscreen();

#if defined(_MSC_VER)
int gettimeofday(struct timeval *tv, struct timezone2 *tz);
Expand Down