• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

Go で書き直した Ikemen


Commit MetaInfo

Revisióne6dd36fdd0f712b5d1e3bfe0e611275af82034d5 (tree)
Tiempo2016-11-30 00:40:54
AutorSUEHIRO <supersuehiro@user...>
CommiterSUEHIRO

Log Message

グローバル変数をsysに移した

Cambiar Resumen

Diferencia incremental

--- a/run.sh
+++ b/run.sh
@@ -3,4 +3,4 @@ GOPATH=$PWD/go
33 export GOPATH
44 go fmt ./src/*.go
55 # godoc -src ./src .* > godoc.txt
6-go generate ./src/main.go && go run ./src/*.go
6+go run ./src/*.go
--- /dev/null
+++ b/script/select.lua
@@ -0,0 +1,8 @@
1+addChar([[
2+randomselect
3+kfmx
4+]])
5+
6+addStage([[
7+SpaceRainbowLowRes.def
8+]])
--- a/src/anim.go
+++ b/src/anim.go
@@ -166,7 +166,7 @@ func ReadAnimation(sff *Sff, lines []string, i *int) *Animation {
166166 af.Ex[0] = clsn1
167167 af.Ex[1] = clsn2
168168 }
169- a.frames = AppendAF(a.frames, *af)
169+ a.frames = append(a.frames, *af)
170170 def1, def2 = true, true
171171 case len(line) >= 9 && line[:9] == "loopstart":
172172 a.loopstart = int32(len(a.frames))
@@ -399,7 +399,7 @@ func (a *Animation) alpha() int32 {
399399 if sa == 1 && da == 255 {
400400 return -2
401401 }
402- sa = byte(int32(sa) * brightness >> 8)
402+ sa = byte(int32(sa) * sys.brightness >> 8)
403403 if sa < 5 && da == 255 {
404404 return 0
405405 }
@@ -483,15 +483,15 @@ func (a *Animation) Draw(window *[4]int32, x, y, xcs, ycs, xs, xbs, ys,
483483 y -= float32(int(y/tmp)) * tmp
484484 }
485485 }
486- rcx, rcy = rcx*widthScale, 0
486+ rcx, rcy = rcx*sys.widthScale, 0
487487 x, y = -x+xs*float32(a.spr.Offset[0]), -y+ys*float32(a.spr.Offset[1])
488488 } else {
489- rcx, rcy = (x+rcx)*widthScale, y*heightScale
489+ rcx, rcy = (x+rcx)*sys.widthScale, y*sys.heightScale
490490 x, y = AbsF(xs)*float32(a.spr.Offset[0]), AbsF(ys)*float32(a.spr.Offset[1])
491491 }
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,
495495 rcx, rcy, pfx)
496496 }
497497
@@ -547,7 +547,7 @@ type Anim struct {
547547 func NewAnim(sff *Sff, action string) *Anim {
548548 lines, i := SplitAndTrim(action, "\n"), 0
549549 a := &Anim{anim: ReadAnimation(sff, lines, &i),
550- window: scrrect, xscl: 1, yscl: 1}
550+ window: sys.scrrect, xscl: 1, yscl: 1}
551551 if len(a.anim.frames) == 0 {
552552 return nil
553553 }
@@ -573,18 +573,18 @@ func (a *Anim) SetScale(x, y float32) {
573573 a.xscl, a.yscl = x, y
574574 }
575575 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)
580580 }
581581 func (a *Anim) Update() {
582582 a.anim.Action()
583583 }
584584 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,
588588 0, 0, 0, nil, false)
589589 }
590590 }
--- a/src/common.go
+++ b/src/common.go
@@ -15,17 +15,15 @@ const (
1515 IErr = ^IMax
1616 )
1717
18-var randseed int32
19-
2018 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)
2523 }
26- return randseed
24+ return sys.randseed
2725 }
28-func Srand(s int32) { randseed = s }
26+func Srand(s int32) { sys.randseed = s }
2927 func Rand(min, max int32) int32 { return min + Random()/(IMax/(max-min+1)+1) }
3028 func RandI(x, y int32) int32 {
3129 if y < x {
@@ -176,29 +174,28 @@ func LoadText(filename string) (string, error) {
176174 }
177175 return AsciiToString(bytes), nil
178176 }
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)
199190 }
200- return true
201191 }
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
202199 *file = strings.Replace(*file, "\\", "/", -1)
203200 defdir := filepath.Dir(strings.Replace(deffile, "\\", "/", -1))
204201 if defdir == "." {
@@ -208,11 +205,11 @@ func LoadFile(file *string, deffile string, load func(string) error) error {
208205 } else {
209206 fp = defdir + "/" + *file
210207 }
211- if isNotExist() {
208+ if fp = FileExist(fp); len(fp) == 0 {
212209 _else := false
213210 if defdir != "data" {
214211 fp = "data/" + *file
215- if isNotExist() {
212+ if fp = FileExist(fp); len(fp) == 0 {
216213 _else = true
217214 }
218215 } else {
@@ -221,7 +218,9 @@ func LoadFile(file *string, deffile string, load func(string) error) error {
221218 if _else {
222219 if defdir != "." {
223220 fp = *file
224- isNotExist()
221+ if fp = FileExist(fp); len(fp) == 0 {
222+ fp = *file
223+ }
225224 }
226225 }
227226 }
@@ -373,32 +372,33 @@ func ReadLayout(pre string, is IniSection) *Layout {
373372 is.ReadF32(pre+"scale", &l.scale[0], &l.scale[1])
374373 return l
375374 }
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) {
377377 if l.layerno == ln {
378378 if l.facing < 0 {
379- x += lifebarFontScale
379+ x += sys.lifebarFontScale
380380 }
381381 if l.vfacing < 0 {
382- y += lifebarFontScale
382+ y += sys.lifebarFontScale
383383 }
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),
386386 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)
388388 }
389389 }
390390 func (l *Layout) DrawText(x, y, scl float32, ln int16,
391391 text string, f *Fnt, b, a int32) {
392392 if l.layerno == ln {
393393 if l.facing < 0 {
394- x += lifebarFontScale
394+ x += sys.lifebarFontScale
395395 }
396396 if l.vfacing < 0 {
397- y += lifebarFontScale
397+ y += sys.lifebarFontScale
398398 }
399399 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)
402402 }
403403 }
404404
@@ -435,7 +435,7 @@ func (al *AnimLayout) Action() {
435435 al.anim.Action()
436436 }
437437 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)
439439 }
440440
441441 type AnimTextSnd struct {
--- a/src/font.go
+++ b/src/font.go
@@ -238,15 +238,16 @@ func (f *Fnt) drawChar(x, y, xscl, yscl float32, bank int32, c rune,
238238 if spr == nil {
239239 return 0
240240 }
241- spr.glDraw(pal, 0, -x*widthScale, -y*heightScale, &notiling,
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, &notiling,
242+ xscl*sys.widthScale, xscl*sys.widthScale, yscl*sys.heightScale, 0, 0,
243+ sys.brightness*255>>8|1<<9, &sys.scrrect, 0, 0, nil)
244244 return float32(spr.Size[0]) * xscl
245245 }
246246 func (f *Fnt) DrawText(txt string, x, y, xscl, yscl float32, bank int32,
247247 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)
250251 if align == 0 {
251252 x -= float32(f.TextWidth(txt)) * xscl * 0.5
252253 } else if align < 0 {
@@ -256,8 +257,8 @@ func (f *Fnt) DrawText(txt string, x, y, xscl, yscl float32, bank int32,
256257 bank = 0
257258 }
258259 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)
261262 }
262263 for _, c := range txt {
263264 x += f.drawChar(x, y, xscl, yscl, bank, c, pal) +
@@ -276,7 +277,7 @@ func NewTextSprite() *TextSprite {
276277 return &TextSprite{align: 1, xscl: 1, yscl: 1}
277278 }
278279 func (ts *TextSprite) Draw() {
279- if !frameSkip && ts.fnt != nil {
280+ if !sys.frameSkip && ts.fnt != nil {
280281 ts.fnt.DrawText(ts.text, ts.x, ts.y, ts.xscl, ts.yscl, ts.bank, ts.align)
281282 }
282283 }
--- a/src/gen/gen.go
+++ /dev/null
@@ -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-}
--- a/src/generated.go
+++ /dev/null
@@ -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-}
--- a/src/image.go
+++ b/src/image.go
@@ -13,8 +13,6 @@ import (
1313 "unsafe"
1414 )
1515
16-var allPalFX = NewPalFX()
17-
1816 type Texture uint32
1917
2018 func textureFinalizer(t *Texture) {
@@ -65,17 +63,17 @@ func (pl *PaletteList) SetSource(i int, p []uint32) {
6563 pl.paletteMap[i] = i
6664 } else {
6765 for i > len(pl.paletteMap) {
68- pl.paletteMap = AppendI(pl.paletteMap, len(pl.paletteMap))
66+ pl.paletteMap = append(pl.paletteMap, len(pl.paletteMap))
6967 }
70- pl.paletteMap = AppendI(pl.paletteMap, i)
68+ pl.paletteMap = append(pl.paletteMap, i)
7169 }
7270 if i < len(pl.palettes) {
7371 pl.palettes[i] = p
7472 } else {
7573 for i > len(pl.palettes) {
76- pl.palettes = AppendPal(pl.palettes, nil)
74+ pl.palettes = append(pl.palettes, nil)
7775 }
78- pl.palettes = AppendPal(pl.palettes, p)
76+ pl.palettes = append(pl.palettes, p)
7977 }
8078 }
8179 func (pl *PaletteList) NewPal() (i int, p []uint32) {
@@ -234,10 +232,10 @@ func LoadFromSff(filename string, g int16, n int16) (*Sprite, error) {
234232 }
235233 var dummy *Sprite
236234 var newSubHeaderOffset []uint32
237- newSubHeaderOffset = AppendU32(newSubHeaderOffset, shofs)
235+ newSubHeaderOffset = append(newSubHeaderOffset, shofs)
238236 i := 0
239237 for ; i < int(h.NumberOfSprites); i++ {
240- newSubHeaderOffset = AppendU32(newSubHeaderOffset, shofs)
238+ newSubHeaderOffset = append(newSubHeaderOffset, shofs)
241239 f.Seek(int64(shofs), 0)
242240 if err := foo(); err != nil {
243241 return nil, err
@@ -842,17 +840,17 @@ func (s *Sprite) glDraw(pal []uint32, mask int32, x, y float32, tile *[4]int32,
842840 }
843841 }
844842 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])
847845 if xscale < 0 {
848846 x *= -1
849847 }
850848 if yscale < 0 {
851849 y *= -1
852850 }
853- s.glDraw(pal, 0, -x*widthScale, -y*heightScale, &notiling,
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, &notiling,
852+ xscale*sys.widthScale, xscale*sys.widthScale, yscale*sys.heightScale, 0, 0,
853+ sys.brightness*255>>8|1<<9, &sys.scrrect, 0, 0, nil)
856854 }
857855
858856 type Sff struct {
--- a/src/input.go
+++ b/src/input.go
@@ -426,7 +426,7 @@ type AiInput struct {
426426 }
427427
428428 func (__ *AiInput) Update() {
429- if introTime != 0 {
429+ if sys.introTime != 0 {
430430 __.dt, __.at, __.bt, __.ct = 0, 0, 0, 0
431431 __.xt, __.yt, __.zt, __.st = 0, 0, 0, 0
432432 return
--- a/src/lifebar.go
+++ b/src/lifebar.go
@@ -5,8 +5,6 @@ import (
55 "strings"
66 )
77
8-var lifebar Lifebar
9-
108 type WinType int32
119
1210 const (
--- a/src/main.go
+++ b/src/main.go
@@ -1,104 +1,20 @@
11 package main
22
3-//go:generate go run ./gen/gen.go
43 import (
54 "github.com/Shopify/go-lua"
6- "github.com/go-gl/gl/v2.1/gl"
7- "github.com/go-gl/glfw/v3.2/glfw"
85 "runtime"
9- "time"
106 )
117
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-
288 func init() {
299 runtime.LockOSThread()
3010 }
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-}
4111 func chk(err error) {
4212 if err != nil {
4313 panic(err)
4414 }
4515 }
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-}
7716 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)
10218 if err := lua.DoFile(l, "script/main.lua"); err != nil {
10319 switch err.(type) {
10420 case lua.RuntimeError:
--- a/src/render.go
+++ b/src/render.go
@@ -145,7 +145,7 @@ func drawQuads(x1, y1, x2, y2, x3, y3, x4, y4, r, g, b, a, pers float32) {
145145 gl.Vertex2f(x4, y4)
146146 if pers != 1 {
147147 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)))
149149 for i := 1; i < n; i++ {
150150 gl.TexCoord2f(float32(i)/float32(n), 1)
151151 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,
177177 x4d = x3d - xtw
178178 x1d = x2d - xbw
179179 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]) {
182183 break
183184 }
184185 } else if x1d <= 0 && x2d <= 0 && x3d <= 0 && x4d <= 0 {
185186 break
186187 }
187188 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])) ||
189190 (0 < x3d || 0 < x4d) &&
190- (x3d < float32(scrrect[2]) || x4d < float32(scrrect[2])) {
191+ (x3d < float32(sys.scrrect[2]) || x4d < float32(sys.scrrect[2])) {
191192 drawQuads(x1d, y1, x2d, y2, x3d, y3, x4d, y4, r, g, b, a, pers)
192193 }
193194 }
@@ -196,17 +197,17 @@ func rmTileHSub(x1, y1, x2, y2, x3, y3, x4, y4, xtw, xbw, xts, xbs float32,
196197 n := (*tl)[2]
197198 for {
198199 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]) {
201202 break
202203 }
203204 } else if x1 <= 0 && x2 <= 0 && x3 <= 0 && x4 <= 0 {
204205 break
205206 }
206207 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])) ||
208209 (0 < x3 || 0 < x4) &&
209- (x3 < float32(scrrect[2]) || x4 < float32(scrrect[2])) {
210+ (x3 < float32(sys.scrrect[2]) || x4 < float32(sys.scrrect[2])) {
210211 drawQuads(x1, y1, x2, y2, x3, y3, x4, y4, r, g, b, a, pers)
211212 }
212213 if (*tl)[2] != 1 && n != 0 {
@@ -254,14 +255,14 @@ func rmTileSub(w, h uint16, x, y float32, tl *[4]int32,
254255 }
255256 y4d = y3d
256257 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]) {
258259 break
259260 }
260261 } else if y1d >= 0 && y4d >= 0 {
261262 break
262263 }
263264 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])) {
265266 rmTileHSub(x1d, y1d, x2d, y2d, x3d, y3d, x4d, y4d, x3d-x4d, x2d-x1d,
266267 (x3d-x4d)/float32(w), (x2d-x1d)/float32(w), tl,
267268 rcx, r, g, b, a, pers)
@@ -272,14 +273,14 @@ func rmTileSub(w, h uint16, x, y float32, tl *[4]int32,
272273 n := (*tl)[3]
273274 for {
274275 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]) {
276277 break
277278 }
278279 } else if y1 >= 0 && y4 >= 0 {
279280 break
280281 }
281282 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])) {
283284 rmTileHSub(x1, y1, x2, y2, x3, y3, x4, y4, x3-x4, x2-x1,
284285 (x3-x4)/float32(w), (x2-x1)/float32(w), tl, rcx, r, g, b, a, pers)
285286 }
@@ -306,10 +307,10 @@ func rmMainSub(a int32, size [2]uint16, x, y float32, tl *[4]int32,
306307 gl.MatrixMode(gl.PROJECTION)
307308 gl.PushMatrix()
308309 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)
310311 gl.MatrixMode(gl.MODELVIEW)
311312 gl.PushMatrix()
312- gl.Translated(0, float64(scrrect[3]), 0)
313+ gl.Translated(0, float64(sys.scrrect[3]), 0)
313314 switch {
314315 case trans == -1:
315316 gl.Uniform1fARB(a, 1)
@@ -382,7 +383,7 @@ func rmInitSub(size [2]uint16, x, y *float32, tile *[4]int32, xts float32,
382383 gl.Enable(gl.BLEND)
383384 gl.Enable(gl.TEXTURE_2D)
384385 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]),
386387 (*window)[2], (*window)[3])
387388 return
388389 }
--- a/src/script.go
+++ b/src/script.go
@@ -66,11 +66,11 @@ func scriptCommonInit(l *lua.State) {
6666 return 0
6767 })
6868 luaRegister(l, "playBGM", func(l *lua.State) int {
69- bgm.Open(strArg(l, 1))
69+ sys.bgm.Open(strArg(l, 1))
7070 return 0
7171 })
7272 luaRegister(l, "setRoundTime", func(l *lua.State) int {
73- roundTime = int32(numArg(l, 1))
73+ sys.roundTime = int32(numArg(l, 1))
7474 return 0
7575 })
7676 }
@@ -229,9 +229,17 @@ func systemScriptInit(l *lua.State) {
229229 a.Draw()
230230 return 0
231231 })
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+ })
232240 luaRegister(l, "refresh", func(*lua.State) int {
233- await(60)
234- if gameEnd {
241+ sys.await(60)
242+ if sys.gameEnd {
235243 lua.Errorf(l, "<game end>")
236244 }
237245 return 0
@@ -241,7 +249,7 @@ func systemScriptInit(l *lua.State) {
241249 if err != nil {
242250 lua.Errorf(l, err.Error())
243251 }
244- lifebar = *lb
252+ sys.lifebar = *lb
245253 return 0
246254 })
247255 luaRegister(l, "loadDebugFont", func(l *lua.State) int {
@@ -249,39 +257,39 @@ func systemScriptInit(l *lua.State) {
249257 if err != nil {
250258 lua.Errorf(l, err.Error())
251259 }
252- debugFont = f
260+ sys.debugFont = f
253261 return 0
254262 })
255263 luaRegister(l, "setDebugScript", func(l *lua.State) int {
256- debugScript = strArg(l, 1)
264+ sys.debugScript = strArg(l, 1)
257265 return 0
258266 })
259267 luaRegister(l, "setLifeMul", func(l *lua.State) int {
260- lifeMul = float32(numArg(l, 1))
268+ sys.lifeMul = float32(numArg(l, 1))
261269 return 0
262270 })
263271 luaRegister(l, "setTeam1VS2Life", func(l *lua.State) int {
264- team1VS2Life = float32(numArg(l, 1))
272+ sys.team1VS2Life = float32(numArg(l, 1))
265273 return 0
266274 })
267275 luaRegister(l, "setTurnsRecoveryRate", func(l *lua.State) int {
268- turnsRecoveryRate = float32(numArg(l, 1))
276+ sys.turnsRecoveryRate = float32(numArg(l, 1))
269277 return 0
270278 })
271279 luaRegister(l, "setZoom", func(l *lua.State) int {
272- zoomEnable = boolArg(l, 1)
280+ sys.zoomEnable = boolArg(l, 1)
273281 return 0
274282 })
275283 luaRegister(l, "setZoomMin", func(l *lua.State) int {
276- zoomMin = float32(numArg(l, 1))
284+ sys.zoomMin = float32(numArg(l, 1))
277285 return 0
278286 })
279287 luaRegister(l, "setZoomMax", func(l *lua.State) int {
280- zoomMax = float32(numArg(l, 1))
288+ sys.zoomMax = float32(numArg(l, 1))
281289 return 0
282290 })
283291 luaRegister(l, "setZoomSpeed", func(l *lua.State) int {
284- zoomSpeed = float32(numArg(l, 1))
292+ sys.zoomSpeed = float32(numArg(l, 1))
285293 return 0
286294 })
287295 }
--- a/src/sound.go
+++ b/src/sound.go
@@ -16,29 +16,23 @@ const (
1616 audioFrequency = 48000
1717 )
1818
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 {
2721 device := openal.OpenDevice("")
2822 if device == nil {
2923 chk(openal.Err())
3024 }
31- audioContext = device.CreateContext()
25+ s.audioContext = device.CreateContext()
3226 chk(device.Err())
33- audioContext.Activate()
34- go soundWrite()
27+ s.audioContext.Activate()
28+ go s.soundWrite()
3529 }
3630 }
37-func soundWrite() {
31+func (s *System) soundWrite() {
3832 src := NewAudioSource()
3933 bgmSrc := NewAudioSource()
4034 processed := false
41- for !gameEnd {
35+ for !s.gameEnd {
4236 if src.Src.State() != openal.Playing {
4337 src.Src.Play()
4438 }
@@ -52,9 +46,9 @@ func soundWrite() {
5246 if src.Src.BuffersProcessed() > 0 {
5347 var out []int16
5448 select {
55- case out = <-mixer.out:
49+ case out = <-s.mixer.out:
5650 default:
57- out = nullSndBuf[:]
51+ out = s.nullSndBuf[:]
5852 }
5953 buf := src.Src.UnqueueBuffer()
6054 buf.SetDataInt16(openal.FormatStereo16, out, audioFrequency)
@@ -63,7 +57,7 @@ func soundWrite() {
6357 processed = true
6458 }
6559 if bgmSrc.Src.BuffersProcessed() > 0 {
66- out := bgm.read()
60+ out := s.bgm.read()
6761 buf := bgmSrc.Src.UnqueueBuffer()
6862 buf.SetDataInt16(openal.FormatStereo16, out, audioFrequency)
6963 out = nil
@@ -75,34 +69,14 @@ func soundWrite() {
7569 bgmSrc.Delete()
7670 src.Delete()
7771 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
8175 device.CloseDevice()
8276 }
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()
10680 }
10781 }
10882
@@ -114,7 +88,7 @@ type AudioSource struct {
11488 func NewAudioSource() (s *AudioSource) {
11589 s = &AudioSource{Src: openal.NewSource(), bufs: openal.NewBuffers(2)}
11690 for i := range s.bufs {
117- s.bufs[i].SetDataInt16(openal.FormatStereo16, nullSndBuf[:],
91+ s.bufs[i].SetDataInt16(openal.FormatStereo16, sys.nullSndBuf[:],
11892 audioFrequency)
11993 }
12094 s.Src.QueueBuffers(s.bufs)
@@ -387,7 +361,7 @@ func (v *Vorbis) read() []int16 {
387361 v.buf = append(v.buf, v.samToAudioOut(sam)...)
388362 }
389363 }
390- return nullSndBuf[:]
364+ return sys.nullSndBuf[:]
391365 }
392366
393367 type Wave struct {
@@ -554,7 +528,7 @@ func (s *Snd) Get(group int32, number int32) *Wave {
554528 return s.table[[2]int32{group, number}]
555529 }
556530 func (s *Snd) Play(g int32, n int32) bool {
557- c := GetChannel()
531+ c := sys.sounds.GetChannel()
558532 if c == nil {
559533 return false
560534 }
@@ -575,7 +549,7 @@ func (s *Sound) Mix() {
575549 if s.sound == nil {
576550 return
577551 }
578- s.fidx = mixer.Mix(s.sound.Wav, s.fidx,
552+ s.fidx = sys.mixer.Mix(s.sound.Wav, s.fidx,
579553 int(s.sound.BytesPerSample), int(s.sound.Channels),
580554 float64(s.sound.SamplesPerSec)*float64(s.freqmul), s.loop,
581555 float32(s.volume)/256)
@@ -585,3 +559,26 @@ func (s *Sound) Mix() {
585559 s.fidx = 0
586560 }
587561 }
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+}
--- /dev/null
+++ b/src/system.go
@@ -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+}