main.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2018 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. "bytes"
  17. "image"
  18. "image/color"
  19. _ "image/png"
  20. "log"
  21. "github.com/hajimehoshi/ebiten/v2"
  22. "github.com/hajimehoshi/ebiten/v2/colorm"
  23. "github.com/hajimehoshi/ebiten/v2/examples/resources/images"
  24. )
  25. const (
  26. screenWidth = 320
  27. screenHeight = 240
  28. )
  29. var (
  30. ebitenImage *ebiten.Image
  31. colors = []color.RGBA{
  32. {0xff, 0xff, 0xff, 0xff},
  33. {0xff, 0xff, 0x0, 0xff},
  34. {0xff, 0x0, 0xff, 0xff},
  35. {0xff, 0x0, 0x0, 0xff},
  36. {0x0, 0xff, 0xff, 0xff},
  37. {0x0, 0xff, 0x0, 0xff},
  38. {0x0, 0x0, 0xff, 0xff},
  39. {0x0, 0x0, 0x0, 0xff},
  40. }
  41. )
  42. type Game struct {
  43. }
  44. func (g *Game) Update() error {
  45. return nil
  46. }
  47. func (g *Game) Draw(screen *ebiten.Image) {
  48. const (
  49. ox = 10
  50. oy = 10
  51. dx = 60
  52. dy = 50
  53. )
  54. screen.Fill(color.NRGBA{0x00, 0x40, 0x80, 0xff})
  55. op := &ebiten.DrawImageOptions{}
  56. op.GeoM.Translate(ox, oy)
  57. screen.DrawImage(ebitenImage, op)
  58. // Fill with solid colors
  59. for i, c := range colors {
  60. op := &colorm.DrawImageOptions{}
  61. x := i % 4
  62. y := i/4 + 1
  63. op.GeoM.Translate(ox+float64(dx*x), oy+float64(dy*y))
  64. // Reset RGB (not Alpha) 0 forcibly
  65. var cm colorm.ColorM
  66. cm.Scale(0, 0, 0, 1)
  67. // Set color
  68. r := float64(c.R) / 0xff
  69. g := float64(c.G) / 0xff
  70. b := float64(c.B) / 0xff
  71. cm.Translate(r, g, b, 0)
  72. colorm.DrawImage(screen, ebitenImage, cm, op)
  73. }
  74. }
  75. func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
  76. return screenWidth, screenHeight
  77. }
  78. func main() {
  79. // Decode an image from the image file's byte slice.
  80. img, _, err := image.Decode(bytes.NewReader(images.Ebiten_png))
  81. if err != nil {
  82. log.Fatal(err)
  83. }
  84. ebitenImage = ebiten.NewImageFromImage(img)
  85. ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
  86. ebiten.SetWindowTitle("Flood fill with solid colors (Ebitengine Demo)")
  87. if err := ebiten.RunGame(&Game{}); err != nil {
  88. log.Fatal(err)
  89. }
  90. }