ebiten/examples/windowsize/main.go

126 lines
2.9 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
2015-02-09 02:25:00 +01:00
package main
import (
2015-02-09 16:10:50 +01:00
"fmt"
"image/color"
2016-07-27 18:10:40 +02:00
_ "image/jpeg"
2015-02-09 02:25:00 +01:00
"log"
2016-02-15 17:13:04 +01:00
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
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
keyStates = map[ebiten.Key]int{
ebiten.KeyUp: 0,
ebiten.KeyDown: 0,
ebiten.KeyLeft: 0,
ebiten.KeyRight: 0,
2015-02-09 16:10:50 +01:00
ebiten.KeyS: 0,
ebiten.KeyF: 0,
2015-02-09 02:25:00 +01:00
}
)
func update(screen *ebiten.Image) error {
2016-05-13 17:25:11 +02:00
for key := range keyStates {
2015-02-09 02:25:00 +01:00
if !ebiten.IsKeyPressed(key) {
keyStates[key] = 0
continue
}
keyStates[key]++
}
2016-03-22 16:44:16 +01:00
screenScale := ebiten.ScreenScale()
d := int(32 / screenScale)
screenWidth, screenHeight := screen.Size()
fullscreen := ebiten.IsFullscreen()
2015-02-09 02:25:00 +01:00
if keyStates[ebiten.KeyUp] == 1 {
2015-02-18 18:33:15 +01:00
screenHeight += d
2015-02-09 02:25:00 +01:00
}
if keyStates[ebiten.KeyDown] == 1 {
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 keyStates[ebiten.KeyLeft] == 1 {
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 keyStates[ebiten.KeyRight] == 1 {
2015-02-18 18:33:15 +01:00
screenWidth += d
2015-02-09 02:25:00 +01:00
}
2015-02-09 16:10:50 +01:00
if keyStates[ebiten.KeyS] == 1 {
switch screenScale {
case 1:
screenScale = 1.5
case 1.5:
screenScale = 2
case 2:
screenScale = 1
default:
panic("not reach")
}
2015-02-09 16:10:50 +01:00
}
if keyStates[ebiten.KeyF] == 1 {
fullscreen = !fullscreen
}
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)
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)
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
Press F key to change the fullscreen state
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() {
var err error
2016-07-27 18:10:40 +02:00
gophersImage, _, err = ebitenutil.NewImageFromFile("_resources/images/gophers.jpg", ebiten.FilterNearest)
2015-02-09 02:25:00 +01:00
if err != nil {
log.Fatal(err)
}
2015-02-09 16:10:50 +01: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)
}
}