mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-26 02:42:02 +01:00
Draggin ebiten
This commit is contained in:
parent
a25a2df6b4
commit
b14684804f
@ -12,19 +12,32 @@ var TexturePaths = map[string]string{
|
|||||||
"text": "images/text.png",
|
"text": "images/text.png",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type drawInfo struct {
|
||||||
|
textures map[string]graphics.TextureId
|
||||||
|
inputStr string
|
||||||
|
textureGeo matrix.Geometry
|
||||||
|
}
|
||||||
|
|
||||||
type Game struct {
|
type Game struct {
|
||||||
textures map[string]graphics.TextureId
|
inputX int
|
||||||
inputStateUpdatedCh chan ui.InputStateUpdatedEvent
|
inputY int
|
||||||
x int
|
inputPrevX int
|
||||||
y int
|
inputPrevY int
|
||||||
|
counter int
|
||||||
|
drawInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewGame() *Game {
|
func NewGame() *Game {
|
||||||
return &Game{
|
return &Game{
|
||||||
textures: map[string]graphics.TextureId{},
|
inputX: -1,
|
||||||
inputStateUpdatedCh: make(chan ui.InputStateUpdatedEvent),
|
inputY: -1,
|
||||||
x: -1,
|
inputPrevX: -1,
|
||||||
y: -1,
|
inputPrevY: -1,
|
||||||
|
counter: 0,
|
||||||
|
drawInfo: drawInfo{
|
||||||
|
textures: map[string]graphics.TextureId{},
|
||||||
|
textureGeo: matrix.IdentityGeometry(),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -36,34 +49,33 @@ func (game *Game) OnTextureCreated(e graphics.TextureCreatedEvent) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (game *Game) OnInputStateUpdated(e ui.InputStateUpdatedEvent) {
|
func (game *Game) OnInputStateUpdated(e ui.InputStateUpdatedEvent) {
|
||||||
go func() {
|
game.inputX, game.inputY = e.X, e.Y
|
||||||
e := e
|
|
||||||
game.inputStateUpdatedCh <- e
|
|
||||||
}()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (game *Game) Update() {
|
func (game *Game) Update() {
|
||||||
events:
|
game.counter++
|
||||||
for {
|
game.drawInfo.inputStr = fmt.Sprintf(`Input State:
|
||||||
select {
|
X: %d
|
||||||
case e := <-game.inputStateUpdatedCh:
|
Y: %d`, game.inputX, game.inputY)
|
||||||
game.x, game.y = e.X, e.Y
|
|
||||||
default:
|
if game.inputPrevX != -1 && game.inputPrevY != -1 &&
|
||||||
break events
|
game.inputX != -1 && game.inputY != -1 {
|
||||||
}
|
dx, dy := game.inputX - game.inputPrevX, game.inputY - game.inputPrevY
|
||||||
|
game.drawInfo.textureGeo.Translate(float64(dx), float64(dy))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update for the next frame.
|
||||||
|
game.inputPrevX, game.inputPrevY = game.inputX, game.inputY
|
||||||
}
|
}
|
||||||
|
|
||||||
func (game *Game) Draw(g graphics.Canvas) {
|
func (game *Game) Draw(g graphics.Canvas) {
|
||||||
if len(game.textures) < len(TexturePaths) {
|
if len(game.drawInfo.textures) < len(TexturePaths) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
g.Fill(128, 128, 255)
|
g.Fill(128, 128, 255)
|
||||||
str := fmt.Sprintf(`Input State:
|
game.drawTexture(g, game.drawInfo.textureGeo, matrix.IdentityColor())
|
||||||
X: %d
|
game.drawText(g, game.drawInfo.inputStr, 5, 5)
|
||||||
Y: %d`, game.x, game.y)
|
|
||||||
game.drawText(g, str, 5, 5)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (game *Game) drawText(g graphics.Canvas, text string, x, y int) {
|
func (game *Game) drawText(g graphics.Canvas, text string, x, y int) {
|
||||||
@ -94,6 +106,10 @@ func (game *Game) drawText(g graphics.Canvas, text string, x, y int) {
|
|||||||
geometryMatrix := matrix.IdentityGeometry()
|
geometryMatrix := matrix.IdentityGeometry()
|
||||||
geometryMatrix.Translate(float64(x), float64(y))
|
geometryMatrix.Translate(float64(x), float64(y))
|
||||||
colorMatrix := matrix.IdentityColor()
|
colorMatrix := matrix.IdentityColor()
|
||||||
g.DrawTextureParts(game.textures["text"], parts,
|
g.DrawTextureParts(game.drawInfo.textures["text"], parts,
|
||||||
geometryMatrix, colorMatrix)
|
geometryMatrix, colorMatrix)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (game *Game) drawTexture(g graphics.Canvas, geo matrix.Geometry, color matrix.Color) {
|
||||||
|
g.DrawTexture(game.drawInfo.textures["ebiten"], geo, color)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user