-
Notifications
You must be signed in to change notification settings - Fork 7
/
FrameCache.go
76 lines (65 loc) · 2.18 KB
/
FrameCache.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
package graphics
import (
"github.com/inkyblackness/hacked/ss1/content/bitmap"
"github.com/inkyblackness/hacked/ui/opengl"
)
// FrameCacheKey is used to identify a cached frame.
type FrameCacheKey uint16
// FrameCache loads bitmaps with their palettes and provides OpenGL textures.
type FrameCache struct {
gl opengl.OpenGL
textures map[FrameCacheKey]*BitmapTexture
palettes map[FrameCacheKey]*PaletteTexture
keyCounter uint16
}
// NewFrameCache returns a new instance.
func NewFrameCache(gl opengl.OpenGL) *FrameCache {
cache := &FrameCache{
gl: gl,
textures: make(map[FrameCacheKey]*BitmapTexture),
palettes: make(map[FrameCacheKey]*PaletteTexture),
}
return cache
}
// AllocateKey returns a new key to be used with uploading a new frame.
func (cache *FrameCache) AllocateKey() FrameCacheKey {
cache.keyCounter++
return FrameCacheKey(cache.keyCounter)
}
// SetTexture registers a texture based on given bitmap under given key.
// The bitmap should contain a palette, otherwise it will most likely not be displayed.
func (cache *FrameCache) SetTexture(key FrameCacheKey, width, height uint16, pixels []byte, palette *bitmap.Palette) {
cache.DropTextureForKey(key)
tex := NewBitmapTexture(cache.gl, int(width), int(height), pixels)
cache.textures[key] = tex
if palette == nil {
return
}
pal := NewPaletteTexture(cache.gl, palette)
cache.palettes[key] = pal
}
// DropTextureForKey removes the currently cached frame.
func (cache *FrameCache) DropTextureForKey(key FrameCacheKey) {
if tex, existing := cache.textures[key]; existing {
tex.Dispose()
delete(cache.textures, key)
}
if pal, existing := cache.palettes[key]; existing {
pal.Dispose()
delete(cache.palettes, key)
}
}
// HandlesForKey returns the OpenGL handles for both palette and texture for given key.
func (cache FrameCache) HandlesForKey(key FrameCacheKey) (palette uint32, texture uint32) {
if tex, existing := cache.textures[key]; existing {
texture = tex.Handle()
}
if pal, existing := cache.palettes[key]; existing {
palette = pal.Handle()
}
return
}
// Texture returns the buffered texture for given key.
func (cache *FrameCache) Texture(key FrameCacheKey) *BitmapTexture {
return cache.textures[key]
}