Skip to content

Commit 5152ce2

Browse files
committed
Fixed fullscreen toggle/broken savegame
1 parent ab717dc commit 5152ce2

6 files changed

Lines changed: 98 additions & 55 deletions

File tree

src/animator.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <set>
2626
#include <array>
2727
#include "shared/IFile.h"
28+
#include "logger.h"
2829

2930
constexpr uint8_t NO_SPECIAL_ID = 0;
3031
constexpr const size_t SEQ_COUNT = 30;
@@ -135,24 +136,28 @@ bool CAnimator::read(IFile &sfile)
135136
{
136137
if (sfile.read(m_tileMainLayer, sizeof(m_tileMainLayer)) != IFILE_OK)
137138
{
139+
LOGE("failed to read tileMainLayer");
138140
return false;
139141
}
140142
if (sfile.read(m_tileLayer, sizeof(m_tileLayer)) != IFILE_OK)
141143
{
144+
LOGE("failed to read tileLayer");
142145
return false;
143146
}
144-
return false;
147+
return true;
145148
}
146149

147150
bool CAnimator::write(IFile &sfile) const
148151
{
149152
if (sfile.write(m_tileMainLayer, sizeof(m_tileMainLayer)) != IFILE_OK)
150153
{
154+
LOGE("failed to write tileMainLayer");
151155
return false;
152156
}
153157
if (sfile.write(m_tileLayer, sizeof(m_tileLayer)) != IFILE_OK)
154158
{
159+
LOGE("failed to write tileMainLayer");
155160
return false;
156161
}
157-
return false;
162+
return true;
158163
}

src/game.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,7 +1213,10 @@ bool CGame::read(IFile &sfile)
12131213

12141214
// read animation state
12151215
if (!m_animator->read(sfile))
1216+
{
1217+
LOGE("Failed to read animator state");
12161218
return false;
1219+
}
12171220

12181221
return true;
12191222
}

src/gamemixin.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1849,6 +1849,7 @@ void CGameMixin::clearJoyStates()
18491849

18501850
bool CGameMixin::read(IFile &sfile, std::string &name)
18511851
{
1852+
LOGI("Reading savegame");
18521853
auto readfile = [&sfile](auto ptr, auto size)
18531854
{
18541855
return sfile.read(ptr, size) == 1;
@@ -1857,6 +1858,9 @@ bool CGameMixin::read(IFile &sfile, std::string &name)
18571858
{
18581859
return false;
18591860
}
1861+
1862+
LOGI("game reading completed");
1863+
18601864
clearButtonStates();
18611865
clearJoyStates();
18621866
clearKeyStates();

src/runtime.cpp

Lines changed: 82 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -362,13 +362,31 @@ void CRuntime::run()
362362
mainLoop();
363363
}
364364

365+
void CRuntime::handleWindowResize()
366+
{
367+
int w = 0, h = 0;
368+
SDL_GetRenderOutputSize(m_app.renderer, &w, &h);
369+
370+
SDL_Rect vp{0, 0, w, h};
371+
SDL_SetRenderViewport(m_app.renderer, &vp);
372+
373+
SDL_SetRenderLogicalPresentation(
374+
m_app.renderer,
375+
640, // logical width
376+
480, // logical height
377+
SDL_LOGICAL_PRESENTATION_INTEGER_SCALE);
378+
379+
LOGI("Resize handled: render output %dx%d", w, h);
380+
}
381+
365382
/**
366383
* @brief Read input devices for user inputs
367384
*
368385
*/
369386
void CRuntime::doInput()
370387
{
371388
SDL_Event event;
389+
bool skip = false;
372390
while (SDL_PollEvent(&event))
373391
{
374392
// SDL_Window *window = SDL_GetWindowFromID(event.window.windowID);
@@ -447,15 +465,37 @@ void CRuntime::doInput()
447465
LOGI("SDL_EVENT_WINDOW_HIDDEN");
448466
break;
449467

468+
case SDL_EVENT_WINDOW_SAFE_AREA_CHANGED:
469+
LOGI("SDL_EVENT_WINDOW_SAFE_AREA_CHANGED");
470+
skip = true;
471+
/*
472+
SDL_Rect safe;
473+
if (SDL_GetWindowSafeArea(window, &safe))
474+
{
475+
SDL_Log("Safe area changed: x=%d y=%d w=%d h=%d",
476+
safe.x, safe.y, safe.w, safe.h);
477+
}
478+
*/
479+
[[fallthrough]];
480+
481+
case SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED:
482+
if (!skip)
483+
LOGI("SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED");
484+
skip = true;
485+
[[fallthrough]];
486+
450487
case SDL_EVENT_WINDOW_RESIZED:
451-
LOGI("SDL_EVENT_WINDOW_RESIZED");
488+
if (!skip)
489+
LOGI("SDL_EVENT_WINDOW_RESIZED");
490+
452491
if (!m_app.isFullscreen)
453492
{
454493
LOGI("resized %d x %d", getWidth(), getHeight());
455494
SDL_SetWindowSize(m_app.window, event.window.data1, event.window.data2);
456495
}
457496
m_engine->resize(getWidth(), getHeight());
458497
onOrientationChange();
498+
handleWindowResize();
459499
break;
460500

461501
case SDL_EVENT_WINDOW_MINIMIZED:
@@ -471,10 +511,6 @@ void CRuntime::doInput()
471511
onOrientationChange();
472512
break;
473513

474-
case SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED:
475-
LOGI("SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED");
476-
break;
477-
478514
case SDL_EVENT_WINDOW_MOUSE_ENTER:
479515
// LOGI("SDL_EVENT_WINDOW_MOUSE_ENTER");
480516
break;
@@ -495,18 +531,6 @@ void CRuntime::doInput()
495531
LOGI("SDL_EVENT_WINDOW_DISPLAY_SCALE_CHANGED");
496532
break;
497533

498-
case SDL_EVENT_WINDOW_SAFE_AREA_CHANGED:
499-
LOGI("SDL_EVENT_WINDOW_SAFE_AREA_CHANGED");
500-
/*
501-
SDL_Rect safe;
502-
if (SDL_GetWindowSafeArea(window, &safe))
503-
{
504-
SDL_Log("Safe area changed: x=%d y=%d w=%d h=%d",
505-
safe.x, safe.y, safe.w, safe.h);
506-
}
507-
*/
508-
break;
509-
510534
case SDL_EVENT_CLIPBOARD_UPDATE:
511535
LOGI("SDL_EVENT_CLIPBOARD_UPDATE");
512536
break;
@@ -1534,18 +1558,42 @@ void CRuntime::initOptions()
15341558
m_trace = isTrue(m_config["trace"]);
15351559
}
15361560

1561+
#ifdef __EMSCRIPTEN__
1562+
EM_JS(void, fix_canvas_css, (), {
1563+
if (!Module || !Module.canvas)
1564+
return;
1565+
const c = Module.canvas;
1566+
c.style.width = '100vw';
1567+
c.style.height = '100vh';
1568+
c.style.maxWidth = 'none';
1569+
c.style.maxHeight = 'none';
1570+
c.style.display = 'block';
1571+
document.body.style.margin = '0';
1572+
document.body.style.padding = '0';
1573+
document.body.style.overflow = 'hidden';
1574+
});
1575+
#endif
1576+
15371577
/**
15381578
* @brief Function to toggle fullscreen mode on/off
15391579
*
15401580
*/
15411581
void CRuntime::toggleFullscreen()
15421582
{
1583+
LOGI("toggleFullscreen");
15431584
#ifdef __EMSCRIPTEN__
1585+
1586+
fix_canvas_css();
1587+
15441588
if (m_config["webfullscreen"] == "true")
15451589
{
1546-
if (!SDL_SetWindowFullscreen(m_app.window, m_app.isFullscreen))
1590+
if (!SDL_SetRenderLogicalPresentation(
1591+
m_app.renderer,
1592+
640, // logical width
1593+
480, // logical height
1594+
SDL_LOGICAL_PRESENTATION_INTEGER_SCALE))
15471595
{
1548-
LOGE("Fullscreen toggle error: %s", SDL_GetError());
1596+
LOGE("toggleFullscreen >> SDL_SetRenderLogicalPresentation error: %s", SDL_GetError());
15491597
}
15501598
}
15511599
else
@@ -1572,41 +1620,21 @@ void CRuntime::toggleFullscreen()
15721620
emscripten_exit_fullscreen();
15731621
}
15741622

1575-
/*
1576-
if (!m_app.isFullscreen)
1577-
{
1578-
EmscriptenFullscreenStrategy strategy{};
1579-
strategy.scaleMode = EMSCRIPTEN_FULLSCREEN_SCALE_ASPECT;
1580-
strategy.canvasResolutionScaleMode =
1581-
EMSCRIPTEN_FULLSCREEN_CANVAS_SCALE_STDDEF;
1582-
strategy.filteringMode =
1583-
EMSCRIPTEN_FULLSCREEN_FILTERING_NEAREST;
1584-
strategy.canvasResizedCallback = nullptr;
1585-
strategy.canvasResizedCallbackUserData = nullptr;
1586-
1587-
emscripten_enter_soft_fullscreen("#canvas", &strategy);
1588-
}
1589-
else
1590-
{
1591-
emscripten_exit_soft_fullscreen();
1592-
}
1593-
1594-
if (m_app.isFullscreen)
1623+
int w = 0, h = 0;
1624+
for (int i = 0; i < 5 && (w == 0 || h == 0); ++i)
15951625
{
1596-
EmscriptenFullscreenStrategy strategy = {
1597-
.scaleMode = EMSCRIPTEN_FULLSCREEN_SCALE_DEFAULT,
1598-
.canvasResolutionScaleMode = EMSCRIPTEN_FULLSCREEN_CANVAS_SCALE_NONE,
1599-
.filteringMode = EMSCRIPTEN_FULLSCREEN_FILTERING_DEFAULT,
1600-
.canvasResizedCallback = nullptr,
1601-
.canvasResizedCallbackUserData = nullptr};
1602-
emscripten_enter_soft_fullscreen("#canvas", &strategy);
1626+
SDL_GetRenderOutputSize(m_app.renderer, &w, &h);
1627+
SDL_Delay(1);
16031628
}
1604-
else
1605-
{
1606-
emscripten_exit_soft_fullscreen();
1607-
}
1608-
*/
1629+
SDL_Rect vp{0, 0, w, h};
1630+
SDL_SetRenderViewport(m_app.renderer, &vp);
16091631
}
1632+
SDL_SetRenderLogicalPresentation(
1633+
m_app.renderer,
1634+
640, // 640
1635+
480, // 480
1636+
SDL_LOGICAL_PRESENTATION_INTEGER_SCALE);
1637+
16101638
#else
16111639
if (m_app.isFullscreen)
16121640
{
@@ -1912,7 +1940,7 @@ bool CRuntime::fileExists(const std::string &name) const
19121940
const std::string CRuntime::getSavePath() const
19131941
{
19141942
#ifdef __EMSCRIPTEN__
1915-
return SAVEGAME_FILE;
1943+
return m_workspace + SAVEGAME_FILE;
19161944
#else
19171945
return m_workspace + SAVEGAME_FILE;
19181946
#endif
@@ -2627,6 +2655,7 @@ bool CRuntime::isValidSavegame(const std::string &filepath)
26272655

26282656
if (!fileExists(filepath))
26292657
{
2658+
LOGW("savegame `%s` doesn't exists", filepath.c_str());
26302659
return false;
26312660
}
26322661

@@ -2639,6 +2668,7 @@ bool CRuntime::isValidSavegame(const std::string &filepath)
26392668
sfile.close();
26402669
if (!CGame::validateSignature(sig, version))
26412670
{
2671+
LOGW("savegame invalid signature");
26422672
return false;
26432673
}
26442674
return true;

src/runtime.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class CRuntime : public CGameMixin
7979
bool saveToFile(const std::string filepath, const std::string name);
8080
bool loadFromFile(const std::string filepath, std::string &name);
8181
bool isValidSavegame(const std::string &filepath);
82+
void handleWindowResize();
8283

8384
private:
8485
typedef struct

src/template/body.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@
180180

181181
// Hide button if running on itch.io (they provide their own)
182182
if (window.self !== window.top) {
183-
fsBtn.style.display = 'none';
183+
// fsBtn.style.display = 'none';
184184
}
185185

186186
// Fullscreen button click handler

0 commit comments

Comments
 (0)