Skip to content

Commit

Permalink
Added rudimentary support for HiDPI on WASM
Browse files Browse the repository at this point in the history
Due to a bug in SDL2 (emscripten-ports/SDL2#109), HiDPI mode forces the
canvas to be larger on HiDPI monitors. To work around this, use the
following CSS rules on the canvas:
```
@media (min-resolution: 144dpi) and (max-resolution: 240dpi) {
    canvas.emscripten {
        clip-path: polygon(0% 0%, 0% 50%, 50% 50%, 50% 0%);
        margin-left: 620px;
        margin-top: 350px;
    }
}

@media (min-resolution: 240dpi) and (max-resolution: 336dpi) {
    canvas.emscripten {
        clip-path: polygon(0% 0%, 0% 33.33333333%, 33.33333333% 33.33333333%, 33.33333333% 0%);
        margin-left: 1240px;
        margin-top: 700px;
    }
}
```
HiDPI can be disabled by adding `-DNO_EMSCRIPTEN_HIDPI` to CPPFLAGS.
  • Loading branch information
MCJack123 committed Mar 26, 2020
1 parent 0fca133 commit d76dd42
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/terminal/SDLTerminal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ extern "C" {
extern void syncfs();
}

void onWindowCreate(int id, const char * title) {EM_ASM({Module.windowEventListener.onWindowCreate($0, $1);}, id, title);}
void onWindowDestroy(int id) {EM_ASM({Module.windowEventListener.onWindowDestroy($0);}, id);}
void onWindowCreate(int id, const char * title) {EM_ASM({if (Module.windowEventListener !== undefined) Module.windowEventListener.onWindowCreate($0, $1);}, id, title);}
void onWindowDestroy(int id) {EM_ASM({if (Module.windowEventListener !== undefined) Module.windowEventListener.onWindowDestroy($0);}, id);}
#endif

SDLTerminal::SDLTerminal(std::string title): Terminal(51, 19) {
Expand Down Expand Up @@ -141,10 +141,16 @@ SDLTerminal::SDLTerminal(std::string title): Terminal(51, 19) {
charWidth = fontWidth * 2/fontScale * charScale;
charHeight = fontHeight * 2/fontScale * charScale;
}
#ifdef __EMSCRIPTEN__
#if defined(__EMSCRIPTEN__) && !defined(NO_EMSCRIPTEN_HIDPI)
if (win == NULL)
#else
dpiScale = 1;
#endif
win = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width*charWidth+(4 * charScale * (2 / fontScale)), height*charHeight+(4 * charScale * (2 / fontScale)), SDL_WINDOW_SHOWN | (EMSCRIPTEN_ENABLED ? 0 : SDL_WINDOW_ALLOW_HIGHDPI) | SDL_WINDOW_RESIZABLE | SDL_WINDOW_INPUT_FOCUS);
win = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width*charWidth*dpiScale+(4 * charScale * (2 / fontScale)*dpiScale), height*charHeight*dpiScale+(4 * charScale * (2 / fontScale)*dpiScale), SDL_WINDOW_SHOWN |
#if !(defined(__EMSCRIPTEN__) && defined(NO_EMSCRIPTEN_HIDPI))
SDL_WINDOW_ALLOW_HIGHDPI |
#endif
SDL_WINDOW_RESIZABLE | SDL_WINDOW_INPUT_FOCUS);
if (win == nullptr || win == NULL || win == (SDL_Window*)0) {
overridden = true;
throw window_exception("Failed to create window");
Expand All @@ -165,7 +171,6 @@ SDLTerminal::SDLTerminal(std::string title): Terminal(51, 19) {
SDL_FreeSurface(icon);
delete[] icon_pixels;
#endif
dpiScale = 1;
SDL_Surface* old_bmp;
if (config.customFontPath.empty())
old_bmp = SDL_CreateRGBSurfaceWithFormatFrom((void*)font_image.pixel_data, font_image.width, font_image.height, font_image.bytes_per_pixel * 8, font_image.bytes_per_pixel * font_image.width, SDL_PIXELFORMAT_RGB565);
Expand Down

0 comments on commit d76dd42

Please sign in to comment.