ebiten/examples/set/main.go

75 lines
1.7 KiB
Go
Raw Normal View History

2019-01-14 11:22:46 +01:00
// Copyright 2019 The Ebiten Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2021-06-24 14:49:37 +02:00
//go:build example
2020-10-06 17:45:54 +02:00
// +build example
2019-01-14 11:22:46 +01:00
package main
import (
"fmt"
"image/color"
"log"
"math/rand"
"time"
2020-10-03 19:35:13 +02:00
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
2019-01-14 11:22:46 +01:00
)
const (
screenWidth = 320
screenHeight = 240
)
func init() {
rand.Seed(time.Now().UnixNano())
}
var offscreen = ebiten.NewImage(screenWidth, screenHeight)
2019-01-14 11:22:46 +01:00
2020-05-30 14:14:10 +02:00
type Game struct {
}
func (g *Game) Update() error {
2019-01-14 11:22:46 +01:00
w, h := offscreen.Size()
x := rand.Intn(w)
y := rand.Intn(h)
2019-01-14 12:04:58 +01:00
c := color.RGBA{
2019-01-14 11:22:46 +01:00
byte(rand.Intn(256)),
byte(rand.Intn(256)),
byte(rand.Intn(256)),
byte(0xff),
}
offscreen.Set(x, y, c)
2020-05-30 14:14:10 +02:00
return nil
}
2019-01-14 11:22:46 +01:00
2020-05-30 14:14:10 +02:00
func (g *Game) Draw(screen *ebiten.Image) {
2019-01-14 11:22:46 +01:00
screen.DrawImage(offscreen, nil)
ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f\nFPS: %0.2f", ebiten.CurrentTPS(), ebiten.CurrentFPS()))
2020-05-30 14:14:10 +02:00
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
2019-01-14 11:22:46 +01:00
}
func main() {
2020-05-30 14:14:10 +02:00
ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
ebiten.SetWindowTitle("Set (Ebiten Demo)")
if err := ebiten.RunGame(&Game{}); err != nil {
2019-01-14 11:22:46 +01:00
log.Fatal(err)
}
}