Skip to content

Commit

Permalink
examples: Use RunGame
Browse files Browse the repository at this point in the history
  • Loading branch information
hajimehoshi committed Apr 12, 2020
1 parent cd4a0ba commit e16a4cd
Show file tree
Hide file tree
Showing 10 changed files with 278 additions and 181 deletions.
18 changes: 12 additions & 6 deletions examples/additive/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ var (
ebitenImage *ebiten.Image
)

func update(screen *ebiten.Image) error {
if ebiten.IsDrawingSkipped() {
return nil
}
type Game struct{}

func (g *Game) Update(screen *ebiten.Image) error {
return nil
}

func (g *Game) Draw(screen *ebiten.Image) {
const (
// The offset point to render the image.
ox = 10
Expand All @@ -60,8 +62,10 @@ func update(screen *ebiten.Image) error {
op.GeoM.Translate(ox+float64(w), oy)
op.CompositeMode = ebiten.CompositeModeLighter
screen.DrawImage(ebitenImage, op)
}

return nil
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
}

func main() {
Expand All @@ -80,7 +84,9 @@ func main() {
}
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)
}
}
19 changes: 13 additions & 6 deletions examples/address/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,23 +102,30 @@ func drawRect(screen *ebiten.Image, img *ebiten.Image, x, y, width, height float
ebitenutil.DebugPrintAt(screen, msg, int(x), int(y)-16)
}

func update(screen *ebiten.Image) error {
if ebiten.IsDrawingSkipped() {
return nil
}
type Game struct{}

func (g *Game) Update(screen *ebiten.Image) error {
return nil
}

func (g *Game) Draw(screen *ebiten.Image) {
const ox, oy = 40, 60
drawRect(screen, ebitenImage, ox, oy, 200, 100, ebiten.AddressClampToZero, "Regular")
drawRect(screen, ebitenImage, 220+ox, oy, 200, 100, ebiten.AddressRepeat, "Regular, Repeat")

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, 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() {
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)
}
}
62 changes: 38 additions & 24 deletions examples/airship/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,8 @@ const (
)

var (
skyColor = color.RGBA{0x66, 0xcc, 0xff, 0xff}
thePlayer = &player{
x16: 16 * 100,
y16: 16 * 200,
angle: maxAngle * 3 / 4,
}
skyColor = color.RGBA{0x66, 0xcc, 0xff, 0xff}

gophersImage *ebiten.Image
repeatedGophersImage *ebiten.Image
groundImage *ebiten.Image
Expand Down Expand Up @@ -186,11 +182,11 @@ func (p *player) Angle() int {
}

// 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()

x16, y16 := thePlayer.Position()
a := thePlayer.Angle()
x16, y16 := g.player.Position()
a := g.player.Angle()
gw, gh := ground.Size()
w, h := gophersImage.Size()
op := &ebiten.DrawImageOptions{}
Expand All @@ -202,7 +198,7 @@ func updateGroundImage(ground *ebiten.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()
gw, _ := ground.Size()
pw, ph := perspectiveGroundImage.Size()
Expand All @@ -225,47 +221,65 @@ func drawGroundImage(screen *ebiten.Image, ground *ebiten.Image) {

op := &ebiten.DrawImageOptions{}
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)
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.
if ebiten.IsKeyPressed(ebiten.KeySpace) {
thePlayer.MoveForward()
g.player.MoveForward()
}
rotated := false
if ebiten.IsKeyPressed(ebiten.KeyRight) {
thePlayer.RotateRight()
g.player.RotateRight()
rotated = true
}
if ebiten.IsKeyPressed(ebiten.KeyLeft) {
thePlayer.RotateLeft()
g.player.RotateLeft()
rotated = true
}
if !rotated {
thePlayer.Stabilize()
}

if ebiten.IsDrawingSkipped() {
return nil
g.player.Stabilize()
}
return nil
}

func (g *Game) Draw(screen *ebiten.Image) {
// Draw the ground image.
screen.Fill(skyColor)
updateGroundImage(groundImage)
drawGroundImage(screen, groundImage)
g.updateGroundImage(groundImage)
g.drawGroundImage(screen, groundImage)

// Draw the message.
tutrial := "Space: Move forward\nLeft/Right: Rotate"
msg := fmt.Sprintf("TPS: %0.2f\nFPS: %0.2f\n%s", ebiten.CurrentTPS(), ebiten.CurrentFPS(), tutrial)
ebitenutil.DebugPrint(screen, msg)
return nil
}

func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
}

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)
}
}
46 changes: 29 additions & 17 deletions examples/alphablending/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,38 +33,48 @@ const (
)

var (
count int
ebitenImage *ebiten.Image
)

func update(screen *ebiten.Image) error {
count++
count %= ebiten.MaxTPS() * 10
diff := float64(count) * 0.2
switch {
case 480 < count:
diff = 0
case 240 < count:
diff = float64(480-count) * 0.2
}
type Game struct {
count int
}

func (g *Game) Update(screen *ebiten.Image) error {
g.count++
g.count %= ebiten.MaxTPS() * 10
return nil
}

if ebiten.IsDrawingSkipped() {
return nil
func (g *Game) offset() float64 {
v := float64(g.count) * 0.2
switch {
case 480 < g.count:
v = 0
case 240 < g.count:
v = float64(480-g.count) * 0.2
}
return v
}

func (g *Game) Draw(screen *ebiten.Image) {
screen.Fill(color.NRGBA{0x00, 0x00, 0x80, 0xff})

// Draw 100 Ebitens
v := g.offset()
op := &ebiten.DrawImageOptions{}
op.ColorM.Scale(1.0, 1.0, 1.0, 0.5)
for i := 0; i < 10*10; i++ {
op.GeoM.Reset()
x := float64(i%10)*diff + 15
y := float64(i/10)*diff + 20
x := float64(i%10)*v + 15
y := float64(i/10)*v + 20
op.GeoM.Translate(x, y)
screen.DrawImage(ebitenImage, op)
}
return nil
}

func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
}

func main() {
Expand All @@ -83,7 +93,9 @@ func main() {
}
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)
}
}
25 changes: 16 additions & 9 deletions examples/animation/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,29 @@ const (
)

var (
count = 0
runnerImage *ebiten.Image
)

func update(screen *ebiten.Image) error {
count++
type Game struct {
count int
}

if ebiten.IsDrawingSkipped() {
return nil
}
func (g *Game) Update(screen *ebiten.Image) error {
g.count++
return nil
}

func (g *Game) Draw(screen *ebiten.Image) {
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(-float64(frameWidth)/2, -float64(frameHeight)/2)
op.GeoM.Translate(screenWidth/2, screenHeight/2)
i := (count / 5) % frameNum
i := (g.count / 5) % frameNum
sx, sy := frameOX+i*frameWidth, frameOY
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() {
Expand All @@ -74,7 +79,9 @@ func main() {
}
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)
}
}
Loading

0 comments on commit e16a4cd

Please sign in to comment.