2018-05-04 09:09:55 +02:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"image"
|
|
|
|
"image/color"
|
|
|
|
_ "image/jpeg"
|
|
|
|
"log"
|
|
|
|
"math"
|
2022-04-09 09:22:49 +02:00
|
|
|
"runtime"
|
2018-05-04 09:09:55 +02:00
|
|
|
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/examples/resources/fonts"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/examples/resources/images"
|
2022-08-07 12:31:53 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
2023-11-19 08:07:13 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/text/v2"
|
2018-05-04 09:09:55 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2023-11-19 08:07:13 +01:00
|
|
|
gophersImage *ebiten.Image
|
|
|
|
mplusFaceSource *text.GoTextFaceSource
|
2018-05-04 09:09:55 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2021-09-20 08:22:46 +02:00
|
|
|
// Decode an image from the image file's byte slice.
|
2018-05-04 09:09:55 +02:00
|
|
|
img, _, err := image.Decode(bytes.NewReader(images.Gophers_jpg))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-10-05 18:03:30 +02:00
|
|
|
gophersImage = ebiten.NewImageFromImage(img)
|
2018-05-04 09:09:55 +02:00
|
|
|
}
|
|
|
|
|
2023-11-19 08:07:13 +01:00
|
|
|
func init() {
|
|
|
|
s, err := text.NewGoTextFaceSource(bytes.NewReader(fonts.MPlus1pRegular_ttf))
|
2020-10-03 16:12:39 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2023-11-19 08:07:13 +01:00
|
|
|
mplusFaceSource = s
|
2018-05-04 09:09:55 +02:00
|
|
|
}
|
|
|
|
|
2020-04-19 18:10:00 +02:00
|
|
|
type Game struct {
|
|
|
|
count int
|
|
|
|
}
|
|
|
|
|
2020-10-04 10:42:54 +02:00
|
|
|
func (g *Game) Update() error {
|
2020-04-19 18:10:00 +02:00
|
|
|
g.count++
|
2018-05-04 09:09:55 +02:00
|
|
|
|
2022-08-07 12:31:53 +02:00
|
|
|
if runtime.GOOS == "js" {
|
|
|
|
if ebiten.IsKeyPressed(ebiten.KeyF) || len(inpututil.AppendJustPressedTouchIDs(nil)) > 0 {
|
|
|
|
ebiten.SetFullscreen(true)
|
|
|
|
}
|
2022-04-09 09:22:49 +02:00
|
|
|
}
|
2022-04-10 13:04:52 +02:00
|
|
|
if runtime.GOOS != "js" && ebiten.IsKeyPressed(ebiten.KeyQ) {
|
2022-08-27 14:33:40 +02:00
|
|
|
return ebiten.Termination
|
2018-05-04 09:09:55 +02:00
|
|
|
}
|
2020-04-19 18:10:00 +02:00
|
|
|
return nil
|
|
|
|
}
|
2018-05-04 09:09:55 +02:00
|
|
|
|
2020-04-19 18:10:00 +02:00
|
|
|
func (g *Game) Draw(screen *ebiten.Image) {
|
2018-05-04 09:09:55 +02:00
|
|
|
scale := ebiten.DeviceScaleFactor()
|
|
|
|
|
2023-01-19 15:42:35 +01:00
|
|
|
w, h := gophersImage.Bounds().Dx(), gophersImage.Bounds().Dy()
|
2018-05-04 09:09:55 +02:00
|
|
|
op := &ebiten.DrawImageOptions{}
|
|
|
|
|
|
|
|
op.GeoM.Translate(-float64(w)/2, -float64(h)/2)
|
|
|
|
op.GeoM.Scale(scale, scale)
|
2020-04-19 18:10:00 +02:00
|
|
|
op.GeoM.Rotate(float64(g.count%360) * 2 * math.Pi / 360)
|
2023-01-19 15:42:35 +01:00
|
|
|
sw, sh := screen.Bounds().Dx(), screen.Bounds().Dy()
|
2018-05-04 09:09:55 +02:00
|
|
|
op.GeoM.Translate(float64(sw)/2, float64(sh)/2)
|
|
|
|
op.Filter = ebiten.FilterLinear
|
|
|
|
screen.DrawImage(gophersImage, op)
|
|
|
|
|
2018-10-09 16:27:29 +02:00
|
|
|
fw, fh := ebiten.ScreenSizeInFullscreen()
|
2022-04-10 13:04:52 +02:00
|
|
|
msg := "This is an example of the finest fullscreen.\n"
|
2022-04-09 09:22:49 +02:00
|
|
|
if runtime.GOOS == "js" {
|
2022-08-07 12:31:53 +02:00
|
|
|
msg += "Press F or touch the screen to enter fullscreen (again).\n"
|
2022-04-10 13:04:52 +02:00
|
|
|
} else {
|
|
|
|
msg += "Press Q to quit.\n"
|
2018-05-04 09:09:55 +02:00
|
|
|
}
|
2022-04-09 09:22:49 +02:00
|
|
|
msg += fmt.Sprintf("Screen size in fullscreen: %d, %d\n", fw, fh)
|
|
|
|
msg += fmt.Sprintf("Game's screen size: %d, %d\n", sw, sh)
|
|
|
|
msg += fmt.Sprintf("Device scale factor: %0.2f\n", scale)
|
2018-05-04 09:09:55 +02:00
|
|
|
|
2023-11-19 08:07:13 +01:00
|
|
|
textOp := &text.DrawOptions{}
|
|
|
|
textOp.GeoM.Translate(50*scale, 50*scale)
|
|
|
|
textOp.ColorScale.ScaleWithColor(color.White)
|
|
|
|
textOp.LineSpacingInPixels = 12 * ebiten.DeviceScaleFactor() * 1.5
|
|
|
|
text.Draw(screen, msg, &text.GoTextFace{
|
|
|
|
Source: mplusFaceSource,
|
|
|
|
Size: 12 * ebiten.DeviceScaleFactor(),
|
|
|
|
}, textOp)
|
2020-04-19 18:10:00 +02:00
|
|
|
}
|
2018-05-04 09:09:55 +02:00
|
|
|
|
2020-04-19 18:10:00 +02:00
|
|
|
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
|
|
|
|
s := ebiten.DeviceScaleFactor()
|
|
|
|
return int(float64(outsideWidth) * s), int(float64(outsideHeight) * s)
|
2018-05-04 09:09:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
ebiten.SetFullscreen(true)
|
2022-08-29 04:16:39 +02:00
|
|
|
ebiten.SetWindowTitle("Fullscreen (Ebitengine Demo)")
|
2022-08-27 14:33:40 +02:00
|
|
|
if err := ebiten.RunGame(&Game{}); err != nil {
|
2018-05-04 09:09:55 +02:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|