1
0

main.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright 2019 The Ebiten Authors
  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. "image/color"
  17. "log"
  18. "math/rand/v2"
  19. "github.com/hajimehoshi/ebiten/v2"
  20. )
  21. const (
  22. screenWidth = 100
  23. screenHeight = 50
  24. screenSize = screenWidth * screenHeight
  25. )
  26. var (
  27. firePalette = []color.RGBA{
  28. {R: 7, G: 7, B: 7, A: 255}, // 0
  29. {R: 31, G: 7, B: 7, A: 255}, // 1
  30. {R: 47, G: 15, B: 7, A: 255}, // 2
  31. {R: 71, G: 15, B: 7, A: 255}, // 3
  32. {R: 87, G: 23, B: 7, A: 255}, // 4
  33. {R: 103, G: 31, B: 7, A: 255}, // 5
  34. {R: 119, G: 31, B: 7, A: 255}, // 6
  35. {R: 143, G: 39, B: 7, A: 255}, // 7
  36. {R: 159, G: 47, B: 7, A: 255}, // 8
  37. {R: 175, G: 63, B: 7, A: 255}, // 9
  38. {R: 191, G: 71, B: 7, A: 255}, // 10
  39. {R: 199, G: 71, B: 7, A: 255}, // 11
  40. {R: 223, G: 79, B: 7, A: 255}, // 12
  41. {R: 223, G: 87, B: 7, A: 255}, // 13
  42. {R: 223, G: 87, B: 7, A: 255}, // 14
  43. {R: 215, G: 95, B: 7, A: 255}, // 15
  44. {R: 215, G: 95, B: 7, A: 255}, // 16
  45. {R: 215, G: 103, B: 15, A: 255}, // 17
  46. {R: 207, G: 111, B: 15, A: 255}, // 18
  47. {R: 207, G: 119, B: 15, A: 255}, // 19
  48. {R: 207, G: 127, B: 15, A: 255}, // 20
  49. {R: 207, G: 135, B: 23, A: 255}, // 21
  50. {R: 199, G: 135, B: 23, A: 255}, // 22
  51. {R: 199, G: 143, B: 23, A: 255}, // 23
  52. {R: 199, G: 151, B: 31, A: 255}, // 24
  53. {R: 191, G: 159, B: 31, A: 255}, // 25
  54. {R: 191, G: 159, B: 31, A: 255}, // 26
  55. {R: 191, G: 167, B: 39, A: 255}, // 27
  56. {R: 191, G: 167, B: 39, A: 255}, // 28
  57. {R: 191, G: 175, B: 47, A: 255}, // 29
  58. {R: 183, G: 175, B: 47, A: 255}, // 30
  59. {R: 183, G: 183, B: 47, A: 255}, // 31
  60. {R: 183, G: 183, B: 55, A: 255}, // 32
  61. {R: 207, G: 207, B: 111, A: 255}, // 33
  62. {R: 223, G: 223, B: 159, A: 255}, // 34
  63. {R: 239, G: 239, B: 199, A: 255}, // 35
  64. {R: 255, G: 255, B: 255, A: 255}, // 36
  65. }
  66. )
  67. type Game struct {
  68. pixels []byte
  69. indices []byte
  70. }
  71. func NewGame() *Game {
  72. indices := make([]byte, screenSize)
  73. for i := screenSize - screenWidth; i < screenSize; i++ {
  74. indices[i] = 36
  75. }
  76. return &Game{
  77. pixels: make([]byte, screenSize*4),
  78. indices: indices,
  79. }
  80. }
  81. func (g *Game) updateFirePixels() {
  82. for i := 0; i < screenWidth; i++ {
  83. for j := 0; j < screenHeight; j++ {
  84. idx := i + (screenWidth * j)
  85. g.updateFireIntensityPerPixel(idx)
  86. }
  87. }
  88. }
  89. func (g *Game) updateFireIntensityPerPixel(currentPixelIndex int) {
  90. below := currentPixelIndex + screenWidth
  91. if below >= screenSize {
  92. return
  93. }
  94. d := rand.IntN(3)
  95. newI := int(g.indices[below]) - d
  96. if newI < 0 {
  97. newI = 0
  98. }
  99. if currentPixelIndex-d < 0 {
  100. return
  101. }
  102. g.indices[currentPixelIndex-d] = byte(newI)
  103. }
  104. func (g *Game) renderFire() {
  105. for i, v := range g.indices {
  106. p := firePalette[v]
  107. g.pixels[i*4] = p.R
  108. g.pixels[i*4+1] = p.G
  109. g.pixels[i*4+2] = p.B
  110. g.pixels[i*4+3] = p.A
  111. }
  112. }
  113. func (g *Game) Update() error {
  114. g.updateFirePixels()
  115. return nil
  116. }
  117. func (g *Game) Draw(screen *ebiten.Image) {
  118. g.renderFire()
  119. screen.WritePixels(g.pixels)
  120. }
  121. func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
  122. return screenWidth, screenHeight
  123. }
  124. func main() {
  125. ebiten.SetWindowSize(screenWidth*6, screenHeight*6)
  126. ebiten.SetWindowTitle("Doom Fire (Ebitengine Demo)")
  127. if err := ebiten.RunGame(NewGame()); err != nil {
  128. log.Fatal(err)
  129. }
  130. }