-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcache.go
More file actions
111 lines (90 loc) · 2.54 KB
/
cache.go
File metadata and controls
111 lines (90 loc) · 2.54 KB
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
package ebui
import (
"fmt"
"image/color"
"sync"
"github.com/hajimehoshi/ebiten/v2"
)
// ImageCache provides a global cache for commonly used images
type ImageCache struct {
mu sync.RWMutex
// Cache for solid color images, keyed by "WxH-RGBA"
colorCache map[string]*ebiten.Image
// Cache for specific sizes, keyed by "WxH"
sizeCache map[string]*ebiten.Image
}
var (
globalCache = &ImageCache{
colorCache: make(map[string]*ebiten.Image),
sizeCache: make(map[string]*ebiten.Image),
}
)
// ImageWithColor returns a cached image of the specified size and color
func (c *ImageCache) ImageWithColor(width, height int, col color.Color) *ebiten.Image {
r, g, b, a := col.RGBA()
key := fmt.Sprintf("%dx%d-%d%d%d%d", width, height, r>>8, g>>8, b>>8, a>>8)
c.mu.RLock()
if img, ok := c.colorCache[key]; ok {
c.mu.RUnlock()
return img
}
c.mu.RUnlock()
c.mu.Lock()
defer c.mu.Unlock()
// Double-check after acquiring write lock
if img, ok := c.colorCache[key]; ok {
return img
}
img := ebiten.NewImage(width, height)
img.Fill(col)
c.colorCache[key] = img
return img
}
// BorderImageWithColor returns a cached border image of the specified size and color
func (c *ImageCache) BorderImageWithColor(width, height int, col color.Color) *ebiten.Image {
r, g, b, a := col.RGBA()
key := fmt.Sprintf("border-%dx%d-%d%d%d%d", width, height, r>>8, g>>8, b>>8, a>>8)
c.mu.RLock()
if img, ok := c.colorCache[key]; ok {
c.mu.RUnlock()
return img
}
c.mu.RUnlock()
c.mu.Lock()
defer c.mu.Unlock()
// Double-check after acquiring write lock
if img, ok := c.colorCache[key]; ok {
return img
}
// Create new border image
img := ebiten.NewImage(width, height)
// Create horizontal and vertical lines
horizontalLine := ebiten.NewImage(width, 1)
horizontalLine.Fill(col)
verticalLine := ebiten.NewImage(1, height)
verticalLine.Fill(col)
// Draw top and bottom
op := &ebiten.DrawImageOptions{}
img.DrawImage(horizontalLine, op)
op.GeoM.Translate(0, float64(height-1))
img.DrawImage(horizontalLine, op)
// Draw left and right
op = &ebiten.DrawImageOptions{}
img.DrawImage(verticalLine, op)
op.GeoM.Translate(float64(width-1), 0)
img.DrawImage(verticalLine, op)
c.colorCache[key] = img
return img
}
// Clear empties the cache
func (c *ImageCache) Clear() {
c.mu.Lock()
defer c.mu.Unlock()
// Clear all references to allow garbage collection
c.colorCache = make(map[string]*ebiten.Image)
c.sizeCache = make(map[string]*ebiten.Image)
}
// GetCache returns the global image cache instance
func GetCache() *ImageCache {
return globalCache
}