Go で書き直した Ikemen
Révision | 2d26e0122745b6efaabe896669c06402ffb10080 (tree) |
---|---|
l'heure | 2016-11-21 00:17:55 |
Auteur | SUEHIRO <supersuehiro@user...> |
Commiter | SUEHIRO |
commandBuffer
@@ -460,7 +460,7 @@ func (a *Animation) Draw(window *[4]int32, x, y, xcs, ycs, xs, xbs, ys, | ||
460 | 460 | x, y = -x+xs*float32(a.spr.Offset[0]), -y+ys*float32(a.spr.Offset[1]) |
461 | 461 | } else { |
462 | 462 | rcx, rcy = (x+rcx)*widthScale, y*heightScale |
463 | - x, y = Abs(xs)*float32(a.spr.Offset[0]), Abs(ys)*float32(a.spr.Offset[1]) | |
463 | + x, y = AbsF(xs)*float32(a.spr.Offset[0]), AbsF(ys)*float32(a.spr.Offset[1]) | |
464 | 464 | } |
465 | 465 | a.spr.glDraw(a.pal(pfx), int32(a.mask), x*widthScale, y*heightScale, |
466 | 466 | &a.tile, xs*widthScale, xcs*xbs*h*widthScale, ys*heightScale, |
@@ -5,7 +5,35 @@ import ( | ||
5 | 5 | "strings" |
6 | 6 | ) |
7 | 7 | |
8 | -func Abs(f float32) float32 { | |
8 | +func Min(arg ...int32) (min int32) { | |
9 | + if len(arg) > 0 { | |
10 | + min = arg[0] | |
11 | + for i := 1; i < len(arg); i++ { | |
12 | + if arg[i] < min { | |
13 | + min = arg[i] | |
14 | + } | |
15 | + } | |
16 | + } | |
17 | + return | |
18 | +} | |
19 | +func Max(arg ...int32) (max int32) { | |
20 | + if len(arg) > 0 { | |
21 | + max = arg[0] | |
22 | + for i := 1; i < len(arg); i++ { | |
23 | + if arg[i] > max { | |
24 | + max = arg[i] | |
25 | + } | |
26 | + } | |
27 | + } | |
28 | + return | |
29 | +} | |
30 | +func Abs(i int32) int32 { | |
31 | + if i < 0 { | |
32 | + return -i | |
33 | + } | |
34 | + return i | |
35 | +} | |
36 | +func AbsF(f float32) float32 { | |
9 | 37 | if f < 0 { |
10 | 38 | return -f |
11 | 39 | } |
@@ -0,0 +1,344 @@ | ||
1 | +package main | |
2 | + | |
3 | +import "github.com/go-gl/glfw/v3.2/glfw" | |
4 | + | |
5 | +type CommandKey int32 | |
6 | + | |
7 | +const ( | |
8 | + CK_B CommandKey = iota | |
9 | + CK_D | |
10 | + CK_F | |
11 | + CK_U | |
12 | + CK_DB | |
13 | + CK_UB | |
14 | + CK_DF | |
15 | + CK_UF | |
16 | + CK_nB | |
17 | + CK_nD | |
18 | + CK_nF | |
19 | + CK_nU | |
20 | + CK_nDB | |
21 | + CK_nUB | |
22 | + CK_nDF | |
23 | + CK_nUF | |
24 | + CK_Bs | |
25 | + CK_Ds | |
26 | + CK_Fs | |
27 | + CK_Us | |
28 | + CK_DBs | |
29 | + CK_UBs | |
30 | + CK_DFs | |
31 | + CK_UFs | |
32 | + CK_nBs | |
33 | + CK_nDs | |
34 | + CK_nFs | |
35 | + CK_nUs | |
36 | + CK_nDBs | |
37 | + CK_nUBs | |
38 | + CK_nDFs | |
39 | + CK_nUFs | |
40 | + CK_a | |
41 | + CK_b | |
42 | + CK_c | |
43 | + CK_x | |
44 | + CK_y | |
45 | + CK_z | |
46 | + CK_s | |
47 | + CK_na | |
48 | + CK_nb | |
49 | + CK_nc | |
50 | + CK_nx | |
51 | + CK_ny | |
52 | + CK_nz | |
53 | + CK_ns | |
54 | +) | |
55 | + | |
56 | +var keySatate = make(map[glfw.Key]bool) | |
57 | + | |
58 | +func keyCallback(_ *glfw.Window, key glfw.Key, _ int, | |
59 | + action glfw.Action, _ glfw.ModifierKey) { | |
60 | + switch action { | |
61 | + case glfw.Release: | |
62 | + keySatate[key] = false | |
63 | + case glfw.Press: | |
64 | + keySatate[key] = true | |
65 | + } | |
66 | +} | |
67 | + | |
68 | +var joystick = [...]glfw.Joystick{glfw.Joystick1, glfw.Joystick2, | |
69 | + glfw.Joystick3, glfw.Joystick4, glfw.Joystick5, glfw.Joystick6, | |
70 | + glfw.Joystick7, glfw.Joystick8, glfw.Joystick9, glfw.Joystick10, | |
71 | + glfw.Joystick11, glfw.Joystick12, glfw.Joystick13, glfw.Joystick14, | |
72 | + glfw.Joystick15, glfw.Joystick16} | |
73 | + | |
74 | +func JoystickState(joy int32, button int32) bool { | |
75 | + if joy < 0 { | |
76 | + return keySatate[glfw.Key(button)] | |
77 | + } | |
78 | + if int(joy) >= len(joystick) { | |
79 | + return false | |
80 | + } | |
81 | + if button < 0 { | |
82 | + button = -button - 1 | |
83 | + axes := glfw.GetJoystickAxes(joystick[joy]) | |
84 | + if len(axes)*2 <= int(button) { | |
85 | + return false | |
86 | + } | |
87 | + switch button & 1 { | |
88 | + case 0: | |
89 | + return axes[button/2] < -0.1 | |
90 | + case 1: | |
91 | + return axes[button/2] > 0.1 | |
92 | + } | |
93 | + } | |
94 | + btns := glfw.GetJoystickButtons(joystick[joy]) | |
95 | + if len(btns) <= int(button) { | |
96 | + return false | |
97 | + } | |
98 | + return btns[button] != 0 | |
99 | +} | |
100 | + | |
101 | +type commandBuffer struct { | |
102 | + Bb, Db, Fb, Ub int32 | |
103 | + ab, bb, cb, xb, yb, zb, sb int32 | |
104 | + B, D, F, U int8 | |
105 | + a, b, c, x, y, z, s int8 | |
106 | +} | |
107 | + | |
108 | +func newCommandBuffer() *commandBuffer { | |
109 | + return &commandBuffer{B: -1, D: -1, F: -1, U: -1, | |
110 | + a: -1, b: -1, c: -1, x: -1, y: -1, z: -1, s: -1} | |
111 | +} | |
112 | +func (__ *commandBuffer) Input(B, D, F, U, a, b, c, x, y, z, s bool) { | |
113 | + if (B && !F) != (__.B > 0) { | |
114 | + __.Bb = 0 | |
115 | + __.B *= -1 | |
116 | + } | |
117 | + __.Bb += int32(__.B) | |
118 | + if (D && !U) != (__.D > 0) { | |
119 | + __.Db = 0 | |
120 | + __.D *= -1 | |
121 | + } | |
122 | + __.Db += int32(__.D) | |
123 | + if (F && !B) != (__.F > 0) { | |
124 | + __.Fb = 0 | |
125 | + __.F *= -1 | |
126 | + } | |
127 | + __.Fb += int32(__.F) | |
128 | + if (U && !D) != (__.U > 0) { | |
129 | + __.Ub = 0 | |
130 | + __.U *= -1 | |
131 | + } | |
132 | + __.Ub += int32(__.U) | |
133 | + if a != (__.a > 0) { | |
134 | + __.ab = 0 | |
135 | + __.a *= -1 | |
136 | + } | |
137 | + __.ab += int32(__.a) | |
138 | + if b != (__.b > 0) { | |
139 | + __.bb = 0 | |
140 | + __.b *= -1 | |
141 | + } | |
142 | + __.bb += int32(__.b) | |
143 | + if c != (__.c > 0) { | |
144 | + __.cb = 0 | |
145 | + __.c *= -1 | |
146 | + } | |
147 | + __.cb += int32(__.c) | |
148 | + if x != (__.x > 0) { | |
149 | + __.xb = 0 | |
150 | + __.x *= -1 | |
151 | + } | |
152 | + __.xb += int32(__.x) | |
153 | + if y != (__.y > 0) { | |
154 | + __.yb = 0 | |
155 | + __.y *= -1 | |
156 | + } | |
157 | + __.yb += int32(__.y) | |
158 | + if z != (__.z > 0) { | |
159 | + __.zb = 0 | |
160 | + __.z *= -1 | |
161 | + } | |
162 | + __.zb += int32(__.z) | |
163 | + if s != (__.s > 0) { | |
164 | + __.sb = 0 | |
165 | + __.s *= -1 | |
166 | + } | |
167 | + __.sb += int32(__.s) | |
168 | +} | |
169 | +func (__ *commandBuffer) State(ck CommandKey) int32 { | |
170 | + switch ck { | |
171 | + case CK_B: | |
172 | + return Min(-Max(__.Db, __.Ub), __.Bb) | |
173 | + case CK_D: | |
174 | + return Min(-Max(__.Bb, __.Fb), __.Db) | |
175 | + case CK_F: | |
176 | + return Min(-Max(__.Db, __.Ub), __.Fb) | |
177 | + case CK_U: | |
178 | + return Min(-Max(__.Bb, __.Fb), __.Ub) | |
179 | + case CK_DB: | |
180 | + return Min(__.Db, __.Bb) | |
181 | + case CK_UB: | |
182 | + return Min(__.Ub, __.Bb) | |
183 | + case CK_DF: | |
184 | + return Min(__.Db, __.Fb) | |
185 | + case CK_UF: | |
186 | + return Min(__.Ub, __.Fb) | |
187 | + case CK_Bs: | |
188 | + return __.Bb | |
189 | + case CK_Ds: | |
190 | + return __.Db | |
191 | + case CK_Fs: | |
192 | + return __.Fb | |
193 | + case CK_Us: | |
194 | + return __.Ub | |
195 | + case CK_DBs: | |
196 | + return Min(-Max(__.Ub, __.Fb), Max(__.Db, __.Bb)) | |
197 | + case CK_UBs: | |
198 | + return Min(-Max(__.Db, __.Fb), Max(__.Ub, __.Bb)) | |
199 | + case CK_DFs: | |
200 | + return Min(-Max(__.Ub, __.Bb), Max(__.Db, __.Fb)) | |
201 | + case CK_UFs: | |
202 | + return Min(-Max(__.Db, __.Bb), Max(__.Ub, __.Fb)) | |
203 | + case CK_a: | |
204 | + return __.ab | |
205 | + case CK_b: | |
206 | + return __.bb | |
207 | + case CK_c: | |
208 | + return __.cb | |
209 | + case CK_x: | |
210 | + return __.xb | |
211 | + case CK_y: | |
212 | + return __.yb | |
213 | + case CK_z: | |
214 | + return __.zb | |
215 | + case CK_s: | |
216 | + return __.sb | |
217 | + case CK_nB: | |
218 | + return -Min(-Max(__.Db, __.Ub), __.Bb) | |
219 | + case CK_nD: | |
220 | + return -Min(-Max(__.Bb, __.Fb), __.Db) | |
221 | + case CK_nF: | |
222 | + return -Min(-Max(__.Db, __.Ub), __.Fb) | |
223 | + case CK_nU: | |
224 | + return -Min(-Max(__.Bb, __.Fb), __.Ub) | |
225 | + case CK_nDB: | |
226 | + return -Min(__.Db, __.Bb) | |
227 | + case CK_nUB: | |
228 | + return -Min(__.Ub, __.Bb) | |
229 | + case CK_nDF: | |
230 | + return -Min(__.Db, __.Fb) | |
231 | + case CK_nUF: | |
232 | + return -Min(__.Ub, __.Fb) | |
233 | + case CK_nBs: | |
234 | + return -__.Bb | |
235 | + case CK_nDs: | |
236 | + return -__.Db | |
237 | + case CK_nFs: | |
238 | + return -__.Fb | |
239 | + case CK_nUs: | |
240 | + return -__.Ub | |
241 | + case CK_nDBs: | |
242 | + return -Min(-Max(__.Ub, __.Fb), Max(__.Db, __.Bb)) | |
243 | + case CK_nUBs: | |
244 | + return -Min(-Max(__.Db, __.Fb), Max(__.Ub, __.Bb)) | |
245 | + case CK_nDFs: | |
246 | + return -Min(-Max(__.Ub, __.Bb), Max(__.Db, __.Fb)) | |
247 | + case CK_nUFs: | |
248 | + return -Min(-Max(__.Db, __.Bb), Max(__.Ub, __.Fb)) | |
249 | + case CK_na: | |
250 | + return -__.ab | |
251 | + case CK_nb: | |
252 | + return -__.bb | |
253 | + case CK_nc: | |
254 | + return -__.cb | |
255 | + case CK_nx: | |
256 | + return -__.xb | |
257 | + case CK_ny: | |
258 | + return -__.yb | |
259 | + case CK_nz: | |
260 | + return -__.zb | |
261 | + case CK_ns: | |
262 | + return -__.sb | |
263 | + } | |
264 | + return 0 | |
265 | +} | |
266 | +func (__ *commandBuffer) State2(ck CommandKey) int32 { | |
267 | + f := func(a, b, c int32) int32 { | |
268 | + switch { | |
269 | + case a > 0: | |
270 | + return -Max(b, c) | |
271 | + case b > 0: | |
272 | + return -Max(a, c) | |
273 | + case c > 0: | |
274 | + return -Max(a, b) | |
275 | + } | |
276 | + return -Max(a, b, c) | |
277 | + } | |
278 | + switch ck { | |
279 | + case CK_Bs: | |
280 | + if __.Bb < 0 { | |
281 | + return __.Bb | |
282 | + } | |
283 | + return Min(Abs(__.Bb), Abs(__.Db), Abs(__.Ub)) | |
284 | + case CK_Ds: | |
285 | + if __.Db < 0 { | |
286 | + return __.Db | |
287 | + } | |
288 | + return Min(Abs(__.Db), Abs(__.Bb), Abs(__.Fb)) | |
289 | + case CK_Fs: | |
290 | + if __.Fb < 0 { | |
291 | + return __.Fb | |
292 | + } | |
293 | + return Min(Abs(__.Fb), Abs(__.Db), Abs(__.Ub)) | |
294 | + case CK_Us: | |
295 | + if __.Ub < 0 { | |
296 | + return __.Ub | |
297 | + } | |
298 | + return Min(Abs(__.Ub), Abs(__.Bb), Abs(__.Fb)) | |
299 | + case CK_DBs: | |
300 | + if s := __.State(CK_DBs); s < 0 { | |
301 | + return s | |
302 | + } | |
303 | + return Min(Abs(__.Db), Abs(__.Bb)) | |
304 | + case CK_UBs: | |
305 | + if s := __.State(CK_UBs); s < 0 { | |
306 | + return s | |
307 | + } | |
308 | + return Min(Abs(__.Ub), Abs(__.Bb)) | |
309 | + case CK_DFs: | |
310 | + if s := __.State(CK_DFs); s < 0 { | |
311 | + return s | |
312 | + } | |
313 | + return Min(Abs(__.Db), Abs(__.Fb)) | |
314 | + case CK_UFs: | |
315 | + if s := __.State(CK_UFs); s < 0 { | |
316 | + return s | |
317 | + } | |
318 | + return Min(Abs(__.Ub), Abs(__.Fb)) | |
319 | + case CK_nBs: | |
320 | + return f(__.State(CK_B), __.State(CK_UB), __.State(CK_DB)) | |
321 | + case CK_nDs: | |
322 | + return f(__.State(CK_D), __.State(CK_DB), __.State(CK_DF)) | |
323 | + case CK_nFs: | |
324 | + return f(__.State(CK_F), __.State(CK_DF), __.State(CK_UF)) | |
325 | + case CK_nUs: | |
326 | + return f(__.State(CK_U), __.State(CK_UB), __.State(CK_UF)) | |
327 | + case CK_nDBs: | |
328 | + return f(__.State(CK_DB), __.State(CK_D), __.State(CK_B)) | |
329 | + case CK_nUBs: | |
330 | + return f(__.State(CK_UB), __.State(CK_U), __.State(CK_B)) | |
331 | + case CK_nDFs: | |
332 | + return f(__.State(CK_DF), __.State(CK_D), __.State(CK_F)) | |
333 | + case CK_nUFs: | |
334 | + return f(__.State(CK_UF), __.State(CK_U), __.State(CK_F)) | |
335 | + } | |
336 | + return __.State(ck) | |
337 | +} | |
338 | +func (__ *commandBuffer) LastDirectionTime() int32 { | |
339 | + return Min(Abs(__.Bb), Abs(__.Db), Abs(__.Fb), Abs(__.Ub)) | |
340 | +} | |
341 | +func (__ *commandBuffer) LastChangeTime() int32 { | |
342 | + return Min(__.LastDirectionTime(), Abs(__.ab), Abs(__.bb), Abs(__.cb), | |
343 | + Abs(__.xb), Abs(__.yb), Abs(__.zb), Abs(__.sb)) | |
344 | +} |
@@ -78,6 +78,7 @@ func main() { | ||
78 | 78 | "Ikemen GO", nil, nil) |
79 | 79 | chk(err) |
80 | 80 | window.MakeContextCurrent() |
81 | + window.SetKeyCallback(keyCallback) | |
81 | 82 | glfw.SwapInterval(1) |
82 | 83 | chk(gl.Init()) |
83 | 84 | RenderInit() |
@@ -149,8 +149,8 @@ func drawQuads(x1, y1, x2, y2, x3, y3, x4, y4, r, g, b, a, pers float32) { | ||
149 | 149 | gl.TexCoord2f(0, 0) |
150 | 150 | gl.Vertex2f(x4, y4) |
151 | 151 | if pers != 1 { |
152 | - n := int((1 - (pers * pers)) * Abs(x1-x2) * | |
153 | - float32(scrrect[3]>>5) / (Abs(y1-y4) + float32(scrrect[3]>>5))) | |
152 | + n := int((1 - (pers * pers)) * AbsF(x1-x2) * | |
153 | + float32(scrrect[3]>>5) / (AbsF(y1-y4) + float32(scrrect[3]>>5))) | |
154 | 154 | for i := 1; i < n; i++ { |
155 | 155 | gl.TexCoord2f(float32(i)/float32(n), 1) |
156 | 156 | gl.Vertex2f(x1+(x2-x1)*float32(i)/float32(n), |
@@ -169,9 +169,9 @@ func drawQuads(x1, y1, x2, y2, x3, y3, x4, y4, r, g, b, a, pers float32) { | ||
169 | 169 | func rmTileHSub(x1, y1, x2, y2, x3, y3, x4, y4, xtw, xbw, xts, xbs float32, |
170 | 170 | tl *[4]int32, rcx, r, g, b, a, pers float32) { |
171 | 171 | topdist := xtw + xts*float32((*tl)[0]) |
172 | - if Abs(topdist) >= 0.01 { | |
172 | + if AbsF(topdist) >= 0.01 { | |
173 | 173 | botdist := xbw + xbs*float32((*tl)[0]) |
174 | - db := (x4 - rcx) * (botdist - topdist) / Abs(topdist) | |
174 | + db := (x4 - rcx) * (botdist - topdist) / AbsF(topdist) | |
175 | 175 | x1 += db |
176 | 176 | x2 += db |
177 | 177 | if (*tl)[2] == 1 { |
@@ -217,7 +217,7 @@ func rmTileHSub(x1, y1, x2, y2, x3, y3, x4, y4, xtw, xbw, xts, xbs float32, | ||
217 | 217 | if (*tl)[2] != 1 && n != 0 { |
218 | 218 | n-- |
219 | 219 | } |
220 | - if n == 0 || Abs(topdist) < 0.01 { | |
220 | + if n == 0 || AbsF(topdist) < 0.01 { | |
221 | 221 | break |
222 | 222 | } |
223 | 223 | x4 = x3 + xts*float32((*tl)[0]) |
@@ -233,10 +233,10 @@ func rmTileSub(w, h uint16, x, y float32, tl *[4]int32, | ||
233 | 233 | x3, y3 := x+xts*float32(w), rcy+(y-rcy)*vs |
234 | 234 | x4, y4 := x, y3 |
235 | 235 | var pers float32 |
236 | - if Abs(xts) < Abs(xbs) { | |
237 | - pers = Abs(xts) / Abs(xbs) | |
236 | + if AbsF(xts) < AbsF(xbs) { | |
237 | + pers = AbsF(xts) / AbsF(xbs) | |
238 | 238 | } else { |
239 | - pers = Abs(xbs) / Abs(xts) | |
239 | + pers = AbsF(xbs) / AbsF(xts) | |
240 | 240 | } |
241 | 241 | if agl != 0 { |
242 | 242 | kaiten(&x1, &y1, float64(agl), rcx, rcy, vs) |
@@ -254,7 +254,7 @@ func rmTileSub(w, h uint16, x, y float32, tl *[4]int32, | ||
254 | 254 | x3d = x4d - rxadd*ys*float32(h) + (xts/xbs)*(x3d-x4d) |
255 | 255 | y3d = y2d + ys*vs*float32(h) |
256 | 256 | x4d = x4d - rxadd*ys*float32(h) |
257 | - if Abs(y3d-y4d) < 0.01 { | |
257 | + if AbsF(y3d-y4d) < 0.01 { | |
258 | 258 | break |
259 | 259 | } |
260 | 260 | y4d = y3d |
@@ -299,7 +299,7 @@ func rmTileSub(w, h uint16, x, y float32, tl *[4]int32, | ||
299 | 299 | x2 = x1 + rxadd*ys*float32(h) + (xbs/xts)*(x2-x1) |
300 | 300 | y2 = y3 - ys*vs*float32(h) |
301 | 301 | x1 = x1 + rxadd*ys*float32(h) |
302 | - if Abs(y1-y2) < 0.01 { | |
302 | + if AbsF(y1-y2) < 0.01 { | |
303 | 303 | break |
304 | 304 | } |
305 | 305 | y1 = y2 |