Go で書き直した Ikemen
Revision | 2ffd702871e224a97d816718eb0e8de3d74134b1 (tree) |
---|---|
Time | 2016-11-03 23:55:13 |
Author | SUEHIRO <supersuehiro@user...> |
Commiter | SUEHIRO |
PalleteList
@@ -3,4 +3,4 @@ GOPATH=$PWD/go | ||
3 | 3 | export GOPATH |
4 | 4 | go fmt ./src/*.go |
5 | 5 | godoc -src ./src .* > godoc.txt |
6 | -GODEBUG=cgocheck=0 go run ./src/*.go | |
6 | +go generate ./src/*.go && GODEBUG=cgocheck=0 go run ./src/*.go |
@@ -0,0 +1,37 @@ | ||
1 | +package main | |
2 | + | |
3 | +import "os" | |
4 | + | |
5 | +var AppendFunc = [...][2]string{ | |
6 | + {"I", "int"}, | |
7 | + {"Pal", "[]color.Color"}, | |
8 | +} | |
9 | + | |
10 | +func main() { | |
11 | + out, err := os.Create("generated.go") | |
12 | + if err != nil { | |
13 | + panic(err) | |
14 | + } | |
15 | + defer out.Close() | |
16 | + write := func(str string) { | |
17 | + _, err := out.WriteString(str) | |
18 | + if err != nil { | |
19 | + panic(err) | |
20 | + } | |
21 | + } | |
22 | + write("package main\n\nimport \"image/color\"\n\n") | |
23 | + for i := range AppendFunc { | |
24 | + write("func Append") | |
25 | + write(AppendFunc[i][0]) | |
26 | + write("(slice *[]") | |
27 | + write(AppendFunc[i][1]) | |
28 | + write(", data ...") | |
29 | + write(AppendFunc[i][1]) | |
30 | + write(") {\n\tm := len(*slice)\n\tn := m + len(data)\n") | |
31 | + write("\tif n > cap(*slice) {\n\t\tnewSlice := make([]") | |
32 | + write(AppendFunc[i][1]) | |
33 | + write(", n+n/4)\n") | |
34 | + write("\t\tcopy(newSlice, *slice)\n\t\t*slice = newSlice\n\t}\n") | |
35 | + write("\t*slice = (*slice)[:n]\n\tcopy((*slice)[m:n], data)\n}\n") | |
36 | + } | |
37 | +} |
@@ -0,0 +1,26 @@ | ||
1 | +package main | |
2 | + | |
3 | +import "image/color" | |
4 | + | |
5 | +func AppendI(slice *[]int, data ...int) { | |
6 | + m := len(*slice) | |
7 | + n := m + len(data) | |
8 | + if n > cap(*slice) { | |
9 | + newSlice := make([]int, n+n/4) | |
10 | + copy(newSlice, *slice) | |
11 | + *slice = newSlice | |
12 | + } | |
13 | + *slice = (*slice)[:n] | |
14 | + copy((*slice)[m:n], data) | |
15 | +} | |
16 | +func AppendPal(slice *[][]color.Color, data ...[]color.Color) { | |
17 | + m := len(*slice) | |
18 | + n := m + len(data) | |
19 | + if n > cap(*slice) { | |
20 | + newSlice := make([][]color.Color, n+n/4) | |
21 | + copy(newSlice, *slice) | |
22 | + *slice = newSlice | |
23 | + } | |
24 | + *slice = (*slice)[:n] | |
25 | + copy((*slice)[m:n], data) | |
26 | +} |
@@ -3,6 +3,7 @@ package main | ||
3 | 3 | import ( |
4 | 4 | "encoding/binary" |
5 | 5 | "github.com/go-gl/gl/v2.1/gl" |
6 | + "image/color" | |
6 | 7 | "os" |
7 | 8 | "strings" |
8 | 9 | ) |
@@ -110,15 +111,74 @@ func gltest() { | ||
110 | 111 | gl.DeleteObjectARB(vertObj) |
111 | 112 | } |
112 | 113 | |
114 | +type PalleteList struct { | |
115 | + palletes [][]color.Color | |
116 | + palleteMap []int | |
117 | + PalTable map[[2]int16]int | |
118 | +} | |
119 | + | |
120 | +func (pl *PalleteList) Clear() { | |
121 | + pl.palletes = nil | |
122 | + pl.palleteMap = nil | |
123 | + pl.PalTable = make(map[[2]int16]int) | |
124 | +} | |
125 | +func (pl *PalleteList) SetSource(i int, p []color.Color) { | |
126 | + if i < len(pl.palleteMap) { | |
127 | + pl.palleteMap[i] = i | |
128 | + } else { | |
129 | + for i > len(pl.palleteMap) { | |
130 | + AppendI(&pl.palleteMap, len(pl.palleteMap)) | |
131 | + } | |
132 | + AppendI(&pl.palleteMap, i) | |
133 | + } | |
134 | + if i < len(pl.palletes) { | |
135 | + pl.palletes[i] = p | |
136 | + } else { | |
137 | + for i > len(pl.palletes) { | |
138 | + AppendPal(&pl.palletes, nil) | |
139 | + } | |
140 | + AppendPal(&pl.palletes, p) | |
141 | + } | |
142 | +} | |
143 | +func (pl *PalleteList) NewPal() (i int, p []color.Color) { | |
144 | + i = len(pl.palletes) | |
145 | + p = make([]color.Color, 256) | |
146 | + pl.SetSource(i, p) | |
147 | + return | |
148 | +} | |
149 | +func (pl *PalleteList) Get(i int) []color.Color { | |
150 | + return pl.palletes[pl.palleteMap[i]] | |
151 | +} | |
152 | +func (pl *PalleteList) Remap(source int, destination int) { | |
153 | + pl.palleteMap[source] = destination | |
154 | +} | |
155 | +func (pl *PalleteList) ResetRemap() { | |
156 | + for i := range pl.palleteMap { | |
157 | + pl.palleteMap[i] = i | |
158 | + } | |
159 | +} | |
160 | +func (pl *PalleteList) GetPalMap() []int { | |
161 | + pm := make([]int, len(pl.palleteMap)) | |
162 | + copy(pm, pl.palleteMap) | |
163 | + return pm | |
164 | +} | |
165 | +func (pl *PalleteList) SwapPalMap(palMap *[]int) bool { | |
166 | + if len(*palMap) != len(pl.palleteMap) { | |
167 | + return false | |
168 | + } | |
169 | + *palMap, pl.palleteMap = pl.palleteMap, *palMap | |
170 | + return true | |
171 | +} | |
172 | + | |
113 | 173 | type SffHeader struct { |
114 | - ver0, ver1, ver2, ver3 byte | |
115 | - firstSpriteHeaderOffset uint32 | |
116 | - firstPaletteHeaderOffset uint32 | |
117 | - numberOfPalettes uint32 | |
118 | - numberOfSprites uint32 | |
174 | + Ver0, Ver1, Ver2, Ver3 byte | |
175 | + FirstSpriteHeaderOffset uint32 | |
176 | + FirstPaletteHeaderOffset uint32 | |
177 | + NumberOfSprites uint32 | |
178 | + NumberOfPalettes uint32 | |
119 | 179 | } |
120 | 180 | |
121 | -func (sh *SffHeader) read(f *os.File, lofs *uint32, tofs *uint32) error { | |
181 | +func (sh *SffHeader) Read(f *os.File, lofs *uint32, tofs *uint32) error { | |
122 | 182 | buf := make([]byte, 12) |
123 | 183 | n, err := f.Read(buf) |
124 | 184 | if err != nil { |
@@ -130,29 +190,29 @@ func (sh *SffHeader) read(f *os.File, lofs *uint32, tofs *uint32) error { | ||
130 | 190 | read := func(x interface{}) error { |
131 | 191 | return binary.Read(f, binary.LittleEndian, x) |
132 | 192 | } |
133 | - if err := read(&sh.ver3); err != nil { | |
193 | + if err := read(&sh.Ver3); err != nil { | |
134 | 194 | return err |
135 | 195 | } |
136 | - if err := read(&sh.ver2); err != nil { | |
196 | + if err := read(&sh.Ver2); err != nil { | |
137 | 197 | return err |
138 | 198 | } |
139 | - if err := read(&sh.ver1); err != nil { | |
199 | + if err := read(&sh.Ver1); err != nil { | |
140 | 200 | return err |
141 | 201 | } |
142 | - if err := read(&sh.ver0); err != nil { | |
202 | + if err := read(&sh.Ver0); err != nil { | |
143 | 203 | return err |
144 | 204 | } |
145 | 205 | var dummy uint32 |
146 | 206 | if err := read(&dummy); err != nil { |
147 | 207 | return err |
148 | 208 | } |
149 | - switch sh.ver0 { | |
209 | + switch sh.Ver0 { | |
150 | 210 | case 1: |
151 | - sh.firstPaletteHeaderOffset ,sh.numberOfPalettes = 0,0 | |
152 | - if err := read(&sh.numberOfSprites); err != nil { | |
211 | + sh.FirstPaletteHeaderOffset, sh.NumberOfPalettes = 0, 0 | |
212 | + if err := read(&sh.NumberOfSprites); err != nil { | |
153 | 213 | return err |
154 | 214 | } |
155 | - if err := read(&sh.firstSpriteHeaderOffset); err != nil { | |
215 | + if err := read(&sh.FirstSpriteHeaderOffset); err != nil { | |
156 | 216 | return err |
157 | 217 | } |
158 | 218 | if err := read(&dummy); err != nil { |
@@ -164,16 +224,16 @@ func (sh *SffHeader) read(f *os.File, lofs *uint32, tofs *uint32) error { | ||
164 | 224 | return err |
165 | 225 | } |
166 | 226 | } |
167 | - if err := read(&sh.firstSpriteHeaderOffset); err != nil { | |
227 | + if err := read(&sh.FirstSpriteHeaderOffset); err != nil { | |
168 | 228 | return err |
169 | 229 | } |
170 | - if err := read(&sh.numberOfSprites); err != nil { | |
230 | + if err := read(&sh.NumberOfSprites); err != nil { | |
171 | 231 | return err |
172 | 232 | } |
173 | - if err := read(&sh.firstPaletteHeaderOffset); err != nil { | |
233 | + if err := read(&sh.FirstPaletteHeaderOffset); err != nil { | |
174 | 234 | return err |
175 | 235 | } |
176 | - if err := read(&sh.numberOfPalettes); err != nil { | |
236 | + if err := read(&sh.NumberOfPalettes); err != nil { | |
177 | 237 | return err |
178 | 238 | } |
179 | 239 | if err := read(lofs); err != nil { |
@@ -1,5 +1,6 @@ | ||
1 | 1 | package main |
2 | 2 | |
3 | +//go:generate go run ./gen/gen.go | |
3 | 4 | import ( |
4 | 5 | "github.com/Shopify/go-lua" |
5 | 6 | "github.com/go-gl/gl/v2.1/gl" |