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.
|
|
|
|
|
2021-06-24 14:49:37 +02:00
|
|
|
//go:build example
|
2020-10-06 17:45:54 +02:00
|
|
|
// +build example
|
2018-05-04 09:09:55 +02:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"image"
|
|
|
|
"image/color"
|
|
|
|
_ "image/jpeg"
|
|
|
|
"log"
|
|
|
|
"math"
|
|
|
|
|
|
|
|
"golang.org/x/image/font"
|
2020-10-03 16:12:39 +02:00
|
|
|
"golang.org/x/image/font/opentype"
|
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"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/text"
|
2018-05-04 09:09:55 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
gophersImage *ebiten.Image
|
|
|
|
mplusFont font.Face
|
|
|
|
regularTermination = errors.New("regular termination")
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2021-09-20 08:22:46 +02:00
|
|
|
// Decode an image from the image file's byte slice.
|
|
|
|
// Now the byte slice is generated with //go:generate for Go 1.15 or older.
|
2021-09-20 08:16:09 +02:00
|
|
|
// If you use Go 1.16 or newer, it is strongly recommended to use //go:embed to embed the image file.
|
|
|
|
// See https://pkg.go.dev/embed for more details.
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func initFont() {
|
2020-10-03 16:12:39 +02:00
|
|
|
tt, err := opentype.Parse(fonts.MPlus1pRegular_ttf)
|
2018-05-04 09:09:55 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
const dpi = 72
|
2020-10-03 16:12:39 +02:00
|
|
|
mplusFont, err = opentype.NewFace(tt, &opentype.FaceOptions{
|
2018-05-04 09:09:55 +02:00
|
|
|
Size: 12 * ebiten.DeviceScaleFactor(),
|
|
|
|
DPI: dpi,
|
|
|
|
Hinting: font.HintingFull,
|
|
|
|
})
|
2020-10-03 16:12:39 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
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
|
|
|
|
|
|
|
if ebiten.IsKeyPressed(ebiten.KeyQ) {
|
|
|
|
return regularTermination
|
|
|
|
}
|
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()
|
|
|
|
|
|
|
|
w, h := gophersImage.Size()
|
|
|
|
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)
|
2018-05-04 09:09:55 +02:00
|
|
|
sw, sh := screen.Size()
|
|
|
|
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()
|
2018-05-04 09:09:55 +02:00
|
|
|
msgs := []string{
|
|
|
|
"This is an example of the finest fullscreen. Press Q to quit.",
|
2018-10-09 16:27:29 +02:00
|
|
|
fmt.Sprintf("Screen size in fullscreen: %d, %d", fw, fh),
|
2018-05-04 09:09:55 +02:00
|
|
|
fmt.Sprintf("Game's screen size: %d, %d", sw, sh),
|
|
|
|
fmt.Sprintf("Device scale factor: %0.2f", scale),
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, msg := range msgs {
|
2018-10-11 18:11:14 +02:00
|
|
|
text.Draw(screen, msg, mplusFont, int(50*scale), int(50+float64(i)*16*scale), color.White)
|
2018-05-04 09:09:55 +02:00
|
|
|
}
|
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() {
|
|
|
|
// Call initFont here instead of init funcs since ebiten.DeviceScaleFactor is not available in init.
|
|
|
|
initFont()
|
|
|
|
|
|
|
|
ebiten.SetFullscreen(true)
|
2020-04-19 18:10:00 +02:00
|
|
|
ebiten.SetWindowTitle("Fullscreen (Ebiten Demo)")
|
|
|
|
if err := ebiten.RunGame(&Game{}); err != nil && err != regularTermination {
|
2018-05-04 09:09:55 +02:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|