1
0

monitor.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright 2023 The Ebitengine 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. package ebiten
  15. import (
  16. "github.com/hajimehoshi/ebiten/v2/internal/ui"
  17. )
  18. // MonitorType represents a monitor available to the system.
  19. type MonitorType ui.Monitor
  20. // Name returns the monitor's name. On Linux, this reports the monitors in xrandr format.
  21. // On Windows, this reports "Generic PnP Monitor" for all monitors.
  22. func (m *MonitorType) Name() string {
  23. return (*ui.Monitor)(m).Name()
  24. }
  25. // DeviceScaleFactor returns the device scale factor of the monitor.
  26. //
  27. // DeviceScaleFactor returns a meaningful value on high-DPI display environment,
  28. // otherwise DeviceScaleFactor returns 1.
  29. //
  30. // On mobiles, DeviceScaleFactor returns 1 before the game starts e.g. in init functions.
  31. func (m *MonitorType) DeviceScaleFactor() float64 {
  32. return (*ui.Monitor)(m).DeviceScaleFactor()
  33. }
  34. // Size returns the size of the monitor in device-independent pixels.
  35. // This is the same as the screen size in fullscreen mode.
  36. // The returned value can be given to SetSize function if the perfectly fit fullscreen is needed.
  37. //
  38. // On mobiles, Size returns (0, 0) before the game starts e.g. in init functions.
  39. //
  40. // Size's use cases are limited. If you are making a fullscreen application, you can use RunGame and
  41. // the Game interface's Layout function instead. If you are making a not-fullscreen application but the application's
  42. // behavior depends on the monitor size, Size is useful.
  43. func (m *MonitorType) Size() (int, int) {
  44. return (*ui.Monitor)(m).Size()
  45. }
  46. // Monitor returns the current monitor.
  47. func Monitor() *MonitorType {
  48. m := ui.Get().Monitor()
  49. if m == nil {
  50. return nil
  51. }
  52. return (*MonitorType)(m)
  53. }
  54. // SetMonitor sets the monitor that the window should be on. This can be called before or after Run.
  55. func SetMonitor(monitor *MonitorType) {
  56. ui.Get().Window().SetMonitor((*ui.Monitor)(monitor))
  57. }
  58. // AppendMonitors returns the monitors reported by the system.
  59. // On desktop platforms, there will always be at least one monitor appended and the first monitor in the slice will be the primary monitor.
  60. // Any monitors added or removed will show up with subsequent calls to this function.
  61. func AppendMonitors(monitors []*MonitorType) []*MonitorType {
  62. // TODO: This is not an efficient operation. It would be best if we could directly pass monitors directly into `ui.AppendMonitors`.
  63. for _, m := range ui.Get().AppendMonitors(nil) {
  64. monitors = append(monitors, (*MonitorType)(m))
  65. }
  66. return monitors
  67. }