github.com/rajveermalviya/gamen@v0.1.2-0.20220930195403-9be15877c1aa/internal/win32/display.go (about) 1 //go:build windows 2 3 package win32 4 5 import ( 6 "time" 7 "unsafe" 8 9 "github.com/rajveermalviya/gamen/internal/common/atomicx" 10 "github.com/rajveermalviya/gamen/internal/win32/procs" 11 ) 12 13 type Display struct { 14 windows map[uintptr]*Window 15 16 destroyRequested atomicx.Bool 17 destroyed atomicx.Bool 18 } 19 20 func NewDisplay() (*Display, error) { 21 return &Display{ 22 windows: map[uintptr]*Window{}, 23 }, nil 24 } 25 26 func (d *Display) Destroy() { 27 d.destroyRequested.Store(true) 28 } 29 30 func (d *Display) destroy() { 31 for hwnd, w := range d.windows { 32 w.Destroy() 33 34 d.windows[hwnd] = nil 35 delete(d.windows, hwnd) 36 } 37 38 d.destroyed.Store(true) 39 } 40 41 func (d *Display) Poll() bool { 42 var msg procs.MSG 43 44 for procs.PeekMessageW(uintptr(unsafe.Pointer(&msg)), 0, 0, 0, procs.PM_REMOVE) { 45 procs.TranslateMessage(uintptr(unsafe.Pointer(&msg))) 46 procs.DispatchMessageW(uintptr(unsafe.Pointer(&msg))) 47 } 48 49 if d.destroyRequested.Load() && !d.destroyed.Load() { 50 d.destroy() 51 return false 52 } 53 54 return !d.destroyed.Load() 55 } 56 57 func (d *Display) Wait() bool { 58 procs.WaitMessage() 59 return d.Poll() 60 } 61 62 func (d *Display) WaitTimeout(timeout time.Duration) bool { 63 procs.MsgWaitForMultipleObjects(0, 0, 0, uintptr(timeout.Milliseconds()), procs.QS_ALLEVENTS) 64 return d.Poll() 65 }