-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathangolmois.c
1915 lines (1750 loc) · 64.1 KB
/
angolmois.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Angolmois -- the simple BMS player
* Copyright (c) 2005, 2007, 2009, 2012, 2013, Kang Seonghoon.
* Project Angolmois is copyright (c) 2003-2007, Choi Kaya (CHKY).
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <limits.h>
#include <float.h>
#include <time.h>
#include <SDL.h>
#include <SDL_mixer.h>
#include <SDL_image.h>
#include <smpeg.h>
static const char VERSION[] = "Angolmois 2.0 alpha 3";
static const char *argv0 = "angolmois";
/******************************************************************************/
/* utility declarations */
#define STRINGIFY_(x) #x
#define STRINGIFY(x) STRINGIFY_(x)
#define R(x,y,w,h) &(SDL_Rect){x,y,w,h}
#define ARRAYSIZE(arr) ((int) (sizeof(arr) / sizeof(*(arr))))
#define MEASURE_TO_MSEC(measure,bpm) ((measure) * 24e4 / (bpm))
#define MSEC_TO_MEASURE(msec,bpm) ((msec) * (bpm) / 24e4)
static void die(const char *msg, ...); /* platform dependent */
#define SHOULD(x) ((x) ? (void) 0 : die("assertion failure at line %d: %s", __LINE__, #x))
static void warn(const char *msg, ...)
{
va_list v;
fprintf(stderr, "*** Warning: ");
va_start(v, msg);
vfprintf(stderr, msg, v);
va_end(v);
fprintf(stderr, "\n");
}
#define UPPERCASE(c) ('a' <= (c) && (c) <= 'z' ? (c) + ('A' - 'a') : (c))
#define STRACOPY(s) strcpy(malloc(strlen(s) + 1), (s))
static int strieq(const char *a, const char *b)
{
while (*a && *b && UPPERCASE(*a) == UPPERCASE(*b)) ++a, ++b;
return *a == *b;
}
static int strisuffix(const char *a, const char *b)
{
size_t alen = strlen(a), blen = strlen(b);
return alen >= blen && strieq(a + (alen - blen), b);
}
/* generic extensible vector */
struct xv_base { ptrdiff_t xv__size, xv__alloc; };
#define XV(...) struct { struct xv_base xv__base; __VA_ARGS__ *xv__ptr; }
#define XV_BASE(xv) ((xv).xv__base)
#define XV_SIZE(xv) (XV_BASE(xv).xv__size)
#define XV_ALLOC(xv) (XV_BASE(xv).xv__alloc)
#define XV_PTR(xv) ((xv).xv__ptr)
#define XV_ITEMSIZE(xv) (ptrdiff_t) sizeof(*XV_PTR(xv))
#define XV_EMPTY {.xv__base = {.xv__size = 0, .xv__alloc = 0}, .xv__ptr = NULL}
#define XV_INIT(xv) (XV_SIZE(xv) = XV_ALLOC(xv) = 0, XV_PTR(xv) = NULL)
#define XV_INVARIANT(xv) \
(XV_SIZE(xv) >= 0 && XV_ALLOC(xv) >= 0 && XV_SIZE(xv) <= XV_ALLOC(xv) && \
(XV_ALLOC(xv) > 0) == (XV_PTR(xv) != NULL))
#define XV_CHECK(xv,i) ((ptrdiff_t) (i) >= 0 && (ptrdiff_t) (i) < XV_SIZE(xv))
#define XV_RESERVE(xv,n) \
((ptrdiff_t) (n) > XV_ALLOC(xv) && \
(XV_PTR(xv) = xv_do_resize(&XV_BASE(xv), XV_PTR(xv), (n), XV_ITEMSIZE(xv)), 1))
#define XV_RESIZE(xv,n) (XV_SIZE(xv) = (ptrdiff_t) (n), XV_RESERVE(xv, XV_SIZE(xv)))
#define XV_RESIZEBY(xv,n) XV_RESIZE(xv, XV_SIZE(xv) + (ptrdiff_t) (n))
#define XV_AT(xv,i) (XV_PTR(xv)[(ptrdiff_t) (i)])
#define XV_LAST(xv) (XV_PTR(xv)[XV_SIZE(xv)-1])
#define XV_LOOP(xv,itype,i,before,after) \
for (itype (i) = 0; (ptrdiff_t) (i) < XV_SIZE(xv) && ((void) (before), 1); \
(void) (after), ++(i))
#define XV_IEACH(i,v,xv) XV_LOOP(xv, ptrdiff_t, i, (v) = XV_AT(xv,i), 0)
#define XV_IEACHPTR(i,p,xv) XV_LOOP(xv, ptrdiff_t, i, (p) = &XV_AT(xv,i), 0)
#define XV_EACH(v,xv) XV_IEACH(xv__i_##__LINE__,v,xv)
#define XV_EACHPTR(p,xv) XV_IEACHPTR(xv__i_##__LINE__,p,xv)
#define XV_PUSH(xv,x) ((void) XV_RESERVE(xv, XV_SIZE(xv)+1), XV_AT(xv, XV_SIZE(xv)++) = (x))
#define XV_POP(xv) (XV_PTR(xv)[--XV_SIZE(xv)])
#define XV_COPYTO(p,xv,i,n) memcpy((p), XV_PTR(xv)+i, (n)*XV_ITEMSIZE(xv))
#define XV_COPYFROM(xv,i,p,n) ((void) memcpy(XV_PTR(xv)+i, (p), (n)*XV_ITEMSIZE(xv)))
#define XV_COPYPUSH(xv,p,n) \
((void) XV_RESERVE(xv, XV_SIZE(xv) + (ptrdiff_t) (n)), \
XV_COPYFROM(xv, XV_SIZE(xv), p, n), (void) (XV_SIZE(xv) += (n)))
#define XV_ZEROIZE(xv,i,n) memset(XV_PTR(xv) + (i), 0, (n) * XV_ITEMSIZE(xv))
#define XV_FREE(xv) if (XV_ALLOC(xv) == 0) { } else free(XV_PTR(xv))
static void *xv_do_resize(struct xv_base *base, void *ptr, ptrdiff_t n, ptrdiff_t itemsize)
{
static const ptrdiff_t GROWLIMIT = 0x7fff;
if (n <= GROWLIMIT) {
--n; n |= n >> 1; n |= n >> 2; n |= n >> 4; n |= n >> 8; ++n;
if (n < 4) n = 4;
} else {
if (n > (PTRDIFF_MAX & ~GROWLIMIT)) die("memory error"); /* overflow */
n = (n + GROWLIMIT) & ~GROWLIMIT;
}
ptr = realloc(ptr, n * itemsize);
if (!ptr) die("memory error");
base->xv__alloc = n;
return ptr;
}
/* George Marsaglia's MWC256 generator (period 2^8222) */
struct rngstate { uint32_t state[257]; uint8_t index; }; /* state[256] for carry */
static void rng_seed(struct rngstate *r, uint32_t seed)
{
r->index = 0;
r->state[256] = seed;
for (int i = 255; i >= 0; --i) {
r->state[i] = 1812433253ull * (r->state[i+1] ^ (r->state[i+1] >> 30)) + i;
}
}
static uint32_t rng_gen(struct rngstate *r, uint32_t range)
{
uint32_t div = 0xffffffffull / range, max = div * range, x; /* so max < 2^32 */
do {
uint64_t t = 1540315826ull * r->state[++r->index] + r->state[256];
r->state[256] = t >> 32;
x = r->state[256] + (t & 0xffffffffull);
if (x < r->state[256]) ++x, ++r->state[256];
r->state[r->index] = x;
} while (x >= max);
return x / div;
}
/******************************************************************************/
/* path resolution & system dependent functions */
static char *bmspath; /* initially a path to BMS file, later a dirname of it */
static const char *SOUND_EXTS[] = {".wav", ".ogg", ".mp3", NULL};
static const char *IMAGE_EXTS[] = {".bmp", ".png", ".jpg", ".jpeg", ".gif", NULL};
static int match_filename(const char *s, const char *t, const char **exts)
{
const char *sbegin = s, *send = s + strlen(s);
while (*s && *t && UPPERCASE(*s) == UPPERCASE(*t)) ++s, ++t;
if (*s == *t) return 1;
if (sbegin != s && exts) {
for (; *exts; ++exts) {
int l = (int) strlen(*exts);
if (send-s < l && strieq(send-l, *exts)) return 1;
}
}
return 0;
}
#ifdef _WIN32
#include <windows.h>
static const char DIRSEP = '\\';
static int filedialog(char *buf)
{
OPENFILENAME ofn = {
.lStructSize = sizeof ofn,
.lpstrFilter =
"All Be-Music Source File (*.bms;*.bme;*.bml;*.pms)\0*.bms;*.bme;*.bml;*.pms\0"
"Be-Music Source File (*.bms)\0*.bms\0"
"Extended Be-Music Source File (*.bme)\0*.bme\0"
"Longnote Be-Music Source File (*.bml)\0*.bml\0"
"Po-Mu Source File (*.pms)\0*.pms\0"
"All Files (*.*)\0*.*\0",
.lpstrFile = buf,
.nMaxFile = 512,
.lpstrTitle = "Choose a file to play",
.Flags = OFN_HIDEREADONLY};
return GetOpenFileName(&ofn);
}
static void die(const char *msg, ...)
{
va_list v;
char buf[512];
va_start(v, msg);
vsnprintf(buf, sizeof buf, msg, v);
va_end(v);
MessageBox(0, buf, VERSION, 0);
exit(1);
}
static SDL_RWops *resolve_relative_path(char *path, const char **exts)
{
HANDLE h;
WIN32_FIND_DATAA fdata;
char pathbuf[strlen(bmspath)+strlen(path)+4], *p, *basename = path;
SDL_RWops *ops = NULL;
strcpy(pathbuf, *bmspath ? bmspath : ".");
p = pathbuf + strlen(pathbuf);
for (*p++ = '\\'; *path; ) {
if (*path == '/' || *path == '\\') {
*p++ = '\\';
basename = ++path;
} else {
*p++ = *path++;
}
}
*p = '\0';
path = strrchr(pathbuf, '\\') + 1;
p = strrchr(path, '.');
if (p) strcpy(p, ".*");
h = FindFirstFileA(pathbuf, &fdata);
if (h == INVALID_HANDLE_VALUE) return NULL;
do {
if (match_filename(fdata.cFileName, basename, exts)) {
strcpy(path, fdata.cFileName);
ops = SDL_RWFromFile(pathbuf, "rb");
break;
}
} while (FindNextFileA(h, &fdata));
FindClose(h);
return ops;
}
#else /* _WIN32 */
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
static const char DIRSEP = '/';
static int filedialog(char *buf)
{
(void) buf;
return 0;
}
static void die(const char *msg, ...)
{
va_list v;
fprintf(stderr, "%s: ", argv0);
va_start(v, msg);
vfprintf(stderr, msg, v);
va_end(v);
fprintf(stderr, "\n");
exit(1);
}
static SDL_RWops *resolve_relative_path(char *path, const char **exts)
{
DIR *d;
struct dirent *e;
int origcwd, i;
char pathbuf[strlen(path)+1]; /* so that we won't overwrite the original path */
SDL_RWops *ops = NULL;
path = strcpy(pathbuf, path);
origcwd = open(".", O_RDONLY);
if (origcwd < 0) return NULL;
if (*bmspath && chdir(bmspath) < 0) goto exit;
for (; path[i = strcspn(path, "\\/")]; ++path) {
if (i == 0) continue; /* skips "//" or trailing "/" etc. */
path[i] = '\0';
d = opendir(".");
if (!d) goto exit;
while ((e = readdir(d))) {
if (strieq(e->d_name, path)) {
if (chdir(e->d_name) < 0) {
closedir(d);
goto exit;
}
break;
}
}
closedir(d);
if (!e) goto exit;
path += i;
}
d = opendir(".");
if (!d) goto exit;
while ((e = readdir(d))) {
if (match_filename(e->d_name, path, exts)) {
ops = SDL_RWFromFile(e->d_name, "rb");
break;
}
}
closedir(d);
exit:
fchdir(origcwd);
close(origcwd);
return ops;
}
#endif
/******************************************************************************/
/* bms parser */
enum { S_TITLE = 0, S_GENRE = 1, S_ARTIST = 2, S_STAGEFILE = 3, S_BASEPATH = 4 };
enum { V_PLAYER = 0, V_PLAYLEVEL = 1, V_RANK = 2, V_LNTYPE = 3, V_LNOBJ = 4 };
#define MAXSTRING 1023
static char string[5][MAXSTRING+1];
static double initbpm = 130;
static int value[] = {[V_PLAYER]=1, [V_PLAYLEVEL]=0, [V_RANK]=2, [V_LNTYPE]=1, [V_LNOBJ]=0};
#define MAXKEY 1296
static char *sndpath[MAXKEY], *imgpath[MAXKEY];
static struct sndres { Mix_Chunk *res; int lastch; } sndres[MAXKEY];
static XV(int) sndlastchmap;
static struct imgres { SDL_Texture *tex; SMPEG *movie; SMPEG_Frame *frame; } imgres[MAXKEY];
static struct bgares { struct imgres *image; int x, y, tx, ty, w, h; } bgares[MAXKEY];
static double bpmtab[MAXKEY], stoptab[MAXKEY];
#define NNOTECHANS (2*36)
#define IS_NOTE_CHANNEL(c) ((c) >= 0 && (c) < NNOTECHANS)
enum { BGM_CHANNEL = NNOTECHANS, BGA_CHANNEL = NNOTECHANS+1, BPM_CHANNEL = NNOTECHANS+2,
STOP_CHANNEL = NNOTECHANS+3, NCHANS = NNOTECHANS+3 };
enum NOTE_type { LNDONE = 0, LNSTART = 1, NOTE = 2, INVNOTE = 3, BOMB = 4 };
enum BGA_type { BGA_LAYER = 0, BGA2_LAYER = 1, BGA3_LAYER = 2, POORBGA_LAYER = 3 };
enum BPM_type { BPM_BY_VALUE = 0, BPM_BY_INDEX = 1 };
enum STOP_type { STOP_BY_MEASURE = 0, STOP_BY_MSEC = 1 };
static const char *preset, *leftkeys, *rightkeys;
static const struct preset { const char *name1, *name2, *left, *right; } presets[] = {
{"5", "10", "16s 11a 12b 13a 14b 15a", "21a 22b 23a 24b 25a 26s"},
{"5/fp", "10/fp", "16s 11a 12b 13a 14b 15a 17p", "27p 21a 22b 23a 24b 25a 26s"},
{"7", "14", "16s 11a 12b 13a 14b 15a 18b 19a", "21a 22b 23a 24b 25a 28b 29a 26s"},
{"7/fp", "14/fp", "16s 11a 12b 13a 14b 15a 18b 19a 17p", "27p 21a 22b 23a 24b 25a 28b 29a 26s"},
{"9", NULL, "11q 12w 13e 14r 15t 22r 23e 24w 25q", NULL},
{"9-bme", NULL, "11q 12w 13e 14r 15t 18r 19e 16w 17q", NULL}};
static const char KEYKIND_MNEMONICS[] = "*aybqwertsp"; /* should align with tkeykinds */
static struct obj { double time; int chan, type, index, value, nograding:1; } *objs;
static int nobjs;
static double shortens[1000], originoffset = 0.0, length;
static int nleftkeys, nrightkeys, keyorder[NNOTECHANS], keykind[NNOTECHANS];
static int nkeys, haslongnote, hasbpmchange, nnotes, maxscore, duration;
static double shorten(double time)
{
return (time < 0 || time >= ARRAYSIZE(shortens) ? 1.0 : shortens[(int) time]);
}
static int getdigit(int n)
{
if ('0' <= n && n <= '9') return n - '0';
if ('a' <= n && n <= 'z') return (n - 'a') + 10;
if ('A' <= n && n <= 'Z') return (n - 'A') + 10;
return -MAXKEY;
}
static int key2index(const char *s, int *v) /* requires s[0] and s[1] allocated */
{
*v = getdigit(s[0]) * 36 + getdigit(s[1]);
return (*v >= 0);
}
static int compare_bmsline(const void *a, const void *b)
{
return strncmp(*(const char *const *)a, *(const char *const *)b, 6);
}
static int compare_obj(const void *a, const void *b)
{
const struct obj *A = a, *B = b;
return (A->time > B->time ? 1 : A->time < B->time ? -1 : A->type - B->type);
}
static void add_obj(int chan, double time, int type, int idx, int val)
{
objs = realloc(objs, sizeof(struct obj) * (nobjs+1));
objs[nobjs++] = (struct obj) {.time=time, .chan=chan, .type=type, .index=idx, .value=val};
}
#define KEY_STRING "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
#define KEY_PATTERN "%2[" KEY_STRING "]"
#define TO_KEY(key) ((char[3]){ KEY_STRING[(key)/36], KEY_STRING[(key)%36], '\0' })
static void parse_bms(struct rngstate *r)
{
static const char *bmsheader[] = {
NULL, "TITLE", "GENRE", "ARTIST", "STAGEFILE", "PATH_WAV", "BPM", "PLAYER",
"PLAYLEVEL", "RANK", "LNTYPE", "LNOBJ", "WAV", "BMP", "BGA", "STOP", "STP",
"RANDOM", "SETRANDOM", "ENDRANDOM", "IF", "ELSEIF", "ELSE", "ENDSW", "END"};
FILE *fp;
int i, j, k, a, b, measure = 0, chan, poorbgafix = 1;
int prev12[NNOTECHANS] = {0}, prev56[NNOTECHANS] = {0};
double t;
char *line, linebuf[4096], buf1[4096], buf2[4096];
XV(struct rnd { int val, state, skip; }) rnd = XV_EMPTY;
XV(char*) bmsline = XV_EMPTY;
fp = fopen(bmspath, "r");
if (!fp) die("Couldn't load BMS file: %s", bmspath);
XV_PUSH(rnd, ((struct rnd) {.val=0, .state=-1, .skip=0}));
while (fgets(line = linebuf, sizeof linebuf, fp)) {
while (*line == ' ' || *line == '\t') ++line;
if (*line++ != '#') continue;
for (i = 1; i < ARRAYSIZE(bmsheader); ++i) {
for (j = 0; bmsheader[i][j]; ++j) {
if (bmsheader[i][j] != UPPERCASE(line[j])) break;
}
if (!bmsheader[i][j]) {
line += j;
break;
}
}
switch (XV_LAST(rnd).skip || XV_LAST(rnd).state > 0 ? -i : i) {
case 1: /* title */
case 2: /* genre */
case 3: /* artist */
case 4: /* stagefile */
case 5: /* path_wav */
sscanf(line, "%*[ \t]%" STRINGIFY(MAXSTRING) "[^\r\n]", string[i-1]);
break;
case 6: /* bpm */
if (sscanf(line, "%*[ \t]%lf", &t) >= 1) {
if (t > 0) initbpm = t;
} else if (sscanf(line, KEY_PATTERN "%*[ ]%lf", buf1, &t) >= 2 && key2index(buf1, &i)) {
bpmtab[i] = t;
}
break;
case 7: /* player */
case 8: /* playlevel */
case 9: /* rank */
case 10: /* lntype */
sscanf(line, "%*[ \t]%d", &value[i-7]);
break;
case 11: /* lnobj */
if (sscanf(line, "%*[ \t]" KEY_PATTERN, buf1) >= 1 && key2index(buf1, &i)) {
value[V_LNOBJ] = i;
}
break;
case 12: /* wav## */
case 13: /* bmp## */
if (sscanf(line, KEY_PATTERN "%*[ \t]%[^\r\n]", buf1, buf2) >= 2 && key2index(buf1, &j)) {
char **path = (i==12 ? sndpath : imgpath);
free(path[j]);
path[j] = STRACOPY(buf2);
}
break;
case 14: /* bga## */
{
int dst, src, x1, y1, x2, y2, dx, dy;
if (sscanf(line, KEY_PATTERN "%*[ \t]" KEY_PATTERN "%*[ ]%d %d %d %d %d %d",
buf1, buf2, &x1, &y1, &x2, &y2, &dx, &dy) >= 8 &&
key2index(buf1, &dst) && key2index(buf2, &src)) {
if (x1 < 0) x1 = 0;
if (y1 < 0) y1 = 0;
if (x2 > x1 + 256) x2 = x1 + 256;
if (y2 > y1 + 256) y2 = y1 + 256;
bgares[dst] = (struct bgares) {.image=&imgres[src], .x=x1, .y=y1, .tx=dx, .ty=dy, .w=x2-x1, .h=y2-y1};
}
}
break;
case 15: /* stop## */
if (sscanf(line, KEY_PATTERN "%*[ \t]%d", buf1, &j) >= 2 && key2index(buf1, &i)) {
if (j > 0) stoptab[i] = j / 192.0;
}
break;
case 16: /* stp## */
if (sscanf(line, "%d.%d %d", &i, &j, &k) >= 3) {
if (k > 0) add_obj(STOP_CHANNEL, i+j/1e3, STOP_BY_MSEC, k, 0);
}
break;
case 17: case -17: /* random */
case 18: case -18: /* setrandom */
if (sscanf(line, "%*[ \t]%d", &j) >= 1) {
/* do not generate a random value if the entire block is skipped */
k = (XV_LAST(rnd).state > 0 || XV_LAST(rnd).skip);
if (i == 17 && !k && j > 0) j = rng_gen(r, j) + 1;
XV_PUSH(rnd, ((struct rnd) {.val=j, .state=-1, .skip=k}));
}
break;
case 19: case -19: /* endrandom */
if (XV_SIZE(rnd) > 1) --XV_SIZE(rnd);
break;
case 20: case -20: /* if */
case 21: case -21: /* elseif */
if (sscanf(line, "%*[ \t]%d", &j) >= 1) {
if (i == 20 || XV_LAST(rnd).state == 1) {
XV_LAST(rnd).state = (j <= 0 || XV_LAST(rnd).val != j);
} else {
XV_LAST(rnd).state = 2; /* ignore further branches */
}
}
break;
case 22: case -22: /* else */
XV_LAST(rnd).state = (XV_LAST(rnd).state == 1 ? 0 : 2);
break;
case 24: case -24: /* end(if) but not endsw */
for (j = (int) XV_SIZE(rnd) - 1; j > 0 && XV_AT(rnd,j).state < 0; --j);
if (j > 0) XV_SIZE(rnd) = j + 1; /* implicitly closes #RANDOMs if needed */
XV_LAST(rnd).state = -1;
break;
case ARRAYSIZE(bmsheader): /* #####:... */
/* only check validity, do not store them yet */
if (sscanf(line, "%*1[0123456789]%*1[0123456789]%*1[0123456789]"
"%*1[" KEY_STRING "]%*1[" KEY_STRING "]:%c", buf1) >= 1) {
XV_PUSH(bmsline, STRACOPY(line));
}
}
}
fclose(fp);
XV_FREE(rnd);
qsort(XV_PTR(bmsline), XV_SIZE(bmsline), XV_ITEMSIZE(bmsline), compare_bmsline);
XV_EACH(line, bmsline) {
measure = (line[0] - '0') * 100 + (line[1] - '0') * 10 + (line[2] - '0');
if (!key2index(line+3, &chan)) SHOULD(0);
if (chan == 2) {
shortens[measure] = atof(line+6);
} else {
j = 6 + strspn(line+6, " \t\r\n");
a = strcspn(line+j, " \t\r\n") / 2;
for (k = 0; k < a; ++k, j+=2) {
if (!key2index(line+j, &b) || !b) continue;
t = measure + 1. * k / a;
if (chan == 1) {
add_obj(BGM_CHANNEL, t, 0, b, 0);
} else if (chan == 3) {
if (b/36<16 && b%36<16) add_obj(BPM_CHANNEL, t, BPM_BY_VALUE, b/36*16+b%36, 0);
} else if (chan == 4) {
add_obj(BGA_CHANNEL, t, BGA_LAYER, b, 0);
} else if (chan == 6) {
add_obj(BGA_CHANNEL, t, POORBGA_LAYER, b, 0);
if (t == 0) poorbgafix = 0;
} else if (chan == 7) {
add_obj(BGA_CHANNEL, t, BGA2_LAYER, b, 0);
} else if (chan == 8) {
add_obj(BPM_CHANNEL, t, BPM_BY_INDEX, b, 0);
} else if (chan == 9) {
add_obj(STOP_CHANNEL, t, STOP_BY_MEASURE, b, 0);
} else if (chan == 10) {
add_obj(BGA_CHANNEL, t, BGA3_LAYER, b, 0);
} else if (chan >= 1*36 && chan < 3*36) { /* channels 1x/2x */
int c = chan - 1*36;
if (value[V_LNOBJ] && b == value[V_LNOBJ]) {
if (prev12[c] && objs[prev12[c]-1].type==NOTE) {
objs[prev12[c]-1].type = LNSTART;
add_obj(c, t, LNDONE, b, 0);
prev12[c] = 0;
}
} else {
add_obj(c, t, NOTE, b, 0);
prev12[c] = nobjs;
}
} else if (chan >= 3*36 && chan < 5*36) { /* channels 3x/4x */
add_obj(chan - 3*36, t, INVNOTE, b, 0);
} else if (chan >= 5*36 && chan < 7*36) { /* channels 5x/6x */
int c = chan - 5*36;
if (value[V_LNTYPE] == 1) {
if (prev56[c]) {
add_obj(c, t, LNDONE, b, 0);
prev56[c] = 0;
} else {
add_obj(c, t, LNSTART, b, 0);
prev56[c] = nobjs;
}
} else if (value[V_LNTYPE] == 2) {
double t2 = measure + 1. * (k + 1) / a;
if (prev56[c] && objs[prev56[c]-1].time == t) {
objs[prev56[c]-1].time = t2;
objs[prev56[c]-1].index = b;
} else {
add_obj(c, t, LNSTART, b, 0);
add_obj(c, t2, LNDONE, b, 0);
prev56[c] = nobjs;
}
}
} else if (chan >= 13*36 && chan < 15*36) { /* channels Dx/Ex */
int dmg = (b == MAXKEY-1 ? -1 : b * 512 / 200);
if (dmg && dmg <= 512) add_obj(chan - 13*36, t, BOMB, 0, dmg);
}
}
}
free(line);
}
XV_FREE(bmsline);
if (poorbgafix) add_obj(BGA_CHANNEL, 0, POORBGA_LAYER, 0, 0); /* for POOR BGA movie */
length = measure + 2;
for (int i = 0; i < NNOTECHANS; ++i) {
if (prev12[i] || (value[V_LNTYPE] == 1 && prev56[i])) {
add_obj(i, measure + 1, LNDONE, 0, 0);
}
}
for (int i = 0; i < ARRAYSIZE(shortens); ++i) {
if (shortens[i] <= .001) shortens[i] = 1;
}
}
static void remove_or_replace_note(int i)
{
if (IS_NOTE_CHANNEL(objs[i].chan) && objs[i].type > LNDONE && objs[i].type < INVNOTE && objs[i].index) {
objs[i].chan = BGM_CHANNEL;
objs[i].type = 0;
} else {
objs[i].chan = -1;
}
}
static void sanitize_bms(void)
{
if (!objs) return;
qsort(objs, nobjs, sizeof(struct obj), compare_obj);
for (int i = 0; i < NCHANS; ++i) if (i != BGM_CHANNEL && i != STOP_CHANNEL) {
int inside = 0, j = 0;
while (j < nobjs) {
int k = j, types = 0, lowest;
for (; k < nobjs && objs[k].time <= objs[j].time; ++k) if (objs[k].chan == i) {
if (types & (1 << objs[k].type)) {
remove_or_replace_note(k);
} else {
types |= 1 << objs[k].type;
}
}
/* remove overlapping LN endpoints altogether */
if (!(~types & ((1<<LNSTART)|(1<<LNDONE)))) types &= ~((1<<LNSTART)|(1<<LNDONE));
/* remove prohibited types according to inside */
types &= ~(inside ? (1<<LNSTART)|(1<<NOTE)|(1<<BOMB) : (1<<LNDONE));
/* invisible note cannot overlap with long note endpoints */
if (types & ((1<<LNSTART)|(1<<LNDONE))) types &= ~(1<<INVNOTE);
/* keep the most important (lowest) type, except for BOMB/INVNOTE combination */
lowest = types & -types;
types = lowest | (lowest == (1<<INVNOTE) ? types & (1<<BOMB) : 0);
inside = (types & (1<<LNSTART) ? 1 : types & (1<<LNDONE) ? 0 : inside);
for (; j < k; ++j) if (objs[j].chan == i) {
if (IS_NOTE_CHANNEL(i) && !(types & (1 << objs[j].type))) {
remove_or_replace_note(j);
}
}
}
if (IS_NOTE_CHANNEL(i) && inside) {
/* remove last starting longnote which is unfinished */
for (j = nobjs - 1; j >= 0 && objs[j].chan != i; --j);
if (j >= 0 && objs[j].type == LNSTART) remove_or_replace_note(j);
}
}
}
static const char *detect_preset(const char *preset)
{
int present[NNOTECHANS] = {0};
for (int i = 0; i < nobjs; ++i) {
if (IS_NOTE_CHANNEL(objs[i].chan)) present[objs[i].chan] = 1;
}
if (!preset || strieq(preset, "bms") || strieq(preset, "bme") || strieq(preset, "bml")) {
int isbme = (present[8] || present[9] || present[36+8] || present[36+9]);
int haspedal = (present[7] || present[36+7]);
if (value[V_PLAYER] == 2 || value[V_PLAYER] == 3) {
preset = (isbme ? (haspedal ? "14/fp" : "14") : (haspedal ? "10/fp" : "10"));
} else {
preset = (isbme ? (haspedal ? "7/fp" : "7") : (haspedal ? "5/fp" : "5"));
}
} else if (strieq(preset, "pms")) {
preset = (present[6] || present[7] || present[8] || present[9] ? "9-bme" : "9");
}
return preset;
}
#define KEYKIND_IS_KEY(kind) ((kind) >= 1 && (kind) <= 8) /* no scratch nor pedal */
static int parse_key_spec(const char *s, int offset)
{
s += strspn(s, " \t\r\n");
do {
int chan, kind;
if (!s[0] || !key2index(s, &chan)) return -1;
chan -= 1*36;
if (chan < 0 || chan >= NNOTECHANS || keykind[chan]) return -1;
for (kind = 1; KEYKIND_MNEMONICS[kind] && KEYKIND_MNEMONICS[kind] != s[2]; ++kind);
if (!KEYKIND_MNEMONICS[kind]) return -1;
s += 3 + strspn(s + 3, " \t\r\n");
keyorder[offset++] = chan;
keykind[chan] = kind;
if (KEYKIND_IS_KEY(kind)) ++nkeys;
} while (*s);
return offset;
}
static void analyze_and_compact_bms(const char *left, const char *right)
{
int i, j;
if (!left) die("No key model is specified using -k or -K");
nleftkeys = parse_key_spec(left, 0);
if (nleftkeys < 0) die("Invalid key spec for left hand side: %s", left);
if (right && *right) {
nrightkeys = parse_key_spec(right, nleftkeys);
if (nrightkeys < 0) die("Invalid key spec for right hand side: %s", right);
if (value[V_PLAYER] != 2) nleftkeys = nrightkeys; /* no split panes */
}
hasbpmchange = haslongnote = nnotes = maxscore = 0;
for (i = 0; i < nobjs; ++i) {
if (IS_NOTE_CHANNEL(objs[i].chan)) {
if (keykind[objs[i].chan]) {
if (objs[i].type == LNSTART) haslongnote = 1;
if (objs[i].type == LNSTART || objs[i].type == NOTE) {
++nnotes;
if (objs[i].time < 1.0) originoffset = -1.0;
}
} else {
remove_or_replace_note(i);
}
} else if (objs[i].chan == BPM_CHANNEL) {
hasbpmchange = 1;
}
}
for (i = 0; i < nnotes; ++i) {
maxscore += (int)(300 * (1 + 1. * i / nnotes));
}
for (i = j = 0; i < nobjs; ++i) {
if (objs[i].chan >= 0) objs[i-j] = objs[i]; else ++j;
}
nobjs -= j;
}
static double adjust_object_time(double base, double offset)
{
int i = (int)(base+1)-1;
if ((i + 1 - base) * shorten(i) > offset) return base + offset / shorten(i);
offset -= (i + 1 - base) * shorten(i);
while (shorten(++i) <= offset) offset -= shorten(i);
return i + offset / shorten(i);
}
static double adjust_object_position(double base, double time)
{
int i = (int)(base+1)-1, j = (int)(time+1)-1;
base = (time - j) * shorten(j) - (base - i) * shorten(i);
while (i < j) base += shorten(i++);
return base;
}
static int get_bms_duration(void)
{
double pos = originoffset, bpm = initbpm, time = 0, sndtime = 0;
for (int i = 0; i < nobjs; ++i) {
int chan = objs[i].chan, type = objs[i].type, index = objs[i].index;
double sndlen = 0;
time += MEASURE_TO_MSEC(adjust_object_position(pos, objs[i].time), bpm);
if ((IS_NOTE_CHANNEL(chan) && (type == NOTE || type == LNSTART)) || chan == BGM_CHANNEL) {
if (index && sndres[index].res) sndlen = sndres[index].res->alen / 176.4;
} else if (chan == BPM_CHANNEL) {
double tmp = (type == BPM_BY_INDEX ? bpmtab[index] : index);
if (tmp > 0) {
bpm = tmp;
} else if (tmp < 0) {
time += MEASURE_TO_MSEC(adjust_object_position(originoffset, pos), -tmp);
goto earlyexit;
}
} else if (chan == STOP_CHANNEL) {
time += (type == STOP_BY_MSEC ? index : MEASURE_TO_MSEC(stoptab[index], bpm));
}
if (sndtime < time + sndlen) sndtime = time + sndlen;
pos = objs[i].time;
}
time += MEASURE_TO_MSEC(adjust_object_position(pos, length), bpm);
earlyexit: /* choose the last note or all key sound ceases to play, whichever comes later */
return (int) (time > sndtime ? time : sndtime);
}
enum modf { NO_MODF, MIRROR_MODF, SHUFFLE_MODF, SHUFFLEEX_MODF, RANDOM_MODF, RANDOMEX_MODF };
static void shuffle_bms(enum modf mode, struct rngstate *r, int begin, int end)
{
int perm[NCHANS], map[NNOTECHANS], nmap = 0;
for (int i = 0; i < ARRAYSIZE(perm); ++i) perm[i] = i;
for (int i = begin; i < end; ++i) {
int chan = keyorder[i];
if (mode != SHUFFLEEX_MODF && mode != RANDOMEX_MODF && !KEYKIND_IS_KEY(keykind[chan])) continue;
map[nmap++] = chan;
}
#define SWAP(x,y) do { int temp = (x); (x) = (y); (y) = temp; } while (0)
if (mode <= MIRROR_MODF) { /* mirror */
for (int i = 0, j = nmap-1; i < j; ++i, --j) SWAP(perm[map[i]], perm[map[j]]);
for (int i = 0; i < nobjs; ++i) objs[i].chan = perm[objs[i].chan];
} else if (mode <= SHUFFLEEX_MODF) { /* shuffle */
for (int i = nmap-1; i > 0; --i) {
int other = rng_gen(r, i);
SWAP(perm[map[i]], perm[map[other]]);
}
for (int i = 0; i < nobjs; ++i) objs[i].chan = perm[objs[i].chan];
} else if (mode <= RANDOMEX_MODF) { /* random */
double lasttime = DBL_MIN;
for (int i = 0; i < nobjs; ++i) {
if (IS_NOTE_CHANNEL(objs[i].chan) && objs[i].type == LNSTART) {
int j;
for (j = 0; map[j] != objs[i].chan; ++j);
map[j] = map[--nmap];
}
if (lasttime < objs[i].time) { /* reshuffle required */
lasttime = objs[i].time + 1e-4;
for (int j = nmap-1; j > 0; --j) {
int other = rng_gen(r, j);
SWAP(perm[map[j]], perm[map[other]]);
}
}
if (IS_NOTE_CHANNEL(objs[i].chan) && objs[i].type == LNDONE) {
map[nmap++] = objs[i].chan;
}
objs[i].chan = perm[objs[i].chan];
}
}
}
/******************************************************************************/
/* general graphic functions and font functions */
static SDL_Window *screen;
static SDL_Renderer *renderer;
static Uint16 fontdata[3072];
static Uint8 (*zoomfont[16])[96] = {NULL};
static int putpixel(SDL_Surface *s, int x, int y, int c)
{
c = SDL_MapRGB(s->format, c >> 16, (c >> 8) & 255, c & 255);
return ((Uint32*)s->pixels)[x+y*s->pitch/4] = c;
}
static int blend(int x, int y, int a, int b)
{
for (int i = 0; i < 24; i += 8) {
y += ((x>>i&255) - (y>>i&255))*a/b << i;
}
return y;
}
static void fontdecompress(void)
{
int words[] = /* delta coded words */
{0, 0, 2, 6, 2, 5, 32, 96, 97, 15, 497, 15, 1521, 15, 1537, 16, 48,
176, 1, 3, 1, 3, 7, 1, 4080, 4096, 3, 1, 8, 3, 4097, 4080, 16,
16128, 240, 1, 2, 9, 3, 8177, 15, 16385, 240, 15, 1, 47, 721, 143,
2673, 2, 6, 7, 1, 31, 17, 16, 63, 64, 33, 0, 1, 2, 1, 8, 3};
const char *indices = /* LZ77-compressed indices to words */
"!!7a/&/&s$7a!f!'M*Q*Qc$(O&J!!&J&Jc(e!2Q2Qc$-Bg2m!2bB[Q7Q2[e&2Q!Qi>"
"&!&!>UT2T2&2>WT!c*T2GWc8icM2U2D!.8(M$UQCQ-jab!'U*2*2*2TXbZ252>9ZWk"
"@*!*!*8(J$JlWi@cxQ!Q!d$#Q'O*?k@e2dfejcNl!&JTLTLG_&J>]c*&Jm@cB&J&J7"
"[e(o>pJM$Qs<7[{Zj`Jm40!3!.8(M$U!C!-oR>UQ2U2]2a9Y[S[QCQ2GWk@*M*Q*B*"
"!*!g$aQs`G8.M(U$[!Ca[o@Q2Q!IJQ!Q!c,GWk@787M6U2C2d!a[2!2k?!bnc32>[u"
"`>Uc4d@b(q@abXU!D!.8(J&J&d$q`Q2IXu`g@Q2aWQ!q@!!ktk,x@M$Qk@3!.8(M$U"
"!H#W'O,?4m_f!7[i&n!:eX5ghCk=>UQ2Q2U2Dc>J!!&J&b&k@J)LKg!GK!)7Wk@'8,"
"M=UWCcfa[c&Q2l`f4If(Q2G[l@MSUQC!2!2c$Q:RWGOk@,[<2WfZQ2U2D2.l`a[eZ7"
"f(!2b2|@b$j!>MSUQCc6[2W2Q:RWGOk@Q2Q2c$a[g*Ql`7[&J&Jk$7[l`!Qi$d^GWk"
"@U2D2.9([$[#['[,@<2W2k@!2!2m$a[l`:^[a[a[T2Td~c$k@d2:R[V[a@_b|o@,M="
"UWCgZU:EW.Ok@>[g<G[!2!2d$k@Ug@Q2V2a2IW_!Wt`Ih*q`!2>WQ!Q!c,Gk_!7[&J"
"&Jm$k@gti$m`k:U:EW.O(?s@T2Tb$a[CW2Qk@M+U:^[GbX,M>U`[WCO-l@'U,D<.W("
"O&J&Je$k@a[Q!U!]!G8.M(U$[!Ca[k@*Q!Q!l$b2m!+!:#W'O,?4!1n;c`*!*!l$h`"
"'8,M=UWCO-pWz!a[i,#Q'O,?4~R>QQ!Q!aUQ2Q2Q2aWl=2!2!2>[e<c$G[p`dZcHd@"
"l`czi|c$al@i`b:[!2Un`>8TJTJ&J7[&b&e$o`i~aWQ!c(hd2!2!2>[g@e$k]epi|e"
"0i!bph(d$dbGWhA2!2U2D2.9(['[,@<2W2k`*J*?*!*!k$o!;[a[T2T2c$c~o@>[c6"
"i$p@Uk>GW}`G[!2!2b$h!al`aWQ!Q!Qp`fVlZf@UWb6>eX:GWk<&J&J7[c&&JTJTb$"
"G?o`c~i$m`k@U:EW.O(v`T2Tb$a[Fp`M+eZ,M=UWCO-u`Q:RWGO.A(M$U!Ck@a[]!G"
"8.M(U$[!Ca[i:78&J&Jc$%[g*7?e<g0w$cD#iVAg*$[g~dB]NaaPGft~!f!7[.W(O";
int i, c = 0, d;
for (i = 0; i < ARRAYSIZE(words); ++i) {
c += words[i];
words[i] = c;
}
for (i = 0; (c = *indices++); ) {
if (c >= 98) {
for (d = *indices++ - 32; c-- >= 96; ++i) fontdata[i] = fontdata[i-d];
} else if (c >= 33) {
fontdata[i++] = words[c - 33];
}
}
}
static void fontprocess(int z)
{
int i, j, k, l, v, p, q, f;
zoomfont[z] = calloc(16*z*z, 96);
for (i = l = 0; i < 96; ++i) {
for (j = 0; j < 16; ++j, l+=2) {
v = fontdata[l] << 16 | fontdata[l+1];
for (k = 0; k < 8; ++k, v >>= 4) {
for (p = 0; p < z; ++p) {
for (q = 0; q < z; ++q) {
f = (p+q>=z) | (p>q)<<1 | (p<q)<<2 | (p+q<z-1)<<3;
if ((v&f) || (v&15)==15) { /* 1 /|, 2 |\, 4 \|, 8 |/, 15 square */
zoomfont[z][(j*z+p)*z+q][i] |= 1<<(7-k);
}
}
}
}
}
}
}
static void printchar(int x, int y, int z, int c, int u, int v)
{
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') return;
c -= (c<0 ? -96 : c<33 || c>126 ? c : 32);
for (int i = 0; i < 16*z; ++i) {
Uint32 blended = blend(u, v, i, 16*z-1);
SDL_SetRenderDrawColor(renderer, blended >> 16, blended >> 8, blended, 255);
for (int j = 0; j < 8*z; ++j) {
if (zoomfont[z][i*z+j%z][c] & (1<<(7-j/z))) SDL_RenderDrawPoint(renderer, x+j, y+i);
}
}
}
static void printstr(int x, int y, int z, int a, const char *c, int u, int v)
{
if (a) x -= strlen(c) * a * (4*z);
for (; *c; x += 8*z) printchar(x, y, z, (Uint8)*c++, u, v);
}
/******************************************************************************/
/* resource loading */
static void update_movie(void *data, SMPEG_Frame *frame)
{
((struct imgres *)data)->frame = frame;
}
enum bga { BGA_AND_MOVIE, BGA_BUT_NO_MOVIE, NO_BGA };
static void load_resource(enum bga range, void (*resource_loaded)(const char *path))
{
SDL_RWops *rwops;
for (int i = 0; i < MAXKEY; ++i) {
sndres[i].lastch = -1;
if (sndpath[i]) {
resource_loaded(sndpath[i]);
rwops = resolve_relative_path(sndpath[i], SOUND_EXTS);
if (rwops) sndres[i].res = Mix_LoadWAV_RW(rwops, 1);
if (!sndres[i].res) {
warn("failed to load sound #WAV%s (%s)", TO_KEY(i), sndpath[i]);
}
free(sndpath[i]);
sndpath[i] = 0;
}
if (imgpath[i]) {
resource_loaded(imgpath[i]);
if (strisuffix(imgpath[i], ".mpg")) {
if (range < BGA_BUT_NO_MOVIE) {
SMPEG *movie = NULL;
rwops = resolve_relative_path(imgpath[i], NULL);
if (rwops) movie = SMPEG_new_rwops(rwops, NULL, 1, 0);
if (!movie) {
warn("failed to load image #BMP%s (%s)", TO_KEY(i), imgpath[i]);
} else {
imgres[i].tex = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_YV12, SDL_TEXTUREACCESS_STREAMING, 256, 256);
imgres[i].movie = movie;
SMPEG_enablevideo(movie, 1);
SMPEG_loop(movie, 1);
SMPEG_setdisplay(movie, update_movie, &imgres[i], NULL);
}
}