ebiten/example/windowsize/main.go

112 lines
2.6 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.
package main
import (
2015-02-09 16:10:50 +01:00
"fmt"
2015-02-09 02:25:00 +01:00
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
2015-02-09 16:10:50 +01:00
"image/color"
2015-02-09 02:25:00 +01:00
_ "image/jpeg"
"log"
)
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
screenWidth = initScreenWidth
screenHeight = initScreenHeight
2015-02-09 16:10:50 +01:00
screenScale = initScreenScale
2015-02-09 02:25:00 +01:00
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,
2015-02-09 02:25:00 +01:00
}
)
func update(screen *ebiten.Image) error {
for key, _ := range keyStates {
if !ebiten.IsKeyPressed(key) {
keyStates[key] = 0
continue
}
keyStates[key]++
}
2015-02-18 18:33:15 +01:00
d := 0
switch screenScale {
case 1:
d = 32
case 2:
d = 16
}
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 {
2015-02-09 16:10:50 +01:00
if 16 < 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 {
2015-02-09 16:10:50 +01:00
if 16 < 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 {
screenScale = 3 - screenScale // Swap 1 and 2
}
2015-02-09 02:25:00 +01:00
ebiten.SetScreenSize(screenWidth, screenHeight)
2015-02-09 16:10:50 +01:00
ebiten.SetScreenScale(screenScale)
2015-02-09 02:25:00 +01:00
2015-02-09 16:10:50 +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)
if err := screen.DrawImage(gophersImage, op); err != nil {
return err
}
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
2015-02-18 18:33:15 +01:00
Cursor: (%d, %d)
FPS: %0.2f`, x, y, ebiten.CurrentFPS())
2015-02-09 16:10:50 +01:00
ebitenutil.DebugPrint(screen, msg)
2015-02-09 02:25:00 +01:00
return nil
}
func main() {
var err error
gophersImage, _, err = ebitenutil.NewImageFromFile("images/gophers.jpg", ebiten.FilterNearest)
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)
}
}