Skip to content

Commit 64ff944

Browse files
tried to make the engine retain the user selected resolution (#416)
I changed 3 files and created preference_data.hpp. You should be weary of my lack of knowledge about how Frogatto treats it's virtual gamescreen sizes. Basically I added an if statement that separates presence and absence of a preferences.cfg. If the file exists, the autoSelectResolution function is not called, because why should the resolution be set automatically when user defined settings exist? Please notify me if my changes require further explanation. Also here's a video showing off the changes. https://youtu.be/Wueo7ppIjr0
1 parent ae23d3c commit 64ff944

File tree

4 files changed

+55
-9
lines changed

4 files changed

+55
-9
lines changed

src/main.cpp

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@
9494
#include "SceneNode.hpp"
9595
#include "WindowManager.hpp"
9696

97+
#include "preference_data.hpp"
98+
9799
#include "xhtml_render_ctx.hpp"
98100

99101
#if defined(__APPLE__)
@@ -531,7 +533,11 @@ int main(int argcount, char* argvec[])
531533
module::set_core_module_name(DEFAULT_MODULE);
532534
}
533535

534-
preferences::load_preferences();
536+
PreferenceData preference_data = preferences::load_preferences();
537+
538+
if(preference_data.error){
539+
LOG_ERROR("Preference data error: " << preference_data.error_message);
540+
}
535541

536542
// load difficulty settings after module, before rest of args.
537543
try {
@@ -944,12 +950,22 @@ int main(int argcount, char* argvec[])
944950
int width = 0;
945951
int height = 0;
946952

947-
bool isFullscreen = preferences::get_screen_mode() != preferences::ScreenMode::WINDOWED;
948-
graphics::GameScreen::autoSelectResolution(main_wnd, width, height, true, isFullscreen);
953+
if(!preference_data.error){
954+
bool isFullscreen = preferences::get_screen_mode() != preferences::ScreenMode::WINDOWED;
955+
//graphics::GameScreen::autoSelectResolution(main_wnd, preference_data.resolution_width, preference_data.resolution_height, true, isFullscreen);
949956

950-
preferences::adjust_virtual_width_to_match_physical(width, height);
957+
preferences::adjust_virtual_width_to_match_physical(preference_data.resolution_width, preference_data.resolution_height);
958+
959+
main_wnd->setWindowSize(preference_data.resolution_width, preference_data.resolution_height);
960+
}
961+
else{
962+
bool isFullscreen = preferences::get_screen_mode() != preferences::ScreenMode::WINDOWED;
963+
graphics::GameScreen::autoSelectResolution(main_wnd, width, height, true, isFullscreen);
951964

952-
main_wnd->setWindowSize(width, height);
965+
preferences::adjust_virtual_width_to_match_physical(width, height);
966+
967+
main_wnd->setWindowSize(width, height);
968+
}
953969
}
954970

955971
int vw = preferences::requested_virtual_window_width() > 0
@@ -968,6 +984,13 @@ int main(int argcount, char* argvec[])
968984

969985
graphics::GameScreen::get().setDimensions(main_wnd->width(), main_wnd->height());
970986
graphics::GameScreen::get().setVirtualDimensions(vw, vh);
987+
988+
std::cout << "Virtual dimensions: " << vw << ", " << vh << std::endl;
989+
std::cout << "Screen virtual dimensions: " << graphics::GameScreen::get().getVirtualWidth() << ", " << graphics::GameScreen::get().getVirtualHeight() << std::endl;
990+
std::cout << "main_wnd->width(): " << main_wnd->width() << ", " << main_wnd->height() << std::endl;
991+
992+
//graphics::GameScreen::get().setDimensions(preference_data.resolution_width, preference_data.resolution_height);
993+
//graphics::GameScreen::get().setVirtualDimensions(preference_data.resolution_width, preference_data.resolution_height);
971994
//main_wnd->setWindowIcon(module::map_file("images/window-icon.png"));
972995

973996
//we prefer late swap tearing so as to minimize frame loss when possible

src/preference_data.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef PREFERENCE_DATA
2+
#define PREFERENCE_DATA
3+
4+
#include <string>
5+
6+
struct PreferenceData{
7+
int resolution_width;
8+
int resolution_height;
9+
bool error = false;
10+
std::string error_message = "";
11+
};
12+
13+
#endif

src/preferences.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
#include "sys.hpp"
5555
#include "string_utils.hpp"
5656
#include "variant_utils.hpp"
57+
#include "screen_handling.hpp"
58+
#include "preference_data.hpp"
5759

5860
#include <time.h>
5961

@@ -869,7 +871,7 @@ namespace preferences
869871
return &GameRegistry::getInstance();
870872
}
871873

872-
void load_preferences()
874+
PreferenceData load_preferences()
873875
{
874876
std::string path;
875877
if(preferences_path_.empty()) {
@@ -894,15 +896,15 @@ namespace preferences
894896
sys::write_file(path + "preferences.cfg", module::get_default_preferences().write_json());
895897
node = module::get_default_preferences();
896898
} else {
897-
return;
899+
return PreferenceData{1600,800,true,"Default preferences.cfg does not exist"};
898900
}
899901
}
900902

901903
if(node.is_null()) {
902904
try {
903905
node = json::parse_from_file(path + "preferences.cfg");
904906
} catch(const json::ParseError&) {
905-
return;
907+
return PreferenceData{1600,800,true,"Could not parse preferences.cfg"};
906908
}
907909
}
908910

@@ -963,6 +965,8 @@ namespace preferences
963965
}
964966

965967
preferences::set_32bpp_textures_if_kb_memory_at_least(512000);
968+
969+
return PreferenceData{node["resolution_width"].as_int(), node["resolution_height"].as_int()};
966970
}
967971

968972
void save_preferences()
@@ -1010,6 +1014,11 @@ namespace preferences
10101014
}
10111015
}
10121016

1017+
node.add("resolution_width", variant(graphics::GameScreen::get().getWidth()));
1018+
node.add("resolution_height", variant(graphics::GameScreen::get().getHeight()));
1019+
1020+
std::cout << "save_preferences() :" << graphics::GameScreen::get().getWidth() << ", " << graphics::GameScreen::get().getHeight() << std::endl;
1021+
10131022
LOG_INFO("WRITE PREFS: " << (preferences_path_ + "preferences.cfg"));
10141023
sys::write_file(preferences_path_ + "preferences.cfg", node.build().write_json());
10151024
}

src/preferences.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include "uri.hpp"
3030
#include "variant.hpp"
31+
#include "preference_data.hpp"
3132

3233
namespace game_logic
3334
{
@@ -204,7 +205,7 @@ namespace preferences
204205

205206
game_logic::FormulaCallable* registry();
206207

207-
void load_preferences();
208+
PreferenceData load_preferences();
208209
void save_preferences();
209210

210211
uri::uri get_tbs_uri();

0 commit comments

Comments
 (0)