examples: Use RunGame

Updates #1111
This commit is contained in:
Hajime Hoshi 2020-04-12 18:18:45 +09:00
parent cd4a0ba489
commit e16a4cd85c
10 changed files with 279 additions and 182 deletions

View File

@ -36,11 +36,13 @@ var (
ebitenImage *ebiten.Image ebitenImage *ebiten.Image
) )
func update(screen *ebiten.Image) error { type Game struct{}
if ebiten.IsDrawingSkipped() {
return nil
}
func (g *Game) Update(screen *ebiten.Image) error {
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
const ( const (
// The offset point to render the image. // The offset point to render the image.
ox = 10 ox = 10
@ -60,8 +62,10 @@ func update(screen *ebiten.Image) error {
op.GeoM.Translate(ox+float64(w), oy) op.GeoM.Translate(ox+float64(w), oy)
op.CompositeMode = ebiten.CompositeModeLighter op.CompositeMode = ebiten.CompositeModeLighter
screen.DrawImage(ebitenImage, op) screen.DrawImage(ebitenImage, op)
}
return nil func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
} }
func main() { func main() {
@ -80,7 +84,9 @@ func main() {
} }
ebitenImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault) ebitenImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Additive Blending (Ebiten Demo)"); err != nil { ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
ebiten.SetWindowTitle("Additive Blending (Ebiten Demo)")
if err := ebiten.RunGame(&Game{}); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }

View File

@ -102,11 +102,13 @@ func drawRect(screen *ebiten.Image, img *ebiten.Image, x, y, width, height float
ebitenutil.DebugPrintAt(screen, msg, int(x), int(y)-16) ebitenutil.DebugPrintAt(screen, msg, int(x), int(y)-16)
} }
func update(screen *ebiten.Image) error { type Game struct{}
if ebiten.IsDrawingSkipped() {
return nil
}
func (g *Game) Update(screen *ebiten.Image) error {
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
const ox, oy = 40, 60 const ox, oy = 40, 60
drawRect(screen, ebitenImage, ox, oy, 200, 100, ebiten.AddressClampToZero, "Regular") drawRect(screen, ebitenImage, ox, oy, 200, 100, ebiten.AddressClampToZero, "Regular")
drawRect(screen, ebitenImage, 220+ox, oy, 200, 100, ebiten.AddressRepeat, "Regular, Repeat") drawRect(screen, ebitenImage, 220+ox, oy, 200, 100, ebiten.AddressRepeat, "Regular, Repeat")
@ -114,11 +116,16 @@ func update(screen *ebiten.Image) error {
subImage := ebitenImage.SubImage(image.Rect(10, 5, 20, 30)).(*ebiten.Image) subImage := ebitenImage.SubImage(image.Rect(10, 5, 20, 30)).(*ebiten.Image)
drawRect(screen, subImage, ox, 200+oy, 200, 100, ebiten.AddressClampToZero, "Subimage") drawRect(screen, subImage, ox, 200+oy, 200, 100, ebiten.AddressClampToZero, "Subimage")
drawRect(screen, subImage, 220+ox, 200+oy, 200, 100, ebiten.AddressRepeat, "Subimage, Repeat") drawRect(screen, subImage, 220+ox, 200+oy, 200, 100, ebiten.AddressRepeat, "Subimage, Repeat")
return nil }
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
} }
func main() { func main() {
if err := ebiten.Run(update, screenWidth, screenHeight, 1, "Sampler Address (Ebiten Demo)"); err != nil { ebiten.SetWindowSize(screenWidth, screenHeight)
ebiten.SetWindowTitle("Sampler Address (Ebiten Demo)")
if err := ebiten.RunGame(&Game{}); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }

View File

@ -38,12 +38,8 @@ const (
) )
var ( var (
skyColor = color.RGBA{0x66, 0xcc, 0xff, 0xff} skyColor = color.RGBA{0x66, 0xcc, 0xff, 0xff}
thePlayer = &player{
x16: 16 * 100,
y16: 16 * 200,
angle: maxAngle * 3 / 4,
}
gophersImage *ebiten.Image gophersImage *ebiten.Image
repeatedGophersImage *ebiten.Image repeatedGophersImage *ebiten.Image
groundImage *ebiten.Image groundImage *ebiten.Image
@ -186,11 +182,11 @@ func (p *player) Angle() int {
} }
// updateGroundImage updates the ground image according to the current player's position. // updateGroundImage updates the ground image according to the current player's position.
func updateGroundImage(ground *ebiten.Image) { func (g *Game) updateGroundImage(ground *ebiten.Image) {
ground.Clear() ground.Clear()
x16, y16 := thePlayer.Position() x16, y16 := g.player.Position()
a := thePlayer.Angle() a := g.player.Angle()
gw, gh := ground.Size() gw, gh := ground.Size()
w, h := gophersImage.Size() w, h := gophersImage.Size()
op := &ebiten.DrawImageOptions{} op := &ebiten.DrawImageOptions{}
@ -202,7 +198,7 @@ func updateGroundImage(ground *ebiten.Image) {
} }
// drawGroundImage draws the ground image to the given screen image. // drawGroundImage draws the ground image to the given screen image.
func drawGroundImage(screen *ebiten.Image, ground *ebiten.Image) { func (g *Game) drawGroundImage(screen *ebiten.Image, ground *ebiten.Image) {
perspectiveGroundImage.Clear() perspectiveGroundImage.Clear()
gw, _ := ground.Size() gw, _ := ground.Size()
pw, ph := perspectiveGroundImage.Size() pw, ph := perspectiveGroundImage.Size()
@ -225,47 +221,65 @@ func drawGroundImage(screen *ebiten.Image, ground *ebiten.Image) {
op := &ebiten.DrawImageOptions{} op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(-float64(pw)/2, 0) op.GeoM.Translate(-float64(pw)/2, 0)
op.GeoM.Rotate(-1 * float64(thePlayer.lean) / maxLean * math.Pi / 8) op.GeoM.Rotate(-1 * float64(g.player.lean) / maxLean * math.Pi / 8)
op.GeoM.Translate(float64(screenWidth)/2, screenHeight/3) op.GeoM.Translate(float64(screenWidth)/2, screenHeight/3)
screen.DrawImage(perspectiveGroundImage, op) screen.DrawImage(perspectiveGroundImage, op)
} }
func update(screen *ebiten.Image) error { type Game struct {
player *player
}
func NewGame() *Game {
return &Game{
player: &player{
x16: 16 * 100,
y16: 16 * 200,
angle: maxAngle * 3 / 4,
},
}
}
func (g *Game) Update(screen *ebiten.Image) error {
// Manipulate the player by the input. // Manipulate the player by the input.
if ebiten.IsKeyPressed(ebiten.KeySpace) { if ebiten.IsKeyPressed(ebiten.KeySpace) {
thePlayer.MoveForward() g.player.MoveForward()
} }
rotated := false rotated := false
if ebiten.IsKeyPressed(ebiten.KeyRight) { if ebiten.IsKeyPressed(ebiten.KeyRight) {
thePlayer.RotateRight() g.player.RotateRight()
rotated = true rotated = true
} }
if ebiten.IsKeyPressed(ebiten.KeyLeft) { if ebiten.IsKeyPressed(ebiten.KeyLeft) {
thePlayer.RotateLeft() g.player.RotateLeft()
rotated = true rotated = true
} }
if !rotated { if !rotated {
thePlayer.Stabilize() g.player.Stabilize()
}
if ebiten.IsDrawingSkipped() {
return nil
} }
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
// Draw the ground image. // Draw the ground image.
screen.Fill(skyColor) screen.Fill(skyColor)
updateGroundImage(groundImage) g.updateGroundImage(groundImage)
drawGroundImage(screen, groundImage) g.drawGroundImage(screen, groundImage)
// Draw the message. // Draw the message.
tutrial := "Space: Move forward\nLeft/Right: Rotate" tutrial := "Space: Move forward\nLeft/Right: Rotate"
msg := fmt.Sprintf("TPS: %0.2f\nFPS: %0.2f\n%s", ebiten.CurrentTPS(), ebiten.CurrentFPS(), tutrial) msg := fmt.Sprintf("TPS: %0.2f\nFPS: %0.2f\n%s", ebiten.CurrentTPS(), ebiten.CurrentFPS(), tutrial)
ebitenutil.DebugPrint(screen, msg) ebitenutil.DebugPrint(screen, msg)
return nil }
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
} }
func main() { func main() {
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Air Ship (Ebiten Demo)"); err != nil { ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
ebiten.SetWindowTitle("Air Ship (Ebiten Demo)")
if err := ebiten.RunGame(NewGame()); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }

View File

@ -33,38 +33,48 @@ const (
) )
var ( var (
count int
ebitenImage *ebiten.Image ebitenImage *ebiten.Image
) )
func update(screen *ebiten.Image) error { type Game struct {
count++ count int
count %= ebiten.MaxTPS() * 10 }
diff := float64(count) * 0.2
func (g *Game) Update(screen *ebiten.Image) error {
g.count++
g.count %= ebiten.MaxTPS() * 10
return nil
}
func (g *Game) offset() float64 {
v := float64(g.count) * 0.2
switch { switch {
case 480 < count: case 480 < g.count:
diff = 0 v = 0
case 240 < count: case 240 < g.count:
diff = float64(480-count) * 0.2 v = float64(480-g.count) * 0.2
}
if ebiten.IsDrawingSkipped() {
return nil
} }
return v
}
func (g *Game) Draw(screen *ebiten.Image) {
screen.Fill(color.NRGBA{0x00, 0x00, 0x80, 0xff}) screen.Fill(color.NRGBA{0x00, 0x00, 0x80, 0xff})
// Draw 100 Ebitens // Draw 100 Ebitens
v := g.offset()
op := &ebiten.DrawImageOptions{} op := &ebiten.DrawImageOptions{}
op.ColorM.Scale(1.0, 1.0, 1.0, 0.5) op.ColorM.Scale(1.0, 1.0, 1.0, 0.5)
for i := 0; i < 10*10; i++ { for i := 0; i < 10*10; i++ {
op.GeoM.Reset() op.GeoM.Reset()
x := float64(i%10)*diff + 15 x := float64(i%10)*v + 15
y := float64(i/10)*diff + 20 y := float64(i/10)*v + 20
op.GeoM.Translate(x, y) op.GeoM.Translate(x, y)
screen.DrawImage(ebitenImage, op) screen.DrawImage(ebitenImage, op)
} }
return nil }
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
} }
func main() { func main() {
@ -83,7 +93,9 @@ func main() {
} }
ebitenImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault) ebitenImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Alpha Blending (Ebiten Demo)"); err != nil { ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
ebiten.SetWindowTitle("Alpha Blending (Ebiten Demo)")
if err := ebiten.RunGame(&Game{}); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }

View File

@ -38,24 +38,29 @@ const (
) )
var ( var (
count = 0
runnerImage *ebiten.Image runnerImage *ebiten.Image
) )
func update(screen *ebiten.Image) error { type Game struct {
count++ count int
}
if ebiten.IsDrawingSkipped() { func (g *Game) Update(screen *ebiten.Image) error {
return nil g.count++
} return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
op := &ebiten.DrawImageOptions{} op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(-float64(frameWidth)/2, -float64(frameHeight)/2) op.GeoM.Translate(-float64(frameWidth)/2, -float64(frameHeight)/2)
op.GeoM.Translate(screenWidth/2, screenHeight/2) op.GeoM.Translate(screenWidth/2, screenHeight/2)
i := (count / 5) % frameNum i := (g.count / 5) % frameNum
sx, sy := frameOX+i*frameWidth, frameOY sx, sy := frameOX+i*frameWidth, frameOY
screen.DrawImage(runnerImage.SubImage(image.Rect(sx, sy, sx+frameWidth, sy+frameHeight)).(*ebiten.Image), op) screen.DrawImage(runnerImage.SubImage(image.Rect(sx, sy, sx+frameWidth, sy+frameHeight)).(*ebiten.Image), op)
return nil }
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
} }
func main() { func main() {
@ -74,7 +79,9 @@ func main() {
} }
runnerImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault) runnerImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Animation (Ebiten Demo)"); err != nil { ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
ebiten.SetWindowTitle("Animation (Ebiten Demo)")
if err := ebiten.RunGame(&Game{}); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }

View File

@ -256,28 +256,46 @@ Press U to switch the runnable-on-unfocused state
Press A to switch Ogg and MP3 Press A to switch Ogg and MP3
Current Time: %s Current Time: %s
Current Volume: %d/128 Current Volume: %d/128
Type: %s`, ebiten.CurrentTPS(), currentTimeStr, int(p.audioPlayer.Volume()*128), musicPlayer.musicType) Type: %s`, ebiten.CurrentTPS(), currentTimeStr, int(p.audioPlayer.Volume()*128), p.musicType)
ebitenutil.DebugPrint(screen, msg) ebitenutil.DebugPrint(screen, msg)
} }
var ( type Game struct {
musicPlayer *Player musicPlayer *Player
musicPlayerCh = make(chan *Player) musicPlayerCh chan *Player
errCh = make(chan error) errCh chan error
) }
func update(screen *ebiten.Image) error { func NewGame() (*Game, error) {
audioContext, err := audio.NewContext(sampleRate)
if err != nil {
return nil, err
}
m, err := NewPlayer(audioContext, typeOgg)
if err != nil {
return nil, err
}
return &Game{
musicPlayer: m,
musicPlayerCh: make(chan *Player),
errCh: make(chan error),
}, nil
}
func (g *Game) Update(screen *ebiten.Image) error {
select { select {
case p := <-musicPlayerCh: case p := <-g.musicPlayerCh:
musicPlayer = p g.musicPlayer = p
case err := <-errCh: case err := <-g.errCh:
return err return err
default: default:
} }
if musicPlayer != nil && inpututil.IsKeyJustPressed(ebiten.KeyA) { if g.musicPlayer != nil && inpututil.IsKeyJustPressed(ebiten.KeyA) {
var t musicType var t musicType
switch musicPlayer.musicType { switch g.musicPlayer.musicType {
case typeOgg: case typeOgg:
t = typeMP3 t = typeMP3
case typeMP3: case typeMP3:
@ -286,46 +304,45 @@ func update(screen *ebiten.Image) error {
panic("not reached") panic("not reached")
} }
musicPlayer.Close() g.musicPlayer.Close()
musicPlayer = nil g.musicPlayer = nil
go func() { go func() {
p, err := NewPlayer(audio.CurrentContext(), t) p, err := NewPlayer(audio.CurrentContext(), t)
if err != nil { if err != nil {
errCh <- err g.errCh <- err
return return
} }
musicPlayerCh <- p g.musicPlayerCh <- p
}() }()
} }
if musicPlayer != nil { if g.musicPlayer != nil {
if err := musicPlayer.update(); err != nil { if err := g.musicPlayer.update(); err != nil {
return err return err
} }
} }
if ebiten.IsDrawingSkipped() {
return nil
}
if musicPlayer != nil {
musicPlayer.draw(screen)
}
return nil return nil
} }
func main() { func (g *Game) Draw(screen *ebiten.Image) {
audioContext, err := audio.NewContext(sampleRate) if g.musicPlayer != nil {
if err != nil { g.musicPlayer.draw(screen)
log.Fatal(err)
} }
}
musicPlayer, err = NewPlayer(audioContext, typeOgg) func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
}
func main() {
ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
ebiten.SetWindowTitle("Audio (Ebiten Demo)")
g, err := NewGame()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Audio (Ebiten Demo)"); err != nil { if err := ebiten.RunGame(g); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }

View File

@ -47,37 +47,40 @@ func init() {
} }
} }
var player *audio.Player type Game struct {
player *audio.Player
}
func update(screen *ebiten.Image) error { func (g *Game) Update(screen *ebiten.Image) error {
if player == nil { if g.player != nil {
// Decode the wav file.
// wavS is a decoded io.ReadCloser and io.Seeker.
oggS, err := vorbis.Decode(audioContext, audio.BytesReadSeekCloser(raudio.Ragtime_ogg))
if err != nil {
return err
}
// Create an infinite loop stream from the decoded bytes.
// s is still an io.ReadCloser and io.Seeker.
s := audio.NewInfiniteLoopWithIntro(oggS, introLengthInSecond*4*sampleRate, loopLengthInSecond*4*sampleRate)
player, err = audio.NewPlayer(audioContext, s)
if err != nil {
return err
}
// Play the infinite-length stream. This never ends.
player.Play()
}
if ebiten.IsDrawingSkipped() {
return nil return nil
} }
pos := player.Current() // Decode the wav file.
// wavS is a decoded io.ReadCloser and io.Seeker.
oggS, err := vorbis.Decode(audioContext, audio.BytesReadSeekCloser(raudio.Ragtime_ogg))
if err != nil {
return err
}
// Create an infinite loop stream from the decoded bytes.
// s is still an io.ReadCloser and io.Seeker.
s := audio.NewInfiniteLoopWithIntro(oggS, introLengthInSecond*4*sampleRate, loopLengthInSecond*4*sampleRate)
g.player, err = audio.NewPlayer(audioContext, s)
if err != nil {
return err
}
// Play the infinite-length stream. This never ends.
g.player.Play()
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
pos := g.player.Current()
if pos > 5*time.Second { if pos > 5*time.Second {
pos = (player.Current()-5*time.Second)%(4*time.Second) + 5*time.Second pos = (g.player.Current()-5*time.Second)%(4*time.Second) + 5*time.Second
} }
msg := fmt.Sprintf(`TPS: %0.2f msg := fmt.Sprintf(`TPS: %0.2f
This is an example using This is an example using
@ -87,11 +90,16 @@ Intro: 0[s] - %[2]d[s]
Loop: %[2]d[s] - %[3]d[s] Loop: %[2]d[s] - %[3]d[s]
Current: %0.2[4]f[s]`, ebiten.CurrentTPS(), introLengthInSecond, introLengthInSecond+loopLengthInSecond, float64(pos)/float64(time.Second)) Current: %0.2[4]f[s]`, ebiten.CurrentTPS(), introLengthInSecond, introLengthInSecond+loopLengthInSecond, float64(pos)/float64(time.Second))
ebitenutil.DebugPrint(screen, msg) ebitenutil.DebugPrint(screen, msg)
return nil }
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
} }
func main() { func main() {
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Audio Infinite Loop (Ebiten Demo)"); err != nil { ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
ebiten.SetWindowTitle("Audio Infinite Loop (Ebiten Demo)")
if err := ebiten.RunGame(&Game{}); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }

View File

@ -35,11 +35,13 @@ var (
gophersImage *ebiten.Image gophersImage *ebiten.Image
) )
func update(screen *ebiten.Image) error { type Game struct{}
if ebiten.IsDrawingSkipped() {
return nil
}
func (g *Game) Update(screen *ebiten.Image) error {
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
op := &ebiten.DrawImageOptions{} op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(0, 0) op.GeoM.Translate(0, 0)
screen.DrawImage(gophersImage, op) screen.DrawImage(gophersImage, op)
@ -60,8 +62,10 @@ func update(screen *ebiten.Image) error {
screen.DrawImage(gophersImage, op) screen.DrawImage(gophersImage, op)
} }
} }
}
return nil func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
} }
func main() { func main() {
@ -80,7 +84,9 @@ func main() {
} }
gophersImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault) gophersImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
if err := ebiten.Run(update, screenWidth, screenHeight, 1, "Blur (Ebiten Demo)"); err != nil { ebiten.SetWindowSize(screenWidth, screenHeight)
ebiten.SetWindowTitle("Blur (Ebiten Demo)")
if err := ebiten.RunGame(&Game{}); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }

View File

@ -27,9 +27,13 @@ import (
"github.com/jakecoffman/cp" "github.com/jakecoffman/cp"
) )
const (
screenWidth = 600
screenHeight = 480
)
var ( var (
space *cp.Space dot *ebiten.Image
dot *ebiten.Image
) )
func init() { func init() {
@ -37,26 +41,77 @@ func init() {
dot.Fill(color.White) dot.Fill(color.White)
} }
func update(screen *ebiten.Image) error { type Game struct {
space.Step(1.0 / float64(ebiten.MaxTPS())) space *cp.Space
}
if ebiten.IsDrawingSkipped() { func NewGame() *Game {
return nil const (
imageWidth = 188
imageHeight = 35
)
space := cp.NewSpace()
space.Iterations = 1
// The space will contain a very large number of similarly sized objects.
// This is the perfect candidate for using the spatial hash.
// Generally you will never need to do this.
space.UseSpatialHash(2.0, 10000)
var body *cp.Body
var shape *cp.Shape
for y := 0; y < imageHeight; y++ {
for x := 0; x < imageWidth; x++ {
if getPixel(uint(x), uint(y)) == 0 {
continue
}
xJitter := 0.05 * rand.Float64()
yJitter := 0.05 * rand.Float64()
shape = makeBall(2.0*(float64(x)+imageWidth/2+xJitter)-75, 2*(imageHeight/2.0+float64(y)+yJitter)+150)
space.AddBody(shape.Body())
space.AddShape(shape)
}
} }
body = space.AddBody(cp.NewBody(1e9, cp.INFINITY))
body.SetPosition(cp.Vector{X: -1000, Y: 225})
body.SetVelocity(400, 0)
shape = space.AddShape(cp.NewCircle(body, 8, cp.Vector{}))
shape.SetElasticity(0)
shape.SetFriction(0)
return &Game{
space: space,
}
}
func (g *Game) Update(screen *ebiten.Image) error {
g.space.Step(1.0 / float64(ebiten.MaxTPS()))
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
screen.Fill(color.Black) screen.Fill(color.Black)
op := &ebiten.DrawImageOptions{} op := &ebiten.DrawImageOptions{}
op.ColorM.Scale(200.0/255.0, 200.0/255.0, 200.0/255.0, 1) op.ColorM.Scale(200.0/255.0, 200.0/255.0, 200.0/255.0, 1)
space.EachBody(func(body *cp.Body) { g.space.EachBody(func(body *cp.Body) {
op.GeoM.Reset() op.GeoM.Reset()
op.GeoM.Translate(body.Position().X, body.Position().Y) op.GeoM.Translate(body.Position().X, body.Position().Y)
screen.DrawImage(dot, op) screen.DrawImage(dot, op)
}) })
ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f", ebiten.CurrentTPS())) ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f", ebiten.CurrentTPS()))
return nil }
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
} }
func getPixel(x, y uint) int { func getPixel(x, y uint) int {
@ -112,46 +167,9 @@ var imageBitmap = []int{
} }
func main() { func main() {
const ( ebiten.SetWindowSize(screenWidth, screenHeight)
imageWidth = 188 ebiten.SetWindowTitle("Ebiten")
imageHeight = 35 if err := ebiten.RunGame(NewGame()); err != nil {
)
space = cp.NewSpace()
space.Iterations = 1
// The space will contain a very large number of similarly sized objects.
// This is the perfect candidate for using the spatial hash.
// Generally you will never need to do this.
space.UseSpatialHash(2.0, 10000)
var body *cp.Body
var shape *cp.Shape
for y := 0; y < imageHeight; y++ {
for x := 0; x < imageWidth; x++ {
if getPixel(uint(x), uint(y)) == 0 {
continue
}
xJitter := 0.05 * rand.Float64()
yJitter := 0.05 * rand.Float64()
shape = makeBall(2.0*(float64(x)+imageWidth/2+xJitter)-75, 2*(imageHeight/2.0+float64(y)+yJitter)+150)
space.AddBody(shape.Body())
space.AddShape(shape)
}
}
body = space.AddBody(cp.NewBody(1e9, cp.INFINITY))
body.SetPosition(cp.Vector{X: -1000, Y: 225})
body.SetVelocity(400, 0)
shape = space.AddShape(cp.NewCircle(body, 8, cp.Vector{}))
shape.SetElasticity(0)
shape.SetFriction(0)
if err := ebiten.Run(update, 600, 480, 1, "Ebiten"); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }

View File

@ -32,9 +32,12 @@ const (
screenHeight = 240 screenHeight = 240
) )
type Game struct { var (
count int
gophersImage *ebiten.Image gophersImage *ebiten.Image
)
type Game struct {
count int
} }
func (g *Game) Update(screen *ebiten.Image) error { func (g *Game) Update(screen *ebiten.Image) error {
@ -43,7 +46,7 @@ func (g *Game) Update(screen *ebiten.Image) error {
} }
func (g *Game) Draw(screen *ebiten.Image) { func (g *Game) Draw(screen *ebiten.Image) {
w, h := g.gophersImage.Size() w, h := gophersImage.Size()
op := &ebiten.DrawImageOptions{} op := &ebiten.DrawImageOptions{}
// Move the image's center to the screen's upper-left corner. // Move the image's center to the screen's upper-left corner.
@ -58,7 +61,7 @@ func (g *Game) Draw(screen *ebiten.Image) {
// Move the image to the screen's center. // Move the image to the screen's center.
op.GeoM.Translate(screenWidth/2, screenHeight/2) op.GeoM.Translate(screenWidth/2, screenHeight/2)
screen.DrawImage(g.gophersImage, op) screen.DrawImage(gophersImage, op)
} }
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) { func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
@ -79,12 +82,11 @@ func main() {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
g := &Game{} gophersImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
g.gophersImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
ebiten.SetWindowSize(screenWidth*2, screenHeight*2) ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
ebiten.SetWindowTitle("Rotate (Ebiten Demo)") ebiten.SetWindowTitle("Rotate (Ebiten Demo)")
if err := ebiten.RunGame(g); err != nil { if err := ebiten.RunGame(&Game{}); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }