examples: Use RunGame

Updates #1111
This commit is contained in:
Hajime Hoshi 2020-05-30 21:14:10 +09:00
parent e25a6f4d46
commit bab3288a37
3 changed files with 52 additions and 31 deletions

View File

@ -42,7 +42,10 @@ func init() {
offscreen, _ = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterDefault)
}
func update(screen *ebiten.Image) error {
type Game struct {
}
func (g *Game) Update(screen *ebiten.Image) error {
w, h := offscreen.Size()
x := rand.Intn(w)
y := rand.Intn(h)
@ -53,18 +56,22 @@ func update(screen *ebiten.Image) error {
byte(0xff),
}
offscreen.Set(x, y, c)
if ebiten.IsDrawingSkipped() {
return nil
}
screen.DrawImage(offscreen, nil)
ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f\nFPS: %0.2f", ebiten.CurrentTPS(), ebiten.CurrentFPS()))
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
screen.DrawImage(offscreen, nil)
ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f\nFPS: %0.2f", ebiten.CurrentTPS(), ebiten.CurrentFPS()))
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
}
func main() {
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Test"); err != nil {
ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
ebiten.SetWindowTitle("Set (Ebiten Demo)")
if err := ebiten.RunGame(&Game{}); err != nil {
log.Fatal(err)
}
}

View File

@ -39,7 +39,9 @@ func init() {
emptyImage.Fill(color.White)
}
var count = 0
type Game struct {
count int
}
func line(x0, y0, x1, y1 float32, clr color.RGBA) ([]ebiten.Vertex, []uint16) {
const width = 1
@ -152,15 +154,14 @@ func rect(x, y, w, h float32, clr color.RGBA) ([]ebiten.Vertex, []uint16) {
}, []uint16{0, 1, 2, 1, 2, 3}
}
func update(screen *ebiten.Image) error {
count++
count %= 240
func (g *Game) Update(screen *ebiten.Image) error {
g.count++
g.count %= 240
return nil
}
if ebiten.IsDrawingSkipped() {
return nil
}
cf := float64(count)
func (g *Game) Draw(screen *ebiten.Image) {
cf := float64(g.count)
v, i := line(100, 100, 300, 100, color.RGBA{0xff, 0xff, 0xff, 0xff})
screen.DrawTriangles(v, i, emptyImage, nil)
v, i = line(50, 150, 50, 350, color.RGBA{0xff, 0xff, 0x00, 0xff})
@ -174,11 +175,16 @@ func update(screen *ebiten.Image) error {
screen.DrawTriangles(v, i, emptyImage, nil)
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 main() {
if err := ebiten.Run(update, screenWidth, screenHeight, 1, "Shapes (Ebiten Demo)"); err != nil {
ebiten.SetWindowSize(screenWidth, screenHeight)
ebiten.SetWindowTitle("Shapes (Ebiten Demo)")
if err := ebiten.RunGame(&Game{}); err != nil {
log.Fatal(err)
}
}

View File

@ -93,29 +93,37 @@ func (s *stream) Close() error {
return nil
}
var player *audio.Player
type Game struct {
player *audio.Player
}
func update(screen *ebiten.Image) error {
if player == nil {
func (g *Game) Update(screen *ebiten.Image) error {
if g.player == nil {
// Pass the (infinite) stream to audio.NewPlayer.
// After calling Play, the stream never ends as long as the player object lives.
var err error
player, err = audio.NewPlayer(audioContext, &stream{})
g.player, err = audio.NewPlayer(audioContext, &stream{})
if err != nil {
return err
}
player.Play()
g.player.Play()
}
if ebiten.IsDrawingSkipped() {
return nil
}
msg := fmt.Sprintf("TPS: %0.2f\nThis is an example using infinite audio stream.", ebiten.CurrentTPS())
ebitenutil.DebugPrint(screen, msg)
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
msg := fmt.Sprintf("TPS: %0.2f\nThis is an example using infinite audio stream.", ebiten.CurrentTPS())
ebitenutil.DebugPrint(screen, msg)
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
}
func main() {
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Sine Wave (Ebiten Demo)"); err != nil {
ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
ebiten.SetWindowTitle("Sine Wave (Ebiten Demo)")
if err := ebiten.RunGame(&Game{}); err != nil {
log.Fatal(err)
}
}