-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtilemap.go
More file actions
187 lines (151 loc) · 3.24 KB
/
tilemap.go
File metadata and controls
187 lines (151 loc) · 3.24 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
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
package retroimg
import (
"image"
"image/color"
"io"
"fmt"
)
type CharSize int
const (
CS_8x8 CharSize = iota
CS_16x16
CS_16x8
CS_32x32
CS_64x64
CS_16x32
CS_32x64
)
func (cs CharSize) XY() (int, int) {
switch cs {
case CS_8x8:
return 8, 8
case CS_16x16:
return 16, 16
case CS_16x8:
return 16, 8
case CS_32x32:
return 32, 32
case CS_64x64:
return 64, 64
case CS_16x32:
return 16, 32
case CS_32x64:
return 32, 64
}
panic(fmt.Sprintf("invalid CharSize: %d", int(cs)))
}
type Tilemap struct {
Tiles []TileMetadata
// 8x8 or 16x16 "tiles". Actual tiles are always 8x8, but 16x16 acts as a
// metatile of sorts.
CharacterSize CharSize
Palettes []color.Palette
}
func validateTilemapValues(cs CharSize, depth BitDepth, pals []color.Palette) error {
switch cs {
case CS_8x8, CS_16x16, CS_16x8:
// valid
default:
return fmt.Errorf("invalid CharSize: %#v", cs)
}
if len(pals) > 8 {
return fmt.Errorf("too many palettes")
} else if len(pals) <= 0 {
return fmt.Errorf("too few palettes")
}
// Max value
var palSize int
switch depth {
case BD_2bpp:
palSize = 4
case BD_4bpp:
palSize = 16
case BD_8bpp:
palSize = 256
case BD_DirectColor:
return fmt.Errorf("not implemented yet")
}
for i, p := range pals {
if len(p) > palSize { // DirectColor??
return fmt.Errorf("palette at index %d contains too many colors: %d; max: %d",
i, len(p), palSize)
}
}
return nil
}
func NewTilemap(cs CharSize, depth BitDepth, pals []color.Palette) (*Tilemap, error) {
err := validateTilemapValues(cs, depth, pals)
if err != nil {
return nil, err
}
md := make([]TileMetadata, 32*32)
for i := 0; i < 32*32; i++ {
md[i] = NewTileMetadata(cs, depth, pals[0])
}
return &Tilemap{
Tiles: md,
}, nil
}
func NewTilemapFromImage(cs CharSize, depth BitDepth, pals []color.Palette, img image.Image) (*Tilemap, error) {
err := validateTilemapValues(cs, depth, pals)
if err != nil {
return nil, err
}
md := make([]TileMetadata, 32*32)
for i := 0; i < 32*32; i++ {
md[i] = NewTileMetadata(cs, depth, pals[0])
}
tm := &Tilemap{
Tiles: md,
CharacterSize: cs,
Palettes: pals,
}
sizeX, sizeY := cs.XY()
for y := 0; y < 32*sizeY; y++ {
for x := 0; x < 32*sizeX; x++ {
tm.Set(x, y, img.At(x, y))
}
}
return tm, nil
}
func (tm *Tilemap) Bounds() image.Rectangle {
x, y := tm.CharacterSize.XY()
return image.Rect(0, 0, 32*x, 32*y)
}
func (tm *Tilemap) Image() image.Image {
img := image.NewRGBA(tm.Bounds())
for y := 0; y < tm.Bounds().Max.Y; y++ {
for x := 0; x < tm.Bounds().Max.X; x++ {
img.Set(x, y, tm.At(x, y))
}
}
return img
}
func (tm *Tilemap) ChrBin() []byte {
panic("not yet")
}
func (tm *Tilemap) ChrAsm() []string {
panic("not yet")
}
func (tm *Tilemap) WriteChr(w io.Writer) error {
panic("not yet")
}
func (tm *Tilemap) WriteAsm(w io.Writer) error {
panic("not yet")
}
func (tm *Tilemap) At(x, y int) color.Color {
width, height := tm.CharacterSize.XY()
row := y / height
col := x / width
tx := x % width
ty := y % height
return tm.Tiles[(row*32)+col].At(tx, ty)
}
func (tm *Tilemap) Set(x, y int, c color.Color) {
width, height := tm.CharacterSize.XY()
row := y / height
col := x / width
tx := x % width
ty := y % height
tm.Tiles[(row*32)+col].Set(tx, ty, c)
}