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

tried to make the engine retain the user selected resolution #416

Merged
merged 1 commit into from
Jan 22, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@
#include "SceneNode.hpp"
#include "WindowManager.hpp"

#include "preference_data.hpp"

#include "xhtml_render_ctx.hpp"

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

preferences::load_preferences();
PreferenceData preference_data = preferences::load_preferences();

if(preference_data.error){
LOG_ERROR("Preference data error: " << preference_data.error_message);
}

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

bool isFullscreen = preferences::get_screen_mode() != preferences::ScreenMode::WINDOWED;
graphics::GameScreen::autoSelectResolution(main_wnd, width, height, true, isFullscreen);
if(!preference_data.error){
bool isFullscreen = preferences::get_screen_mode() != preferences::ScreenMode::WINDOWED;
//graphics::GameScreen::autoSelectResolution(main_wnd, preference_data.resolution_width, preference_data.resolution_height, true, isFullscreen);

preferences::adjust_virtual_width_to_match_physical(width, height);
preferences::adjust_virtual_width_to_match_physical(preference_data.resolution_width, preference_data.resolution_height);

main_wnd->setWindowSize(preference_data.resolution_width, preference_data.resolution_height);
}
else{
bool isFullscreen = preferences::get_screen_mode() != preferences::ScreenMode::WINDOWED;
graphics::GameScreen::autoSelectResolution(main_wnd, width, height, true, isFullscreen);

main_wnd->setWindowSize(width, height);
preferences::adjust_virtual_width_to_match_physical(width, height);

main_wnd->setWindowSize(width, height);
}
}

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

graphics::GameScreen::get().setDimensions(main_wnd->width(), main_wnd->height());
graphics::GameScreen::get().setVirtualDimensions(vw, vh);

std::cout << "Virtual dimensions: " << vw << ", " << vh << std::endl;
std::cout << "Screen virtual dimensions: " << graphics::GameScreen::get().getVirtualWidth() << ", " << graphics::GameScreen::get().getVirtualHeight() << std::endl;
std::cout << "main_wnd->width(): " << main_wnd->width() << ", " << main_wnd->height() << std::endl;

//graphics::GameScreen::get().setDimensions(preference_data.resolution_width, preference_data.resolution_height);
//graphics::GameScreen::get().setVirtualDimensions(preference_data.resolution_width, preference_data.resolution_height);
//main_wnd->setWindowIcon(module::map_file("images/window-icon.png"));

//we prefer late swap tearing so as to minimize frame loss when possible
Expand Down
13 changes: 13 additions & 0 deletions src/preference_data.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef PREFERENCE_DATA
#define PREFERENCE_DATA

#include <string>

struct PreferenceData{
int resolution_width;
int resolution_height;
bool error = false;
std::string error_message = "";
};

#endif
DDR0 marked this conversation as resolved.
Show resolved Hide resolved
15 changes: 12 additions & 3 deletions src/preferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
#include "sys.hpp"
#include "string_utils.hpp"
#include "variant_utils.hpp"
#include "screen_handling.hpp"
#include "preference_data.hpp"

#include <time.h>

Expand Down Expand Up @@ -869,7 +871,7 @@ namespace preferences
return &GameRegistry::getInstance();
}

void load_preferences()
PreferenceData load_preferences()
{
std::string path;
if(preferences_path_.empty()) {
Expand All @@ -894,15 +896,15 @@ namespace preferences
sys::write_file(path + "preferences.cfg", module::get_default_preferences().write_json());
node = module::get_default_preferences();
} else {
return;
return PreferenceData{1600,800,true,"Default preferences.cfg does not exist"};
}
}

if(node.is_null()) {
try {
node = json::parse_from_file(path + "preferences.cfg");
} catch(const json::ParseError&) {
return;
return PreferenceData{1600,800,true,"Could not parse preferences.cfg"};
}
}

Expand Down Expand Up @@ -963,6 +965,8 @@ namespace preferences
}

preferences::set_32bpp_textures_if_kb_memory_at_least(512000);

return PreferenceData{node["resolution_width"].as_int(), node["resolution_height"].as_int()};
}

void save_preferences()
Expand Down Expand Up @@ -1010,6 +1014,11 @@ namespace preferences
}
}

node.add("resolution_width", variant(graphics::GameScreen::get().getWidth()));
node.add("resolution_height", variant(graphics::GameScreen::get().getHeight()));

std::cout << "save_preferences() :" << graphics::GameScreen::get().getWidth() << ", " << graphics::GameScreen::get().getHeight() << std::endl;

LOG_INFO("WRITE PREFS: " << (preferences_path_ + "preferences.cfg"));
sys::write_file(preferences_path_ + "preferences.cfg", node.build().write_json());
}
Expand Down
3 changes: 2 additions & 1 deletion src/preferences.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include "uri.hpp"
#include "variant.hpp"
#include "preference_data.hpp"

namespace game_logic
{
Expand Down Expand Up @@ -204,7 +205,7 @@ namespace preferences

game_logic::FormulaCallable* registry();

void load_preferences();
PreferenceData load_preferences();
void save_preferences();

uri::uri get_tbs_uri();
Expand Down
Loading