glfw: high dpi on windows

pull/4027/head
yuyi 2020-03-15 08:16:55 +08:00 committed by GitHub
parent 843bb6dac1
commit a121dfd23a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 37 additions and 15 deletions

View File

@ -46,30 +46,33 @@ pub const (
KeyDown = 264 KeyDown = 264
) )
__global monitor_scale f32
// joe-c: fix & remove // joe-c: fix & remove
struct TmpGlImportHack { struct TmpGlImportHack {
hack gl.TmpGlImportHack hack gl.TmpGlImportHack
} }
pub struct WinCfg { pub struct WinCfg {
width int width int
height int height int
title string title string
ptr voidptr ptr voidptr
borderless bool borderless bool
is_modal int is_modal int
is_browser bool is_browser bool
url string url string
always_on_top bool always_on_top bool
scale_to_monitor bool = true
} }
// data *C.GLFWwindow // data *C.GLFWwindow
// TODO change data to cobj // TODO change data to cobj
pub struct Window { pub struct Window {
data voidptr data voidptr
title string title string
mx int mx int
my int my int
} }
pub struct Size { pub struct Size {
@ -127,6 +130,11 @@ pub fn create_window(c WinCfg) &glfw.Window {
if c.always_on_top { if c.always_on_top {
window_hint(C.GLFW_FLOATING, 1) window_hint(C.GLFW_FLOATING, 1)
} }
if c.scale_to_monitor {
$if windows {
window_hint(C.GLFW_SCALE_TO_MONITOR, 1)
}
}
cwindow := C.glfwCreateWindow(c.width, c.height, c.title.str, 0, 0) cwindow := C.glfwCreateWindow(c.width, c.height, c.title.str, 0, 0)
if isnil(cwindow) { if isnil(cwindow) {
println('failed to create a glfw window, make sure you have a GPU driver installed') println('failed to create a glfw window, make sure you have a GPU driver installed')
@ -134,6 +142,14 @@ pub fn create_window(c WinCfg) &glfw.Window {
} }
// println('create window wnd=$cwindow ptr==$c.ptr') // println('create window wnd=$cwindow ptr==$c.ptr')
C.glfwSetWindowUserPointer(cwindow, c.ptr) C.glfwSetWindowUserPointer(cwindow, c.ptr)
$if windows {
C.glfwGetWindowContentScale(cwindow, &monitor_scale, &monitor_scale)
}
$else {
monitor_scale = 1.0
}
window := &glfw.Window { window := &glfw.Window {
data: cwindow, data: cwindow,
title: c.title, title: c.title,
@ -149,6 +165,10 @@ pub fn (w &glfw.Window) make_context_current() {
C.glfwMakeContextCurrent(w.data) C.glfwMakeContextCurrent(w.data)
} }
pub fn (w &glfw.Window) scale() f64 {
return monitor_scale
}
pub fn swap_interval(interval int) { pub fn swap_interval(interval int) {
C.glfwSwapInterval(interval) C.glfwSwapInterval(interval)
} }
@ -233,16 +253,18 @@ pub fn get_cursor_pos(glfw_window voidptr) (f64, f64) {
x := f64(0) x := f64(0)
y := f64(0) y := f64(0)
C.glfwGetCursorPos(glfw_window, &x, &y) C.glfwGetCursorPos(glfw_window, &x, &y)
return x,y
return x/monitor_scale, y/monitor_scale
} }
pub fn (w &glfw.Window) get_cursor_pos() Pos { pub fn (w &glfw.Window) get_cursor_pos() Pos {
x := f64(0) x := f64(0)
y := f64(0) y := f64(0)
C.glfwGetCursorPos(w.data, &x, &y) C.glfwGetCursorPos(w.data, &x, &y)
return Pos { return Pos {
x: int(x) x: int(x/monitor_scale)
y: int(y) y: int(y/monitor_scale)
} }
} }