ebiten/examples/noise/main.go

85 lines
2.1 KiB
Go
Raw Normal View History

2015-01-20 16:02:42 +01:00
// 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.
2021-06-24 14:49:37 +02:00
//go:build example
2020-10-06 17:45:54 +02:00
// +build example
package main
import (
"fmt"
"image"
"log"
2016-02-15 17:13:04 +01:00
2020-10-03 19:35:13 +02:00
"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
}
2018-01-29 19:50:07 +01:00
var theRand = &rand{12345678, 4185243, 776511, 45411}
2020-05-11 19:11:09 +02:00
type Game struct {
noiseImage *image.RGBA
}
func (g *Game) Update() error {
2018-01-29 19:50:07 +01:00
// Generate the noise with random RGB values.
const l = screenWidth * screenHeight
for i := 0; i < l; i++ {
2018-01-29 19:50:07 +01:00
x := theRand.next()
2020-05-11 19:11:09 +02:00
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
2017-05-16 03:35:58 +02:00
}
2020-05-11 19:11:09 +02:00
return nil
}
2018-01-29 19:50:07 +01:00
2020-05-11 19:11:09 +02:00
func (g *Game) Draw(screen *ebiten.Image) {
screen.WritePixels(g.noiseImage.Pix)
ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f\nFPS: %0.2f", ebiten.ActualTPS(), ebiten.ActualFPS()))
2020-05-11 19:11:09 +02:00
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
}
func main() {
2020-05-11 19:11:09 +02:00
ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
ebiten.SetWindowTitle("Noise (Ebitengine Demo)")
2020-05-11 19:11:09 +02:00
g := &Game{
noiseImage: image.NewRGBA(image.Rect(0, 0, screenWidth, screenHeight)),
}
if err := ebiten.RunGame(g); err != nil {
log.Fatal(err)
}
}