ebiten/examples/blocks/blocks/titlescene.go

116 lines
2.9 KiB
Go
Raw Normal View History

// Copyright 2014 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.
2014-12-09 15:16:04 +01:00
2014-12-08 17:35:35 +01:00
package blocks
2013-12-18 10:05:28 +01:00
import (
"bytes"
"image"
2016-02-15 17:13:04 +01:00
"image/color"
_ "image/png"
2016-02-15 17:13:04 +01:00
2020-10-03 19:35:13 +02:00
"github.com/hajimehoshi/ebiten/v2"
rblocks "github.com/hajimehoshi/ebiten/v2/examples/resources/images/blocks"
"github.com/hajimehoshi/ebiten/v2/inpututil"
2013-12-18 10:05:28 +01:00
)
var imageBackground *ebiten.Image
2013-12-18 19:21:25 +01:00
func init() {
img, _, err := image.Decode(bytes.NewReader(rblocks.Background_png))
if err != nil {
panic(err)
}
imageBackground = ebiten.NewImageFromImage(img)
2013-12-18 19:21:25 +01:00
}
2013-12-18 10:05:28 +01:00
type TitleScene struct {
count int
}
func anyGamepadVirtualButtonJustPressed(i *Input) bool {
if !i.gamepadConfig.IsGamepadIDInitialized() {
return false
}
2018-01-22 07:12:17 +01:00
for _, b := range virtualGamepadButtons {
if i.gamepadConfig.IsButtonJustPressed(b) {
2015-01-19 14:21:24 +01:00
return true
}
}
return false
}
2014-12-29 05:36:29 +01:00
func (s *TitleScene) Update(state *GameState) error {
2013-12-18 10:05:28 +01:00
s.count++
if inpututil.IsKeyJustPressed(ebiten.KeySpace) {
2013-12-18 19:21:25 +01:00
state.SceneManager.GoTo(NewGameScene())
2015-01-19 14:21:24 +01:00
return nil
}
if anyGamepadVirtualButtonJustPressed(state.Input) {
2015-01-19 14:21:24 +01:00
state.SceneManager.GoTo(NewGameScene())
return nil
}
2018-01-22 07:12:17 +01:00
if state.Input.gamepadConfig.IsGamepadIDInitialized() {
return nil
}
// If 'virtual' gamepad buttons are not set and any gamepad buttons are pressed,
2018-01-22 07:12:17 +01:00
// go to the gamepad configuration scene.
id := state.Input.GamepadIDButtonPressed()
if id < 0 {
return nil
}
state.Input.gamepadConfig.SetGamepadID(id)
if state.Input.gamepadConfig.NeedsConfiguration() {
g := &GamepadScene{}
g.gamepadID = id
state.SceneManager.GoTo(g)
2013-12-18 19:21:25 +01:00
}
2014-12-29 05:36:29 +01:00
return nil
2013-12-18 10:05:28 +01:00
}
func (s *TitleScene) Draw(r *ebiten.Image) {
s.drawTitleBackground(r, s.count)
drawLogo(r, "BLOCKS")
2013-12-18 19:21:25 +01:00
message := "PRESS SPACE TO START"
x := 0
2014-01-07 15:29:12 +01:00
y := ScreenHeight - 48
drawTextWithShadowCenter(r, message, x, y, 1, color.NRGBA{0x80, 0, 0, 0xff}, ScreenWidth)
2013-12-18 10:05:28 +01:00
}
func (s *TitleScene) drawTitleBackground(r *ebiten.Image, c int) {
2017-05-27 19:24:23 +02:00
w, h := imageBackground.Size()
op := &ebiten.DrawImageOptions{}
for i := 0; i < (ScreenWidth/w+1)*(ScreenHeight/h+2); i++ {
op.GeoM.Reset()
2018-01-22 07:12:17 +01:00
dx := -(c / 4) % w
2017-05-27 19:24:23 +02:00
dy := (c / 4) % h
dstX := (i%(ScreenWidth/w+1))*w + dx
dstY := (i/(ScreenWidth/w+1)-1)*h + dy
op.GeoM.Translate(float64(dstX), float64(dstY))
r.DrawImage(imageBackground, op)
}
2013-12-18 10:05:28 +01:00
}
func drawLogo(r *ebiten.Image, str string) {
2018-01-22 07:12:17 +01:00
const scale = 4
x := 0
2013-12-18 19:21:25 +01:00
y := 32
drawTextWithShadowCenter(r, str, x, y, scale, color.NRGBA{0x00, 0x00, 0x80, 0xff}, ScreenWidth)
2013-12-18 10:05:28 +01:00
}