zorldo

Goofing around with Ebiten
git clone git://bsandro.tech/zorldo
Log | Files | Refs | README

glfw_notwindows.go (7294B)


      1 // Copyright 2018 The Ebiten Authors
      2 //
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 // You may obtain a copy of the License at
      6 //
      7 //     http://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 
     15 //go:build !windows && !js
     16 // +build !windows,!js
     17 
     18 package glfw
     19 
     20 import (
     21 	"image"
     22 	"sync"
     23 
     24 	"github.com/go-gl/glfw/v3.3/glfw"
     25 )
     26 
     27 type windows map[*glfw.Window]*Window
     28 
     29 var (
     30 	theWindows = windows{}
     31 	windowsM   sync.Mutex
     32 )
     33 
     34 func (w windows) add(win *glfw.Window) *Window {
     35 	if win == nil {
     36 		return nil
     37 	}
     38 	ww := &Window{w: win}
     39 	windowsM.Lock()
     40 	w[win] = ww
     41 	windowsM.Unlock()
     42 	return ww
     43 }
     44 
     45 func (w windows) remove(win *glfw.Window) {
     46 	windowsM.Lock()
     47 	delete(w, win)
     48 	windowsM.Unlock()
     49 }
     50 
     51 func (w windows) get(win *glfw.Window) *Window {
     52 	if win == nil {
     53 		return nil
     54 	}
     55 	windowsM.Lock()
     56 	ww := w[win]
     57 	windowsM.Unlock()
     58 	return ww
     59 }
     60 
     61 type Cursor struct {
     62 	c *glfw.Cursor
     63 }
     64 
     65 func CreateStandardCursor(shape StandardCursor) *Cursor {
     66 	c := glfw.CreateStandardCursor(glfw.StandardCursor(shape))
     67 	return &Cursor{c: c}
     68 }
     69 
     70 type Monitor struct {
     71 	m *glfw.Monitor
     72 }
     73 
     74 func (m *Monitor) GetContentScale() (float32, float32) {
     75 	return m.m.GetContentScale()
     76 }
     77 
     78 func (m *Monitor) GetPos() (x, y int) {
     79 	return m.m.GetPos()
     80 }
     81 
     82 func (m *Monitor) GetVideoMode() *VidMode {
     83 	v := m.m.GetVideoMode()
     84 	if v == nil {
     85 		return nil
     86 	}
     87 	return &VidMode{
     88 		Width:       v.Width,
     89 		Height:      v.Height,
     90 		RedBits:     v.RedBits,
     91 		GreenBits:   v.GreenBits,
     92 		BlueBits:    v.BlueBits,
     93 		RefreshRate: v.RefreshRate,
     94 	}
     95 }
     96 
     97 type Window struct {
     98 	w *glfw.Window
     99 
    100 	prevSizeCallback SizeCallback
    101 }
    102 
    103 func (w *Window) Destroy() {
    104 	w.w.Destroy()
    105 	theWindows.remove(w.w)
    106 }
    107 
    108 func (w *Window) GetAttrib(attrib Hint) int {
    109 	return w.w.GetAttrib(glfw.Hint(attrib))
    110 }
    111 
    112 func (w *Window) GetCursorPos() (x, y float64) {
    113 	return w.w.GetCursorPos()
    114 }
    115 
    116 func (w *Window) GetInputMode(mode InputMode) int {
    117 	return w.w.GetInputMode(glfw.InputMode(mode))
    118 }
    119 
    120 func (w *Window) GetKey(key Key) Action {
    121 	return Action(w.w.GetKey(glfw.Key(key)))
    122 }
    123 
    124 func (w *Window) GetMonitor() *Monitor {
    125 	m := w.w.GetMonitor()
    126 	if m == nil {
    127 		return nil
    128 	}
    129 	return &Monitor{m}
    130 }
    131 
    132 func (w *Window) GetMouseButton(button MouseButton) Action {
    133 	return Action(w.w.GetMouseButton(glfw.MouseButton(button)))
    134 }
    135 
    136 func (w *Window) GetPos() (x, y int) {
    137 	return w.w.GetPos()
    138 }
    139 
    140 func (w *Window) GetSize() (width, height int) {
    141 	return w.w.GetSize()
    142 }
    143 
    144 func (w *Window) Hide() {
    145 	w.w.Hide()
    146 }
    147 
    148 func (w *Window) Iconify() {
    149 	w.w.Iconify()
    150 }
    151 
    152 func (w *Window) MakeContextCurrent() {
    153 	w.w.MakeContextCurrent()
    154 }
    155 
    156 func (w *Window) Maximize() {
    157 	w.w.Maximize()
    158 }
    159 
    160 func (w *Window) Restore() {
    161 	w.w.Restore()
    162 }
    163 
    164 func (w *Window) SetAttrib(attrib Hint, value int) {
    165 	w.w.SetAttrib(glfw.Hint(attrib), value)
    166 }
    167 
    168 func (w *Window) SetCharModsCallback(cbfun CharModsCallback) (previous CharModsCallback) {
    169 	w.w.SetCharModsCallback(charModsCallbacks[cbfun])
    170 	return ToCharModsCallback(nil) // TODO
    171 }
    172 
    173 func (w *Window) SetCursor(cursor *Cursor) {
    174 	var c *glfw.Cursor
    175 	if cursor != nil {
    176 		c = cursor.c
    177 	}
    178 	w.w.SetCursor(c)
    179 }
    180 
    181 func (w *Window) SetCloseCallback(cbfun CloseCallback) (previous CloseCallback) {
    182 	w.w.SetCloseCallback(closeCallbacks[cbfun])
    183 	return ToCloseCallback(nil) // TODO
    184 }
    185 
    186 func (w *Window) SetFramebufferSizeCallback(cbfun FramebufferSizeCallback) (previous FramebufferSizeCallback) {
    187 	w.w.SetFramebufferSizeCallback(framebufferSizeCallbacks[cbfun])
    188 	return ToFramebufferSizeCallback(nil) // TODO
    189 }
    190 
    191 func (w *Window) SetScrollCallback(cbfun ScrollCallback) (previous ScrollCallback) {
    192 	w.w.SetScrollCallback(scrollCallbacks[cbfun])
    193 	return ToScrollCallback(nil) // TODO
    194 }
    195 
    196 func (w *Window) SetShouldClose(value bool) {
    197 	w.w.SetShouldClose(value)
    198 }
    199 
    200 func (w *Window) SetSizeCallback(cbfun SizeCallback) (previous SizeCallback) {
    201 	w.w.SetSizeCallback(sizeCallbacks[cbfun])
    202 	prev := w.prevSizeCallback
    203 	w.prevSizeCallback = cbfun
    204 	return prev
    205 }
    206 
    207 func (w *Window) SetSizeLimits(minw, minh, maxw, maxh int) {
    208 	w.w.SetSizeLimits(minw, minh, maxw, maxh)
    209 }
    210 
    211 func (w *Window) SetIcon(images []image.Image) {
    212 	w.w.SetIcon(images)
    213 }
    214 
    215 func (w *Window) SetInputMode(mode InputMode, value int) {
    216 	w.w.SetInputMode(glfw.InputMode(mode), value)
    217 }
    218 
    219 func (w *Window) SetMonitor(monitor *Monitor, xpos, ypos, width, height, refreshRate int) {
    220 	var m *glfw.Monitor
    221 	if monitor != nil {
    222 		m = monitor.m
    223 	}
    224 	w.w.SetMonitor(m, xpos, ypos, width, height, refreshRate)
    225 }
    226 
    227 func (w *Window) SetPos(xpos, ypos int) {
    228 	w.w.SetPos(xpos, ypos)
    229 }
    230 
    231 func (w *Window) SetSize(width, height int) {
    232 	w.w.SetSize(width, height)
    233 }
    234 
    235 func (w *Window) SetTitle(title string) {
    236 	w.w.SetTitle(title)
    237 }
    238 
    239 func (w *Window) ShouldClose() bool {
    240 	return w.w.ShouldClose()
    241 }
    242 
    243 func (w *Window) Show() {
    244 	w.w.Show()
    245 }
    246 
    247 func (w *Window) SwapBuffers() {
    248 	w.w.SwapBuffers()
    249 }
    250 
    251 func CreateWindow(width, height int, title string, monitor *Monitor, share *Window) (*Window, error) {
    252 	var gm *glfw.Monitor
    253 	if monitor != nil {
    254 		gm = monitor.m
    255 	}
    256 	var gw *glfw.Window
    257 	if share != nil {
    258 		gw = share.w
    259 	}
    260 
    261 	w, err := glfw.CreateWindow(width, height, title, gm, gw)
    262 	if err != nil {
    263 		return nil, err
    264 	}
    265 	return theWindows.add(w), nil
    266 }
    267 
    268 func (j Joystick) GetGUID() string {
    269 	return glfw.Joystick(j).GetGUID()
    270 }
    271 
    272 func (j Joystick) GetName() string {
    273 	return glfw.Joystick(j).GetName()
    274 }
    275 
    276 func (j Joystick) GetAxes() []float32 {
    277 	return glfw.Joystick(j).GetAxes()
    278 }
    279 
    280 func (j Joystick) GetButtons() []Action {
    281 	var bs []Action
    282 	for _, b := range glfw.Joystick(j).GetButtons() {
    283 		bs = append(bs, Action(b))
    284 	}
    285 	return bs
    286 }
    287 
    288 func (j Joystick) GetHats() []JoystickHatState {
    289 	var hats []JoystickHatState
    290 	for _, s := range glfw.Joystick(j).GetHats() {
    291 		hats = append(hats, JoystickHatState(s))
    292 	}
    293 	return hats
    294 }
    295 
    296 func GetMonitors() []*Monitor {
    297 	ms := []*Monitor{}
    298 	for _, m := range glfw.GetMonitors() {
    299 		if m != nil {
    300 			ms = append(ms, &Monitor{m})
    301 		} else {
    302 			ms = append(ms, nil)
    303 		}
    304 	}
    305 	return ms
    306 }
    307 
    308 func GetPrimaryMonitor() *Monitor {
    309 	m := glfw.GetPrimaryMonitor()
    310 	if m == nil {
    311 		return nil
    312 	}
    313 	return &Monitor{m}
    314 }
    315 
    316 func Init() error {
    317 	return glfw.Init()
    318 }
    319 
    320 func (j Joystick) Present() bool {
    321 	return glfw.Joystick(j).Present()
    322 }
    323 
    324 func PollEvents() {
    325 	glfw.PollEvents()
    326 }
    327 
    328 func PostEmptyEvent() {
    329 	glfw.PostEmptyEvent()
    330 }
    331 
    332 func SetMonitorCallback(cbfun func(monitor *Monitor, event PeripheralEvent)) {
    333 	var gcb func(monitor *glfw.Monitor, event glfw.PeripheralEvent)
    334 	if cbfun != nil {
    335 		gcb = func(monitor *glfw.Monitor, event glfw.PeripheralEvent) {
    336 			var m *Monitor
    337 			if monitor != nil {
    338 				m = &Monitor{monitor}
    339 			}
    340 			cbfun(m, PeripheralEvent(event))
    341 		}
    342 	}
    343 	glfw.SetMonitorCallback(gcb)
    344 }
    345 
    346 func SwapInterval(interval int) {
    347 	glfw.SwapInterval(interval)
    348 }
    349 
    350 func Terminate() {
    351 	glfw.Terminate()
    352 }
    353 
    354 func WaitEvents() {
    355 	glfw.WaitEvents()
    356 }
    357 
    358 func WaitEventsTimeout(timeout float64) {
    359 	glfw.WaitEventsTimeout(timeout)
    360 }
    361 
    362 func WindowHint(target Hint, hint int) {
    363 	glfw.WindowHint(glfw.Hint(target), hint)
    364 }