Skip to content

Commit

Permalink
Implement overlay support on Emscripten
Browse files Browse the repository at this point in the history
  • Loading branch information
felipemanga committed Dec 31, 2023
1 parent 27e7fcf commit 5a2389d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
20 changes: 18 additions & 2 deletions src/she/sdl2/sdl2_display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ namespace she {
if (m_window) {
sdl::windowIdToDisplay.erase(SDL_GetWindowID(m_window));
m_surface->dispose();
if (m_doublebuffer)
m_doublebuffer->dispose();
if (m_renderer)
SDL_DestroyRenderer(m_renderer);
SDL_DestroyWindow(m_window);
Expand Down Expand Up @@ -182,6 +184,15 @@ namespace she {
m_dirty = true;
m_surface = newSurface;
she::sdl::screen = newSurface;

#ifdef EMSCRIPTEN
newSurface = new SDL2Surface(width() / m_scale, height() / m_scale, SDL2Surface::DeleteAndDestroy);
if (m_doublebuffer) {
m_doublebuffer->blitTo(newSurface, 0, 0, 0, 0, width(), height());
m_doublebuffer->dispose();
}
m_doublebuffer = newSurface;
#endif
}

Surface* SDL2Display::getSurface()
Expand All @@ -197,7 +208,7 @@ namespace she {

if (m_renderer) {
#ifdef EMSCRIPTEN
auto texture = static_cast<SDL2Surface*>(m_surface)->getTexture(nullptr);
auto texture = static_cast<SDL2Surface*>(m_doublebuffer)->getTexture(nullptr);
#else
SDL_Rect empty{0, 0, 0, 0};
auto texture = static_cast<SDL2Surface*>(m_surface)->getTexture(&empty);
Expand All @@ -211,8 +222,13 @@ namespace she {
void SDL2Display::flip(const gfx::Rect& bounds)
{
m_dirty = true;
if (!she::instance()->isGfxThread())
if (!she::instance()->isGfxThread()) {
SDL_Rect rect {bounds.x, bounds.y, bounds.w, bounds.h};
SDL_Rect dst { rect.x, rect.y, rect.w, rect.h };
SDL_BlitScaled((SDL_Surface*)m_surface->nativeHandle(), &rect,
(SDL_Surface*)m_doublebuffer->nativeHandle(), &dst);
return;
}

SDL_Rect rect {bounds.x, bounds.y, bounds.w, bounds.h};
if (m_renderer) {
Expand Down
3 changes: 2 additions & 1 deletion src/she/sdl2/sdl2_display.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ namespace she {
private:
SDL_Window* m_window;
SDL_Renderer* m_renderer;
Surface* m_surface;
Surface* m_surface{};
Surface* m_doublebuffer{};
int m_scale;
int m_width;
int m_height;
Expand Down
19 changes: 14 additions & 5 deletions src/she/sdl2/she.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#include <chrono>
#include <thread>

#ifdef EMSCRIPTEN
#ifdef __EMSCRIPTEN__
#include <emscripten/emscripten.h>
#endif

Expand Down Expand Up @@ -216,7 +216,7 @@ she::KeyModifiers getSheModifiers() {
return (she::KeyModifiers) mod;
}

#ifdef EMSCRIPTEN
#ifdef __EMSCRIPTEN__
EM_JS(int, get_canvas_width, (), { return canvas.clientWidth; });
EM_JS(int, get_canvas_height, (), { return canvas.clientHeight; });
static int oldWidth, oldHeight;
Expand Down Expand Up @@ -319,7 +319,7 @@ namespace she {
continue;

case SDL_WINDOWEVENT_RESIZED: {
#ifdef EMSCRIPTEN
#ifdef __EMSCRIPTEN__
continue;
#else
auto display = sdl::windowIdToDisplay[sdlEvent.window.windowID];
Expand Down Expand Up @@ -584,6 +584,15 @@ namespace she {
#ifndef EMSCRIPTEN
mainThreadId = gfxThreadId;
return func();
#elif defined(EMSCRIPTEN) && !defined(__EMSCRIPTEN__)
// like emscripten, but not really
mainThread = std::thread{[this, func = std::move(func)]{
mainThreadId = std::this_thread::get_id();
func();
}};
while (!shutdown)(
refresh();
}
#else
mainThread = std::thread{[this, func = std::move(func)]{
mainThreadId = std::this_thread::get_id();
Expand All @@ -592,16 +601,16 @@ namespace she {
emscripten_set_main_loop([]{
static_cast<SDL2System*>(g_instance)->refresh();
}, 0, true);
return 0;
#endif
return 0;
}

void refresh() {
if (!sleeping) {
static_cast<SDL2EventQueue*>(EventQueue::instance())->refresh();
return;
}
#ifdef EMSCRIPTEN
#ifdef __EMSCRIPTEN__
auto width = get_canvas_width();
auto height = get_canvas_height();
if (width && height && (oldWidth != width || oldHeight != height) && !sdl::windowIdToDisplay.empty()) {
Expand Down

0 comments on commit 5a2389d

Please sign in to comment.