examples: Rename texture -> image

This commit is contained in:
Hajime Hoshi 2014-12-21 03:05:36 +09:00
parent d774c19581
commit d34c94aa09
12 changed files with 106 additions and 106 deletions

View File

@ -32,7 +32,7 @@ const (
type Game struct {
count int
tmpRenderTarget *ebiten.RenderTarget
ebitenTexture *ebiten.Image
ebitenImage *ebiten.Image
}
func (g *Game) Update(r *ebiten.RenderTarget) error {
@ -53,7 +53,7 @@ func (g *Game) Update(r *ebiten.RenderTarget) error {
for i := 0; i < 10; i++ {
geo := ebiten.TranslateGeometry(15+float64(i)*(diff), 20)
clr := ebiten.ScaleColor(color.RGBA{0xff, 0xff, 0xff, 0x80})
if err := ebiten.DrawWholeImage(g.tmpRenderTarget, g.ebitenTexture, geo, clr); err != nil {
if err := ebiten.DrawWholeImage(g.tmpRenderTarget, g.ebitenImage, geo, clr); err != nil {
return err
}
}
@ -72,7 +72,7 @@ func (g *Game) Update(r *ebiten.RenderTarget) error {
func main() {
g := new(Game)
var err error
g.ebitenTexture, _, err = ebitenutil.NewImageFromFile("images/ebiten.png", ebiten.FilterNearest)
g.ebitenImage, _, err = ebitenutil.NewImageFromFile("images/ebiten.png", ebiten.FilterNearest)
if err != nil {
log.Fatal(err)
}

View File

@ -112,11 +112,11 @@ func (f *Field) flushLine(j int) bool {
return true
}
func (f *Field) Draw(r *ebiten.RenderTarget, textures *Textures, geo ebiten.GeometryMatrix) {
func (f *Field) Draw(r *ebiten.RenderTarget, images *Images, geo ebiten.GeometryMatrix) {
blocks := make([][]BlockType, len(f.blocks))
for i, blockCol := range f.blocks {
blocks[i] = make([]BlockType, len(blockCol))
copy(blocks[i], blockCol[:])
}
drawBlocks(r, textures, blocks, geo)
drawBlocks(r, images, blocks, geo)
}

View File

@ -22,7 +22,7 @@ import (
)
func init() {
texturePaths["font"] = "images/blocks/font.png"
imagePaths["font"] = "images/blocks/font.png"
}
const charWidth = 8
@ -32,8 +32,8 @@ func textWidth(str string) int {
return charWidth * len(str)
}
func drawText(r *ebiten.RenderTarget, textures *Textures, str string, ox, oy, scale int, clr color.Color) {
fontTextureId := textures.GetTexture("font")
func drawText(r *ebiten.RenderTarget, images *Images, str string, ox, oy, scale int, clr color.Color) {
fontImageId := images.GetImage("font")
parts := []ebiten.ImagePart{}
locationX, locationY := 0, 0
@ -56,10 +56,10 @@ func drawText(r *ebiten.RenderTarget, textures *Textures, str string, ox, oy, sc
geoMat := ebiten.ScaleGeometry(float64(scale), float64(scale))
geoMat.Concat(ebiten.TranslateGeometry(float64(ox), float64(oy)))
clrMat := ebiten.ScaleColor(clr)
r.DrawImage(fontTextureId, parts, geoMat, clrMat)
r.DrawImage(fontImageId, parts, geoMat, clrMat)
}
func drawTextWithShadow(r *ebiten.RenderTarget, textures *Textures, str string, x, y, scale int, clr color.Color) {
drawText(r, textures, str, x+1, y+1, scale, color.RGBA{0, 0, 0, 0x80})
drawText(r, textures, str, x, y, scale, clr)
func drawTextWithShadow(r *ebiten.RenderTarget, images *Images, str string, x, y, scale int, clr color.Color) {
drawText(r, images, str, x+1, y+1, scale, color.RGBA{0, 0, 0, 0x80})
drawText(r, images, str, x, y, scale, clr)
}

View File

@ -27,7 +27,7 @@ type Size struct {
}
// TODO: Should they be global??
var texturePaths = map[string]string{}
var imagePaths = map[string]string{}
var renderTargetSizes = map[string]Size{}
const ScreenWidth = 256
@ -42,7 +42,7 @@ type Game struct {
once sync.Once
sceneManager *SceneManager
input *Input
textures *Textures
images *Images
}
func NewGame() *Game {
@ -54,16 +54,16 @@ func NewGame() *Game {
}
func (game *Game) isInitialized() bool {
if game.textures == nil {
if game.images == nil {
return false
}
for name := range texturePaths {
if !game.textures.Has(name) {
for name := range imagePaths {
if !game.images.Has(name) {
return false
}
}
for name := range renderTargetSizes {
if !game.textures.Has(name) {
if !game.images.Has(name) {
return false
}
}
@ -72,12 +72,12 @@ func (game *Game) isInitialized() bool {
func (game *Game) Update(r *ebiten.RenderTarget) error {
game.once.Do(func() {
game.textures = NewTextures()
for name, path := range texturePaths {
game.textures.RequestTexture(name, path)
game.images = NewImages()
for name, path := range imagePaths {
game.images.RequestImage(name, path)
}
for name, size := range renderTargetSizes {
game.textures.RequestRenderTarget(name, size)
game.images.RequestRenderTarget(name, size)
}
})
if !game.isInitialized() {
@ -88,6 +88,6 @@ func (game *Game) Update(r *ebiten.RenderTarget) error {
SceneManager: game.sceneManager,
Input: game.input,
})
game.sceneManager.Draw(r, game.textures)
game.sceneManager.Draw(r, game.images)
return nil
}

View File

@ -24,7 +24,7 @@ import (
)
func init() {
texturePaths["empty"] = "images/blocks/empty.png"
imagePaths["empty"] = "images/blocks/empty.png"
}
type GameScene struct {
@ -109,10 +109,10 @@ func (s *GameScene) Update(state *GameState) {
}
}
func (s *GameScene) Draw(r *ebiten.RenderTarget, textures *Textures) {
func (s *GameScene) Draw(r *ebiten.RenderTarget, images *Images) {
r.Fill(color.White)
field := textures.GetTexture("empty")
field := images.GetImage("empty")
w, h := field.Size()
geoMat := ebiten.ScaleGeometry(float64(fieldWidth)/float64(w), float64(fieldHeight)/float64(h))
geoMat.Concat(ebiten.TranslateGeometry(20, 20)) // TODO: magic number?
@ -121,9 +121,9 @@ func (s *GameScene) Draw(r *ebiten.RenderTarget, textures *Textures) {
geoMat = ebiten.GeometryMatrixI()
geoMat.Concat(ebiten.TranslateGeometry(20, 20))
s.field.Draw(r, textures, geoMat)
s.field.Draw(r, images, geoMat)
if s.currentPiece != nil {
s.currentPiece.Draw(r, textures, 20, 20, s.currentPieceX, s.currentPieceY, s.currentPieceAngle)
s.currentPiece.Draw(r, images, 20, 20, s.currentPieceX, s.currentPieceY, s.currentPieceAngle)
}
}

View File

@ -34,27 +34,27 @@ type nameSize struct {
size Size
}
type Textures struct {
texturePaths chan namePath
type Images struct {
imagePaths chan namePath
renderTargetSizes chan nameSize
textures map[string]*ebiten.Image
images map[string]*ebiten.Image
renderTargets map[string]*ebiten.RenderTarget
sync.RWMutex
}
func NewTextures() *Textures {
textures := &Textures{
texturePaths: make(chan namePath),
func NewImages() *Images {
images := &Images{
imagePaths: make(chan namePath),
renderTargetSizes: make(chan nameSize),
textures: map[string]*ebiten.Image{},
images: map[string]*ebiten.Image{},
renderTargets: map[string]*ebiten.RenderTarget{},
}
go func() {
for {
textures.loopMain()
images.loopMain()
}
}()
return textures
return images
}
func loadImage(path string) (image.Image, error) {
@ -71,9 +71,9 @@ func loadImage(path string) (image.Image, error) {
return img, nil
}
func (t *Textures) loopMain() {
func (i *Images) loopMain() {
select {
case p := <-t.texturePaths:
case p := <-i.imagePaths:
name := p.name
path := p.path
go func() {
@ -85,11 +85,11 @@ func (t *Textures) loopMain() {
if err != nil {
panic(err)
}
t.Lock()
defer t.Unlock()
t.textures[name] = id
i.Lock()
defer i.Unlock()
i.images[name] = id
}()
case s := <-t.renderTargetSizes:
case s := <-i.renderTargetSizes:
name := s.name
size := s.size
go func() {
@ -97,40 +97,40 @@ func (t *Textures) loopMain() {
if err != nil {
panic(err)
}
t.Lock()
defer t.Unlock()
t.renderTargets[name] = id
i.Lock()
defer i.Unlock()
i.renderTargets[name] = id
}()
}
}
func (t *Textures) RequestTexture(name string, path string) {
t.texturePaths <- namePath{name, path}
func (i *Images) RequestImage(name string, path string) {
i.imagePaths <- namePath{name, path}
}
func (t *Textures) RequestRenderTarget(name string, size Size) {
t.renderTargetSizes <- nameSize{name, size}
func (i *Images) RequestRenderTarget(name string, size Size) {
i.renderTargetSizes <- nameSize{name, size}
}
func (t *Textures) Has(name string) bool {
t.RLock()
defer t.RUnlock()
_, ok := t.textures[name]
func (i *Images) Has(name string) bool {
i.RLock()
defer i.RUnlock()
_, ok := i.images[name]
if ok {
return true
}
_, ok = t.renderTargets[name]
_, ok = i.renderTargets[name]
return ok
}
func (t *Textures) GetTexture(name string) *ebiten.Image {
t.RLock()
defer t.RUnlock()
return t.textures[name]
func (i *Images) GetImage(name string) *ebiten.Image {
i.RLock()
defer i.RUnlock()
return i.images[name]
}
func (t *Textures) GetRenderTarget(name string) *ebiten.RenderTarget {
t.RLock()
defer t.RUnlock()
return t.renderTargets[name]
func (i *Images) GetRenderTarget(name string) *ebiten.RenderTarget {
i.RLock()
defer i.RUnlock()
return i.renderTargets[name]
}

View File

@ -21,7 +21,7 @@ import (
)
func init() {
texturePaths["blocks"] = "images/blocks/blocks.png"
imagePaths["blocks"] = "images/blocks/blocks.png"
}
type Angle int
@ -138,7 +138,7 @@ const blockHeight = 10
const fieldBlockNumX = 10
const fieldBlockNumY = 20
func drawBlocks(r *ebiten.RenderTarget, textures *Textures, blocks [][]BlockType, geo ebiten.GeometryMatrix) {
func drawBlocks(r *ebiten.RenderTarget, images *Images, blocks [][]BlockType, geo ebiten.GeometryMatrix) {
parts := []ebiten.ImagePart{}
for i, blockCol := range blocks {
for j, block := range blockCol {
@ -152,8 +152,8 @@ func drawBlocks(r *ebiten.RenderTarget, textures *Textures, blocks [][]BlockType
parts = append(parts, ebiten.ImagePart{dst, src})
}
}
blocksTexture := textures.GetTexture("blocks")
r.DrawImage(blocksTexture, parts, geo, ebiten.ColorMatrixI())
blocksImage := images.GetImage("blocks")
r.DrawImage(blocksImage, parts, geo, ebiten.ColorMatrixI())
}
func (p *Piece) InitialPosition() (int, int) {
@ -213,7 +213,7 @@ func (p *Piece) AbsorbInto(field *Field, x, y int, angle Angle) {
}
}
func (p *Piece) Draw(r *ebiten.RenderTarget, textures *Textures, fieldX, fieldY int, pieceX, pieceY int, angle Angle) {
func (p *Piece) Draw(r *ebiten.RenderTarget, images *Images, fieldX, fieldY int, pieceX, pieceY int, angle Angle) {
size := len(p.blocks)
blocks := make([][]BlockType, size)
for i := range p.blocks {
@ -230,5 +230,5 @@ func (p *Piece) Draw(r *ebiten.RenderTarget, textures *Textures, fieldX, fieldY
y := fieldY + pieceY*blockHeight
geoMat.Concat(ebiten.TranslateGeometry(float64(x), float64(y)))
drawBlocks(r, textures, blocks, geoMat)
drawBlocks(r, images, blocks, geoMat)
}

View File

@ -29,7 +29,7 @@ func init() {
type Scene interface {
Update(state *GameState)
Draw(r *ebiten.RenderTarget, textures *Textures)
Draw(r *ebiten.RenderTarget, images *Images)
}
const transitionMaxCount = 20
@ -60,18 +60,18 @@ func (s *SceneManager) Update(state *GameState) {
}
}
func (s *SceneManager) Draw(r *ebiten.RenderTarget, textures *Textures) {
func (s *SceneManager) Draw(r *ebiten.RenderTarget, images *Images) {
if s.transitionCount == -1 {
s.current.Draw(r, textures)
s.current.Draw(r, images)
return
}
from := textures.GetRenderTarget("scene_manager_transition_from")
from := images.GetRenderTarget("scene_manager_transition_from")
from.Clear()
s.current.Draw(from, textures)
s.current.Draw(from, images)
to := textures.GetRenderTarget("scene_manager_transition_to")
to := images.GetRenderTarget("scene_manager_transition_to")
to.Clear()
s.next.Draw(to, textures)
s.next.Draw(to, images)
color := ebiten.ColorMatrixI()
ebiten.DrawWholeImage(r, from.Image(), ebiten.GeometryMatrixI(), color)

View File

@ -22,7 +22,7 @@ import (
)
func init() {
texturePaths["background"] = "images/blocks/background.png"
imagePaths["background"] = "images/blocks/background.png"
}
type TitleScene struct {
@ -40,43 +40,43 @@ func (s *TitleScene) Update(state *GameState) {
}
}
func (s *TitleScene) Draw(r *ebiten.RenderTarget, textures *Textures) {
drawTitleBackground(r, textures, s.count)
drawLogo(r, textures, "BLOCKS")
func (s *TitleScene) Draw(r *ebiten.RenderTarget, images *Images) {
drawTitleBackground(r, images, s.count)
drawLogo(r, images, "BLOCKS")
message := "PRESS SPACE TO START"
x := (ScreenWidth - textWidth(message)) / 2
y := ScreenHeight - 48
drawTextWithShadow(r, textures, message, x, y, 1, color.RGBA{0x80, 0, 0, 0xff})
drawTextWithShadow(r, images, message, x, y, 1, color.RGBA{0x80, 0, 0, 0xff})
}
func drawTitleBackground(r *ebiten.RenderTarget, textures *Textures, c int) {
const textureWidth = 32
const textureHeight = 32
func drawTitleBackground(r *ebiten.RenderTarget, images *Images, c int) {
const imageWidth = 32
const imageHeight = 32
backgroundTexture := textures.GetTexture("background")
backgroundImage := images.GetImage("background")
parts := []ebiten.ImagePart{}
for j := -1; j < ScreenHeight/textureHeight+1; j++ {
for i := 0; i < ScreenWidth/textureWidth+1; i++ {
for j := -1; j < ScreenHeight/imageHeight+1; j++ {
for i := 0; i < ScreenWidth/imageWidth+1; i++ {
parts = append(parts, ebiten.ImagePart{
Dst: ebiten.Rect{float64(i * textureWidth), float64(j * textureHeight), textureWidth, textureHeight},
Src: ebiten.Rect{0, 0, textureWidth, textureHeight},
Dst: ebiten.Rect{float64(i * imageWidth), float64(j * imageHeight), imageWidth, imageHeight},
Src: ebiten.Rect{0, 0, imageWidth, imageHeight},
})
}
}
dx := (-c / 4) % textureWidth
dy := (c / 4) % textureHeight
dx := (-c / 4) % imageWidth
dy := (c / 4) % imageHeight
geo := ebiten.GeometryMatrixI()
geo.Concat(ebiten.TranslateGeometry(float64(dx), float64(dy)))
clr := ebiten.ColorMatrixI()
r.DrawImage(backgroundTexture, parts, geo, clr)
r.DrawImage(backgroundImage, parts, geo, clr)
}
func drawLogo(r *ebiten.RenderTarget, textures *Textures, str string) {
func drawLogo(r *ebiten.RenderTarget, images *Images, str string) {
scale := 4
textWidth := textWidth(str) * scale
x := (ScreenWidth - textWidth) / 2
y := 32
drawTextWithShadow(r, textures, str, x, y, scale, color.RGBA{0x00, 0x00, 0x80, 0xff})
drawTextWithShadow(r, images, str, x, y, scale, color.RGBA{0x00, 0x00, 0x80, 0xff})
}

View File

@ -33,7 +33,7 @@ type Game struct {
count int
horizontalCount int
verticalCount int
gophersTexture *ebiten.Image
gophersImage *ebiten.Image
}
func (g *Game) Update(r *ebiten.RenderTarget) error {
@ -51,7 +51,7 @@ func (g *Game) Update(r *ebiten.RenderTarget) error {
g.verticalCount++
}
w, h := g.gophersTexture.Size()
w, h := g.gophersImage.Size()
geo := ebiten.TranslateGeometry(-float64(w)/2, -float64(h)/2)
scaleX := 0.5 * math.Pow(1.05, float64(g.horizontalCount))
scaleY := 0.5 * math.Pow(1.05, float64(g.verticalCount))
@ -60,14 +60,14 @@ func (g *Game) Update(r *ebiten.RenderTarget) error {
geo.Concat(ebiten.TranslateGeometry(screenWidth/2, screenHeight/2))
//clr := ebiten.RotateHue(float64(g.count%180) * 2 * math.Pi / 180)
clr := ebiten.ColorMatrixI()
ebiten.DrawWholeImage(r, g.gophersTexture, geo, clr)
ebiten.DrawWholeImage(r, g.gophersImage, geo, clr)
return nil
}
func main() {
g := new(Game)
var err error
g.gophersTexture, _, err = ebitenutil.NewImageFromFile("images/gophers.jpg", ebiten.FilterNearest)
g.gophersImage, _, err = ebitenutil.NewImageFromFile("images/gophers.jpg", ebiten.FilterNearest)
if err != nil {
log.Fatal(err)
}

View File

@ -31,13 +31,13 @@ const (
const mosaicRatio = 16
type Game struct {
gophersTexture *ebiten.Image
gophersImage *ebiten.Image
gophersRenderTarget *ebiten.RenderTarget
}
func (g *Game) Update(r *ebiten.RenderTarget) error {
geo := ebiten.ScaleGeometry(1.0/mosaicRatio, 1.0/mosaicRatio)
ebiten.DrawWholeImage(g.gophersRenderTarget, g.gophersTexture, geo, ebiten.ColorMatrixI())
ebiten.DrawWholeImage(g.gophersRenderTarget, g.gophersImage, geo, ebiten.ColorMatrixI())
geo = ebiten.ScaleGeometry(mosaicRatio/2.0, mosaicRatio/2.0)
ebiten.DrawWholeImage(r, g.gophersRenderTarget.Image(), geo, ebiten.ColorMatrixI())
@ -47,11 +47,11 @@ func (g *Game) Update(r *ebiten.RenderTarget) error {
func main() {
g := new(Game)
var err error
g.gophersTexture, _, err = ebitenutil.NewImageFromFile("images/gophers.jpg", ebiten.FilterNearest)
g.gophersImage, _, err = ebitenutil.NewImageFromFile("images/gophers.jpg", ebiten.FilterNearest)
if err != nil {
log.Fatal(err)
}
w, h := g.gophersTexture.Size()
w, h := g.gophersImage.Size()
g.gophersRenderTarget, err = ebiten.NewRenderTarget(w/mosaicRatio, h/mosaicRatio, ebiten.FilterNearest)
if err != nil {
log.Fatal(err)

View File

@ -29,12 +29,12 @@ const (
)
type Game struct {
gophersTexture *ebiten.Image
gophersImage *ebiten.Image
}
func (g *Game) Update(r *ebiten.RenderTarget) error {
parts := []ebiten.ImagePart{}
w, h := g.gophersTexture.Size()
w, h := g.gophersImage.Size()
for i := 0; i < h; i++ {
width := float64(w) + float64(i)*0.75
x := float64(h-i) * 0.75 / 2
@ -48,14 +48,14 @@ func (g *Game) Update(r *ebiten.RenderTarget) error {
geo := ebiten.TranslateGeometry(-maxWidth/2, -float64(h)/2)
geo.Concat(ebiten.ScaleGeometry(0.4, 0.4))
geo.Concat(ebiten.TranslateGeometry(screenWidth/2, screenHeight/2))
r.DrawImage(g.gophersTexture, parts, geo, ebiten.ColorMatrixI())
r.DrawImage(g.gophersImage, parts, geo, ebiten.ColorMatrixI())
return nil
}
func main() {
g := new(Game)
var err error
g.gophersTexture, _, err = ebitenutil.NewImageFromFile("images/gophers.jpg", ebiten.FilterNearest)
g.gophersImage, _, err = ebitenutil.NewImageFromFile("images/gophers.jpg", ebiten.FilterNearest)
if err != nil {
log.Fatal(err)
}