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.
|
|
|
|
|
2018-03-14 17:26:21 +01:00
|
|
|
// +build example jsgo
|
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"
|
2015-02-09 16:10:50 +01:00
|
|
|
"image/color"
|
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"
|
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
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten"
|
|
|
|
"github.com/hajimehoshi/ebiten/ebitenutil"
|
2018-03-14 17:16:41 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/examples/resources/images"
|
2018-02-04 15:18:32 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/inpututil"
|
2015-02-09 02:25:00 +01:00
|
|
|
)
|
|
|
|
|
2019-11-30 15:22:23 +01:00
|
|
|
var (
|
|
|
|
flagWindowPosition = flag.String("windowposition", "", "window position (e.g., 100,200)")
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
|
2019-10-14 16:32:38 +02:00
|
|
|
r := byte(rand.Intn(0x100))
|
|
|
|
g := byte(rand.Intn(0x100))
|
|
|
|
b := byte(rand.Intn(0x100))
|
2017-09-22 21:12:02 +02:00
|
|
|
img := image.NewNRGBA(image.Rect(0, 0, size, size))
|
|
|
|
for j := 0; j < size; j++ {
|
|
|
|
for i := 0; i < size; i++ {
|
|
|
|
img.Pix[j*img.Stride+4*i] = r
|
|
|
|
img.Pix[j*img.Stride+4*i+1] = g
|
|
|
|
img.Pix[j*img.Stride+4*i+2] = b
|
2019-10-14 16:32:38 +02:00
|
|
|
img.Pix[j*img.Stride+4*i+3] = byte(float64(i+j) / float64(2*size) * 0xff)
|
2017-09-22 21:12:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return img
|
|
|
|
}
|
|
|
|
|
2015-02-09 02:25:00 +01:00
|
|
|
func update(screen *ebiten.Image) error {
|
2016-03-22 16:44:16 +01:00
|
|
|
screenScale := ebiten.ScreenScale()
|
2018-10-14 10:34:02 +02:00
|
|
|
const d = 16
|
2016-03-21 17:36:04 +01:00
|
|
|
screenWidth, screenHeight := screen.Size()
|
2017-06-29 17:35:34 +02:00
|
|
|
fullscreen := ebiten.IsFullscreen()
|
2017-08-02 16:37:50 +02:00
|
|
|
runnableInBackground := ebiten.IsRunnableInBackground()
|
2017-08-12 08:39:41 +02:00
|
|
|
cursorVisible := ebiten.IsCursorVisible()
|
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()
|
2017-06-29 17:35:34 +02:00
|
|
|
|
2019-11-30 15:22:23 +01:00
|
|
|
if ebiten.IsKeyPressed(ebiten.KeyShift) {
|
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyUp) {
|
|
|
|
screenHeight += d
|
2015-02-09 16:10:50 +01:00
|
|
|
}
|
2019-11-30 15:22:23 +01:00
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyDown) {
|
|
|
|
if 16 < screenHeight && d < screenHeight {
|
|
|
|
screenHeight -= d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyLeft) {
|
|
|
|
if 16 < screenWidth && d < screenWidth {
|
|
|
|
screenWidth -= d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyRight) {
|
|
|
|
screenWidth += d
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyUp) {
|
|
|
|
positionY -= 4
|
|
|
|
}
|
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyDown) {
|
|
|
|
positionY += 4
|
|
|
|
}
|
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyLeft) {
|
|
|
|
positionX -= 4
|
|
|
|
}
|
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyRight) {
|
|
|
|
positionX += 4
|
2015-02-09 16:10:50 +01:00
|
|
|
}
|
2015-02-09 02:25:00 +01:00
|
|
|
}
|
2018-02-04 15:18:32 +01:00
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyS) {
|
2016-06-24 19:17:08 +02:00
|
|
|
switch screenScale {
|
2018-06-02 19:32:42 +02:00
|
|
|
case 0.75:
|
|
|
|
screenScale = 1
|
2016-06-24 19:17:08 +02:00
|
|
|
case 1:
|
|
|
|
screenScale = 1.5
|
|
|
|
case 1.5:
|
|
|
|
screenScale = 2
|
|
|
|
case 2:
|
2018-06-02 19:32:42 +02:00
|
|
|
screenScale = 0.75
|
2016-06-24 19:17:08 +02:00
|
|
|
default:
|
2017-08-12 08:39:41 +02:00
|
|
|
panic("not reached")
|
2016-06-24 19:17:08 +02:00
|
|
|
}
|
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
|
|
|
|
}
|
2018-02-04 15:18:32 +01:00
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyB) {
|
2017-08-02 16:37:50 +02:00
|
|
|
runnableInBackground = !runnableInBackground
|
|
|
|
}
|
2018-02-04 15:18:32 +01:00
|
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyC) {
|
2017-08-12 08:39:41 +02:00
|
|
|
cursorVisible = !cursorVisible
|
|
|
|
}
|
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
|
|
|
|
}
|
2018-07-14 13:50:09 +02:00
|
|
|
|
2015-02-09 02:25:00 +01:00
|
|
|
ebiten.SetScreenSize(screenWidth, screenHeight)
|
2019-10-27 09:13:17 +01:00
|
|
|
ebiten.SetScreenScale(screenScale)
|
2017-06-29 17:35:34 +02:00
|
|
|
ebiten.SetFullscreen(fullscreen)
|
2017-08-02 16:37:50 +02:00
|
|
|
ebiten.SetRunnableInBackground(runnableInBackground)
|
2018-01-08 18:01:33 +01:00
|
|
|
ebiten.SetCursorVisible(cursorVisible)
|
2018-07-14 13:50:09 +02:00
|
|
|
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)
|
2019-11-30 15:22:23 +01:00
|
|
|
ebiten.SetWindowPosition(positionX, positionY)
|
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++
|
2015-02-09 02:25:00 +01:00
|
|
|
|
2018-07-09 17:36:43 +02:00
|
|
|
if ebiten.IsDrawingSkipped() {
|
2017-05-16 03:35:58 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-04 15:00:19 +01:00
|
|
|
screen.Fill(color.RGBA{0x80, 0x80, 0xc0, 0xff})
|
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)
|
2017-08-02 16:37:50 +02:00
|
|
|
dx := math.Cos(2*math.Pi*float64(count)/360) * 10
|
|
|
|
dy := math.Sin(2*math.Pi*float64(count)/360) * 10
|
|
|
|
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()
|
|
|
|
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-11-30 15:22:23 +01:00
|
|
|
msg := fmt.Sprintf(`Press shift + arrow keys to change the window size
|
2019-10-14 16:32:38 +02:00
|
|
|
Press S key to change the window scale (only for desktops)
|
|
|
|
Press F key to switch the fullscreen state (only for desktops)
|
2017-08-12 08:39:41 +02:00
|
|
|
Press B key to switch the run-in-background state
|
|
|
|
Press C key to switch the cursor visibility
|
2019-10-14 16:32:38 +02:00
|
|
|
Press I key to change the window icon (only for desktops)
|
2018-07-14 13:50:09 +02:00
|
|
|
Press V key to switch vsync
|
2018-07-16 18:42:19 +02:00
|
|
|
Press T key to switch TPS (ticks per second)
|
2019-11-26 03:45:52 +01:00
|
|
|
Press D key to switch the window decoration
|
2019-11-30 14:37:53 +01:00
|
|
|
Windows Position: (%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
|
2019-11-30 14:37:53 +01:00
|
|
|
Device Scale Factor: %0.2f`, wx, wy, 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
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
2018-03-14 17:16:41 +01:00
|
|
|
gophersImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
|
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-10-14 16:49:39 +02:00
|
|
|
if err := ebiten.Run(update, initScreenWidth, initScreenHeight, initScreenScale, "Window Size (Ebiten Demo)"); err != nil {
|
2015-02-09 02:25:00 +01:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|