Remove unnecessary channels

This commit is contained in:
Hajime Hoshi 2014-05-11 19:44:36 +09:00
parent 1c187597e6
commit 86846b0171
12 changed files with 42 additions and 41 deletions

View File

@ -93,7 +93,7 @@ func (f *Field) Flush() {
} }
} }
func (f *Field) Draw(context graphics.Context, textures *Textures, geo matrix.Geometry) { func (f *Field) Draw(context graphics.Context, textures Textures, geo matrix.Geometry) {
blocks := make([][]BlockType, len(f.blocks)) blocks := make([][]BlockType, len(f.blocks))
for i, blockCol := range f.blocks { for i, blockCol := range f.blocks {
blocks[i] = make([]BlockType, len(blockCol)) blocks[i] = make([]BlockType, len(blockCol))

View File

@ -19,7 +19,7 @@ func textWidth(str string) int {
func drawText( func drawText(
context graphics.Context, context graphics.Context,
textures *Textures, textures Textures,
str string, str string,
x, y, scale int, x, y, scale int,
clr color.Color) { clr color.Color) {
@ -55,7 +55,7 @@ func drawText(
func drawTextWithShadow( func drawTextWithShadow(
context graphics.Context, context graphics.Context,
textures *Textures, textures Textures,
str string, str string,
x, y, scale int, x, y, scale int,
clr color.Color) { clr color.Color) {

View File

@ -23,17 +23,25 @@ type GameState struct {
Input *Input Input *Input
} }
type Textures interface {
RequestTexture(name string, path string)
RequestRenderTarget(name string, size Size)
Has(name string) bool
GetTexture(name string) graphics.TextureId
GetRenderTarget(name string) graphics.RenderTargetId
}
type Game struct { type Game struct {
sceneManager *SceneManager sceneManager *SceneManager
input *Input input *Input
textures *Textures textures Textures
} }
func NewGame(textureFactory graphics.TextureFactory) *Game { func NewGame(textures Textures) *Game {
game := &Game{ game := &Game{
sceneManager: NewSceneManager(NewTitleScene()), sceneManager: NewSceneManager(NewTitleScene()),
input: NewInput(), input: NewInput(),
textures: NewTextures(textureFactory), textures: textures,
} }
for name, path := range texturePaths { for name, path := range texturePaths {
game.textures.RequestTexture(name, path) game.textures.RequestTexture(name, path)

View File

@ -97,7 +97,7 @@ func (s *GameScene) Update(state *GameState) {
} }
} }
func (s *GameScene) Draw(context graphics.Context, textures *Textures) { func (s *GameScene) Draw(context graphics.Context, textures Textures) {
context.Fill(0xff, 0xff, 0xff) context.Fill(0xff, 0xff, 0xff)
field := textures.GetTexture("empty") field := textures.GetTexture("empty")

View File

@ -125,7 +125,7 @@ const fieldBlockNumY = 20
func drawBlocks( func drawBlocks(
context graphics.Context, context graphics.Context,
textures *Textures, textures Textures,
blocks [][]BlockType, blocks [][]BlockType,
geo matrix.Geometry) { geo matrix.Geometry) {
parts := []graphics.TexturePart{} parts := []graphics.TexturePart{}
@ -211,7 +211,7 @@ func (p *Piece) AbsorbInto(field *Field, x, y int, angle Angle) {
func (p *Piece) Draw( func (p *Piece) Draw(
context graphics.Context, context graphics.Context,
textures *Textures, textures Textures,
fieldX, fieldY int, fieldX, fieldY int,
pieceX, pieceY int, pieceX, pieceY int,
angle Angle) { angle Angle) {

View File

@ -14,7 +14,7 @@ func init() {
type Scene interface { type Scene interface {
Update(state *GameState) Update(state *GameState)
Draw(context graphics.Context, textures *Textures) Draw(context graphics.Context, textures Textures)
} }
const transitionMaxCount = 20 const transitionMaxCount = 20
@ -45,7 +45,7 @@ func (s *SceneManager) Update(state *GameState) {
} }
} }
func (s *SceneManager) Draw(context graphics.Context, textures *Textures) { func (s *SceneManager) Draw(context graphics.Context, textures Textures) {
if s.transitionCount == -1 { if s.transitionCount == -1 {
s.current.Draw(context, textures) s.current.Draw(context, textures)
return return

View File

@ -26,7 +26,7 @@ func (s *TitleScene) Update(state *GameState) {
} }
} }
func (s *TitleScene) Draw(context graphics.Context, textures *Textures) { func (s *TitleScene) Draw(context graphics.Context, textures Textures) {
drawTitleBackground(context, textures, s.count) drawTitleBackground(context, textures, s.count)
drawLogo(context, textures, "BLOCKS") drawLogo(context, textures, "BLOCKS")
@ -36,7 +36,7 @@ func (s *TitleScene) Draw(context graphics.Context, textures *Textures) {
drawTextWithShadow(context, textures, message, x, y, 1, color.RGBA{0x80, 0, 0, 0xff}) drawTextWithShadow(context, textures, message, x, y, 1, color.RGBA{0x80, 0, 0, 0xff})
} }
func drawTitleBackground(context graphics.Context, textures *Textures, c int) { func drawTitleBackground(context graphics.Context, textures Textures, c int) {
const textureWidth = 32 const textureWidth = 32
const textureHeight = 32 const textureHeight = 32
@ -60,7 +60,7 @@ func drawTitleBackground(context graphics.Context, textures *Textures, c int) {
context.Texture(backgroundTextureId).Draw(parts, geo, clr) context.Texture(backgroundTextureId).Draw(parts, geo, clr)
} }
func drawLogo(context graphics.Context, textures *Textures, str string) { func drawLogo(context graphics.Context, textures Textures, str string) {
scale := 4 scale := 4
textWidth := textWidth(str) * scale textWidth := textWidth(str) * scale
x := (ScreenWidth - textWidth) / 2 x := (ScreenWidth - textWidth) / 2

View File

@ -30,7 +30,6 @@ func main() {
const title = "Ebiten Demo" const title = "Ebiten Demo"
u := cocoa.UI() u := cocoa.UI()
textureFactory := cocoa.TextureFactory()
window := u.CreateGameWindow(screenWidth, screenHeight, screenScale, title) window := u.CreateGameWindow(screenWidth, screenHeight, screenScale, title)
drawing := make(chan struct{}) drawing := make(chan struct{})
@ -39,7 +38,8 @@ func main() {
defer close(quit) defer close(quit)
windowEvents := window.Events() windowEvents := window.Events()
var game Game = blocks.NewGame(textureFactory) textureFactory := cocoa.TextureFactory()
var game Game = blocks.NewGame(NewTextures(textureFactory))
frameTime := time.Duration(int64(time.Second) / int64(fps)) frameTime := time.Duration(int64(time.Second) / int64(fps))
tick := time.Tick(frameTime) tick := time.Tick(frameTime)
for { for {

View File

@ -1,6 +1,7 @@
package blocks package main
import ( import (
"github.com/hajimehoshi/go-ebiten/example/blocks"
"github.com/hajimehoshi/go-ebiten/graphics" "github.com/hajimehoshi/go-ebiten/graphics"
"image" "image"
"os" "os"
@ -14,7 +15,7 @@ type namePath struct {
type nameSize struct { type nameSize struct {
name string name string
size Size size blocks.Size
} }
type Textures struct { type Textures struct {
@ -98,7 +99,7 @@ func (t *Textures) RequestTexture(name string, path string) {
t.texturePaths <- namePath{name, path} t.texturePaths <- namePath{name, path}
} }
func (t *Textures) RequestRenderTarget(name string, size Size) { func (t *Textures) RequestRenderTarget(name string, size blocks.Size) {
t.renderTargetSizes <- nameSize{name, size} t.renderTargetSizes <- nameSize{name, size}
} }

View File

@ -35,7 +35,7 @@ type ids struct {
textures map[graphics.TextureId]*Texture textures map[graphics.TextureId]*Texture
renderTargets map[graphics.RenderTargetId]*RenderTarget renderTargets map[graphics.RenderTargetId]*RenderTarget
renderTargetToTexture map[graphics.RenderTargetId]graphics.TextureId renderTargetToTexture map[graphics.RenderTargetId]graphics.TextureId
counts chan int lastId int
currentRenderTargetId graphics.RenderTargetId currentRenderTargetId graphics.RenderTargetId
sync.RWMutex sync.RWMutex
} }
@ -45,14 +45,9 @@ func newIds() *ids {
textures: map[graphics.TextureId]*Texture{}, textures: map[graphics.TextureId]*Texture{},
renderTargets: map[graphics.RenderTargetId]*RenderTarget{}, renderTargets: map[graphics.RenderTargetId]*RenderTarget{},
renderTargetToTexture: map[graphics.RenderTargetId]graphics.TextureId{}, renderTargetToTexture: map[graphics.RenderTargetId]graphics.TextureId{},
counts: make(chan int), lastId: 0,
currentRenderTargetId: -1, currentRenderTargetId: -1,
} }
go func() {
for i := 1; ; i++ {
ids.counts <- i
}
}()
return ids return ids
} }
@ -80,10 +75,11 @@ func (i *ids) createTexture(img image.Image, filter graphics.Filter) (
if err != nil { if err != nil {
return 0, err return 0, err
} }
textureId := graphics.TextureId(<-i.counts)
i.Lock() i.Lock()
defer i.Unlock() defer i.Unlock()
i.lastId++
textureId := graphics.TextureId(i.lastId)
i.textures[textureId] = texture i.textures[textureId] = texture
return textureId, nil return textureId, nil
} }
@ -100,11 +96,13 @@ func (i *ids) createRenderTarget(width, height int, filter graphics.Filter) (
i.currentRenderTargetId = -1 i.currentRenderTargetId = -1
renderTarget := &RenderTarget{framebuffer, texture.width, texture.height, false} renderTarget := &RenderTarget{framebuffer, texture.width, texture.height, false}
textureId := graphics.TextureId(<-i.counts)
renderTargetId := graphics.RenderTargetId(<-i.counts)
i.Lock() i.Lock()
defer i.Unlock() defer i.Unlock()
i.lastId++
textureId := graphics.TextureId(i.lastId)
i.lastId++
renderTargetId := graphics.RenderTargetId(i.lastId)
i.textures[textureId] = texture i.textures[textureId] = texture
i.renderTargets[renderTargetId] = renderTarget i.renderTargets[renderTargetId] = renderTarget
i.renderTargetToTexture[renderTargetId] = textureId i.renderTargetToTexture[renderTargetId] = textureId
@ -114,10 +112,10 @@ func (i *ids) createRenderTarget(width, height int, filter graphics.Filter) (
// NOTE: renderTarget can't be used as a texture. // NOTE: renderTarget can't be used as a texture.
func (i *ids) addRenderTarget(renderTarget *RenderTarget) graphics.RenderTargetId { func (i *ids) addRenderTarget(renderTarget *RenderTarget) graphics.RenderTargetId {
id := graphics.RenderTargetId(<-i.counts)
i.Lock() i.Lock()
defer i.Unlock() defer i.Unlock()
i.lastId++
id := graphics.RenderTargetId(i.lastId)
i.renderTargets[id] = renderTarget i.renderTargets[id] = renderTarget
return id return id

View File

@ -27,7 +27,6 @@ func DrawTexture(native NativeTexture, projectionMatrix [16]float32,
} }
// TODO: Check performance // TODO: Check performance
shaderProgram := use(projectionMatrix, geometryMatrix, colorMatrix) shaderProgram := use(projectionMatrix, geometryMatrix, colorMatrix)
defer C.glUseProgram(0)
C.glBindTexture(C.GL_TEXTURE_2D, C.GLuint(native)) C.glBindTexture(C.GL_TEXTURE_2D, C.GLuint(native))
defer C.glBindTexture(C.GL_TEXTURE_2D, 0) defer C.glBindTexture(C.GL_TEXTURE_2D, 0)

View File

@ -23,6 +23,7 @@ const (
) )
var programs = map[programId]*program{ var programs = map[programId]*program{
// TODO: programRegular is not used for now. Remove this.
programRegular: &program{ programRegular: &program{
shaderIds: []shaderId{shaderVertex, shaderFragment}, shaderIds: []shaderId{shaderVertex, shaderFragment},
}, },
@ -115,11 +116,9 @@ func getUniformLocation(program C.GLuint, name string) C.GLint {
func use(projectionMatrix [16]float32, func use(projectionMatrix [16]float32,
geometryMatrix matrix.Geometry, geometryMatrix matrix.Geometry,
colorMatrix matrix.Color) C.GLuint { colorMatrix matrix.Color) C.GLuint {
programId := programRegular programId := programColorMatrix
if !colorMatrix.IsIdentity() {
programId = programColorMatrix
}
program := programs[programId] program := programs[programId]
// TODO: Check the performance.
C.glUseProgram(program.native) C.glUseProgram(program.native)
C.glUniformMatrix4fv(C.GLint(getUniformLocation(program.native, "projection_matrix")), C.glUniformMatrix4fv(C.GLint(getUniformLocation(program.native, "projection_matrix")),
@ -143,10 +142,6 @@ func use(projectionMatrix [16]float32,
C.glUniform1i(getUniformLocation(program.native, "texture"), 0) C.glUniform1i(getUniformLocation(program.native, "texture"), 0)
if programId != programColorMatrix {
return program.native
}
e := [4][5]float32{} e := [4][5]float32{}
for i := 0; i < 4; i++ { for i := 0; i < 4; i++ {
for j := 0; j < 5; j++ { for j := 0; j < 5; j++ {