Add TestPattern

This commit is contained in:
Hajime Hoshi 2013-10-19 02:58:00 +09:00
parent a70cca8a9f
commit d360f0effe
6 changed files with 87 additions and 12 deletions

View File

@ -0,0 +1,79 @@
package testpattern
import (
"github.com/hajimehoshi/go-ebiten"
"github.com/hajimehoshi/go-ebiten/graphics"
"github.com/hajimehoshi/go-ebiten/graphics/matrix"
"image"
_ "image/png"
"math"
"os"
)
type TestPattern struct {
textureId graphics.TextureID
textureWidth int
textureHeight int
geos []matrix.Geometry
}
func New() *TestPattern {
return &TestPattern{}
}
func (game *TestPattern) Init(tf graphics.TextureFactory) {
file, err := os.Open("images/test_pattern.png")
if err != nil {
panic(err)
}
defer file.Close()
img, _, err := image.Decode(file)
if err != nil {
panic(err)
}
if game.textureId, err = tf.NewTextureFromImage(img); err != nil {
panic(err)
}
size := img.Bounds().Size()
game.textureWidth = size.X
game.textureHeight = size.Y
}
func (game *TestPattern) Update(context ebiten.GameContext) {
geo := matrix.IdentityGeometry()
geo.Translate(13, 13)
game.geos = append(game.geos, geo)
geo = matrix.IdentityGeometry()
geo.Translate(float64(13+game.textureWidth), 13)
game.geos = append(game.geos, geo)
geo = matrix.IdentityGeometry()
geo.Translate(float64(13+game.textureWidth*2), 13)
game.geos = append(game.geos, geo)
geo = matrix.IdentityGeometry()
geo.Translate(float64(13+game.textureWidth*3), 13)
game.geos = append(game.geos, geo)
geo = matrix.IdentityGeometry()
geo.Scale(2, 2)
geo.Translate(13, float64(13+game.textureHeight))
game.geos = append(game.geos, geo)
geo = matrix.IdentityGeometry()
geo.Rotate(math.Pi)
geo.Scale(2, 2)
geo.Translate(float64(game.textureWidth*2),
float64(game.textureHeight*2))
geo.Translate(float64(13+game.textureWidth*2),
float64(13+game.textureHeight))
game.geos = append(game.geos, geo)
}
func (game *TestPattern) Draw(g graphics.Context) {
for _, geo := range game.geos {
g.DrawTexture(game.textureId, geo, matrix.IdentityColor())
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 137 B

View File

@ -8,6 +8,7 @@ import (
"github.com/hajimehoshi/go-ebiten/example/game/rects"
"github.com/hajimehoshi/go-ebiten/example/game/rotating"
"github.com/hajimehoshi/go-ebiten/example/game/sprites"
"github.com/hajimehoshi/go-ebiten/example/game/testpattern"
"github.com/hajimehoshi/go-ebiten/ui"
"github.com/hajimehoshi/go-ebiten/ui/cocoa"
"github.com/hajimehoshi/go-ebiten/ui/glut"
@ -43,6 +44,8 @@ func main() {
game = rotating.New()
case "sprites":
game = sprites.New()
case "testpattern":
game = testpattern.New()
}
const screenWidth = 256

View File

@ -19,7 +19,7 @@ type TexturePart struct {
}
type Context interface {
ToTexture(renderTargetID RenderTargetID) TextureID
ToTexture(id RenderTargetID) TextureID
Clear()
Fill(r, g, b uint8)

View File

@ -141,13 +141,6 @@ func (context *Context) ResetOffscreen() {
context.setOffscreen(context.screen)
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
func (context *Context) SetOffscreen(renderTargetID graphics.RenderTargetID) {
renderTarget := context.textures[C.GLuint(renderTargetID)]
context.setOffscreen(renderTarget)
@ -168,8 +161,8 @@ func (context *Context) setOffscreen(renderTarget *Texture) {
C.glBlendFuncSeparate(C.GL_SRC_ALPHA, C.GL_ONE_MINUS_SRC_ALPHA,
C.GL_ZERO, C.GL_ONE)
C.glViewport(0, 0, C.GLsizei(abs(renderTarget.textureWidth)),
C.GLsizei(abs(renderTarget.textureHeight)))
C.glViewport(0, 0, C.GLsizei(renderTarget.textureWidth),
C.GLsizei(renderTarget.textureHeight))
}
func (context *Context) setMainFramebufferOffscreen() {