mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-02-20 15:00:08 +01:00
Remove unnecessary channels
This commit is contained in:
parent
1c187597e6
commit
86846b0171
@ -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))
|
||||
for i, blockCol := range f.blocks {
|
||||
blocks[i] = make([]BlockType, len(blockCol))
|
||||
|
@ -19,7 +19,7 @@ func textWidth(str string) int {
|
||||
|
||||
func drawText(
|
||||
context graphics.Context,
|
||||
textures *Textures,
|
||||
textures Textures,
|
||||
str string,
|
||||
x, y, scale int,
|
||||
clr color.Color) {
|
||||
@ -55,7 +55,7 @@ func drawText(
|
||||
|
||||
func drawTextWithShadow(
|
||||
context graphics.Context,
|
||||
textures *Textures,
|
||||
textures Textures,
|
||||
str string,
|
||||
x, y, scale int,
|
||||
clr color.Color) {
|
||||
|
@ -23,17 +23,25 @@ type GameState struct {
|
||||
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 {
|
||||
sceneManager *SceneManager
|
||||
input *Input
|
||||
textures *Textures
|
||||
textures Textures
|
||||
}
|
||||
|
||||
func NewGame(textureFactory graphics.TextureFactory) *Game {
|
||||
func NewGame(textures Textures) *Game {
|
||||
game := &Game{
|
||||
sceneManager: NewSceneManager(NewTitleScene()),
|
||||
input: NewInput(),
|
||||
textures: NewTextures(textureFactory),
|
||||
textures: textures,
|
||||
}
|
||||
for name, path := range texturePaths {
|
||||
game.textures.RequestTexture(name, path)
|
||||
|
@ -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)
|
||||
|
||||
field := textures.GetTexture("empty")
|
||||
|
@ -125,7 +125,7 @@ const fieldBlockNumY = 20
|
||||
|
||||
func drawBlocks(
|
||||
context graphics.Context,
|
||||
textures *Textures,
|
||||
textures Textures,
|
||||
blocks [][]BlockType,
|
||||
geo matrix.Geometry) {
|
||||
parts := []graphics.TexturePart{}
|
||||
@ -211,7 +211,7 @@ func (p *Piece) AbsorbInto(field *Field, x, y int, angle Angle) {
|
||||
|
||||
func (p *Piece) Draw(
|
||||
context graphics.Context,
|
||||
textures *Textures,
|
||||
textures Textures,
|
||||
fieldX, fieldY int,
|
||||
pieceX, pieceY int,
|
||||
angle Angle) {
|
||||
|
@ -14,7 +14,7 @@ func init() {
|
||||
|
||||
type Scene interface {
|
||||
Update(state *GameState)
|
||||
Draw(context graphics.Context, textures *Textures)
|
||||
Draw(context graphics.Context, textures Textures)
|
||||
}
|
||||
|
||||
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 {
|
||||
s.current.Draw(context, textures)
|
||||
return
|
||||
|
@ -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)
|
||||
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})
|
||||
}
|
||||
|
||||
func drawTitleBackground(context graphics.Context, textures *Textures, c int) {
|
||||
func drawTitleBackground(context graphics.Context, textures Textures, c int) {
|
||||
const textureWidth = 32
|
||||
const textureHeight = 32
|
||||
|
||||
@ -60,7 +60,7 @@ func drawTitleBackground(context graphics.Context, textures *Textures, c int) {
|
||||
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
|
||||
textWidth := textWidth(str) * scale
|
||||
x := (ScreenWidth - textWidth) / 2
|
||||
|
@ -30,7 +30,6 @@ func main() {
|
||||
const title = "Ebiten Demo"
|
||||
|
||||
u := cocoa.UI()
|
||||
textureFactory := cocoa.TextureFactory()
|
||||
window := u.CreateGameWindow(screenWidth, screenHeight, screenScale, title)
|
||||
|
||||
drawing := make(chan struct{})
|
||||
@ -39,7 +38,8 @@ func main() {
|
||||
defer close(quit)
|
||||
|
||||
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))
|
||||
tick := time.Tick(frameTime)
|
||||
for {
|
||||
|
@ -1,6 +1,7 @@
|
||||
package blocks
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/hajimehoshi/go-ebiten/example/blocks"
|
||||
"github.com/hajimehoshi/go-ebiten/graphics"
|
||||
"image"
|
||||
"os"
|
||||
@ -14,7 +15,7 @@ type namePath struct {
|
||||
|
||||
type nameSize struct {
|
||||
name string
|
||||
size Size
|
||||
size blocks.Size
|
||||
}
|
||||
|
||||
type Textures struct {
|
||||
@ -98,7 +99,7 @@ func (t *Textures) RequestTexture(name string, path string) {
|
||||
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}
|
||||
}
|
||||
|
@ -35,7 +35,7 @@ type ids struct {
|
||||
textures map[graphics.TextureId]*Texture
|
||||
renderTargets map[graphics.RenderTargetId]*RenderTarget
|
||||
renderTargetToTexture map[graphics.RenderTargetId]graphics.TextureId
|
||||
counts chan int
|
||||
lastId int
|
||||
currentRenderTargetId graphics.RenderTargetId
|
||||
sync.RWMutex
|
||||
}
|
||||
@ -45,14 +45,9 @@ func newIds() *ids {
|
||||
textures: map[graphics.TextureId]*Texture{},
|
||||
renderTargets: map[graphics.RenderTargetId]*RenderTarget{},
|
||||
renderTargetToTexture: map[graphics.RenderTargetId]graphics.TextureId{},
|
||||
counts: make(chan int),
|
||||
lastId: 0,
|
||||
currentRenderTargetId: -1,
|
||||
}
|
||||
go func() {
|
||||
for i := 1; ; i++ {
|
||||
ids.counts <- i
|
||||
}
|
||||
}()
|
||||
return ids
|
||||
}
|
||||
|
||||
@ -80,10 +75,11 @@ func (i *ids) createTexture(img image.Image, filter graphics.Filter) (
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
textureId := graphics.TextureId(<-i.counts)
|
||||
|
||||
i.Lock()
|
||||
defer i.Unlock()
|
||||
i.lastId++
|
||||
textureId := graphics.TextureId(i.lastId)
|
||||
i.textures[textureId] = texture
|
||||
return textureId, nil
|
||||
}
|
||||
@ -100,11 +96,13 @@ func (i *ids) createRenderTarget(width, height int, filter graphics.Filter) (
|
||||
i.currentRenderTargetId = -1
|
||||
renderTarget := &RenderTarget{framebuffer, texture.width, texture.height, false}
|
||||
|
||||
textureId := graphics.TextureId(<-i.counts)
|
||||
renderTargetId := graphics.RenderTargetId(<-i.counts)
|
||||
|
||||
i.Lock()
|
||||
defer i.Unlock()
|
||||
i.lastId++
|
||||
textureId := graphics.TextureId(i.lastId)
|
||||
i.lastId++
|
||||
renderTargetId := graphics.RenderTargetId(i.lastId)
|
||||
|
||||
i.textures[textureId] = texture
|
||||
i.renderTargets[renderTargetId] = renderTarget
|
||||
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.
|
||||
func (i *ids) addRenderTarget(renderTarget *RenderTarget) graphics.RenderTargetId {
|
||||
id := graphics.RenderTargetId(<-i.counts)
|
||||
|
||||
i.Lock()
|
||||
defer i.Unlock()
|
||||
i.lastId++
|
||||
id := graphics.RenderTargetId(i.lastId)
|
||||
i.renderTargets[id] = renderTarget
|
||||
|
||||
return id
|
||||
|
@ -27,7 +27,6 @@ func DrawTexture(native NativeTexture, projectionMatrix [16]float32,
|
||||
}
|
||||
// TODO: Check performance
|
||||
shaderProgram := use(projectionMatrix, geometryMatrix, colorMatrix)
|
||||
defer C.glUseProgram(0)
|
||||
|
||||
C.glBindTexture(C.GL_TEXTURE_2D, C.GLuint(native))
|
||||
defer C.glBindTexture(C.GL_TEXTURE_2D, 0)
|
||||
|
@ -23,6 +23,7 @@ const (
|
||||
)
|
||||
|
||||
var programs = map[programId]*program{
|
||||
// TODO: programRegular is not used for now. Remove this.
|
||||
programRegular: &program{
|
||||
shaderIds: []shaderId{shaderVertex, shaderFragment},
|
||||
},
|
||||
@ -115,11 +116,9 @@ func getUniformLocation(program C.GLuint, name string) C.GLint {
|
||||
func use(projectionMatrix [16]float32,
|
||||
geometryMatrix matrix.Geometry,
|
||||
colorMatrix matrix.Color) C.GLuint {
|
||||
programId := programRegular
|
||||
if !colorMatrix.IsIdentity() {
|
||||
programId = programColorMatrix
|
||||
}
|
||||
programId := programColorMatrix
|
||||
program := programs[programId]
|
||||
// TODO: Check the performance.
|
||||
C.glUseProgram(program.native)
|
||||
|
||||
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)
|
||||
|
||||
if programId != programColorMatrix {
|
||||
return program.native
|
||||
}
|
||||
|
||||
e := [4][5]float32{}
|
||||
for i := 0; i < 4; i++ {
|
||||
for j := 0; j < 5; j++ {
|
||||
|
Loading…
Reference in New Issue
Block a user