mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-12 20:18:59 +01:00
Remove imagePaths and renderTargetSizes; Initialize images in init()
This commit is contained in:
parent
22035e79fe
commit
0a2a76be3b
@ -16,13 +16,20 @@ package blocks
|
||||
|
||||
import (
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
"github.com/hajimehoshi/ebiten/ebitenutil"
|
||||
"image"
|
||||
"image/color"
|
||||
"math"
|
||||
)
|
||||
|
||||
var imageFont *ebiten.Image
|
||||
|
||||
func init() {
|
||||
imagePaths["font"] = "images/blocks/font.png"
|
||||
var err error
|
||||
imageFont, _, err = ebitenutil.NewImageFromFile("images/blocks/font.png", ebiten.FilterNearest)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
const charWidth = 8
|
||||
@ -33,7 +40,6 @@ func textWidth(str string) int {
|
||||
}
|
||||
|
||||
func drawText(rt *ebiten.Image, images *Images, str string, ox, oy, scale int, c color.Color) {
|
||||
fontImageId := images.GetImage("font")
|
||||
parts := []ebiten.ImagePart{}
|
||||
|
||||
locationX, locationY := 0, 0
|
||||
@ -62,7 +68,7 @@ func drawText(rt *ebiten.Image, images *Images, str string, ox, oy, scale int, c
|
||||
b := float64(c2.B) / max
|
||||
a := float64(c2.A) / max
|
||||
clr := ebiten.ScaleColor(r, g, b, a)
|
||||
rt.DrawImage(fontImageId, &ebiten.DrawImageOptions{
|
||||
rt.DrawImage(imageFont, &ebiten.DrawImageOptions{
|
||||
Parts: parts,
|
||||
GeoM: geo,
|
||||
ColorM: clr,
|
||||
|
@ -24,10 +24,6 @@ type Size struct {
|
||||
Height int
|
||||
}
|
||||
|
||||
// TODO: Should they be global??
|
||||
var imagePaths = map[string]string{}
|
||||
var renderTargetSizes = map[string]Size{}
|
||||
|
||||
const ScreenWidth = 256
|
||||
const ScreenHeight = 240
|
||||
|
||||
@ -51,36 +47,7 @@ func NewGame() *Game {
|
||||
return game
|
||||
}
|
||||
|
||||
func (game *Game) isInitialized() bool {
|
||||
if game.images == nil {
|
||||
return false
|
||||
}
|
||||
for name := range imagePaths {
|
||||
if !game.images.Has(name) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
for name := range renderTargetSizes {
|
||||
if !game.images.Has(name) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (game *Game) Update(r *ebiten.Image) error {
|
||||
game.once.Do(func() {
|
||||
game.images = NewImages()
|
||||
for name, path := range imagePaths {
|
||||
game.images.RequestImage(name, path)
|
||||
}
|
||||
for name, size := range renderTargetSizes {
|
||||
game.images.RequestRenderTarget(name, size)
|
||||
}
|
||||
})
|
||||
if !game.isInitialized() {
|
||||
return nil
|
||||
}
|
||||
game.input.Update()
|
||||
game.sceneManager.Update(&GameState{
|
||||
SceneManager: game.sceneManager,
|
||||
|
@ -16,13 +16,20 @@ package blocks
|
||||
|
||||
import (
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
"github.com/hajimehoshi/ebiten/ebitenutil"
|
||||
"image/color"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
var imageEmpty *ebiten.Image
|
||||
|
||||
func init() {
|
||||
imagePaths["empty"] = "images/blocks/empty.png"
|
||||
var err error
|
||||
imageEmpty, _, err = ebitenutil.NewImageFromFile("images/blocks/empty.png", ebiten.FilterNearest)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
type GameScene struct {
|
||||
@ -110,7 +117,7 @@ func (s *GameScene) Update(state *GameState) {
|
||||
func (s *GameScene) Draw(r *ebiten.Image, images *Images) {
|
||||
r.Fill(color.White)
|
||||
|
||||
field := images.GetImage("empty")
|
||||
field := imageEmpty
|
||||
w, h := field.Size()
|
||||
geo := ebiten.ScaleGeo(float64(fieldWidth)/float64(w), float64(fieldHeight)/float64(h))
|
||||
geo.Concat(ebiten.TranslateGeo(20, 20)) // TODO: magic number?
|
||||
|
@ -16,11 +16,18 @@ package blocks
|
||||
|
||||
import (
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
"github.com/hajimehoshi/ebiten/ebitenutil"
|
||||
"image"
|
||||
)
|
||||
|
||||
var imageBlocks *ebiten.Image
|
||||
|
||||
func init() {
|
||||
imagePaths["blocks"] = "images/blocks/blocks.png"
|
||||
var err error
|
||||
imageBlocks, _, err = ebitenutil.NewImageFromFile("images/blocks/blocks.png", ebiten.FilterNearest)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
type Angle int
|
||||
@ -153,8 +160,7 @@ func drawBlocks(r *ebiten.Image, images *Images, blocks [][]BlockType, x, y int)
|
||||
})
|
||||
}
|
||||
}
|
||||
blocksImage := images.GetImage("blocks")
|
||||
r.DrawImage(blocksImage, &ebiten.DrawImageOptions{
|
||||
r.DrawImage(imageBlocks, &ebiten.DrawImageOptions{
|
||||
Parts: parts,
|
||||
})
|
||||
}
|
||||
|
@ -18,11 +18,21 @@ import (
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
)
|
||||
|
||||
var (
|
||||
transitionFrom *ebiten.Image
|
||||
transitionTo *ebiten.Image
|
||||
)
|
||||
|
||||
func init() {
|
||||
renderTargetSizes["scene_manager_transition_from"] =
|
||||
Size{ScreenWidth, ScreenHeight}
|
||||
renderTargetSizes["scene_manager_transition_to"] =
|
||||
Size{ScreenWidth, ScreenHeight}
|
||||
var err error
|
||||
transitionFrom, err = ebiten.NewImage(ScreenWidth, ScreenHeight, ebiten.FilterNearest)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
transitionTo, err = ebiten.NewImage(ScreenWidth, ScreenHeight, ebiten.FilterNearest)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
type Scene interface {
|
||||
@ -63,21 +73,18 @@ func (s *SceneManager) Draw(r *ebiten.Image, images *Images) {
|
||||
s.current.Draw(r, images)
|
||||
return
|
||||
}
|
||||
from := images.GetRenderTarget("scene_manager_transition_from")
|
||||
from.Clear()
|
||||
s.current.Draw(from, images)
|
||||
transitionFrom.Clear()
|
||||
s.current.Draw(transitionFrom, images)
|
||||
|
||||
to := images.GetRenderTarget("scene_manager_transition_to")
|
||||
to.Clear()
|
||||
s.next.Draw(to, images)
|
||||
transitionTo.Clear()
|
||||
s.next.Draw(transitionTo, images)
|
||||
|
||||
r.DrawImage(from, nil)
|
||||
r.DrawImage(transitionFrom, nil)
|
||||
|
||||
alpha := float64(s.transitionCount) / float64(transitionMaxCount)
|
||||
op := &ebiten.DrawImageOptions{
|
||||
r.DrawImage(transitionTo, &ebiten.DrawImageOptions{
|
||||
ColorM: ebiten.ScaleColor(1, 1, 1, alpha),
|
||||
}
|
||||
r.DrawImage(to, op)
|
||||
})
|
||||
}
|
||||
|
||||
func (s *SceneManager) GoTo(scene Scene) {
|
||||
|
@ -16,12 +16,19 @@ package blocks
|
||||
|
||||
import (
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
"github.com/hajimehoshi/ebiten/ebitenutil"
|
||||
"image"
|
||||
"image/color"
|
||||
)
|
||||
|
||||
var imageBackground *ebiten.Image
|
||||
|
||||
func init() {
|
||||
imagePaths["background"] = "images/blocks/background.png"
|
||||
var err error
|
||||
imageBackground, _, err = ebitenutil.NewImageFromFile("images/blocks/background.png", ebiten.FilterNearest)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
type TitleScene struct {
|
||||
@ -40,7 +47,7 @@ func (s *TitleScene) Update(state *GameState) {
|
||||
}
|
||||
|
||||
func (s *TitleScene) Draw(r *ebiten.Image, images *Images) {
|
||||
drawTitleBackground(r, images, s.count)
|
||||
drawTitleBackground(r, s.count)
|
||||
drawLogo(r, images, "BLOCKS")
|
||||
|
||||
message := "PRESS SPACE TO START"
|
||||
@ -49,14 +56,14 @@ func (s *TitleScene) Draw(r *ebiten.Image, images *Images) {
|
||||
drawTextWithShadow(r, images, message, x, y, 1, color.NRGBA{0x80, 0, 0, 0xff})
|
||||
}
|
||||
|
||||
func drawTitleBackground(r *ebiten.Image, images *Images, c int) {
|
||||
func drawTitleBackground(r *ebiten.Image, c int) {
|
||||
const imageWidth = 32
|
||||
const imageHeight = 32
|
||||
|
||||
dx := (-c / 4) % imageWidth
|
||||
dy := (c / 4) % imageHeight
|
||||
|
||||
backgroundImage := images.GetImage("background")
|
||||
backgroundImage := imageBackground
|
||||
parts := []ebiten.ImagePart{}
|
||||
for j := -1; j < ScreenHeight/imageHeight+1; j++ {
|
||||
for i := 0; i < ScreenWidth/imageWidth+1; i++ {
|
||||
|
Loading…
Reference in New Issue
Block a user