main.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright 2023 The Ebitengine 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. "fmt"
  18. "image"
  19. "image/color"
  20. _ "image/png"
  21. "log"
  22. "golang.org/x/image/font/inconsolata"
  23. "github.com/hajimehoshi/ebiten/v2"
  24. "github.com/hajimehoshi/ebiten/v2/examples/resources/images/blend"
  25. "github.com/hajimehoshi/ebiten/v2/text/v2"
  26. )
  27. const (
  28. screenWidth = 800
  29. screenHeight = 800
  30. )
  31. var (
  32. inconsolataFace = text.NewGoXFace(inconsolata.Bold8x16)
  33. )
  34. // mode is a blend mode with description.
  35. type mode struct {
  36. blend ebiten.Blend
  37. name string
  38. }
  39. // Game is a canvas for drawing blend mode tiles.
  40. type Game struct {
  41. source *ebiten.Image
  42. dest *ebiten.Image
  43. offscreen *ebiten.Image
  44. tileSize int
  45. modes []mode
  46. }
  47. func NewGame() (*Game, error) {
  48. source, err := loadImage(blend.Source_png)
  49. if err != nil {
  50. return nil, fmt.Errorf("fail to load source: %w", err)
  51. }
  52. dest, err := loadImage(blend.Dest_png)
  53. if err != nil {
  54. return nil, fmt.Errorf("fail to load dest: %w", err)
  55. }
  56. // Set up a grid for drawing.
  57. g := &Game{
  58. source: source,
  59. dest: dest,
  60. }
  61. // Add all known blend modes and their names.
  62. g.modes = []mode{
  63. {blend: ebiten.BlendCopy, name: "BlendCopy"},
  64. {blend: ebiten.BlendSourceAtop, name: "BlendSourceAtop"},
  65. {blend: ebiten.BlendSourceOver, name: "BlendSourceOver"},
  66. {blend: ebiten.BlendSourceIn, name: "BlendSourceIn"},
  67. {blend: ebiten.BlendSourceOut, name: "BlendSourceOut"},
  68. {blend: ebiten.BlendDestination, name: "BlendDestination"},
  69. {blend: ebiten.BlendDestinationAtop, name: "BlendDestinationAtop"},
  70. {blend: ebiten.BlendDestinationOver, name: "BlendDestinationOver"},
  71. {blend: ebiten.BlendDestinationIn, name: "BlendDestinationIn"},
  72. {blend: ebiten.BlendDestinationOut, name: "BlendDestinationOut"},
  73. {blend: ebiten.BlendXor, name: "BlendXor"},
  74. {blend: ebiten.BlendLighter, name: "BlendLighter"},
  75. {blend: ebiten.BlendClear, name: "BlendClear"},
  76. }
  77. // Adjust the tile size for the available images.
  78. g.tileSize = maxSide(g.source, g.dest)
  79. g.offscreen = ebiten.NewImage(g.tileSize, g.tileSize)
  80. return g, nil
  81. }
  82. func (g *Game) Update() error {
  83. return nil
  84. }
  85. func (g *Game) Draw(screen *ebiten.Image) {
  86. const (
  87. tileGap = 64
  88. textGap = 16
  89. gridW = 4
  90. )
  91. // Clear the screen.
  92. screen.Fill(color.White)
  93. // Get an offset for center alignment.
  94. // Where sw, sh is the screen size in pixels.
  95. // And gw, gh is the grid size in pixels.
  96. totalTileSize := g.tileSize + tileGap
  97. gridH := (len(g.modes)-1)/gridW + 1
  98. gw, gh := gridW*totalTileSize-tileGap, gridH*totalTileSize
  99. sw, sh := screen.Bounds().Dx(), screen.Bounds().Dy()
  100. ox, oy := float64(sw-gw)/2, float64(sh-gh)/2
  101. // Draw a tilemap.
  102. for i, m := range g.modes {
  103. x := i % gridW
  104. y := i / gridW
  105. px, py := x*totalTileSize, y*totalTileSize
  106. // Making a place for the text.
  107. py += textGap * y
  108. // Drawing the blend mode and it's name.
  109. alignedX, alignedY := float64(px)+ox, float64(py)+oy
  110. g.drawBlendMode(screen, alignedX, alignedY, m.blend)
  111. drawCenteredText(screen, alignedX+float64(g.tileSize)/2, alignedY+float64(g.tileSize)+textGap, m.name)
  112. }
  113. }
  114. func (g *Game) Layout(w, h int) (int, int) {
  115. return screenWidth, screenHeight
  116. }
  117. func (g *Game) drawBlendMode(screen *ebiten.Image, x, y float64, mode ebiten.Blend) {
  118. // Copy the destination image to offscreen so as not to modify it.
  119. g.offscreen.Clear()
  120. g.offscreen.DrawImage(g.dest, nil)
  121. // Select and apply blending mode.
  122. op := &ebiten.DrawImageOptions{}
  123. op.Blend = mode
  124. g.offscreen.DrawImage(g.source, op)
  125. // Draw the result on the passed coordinates.
  126. op = &ebiten.DrawImageOptions{}
  127. op.GeoM.Translate(x, y)
  128. screen.DrawImage(g.offscreen, op)
  129. }
  130. // loadImage is a util function for loading embedded images.
  131. func loadImage(data []byte) (*ebiten.Image, error) {
  132. m, _, err := image.Decode(bytes.NewReader(data))
  133. if err != nil {
  134. return nil, fmt.Errorf("failed to decode image: %w", err)
  135. }
  136. return ebiten.NewImageFromImage(m), nil
  137. }
  138. // max returns the largest of x or y.
  139. // maxSide returns the largest side of a or b images.
  140. func maxSide(a, b *ebiten.Image) int {
  141. return max(a.Bounds().Dx(), b.Bounds().Dx(), a.Bounds().Dy(), b.Bounds().Dy())
  142. }
  143. // drawCenteredText is a util function for drawing blend mode description.
  144. func drawCenteredText(screen *ebiten.Image, cx, cy float64, s string) {
  145. op := &text.DrawOptions{}
  146. op.GeoM.Translate(cx, cy)
  147. op.ColorScale.ScaleWithColor(color.Black)
  148. op.PrimaryAlign = text.AlignCenter
  149. op.SecondaryAlign = text.AlignCenter
  150. text.Draw(screen, s, inconsolataFace, op)
  151. }
  152. func main() {
  153. ebiten.SetWindowSize(640, 640)
  154. ebiten.SetWindowResizingMode(ebiten.WindowResizingModeEnabled)
  155. ebiten.SetWindowTitle("Blend modes (Ebitengine Demo)")
  156. game, err := NewGame()
  157. if err != nil {
  158. log.Fatal(err)
  159. }
  160. if err = ebiten.RunGame(game); err != nil {
  161. log.Fatal(err)
  162. }
  163. }