mirror of
https://github.com/zhenyan121/SporeBG-Conid.git
synced 2026-04-10 06:14:08 +08:00
Load config using JSON
This commit is contained in:
@@ -9,11 +9,11 @@ struct WindowConfig {
|
||||
640x360 逻辑分辨率(窗口大小的1/2)
|
||||
画面更高清晰
|
||||
*/
|
||||
int logicalWidth = 640;
|
||||
int logicalHeight = 360;
|
||||
std::string windowTitle = "孢子棋";
|
||||
int width = 320 * 4; // 初始窗口宽度(像素)
|
||||
int height = 180 * 4; // 初始窗口高度(像素)
|
||||
std::string title = "孢子棋";
|
||||
bool vsync = true;
|
||||
int scale = 2;
|
||||
|
||||
|
||||
|
||||
/*
|
||||
@@ -24,6 +24,12 @@ struct WindowConfig {
|
||||
*/
|
||||
} ;
|
||||
|
||||
struct RenderConfig {
|
||||
int logicalWidth = 640;
|
||||
int logicalHeight = 360;
|
||||
int scale = 2;
|
||||
};
|
||||
|
||||
// 获取棋盘渲染区域信息(用于坐标转换)
|
||||
struct BoardArea {
|
||||
int x, y; // 左上角像素坐标
|
||||
@@ -42,8 +48,7 @@ namespace UI
|
||||
constexpr int FontHeight = 8;
|
||||
constexpr int LogicalWidth = 640;
|
||||
constexpr int LogicalHeight = 360;
|
||||
constexpr int StartWindowWidth = 320 * 4; // 初始窗口宽度(像素)
|
||||
constexpr int StartWindowHeight = 180 * 4; // 初始窗口高度(像素)
|
||||
|
||||
// 字体大小(逻辑像素)
|
||||
constexpr int DIALOG_FONT_SIZE = 14;
|
||||
constexpr int UI_SMALL_FONT_SIZE = 8;
|
||||
@@ -73,4 +78,9 @@ struct Viewport {
|
||||
// 逻辑画面被放大后,在窗口中的位置和大小
|
||||
// 用于 SDL_RenderTexture / 输入坐标转换
|
||||
SDL_FRect dst{};
|
||||
};
|
||||
|
||||
struct Config {
|
||||
WindowConfig window;
|
||||
RenderConfig render;
|
||||
};
|
||||
59
src/utils/ConfigLoader.cpp
Normal file
59
src/utils/ConfigLoader.cpp
Normal file
@@ -0,0 +1,59 @@
|
||||
#include "ConfigLoader.h"
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
using json = nlohmann::json;
|
||||
|
||||
|
||||
static void loadWindow(const json& j, WindowConfig& window) {
|
||||
auto get = [&](const char* key, auto& out) {
|
||||
if (j.contains(key))
|
||||
j.at(key).get_to(out);
|
||||
else {
|
||||
std::cout << "Unkonw key " << key << "\n";
|
||||
}
|
||||
};
|
||||
get("width", window.width);
|
||||
get("height", window.height);
|
||||
get("title", window.title);
|
||||
}
|
||||
|
||||
static void loadRender(const json& j, RenderConfig& render) {
|
||||
auto get = [&](const char* key, auto& out) {
|
||||
if (j.contains(key))
|
||||
j.at(key).get_to(out);
|
||||
else {
|
||||
std::cout << "Unkonw key " << key << "\n";
|
||||
}
|
||||
};
|
||||
get("logical_width", render.logicalWidth);
|
||||
get("logical_height", render.logicalHeight);
|
||||
}
|
||||
|
||||
bool ConfigLoader::load(const std::string& path, Config& config) {
|
||||
std::ifstream file(path);
|
||||
|
||||
if (!file.is_open()) {
|
||||
std::cerr << "[Config] Cannot open " << path << "\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
json j;
|
||||
|
||||
try {
|
||||
file >> j;
|
||||
}
|
||||
catch (const json::parse_error& e) {
|
||||
std::cerr << "[Config] JSON parse error: " << e.what() << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (j.contains("window")) {
|
||||
loadWindow(j["window"], config.window);
|
||||
}
|
||||
if (j.contains("render")) {
|
||||
loadRender(j["render"], config.render);
|
||||
}
|
||||
std::cout << "load json success!\n";
|
||||
return true;
|
||||
|
||||
}
|
||||
11
src/utils/ConfigLoader.h
Normal file
11
src/utils/ConfigLoader.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include "Config.h"
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
class ConfigLoader {
|
||||
public:
|
||||
static bool load(const std::string& path, Config& config);
|
||||
|
||||
|
||||
};
|
||||
Reference in New Issue
Block a user