ebiten/examples/windowsize/main.go

195 lines
5.1 KiB
Go
Raw Normal View History

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.
// +build example jsgo
2015-02-09 02:25:00 +01:00
package main
import (
"bytes"
"errors"
"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"
"math"
2017-09-22 21:12:02 +02:00
"math/rand"
"time"
2016-02-15 17:13:04 +01:00
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/examples/resources/images"
"github.com/hajimehoshi/ebiten/inpututil"
2015-02-09 02:25:00 +01:00
)
var (
windowDecorated = flag.Bool("windowdecorated", true, "whether the window is decorated")
)
2017-09-22 21:12:02 +02:00
func init() {
rand.Seed(time.Now().UnixNano())
}
2015-02-09 02:25:00 +01:00
const (
initScreenWidth = 320
initScreenHeight = 240
2015-02-09 16:10:50 +01:00
initScreenScale = 2
2015-02-09 02:25:00 +01:00
)
var (
gophersImage *ebiten.Image
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
r := uint8(rand.Intn(0x100))
g := uint8(rand.Intn(0x100))
b := uint8(rand.Intn(0x100))
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
img.Pix[j*img.Stride+4*i+3] = uint8(float64(i+j) / float64(2*size) * 0xff)
}
}
return img
}
var terminated = errors.New("terminated")
2015-02-09 02:25:00 +01:00
func update(screen *ebiten.Image) error {
if inpututil.IsKeyJustPressed(ebiten.KeyQ) {
return terminated
}
2016-03-22 16:44:16 +01:00
screenScale := ebiten.ScreenScale()
d := int(32 / screenScale)
screenWidth, screenHeight := screen.Size()
fullscreen := ebiten.IsFullscreen()
runnableInBackground := ebiten.IsRunnableInBackground()
2017-08-12 08:39:41 +02:00
cursorVisible := ebiten.IsCursorVisible()
if inpututil.IsKeyJustPressed(ebiten.KeyUp) {
2015-02-18 18:33:15 +01:00
screenHeight += d
2015-02-09 02:25:00 +01:00
}
if inpututil.IsKeyJustPressed(ebiten.KeyDown) {
if 16 < screenHeight && d < screenHeight {
2015-02-18 18:33:15 +01:00
screenHeight -= d
2015-02-09 16:10:50 +01:00
}
2015-02-09 02:25:00 +01:00
}
if inpututil.IsKeyJustPressed(ebiten.KeyLeft) {
if 16 < screenWidth && d < screenWidth {
2015-02-18 18:33:15 +01:00
screenWidth -= d
2015-02-09 16:10:50 +01:00
}
2015-02-09 02:25:00 +01:00
}
if inpututil.IsKeyJustPressed(ebiten.KeyRight) {
2015-02-18 18:33:15 +01:00
screenWidth += d
2015-02-09 02:25:00 +01:00
}
if inpututil.IsKeyJustPressed(ebiten.KeyS) {
switch screenScale {
case 1:
screenScale = 1.5
case 1.5:
screenScale = 2
case 2:
screenScale = 1
default:
2017-08-12 08:39:41 +02:00
panic("not reached")
}
2015-02-09 16:10:50 +01:00
}
if inpututil.IsKeyJustPressed(ebiten.KeyF) {
fullscreen = !fullscreen
}
if inpututil.IsKeyJustPressed(ebiten.KeyB) {
runnableInBackground = !runnableInBackground
}
if inpututil.IsKeyJustPressed(ebiten.KeyC) {
2017-08-12 08:39:41 +02:00
cursorVisible = !cursorVisible
}
2015-02-09 02:25:00 +01:00
ebiten.SetScreenSize(screenWidth, screenHeight)
2015-02-09 16:10:50 +01:00
ebiten.SetScreenScale(screenScale)
ebiten.SetFullscreen(fullscreen)
ebiten.SetRunnableInBackground(runnableInBackground)
ebiten.SetCursorVisible(cursorVisible)
if inpututil.IsKeyJustPressed(ebiten.KeyI) {
ebiten.SetWindowIcon([]image.Image{createRandomIconImage()})
2017-09-22 21:12:02 +02:00
}
count++
2015-02-09 02:25:00 +01:00
2017-05-16 03:35:58 +02:00
if ebiten.IsRunningSlowly() {
return nil
}
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)
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)
screen.DrawImage(gophersImage, op)
2015-02-09 02:25:00 +01:00
2015-02-09 16:10:50 +01:00
x, y := ebiten.CursorPosition()
msg := fmt.Sprintf(`Press arrow keys to change the window size
Press S key to change the window scale
2017-08-12 08:39:41 +02:00
Press F key to switch the fullscreen state
Press B key to switch the run-in-background state
Press C key to switch the cursor visibility
2017-09-22 21:12:02 +02:00
Press I key to change the window icon
Press Q key to quit
2015-02-18 18:33:15 +01:00
Cursor: (%d, %d)
FPS: %0.2f`, x, y, ebiten.CurrentFPS())
ebitenutil.DebugPrint(screen, msg)
2015-02-09 02:25:00 +01:00
return nil
}
func main() {
flag.Parse()
fmt.Printf("Device scale factor: %0.2f\n", ebiten.DeviceScaleFactor())
2018-01-02 21:25:22 +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.
img, _, err := image.Decode(bytes.NewReader(images.Gophers_jpg))
2015-02-09 02:25:00 +01:00
if err != nil {
log.Fatal(err)
}
gophersImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
2017-09-22 21:12:02 +02:00
ebiten.SetWindowIcon([]image.Image{createRandomIconImage()})
2017-09-22 21:12:02 +02:00
ebiten.SetWindowDecorated(*windowDecorated)
if err := ebiten.Run(update, initScreenWidth, initScreenHeight, initScreenScale, "Window Size (Ebiten Demo)"); err != nil && err != terminated {
2015-02-09 02:25:00 +01:00
log.Fatal(err)
}
}