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