mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-12 12:08:58 +01:00
Add TestPattern
This commit is contained in:
parent
a70cca8a9f
commit
d360f0effe
79
example/game/testpattern/testpattern.go
Normal file
79
example/game/testpattern/testpattern.go
Normal 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())
|
||||||
|
}
|
||||||
|
}
|
BIN
example/images/test_pattern.png
Normal file
BIN
example/images/test_pattern.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 137 B |
@ -8,6 +8,7 @@ import (
|
|||||||
"github.com/hajimehoshi/go-ebiten/example/game/rects"
|
"github.com/hajimehoshi/go-ebiten/example/game/rects"
|
||||||
"github.com/hajimehoshi/go-ebiten/example/game/rotating"
|
"github.com/hajimehoshi/go-ebiten/example/game/rotating"
|
||||||
"github.com/hajimehoshi/go-ebiten/example/game/sprites"
|
"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"
|
||||||
"github.com/hajimehoshi/go-ebiten/ui/cocoa"
|
"github.com/hajimehoshi/go-ebiten/ui/cocoa"
|
||||||
"github.com/hajimehoshi/go-ebiten/ui/glut"
|
"github.com/hajimehoshi/go-ebiten/ui/glut"
|
||||||
@ -43,6 +44,8 @@ func main() {
|
|||||||
game = rotating.New()
|
game = rotating.New()
|
||||||
case "sprites":
|
case "sprites":
|
||||||
game = sprites.New()
|
game = sprites.New()
|
||||||
|
case "testpattern":
|
||||||
|
game = testpattern.New()
|
||||||
}
|
}
|
||||||
|
|
||||||
const screenWidth = 256
|
const screenWidth = 256
|
||||||
|
@ -19,7 +19,7 @@ type TexturePart struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Context interface {
|
type Context interface {
|
||||||
ToTexture(renderTargetID RenderTargetID) TextureID
|
ToTexture(id RenderTargetID) TextureID
|
||||||
|
|
||||||
Clear()
|
Clear()
|
||||||
Fill(r, g, b uint8)
|
Fill(r, g, b uint8)
|
||||||
|
@ -141,13 +141,6 @@ func (context *Context) ResetOffscreen() {
|
|||||||
context.setOffscreen(context.screen)
|
context.setOffscreen(context.screen)
|
||||||
}
|
}
|
||||||
|
|
||||||
func abs(x int) int {
|
|
||||||
if x < 0 {
|
|
||||||
return -x
|
|
||||||
}
|
|
||||||
return x
|
|
||||||
}
|
|
||||||
|
|
||||||
func (context *Context) SetOffscreen(renderTargetID graphics.RenderTargetID) {
|
func (context *Context) SetOffscreen(renderTargetID graphics.RenderTargetID) {
|
||||||
renderTarget := context.textures[C.GLuint(renderTargetID)]
|
renderTarget := context.textures[C.GLuint(renderTargetID)]
|
||||||
context.setOffscreen(renderTarget)
|
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.glBlendFuncSeparate(C.GL_SRC_ALPHA, C.GL_ONE_MINUS_SRC_ALPHA,
|
||||||
C.GL_ZERO, C.GL_ONE)
|
C.GL_ZERO, C.GL_ONE)
|
||||||
|
|
||||||
C.glViewport(0, 0, C.GLsizei(abs(renderTarget.textureWidth)),
|
C.glViewport(0, 0, C.GLsizei(renderTarget.textureWidth),
|
||||||
C.GLsizei(abs(renderTarget.textureHeight)))
|
C.GLsizei(renderTarget.textureHeight))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (context *Context) setMainFramebufferOffscreen() {
|
func (context *Context) setMainFramebufferOffscreen() {
|
||||||
|
Loading…
Reference in New Issue
Block a user