Go で書き直した Ikemen
Revisión | e6dd36fdd0f712b5d1e3bfe0e611275af82034d5 (tree) |
---|---|
Tiempo | 2016-11-30 00:40:54 |
Autor | SUEHIRO <supersuehiro@user...> |
Commiter | SUEHIRO |
グローバル変数をsysに移した
@@ -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 | -go generate ./src/main.go && go run ./src/*.go | |
6 | +go run ./src/*.go |
@@ -0,0 +1,8 @@ | ||
1 | +addChar([[ | |
2 | +randomselect | |
3 | +kfmx | |
4 | +]]) | |
5 | + | |
6 | +addStage([[ | |
7 | +SpaceRainbowLowRes.def | |
8 | +]]) |
@@ -166,7 +166,7 @@ func ReadAnimation(sff *Sff, lines []string, i *int) *Animation { | ||
166 | 166 | af.Ex[0] = clsn1 |
167 | 167 | af.Ex[1] = clsn2 |
168 | 168 | } |
169 | - a.frames = AppendAF(a.frames, *af) | |
169 | + a.frames = append(a.frames, *af) | |
170 | 170 | def1, def2 = true, true |
171 | 171 | case len(line) >= 9 && line[:9] == "loopstart": |
172 | 172 | a.loopstart = int32(len(a.frames)) |
@@ -399,7 +399,7 @@ func (a *Animation) alpha() int32 { | ||
399 | 399 | if sa == 1 && da == 255 { |
400 | 400 | return -2 |
401 | 401 | } |
402 | - sa = byte(int32(sa) * brightness >> 8) | |
402 | + sa = byte(int32(sa) * sys.brightness >> 8) | |
403 | 403 | if sa < 5 && da == 255 { |
404 | 404 | return 0 |
405 | 405 | } |
@@ -483,15 +483,15 @@ func (a *Animation) Draw(window *[4]int32, x, y, xcs, ycs, xs, xbs, ys, | ||
483 | 483 | y -= float32(int(y/tmp)) * tmp |
484 | 484 | } |
485 | 485 | } |
486 | - rcx, rcy = rcx*widthScale, 0 | |
486 | + rcx, rcy = rcx*sys.widthScale, 0 | |
487 | 487 | x, y = -x+xs*float32(a.spr.Offset[0]), -y+ys*float32(a.spr.Offset[1]) |
488 | 488 | } else { |
489 | - rcx, rcy = (x+rcx)*widthScale, y*heightScale | |
489 | + rcx, rcy = (x+rcx)*sys.widthScale, y*sys.heightScale | |
490 | 490 | x, y = AbsF(xs)*float32(a.spr.Offset[0]), AbsF(ys)*float32(a.spr.Offset[1]) |
491 | 491 | } |
492 | - a.spr.glDraw(a.pal(pfx), int32(a.mask), x*widthScale, y*heightScale, | |
493 | - &a.tile, xs*widthScale, xcs*xbs*h*widthScale, ys*heightScale, | |
494 | - xcs*rxadd*widthScale/heightScale, angle, a.alpha(), window, | |
492 | + a.spr.glDraw(a.pal(pfx), int32(a.mask), x*sys.widthScale, y*sys.heightScale, | |
493 | + &a.tile, xs*sys.widthScale, xcs*xbs*h*sys.widthScale, ys*sys.heightScale, | |
494 | + xcs*rxadd*sys.widthScale/sys.heightScale, angle, a.alpha(), window, | |
495 | 495 | rcx, rcy, pfx) |
496 | 496 | } |
497 | 497 |
@@ -547,7 +547,7 @@ type Anim struct { | ||
547 | 547 | func NewAnim(sff *Sff, action string) *Anim { |
548 | 548 | lines, i := SplitAndTrim(action, "\n"), 0 |
549 | 549 | a := &Anim{anim: ReadAnimation(sff, lines, &i), |
550 | - window: scrrect, xscl: 1, yscl: 1} | |
550 | + window: sys.scrrect, xscl: 1, yscl: 1} | |
551 | 551 | if len(a.anim.frames) == 0 { |
552 | 552 | return nil |
553 | 553 | } |
@@ -573,18 +573,18 @@ func (a *Anim) SetScale(x, y float32) { | ||
573 | 573 | a.xscl, a.yscl = x, y |
574 | 574 | } |
575 | 575 | func (a *Anim) SetWindow(x, y, w, h float32) { |
576 | - a.window[0] = int32((x + float32(gameWidth-320)/2) * widthScale) | |
577 | - a.window[1] = int32((y + float32(gameHeight-240)) * heightScale) | |
578 | - a.window[2] = int32(w*widthScale + 0.5) | |
579 | - a.window[3] = int32(h*heightScale + 0.5) | |
576 | + a.window[0] = int32((x + float32(sys.gameWidth-320)/2) * sys.widthScale) | |
577 | + a.window[1] = int32((y + float32(sys.gameHeight-240)) * sys.heightScale) | |
578 | + a.window[2] = int32(w*sys.widthScale + 0.5) | |
579 | + a.window[3] = int32(h*sys.heightScale + 0.5) | |
580 | 580 | } |
581 | 581 | func (a *Anim) Update() { |
582 | 582 | a.anim.Action() |
583 | 583 | } |
584 | 584 | func (a *Anim) Draw() { |
585 | - if !frameSkip { | |
586 | - a.anim.Draw(&a.window, a.x+float32(gameWidth-320)/2, | |
587 | - a.y+float32(gameHeight-240), 1, 1, a.xscl, a.xscl, a.yscl, | |
585 | + if !sys.frameSkip { | |
586 | + a.anim.Draw(&a.window, a.x+float32(sys.gameWidth-320)/2, | |
587 | + a.y+float32(sys.gameHeight-240), 1, 1, a.xscl, a.xscl, a.yscl, | |
588 | 588 | 0, 0, 0, nil, false) |
589 | 589 | } |
590 | 590 | } |
@@ -15,17 +15,15 @@ const ( | ||
15 | 15 | IErr = ^IMax |
16 | 16 | ) |
17 | 17 | |
18 | -var randseed int32 | |
19 | - | |
20 | 18 | func Random() int32 { |
21 | - w := randseed / 127773 | |
22 | - randseed = (randseed-w*127773)*16807 - w*2836 | |
23 | - if randseed <= 0 { | |
24 | - randseed += IMax - Btoi(randseed == 0) | |
19 | + w := sys.randseed / 127773 | |
20 | + sys.randseed = (sys.randseed-w*127773)*16807 - w*2836 | |
21 | + if sys.randseed <= 0 { | |
22 | + sys.randseed += IMax - Btoi(sys.randseed == 0) | |
25 | 23 | } |
26 | - return randseed | |
24 | + return sys.randseed | |
27 | 25 | } |
28 | -func Srand(s int32) { randseed = s } | |
26 | +func Srand(s int32) { sys.randseed = s } | |
29 | 27 | func Rand(min, max int32) int32 { return min + Random()/(IMax/(max-min+1)+1) } |
30 | 28 | func RandI(x, y int32) int32 { |
31 | 29 | if y < x { |
@@ -176,29 +174,28 @@ func LoadText(filename string) (string, error) { | ||
176 | 174 | } |
177 | 175 | return AsciiToString(bytes), nil |
178 | 176 | } |
179 | -func LoadFile(file *string, deffile string, load func(string) error) error { | |
180 | - var fp string | |
181 | - isNotExist := func() bool { | |
182 | - if _, err := os.Stat(fp); !os.IsNotExist(err) { | |
183 | - return false | |
184 | - } | |
185 | - var pattern string | |
186 | - for _, r := range fp { | |
187 | - if r >= 'A' && r <= 'Z' || r >= 'a' && r <= 'z' { | |
188 | - pattern += "[" + string(unicode.ToLower(r)) + | |
189 | - string(unicode.ToLower(r)+'A'-'a') + "]" | |
190 | - } else if r == '*' || r == '?' || r == '[' { | |
191 | - pattern += "\\" + string(r) | |
192 | - } else { | |
193 | - pattern += string(r) | |
194 | - } | |
195 | - } | |
196 | - if m, _ := filepath.Glob(pattern); len(m) > 0 { | |
197 | - fp = m[0] | |
198 | - return false | |
177 | +func FileExist(filename string) string { | |
178 | + if _, err := os.Stat(filename); !os.IsNotExist(err) { | |
179 | + return filename | |
180 | + } | |
181 | + var pattern string | |
182 | + for _, r := range filename { | |
183 | + if r >= 'A' && r <= 'Z' || r >= 'a' && r <= 'z' { | |
184 | + pattern += "[" + string(unicode.ToLower(r)) + | |
185 | + string(unicode.ToLower(r)+'A'-'a') + "]" | |
186 | + } else if r == '*' || r == '?' || r == '[' { | |
187 | + pattern += "\\" + string(r) | |
188 | + } else { | |
189 | + pattern += string(r) | |
199 | 190 | } |
200 | - return true | |
201 | 191 | } |
192 | + if m, _ := filepath.Glob(pattern); len(m) > 0 { | |
193 | + return m[0] | |
194 | + } | |
195 | + return "" | |
196 | +} | |
197 | +func LoadFile(file *string, deffile string, load func(string) error) error { | |
198 | + var fp string | |
202 | 199 | *file = strings.Replace(*file, "\\", "/", -1) |
203 | 200 | defdir := filepath.Dir(strings.Replace(deffile, "\\", "/", -1)) |
204 | 201 | if defdir == "." { |
@@ -208,11 +205,11 @@ func LoadFile(file *string, deffile string, load func(string) error) error { | ||
208 | 205 | } else { |
209 | 206 | fp = defdir + "/" + *file |
210 | 207 | } |
211 | - if isNotExist() { | |
208 | + if fp = FileExist(fp); len(fp) == 0 { | |
212 | 209 | _else := false |
213 | 210 | if defdir != "data" { |
214 | 211 | fp = "data/" + *file |
215 | - if isNotExist() { | |
212 | + if fp = FileExist(fp); len(fp) == 0 { | |
216 | 213 | _else = true |
217 | 214 | } |
218 | 215 | } else { |
@@ -221,7 +218,9 @@ func LoadFile(file *string, deffile string, load func(string) error) error { | ||
221 | 218 | if _else { |
222 | 219 | if defdir != "." { |
223 | 220 | fp = *file |
224 | - isNotExist() | |
221 | + if fp = FileExist(fp); len(fp) == 0 { | |
222 | + fp = *file | |
223 | + } | |
225 | 224 | } |
226 | 225 | } |
227 | 226 | } |
@@ -373,32 +372,33 @@ func ReadLayout(pre string, is IniSection) *Layout { | ||
373 | 372 | is.ReadF32(pre+"scale", &l.scale[0], &l.scale[1]) |
374 | 373 | return l |
375 | 374 | } |
376 | -func (l *Layout) DrawAnim(r *[4]int32, x, y, scl float32, ln int16, a *Animation) { | |
375 | +func (l *Layout) DrawAnim(r *[4]int32, x, y, scl float32, ln int16, | |
376 | + a *Animation) { | |
377 | 377 | if l.layerno == ln { |
378 | 378 | if l.facing < 0 { |
379 | - x += lifebarFontScale | |
379 | + x += sys.lifebarFontScale | |
380 | 380 | } |
381 | 381 | if l.vfacing < 0 { |
382 | - y += lifebarFontScale | |
382 | + y += sys.lifebarFontScale | |
383 | 383 | } |
384 | - a.Draw(r, x+l.offset[0], y+l.offset[1]+float32(gameHeight-240), scl, scl, | |
385 | - l.scale[0]*float32(l.facing), l.scale[0]*float32(l.facing), | |
384 | + a.Draw(r, x+l.offset[0], y+l.offset[1]+float32(sys.gameHeight-240), | |
385 | + scl, scl, l.scale[0]*float32(l.facing), l.scale[0]*float32(l.facing), | |
386 | 386 | l.scale[1]*float32(l.vfacing), |
387 | - 0, 0, float32(gameWidth-320)/2, nil, false) | |
387 | + 0, 0, float32(sys.gameWidth-320)/2, nil, false) | |
388 | 388 | } |
389 | 389 | } |
390 | 390 | func (l *Layout) DrawText(x, y, scl float32, ln int16, |
391 | 391 | text string, f *Fnt, b, a int32) { |
392 | 392 | if l.layerno == ln { |
393 | 393 | if l.facing < 0 { |
394 | - x += lifebarFontScale | |
394 | + x += sys.lifebarFontScale | |
395 | 395 | } |
396 | 396 | if l.vfacing < 0 { |
397 | - y += lifebarFontScale | |
397 | + y += sys.lifebarFontScale | |
398 | 398 | } |
399 | 399 | f.DrawText(text, (x+l.offset[0])*scl, (y+l.offset[1])*scl, |
400 | - l.scale[0]*lifebarFontScale*float32(l.facing)*scl, | |
401 | - l.scale[1]*lifebarFontScale*float32(l.vfacing)*scl, a, b) | |
400 | + l.scale[0]*sys.lifebarFontScale*float32(l.facing)*scl, | |
401 | + l.scale[1]*sys.lifebarFontScale*float32(l.vfacing)*scl, a, b) | |
402 | 402 | } |
403 | 403 | } |
404 | 404 |
@@ -435,7 +435,7 @@ func (al *AnimLayout) Action() { | ||
435 | 435 | al.anim.Action() |
436 | 436 | } |
437 | 437 | func (al *AnimLayout) Draw(x, y float32, layerno int16) { |
438 | - al.lay.DrawAnim(&scrrect, x, y, 1, layerno, &al.anim) | |
438 | + al.lay.DrawAnim(&sys.scrrect, x, y, 1, layerno, &al.anim) | |
439 | 439 | } |
440 | 440 | |
441 | 441 | type AnimTextSnd struct { |
@@ -238,15 +238,16 @@ func (f *Fnt) drawChar(x, y, xscl, yscl float32, bank int32, c rune, | ||
238 | 238 | if spr == nil { |
239 | 239 | return 0 |
240 | 240 | } |
241 | - spr.glDraw(pal, 0, -x*widthScale, -y*heightScale, ¬iling, | |
242 | - xscl*widthScale, xscl*widthScale, yscl*heightScale, 0, 0, | |
243 | - brightness*255>>8|1<<9, &scrrect, 0, 0, nil) | |
241 | + spr.glDraw(pal, 0, -x*sys.widthScale, -y*sys.heightScale, ¬iling, | |
242 | + xscl*sys.widthScale, xscl*sys.widthScale, yscl*sys.heightScale, 0, 0, | |
243 | + sys.brightness*255>>8|1<<9, &sys.scrrect, 0, 0, nil) | |
244 | 244 | return float32(spr.Size[0]) * xscl |
245 | 245 | } |
246 | 246 | func (f *Fnt) DrawText(txt string, x, y, xscl, yscl float32, bank int32, |
247 | 247 | align int32) { |
248 | - x += float32(f.offset[0])*xscl + float32(gameWidth-320)/2 | |
249 | - y += float32(f.offset[1]-int32(f.Size[1])+1)*yscl + float32(gameHeight-240) | |
248 | + x += float32(f.offset[0])*xscl + float32(sys.gameWidth-320)/2 | |
249 | + y += float32(f.offset[1]-int32(f.Size[1])+1)*yscl + | |
250 | + float32(sys.gameHeight-240) | |
250 | 251 | if align == 0 { |
251 | 252 | x -= float32(f.TextWidth(txt)) * xscl * 0.5 |
252 | 253 | } else if align < 0 { |
@@ -256,8 +257,8 @@ func (f *Fnt) DrawText(txt string, x, y, xscl, yscl float32, bank int32, | ||
256 | 257 | bank = 0 |
257 | 258 | } |
258 | 259 | pal := f.palettes[bank][:] |
259 | - if allPalFX.Time != 0 { | |
260 | - pal = allPalFX.GetFxPal(pal, false) | |
260 | + if sys.allPalFX.Time != 0 { | |
261 | + pal = sys.allPalFX.GetFxPal(pal, false) | |
261 | 262 | } |
262 | 263 | for _, c := range txt { |
263 | 264 | x += f.drawChar(x, y, xscl, yscl, bank, c, pal) + |
@@ -276,7 +277,7 @@ func NewTextSprite() *TextSprite { | ||
276 | 277 | return &TextSprite{align: 1, xscl: 1, yscl: 1} |
277 | 278 | } |
278 | 279 | func (ts *TextSprite) Draw() { |
279 | - if !frameSkip && ts.fnt != nil { | |
280 | + if !sys.frameSkip && ts.fnt != nil { | |
280 | 281 | ts.fnt.DrawText(ts.text, ts.x, ts.y, ts.xscl, ts.yscl, ts.bank, ts.align) |
281 | 282 | } |
282 | 283 | } |
@@ -1,42 +0,0 @@ | ||
1 | -package main | |
2 | - | |
3 | -import "os" | |
4 | - | |
5 | -var AppendFunc = [...][2]string{ | |
6 | - {"I", "int"}, | |
7 | - {"U32", "uint32"}, | |
8 | - {"Pal", "[]uint32"}, | |
9 | - {"AF", "AnimFrame"}, | |
10 | -} | |
11 | - | |
12 | -func main() { | |
13 | - out, err := os.Create("generated.go") | |
14 | - if err != nil { | |
15 | - panic(err) | |
16 | - } | |
17 | - defer out.Close() | |
18 | - write := func(str string) { | |
19 | - _, err := out.WriteString(str) | |
20 | - if err != nil { | |
21 | - panic(err) | |
22 | - } | |
23 | - } | |
24 | - write("package main\n\n") | |
25 | - for i := range AppendFunc { | |
26 | - write("func Append") | |
27 | - write(AppendFunc[i][0]) | |
28 | - write("(slice []") | |
29 | - write(AppendFunc[i][1]) | |
30 | - write(", data ...") | |
31 | - write(AppendFunc[i][1]) | |
32 | - write(") []") | |
33 | - write(AppendFunc[i][1]) | |
34 | - write(" {\n\tm := len(slice)\n\tn := m + len(data)\n") | |
35 | - write("\tif n > cap(slice) {\n\t\tnewSlice := make([]") | |
36 | - write(AppendFunc[i][1]) | |
37 | - write(", n+n/4)\n") | |
38 | - write("\t\tcopy(newSlice, slice)\n\t\tslice = newSlice\n\t}\n") | |
39 | - write("\tslice = slice[:n]\n\tcopy(slice[m:n], data)\n") | |
40 | - write("\treturn slice\n}\n") | |
41 | - } | |
42 | -} |
@@ -1,50 +0,0 @@ | ||
1 | -package main | |
2 | - | |
3 | -func AppendI(slice []int, data ...int) []int { | |
4 | - m := len(slice) | |
5 | - n := m + len(data) | |
6 | - if n > cap(slice) { | |
7 | - newSlice := make([]int, n+n/4) | |
8 | - copy(newSlice, slice) | |
9 | - slice = newSlice | |
10 | - } | |
11 | - slice = slice[:n] | |
12 | - copy(slice[m:n], data) | |
13 | - return slice | |
14 | -} | |
15 | -func AppendU32(slice []uint32, data ...uint32) []uint32 { | |
16 | - m := len(slice) | |
17 | - n := m + len(data) | |
18 | - if n > cap(slice) { | |
19 | - newSlice := make([]uint32, n+n/4) | |
20 | - copy(newSlice, slice) | |
21 | - slice = newSlice | |
22 | - } | |
23 | - slice = slice[:n] | |
24 | - copy(slice[m:n], data) | |
25 | - return slice | |
26 | -} | |
27 | -func AppendPal(slice [][]uint32, data ...[]uint32) [][]uint32 { | |
28 | - m := len(slice) | |
29 | - n := m + len(data) | |
30 | - if n > cap(slice) { | |
31 | - newSlice := make([][]uint32, n+n/4) | |
32 | - copy(newSlice, slice) | |
33 | - slice = newSlice | |
34 | - } | |
35 | - slice = slice[:n] | |
36 | - copy(slice[m:n], data) | |
37 | - return slice | |
38 | -} | |
39 | -func AppendAF(slice []AnimFrame, data ...AnimFrame) []AnimFrame { | |
40 | - m := len(slice) | |
41 | - n := m + len(data) | |
42 | - if n > cap(slice) { | |
43 | - newSlice := make([]AnimFrame, n+n/4) | |
44 | - copy(newSlice, slice) | |
45 | - slice = newSlice | |
46 | - } | |
47 | - slice = slice[:n] | |
48 | - copy(slice[m:n], data) | |
49 | - return slice | |
50 | -} |
@@ -13,8 +13,6 @@ import ( | ||
13 | 13 | "unsafe" |
14 | 14 | ) |
15 | 15 | |
16 | -var allPalFX = NewPalFX() | |
17 | - | |
18 | 16 | type Texture uint32 |
19 | 17 | |
20 | 18 | func textureFinalizer(t *Texture) { |
@@ -65,17 +63,17 @@ func (pl *PaletteList) SetSource(i int, p []uint32) { | ||
65 | 63 | pl.paletteMap[i] = i |
66 | 64 | } else { |
67 | 65 | for i > len(pl.paletteMap) { |
68 | - pl.paletteMap = AppendI(pl.paletteMap, len(pl.paletteMap)) | |
66 | + pl.paletteMap = append(pl.paletteMap, len(pl.paletteMap)) | |
69 | 67 | } |
70 | - pl.paletteMap = AppendI(pl.paletteMap, i) | |
68 | + pl.paletteMap = append(pl.paletteMap, i) | |
71 | 69 | } |
72 | 70 | if i < len(pl.palettes) { |
73 | 71 | pl.palettes[i] = p |
74 | 72 | } else { |
75 | 73 | for i > len(pl.palettes) { |
76 | - pl.palettes = AppendPal(pl.palettes, nil) | |
74 | + pl.palettes = append(pl.palettes, nil) | |
77 | 75 | } |
78 | - pl.palettes = AppendPal(pl.palettes, p) | |
76 | + pl.palettes = append(pl.palettes, p) | |
79 | 77 | } |
80 | 78 | } |
81 | 79 | func (pl *PaletteList) NewPal() (i int, p []uint32) { |
@@ -234,10 +232,10 @@ func LoadFromSff(filename string, g int16, n int16) (*Sprite, error) { | ||
234 | 232 | } |
235 | 233 | var dummy *Sprite |
236 | 234 | var newSubHeaderOffset []uint32 |
237 | - newSubHeaderOffset = AppendU32(newSubHeaderOffset, shofs) | |
235 | + newSubHeaderOffset = append(newSubHeaderOffset, shofs) | |
238 | 236 | i := 0 |
239 | 237 | for ; i < int(h.NumberOfSprites); i++ { |
240 | - newSubHeaderOffset = AppendU32(newSubHeaderOffset, shofs) | |
238 | + newSubHeaderOffset = append(newSubHeaderOffset, shofs) | |
241 | 239 | f.Seek(int64(shofs), 0) |
242 | 240 | if err := foo(); err != nil { |
243 | 241 | return nil, err |
@@ -842,17 +840,17 @@ func (s *Sprite) glDraw(pal []uint32, mask int32, x, y float32, tile *[4]int32, | ||
842 | 840 | } |
843 | 841 | } |
844 | 842 | func (s *Sprite) Draw(x, y, xscale, yscale float32, pal []uint32) { |
845 | - x += float32(gameWidth-320)/2 - xscale*float32(s.Offset[0]) | |
846 | - y += float32(gameHeight-240) - yscale*float32(s.Offset[1]) | |
843 | + x += float32(sys.gameWidth-320)/2 - xscale*float32(s.Offset[0]) | |
844 | + y += float32(sys.gameHeight-240) - yscale*float32(s.Offset[1]) | |
847 | 845 | if xscale < 0 { |
848 | 846 | x *= -1 |
849 | 847 | } |
850 | 848 | if yscale < 0 { |
851 | 849 | y *= -1 |
852 | 850 | } |
853 | - s.glDraw(pal, 0, -x*widthScale, -y*heightScale, ¬iling, | |
854 | - xscale*widthScale, xscale*widthScale, yscale*heightScale, 0, 0, | |
855 | - brightness*255>>8|1<<9, &scrrect, 0, 0, nil) | |
851 | + s.glDraw(pal, 0, -x*sys.widthScale, -y*sys.heightScale, ¬iling, | |
852 | + xscale*sys.widthScale, xscale*sys.widthScale, yscale*sys.heightScale, 0, 0, | |
853 | + sys.brightness*255>>8|1<<9, &sys.scrrect, 0, 0, nil) | |
856 | 854 | } |
857 | 855 | |
858 | 856 | type Sff struct { |
@@ -426,7 +426,7 @@ type AiInput struct { | ||
426 | 426 | } |
427 | 427 | |
428 | 428 | func (__ *AiInput) Update() { |
429 | - if introTime != 0 { | |
429 | + if sys.introTime != 0 { | |
430 | 430 | __.dt, __.at, __.bt, __.ct = 0, 0, 0, 0 |
431 | 431 | __.xt, __.yt, __.zt, __.st = 0, 0, 0, 0 |
432 | 432 | return |
@@ -5,8 +5,6 @@ import ( | ||
5 | 5 | "strings" |
6 | 6 | ) |
7 | 7 | |
8 | -var lifebar Lifebar | |
9 | - | |
10 | 8 | type WinType int32 |
11 | 9 | |
12 | 10 | const ( |
@@ -1,104 +1,20 @@ | ||
1 | 1 | package main |
2 | 2 | |
3 | -//go:generate go run ./gen/gen.go | |
4 | 3 | import ( |
5 | 4 | "github.com/Shopify/go-lua" |
6 | - "github.com/go-gl/gl/v2.1/gl" | |
7 | - "github.com/go-gl/glfw/v3.2/glfw" | |
8 | 5 | "runtime" |
9 | - "time" | |
10 | 6 | ) |
11 | 7 | |
12 | -var scrrect = [4]int32{0, 0, 320, 240} | |
13 | -var gameWidth, gameHeight int32 = 320, 240 | |
14 | -var widthScale, heightScale float32 = 1, 1 | |
15 | -var window *glfw.Window | |
16 | -var gameEnd, frameSkip = false, false | |
17 | -var redrawWait struct{ nextTime, lastDraw time.Time } | |
18 | -var brightness int32 = 256 | |
19 | -var introTime, roundTime int32 = 0, -1 | |
20 | -var lifeMul, team1VS2Life float32 = 1, 1 | |
21 | -var turnsRecoveryRate float32 = 1.0 / 300 | |
22 | -var zoomEnable = false | |
23 | -var zoomMin, zoomMax, zoomSpeed float32 = 1, 1, 1 | |
24 | -var lifebarFontScale float32 = 1 | |
25 | -var debugFont *Fnt | |
26 | -var debugScript string | |
27 | - | |
28 | 8 | func init() { |
29 | 9 | runtime.LockOSThread() |
30 | 10 | } |
31 | -func setWindowSize(w, h int32) { | |
32 | - scrrect[2], scrrect[3] = w, h | |
33 | - if scrrect[2]*3 > scrrect[3]*4 { | |
34 | - gameWidth, gameHeight = scrrect[2]*3*320/(scrrect[3]*4), 240 | |
35 | - } else { | |
36 | - gameWidth, gameHeight = 320, scrrect[3]*4*240/(scrrect[2]*3) | |
37 | - } | |
38 | - widthScale = float32(scrrect[2]) / float32(gameWidth) | |
39 | - heightScale = float32(scrrect[3]) / float32(gameHeight) | |
40 | -} | |
41 | 11 | func chk(err error) { |
42 | 12 | if err != nil { |
43 | 13 | panic(err) |
44 | 14 | } |
45 | 15 | } |
46 | -func await(fps int) { | |
47 | - playSound() | |
48 | - if !frameSkip { | |
49 | - window.SwapBuffers() | |
50 | - } | |
51 | - now := time.Now() | |
52 | - diff := redrawWait.nextTime.Sub(now) | |
53 | - wait := time.Second / time.Duration(fps) | |
54 | - redrawWait.nextTime = redrawWait.nextTime.Add(wait) | |
55 | - switch { | |
56 | - case diff >= 0 && diff < wait+2*time.Millisecond: | |
57 | - time.Sleep(diff) | |
58 | - fallthrough | |
59 | - case now.Sub(redrawWait.lastDraw) > 250*time.Millisecond: | |
60 | - fallthrough | |
61 | - case diff >= -17*time.Millisecond: | |
62 | - redrawWait.lastDraw = now | |
63 | - frameSkip = false | |
64 | - default: | |
65 | - if diff < -150*time.Millisecond { | |
66 | - redrawWait.nextTime = now.Add(wait) | |
67 | - } | |
68 | - frameSkip = true | |
69 | - } | |
70 | - glfw.PollEvents() | |
71 | - gameEnd = window.ShouldClose() | |
72 | - if !frameSkip { | |
73 | - gl.Viewport(0, 0, int32(scrrect[2]), int32(scrrect[3])) | |
74 | - gl.Clear(gl.COLOR_BUFFER_BIT) | |
75 | - } | |
76 | -} | |
77 | 16 | func main() { |
78 | - chk(glfw.Init()) | |
79 | - defer glfw.Terminate() | |
80 | - glfw.WindowHint(glfw.Resizable, glfw.False) | |
81 | - glfw.WindowHint(glfw.ContextVersionMajor, 2) | |
82 | - glfw.WindowHint(glfw.ContextVersionMinor, 1) | |
83 | - setWindowSize(640, 480) | |
84 | - var err error | |
85 | - window, err = glfw.CreateWindow(int(scrrect[2]), int(scrrect[3]), | |
86 | - "Ikemen GO", nil, nil) | |
87 | - chk(err) | |
88 | - window.MakeContextCurrent() | |
89 | - window.SetKeyCallback(keyCallback) | |
90 | - glfw.SwapInterval(1) | |
91 | - chk(gl.Init()) | |
92 | - randseed = int32(time.Now().UnixNano()) | |
93 | - keyConfig = append(keyConfig, &KeyConfig{-1, | |
94 | - int(glfw.KeyUp), int(glfw.KeyDown), int(glfw.KeyLeft), int(glfw.KeyRight), | |
95 | - int(glfw.KeyZ), int(glfw.KeyX), int(glfw.KeyC), | |
96 | - int(glfw.KeyA), int(glfw.KeyS), int(glfw.KeyD), int(glfw.KeyEnter)}) | |
97 | - RenderInit() | |
98 | - audioOpen() | |
99 | - l := lua.NewState() | |
100 | - lua.OpenLibraries(l) | |
101 | - systemScriptInit(l) | |
17 | + l := sys.init(640, 480) | |
102 | 18 | if err := lua.DoFile(l, "script/main.lua"); err != nil { |
103 | 19 | switch err.(type) { |
104 | 20 | case lua.RuntimeError: |
@@ -145,7 +145,7 @@ func drawQuads(x1, y1, x2, y2, x3, y3, x4, y4, r, g, b, a, pers float32) { | ||
145 | 145 | gl.Vertex2f(x4, y4) |
146 | 146 | if pers != 1 { |
147 | 147 | n := int((1 - (pers * pers)) * AbsF(x1-x2) * |
148 | - float32(scrrect[3]>>5) / (AbsF(y1-y4) + float32(scrrect[3]>>5))) | |
148 | + float32(sys.scrrect[3]>>5) / (AbsF(y1-y4) + float32(sys.scrrect[3]>>5))) | |
149 | 149 | for i := 1; i < n; i++ { |
150 | 150 | gl.TexCoord2f(float32(i)/float32(n), 1) |
151 | 151 | gl.Vertex2f(x1+(x2-x1)*float32(i)/float32(n), |
@@ -177,17 +177,18 @@ func rmTileHSub(x1, y1, x2, y2, x3, y3, x4, y4, xtw, xbw, xts, xbs float32, | ||
177 | 177 | x4d = x3d - xtw |
178 | 178 | x1d = x2d - xbw |
179 | 179 | if topdist < 0 { |
180 | - if x1d >= float32(scrrect[2]) && x2d >= float32(scrrect[2]) && | |
181 | - x3d >= float32(scrrect[2]) && x4d >= float32(scrrect[2]) { | |
180 | + if x1d >= float32(sys.scrrect[2]) && | |
181 | + x2d >= float32(sys.scrrect[2]) && x3d >= float32(sys.scrrect[2]) && | |
182 | + x4d >= float32(sys.scrrect[2]) { | |
182 | 183 | break |
183 | 184 | } |
184 | 185 | } else if x1d <= 0 && x2d <= 0 && x3d <= 0 && x4d <= 0 { |
185 | 186 | break |
186 | 187 | } |
187 | 188 | if (0 < x1d || 0 < x2d) && |
188 | - (x1d < float32(scrrect[2]) || x2d < float32(scrrect[2])) || | |
189 | + (x1d < float32(sys.scrrect[2]) || x2d < float32(sys.scrrect[2])) || | |
189 | 190 | (0 < x3d || 0 < x4d) && |
190 | - (x3d < float32(scrrect[2]) || x4d < float32(scrrect[2])) { | |
191 | + (x3d < float32(sys.scrrect[2]) || x4d < float32(sys.scrrect[2])) { | |
191 | 192 | drawQuads(x1d, y1, x2d, y2, x3d, y3, x4d, y4, r, g, b, a, pers) |
192 | 193 | } |
193 | 194 | } |
@@ -196,17 +197,17 @@ func rmTileHSub(x1, y1, x2, y2, x3, y3, x4, y4, xtw, xbw, xts, xbs float32, | ||
196 | 197 | n := (*tl)[2] |
197 | 198 | for { |
198 | 199 | if topdist > 0 { |
199 | - if x1 >= float32(scrrect[2]) && x2 >= float32(scrrect[2]) && | |
200 | - x3 >= float32(scrrect[2]) && x4 >= float32(scrrect[2]) { | |
200 | + if x1 >= float32(sys.scrrect[2]) && x2 >= float32(sys.scrrect[2]) && | |
201 | + x3 >= float32(sys.scrrect[2]) && x4 >= float32(sys.scrrect[2]) { | |
201 | 202 | break |
202 | 203 | } |
203 | 204 | } else if x1 <= 0 && x2 <= 0 && x3 <= 0 && x4 <= 0 { |
204 | 205 | break |
205 | 206 | } |
206 | 207 | if (0 < x1 || 0 < x2) && |
207 | - (x1 < float32(scrrect[2]) || x2 < float32(scrrect[2])) || | |
208 | + (x1 < float32(sys.scrrect[2]) || x2 < float32(sys.scrrect[2])) || | |
208 | 209 | (0 < x3 || 0 < x4) && |
209 | - (x3 < float32(scrrect[2]) || x4 < float32(scrrect[2])) { | |
210 | + (x3 < float32(sys.scrrect[2]) || x4 < float32(sys.scrrect[2])) { | |
210 | 211 | drawQuads(x1, y1, x2, y2, x3, y3, x4, y4, r, g, b, a, pers) |
211 | 212 | } |
212 | 213 | if (*tl)[2] != 1 && n != 0 { |
@@ -254,14 +255,14 @@ func rmTileSub(w, h uint16, x, y float32, tl *[4]int32, | ||
254 | 255 | } |
255 | 256 | y4d = y3d |
256 | 257 | if ys*(float32(h)+float32((*tl)[1])) < 0 { |
257 | - if y1d <= float32(-scrrect[3]) && y4d <= float32(-scrrect[3]) { | |
258 | + if y1d <= float32(-sys.scrrect[3]) && y4d <= float32(-sys.scrrect[3]) { | |
258 | 259 | break |
259 | 260 | } |
260 | 261 | } else if y1d >= 0 && y4d >= 0 { |
261 | 262 | break |
262 | 263 | } |
263 | 264 | if (0 > y1d || 0 > y4d) && |
264 | - (y1d > float32(-scrrect[3]) || y4d > float32(-scrrect[3])) { | |
265 | + (y1d > float32(-sys.scrrect[3]) || y4d > float32(-sys.scrrect[3])) { | |
265 | 266 | rmTileHSub(x1d, y1d, x2d, y2d, x3d, y3d, x4d, y4d, x3d-x4d, x2d-x1d, |
266 | 267 | (x3d-x4d)/float32(w), (x2d-x1d)/float32(w), tl, |
267 | 268 | rcx, r, g, b, a, pers) |
@@ -272,14 +273,14 @@ func rmTileSub(w, h uint16, x, y float32, tl *[4]int32, | ||
272 | 273 | n := (*tl)[3] |
273 | 274 | for { |
274 | 275 | if ys*(float32(h)+float32((*tl)[1])) > 0 { |
275 | - if y1 <= float32(-scrrect[3]) && y4 <= float32(-scrrect[3]) { | |
276 | + if y1 <= float32(-sys.scrrect[3]) && y4 <= float32(-sys.scrrect[3]) { | |
276 | 277 | break |
277 | 278 | } |
278 | 279 | } else if y1 >= 0 && y4 >= 0 { |
279 | 280 | break |
280 | 281 | } |
281 | 282 | if (0 > y1 || 0 > y4) && |
282 | - (y1 > float32(-scrrect[3]) || y4 > float32(-scrrect[3])) { | |
283 | + (y1 > float32(-sys.scrrect[3]) || y4 > float32(-sys.scrrect[3])) { | |
283 | 284 | rmTileHSub(x1, y1, x2, y2, x3, y3, x4, y4, x3-x4, x2-x1, |
284 | 285 | (x3-x4)/float32(w), (x2-x1)/float32(w), tl, rcx, r, g, b, a, pers) |
285 | 286 | } |
@@ -306,10 +307,10 @@ func rmMainSub(a int32, size [2]uint16, x, y float32, tl *[4]int32, | ||
306 | 307 | gl.MatrixMode(gl.PROJECTION) |
307 | 308 | gl.PushMatrix() |
308 | 309 | gl.LoadIdentity() |
309 | - gl.Ortho(0, float64(scrrect[2]), 0, float64(scrrect[3]), -1, 1) | |
310 | + gl.Ortho(0, float64(sys.scrrect[2]), 0, float64(sys.scrrect[3]), -1, 1) | |
310 | 311 | gl.MatrixMode(gl.MODELVIEW) |
311 | 312 | gl.PushMatrix() |
312 | - gl.Translated(0, float64(scrrect[3]), 0) | |
313 | + gl.Translated(0, float64(sys.scrrect[3]), 0) | |
313 | 314 | switch { |
314 | 315 | case trans == -1: |
315 | 316 | gl.Uniform1fARB(a, 1) |
@@ -382,7 +383,7 @@ func rmInitSub(size [2]uint16, x, y *float32, tile *[4]int32, xts float32, | ||
382 | 383 | gl.Enable(gl.BLEND) |
383 | 384 | gl.Enable(gl.TEXTURE_2D) |
384 | 385 | gl.Enable(gl.SCISSOR_TEST) |
385 | - gl.Scissor((*window)[0], scrrect[3]-((*window)[1]+(*window)[3]), | |
386 | + gl.Scissor((*window)[0], sys.scrrect[3]-((*window)[1]+(*window)[3]), | |
386 | 387 | (*window)[2], (*window)[3]) |
387 | 388 | return |
388 | 389 | } |
@@ -66,11 +66,11 @@ func scriptCommonInit(l *lua.State) { | ||
66 | 66 | return 0 |
67 | 67 | }) |
68 | 68 | luaRegister(l, "playBGM", func(l *lua.State) int { |
69 | - bgm.Open(strArg(l, 1)) | |
69 | + sys.bgm.Open(strArg(l, 1)) | |
70 | 70 | return 0 |
71 | 71 | }) |
72 | 72 | luaRegister(l, "setRoundTime", func(l *lua.State) int { |
73 | - roundTime = int32(numArg(l, 1)) | |
73 | + sys.roundTime = int32(numArg(l, 1)) | |
74 | 74 | return 0 |
75 | 75 | }) |
76 | 76 | } |
@@ -229,9 +229,17 @@ func systemScriptInit(l *lua.State) { | ||
229 | 229 | a.Draw() |
230 | 230 | return 0 |
231 | 231 | }) |
232 | + luaRegister(l, "addChar", func(l *lua.State) int { | |
233 | + sys.sel.AddCahr(strArg(l, 1)) | |
234 | + return 0 | |
235 | + }) | |
236 | + luaRegister(l, "addStage", func(l *lua.State) int { | |
237 | + lua.Errorf(l, strArg(l, 1)) | |
238 | + return 0 | |
239 | + }) | |
232 | 240 | luaRegister(l, "refresh", func(*lua.State) int { |
233 | - await(60) | |
234 | - if gameEnd { | |
241 | + sys.await(60) | |
242 | + if sys.gameEnd { | |
235 | 243 | lua.Errorf(l, "<game end>") |
236 | 244 | } |
237 | 245 | return 0 |
@@ -241,7 +249,7 @@ func systemScriptInit(l *lua.State) { | ||
241 | 249 | if err != nil { |
242 | 250 | lua.Errorf(l, err.Error()) |
243 | 251 | } |
244 | - lifebar = *lb | |
252 | + sys.lifebar = *lb | |
245 | 253 | return 0 |
246 | 254 | }) |
247 | 255 | luaRegister(l, "loadDebugFont", func(l *lua.State) int { |
@@ -249,39 +257,39 @@ func systemScriptInit(l *lua.State) { | ||
249 | 257 | if err != nil { |
250 | 258 | lua.Errorf(l, err.Error()) |
251 | 259 | } |
252 | - debugFont = f | |
260 | + sys.debugFont = f | |
253 | 261 | return 0 |
254 | 262 | }) |
255 | 263 | luaRegister(l, "setDebugScript", func(l *lua.State) int { |
256 | - debugScript = strArg(l, 1) | |
264 | + sys.debugScript = strArg(l, 1) | |
257 | 265 | return 0 |
258 | 266 | }) |
259 | 267 | luaRegister(l, "setLifeMul", func(l *lua.State) int { |
260 | - lifeMul = float32(numArg(l, 1)) | |
268 | + sys.lifeMul = float32(numArg(l, 1)) | |
261 | 269 | return 0 |
262 | 270 | }) |
263 | 271 | luaRegister(l, "setTeam1VS2Life", func(l *lua.State) int { |
264 | - team1VS2Life = float32(numArg(l, 1)) | |
272 | + sys.team1VS2Life = float32(numArg(l, 1)) | |
265 | 273 | return 0 |
266 | 274 | }) |
267 | 275 | luaRegister(l, "setTurnsRecoveryRate", func(l *lua.State) int { |
268 | - turnsRecoveryRate = float32(numArg(l, 1)) | |
276 | + sys.turnsRecoveryRate = float32(numArg(l, 1)) | |
269 | 277 | return 0 |
270 | 278 | }) |
271 | 279 | luaRegister(l, "setZoom", func(l *lua.State) int { |
272 | - zoomEnable = boolArg(l, 1) | |
280 | + sys.zoomEnable = boolArg(l, 1) | |
273 | 281 | return 0 |
274 | 282 | }) |
275 | 283 | luaRegister(l, "setZoomMin", func(l *lua.State) int { |
276 | - zoomMin = float32(numArg(l, 1)) | |
284 | + sys.zoomMin = float32(numArg(l, 1)) | |
277 | 285 | return 0 |
278 | 286 | }) |
279 | 287 | luaRegister(l, "setZoomMax", func(l *lua.State) int { |
280 | - zoomMax = float32(numArg(l, 1)) | |
288 | + sys.zoomMax = float32(numArg(l, 1)) | |
281 | 289 | return 0 |
282 | 290 | }) |
283 | 291 | luaRegister(l, "setZoomSpeed", func(l *lua.State) int { |
284 | - zoomSpeed = float32(numArg(l, 1)) | |
292 | + sys.zoomSpeed = float32(numArg(l, 1)) | |
285 | 293 | return 0 |
286 | 294 | }) |
287 | 295 | } |
@@ -16,29 +16,23 @@ const ( | ||
16 | 16 | audioFrequency = 48000 |
17 | 17 | ) |
18 | 18 | |
19 | -var mixer = newMixer() | |
20 | -var bgm = newVorbis() | |
21 | -var audioContext *openal.Context | |
22 | -var nullSndBuf [audioOutLen * 2]int16 | |
23 | -var sounds = newSounds() | |
24 | - | |
25 | -func audioOpen() { | |
26 | - if audioContext == nil { | |
19 | +func (s *System) audioOpen() { | |
20 | + if s.audioContext == nil { | |
27 | 21 | device := openal.OpenDevice("") |
28 | 22 | if device == nil { |
29 | 23 | chk(openal.Err()) |
30 | 24 | } |
31 | - audioContext = device.CreateContext() | |
25 | + s.audioContext = device.CreateContext() | |
32 | 26 | chk(device.Err()) |
33 | - audioContext.Activate() | |
34 | - go soundWrite() | |
27 | + s.audioContext.Activate() | |
28 | + go s.soundWrite() | |
35 | 29 | } |
36 | 30 | } |
37 | -func soundWrite() { | |
31 | +func (s *System) soundWrite() { | |
38 | 32 | src := NewAudioSource() |
39 | 33 | bgmSrc := NewAudioSource() |
40 | 34 | processed := false |
41 | - for !gameEnd { | |
35 | + for !s.gameEnd { | |
42 | 36 | if src.Src.State() != openal.Playing { |
43 | 37 | src.Src.Play() |
44 | 38 | } |
@@ -52,9 +46,9 @@ func soundWrite() { | ||
52 | 46 | if src.Src.BuffersProcessed() > 0 { |
53 | 47 | var out []int16 |
54 | 48 | select { |
55 | - case out = <-mixer.out: | |
49 | + case out = <-s.mixer.out: | |
56 | 50 | default: |
57 | - out = nullSndBuf[:] | |
51 | + out = s.nullSndBuf[:] | |
58 | 52 | } |
59 | 53 | buf := src.Src.UnqueueBuffer() |
60 | 54 | buf.SetDataInt16(openal.FormatStereo16, out, audioFrequency) |
@@ -63,7 +57,7 @@ func soundWrite() { | ||
63 | 57 | processed = true |
64 | 58 | } |
65 | 59 | if bgmSrc.Src.BuffersProcessed() > 0 { |
66 | - out := bgm.read() | |
60 | + out := s.bgm.read() | |
67 | 61 | buf := bgmSrc.Src.UnqueueBuffer() |
68 | 62 | buf.SetDataInt16(openal.FormatStereo16, out, audioFrequency) |
69 | 63 | out = nil |
@@ -75,34 +69,14 @@ func soundWrite() { | ||
75 | 69 | bgmSrc.Delete() |
76 | 70 | src.Delete() |
77 | 71 | openal.NullContext.Activate() |
78 | - device := audioContext.GetDevice() | |
79 | - audioContext.Destroy() | |
80 | - audioContext = nil | |
72 | + device := s.audioContext.GetDevice() | |
73 | + s.audioContext.Destroy() | |
74 | + s.audioContext = nil | |
81 | 75 | device.CloseDevice() |
82 | 76 | } |
83 | -func newSounds() (s []*Sound) { | |
84 | - s = make([]*Sound, 16) | |
85 | - for i := range s { | |
86 | - s[i] = &Sound{volume: 256, freqmul: 1} | |
87 | - } | |
88 | - return | |
89 | -} | |
90 | -func GetChannel() *Sound { | |
91 | - for i := range sounds { | |
92 | - if sounds[i].sound == nil { | |
93 | - return sounds[i] | |
94 | - } | |
95 | - } | |
96 | - return nil | |
97 | -} | |
98 | -func mixSounds() { | |
99 | - for i := range sounds { | |
100 | - sounds[i].Mix() | |
101 | - } | |
102 | -} | |
103 | -func playSound() { | |
104 | - if mixer.write() { | |
105 | - mixSounds() | |
77 | +func (s *System) playSound() { | |
78 | + if s.mixer.write() { | |
79 | + s.sounds.mixSounds() | |
106 | 80 | } |
107 | 81 | } |
108 | 82 |
@@ -114,7 +88,7 @@ type AudioSource struct { | ||
114 | 88 | func NewAudioSource() (s *AudioSource) { |
115 | 89 | s = &AudioSource{Src: openal.NewSource(), bufs: openal.NewBuffers(2)} |
116 | 90 | for i := range s.bufs { |
117 | - s.bufs[i].SetDataInt16(openal.FormatStereo16, nullSndBuf[:], | |
91 | + s.bufs[i].SetDataInt16(openal.FormatStereo16, sys.nullSndBuf[:], | |
118 | 92 | audioFrequency) |
119 | 93 | } |
120 | 94 | s.Src.QueueBuffers(s.bufs) |
@@ -387,7 +361,7 @@ func (v *Vorbis) read() []int16 { | ||
387 | 361 | v.buf = append(v.buf, v.samToAudioOut(sam)...) |
388 | 362 | } |
389 | 363 | } |
390 | - return nullSndBuf[:] | |
364 | + return sys.nullSndBuf[:] | |
391 | 365 | } |
392 | 366 | |
393 | 367 | type Wave struct { |
@@ -554,7 +528,7 @@ func (s *Snd) Get(group int32, number int32) *Wave { | ||
554 | 528 | return s.table[[2]int32{group, number}] |
555 | 529 | } |
556 | 530 | func (s *Snd) Play(g int32, n int32) bool { |
557 | - c := GetChannel() | |
531 | + c := sys.sounds.GetChannel() | |
558 | 532 | if c == nil { |
559 | 533 | return false |
560 | 534 | } |
@@ -575,7 +549,7 @@ func (s *Sound) Mix() { | ||
575 | 549 | if s.sound == nil { |
576 | 550 | return |
577 | 551 | } |
578 | - s.fidx = mixer.Mix(s.sound.Wav, s.fidx, | |
552 | + s.fidx = sys.mixer.Mix(s.sound.Wav, s.fidx, | |
579 | 553 | int(s.sound.BytesPerSample), int(s.sound.Channels), |
580 | 554 | float64(s.sound.SamplesPerSec)*float64(s.freqmul), s.loop, |
581 | 555 | float32(s.volume)/256) |
@@ -585,3 +559,26 @@ func (s *Sound) Mix() { | ||
585 | 559 | s.fidx = 0 |
586 | 560 | } |
587 | 561 | } |
562 | + | |
563 | +type Sounds []*Sound | |
564 | + | |
565 | +func newSounds() (s Sounds) { | |
566 | + s = Sounds(make([]*Sound, 16)) | |
567 | + for i := range s { | |
568 | + s[i] = &Sound{volume: 256, freqmul: 1} | |
569 | + } | |
570 | + return | |
571 | +} | |
572 | +func (s Sounds) GetChannel() *Sound { | |
573 | + for i := range s { | |
574 | + if s[i].sound == nil { | |
575 | + return s[i] | |
576 | + } | |
577 | + } | |
578 | + return nil | |
579 | +} | |
580 | +func (s Sounds) mixSounds() { | |
581 | + for i := range s { | |
582 | + s[i].Mix() | |
583 | + } | |
584 | +} |
@@ -0,0 +1,188 @@ | ||
1 | +package main | |
2 | + | |
3 | +import ( | |
4 | + "github.com/Shopify/go-lua" | |
5 | + "github.com/go-gl/gl/v2.1/gl" | |
6 | + "github.com/go-gl/glfw/v3.2/glfw" | |
7 | + "github.com/timshannon/go-openal/openal" | |
8 | + "strings" | |
9 | + "time" | |
10 | +) | |
11 | + | |
12 | +var sys = System{ | |
13 | + randseed: int32(time.Now().UnixNano()), | |
14 | + scrrect: [4]int32{0, 0, 320, 240}, | |
15 | + gameWidth: 320, gameHeight: 240, | |
16 | + widthScale: 1, heightScale: 1, | |
17 | + brightness: 256, | |
18 | + introTime: 0, roundTime: -1, | |
19 | + lifeMul: 1, team1VS2Life: 1, | |
20 | + turnsRecoveryRate: 1.0 / 300, | |
21 | + zoomMin: 1, zoomMax: 1, zoomSpeed: 1, | |
22 | + lifebarFontScale: 1, | |
23 | + mixer: *newMixer(), | |
24 | + bgm: *newVorbis(), | |
25 | + sounds: newSounds(), | |
26 | + allPalFX: *NewPalFX()} | |
27 | + | |
28 | +type System struct { | |
29 | + randseed int32 | |
30 | + scrrect [4]int32 | |
31 | + gameWidth, gameHeight int32 | |
32 | + widthScale, heightScale float32 | |
33 | + window *glfw.Window | |
34 | + gameEnd, frameSkip bool | |
35 | + redrawWait struct{ nextTime, lastDraw time.Time } | |
36 | + brightness int32 | |
37 | + introTime, roundTime int32 | |
38 | + lifeMul, team1VS2Life float32 | |
39 | + turnsRecoveryRate float32 | |
40 | + zoomEnable bool | |
41 | + zoomMin, zoomMax, zoomSpeed float32 | |
42 | + lifebarFontScale float32 | |
43 | + debugFont *Fnt | |
44 | + debugScript string | |
45 | + mixer Mixer | |
46 | + bgm Vorbis | |
47 | + audioContext *openal.Context | |
48 | + nullSndBuf [audioOutLen * 2]int16 | |
49 | + sounds Sounds | |
50 | + allPalFX PalFX | |
51 | + lifebar Lifebar | |
52 | + sel Select | |
53 | +} | |
54 | + | |
55 | +func (s *System) init(w, h int32) *lua.State { | |
56 | + chk(glfw.Init()) | |
57 | + defer glfw.Terminate() | |
58 | + glfw.WindowHint(glfw.Resizable, glfw.False) | |
59 | + glfw.WindowHint(glfw.ContextVersionMajor, 2) | |
60 | + glfw.WindowHint(glfw.ContextVersionMinor, 1) | |
61 | + s.setWindowSize(w, h) | |
62 | + var err error | |
63 | + s.window, err = glfw.CreateWindow(int(s.scrrect[2]), int(s.scrrect[3]), | |
64 | + "Ikemen GO", nil, nil) | |
65 | + chk(err) | |
66 | + s.window.MakeContextCurrent() | |
67 | + s.window.SetKeyCallback(keyCallback) | |
68 | + glfw.SwapInterval(1) | |
69 | + chk(gl.Init()) | |
70 | + keyConfig = append(keyConfig, &KeyConfig{-1, | |
71 | + int(glfw.KeyUp), int(glfw.KeyDown), int(glfw.KeyLeft), int(glfw.KeyRight), | |
72 | + int(glfw.KeyZ), int(glfw.KeyX), int(glfw.KeyC), | |
73 | + int(glfw.KeyA), int(glfw.KeyS), int(glfw.KeyD), int(glfw.KeyEnter)}) | |
74 | + RenderInit() | |
75 | + s.audioOpen() | |
76 | + l := lua.NewState() | |
77 | + lua.OpenLibraries(l) | |
78 | + systemScriptInit(l) | |
79 | + return l | |
80 | +} | |
81 | +func (s *System) setWindowSize(w, h int32) { | |
82 | + s.scrrect[2], s.scrrect[3] = w, h | |
83 | + if s.scrrect[2]*3 > s.scrrect[3]*4 { | |
84 | + s.gameWidth, s.gameHeight = s.scrrect[2]*3*320/(s.scrrect[3]*4), 240 | |
85 | + } else { | |
86 | + s.gameWidth, s.gameHeight = 320, s.scrrect[3]*4*240/(s.scrrect[2]*3) | |
87 | + } | |
88 | + s.widthScale = float32(s.scrrect[2]) / float32(s.gameWidth) | |
89 | + s.heightScale = float32(s.scrrect[3]) / float32(s.gameHeight) | |
90 | +} | |
91 | +func (s *System) await(fps int) { | |
92 | + s.playSound() | |
93 | + if !s.frameSkip { | |
94 | + s.window.SwapBuffers() | |
95 | + } | |
96 | + now := time.Now() | |
97 | + diff := s.redrawWait.nextTime.Sub(now) | |
98 | + wait := time.Second / time.Duration(fps) | |
99 | + s.redrawWait.nextTime = s.redrawWait.nextTime.Add(wait) | |
100 | + switch { | |
101 | + case diff >= 0 && diff < wait+2*time.Millisecond: | |
102 | + time.Sleep(diff) | |
103 | + fallthrough | |
104 | + case now.Sub(s.redrawWait.lastDraw) > 250*time.Millisecond: | |
105 | + fallthrough | |
106 | + case diff >= -17*time.Millisecond: | |
107 | + s.redrawWait.lastDraw = now | |
108 | + s.frameSkip = false | |
109 | + default: | |
110 | + if diff < -150*time.Millisecond { | |
111 | + s.redrawWait.nextTime = now.Add(wait) | |
112 | + } | |
113 | + s.frameSkip = true | |
114 | + } | |
115 | + glfw.PollEvents() | |
116 | + s.gameEnd = s.window.ShouldClose() | |
117 | + if !s.frameSkip { | |
118 | + gl.Viewport(0, 0, int32(s.scrrect[2]), int32(s.scrrect[3])) | |
119 | + gl.Clear(gl.COLOR_BUFFER_BIT) | |
120 | + } | |
121 | +} | |
122 | + | |
123 | +type SelectChar struct { | |
124 | + def, name string | |
125 | + sportrait, lportrait *Sprite | |
126 | +} | |
127 | +type Select struct{ charlist []SelectChar } | |
128 | + | |
129 | +func (s *Select) AddCahr(def string) { | |
130 | + s.charlist = append(s.charlist, SelectChar{}) | |
131 | + sc := &s.charlist[len(s.charlist)-1] | |
132 | + def = strings.Replace(strings.TrimSpace(strings.Split(",", def)[0]), | |
133 | + "\\", "/", -1) | |
134 | + if strings.ToLower(def) == "randomselect" { | |
135 | + sc.def, sc.name = "randomselect", "Random" | |
136 | + return | |
137 | + } | |
138 | + idx := strings.Index(def, "/") | |
139 | + if len(def) >= 4 && strings.ToLower(def[len(def)-4:]) == ".def" { | |
140 | + if idx < 0 { | |
141 | + return | |
142 | + } | |
143 | + } else if idx < 0 { | |
144 | + def += "/" + def + ".def" | |
145 | + } else { | |
146 | + def += ".def" | |
147 | + } | |
148 | + if def[0] != '/' || idx > 0 && strings.Index(def[:idx], ":") < 0 { | |
149 | + def = "chars/" + def | |
150 | + } | |
151 | + if def = FileExist(def); len(def) == 0 { | |
152 | + return | |
153 | + } | |
154 | + str, err := LoadText(def) | |
155 | + if err != nil { | |
156 | + return | |
157 | + } | |
158 | + lines, i, info, files := SplitAndTrim(str, "\n"), 0, true, true | |
159 | + sprite := "" | |
160 | + for i < len(lines) { | |
161 | + is, name, _ := ReadIniSection(lines, &i) | |
162 | + switch name { | |
163 | + case "info": | |
164 | + if info { | |
165 | + info = false | |
166 | + sc.name = is["displayname"] | |
167 | + if len(sc.name) == 0 { | |
168 | + sc.name = is["name"] | |
169 | + } | |
170 | + } | |
171 | + case "files": | |
172 | + if files { | |
173 | + files = false | |
174 | + sprite = is["sprite"] | |
175 | + } | |
176 | + } | |
177 | + } | |
178 | + LoadFile(&sprite, def, func(file string) error { | |
179 | + var err error | |
180 | + sc.sportrait, err = LoadFromSff(file, 9000, 0) | |
181 | + return err | |
182 | + }) | |
183 | + LoadFile(&sprite, def, func(file string) error { | |
184 | + var err error | |
185 | + sc.lportrait, err = LoadFromSff(file, 9000, 1) | |
186 | + return err | |
187 | + }) | |
188 | +} |