Fixing minor stuff, working on Web version

This commit is contained in:
Vladislav Khorev committed 2026-07-25 16:56:02 +03:00
1 parent 5ecf5fb9d7
commit 7df68febca
38 files changed
+502 -167

No files matched your search

+80 -59
View File
@@ -20,15 +20,48 @@
FRG::Game* g_game = nullptr;
// Read the persisted fullscreen preference (if any) before creating the
// window, so it opens directly in the right mode instead of flashing
// windowed -> fullscreen once settings are loaded later during setupPart2().
void readSettings()
{
const std::string settingsContent = FRG::readSavedTextFile("settings.json");
if (!settingsContent.empty()) {
try {
const nlohmann::json settingsRoot = nlohmann::json::parse(settingsContent);
if (settingsRoot.contains("fullscreen")) {
FRG::Environment::isFullscreen = settingsRoot["fullscreen"].get<bool>();
}
if (settingsRoot.contains("isHighDPIEnabled")) {
FRG::Environment::isHighDPIEnabled = settingsRoot["isHighDPIEnabled"].get<bool>();
}
if (settingsRoot.contains("enableLogging")) {
FRG::Environment::enableLogging = settingsRoot["enableLogging"].get<bool>();
}
}
catch (const std::exception& e) {
std::cerr << "[settings] Failed to parse settings.json: " << e.what() << std::endl;
}
}
}
#if defined(EMSCRIPTEN)
static SDL_GLContext glContext_x;
static bool g_isFsLoaded = false; // Флаг готовности
static bool g_pendingFullscreenResize = false;
// Макрос EMSCRIPTEN_KEEPALIVE экспортирует функцию в глобальный объект Module
extern "C" void EMSCRIPTEN_KEEPALIVE onFileSystemLoaded() {
// 1. Файлы загружены. Теперь можно читать настройки и логи
// Если функции readSettings() нет для Emscripten, форсируем логгер:
FRG::Environment::enableLogging = true;
//FRG::Environment::enableLogging = true;
//readSettings();
FRG::initLogger();
FRG::logger() << "[boot] File system is ready. Setting up game..." << std::endl;
@@ -40,26 +73,7 @@ extern "C" void EMSCRIPTEN_KEEPALIVE onFileSystemLoaded() {
// 3. Даем отмашку главному циклу
g_isFsLoaded = true;
}
#endif
void MainLoop() {
#if defined(EMSCRIPTEN)
// Крутим цикл вхолостую, пока JS читает IndexedDB
if (!g_isFsLoaded) return;
#endif
g_game->update();
}
#ifdef EMSCRIPTEN
EM_BOOL onWebGLContextLost(int /*eventType*/, const void* /*reserved*/, void* /*userData*/) {
return EM_TRUE;
}
EM_BOOL onWebGLContextRestored(int /*eventType*/, const void* /*reserved*/, void* /*userData*/) {
return EM_TRUE;
}
static void applyResize(int logicalW, int logicalH) {
// Получаем коэффициент плотности пикселей (например, 2.625 на Pixel или 3.0 на Samsung)
@@ -92,22 +106,59 @@ static void applyResize(int logicalW, int logicalH) {
<< " (physical: " << physicalW << "x" << physicalH
<< ", DPR: " << dpr << ")" << std::endl;
}
#endif
void MainLoop() {
#if defined(EMSCRIPTEN)
// Крутим цикл вхолостую, пока JS читает IndexedDB
if (!g_isFsLoaded) return;
if (g_pendingFullscreenResize) {
g_pendingFullscreenResize = false;
// Теперь DOM перестроен, можно забирать актуальные размеры.
// Если canvas должен занимать все окно браузера:
int canvasW = EM_ASM_INT({ return window.innerWidth; });
int canvasH = EM_ASM_INT({ return window.innerHeight; });
applyResize(canvasW, canvasH);
}
#endif
g_game->update();
}
#ifdef EMSCRIPTEN
EM_BOOL onWebGLContextLost(int /*eventType*/, const void* /*reserved*/, void* /*userData*/) {
return EM_TRUE;
}
EM_BOOL onWebGLContextRestored(int /*eventType*/, const void* /*reserved*/, void* /*userData*/) {
return EM_TRUE;
}
EM_BOOL onWindowResized(int /*eventType*/, const EmscriptenUiEvent* e, void* /*userData*/) {
// Use the event's window dimensions — querying the canvas element would
// return its old fixed size (e.g. 1280x720) before it has been resized.
applyResize(e->windowInnerWidth, e->windowInnerHeight);
double w, h;
emscripten_get_element_css_size("window", &w, &h);
// Если "window" не поддерживается вашей версией SDK, используем данные из e:
// int logicalW = e->windowInnerWidth;
// int logicalH = e->windowInnerHeight;
applyResize(static_cast<int>(w), static_cast<int>(h));
return EM_FALSE;
}
EM_BOOL onFullscreenChanged(int /*eventType*/, const EmscriptenFullscreenChangeEvent* e, void* /*userData*/) {
FRG::Environment::isFullscreen = e->isFullscreen;
FRG::logger() << "onFullscreenChanged: isFullscreen = " << e->isFullscreen << std::endl;
// Браузер еще не обновил DOM. Ставим флаг, чтобы применить размеры в следующем кадре.
g_pendingFullscreenResize = true;
// Вместо window.innerWidth, попробуйте запросить размер целевого элемента
// так как после перехода в FS именно он растягивается на весь экран.
double clientW, clientH;
emscripten_get_element_css_size("#canvas", &clientW, &clientH);
applyResize(clientW, clientH);
return EM_FALSE;
}
@@ -128,7 +179,7 @@ int main(int argc, char* argv[]) {
emscripten_set_webglcontextrestored_callback("#canvas", nullptr, EM_TRUE, onWebGLContextRestored);
emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, nullptr, EM_FALSE, onWindowResized);
emscripten_set_fullscreenchange_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, nullptr, EM_FALSE, onFullscreenChanged);
emscripten_set_fullscreenchange_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, nullptr, EM_TRUE, onFullscreenChanged);
int canvasW = EM_ASM_INT({ return window.innerWidth; });
int canvasH = EM_ASM_INT({ return window.innerHeight; });
@@ -289,36 +340,6 @@ int main(int argc, char* argv[]) {
#ifndef __ANDROID__
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__linux__)
// Read the persisted fullscreen preference (if any) before creating the
// window, so it opens directly in the right mode instead of flashing
// windowed -> fullscreen once settings are loaded later during setupPart2().
void readSettings()
{
const std::string settingsContent = FRG::readSavedTextFile("settings.json");
if (!settingsContent.empty()) {
try {
const nlohmann::json settingsRoot = nlohmann::json::parse(settingsContent);
if (settingsRoot.contains("fullscreen")) {
FRG::Environment::isFullscreen = settingsRoot["fullscreen"].get<bool>();
}
if (settingsRoot.contains("isHighDPIEnabled")) {
FRG::Environment::isHighDPIEnabled = settingsRoot["isHighDPIEnabled"].get<bool>();
}
if (settingsRoot.contains("enableLogging")) {
FRG::Environment::enableLogging = settingsRoot["enableLogging"].get<bool>();
}
}
catch (const std::exception& e) {
std::cerr << "[settings] Failed to parse settings.json: " << e.what() << std::endl;
}
}
}
int main(int argc, char* argv[]) {
SDL_GLContext ctx = nullptr;