1
0

main.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2015 Hajime Hoshi
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "fmt"
  17. "image"
  18. "log"
  19. "github.com/hajimehoshi/ebiten/v2"
  20. "github.com/hajimehoshi/ebiten/v2/ebitenutil"
  21. )
  22. const (
  23. screenWidth = 320
  24. screenHeight = 240
  25. )
  26. type rand struct {
  27. x, y, z, w uint32
  28. }
  29. func (r *rand) next() uint32 {
  30. // math/rand is too slow to keep 60 FPS on web browsers.
  31. // Use Xorshift instead: http://en.wikipedia.org/wiki/Xorshift
  32. t := r.x ^ (r.x << 11)
  33. r.x, r.y, r.z = r.y, r.z, r.w
  34. r.w = (r.w ^ (r.w >> 19)) ^ (t ^ (t >> 8))
  35. return r.w
  36. }
  37. var theRand = &rand{12345678, 4185243, 776511, 45411}
  38. type Game struct {
  39. noiseImage *image.RGBA
  40. }
  41. func (g *Game) Update() error {
  42. // Generate the noise with random RGB values.
  43. const l = screenWidth * screenHeight
  44. for i := 0; i < l; i++ {
  45. x := theRand.next()
  46. g.noiseImage.Pix[4*i] = uint8(x >> 24)
  47. g.noiseImage.Pix[4*i+1] = uint8(x >> 16)
  48. g.noiseImage.Pix[4*i+2] = uint8(x >> 8)
  49. g.noiseImage.Pix[4*i+3] = 0xff
  50. }
  51. return nil
  52. }
  53. func (g *Game) Draw(screen *ebiten.Image) {
  54. screen.WritePixels(g.noiseImage.Pix)
  55. ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f\nFPS: %0.2f", ebiten.ActualTPS(), ebiten.ActualFPS()))
  56. }
  57. func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
  58. return screenWidth, screenHeight
  59. }
  60. func main() {
  61. ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
  62. ebiten.SetWindowTitle("Noise (Ebitengine Demo)")
  63. g := &Game{
  64. noiseImage: image.NewRGBA(image.Rect(0, 0, screenWidth, screenHeight)),
  65. }
  66. if err := ebiten.RunGame(g); err != nil {
  67. log.Fatal(err)
  68. }
  69. }