Skip to content

Commit

Permalink
DPI scale widgets and stuff in web client too
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Oct 13, 2019
1 parent 298d497 commit cee0fd3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
10 changes: 7 additions & 3 deletions src/Drawer2D.c
Expand Up @@ -1012,7 +1012,7 @@ String Font_Lookup(const String* fontName, int style) {
cc_result Font_Make(struct FontDesc* desc, const String* fontName, int size, int style) {
struct SysFont* font;
String value, path, index;
int faceIndex;
int faceIndex, dpiX, dpiY;
FT_Open_Args args;
FT_Error err;

Expand All @@ -1029,8 +1029,12 @@ cc_result Font_Make(struct FontDesc* desc, const String* fontName, int size, int
if ((err = SysFont_Init(&path, font, &args))) { Mem_Free(font); return err; }
desc->handle = font;

if ((err = FT_New_Face(ft_lib, &args, faceIndex, &font->face))) return err;
if ((err = FT_Set_Char_Size(font->face, size * 64, 0, Display_DpiX, Display_DpiY))) return err;
/* TODO: Use 72 instead of 96 dpi for mobile devices */
dpiX = (int)(Display_DpiX * 96);
dpiY = (int)(Display_DpiY * 96);

if ((err = FT_New_Face(ft_lib, &args, faceIndex, &font->face))) return err;
if ((err = FT_Set_Char_Size(font->face, size * 64, 0, dpiX, dpiY))) return err;

/* height of any text when drawn with the given system font */
desc->height = TEXT_CEIL(font->face->size->metrics.height);
Expand Down
27 changes: 12 additions & 15 deletions src/Window.c
Expand Up @@ -7,12 +7,12 @@
#include "ExtMath.h"

int Display_BitsPerPixel;
int Display_DpiX = DISPLAY_DEFAULT_DPI;
int Display_DpiY = DISPLAY_DEFAULT_DPI;
float Display_DpiX = 1.0f;
float Display_DpiY = 1.0f;
Rect2D Display_Bounds;

int Display_ScaleX(int x) { return x * Display_DpiX / DISPLAY_DEFAULT_DPI; }
int Display_ScaleY(int y) { return y * Display_DpiY / DISPLAY_DEFAULT_DPI; }
int Display_ScaleX(int x) { return (int)(x * Display_DpiX); }
int Display_ScaleY(int y) { return (int)(y * Display_DpiY); }
#define Display_CentreX(width) (Display_Bounds.X + (Display_Bounds.Width - width) / 2)
#define Display_CentreY(height) (Display_Bounds.Y + (Display_Bounds.Height - height) / 2)

Expand Down Expand Up @@ -362,10 +362,8 @@ void Window_Init(void) {
Display_Bounds.Width = GetSystemMetrics(SM_CXSCREEN);
Display_Bounds.Height = GetSystemMetrics(SM_CYSCREEN);
Display_BitsPerPixel = GetDeviceCaps(hdc, BITSPIXEL);
Display_DpiX = GetDeviceCaps(hdc, LOGPIXELSX);
Display_DpiY = GetDeviceCaps(hdc, LOGPIXELSY);

Platform_Log2("DPI: %i, %i", &Display_DpiX, &Display_DpiY);
Display_DpiX = GetDeviceCaps(hdc, LOGPIXELSX) / 96.0f;
Display_DpiY = GetDeviceCaps(hdc, LOGPIXELSY) / 96.0f;
ReleaseDC(NULL, hdc);
}

Expand Down Expand Up @@ -2763,8 +2761,6 @@ void GLContext_SetFpsLimit(cc_bool vsync, float minFrameMs) {
#include <emscripten/emscripten.h>
#include <emscripten/html5.h>
#include <emscripten/key_codes.h>
static float dpiScale;
#define DPI_SCALE(x) ((int)(x * dpiScale))

static void Window_RefreshBounds(void) {
emscripten_get_canvas_element_size(NULL, &Window_Width, &Window_Height);
Expand Down Expand Up @@ -2804,7 +2800,7 @@ static EM_BOOL Window_MouseMove(int type, const EmscriptenMouseEvent* ev, void*
Input_SetPressed(KEY_RMOUSE, (ev->buttons & 0x02) != 0);
Input_SetPressed(KEY_MMOUSE, (ev->buttons & 0x04) != 0);

Pointer_SetPosition(0, DPI_SCALE(ev->canvasX), DPI_SCALE(ev->canvasY));
Pointer_SetPosition(0, Display_ScaleX(ev->canvasX), Display_ScaleY(ev->canvasY));
if (Input_RawMode) Event_RaiseMove(&PointerEvents.RawMoved, 0, ev->movementX, ev->movementY);
return true;
}
Expand All @@ -2816,7 +2812,7 @@ static EM_BOOL Window_TouchStart(int type, const EmscriptenTouchEvent* ev, void*
t = &ev->touches[i];
if (!t->isChanged) continue;

Input_AddTouch(t->identifier, DPI_SCALE(t->canvasX), DPI_SCALE(t->canvasY));
Input_AddTouch(t->identifier, Display_ScaleX(t->canvasX), Display_ScaleY(t->canvasY));
}
return true;
}
Expand All @@ -2828,7 +2824,7 @@ static EM_BOOL Window_TouchMove(int type, const EmscriptenTouchEvent* ev, void*
t = &ev->touches[i];
if (!t->isChanged) continue;

Input_UpdateTouch(t->identifier, DPI_SCALE(t->canvasX), DPI_SCALE(t->canvasY));
Input_UpdateTouch(t->identifier, Display_ScaleX(t->canvasX), Display_ScaleY(t->canvasY));
}
return true;
}
Expand All @@ -2840,7 +2836,7 @@ static EM_BOOL Window_TouchEnd(int type, const EmscriptenTouchEvent* ev, void* d
t = &ev->touches[i];
if (!t->isChanged) continue;

Input_RemoveTouch(t->identifier, DPI_SCALE(t->canvasX), DPI_SCALE(t->canvasY));
Input_RemoveTouch(t->identifier, Display_ScaleX(t->canvasX), Display_ScaleY(t->canvasY));
}
return true;
}
Expand Down Expand Up @@ -3021,7 +3017,8 @@ void Window_Init(void) {
Display_Bounds.Height = EM_ASM_INT_V({ return screen.height; });
Display_BitsPerPixel = 24;

dpiScale = EM_ASM_DOUBLE_V({ return window.devicePixelRatio || 1.0; });
Display_DpiX = EM_ASM_DOUBLE_V({ return window.devicePixelRatio || 1.0; });
Display_DpiY = Display_DpiX;

/* copy text, but only if user isn't selecting something else on the webpage */
EM_ASM(window.addEventListener('copy',
Expand Down
7 changes: 3 additions & 4 deletions src/Window.h
Expand Up @@ -33,16 +33,15 @@

/* The states the window can be in. */
enum WindowState { WINDOW_STATE_NORMAL, WINDOW_STATE_MINIMISED, WINDOW_STATE_FULLSCREEN };
#define DISPLAY_DEFAULT_DPI 96

/* Number of bits per pixel. (red bits + green bits + blue bits + alpha bits) */
/* NOTE: Only 24 or 32 bits per pixel are officially supported. */
/* Support for other values of bits per pixel is platform dependent. */
extern int Display_BitsPerPixel;
/* Number of physical dots per inch of the display both horizontally and vertically. */
/* NOTE: Usually 96 for compatibility, even if the display's DPI is higher. */
/* Scale based on number of physical dots per inch of the display. (horizontally and vertically) */
/* NOTE: Usually 1 for compatibility, even if the display's DPI is really higher. */
/* GUI elements must be scaled by this to look correct. */
extern int Display_DpiX, Display_DpiY;
extern float Display_DpiX, Display_DpiY;
/* Position and size of this display. */
/* NOTE: Position may be non-zero in a multi-monitor setup. Platform dependent. */
extern Rect2D Display_Bounds;
Expand Down

0 comments on commit cee0fd3

Please sign in to comment.