Skip to content

Commit

Permalink
Make window size and position persistent
Browse files Browse the repository at this point in the history
  • Loading branch information
Antidote committed Oct 27, 2023
1 parent c0e405c commit a6392ab
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 4 deletions.
19 changes: 19 additions & 0 deletions Runtime/CMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,19 @@ struct Application {
}

void onAppWindowResized(const AuroraWindowSize& size) noexcept {
if (size.width != m_cvarCommons.getWindowSize().x || size.height != m_cvarCommons.getWindowSize().y) {
m_cvarCommons.m_windowSize->fromVec2i(zeus::CVector2i(size.width, size.height));
}

CGraphics::SetViewportResolution({static_cast<s32>(size.fb_width), static_cast<s32>(size.fb_height)});
}

void onAppWindowMoved(const AuroraWindowPos& pos) {
if (pos.x > 0 && pos.y > 0 && (pos.x != m_cvarCommons.getWindowPos().x || pos.y != m_cvarCommons.getWindowPos().y)) {
m_cvarCommons.m_windowPos->fromVec2i(zeus::CVector2i(pos.x, pos.y));
}
}

void onAppDisplayScaleChanged(float scale) noexcept { ImGuiEngine_Initialize(scale); }

void onControllerAdded(uint32_t which) noexcept { m_imGuiConsole.ControllerAdded(which); }
Expand Down Expand Up @@ -519,13 +529,19 @@ int main(int argc, char** argv) {

g_app = std::make_unique<metaforce::Application>(argc, argv, fileMgr, cvarMgr, cvarCmns);
std::string configPath{fileMgr.getStoreRoot()};


const AuroraConfig config{
.appName = "Metaforce",
.configPath = configPath.c_str(),
.desiredBackend = metaforce::backend_from_string(cvarCmns.getGraphicsApi()),
.msaa = cvarCmns.getSamples(),
.maxTextureAnisotropy = static_cast<uint16_t>(cvarCmns.getAnisotropy()),
.startFullscreen = cvarCmns.getFullscreen(),
.windowPosX = cvarCmns.getWindowPos().x,
.windowPosY = cvarCmns.getWindowPos().y,
.windowWidth = static_cast<uint>(cvarCmns.getWindowSize().x < 0 ? 0 : cvarCmns.getWindowSize().x),
.windowHeight = static_cast<uint>(cvarCmns.getWindowSize().y < 0 ? 0 : cvarCmns.getWindowSize().y),
.iconRGBA8 = icon.data.get(),
.iconWidth = icon.width,
.iconHeight = icon.height,
Expand All @@ -551,6 +567,9 @@ int main(int argc, char** argv) {
case AURORA_WINDOW_RESIZED:
g_app->onAppWindowResized(event->windowSize);
break;
case AURORA_WINDOW_MOVED:
g_app->onAppWindowMoved(event->windowPos);
break;
case AURORA_CONTROLLER_ADDED:
g_app->onControllerAdded(event->controller);
break;
Expand Down
48 changes: 48 additions & 0 deletions Runtime/ConsoleVariables/CVar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ CVar::CVar(std::string_view name, std::string_view value, std::string_view help,
init(flags);
}

CVar::CVar(std::string_view name, const zeus::CVector2i& value, std::string_view help, EFlags flags)
: CVar(name, help, EType::Vec2i) {
fromVec2i(value);
init(flags);
}

CVar::CVar(std::string_view name, const zeus::CVector2f& value, std::string_view help, EFlags flags)
: CVar(name, help, EType::Vec2f) {
fromVec2f(value);
Expand Down Expand Up @@ -84,6 +90,23 @@ std::string CVar::help() const {
return m_help + (m_defaultValue.empty() ? "" : "\ndefault: " + m_defaultValue) + (isReadOnly() ? " [ReadOnly]" : "");
}

zeus::CVector2i CVar::toVec2i(bool* isValid) const {
if (m_type != EType::Vec2i) {
if (isValid != nullptr)
*isValid = false;

return {};
}

if (isValid != nullptr)
*isValid = true;

std::array<int, 2> f;
std::sscanf(m_value.c_str(), "%i %i", &f[0], &f[1]);
return {f[0], f[1]};
}


zeus::CVector2f CVar::toVec2f(bool* isValid) const {
if (m_type != EType::Vec2f) {
if (isValid != nullptr)
Expand Down Expand Up @@ -242,6 +265,15 @@ std::string CVar::toLiteral(bool* isValid) const {
return m_value;
}

bool CVar::fromVec2i(const zeus::CVector2i& val) {
if (!safeToModify(EType::Vec2i))
return false;

m_value.assign(fmt::format(FMT_STRING("{} {}"), val.x, val.y));
m_flags |= EFlags::Modified;
return true;
}

bool CVar::fromVec2f(const zeus::CVector2f& val) {
if (!safeToModify(EType::Vec2f))
return false;
Expand Down Expand Up @@ -433,6 +465,20 @@ void CVar::dispatch() {
}
}

bool isInt(std::string_view v) {
char* p;
std::strtol(v.data(), &p, 10);
return p != nullptr && *p == 0;
}

bool isInt(const std::vector<std::string>& v) {
for (auto& s : v) {
if (!isInt(s))
return false;
}
return true;
}

bool isReal(std::string_view v) {
char* p;
std::strtod(v.data(), &p);
Expand Down Expand Up @@ -468,6 +514,8 @@ bool CVar::isValidInput(std::string_view input) const {
}
case EType::Literal:
return true;
case EType::Vec2i:
return parts.size() == 2 && isInt(parts);
case EType::Vec2f:
case EType::Vec2d:
return parts.size() == 2 && isReal(parts);
Expand Down
6 changes: 5 additions & 1 deletion Runtime/ConsoleVariables/CVar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <vector>
namespace metaforce {
namespace StoreCVar {
enum class EType : uint32_t { Boolean, Signed, Unsigned, Real, Literal, Vec2f, Vec2d, Vec3f, Vec3d, Vec4f, Vec4d };
enum class EType : uint32_t { Boolean, Signed, Unsigned, Real, Literal, Vec2i, Vec2f, Vec2d, Vec3f, Vec3d, Vec4f, Vec4d };
enum class EFlags {
None = 0,
System = (1 << 0),
Expand Down Expand Up @@ -53,6 +53,7 @@ class CVar : protected StoreCVar::CVar {
using EFlags = StoreCVar::EFlags;

CVar(std::string_view name, std::string_view value, std::string_view help, EFlags flags);
CVar(std::string_view name, const zeus::CVector2i& value, std::string_view help, EFlags flags);
CVar(std::string_view name, const zeus::CVector2f& value, std::string_view help, EFlags flags);
CVar(std::string_view name, const zeus::CVector2d& value, std::string_view help, EFlags flags);
CVar(std::string_view name, const zeus::CVector3f& value, std::string_view help, EFlags flags);
Expand All @@ -72,6 +73,7 @@ class CVar : protected StoreCVar::CVar {

template <typename T>
inline bool toValue(T& value) const;
zeus::CVector2i toVec2i(bool* isValid = nullptr) const;
zeus::CVector2f toVec2f(bool* isValid = nullptr) const;
zeus::CVector2d toVec2d(bool* isValid = nullptr) const;
zeus::CVector3f toVec3f(bool* isValid = nullptr) const;
Expand All @@ -88,6 +90,7 @@ class CVar : protected StoreCVar::CVar {
inline bool fromValue(T value) {
return false;
}
bool fromVec2i(const zeus::CVector2i& val);
bool fromVec2f(const zeus::CVector2f& val);
bool fromVec2d(const zeus::CVector2d& val);
bool fromVec3f(const zeus::CVector3f& val);
Expand All @@ -101,6 +104,7 @@ class CVar : protected StoreCVar::CVar {
bool fromLiteral(std::string_view val);
bool fromLiteralToType(std::string_view val);

bool isVec2i() const { return m_type == EType::Vec2i; }
bool isVec2f() const { return m_type == EType::Vec2f; }
bool isVec2d() const { return m_type == EType::Vec2d; }
bool isVec3f() const { return m_type == EType::Vec3f; }
Expand Down
8 changes: 6 additions & 2 deletions Runtime/ConsoleVariables/CVarCommons.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ CVarCommons::CVarCommons(CVarManager& manager) : m_mgr(manager) {
CVar::EFlags::System | CVar::EFlags::Archive | CVar::EFlags::ModifyRestart);
m_variableDt = m_mgr.findOrMakeCVar("variableDt", "Enable variable delta time (experimental)", false,
(CVar::EFlags::System | CVar::EFlags::Archive | CVar::EFlags::ModifyRestart));
m_windowSize = m_mgr.findOrMakeCVar("windowSize", "Stores the last known window size", zeus::CVector2i(1280, 960),
(CVar::EFlags::System | CVar::EFlags::Archive));
m_windowPos = m_mgr.findOrMakeCVar("windowPos", "Stores the last known window position", zeus::CVector2i(-1, -1),
(CVar::EFlags::System | CVar::EFlags::Archive));

m_debugOverlayPlayerInfo = m_mgr.findOrMakeCVar(
"debugOverlay.playerInfo"sv, "Displays information about the player, such as location and orientation"sv, false,
Expand Down Expand Up @@ -58,8 +62,8 @@ CVarCommons::CVarCommons(CVarManager& manager) : m_mgr(manager) {
m_mgr.findOrMakeCVar("debugOverlay.drawCallInfo"sv, "Displays the current number of draw calls per frame"sv,
false, CVar::EFlags::Game | CVar::EFlags::Archive | CVar::EFlags::ReadOnly);
m_debugOverlayBufferInfo =
m_mgr.findOrMakeCVar("debugOverlay.bufferInfo"sv, "Displays the current buffer memory usage per frame"sv,
false, CVar::EFlags::Game | CVar::EFlags::Archive | CVar::EFlags::ReadOnly);
m_mgr.findOrMakeCVar("debugOverlay.bufferInfo"sv, "Displays the current buffer memory usage per frame"sv, false,
CVar::EFlags::Game | CVar::EFlags::Archive | CVar::EFlags::ReadOnly);
m_debugOverlayShowInput = m_mgr.findOrMakeCVar("debugOverlay.showInput"sv, "Displays controller input"sv, false,
CVar::EFlags::Game | CVar::EFlags::Archive | CVar::EFlags::ReadOnly);
m_debugOverlayCorner =
Expand Down
4 changes: 4 additions & 0 deletions Runtime/ConsoleVariables/CVarCommons.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ struct CVarCommons {
CVar* m_texAnisotropy = nullptr;
CVar* m_deepColor = nullptr;
CVar* m_variableDt = nullptr;
CVar* m_windowSize = nullptr;
CVar* m_windowPos = nullptr;

CVar* m_debugOverlayPlayerInfo = nullptr;
CVar* m_debugOverlayWorldInfo = nullptr;
Expand Down Expand Up @@ -68,6 +70,8 @@ struct CVarCommons {
void setSamples(uint32_t v) { m_drawSamples->fromInteger(std::max(uint32_t(1), v)); }

uint32_t getAnisotropy() const { return std::max(1u, uint32_t(m_texAnisotropy->toUnsigned())); }
zeus::CVector2i getWindowSize() const { return m_windowSize->toVec2i(); }
zeus::CVector2i getWindowPos() const { return m_windowPos->toVec2i(); }

void setAnisotropy(uint32_t v) { m_texAnisotropy->fromInteger(std::max(1u, v)); }

Expand Down
4 changes: 4 additions & 0 deletions Runtime/ConsoleVariables/CVarManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ class CVarManager final {
CVarManager(FileStoreManager& store, bool useBinary = false);
~CVarManager();

CVar* newCVar(std::string_view name, std::string_view help, const zeus::CVector2i& value, CVar::EFlags flags) {
return _newCVar<const zeus::CVector2i>(name, help, value, flags);
}

CVar* newCVar(std::string_view name, std::string_view help, const zeus::CVector2f& value, CVar::EFlags flags) {
return _newCVar<const zeus::CVector2f>(name, help, value, flags);
}
Expand Down
2 changes: 1 addition & 1 deletion extern/aurora

0 comments on commit a6392ab

Please sign in to comment.