ebiten/examples/noise/main.go
Hajime Hoshi 0f52381580 ebiten: rename TPS functions
* SetMaxTPS() -> SetTPS()
* MaxTPS() -> TPS()
* CurrentTPS() -> ActualTPS()
* CurrentFPS() -> ActualFPS()

Closes #2071
2022-07-17 11:30:12 +09:00

85 lines
2.1 KiB
Go

// Copyright 2015 Hajime Hoshi
//
// 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.
//go:build example
// +build example
package main
import (
"fmt"
"image"
"log"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
)
const (
screenWidth = 320
screenHeight = 240
)
type rand struct {
x, y, z, w uint32
}
func (r *rand) next() uint32 {
// math/rand is too slow to keep 60 FPS on web browsers.
// Use Xorshift instead: http://en.wikipedia.org/wiki/Xorshift
t := r.x ^ (r.x << 11)
r.x, r.y, r.z = r.y, r.z, r.w
r.w = (r.w ^ (r.w >> 19)) ^ (t ^ (t >> 8))
return r.w
}
var theRand = &rand{12345678, 4185243, 776511, 45411}
type Game struct {
noiseImage *image.RGBA
}
func (g *Game) Update() error {
// Generate the noise with random RGB values.
const l = screenWidth * screenHeight
for i := 0; i < l; i++ {
x := theRand.next()
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
}
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
screen.ReplacePixels(g.noiseImage.Pix)
ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f\nFPS: %0.2f", ebiten.ActualTPS(), ebiten.ActualFPS()))
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
}
func main() {
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)
}
}