2015-02-09 02:25:00 +01:00
|
|
|
// Copyright 2015 Hajime Hoshi
|
|
|
|
//
|
|
|
|
// 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
|
2016-08-25 17:40:39 +02:00
|
|
|
|
2015-02-09 02:25:00 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-03-14 17:16:41 +01:00
|
|
|
"bytes"
|
2019-11-30 15:22:23 +01:00
|
|
|
"flag"
|
2015-02-09 16:10:50 +01:00
|
|
|
"fmt"
|
2017-09-22 21:12:02 +02:00
|
|
|
"image"
|
2016-07-27 18:10:40 +02:00
|
|
|
_ "image/jpeg"
|
2015-02-09 02:25:00 +01:00
|
|
|
"log"
|
2017-08-02 16:37:50 +02:00
|
|
|
"math"
|
2017-09-22 21:12:02 +02:00
|
|
|
"math/rand"
|
2021-04-16 19:27:04 +02:00
|
|
|
"regexp"
|
2019-11-30 15:22:23 +01:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2017-09-22 21:12:02 +02:00
|
|
|
"time"
|
2016-02-15 17:13:04 +01:00
|
|
|
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/examples/resources/images"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
2015-02-09 02:25:00 +01:00
|
|
|
)
|
|
|
|
|
2019-11-30 15:22:23 +01:00
|
|
|
var (
|
2019-12-16 02:52:53 +01:00
|
|
|
flagFullscreen = flag.Bool("fullscreen", false, "fullscreen")
|
2020-03-21 11:26:28 +01:00
|
|
|
flagResizable = flag.Bool("resizable", false, "make the window resizable")
|
2019-11-30 16:07:41 +01:00
|
|
|
flagWindowPosition = flag.String("windowposition", "", "window position (e.g., 100,200)")
|
|
|
|
flagScreenTransparent = flag.Bool("screentransparent", false, "screen transparent")
|
2019-12-22 10:08:02 +01:00
|
|
|
flagAutoAdjusting = flag.Bool("autoadjusting", false, "make the game screen auto-adjusting")
|
2020-03-20 13:46:23 +01:00
|
|
|
flagFloating = flag.Bool("floating", false, "make the window floating")
|
2020-03-21 11:26:28 +01:00
|
|
|
flagMaximize = flag.Bool("maximize", false, "maximize the window")
|
2020-06-14 04:50:58 +02:00
|
|
|
flagVsync = flag.Bool("vsync", true, "enable vsync")
|
2020-02-09 15:03:03 +01:00
|
|
|
flagInitFocused = flag.Bool("initfocused", true, "whether the window is focused on start")
|
2021-04-16 19:27:04 +02:00
|
|
|
flagMinWindowSize = flag.String("minwindowsize", "", "minimum window size (e.g., 100x200)")
|
|
|
|
flagMaxWindowSize = flag.String("maxwindowsize", "", "maximium window size (e.g., 1920x1080)")
|
2019-11-30 15:22:23 +01:00
|
|
|
)
|
|
|
|
|
2017-09-22 21:12:02 +02:00
|
|
|
func init() {
|
2019-11-30 15:22:23 +01:00
|
|
|
flag.Parse()
|
2017-09-22 21:12:02 +02:00
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
}
|
|
|
|
|
2015-02-09 02:25:00 +01:00
|
|
|
const (
|
2019-10-14 16:32:38 +02:00
|
|
|
initScreenWidth = 480
|
|
|
|
initScreenHeight = 360
|
|
|
|
initScreenScale = 1
|
2015-02-09 02:25:00 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
gophersImage *ebiten.Image
|
2018-02-04 15:18:32 +01:00
|
|
|
count = 0
|
2015-02-09 02:25:00 +01:00
|
|
|
)
|
|
|
|
|
2017-09-22 21:12:02 +02:00
|
|
|
func createRandomIconImage() image.Image {
|
|
|
|
const size = 32
|
|
|
|
|
2021-02-07 13:16:09 +01:00
|
|
|
rf := float64(rand.Intn(0x100))
|
|
|
|
gf := float64(rand.Intn(0x100))
|
|
|
|
bf := float64(rand.Intn(0x100))
|
|
|
|
img := ebiten.NewImage(size, size)
|
|
|
|
pix := make([]byte, 4*size*size)
|
2017-09-22 21:12:02 +02:00
|
|
|
for j := 0; j < size; j++ {
|
|
|
|
for i := 0; i < size; i++ {
|
2021-02-07 13:16:09 +01:00
|
|
|
af := float64(i+j) / float64(2*size)
|
|
|
|
if af > 0 {
|
|
|
|
pix[4*(j*size+i)] = byte(rf * af)
|
|
|
|
pix[4*(j*size+i)+1] = byte(gf * af)
|
|
|
|
pix[4*(j*size+i)+2] = byte(bf * af)
|
|
|
|
pix[4*(j*size+i)+3] = byte(af * 0xff)
|
|
|
|
}
|
2017-09-22 21:12:02 +02:00
|
|
|
}
|
|
|
|
}
|
2021-02-07 13:16:09 +01:00
|
|
|
img.ReplacePixels(pix)
|
2017-09-22 21:12:02 +02:00
|
|
|
|
|
|
|
return img
|
|
|
|
}
|
|
|
|
|
2019-12-16 02:52:53 +01:00
|
|
|
type game struct {
|
2020-03-24 04:01:37 +01:00
|
|
|
width int
|
|
|
|
height int
|
|
|
|
transparent bool
|
2019-12-16 02:52:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *game) Layout(outsideWidth, outsideHeight int) (int, int) {
|
2019-12-22 10:08:02 +01:00
|
|
|
if *flagAutoAdjusting {
|
|
|
|
g.width, g.height = outsideWidth, outsideHeight
|
|
|
|
return outsideWidth, outsideHeight
|
|
|
|
}
|
2019-12-16 02:52:53 +01:00
|
|
|
// Ignore the outside size. This means that the offscreen is not adjusted with the outside world.
|
|
|
|
return g.width, g.height
|
|
|
|
}
|
|
|
|
|
2020-10-04 10:42:54 +02:00
|
|
|
func (g *game) Update() error {
|
2019-12-16 02:52:53 +01:00
|
|
|
var (
|
|
|
|
screenWidth int
|
|
|
|
screenHeight int
|
|
|
|
screenScale float64
|
|
|
|
)
|
2020-10-03 21:39:32 +02:00
|
|
|
screenWidth = g.width
|
|
|
|
screenHeight = g.height
|
|
|
|
if ww, wh := ebiten.WindowSize(); ww > 0 && wh > 0 {
|
|
|
|
screenScale = math.Min(float64(ww)/float64(g.width), float64(wh)/float64(g.height))
|
2019-12-16 02:52:53 +01:00
|
|
|
} else {
|
2020-10-03 21:39:32 +02:00
|
|
|
// ebiten.WindowSize can return (0, 0) on browsers or mobiles.
|
|
|
|
screenScale = 1
|
2019-12-16 02:52:53 +01:00
|
|
|
}
|
|
|
|
|
2017-06-29 17:35:34 +02:00
|
|
|
fullscreen := ebiten.IsFullscreen()
|
2020-03-20 16:34:12 +01:00
|
|
|
runnableOnUnfocused := ebiten.IsRunnableOnUnfocused()
|
2021-04-10 20:33:02 +02:00
|
|
|
cursorMode := ebiten.CursorMode()
|
2018-07-14 13:50:09 +02:00
|
|
|
vsyncEnabled := ebiten.IsVsyncEnabled()
|
2018-07-17 15:41:27 +02:00
|
|
|
tps := ebiten.MaxTPS()
|
2019-11-26 03:45:52 +01:00
|
|
|
decorated := ebiten.IsWindowDecorated()
|
2019-11-30 15:22:23 +01:00
|
|
|
positionX, positionY := ebiten.WindowPosition()
|
2020-03-24 04:01:37 +01:00
|
|
|
g.transparent = ebiten.IsScreenTransparent()
|
2020-03-20 13:46:23 +01:00
|
|
|
floating := ebiten.IsWindowFloating()
|
2019-12-22 10:08:02 +01:00
|
|
|
resizable := ebiten.IsWindowResizable()
|
2020-08-20 10:12:22 +02:00
|
|
|
screenCleared := ebiten.IsScreenClearedEveryFrame()
|
2017-06-29 17:35:34 +02:00
|
|
|
|
2019-12-16 02:52:53 +01:00
|
|
|
const d = 16
|
2019-12-22 10:08:02 +01:00
|
|
|
toUpdateWindowSize := false
|
2021-04-20 13:47:56 +02:00
|
|
|
toUpdateWindowPosition := false
|
2019-11-30 15:22:23 +01:00
|
|
|
if ebiten.IsKeyPressed(ebiten.KeyShift) {
|
2021-04-17 18:13:17 +02:00
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyArrowDown) {
|
2019-11-30 15:22:23 +01:00
|
|
|
screenHeight += d
|
2019-12-22 10:08:02 +01:00
|
|
|
toUpdateWindowSize = true
|
2015-02-09 16:10:50 +01:00
|
|
|
}
|
2021-04-17 18:13:17 +02:00
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyArrowUp) {
|
2019-11-30 15:22:23 +01:00
|
|
|
if 16 < screenHeight && d < screenHeight {
|
|
|
|
screenHeight -= d
|
2019-12-22 10:08:02 +01:00
|
|
|
toUpdateWindowSize = true
|
2019-11-30 15:22:23 +01:00
|
|
|
}
|
|
|
|
}
|
2021-04-17 18:13:17 +02:00
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyArrowLeft) {
|
2019-11-30 15:22:23 +01:00
|
|
|
if 16 < screenWidth && d < screenWidth {
|
|
|
|
screenWidth -= d
|
2019-12-22 10:08:02 +01:00
|
|
|
toUpdateWindowSize = true
|
2019-11-30 15:22:23 +01:00
|
|
|
}
|
|
|
|
}
|
2021-04-17 18:13:17 +02:00
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyArrowRight) {
|
2019-11-30 15:22:23 +01:00
|
|
|
screenWidth += d
|
2019-12-22 10:08:02 +01:00
|
|
|
toUpdateWindowSize = true
|
2019-11-30 15:22:23 +01:00
|
|
|
}
|
|
|
|
} else {
|
2021-04-17 18:13:17 +02:00
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyArrowUp) {
|
2019-12-21 09:08:04 +01:00
|
|
|
positionY -= d
|
2021-04-20 13:47:56 +02:00
|
|
|
toUpdateWindowPosition = true
|
2019-11-30 15:22:23 +01:00
|
|
|
}
|
2021-04-17 18:13:17 +02:00
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyArrowDown) {
|
2019-12-21 09:08:04 +01:00
|
|
|
positionY += d
|
2021-04-20 13:47:56 +02:00
|
|
|
toUpdateWindowPosition = true
|
2019-11-30 15:22:23 +01:00
|
|
|
}
|
2021-04-17 18:13:17 +02:00
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyArrowLeft) {
|
2019-12-21 09:08:04 +01:00
|
|
|
positionX -= d
|
2021-04-20 13:47:56 +02:00
|
|
|
toUpdateWindowPosition = true
|
2019-11-30 15:22:23 +01:00
|
|
|
}
|
2021-04-17 18:13:17 +02:00
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyArrowRight) {
|
2019-12-21 09:08:04 +01:00
|
|
|
positionX += d
|
2021-04-20 13:47:56 +02:00
|
|
|
toUpdateWindowPosition = true
|
2015-02-09 16:10:50 +01:00
|
|
|
}
|
2015-02-09 02:25:00 +01:00
|
|
|
}
|
2019-12-22 10:08:02 +01:00
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyS) && !*flagAutoAdjusting {
|
|
|
|
switch {
|
|
|
|
case screenScale < 1:
|
2018-06-02 19:32:42 +02:00
|
|
|
screenScale = 1
|
2019-12-22 10:08:02 +01:00
|
|
|
case screenScale < 1.5:
|
2016-06-24 19:17:08 +02:00
|
|
|
screenScale = 1.5
|
2019-12-22 10:08:02 +01:00
|
|
|
case screenScale < 2:
|
2016-06-24 19:17:08 +02:00
|
|
|
screenScale = 2
|
|
|
|
default:
|
2019-12-22 10:08:02 +01:00
|
|
|
screenScale = 0.75
|
2016-06-24 19:17:08 +02:00
|
|
|
}
|
2019-12-22 10:08:02 +01:00
|
|
|
toUpdateWindowSize = true
|
2015-02-09 16:10:50 +01:00
|
|
|
}
|
2018-02-04 15:18:32 +01:00
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyF) {
|
2017-06-29 17:35:34 +02:00
|
|
|
fullscreen = !fullscreen
|
|
|
|
}
|
2020-03-20 16:34:12 +01:00
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyU) {
|
|
|
|
runnableOnUnfocused = !runnableOnUnfocused
|
2017-08-02 16:37:50 +02:00
|
|
|
}
|
2018-02-04 15:18:32 +01:00
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyC) {
|
2021-04-10 20:33:02 +02:00
|
|
|
switch cursorMode {
|
|
|
|
case ebiten.CursorModeVisible:
|
|
|
|
cursorMode = ebiten.CursorModeHidden
|
|
|
|
case ebiten.CursorModeHidden:
|
|
|
|
cursorMode = ebiten.CursorModeCaptured
|
|
|
|
case ebiten.CursorModeCaptured:
|
|
|
|
cursorMode = ebiten.CursorModeVisible
|
|
|
|
}
|
2017-08-12 08:39:41 +02:00
|
|
|
}
|
2018-07-14 13:50:09 +02:00
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyV) {
|
|
|
|
vsyncEnabled = !vsyncEnabled
|
|
|
|
}
|
2018-07-16 18:42:19 +02:00
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyT) {
|
|
|
|
switch tps {
|
2018-07-17 15:36:55 +02:00
|
|
|
case ebiten.UncappedTPS:
|
2018-07-16 18:42:19 +02:00
|
|
|
tps = 30
|
|
|
|
case 30:
|
|
|
|
tps = 60
|
|
|
|
case 60:
|
|
|
|
tps = 120
|
|
|
|
case 120:
|
2018-07-17 15:36:55 +02:00
|
|
|
tps = ebiten.UncappedTPS
|
2018-07-16 18:42:19 +02:00
|
|
|
default:
|
|
|
|
panic("not reached")
|
|
|
|
}
|
|
|
|
}
|
2019-11-26 03:45:52 +01:00
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyD) {
|
|
|
|
decorated = !decorated
|
|
|
|
}
|
2020-03-20 13:46:23 +01:00
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyL) {
|
|
|
|
floating = !floating
|
|
|
|
}
|
2019-12-22 10:08:02 +01:00
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyR) {
|
|
|
|
resizable = !resizable
|
|
|
|
}
|
2020-08-14 20:14:32 +02:00
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyW) {
|
2020-08-20 10:12:22 +02:00
|
|
|
screenCleared = !screenCleared
|
2020-08-14 20:14:32 +02:00
|
|
|
}
|
2020-03-21 11:26:28 +01:00
|
|
|
maximize := inpututil.IsKeyJustPressed(ebiten.KeyM)
|
2020-03-28 07:28:56 +01:00
|
|
|
minimize := inpututil.IsKeyJustPressed(ebiten.KeyN)
|
2020-03-30 19:48:19 +02:00
|
|
|
restore := false
|
|
|
|
if ebiten.IsWindowMaximized() || ebiten.IsWindowMinimized() {
|
|
|
|
restore = inpututil.IsKeyJustPressed(ebiten.KeyE)
|
|
|
|
}
|
2018-07-14 13:50:09 +02:00
|
|
|
|
2019-12-22 10:08:02 +01:00
|
|
|
if toUpdateWindowSize {
|
2020-10-03 21:39:32 +02:00
|
|
|
g.width = screenWidth
|
|
|
|
g.height = screenHeight
|
|
|
|
ebiten.SetWindowSize(int(float64(screenWidth)*screenScale), int(float64(screenHeight)*screenScale))
|
2019-12-16 02:52:53 +01:00
|
|
|
}
|
2017-06-29 17:35:34 +02:00
|
|
|
ebiten.SetFullscreen(fullscreen)
|
2020-03-20 16:34:12 +01:00
|
|
|
ebiten.SetRunnableOnUnfocused(runnableOnUnfocused)
|
2021-04-10 20:33:02 +02:00
|
|
|
ebiten.SetCursorMode(cursorMode)
|
2020-10-13 18:55:32 +02:00
|
|
|
|
|
|
|
// Set vsync enabled only when this is needed.
|
|
|
|
// This makes a bug around vsync initialization more explicit (#1364).
|
|
|
|
if vsyncEnabled != ebiten.IsVsyncEnabled() {
|
|
|
|
ebiten.SetVsyncEnabled(vsyncEnabled)
|
|
|
|
}
|
2018-07-17 15:33:53 +02:00
|
|
|
ebiten.SetMaxTPS(tps)
|
2019-11-26 03:45:52 +01:00
|
|
|
ebiten.SetWindowDecorated(decorated)
|
2021-04-20 13:47:56 +02:00
|
|
|
if toUpdateWindowPosition {
|
|
|
|
ebiten.SetWindowPosition(positionX, positionY)
|
|
|
|
}
|
2020-03-20 13:46:23 +01:00
|
|
|
ebiten.SetWindowFloating(floating)
|
2020-08-20 10:12:22 +02:00
|
|
|
ebiten.SetScreenClearedEveryFrame(screenCleared)
|
2020-03-28 14:00:40 +01:00
|
|
|
if maximize && ebiten.IsWindowResizable() {
|
2020-03-21 11:26:28 +01:00
|
|
|
ebiten.MaximizeWindow()
|
|
|
|
}
|
|
|
|
if minimize {
|
|
|
|
ebiten.MinimizeWindow()
|
|
|
|
}
|
|
|
|
if restore {
|
|
|
|
ebiten.RestoreWindow()
|
|
|
|
}
|
2020-10-03 21:39:32 +02:00
|
|
|
ebiten.SetWindowResizable(resizable)
|
2017-08-02 16:37:50 +02:00
|
|
|
|
2018-02-04 15:18:32 +01:00
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyI) {
|
2017-09-23 10:40:09 +02:00
|
|
|
ebiten.SetWindowIcon([]image.Image{createRandomIconImage()})
|
2017-09-22 21:12:02 +02:00
|
|
|
}
|
|
|
|
|
2017-08-02 16:37:50 +02:00
|
|
|
count++
|
2020-03-24 04:01:37 +01:00
|
|
|
return nil
|
|
|
|
}
|
2015-02-09 02:25:00 +01:00
|
|
|
|
2020-03-24 04:01:37 +01:00
|
|
|
func (g *game) Draw(screen *ebiten.Image) {
|
2015-02-09 02:25:00 +01:00
|
|
|
w, h := gophersImage.Size()
|
|
|
|
w2, h2 := screen.Size()
|
|
|
|
op := &ebiten.DrawImageOptions{}
|
|
|
|
op.GeoM.Translate(float64(-w+w2)/2, float64(-h+h2)/2)
|
2020-08-14 20:14:32 +02:00
|
|
|
dx := math.Cos(2*math.Pi*float64(count)/360) * 20
|
|
|
|
dy := math.Sin(2*math.Pi*float64(count)/360) * 20
|
2017-08-02 16:37:50 +02:00
|
|
|
op.GeoM.Translate(dx, dy)
|
2017-03-04 15:00:19 +01:00
|
|
|
screen.DrawImage(gophersImage, op)
|
2015-02-09 02:25:00 +01:00
|
|
|
|
2019-11-30 14:37:53 +01:00
|
|
|
wx, wy := ebiten.WindowPosition()
|
2021-04-16 19:27:04 +02:00
|
|
|
minw, minh, maxw, maxh := ebiten.WindowSizeLimits()
|
2019-11-30 14:37:53 +01:00
|
|
|
cx, cy := ebiten.CursorPosition()
|
2018-07-17 15:33:53 +02:00
|
|
|
tpsStr := "Uncapped"
|
2018-07-17 19:11:00 +02:00
|
|
|
if t := ebiten.MaxTPS(); t != ebiten.UncappedTPS {
|
|
|
|
tpsStr = fmt.Sprintf("%d", t)
|
2018-07-17 15:33:53 +02:00
|
|
|
}
|
2019-12-22 10:08:02 +01:00
|
|
|
|
2020-03-21 11:26:28 +01:00
|
|
|
var lines []string
|
2020-03-28 14:00:40 +01:00
|
|
|
if !ebiten.IsWindowMaximized() && ebiten.IsWindowResizable() {
|
2020-03-28 18:36:10 +01:00
|
|
|
lines = append(lines, "[M] Maximize the window (only for desktops)")
|
2020-03-21 11:26:28 +01:00
|
|
|
}
|
|
|
|
if !ebiten.IsWindowMinimized() {
|
2020-03-28 18:36:10 +01:00
|
|
|
lines = append(lines, "[N] Minimize the window (only for desktops)")
|
2020-03-21 11:26:28 +01:00
|
|
|
}
|
|
|
|
if ebiten.IsWindowMaximized() || ebiten.IsWindowMinimized() {
|
2020-03-28 18:36:10 +01:00
|
|
|
lines = append(lines, "[E] Restore the window from maximized/minimized state (only for desktops)")
|
2020-03-21 11:26:28 +01:00
|
|
|
}
|
|
|
|
msgM := strings.Join(lines, "\n")
|
|
|
|
|
2020-10-03 21:39:32 +02:00
|
|
|
msgR := "[R] Switch the window resizable state (only for desktops)\n"
|
2020-01-21 15:34:01 +01:00
|
|
|
fg := "Yes"
|
2020-03-20 16:08:40 +01:00
|
|
|
if !ebiten.IsFocused() {
|
2020-01-21 15:34:01 +01:00
|
|
|
fg = "No"
|
|
|
|
}
|
2019-12-22 10:08:02 +01:00
|
|
|
|
2020-03-28 07:27:15 +01:00
|
|
|
msg := fmt.Sprintf(`[Arrow keys] Move the window
|
|
|
|
[Shift + Arrow keys] Change the window size
|
2020-10-10 16:00:49 +02:00
|
|
|
%s
|
|
|
|
[F] Switch the fullscreen state (only for desktops)
|
2020-03-28 07:27:15 +01:00
|
|
|
[U] Switch the runnable-on-unfocused state
|
2021-04-10 20:33:02 +02:00
|
|
|
[C] Switch the cursor mode (visible, hidden, or captured)
|
2020-03-28 07:27:15 +01:00
|
|
|
[I] Change the window icon (only for desktops)
|
|
|
|
[V] Switch vsync
|
|
|
|
[T] Switch TPS (ticks per second)
|
|
|
|
[D] Switch the window decoration (only for desktops)
|
|
|
|
[L] Switch the window floating state (only for desktops)
|
2020-08-14 20:14:32 +02:00
|
|
|
[W] Switch whether to skip clearing the screen
|
2020-01-21 15:34:01 +01:00
|
|
|
%s
|
2020-03-20 16:08:40 +01:00
|
|
|
IsFocused?: %s
|
2020-01-21 15:34:01 +01:00
|
|
|
Windows Position: (%d, %d)
|
2021-04-16 19:27:04 +02:00
|
|
|
Window size limitation: (%d, %d) - (%d, %d)
|
2015-02-18 18:33:15 +01:00
|
|
|
Cursor: (%d, %d)
|
2018-09-29 19:27:33 +02:00
|
|
|
TPS: Current: %0.2f / Max: %s
|
2018-10-14 12:15:26 +02:00
|
|
|
FPS: %0.2f
|
2021-04-16 19:27:04 +02:00
|
|
|
Device Scale Factor: %0.2f`, msgM, msgR, fg, wx, wy, minw, minh, maxw, maxh, cx, cy, ebiten.CurrentTPS(), tpsStr, ebiten.CurrentFPS(), ebiten.DeviceScaleFactor())
|
2017-03-04 15:00:19 +01:00
|
|
|
ebitenutil.DebugPrint(screen, msg)
|
2015-02-09 02:25:00 +01:00
|
|
|
}
|
|
|
|
|
2019-11-30 15:22:23 +01:00
|
|
|
func parseWindowPosition() (int, int, bool) {
|
|
|
|
if *flagWindowPosition == "" {
|
|
|
|
return 0, 0, false
|
|
|
|
}
|
|
|
|
tokens := strings.Split(*flagWindowPosition, ",")
|
|
|
|
if len(tokens) != 2 {
|
|
|
|
return 0, 0, false
|
|
|
|
}
|
|
|
|
x, err := strconv.Atoi(tokens[0])
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, false
|
|
|
|
}
|
|
|
|
y, err := strconv.Atoi(tokens[1])
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, false
|
|
|
|
}
|
|
|
|
return x, y, true
|
|
|
|
}
|
|
|
|
|
2015-02-09 02:25:00 +01:00
|
|
|
func main() {
|
2018-01-03 11:23:29 +01:00
|
|
|
fmt.Printf("Device scale factor: %0.2f\n", ebiten.DeviceScaleFactor())
|
2018-10-09 16:27:29 +02:00
|
|
|
w, h := ebiten.ScreenSizeInFullscreen()
|
|
|
|
fmt.Printf("Screen size in fullscreen: %d, %d\n", w, h)
|
2018-01-02 21:25:22 +01:00
|
|
|
|
2018-03-16 04:05:53 +01:00
|
|
|
// 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.
|
2018-03-14 17:16:41 +01:00
|
|
|
img, _, err := image.Decode(bytes.NewReader(images.Gophers_jpg))
|
2015-02-09 02:25:00 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-10-05 18:03:30 +02:00
|
|
|
gophersImage = ebiten.NewImageFromImage(img)
|
2017-09-22 21:12:02 +02:00
|
|
|
|
2017-09-23 10:40:09 +02:00
|
|
|
ebiten.SetWindowIcon([]image.Image{createRandomIconImage()})
|
2017-09-22 21:12:02 +02:00
|
|
|
|
2019-11-30 15:22:23 +01:00
|
|
|
if x, y, ok := parseWindowPosition(); ok {
|
|
|
|
ebiten.SetWindowPosition(x, y)
|
|
|
|
}
|
2019-11-30 16:07:41 +01:00
|
|
|
ebiten.SetScreenTransparent(*flagScreenTransparent)
|
2019-11-30 15:22:23 +01:00
|
|
|
|
2019-12-16 02:52:53 +01:00
|
|
|
g := &game{
|
|
|
|
width: initScreenWidth,
|
|
|
|
height: initScreenHeight,
|
|
|
|
}
|
|
|
|
|
|
|
|
if *flagFullscreen {
|
|
|
|
ebiten.SetFullscreen(true)
|
|
|
|
}
|
2020-03-21 11:26:28 +01:00
|
|
|
if *flagResizable {
|
|
|
|
ebiten.SetWindowResizable(true)
|
|
|
|
}
|
2020-03-20 13:46:23 +01:00
|
|
|
if *flagFloating {
|
|
|
|
ebiten.SetWindowFloating(true)
|
|
|
|
}
|
2020-03-21 11:26:28 +01:00
|
|
|
if *flagMaximize {
|
2020-03-28 14:00:40 +01:00
|
|
|
ebiten.SetWindowResizable(true)
|
2020-03-21 11:26:28 +01:00
|
|
|
ebiten.MaximizeWindow()
|
|
|
|
}
|
2020-06-14 04:50:58 +02:00
|
|
|
ebiten.SetVsyncEnabled(*flagVsync)
|
2019-12-22 10:08:02 +01:00
|
|
|
if *flagAutoAdjusting {
|
|
|
|
ebiten.SetWindowResizable(true)
|
|
|
|
}
|
2019-12-16 02:52:53 +01:00
|
|
|
|
2020-02-09 15:03:03 +01:00
|
|
|
ebiten.SetInitFocused(*flagInitFocused)
|
|
|
|
if !*flagInitFocused {
|
|
|
|
ebiten.SetRunnableOnUnfocused(true)
|
|
|
|
}
|
|
|
|
|
2021-04-16 19:27:04 +02:00
|
|
|
minw, minh, maxw, maxh := -1, -1, -1, -1
|
|
|
|
reSize := regexp.MustCompile(`^(\d+)x(\d+)$`)
|
|
|
|
if m := reSize.FindStringSubmatch(*flagMinWindowSize); m != nil {
|
|
|
|
minw, _ = strconv.Atoi(m[1])
|
|
|
|
minh, _ = strconv.Atoi(m[2])
|
|
|
|
}
|
|
|
|
if m := reSize.FindStringSubmatch(*flagMaxWindowSize); m != nil {
|
|
|
|
maxw, _ = strconv.Atoi(m[1])
|
|
|
|
maxh, _ = strconv.Atoi(m[2])
|
|
|
|
}
|
2021-04-18 16:21:34 +02:00
|
|
|
if minw >= 0 || minh >= 0 || maxw >= 0 || maxh >= 0 {
|
2021-04-16 19:27:04 +02:00
|
|
|
ebiten.SetWindowSizeLimits(minw, minh, maxw, maxh)
|
|
|
|
ebiten.SetWindowResizable(true)
|
|
|
|
}
|
|
|
|
|
2019-12-16 02:52:53 +01:00
|
|
|
const title = "Window Size (Ebiten Demo)"
|
2020-10-03 21:39:32 +02:00
|
|
|
ww := int(float64(g.width) * initScreenScale)
|
|
|
|
wh := int(float64(g.height) * initScreenScale)
|
|
|
|
ebiten.SetWindowSize(ww, wh)
|
|
|
|
ebiten.SetWindowTitle(title)
|
|
|
|
if err := ebiten.RunGame(g); err != nil {
|
|
|
|
log.Fatal(err)
|
2015-02-09 02:25:00 +01:00
|
|
|
}
|
|
|
|
}
|