ui: Add MonitorSize

Fixes #470
This commit is contained in:
Hajime Hoshi 2018-05-04 16:09:55 +09:00
parent 9a06b31556
commit d8dba69b47
6 changed files with 188 additions and 0 deletions

133
examples/fullscreen/main.go Normal file
View File

@ -0,0 +1,133 @@
// Copyright 2018 The Ebiten Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build example jsgo
package main
import (
"bytes"
"errors"
"fmt"
"image"
"image/color"
_ "image/jpeg"
"log"
"math"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/examples/resources/fonts"
"github.com/hajimehoshi/ebiten/examples/resources/images"
"github.com/hajimehoshi/ebiten/text"
)
var (
count = 0
gophersImage *ebiten.Image
mplusFont font.Face
regularTermination = errors.New("regular termination")
)
func init() {
// Decode image from a byte slice instead of a file so that
// this example works in any working directory.
// If you want to use a file, there are some options:
// 1) Use os.Open and pass the file to the image decoder.
// This is a very regular way, but doesn't work on browsers.
// 2) Use ebitenutil.OpenFile and pass the file to the image decoder.
// This works even on browsers.
// 3) Use ebitenutil.NewImageFromFile to create an ebiten.Image directly from a file.
// This also works on browsers.
img, _, err := image.Decode(bytes.NewReader(images.Gophers_jpg))
if err != nil {
log.Fatal(err)
}
gophersImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
}
func initFont() {
tt, err := truetype.Parse(fonts.MPlus1pRegular_ttf)
if err != nil {
log.Fatal(err)
}
const dpi = 72
mplusFont = truetype.NewFace(tt, &truetype.Options{
Size: 12 * ebiten.DeviceScaleFactor(),
DPI: dpi,
Hinting: font.HintingFull,
})
}
func update(screen *ebiten.Image) error {
count++
if ebiten.IsKeyPressed(ebiten.KeyQ) {
return regularTermination
}
if ebiten.IsRunningSlowly() {
return nil
}
scale := ebiten.DeviceScaleFactor()
w, h := gophersImage.Size()
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(-float64(w)/2, -float64(h)/2)
op.GeoM.Scale(scale, scale)
op.GeoM.Rotate(float64(count%360) * 2 * math.Pi / 360)
sw, sh := screen.Size()
op.GeoM.Translate(float64(sw)/2, float64(sh)/2)
op.Filter = ebiten.FilterLinear
screen.DrawImage(gophersImage, op)
mw, mh := ebiten.MonitorSize()
msgs := []string{
"This is an example of the finest fullscreen. Press Q to quit.",
fmt.Sprintf("Monitor size: %d, %d", mw, mh),
fmt.Sprintf("Game's screen size: %d, %d", sw, sh),
fmt.Sprintf("Device scale factor: %0.2f", scale),
}
for i, msg := range msgs {
text.Draw(screen, msg, mplusFont, 100, 100+int(float64(i)*16*scale), color.White)
}
return nil
}
func main() {
// Call initFont here instead of init funcs since ebiten.DeviceScaleFactor is not available in init.
initFont()
ebiten.SetFullscreen(true)
w, h := ebiten.MonitorSize()
// On mobiles, ebiten.MonitorSize is not available so far.
// Use arbitrary values.
if w == 0 || h == 0 {
w = 300
h = 450
}
s := ebiten.DeviceScaleFactor()
if err := ebiten.Run(update, int(float64(w)*s), int(float64(h)*s), 1/s, "Rotate (Ebiten Demo)"); err != nil && err != regularTermination {
log.Fatal(err)
}
}

View File

@ -168,6 +168,8 @@ func main() {
flag.Parse() flag.Parse()
fmt.Printf("Device scale factor: %0.2f\n", ebiten.DeviceScaleFactor()) fmt.Printf("Device scale factor: %0.2f\n", ebiten.DeviceScaleFactor())
w, h := ebiten.MonitorSize()
fmt.Printf("Monitor size: %d, %d\n", w, h)
// Decode image from a byte slice instead of a file so that // Decode image from a byte slice instead of a file so that
// this example works in any working directory. // this example works in any working directory.

View File

@ -227,6 +227,12 @@ func (u *userInterface) runOnMainThread(f func() error) error {
return err return err
} }
func MonitorSize() (int, int) {
m := glfw.GetPrimaryMonitor()
v := m.GetVideoMode()
return v.Width, v.Height
}
func SetScreenSize(width, height int) bool { func SetScreenSize(width, height int) bool {
u := currentUI u := currentUI
if !u.isRunning() { if !u.isRunning() {

View File

@ -45,6 +45,11 @@ var currentUI = &userInterface{
windowFocus: true, windowFocus: true,
} }
func MonitorSize() (int, int) {
w := js.Global.Get("window")
return w.Get("innerWidth").Int(), w.Get("innerHeight").Int()
}
func SetScreenSize(width, height int) bool { func SetScreenSize(width, height int) bool {
return currentUI.setScreenSize(width, height, currentUI.scale, currentUI.fullscreen) return currentUI.setScreenSize(width, height, currentUI.scale, currentUI.fullscreen)
} }

View File

@ -227,6 +227,12 @@ func (u *userInterface) screenSize() (int, int) {
return w, h return w, h
} }
func MonitorSize() (int, int) {
// TODO: This function should return fullscreenWidthPx, fullscreenHeightPx,
// but these values are not initialized until the main loop starts.
return 0, 0
}
func SetScreenSize(width, height int) bool { func SetScreenSize(width, height int) bool {
currentUI.setScreenSize(width, height) currentUI.setScreenSize(width, height)
return true return true

36
run.go
View File

@ -295,6 +295,39 @@ func RunWithoutMainLoop(f func(*Image) error, width, height int, scale float64,
return ch return ch
} }
// MonitorSize returns the monitor size in device-independent pixels.
//
// On browsers, MonitorSize returns the 'window' size, not 'screen' size since an Ebiten game
// should not know the outside of the window object.
// For more detials, see SetFullscreen API comment.
//
// On mobiles, MonitorSize returns (0, 0) so far.
//
// Note that MonitorSize returns the 'primary' monitor size, which is the monitor
// where taskbar is present (Windows) or menubar is present (macOS).
//
// If you use this for screen size with SetFullscreen(true), you can get the fullscreen mode
// which size is well adjusted with the monitor.
//
// w, h := MonitorSize()
// ebiten.SetFullscreen(true)
// ebiten.Run(update, w, h, 1, "title")
//
// Furthermore, you can use them with DeviceScaleFactor(), you can get the finest
// fullscreen mode.
//
// s := ebiten.DeviceScaleFactor()
// w, h := MonitorSize()
// ebiten.SetFullscreen(true)
// ebiten.Run(update, int(float64(w) * s), int(float64(h) * s), 1/s, "title")
//
// For actual example, see examples/fullscreen
//
// MonitorSize is concurrent-safe.
func MonitorSize() (int, int) {
return ui.MonitorSize()
}
// SetScreenSize changes the (logical) size of the screen. // SetScreenSize changes the (logical) size of the screen.
// This doesn't affect the current scale of the screen. // This doesn't affect the current scale of the screen.
// //
@ -377,6 +410,9 @@ func IsFullscreen() bool {
// //
// On browsers, the game screen is resized to fit with the body element (client) size. // On browsers, the game screen is resized to fit with the body element (client) size.
// Additionally, the game screen is automatically resized when the body element is resized. // Additionally, the game screen is automatically resized when the body element is resized.
// Note that this has nothing to do with 'screen' which is outside of 'window'.
// It is recommended to put Ebiten game in an iframe, and if you want to make the game 'fullscreen'
// on browsers with Fullscreen API, you can do this by applying the API to the iframe.
// //
// SetFullscreen does nothing on mobiles. // SetFullscreen does nothing on mobiles.
// //