2013-12-08 08:19:24 +01:00
|
|
|
package main
|
2013-07-02 18:27:04 +02:00
|
|
|
|
|
|
|
import (
|
2013-12-08 11:38:22 +01:00
|
|
|
"image/color"
|
2013-07-04 16:57:53 +02:00
|
|
|
"fmt"
|
2013-10-14 04:34:58 +02:00
|
|
|
"github.com/hajimehoshi/go-ebiten/graphics"
|
|
|
|
"github.com/hajimehoshi/go-ebiten/graphics/matrix"
|
2013-12-02 16:15:01 +01:00
|
|
|
"github.com/hajimehoshi/go-ebiten/ui"
|
2013-07-02 18:27:04 +02:00
|
|
|
)
|
|
|
|
|
2013-12-08 08:19:24 +01:00
|
|
|
var TexturePaths = map[string]string{
|
|
|
|
"ebiten": "images/ebiten.png",
|
2013-12-08 08:28:56 +01:00
|
|
|
"text": "images/text.png",
|
2013-12-08 08:19:24 +01:00
|
|
|
}
|
|
|
|
|
2013-12-08 09:02:35 +01:00
|
|
|
type drawInfo struct {
|
|
|
|
textures map[string]graphics.TextureId
|
|
|
|
inputStr string
|
|
|
|
textureGeo matrix.Geometry
|
|
|
|
}
|
|
|
|
|
2013-12-08 08:19:24 +01:00
|
|
|
type Game struct {
|
2013-12-08 09:02:35 +01:00
|
|
|
inputX int
|
|
|
|
inputY int
|
|
|
|
inputPrevX int
|
|
|
|
inputPrevY int
|
|
|
|
counter int
|
|
|
|
drawInfo
|
2013-07-02 18:27:04 +02:00
|
|
|
}
|
|
|
|
|
2013-12-08 08:19:24 +01:00
|
|
|
func NewGame() *Game {
|
|
|
|
return &Game{
|
2013-12-08 09:02:35 +01:00
|
|
|
inputX: -1,
|
|
|
|
inputY: -1,
|
|
|
|
inputPrevX: -1,
|
|
|
|
inputPrevY: -1,
|
|
|
|
counter: 0,
|
|
|
|
drawInfo: drawInfo{
|
|
|
|
textures: map[string]graphics.TextureId{},
|
|
|
|
textureGeo: matrix.IdentityGeometry(),
|
|
|
|
},
|
2013-11-29 19:21:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-08 08:19:24 +01:00
|
|
|
func (game *Game) OnTextureCreated(e graphics.TextureCreatedEvent) {
|
|
|
|
if e.Error != nil {
|
|
|
|
panic(e.Error)
|
|
|
|
}
|
|
|
|
game.textures[e.Tag.(string)] = e.Id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (game *Game) OnInputStateUpdated(e ui.InputStateUpdatedEvent) {
|
2013-12-08 09:02:35 +01:00
|
|
|
game.inputX, game.inputY = e.X, e.Y
|
2013-07-02 18:27:04 +02:00
|
|
|
}
|
|
|
|
|
2013-12-08 08:19:24 +01:00
|
|
|
func (game *Game) Update() {
|
2013-12-08 09:02:35 +01:00
|
|
|
game.counter++
|
|
|
|
game.drawInfo.inputStr = fmt.Sprintf(`Input State:
|
|
|
|
X: %d
|
|
|
|
Y: %d`, game.inputX, game.inputY)
|
|
|
|
|
|
|
|
if game.inputPrevX != -1 && game.inputPrevY != -1 &&
|
|
|
|
game.inputX != -1 && game.inputY != -1 {
|
|
|
|
dx, dy := game.inputX - game.inputPrevX, game.inputY - game.inputPrevY
|
|
|
|
game.drawInfo.textureGeo.Translate(float64(dx), float64(dy))
|
2013-11-29 19:21:10 +01:00
|
|
|
}
|
2013-12-08 09:02:35 +01:00
|
|
|
|
|
|
|
// Update for the next frame.
|
|
|
|
game.inputPrevX, game.inputPrevY = game.inputX, game.inputY
|
2013-07-02 18:27:04 +02:00
|
|
|
}
|
|
|
|
|
2013-12-08 08:19:24 +01:00
|
|
|
func (game *Game) Draw(g graphics.Canvas) {
|
2013-12-08 09:02:35 +01:00
|
|
|
if len(game.drawInfo.textures) < len(TexturePaths) {
|
2013-12-08 08:19:24 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-10-11 20:20:13 +02:00
|
|
|
g.Fill(128, 128, 255)
|
2013-12-08 09:02:35 +01:00
|
|
|
game.drawTexture(g, game.drawInfo.textureGeo, matrix.IdentityColor())
|
2013-12-08 11:38:22 +01:00
|
|
|
game.drawText(g, game.drawInfo.inputStr, 6, 6, &color.RGBA{0x0, 0x0, 0x0, 0x80})
|
|
|
|
game.drawText(g, game.drawInfo.inputStr, 5, 5, color.White)
|
2013-07-02 18:27:04 +02:00
|
|
|
}
|
|
|
|
|
2013-12-08 11:38:22 +01:00
|
|
|
func (game *Game) drawText(g graphics.Canvas, text string, x, y int, clr color.Color) {
|
2013-07-04 16:57:53 +02:00
|
|
|
const letterWidth = 6
|
|
|
|
const letterHeight = 16
|
|
|
|
|
|
|
|
parts := []graphics.TexturePart{}
|
|
|
|
textX := 0
|
|
|
|
textY := 0
|
|
|
|
for _, c := range text {
|
|
|
|
if c == '\n' {
|
|
|
|
textX = 0
|
|
|
|
textY += letterHeight
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
code := int(c)
|
|
|
|
x := (code % 32) * letterWidth
|
|
|
|
y := (code / 32) * letterHeight
|
|
|
|
source := graphics.Rect{x, y, letterWidth, letterHeight}
|
|
|
|
parts = append(parts, graphics.TexturePart{
|
|
|
|
LocationX: textX,
|
|
|
|
LocationY: textY,
|
|
|
|
Source: source,
|
|
|
|
})
|
|
|
|
textX += letterWidth
|
|
|
|
}
|
|
|
|
|
|
|
|
geometryMatrix := matrix.IdentityGeometry()
|
|
|
|
geometryMatrix.Translate(float64(x), float64(y))
|
|
|
|
colorMatrix := matrix.IdentityColor()
|
2013-12-08 11:38:22 +01:00
|
|
|
colorMatrix.Scale(clr)
|
2013-12-08 09:02:35 +01:00
|
|
|
g.DrawTextureParts(game.drawInfo.textures["text"], parts,
|
2013-07-04 16:57:53 +02:00
|
|
|
geometryMatrix, colorMatrix)
|
2013-07-02 18:27:04 +02:00
|
|
|
}
|
2013-12-08 09:02:35 +01:00
|
|
|
|
|
|
|
func (game *Game) drawTexture(g graphics.Canvas, geo matrix.Geometry, color matrix.Color) {
|
|
|
|
g.DrawTexture(game.drawInfo.textures["ebiten"], geo, color)
|
|
|
|
}
|