examples: Use RunGame

Updates #1111
This commit is contained in:
Hajime Hoshi 2020-05-12 02:11:09 +09:00
parent a5d570a7a8
commit dc3ed76e3e
5 changed files with 130 additions and 83 deletions

View File

@ -35,14 +35,6 @@ var (
palette [maxIt]byte
)
func init() {
offscreen, _ = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterDefault)
offscreenPix = make([]byte, screenWidth*screenHeight*4)
for i := range palette {
palette[i] = byte(math.Sqrt(float64(i)/float64(len(palette))) * 0x80)
}
}
func color(it int) (r, g, b byte) {
if it == maxIt {
return 0xff, 0xff, 0xff
@ -77,21 +69,34 @@ func updateOffscreen(centerX, centerY, size float64) {
}
func init() {
offscreen, _ = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterDefault)
offscreenPix = make([]byte, screenWidth*screenHeight*4)
for i := range palette {
palette[i] = byte(math.Sqrt(float64(i)/float64(len(palette))) * 0x80)
}
// Now it is not feasible to call updateOffscreen every frame due to performance.
updateOffscreen(-0.75, 0.25, 2)
}
func update(screen *ebiten.Image) error {
if ebiten.IsDrawingSkipped() {
return nil
}
type Game struct {
}
screen.DrawImage(offscreen, nil)
func (g *Game) Update(screen *ebiten.Image) error {
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
screen.DrawImage(offscreen, nil)
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
}
func main() {
if err := ebiten.Run(update, screenWidth, screenHeight, 1, "Mandelbrot (Ebiten Demo)"); err != nil {
ebiten.SetWindowSize(screenWidth, screenHeight)
ebiten.SetWindowTitle("Mandelbrot (Ebiten Demo)")
if err := ebiten.RunGame(&Game{}); err != nil {
log.Fatal(err)
}
}

View File

@ -38,10 +38,6 @@ var (
fgImage *ebiten.Image
maskedFgImage *ebiten.Image
spotLightImage *ebiten.Image
spotLightX = 0
spotLightY = 0
spotLightVX = 1
spotLightVY = 1
)
func init() {
@ -84,37 +80,59 @@ func init() {
spotLightImage, _ = ebiten.NewImageFromImage(a, ebiten.FilterDefault)
}
func update(screen *ebiten.Image) error {
spotLightX += spotLightVX
spotLightY += spotLightVY
if spotLightX < 0 {
spotLightX = -spotLightX
spotLightVX = -spotLightVX
type Game struct {
spotLightX int
spotLightY int
spotLightVX int
spotLightVY int
}
func NewGame() *Game {
return &Game{
spotLightX: 0,
spotLightY: 0,
spotLightVX: 1,
spotLightVY: 1,
}
if spotLightY < 0 {
spotLightY = -spotLightY
spotLightVY = -spotLightVY
}
func (g *Game) Update(screen *ebiten.Image) error {
if g.spotLightVX == 0 {
g.spotLightVX = 1
}
if g.spotLightVY == 0 {
g.spotLightVY = 1
}
g.spotLightX += g.spotLightVX
g.spotLightY += g.spotLightVY
if g.spotLightX < 0 {
g.spotLightX = -g.spotLightX
g.spotLightVX = -g.spotLightVX
}
if g.spotLightY < 0 {
g.spotLightY = -g.spotLightY
g.spotLightVY = -g.spotLightVY
}
w, h := spotLightImage.Size()
maxX, maxY := screenWidth-w, screenHeight-h
if maxX < spotLightX {
spotLightX = -spotLightX + 2*maxX
spotLightVX = -spotLightVX
if maxX < g.spotLightX {
g.spotLightX = -g.spotLightX + 2*maxX
g.spotLightVX = -g.spotLightVX
}
if maxY < spotLightY {
spotLightY = -spotLightY + 2*maxY
spotLightVY = -spotLightVY
}
if ebiten.IsDrawingSkipped() {
return nil
if maxY < g.spotLightY {
g.spotLightY = -g.spotLightY + 2*maxY
g.spotLightVY = -g.spotLightVY
}
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
// Reset the maskedFgImage.
maskedFgImage.Fill(color.White)
op := &ebiten.DrawImageOptions{}
op.CompositeMode = ebiten.CompositeModeCopy
op.GeoM.Translate(float64(spotLightX), float64(spotLightY))
op.GeoM.Translate(float64(g.spotLightX), float64(g.spotLightY))
maskedFgImage.DrawImage(spotLightImage, op)
// Use 'source-in' composite mode so that the source image (fgImage) is used but the alpha
@ -133,8 +151,10 @@ func update(screen *ebiten.Image) error {
screen.Fill(color.RGBA{0x00, 0x00, 0x80, 0xff})
screen.DrawImage(bgImage, &ebiten.DrawImageOptions{})
screen.DrawImage(maskedFgImage, &ebiten.DrawImageOptions{})
}
return nil
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
}
func max(a, b int) int {
@ -152,7 +172,9 @@ func min(a, b int) int {
}
func main() {
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Masking (Ebiten Demo)"); err != nil {
ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
ebiten.SetWindowTitle("Masking (Ebiten Demo)")
if err := ebiten.RunGame(&Game{}); err != nil {
log.Fatal(err)
}
}

View File

@ -40,29 +40,31 @@ const (
var (
gophersImage *ebiten.Image
rotate = false
clip = false
counter = 0
)
func update(screen *ebiten.Image) error {
counter++
if counter == 480 {
counter = 0
type Game struct {
rotate bool
clip bool
counter int
}
func (g *Game) Update(screen *ebiten.Image) error {
g.counter++
if g.counter == 480 {
g.counter = 0
}
if inpututil.IsKeyJustPressed(ebiten.KeyR) {
rotate = !rotate
g.rotate = !g.rotate
}
if inpututil.IsKeyJustPressed(ebiten.KeyC) {
clip = !clip
g.clip = !g.clip
}
return nil
}
if ebiten.IsDrawingSkipped() {
return nil
}
s := 1.5 / math.Pow(1.01, float64(counter))
func (g *Game) Draw(screen *ebiten.Image) {
s := 1.5 / math.Pow(1.01, float64(g.counter))
msg := fmt.Sprintf(`Minifying images (Nearest filter vs Linear filter):
Press R to rotate the images.
Press C to clip the images.
@ -74,22 +76,24 @@ Scale: %0.2f`, s)
w, h := gophersImage.Size()
op := &ebiten.DrawImageOptions{}
if rotate {
if g.rotate {
op.GeoM.Translate(-float64(w)/2, -float64(h)/2)
op.GeoM.Rotate(float64(counter) / 300 * 2 * math.Pi)
op.GeoM.Rotate(float64(g.counter) / 300 * 2 * math.Pi)
op.GeoM.Translate(float64(w)/2, float64(h)/2)
}
op.GeoM.Scale(s, s)
op.GeoM.Translate(32+float64(i*w)*s+float64(i*4), 64)
op.Filter = f
if clip {
if g.clip {
screen.DrawImage(clippedGophersImage, op)
} else {
screen.DrawImage(gophersImage, op)
}
}
}
return nil
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
}
func main() {
@ -112,7 +116,9 @@ func main() {
// Specify FilterDefault here, that means to prefer filter specified at DrawImageOptions.
gophersImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
if err := ebiten.Run(update, screenWidth, screenHeight, 1, "Minify (Ebiten Demo)"); err != nil {
ebiten.SetWindowSize(screenWidth, screenHeight)
ebiten.SetWindowTitle("Minify (Ebiten Demo)")
if err := ebiten.RunGame(&Game{}); err != nil {
log.Fatal(err)
}
}

View File

@ -55,11 +55,14 @@ func init() {
gophersImage, _ = ebiten.NewImageFromImage(img, ebiten.FilterDefault)
}
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) {
// Shrink the image once.
op := &ebiten.DrawImageOptions{}
op.GeoM.Scale(1.0/mosaicRatio, 1.0/mosaicRatio)
@ -70,13 +73,18 @@ func update(screen *ebiten.Image) error {
op = &ebiten.DrawImageOptions{}
op.GeoM.Scale(mosaicRatio, mosaicRatio)
screen.DrawImage(gophersRenderTarget, op)
return nil
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
}
func main() {
w, h := gophersImage.Size()
gophersRenderTarget, _ = ebiten.NewImage(w/mosaicRatio, h/mosaicRatio, ebiten.FilterDefault)
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Mosaic (Ebiten Demo)"); err != nil {
ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
ebiten.SetWindowTitle("Mosaic (Ebiten Demo)")
if err := ebiten.RunGame(&Game{}); err != nil {
log.Fatal(err)
}
}

View File

@ -30,10 +30,6 @@ const (
screenHeight = 240
)
var (
noiseImage *image.RGBA
)
type rand struct {
x, y, z, w uint32
}
@ -49,29 +45,39 @@ func (r *rand) next() uint32 {
var theRand = &rand{12345678, 4185243, 776511, 45411}
func update(screen *ebiten.Image) error {
type Game struct {
noiseImage *image.RGBA
}
func (g *Game) Update(screen *ebiten.Image) error {
// Generate the noise with random RGB values.
const l = screenWidth * screenHeight
for i := 0; i < l; i++ {
x := theRand.next()
noiseImage.Pix[4*i] = uint8(x >> 24)
noiseImage.Pix[4*i+1] = uint8(x >> 16)
noiseImage.Pix[4*i+2] = uint8(x >> 8)
noiseImage.Pix[4*i+3] = 0xff
g.noiseImage.Pix[4*i] = uint8(x >> 24)
g.noiseImage.Pix[4*i+1] = uint8(x >> 16)
g.noiseImage.Pix[4*i+2] = uint8(x >> 8)
g.noiseImage.Pix[4*i+3] = 0xff
}
if ebiten.IsDrawingSkipped() {
return nil
}
screen.ReplacePixels(noiseImage.Pix)
ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f", ebiten.CurrentTPS()))
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
screen.ReplacePixels(g.noiseImage.Pix)
ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f", ebiten.CurrentTPS()))
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
}
func main() {
noiseImage = image.NewRGBA(image.Rect(0, 0, screenWidth, screenHeight))
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Noise (Ebiten Demo)"); err != nil {
ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
ebiten.SetWindowTitle("Noise (Ebiten Demo)")
g := &Game{
noiseImage: image.NewRGBA(image.Rect(0, 0, screenWidth, screenHeight)),
}
if err := ebiten.RunGame(g); err != nil {
log.Fatal(err)
}
}