/
tile.go
83 lines (70 loc) · 1.47 KB
/
tile.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
package zrle
import ()
const (
TILE_WIDTH int = 64
TILE_HEIGHT = 64
)
type tileConfig struct {
width, height int
}
type tile struct {
x, y, width, height, bytesPerCPixel int
pixels []cpixel
}
func (t tile) toPixelGrid() [][]cpixel {
pixels := make([][]cpixel, t.height)
for i := range pixels {
pixels[i] = make([]cpixel, t.width)
}
i, j := 0, 0
for _, pixel := range t.pixels {
if j >= t.width {
i++
j = 0
}
pixels[i][j] = pixel
j++
}
return pixels
}
func createTiles(width int, height int) (tiles []tile) {
x, y := 0, 0
for height > 0 {
rowWidth := width
// If row is shorter than TILE_HEIGHT adjust
rowHeight := TILE_HEIGHT
if height < rowHeight {
rowHeight = height
}
height -= rowHeight
for rowWidth > 0 {
// If tile is narrower than TILE_WIDTH adjust
tileWidth := TILE_WIDTH
if rowWidth < tileWidth {
tileWidth = rowWidth
}
rowWidth -= tileWidth
newTile := tile{x: x, y: y, width: tileWidth, height: rowHeight}
tiles = append(tiles, newTile)
x += tileWidth
}
x = 0
y += rowHeight
}
return
}
func tilesToPixels(width int, height int, tiles []tile) [][]cpixel {
pixels := make([][]cpixel, height)
for i := range pixels {
pixels[i] = make([]cpixel, width)
}
for _, tile := range tiles {
tilePixels := tile.toPixelGrid()
for i, tileRow := range tilePixels {
for j, pixel := range tileRow {
pixels[tile.y+i][tile.x+j] = pixel
}
}
}
return pixels
}