Add ebitenutil/debugprint.go

This commit is contained in:
Hajime Hoshi 2014-12-11 01:59:38 +09:00
parent a39f0e904d
commit d5529d4351
11 changed files with 436 additions and 21 deletions

85
ebitenutil/debugprint.go Normal file
View File

@ -0,0 +1,85 @@
/*
Copyright 2014 Hajime Hoshi
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package ebitenutil
import (
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/internal/assets"
"image/color"
)
type debugPrintState struct {
textTexture ebiten.TextureID
debugPrintRenderTarget ebiten.RenderTargetID
y int
}
var defaultDebugPrintState = new(debugPrintState)
func DebugPrint(ga ebiten.GameContext, gr ebiten.GraphicsContext, str string) {
defaultDebugPrintState.DebugPrint(ga, gr, str)
}
func (d *debugPrintState) drawText(gr ebiten.GraphicsContext, str string, x, y int) {
parts := []ebiten.TexturePart{}
locationX, locationY := 0, 0
for _, c := range str {
if c == '\n' {
locationX = 0
locationY += assets.TextImageCharHeight
continue
}
code := int(c)
const xCharNum = assets.TextImageWidth / assets.TextImageCharWidth
srcX := (code % xCharNum) * assets.TextImageCharWidth
srcY := (code / xCharNum) * assets.TextImageCharHeight
parts = append(parts, ebiten.TexturePart{
LocationX: locationX,
LocationY: locationY,
Source: ebiten.Rect{srcX, srcY, assets.TextImageCharWidth, assets.TextImageCharHeight},
})
locationX += assets.TextImageCharWidth
}
geo := ebiten.GeometryMatrixI()
geo.Translate(float64(x)+1, float64(y))
clr := ebiten.ColorMatrixI()
// TODO: Is this color OK?
clr.Scale(color.RGBA{0x80, 0x80, 0x80, 0xff})
gr.Texture(d.textTexture).Draw(parts, geo, clr)
}
func (d *debugPrintState) DebugPrint(ga ebiten.GameContext, gr ebiten.GraphicsContext, str string) {
if d.textTexture.IsNil() {
img, err := assets.TextImage()
if err != nil {
panic(err)
}
d.textTexture, err = ga.NewTextureID(img, ebiten.FilterNearest)
if err != nil {
panic(err)
}
}
if d.debugPrintRenderTarget.IsNil() {
width, height := 256, 256
var err error
d.debugPrintRenderTarget, err = ga.NewRenderTargetID(width, height, ebiten.FilterNearest)
if err != nil {
panic(err)
}
}
d.drawText(gr, str, 0, d.y)
}

View File

@ -69,7 +69,7 @@ func (game *Game) isInitialized() bool {
return true
}
func (game *Game) Initialize(g ebiten.GameContext) {
func (game *Game) Initialize(g ebiten.GameContext) error {
game.gameContext = g
game.textures = NewTextures(g)
for name, path := range texturePaths {
@ -78,6 +78,7 @@ func (game *Game) Initialize(g ebiten.GameContext) {
for name, size := range renderTargetSizes {
game.textures.RequestRenderTarget(name, size)
}
return nil
}
func (game *Game) Update() error {

View File

@ -28,24 +28,6 @@ Email: info@9031.com
http://www.sozai-page.com/02_sozai/b/b04/b04_002/b04_002.html
```
## text.png
```
-
M+ BITMAP FONTS Copyright 2002-2005 COZ <coz@users.sourceforge.jp>
-
LICENSE
These fonts are free softwares.
Unlimited permission is granted to use, copy, and distribute it, with
or without modification, either commercially and noncommercially.
THESE FONTS ARE PROVIDED "AS IS" WITHOUT WARRANTY.
```
## Other files
```

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 KiB

56
example/paint.go Normal file
View File

@ -0,0 +1,56 @@
package main
import (
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/runner"
"log"
"runtime"
)
const (
screenWidth = 320
screenHeight = 240
)
type Game struct {
gameContext ebiten.GameContext
canvasRenderTarget ebiten.RenderTargetID
}
func (g *Game) Initialize(ga ebiten.GameContext) error {
g.gameContext = ga
return nil
}
func (g *Game) Update() error {
// TODO: Implement
return nil
}
func (g *Game) Draw(gr ebiten.GraphicsContext) error {
if g.canvasRenderTarget.IsNil() {
var err error
g.canvasRenderTarget, err = g.gameContext.NewRenderTargetID(screenWidth, screenHeight, ebiten.FilterNearest)
if err != nil {
return err
}
gr.SetOffscreen(g.canvasRenderTarget)
gr.Fill(0xff, 0xff, 0xff)
}
gr.ResetOffscreen()
ebiten.DrawWhole(gr.RenderTarget(g.canvasRenderTarget), screenWidth, screenHeight, ebiten.GeometryMatrixI(), ebiten.ColorMatrixI())
ebitenutil.DebugPrint(g.gameContext, gr, "Hello\nWorld!")
return nil
}
func init() {
runtime.LockOSThread()
}
func main() {
game := new(Game)
if err := runner.Run(game, screenWidth, screenHeight, 2, "Paint (Ebiten Demo)", 60); err != nil {
log.Fatal(err)
}
}

View File

@ -21,7 +21,7 @@ import (
)
type Game interface {
Initialize(g GameContext)
Initialize(g GameContext) error
Update() error
Draw(gr GraphicsContext) error
}

43
internal/assets/assets.go Normal file
View File

@ -0,0 +1,43 @@
/*
Copyright 2014 Hajime Hoshi
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package assets
import (
"bytes"
"image"
_ "image/png"
)
const FileNameText = "text.png"
//go:generate go-bindata -nocompress -pkg=assets -nomemcopy text.png
const (
TextImageWidth = 192
TextImageHeight = 128
TextImageCharWidth = TextImageWidth / 32
TextImageCharHeight = TextImageHeight / 8
)
func TextImage() (image.Image, error) {
b, err := Asset("text.png")
if err != nil {
return nil, err
}
img, _, err := image.Decode(bytes.NewBuffer(b))
return img, err
}

209
internal/assets/bindata.go Normal file

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,37 @@
# License
## text.png
```
-
M+ BITMAP FONTS Copyright 2002-2005 COZ <coz@users.sourceforge.jp>
-
LICENSE
These fonts are free softwares.
Unlimited permission is granted to use, copy, and distribute it, with
or without modification, either commercially and noncommercially.
THESE FONTS ARE PROVIDED "AS IS" WITHOUT WARRANTY.
```
## Other files
```
Copyright 2014 Hajime Hoshi
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
```

BIN
internal/assets/text.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB

View File

@ -63,7 +63,9 @@ func (u *UI) Start(game ebiten.Game, width, height, scale int, title string) err
}
u.canvas = c
game.Initialize(c)
if err := game.Initialize(c); err != nil {
return err
}
return nil
}