Better DPI handling on windows (crispy text!)

This commit is contained in:
bloeys
2023-02-04 05:21:48 +04:00
parent b44b00d7e2
commit 78ea3ae747
3 changed files with 42 additions and 1 deletions

43
main.go
View File

@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"runtime"
"github.com/bloeys/gglm/gglm" "github.com/bloeys/gglm/gglm"
"github.com/bloeys/nmage/assets" "github.com/bloeys/nmage/assets"
@ -35,6 +36,9 @@ import (
const ( const (
camSpeed = 15 camSpeed = 15
mouseSensitivity = 0.5 mouseSensitivity = 0.5
unscaledWindowWidth = 1280
unscaledWindowHeight = 720
) )
var ( var (
@ -60,6 +64,8 @@ var (
debugDrawDepthBuffer bool debugDrawDepthBuffer bool
skyboxCmap assets.Cubemap skyboxCmap assets.Cubemap
dpiScaling float32
) )
type OurGame struct { type OurGame struct {
@ -113,7 +119,8 @@ func main() {
} }
//Create window //Create window
window, err = engine.CreateOpenGLWindowCentered("nMage", 1280, 720, engine.WindowFlags_RESIZABLE, rend3dgl.NewRend3DGL()) dpiScaling = getDpiScaling(unscaledWindowWidth, unscaledWindowHeight)
window, err = engine.CreateOpenGLWindowCentered("nMage", int32(unscaledWindowWidth*dpiScaling), int32(unscaledWindowHeight*dpiScaling), engine.WindowFlags_RESIZABLE, rend3dgl.NewRend3DGL())
if err != nil { if err != nil {
logging.ErrLog.Fatalln("Failed to create window. Err: ", err) logging.ErrLog.Fatalln("Failed to create window. Err: ", err)
} }
@ -147,6 +154,40 @@ func (g *OurGame) handleWindowEvents(e sdl.Event) {
} }
} }
func getDpiScaling(unscaledWindowWidth, unscaledWindowHeight int32) float32 {
// Great read on DPI here: https://nlguillemot.wordpress.com/2016/12/11/high-dpi-rendering/
// The no-scaling DPI on different platforms (e.g. when scale=100% on windows)
var defaultDpi float32 = 96
if runtime.GOOS == "windows" {
defaultDpi = 96
} else if runtime.GOOS == "darwin" {
defaultDpi = 72
}
// Current DPI of the monitor
_, dpiHorizontal, _, err := sdl.GetDisplayDPI(0)
if err != nil {
dpiHorizontal = defaultDpi
fmt.Printf("Failed to get DPI with error '%s'. Using default DPI of '%f'\n", err.Error(), defaultDpi)
}
// Scaling factor (e.g. will be 1.25 for 125% scaling on windows)
dpiScaling := dpiHorizontal / defaultDpi
fmt.Printf(
"Default DPI=%f\nHorizontal DPI=%f\nDPI scaling=%f\nUnscaled window size (width, height)=(%d, %d)\nScaled window size (width, height)=(%d, %d)\n\n",
defaultDpi,
dpiHorizontal,
dpiScaling,
unscaledWindowWidth, unscaledWindowHeight,
int32(float32(unscaledWindowWidth)*dpiScaling), int32(float32(unscaledWindowHeight)*dpiScaling),
)
return dpiScaling
}
func (g *OurGame) Init() { func (g *OurGame) Init() {
var err error var err error

BIN
rsrc_windows_386.syso Executable file

Binary file not shown.

BIN
rsrc_windows_amd64.syso Executable file

Binary file not shown.