Browse Source

generate a const shot speed table to save lots of rom bank 0 space

Thomas B 3 weeks ago
parent
commit
421b25de97
4 changed files with 130 additions and 37 deletions
  1. 8
    1
      Makefile
  2. 10
    34
      src/game.c
  3. 11
    2
      src/main.c
  4. 101
    0
      util/gen_angles.py

+ 8
- 1
Makefile View File

30
 GIT := $(BUILD_DIR)/$(DATA_DIR)/git.c
30
 GIT := $(BUILD_DIR)/$(DATA_DIR)/git.c
31
 SRCS += $(GIT)
31
 SRCS += $(GIT)
32
 
32
 
33
+SPEED_TABLE := $(BUILD_DIR)/$(DATA_DIR)/speed_table.c
34
+SRCS += $(SPEED_TABLE)
35
+
33
 OBJS := $(SRCS:%.c=$(BUILD_DIR)/%.o)
36
 OBJS := $(SRCS:%.c=$(BUILD_DIR)/%.o)
34
 
37
 
35
 IMAGES := $(wildcard $(DATA_DIR)/*.png)
38
 IMAGES := $(wildcard $(DATA_DIR)/*.png)
78
 DEPS=$(OBJS:%.o=%.d)
81
 DEPS=$(OBJS:%.o=%.d)
79
 -include $(DEPS)
82
 -include $(DEPS)
80
 
83
 
81
-.PHONY: all run cloc sgb_run bgb_run gbe_run flash clean compile_commands.json usage $(GIT)
84
+.PHONY: all run cloc sgb_run bgb_run gbe_run flash clean compile_commands.json usage $(GIT) $(SPEED_TABLE)
82
 .PRECIOUS: $(BUILD_DIR)/$(DATA_DIR)/%.c $(BUILD_DIR)/$(DATA_DIR)/%.h
85
 .PRECIOUS: $(BUILD_DIR)/$(DATA_DIR)/%.c $(BUILD_DIR)/$(DATA_DIR)/%.h
83
 
86
 
84
 all: $(BIN)
87
 all: $(BIN)
99
 	@echo Generating $@ from $<
102
 	@echo Generating $@ from $<
100
 	@sed 's|GIT_VERSION|$(shell git describe --abbrev=7 --dirty --always --tags)|g' $< > $@
103
 	@sed 's|GIT_VERSION|$(shell git describe --abbrev=7 --dirty --always --tags)|g' $< > $@
101
 
104
 
105
+$(SPEED_TABLE):
106
+	@echo Generating $@
107
+	@util/gen_angles.py -n speed_table -d $(BUILD_DIR)/$(DATA_DIR) -s 16 -w 2 -f 0 -m 42 -t int8_t
108
+
102
 usage: $(BUILD_DIR)/$(BIN)
109
 usage: $(BUILD_DIR)/$(BIN)
103
 	@echo Analyzing $<
110
 	@echo Analyzing $<
104
 	@$(ROMU) $(BUILD_DIR)/$(BIN:%.gb=%.map)
111
 	@$(ROMU) $(BUILD_DIR)/$(BIN:%.gb=%.map)

+ 10
- 34
src/game.c View File

22
 #include <rand.h>
22
 #include <rand.h>
23
 #include <stdint.h>
23
 #include <stdint.h>
24
 
24
 
25
+#include "banks.h"
25
 #include "config.h"
26
 #include "config.h"
26
 #include "maps.h"
27
 #include "maps.h"
27
 #include "obj.h"
28
 #include "obj.h"
32
 #include "sample.h"
33
 #include "sample.h"
33
 #include "window.h"
34
 #include "window.h"
34
 #include "multiplayer.h"
35
 #include "multiplayer.h"
36
+#include "speed_table.h"
35
 #include "game.h"
37
 #include "game.h"
36
 
38
 
37
 #define BAR_OFFSET_X (4 - 80)
39
 #define BAR_OFFSET_X (4 - 80)
514
         }
516
         }
515
 
517
 
516
         if (key_pressed(J_B)) {
518
         if (key_pressed(J_B)) {
519
+            int16_t shot_spd_x;
520
+            int16_t shot_spd_y;
521
+            START_ROM_BANK(BANK(speed_table)) {
522
+                shot_spd_x = spd_x + speed_table[(rot * speed_table_WIDTH) + 0];
523
+                shot_spd_y = spd_y - speed_table[(rot * speed_table_WIDTH) + 1];
524
+            } END_ROM_BANK;
525
+
526
+            // TODO ugly hard-coded offsets?!
517
             int16_t shot_pos_x = 0, shot_pos_y = 0;
527
             int16_t shot_pos_x = 0, shot_pos_y = 0;
518
-            int16_t shot_spd_x = 0, shot_spd_y = 0;
519
-
520
             switch (rot) {
528
             switch (rot) {
521
                 case ROT_0:
529
                 case ROT_0:
522
                     shot_pos_x = 0;
530
                     shot_pos_x = 0;
523
                     shot_pos_y = -SHIP_OFF;
531
                     shot_pos_y = -SHIP_OFF;
524
-                    shot_spd_x = spd_x;
525
-                    shot_spd_y = spd_y - SHOT_SPEED;
526
                     break;
532
                     break;
527
 
533
 
528
                 case ROT_22_5:
534
                 case ROT_22_5:
529
                     shot_pos_x = SHIP_OFF / 2 - 1;
535
                     shot_pos_x = SHIP_OFF / 2 - 1;
530
                     shot_pos_y = -SHIP_OFF / 2 - 4;
536
                     shot_pos_y = -SHIP_OFF / 2 - 4;
531
-                    shot_spd_x = spd_x + SHOT_SPEED_D_LO;
532
-                    shot_spd_y = spd_y - SHOT_SPEED_D_HI;
533
                     break;
537
                     break;
534
 
538
 
535
                 case ROT_45:
539
                 case ROT_45:
536
                     shot_pos_x = SHIP_OFF / 2 + 3;
540
                     shot_pos_x = SHIP_OFF / 2 + 3;
537
                     shot_pos_y = -SHIP_OFF / 2 - 2;
541
                     shot_pos_y = -SHIP_OFF / 2 - 2;
538
-                    shot_spd_x = spd_x + SHOT_SPEED_DIAG;
539
-                    shot_spd_y = spd_y - SHOT_SPEED_DIAG;
540
                     break;
542
                     break;
541
 
543
 
542
                 case ROT_67_5:
544
                 case ROT_67_5:
543
                     shot_pos_x = SHIP_OFF / 2 + 5;
545
                     shot_pos_x = SHIP_OFF / 2 + 5;
544
                     shot_pos_y = -SHIP_OFF / 2 + 2;
546
                     shot_pos_y = -SHIP_OFF / 2 + 2;
545
-                    shot_spd_x = spd_x + SHOT_SPEED_D_HI;
546
-                    shot_spd_y = spd_y - SHOT_SPEED_D_LO;
547
                     break;
547
                     break;
548
 
548
 
549
                 case ROT_90:
549
                 case ROT_90:
550
                     shot_pos_x = SHIP_OFF;
550
                     shot_pos_x = SHIP_OFF;
551
                     shot_pos_y = 0;
551
                     shot_pos_y = 0;
552
-                    shot_spd_x = spd_x + SHOT_SPEED;
553
-                    shot_spd_y = spd_y;
554
                     break;
552
                     break;
555
 
553
 
556
                 case ROT_112_5:
554
                 case ROT_112_5:
557
                     shot_pos_x = SHIP_OFF / 2 + 5;
555
                     shot_pos_x = SHIP_OFF / 2 + 5;
558
                     shot_pos_y = SHIP_OFF / 2 + 0;
556
                     shot_pos_y = SHIP_OFF / 2 + 0;
559
-                    shot_spd_x = spd_x + SHOT_SPEED_D_HI;
560
-                    shot_spd_y = spd_y + SHOT_SPEED_D_LO;
561
                     break;
557
                     break;
562
 
558
 
563
                 case ROT_135:
559
                 case ROT_135:
564
                     shot_pos_x = SHIP_OFF / 2 + 3;
560
                     shot_pos_x = SHIP_OFF / 2 + 3;
565
                     shot_pos_y = SHIP_OFF / 2 + 2;
561
                     shot_pos_y = SHIP_OFF / 2 + 2;
566
-                    shot_spd_x = spd_x + SHOT_SPEED_DIAG;
567
-                    shot_spd_y = spd_y + SHOT_SPEED_DIAG;
568
                     break;
562
                     break;
569
 
563
 
570
                 case ROT_157_5:
564
                 case ROT_157_5:
571
                     shot_pos_x = SHIP_OFF / 2 + 1;
565
                     shot_pos_x = SHIP_OFF / 2 + 1;
572
                     shot_pos_y = SHIP_OFF / 2 + 4;
566
                     shot_pos_y = SHIP_OFF / 2 + 4;
573
-                    shot_spd_x = spd_x + SHOT_SPEED_D_LO;
574
-                    shot_spd_y = spd_y + SHOT_SPEED_D_HI;
575
                     break;
567
                     break;
576
 
568
 
577
                 case ROT_180:
569
                 case ROT_180:
578
                     shot_pos_x = 0;
570
                     shot_pos_x = 0;
579
                     shot_pos_y = SHIP_OFF;
571
                     shot_pos_y = SHIP_OFF;
580
-                    shot_spd_x = spd_x;
581
-                    shot_spd_y = spd_y + SHOT_SPEED;
582
                     break;
572
                     break;
583
 
573
 
584
                 case ROT_202_5:
574
                 case ROT_202_5:
585
                     shot_pos_x = -SHIP_OFF / 2 + 2;
575
                     shot_pos_x = -SHIP_OFF / 2 + 2;
586
                     shot_pos_y = SHIP_OFF / 2 + 3;
576
                     shot_pos_y = SHIP_OFF / 2 + 3;
587
-                    shot_spd_x = spd_x - SHOT_SPEED_D_LO;
588
-                    shot_spd_y = spd_y + SHOT_SPEED_D_HI;
589
                     break;
577
                     break;
590
 
578
 
591
                 case ROT_225:
579
                 case ROT_225:
592
                     shot_pos_x = -SHIP_OFF / 2 - 3;
580
                     shot_pos_x = -SHIP_OFF / 2 - 3;
593
                     shot_pos_y = SHIP_OFF / 2 + 2;
581
                     shot_pos_y = SHIP_OFF / 2 + 2;
594
-                    shot_spd_x = spd_x - SHOT_SPEED_DIAG;
595
-                    shot_spd_y = spd_y + SHOT_SPEED_DIAG;
596
                     break;
582
                     break;
597
 
583
 
598
                 case ROT_247_5:
584
                 case ROT_247_5:
599
                     shot_pos_x = -SHIP_OFF / 2 - 5;
585
                     shot_pos_x = -SHIP_OFF / 2 - 5;
600
                     shot_pos_y = SHIP_OFF / 2 - 1;
586
                     shot_pos_y = SHIP_OFF / 2 - 1;
601
-                    shot_spd_x = spd_x - SHOT_SPEED_D_HI;
602
-                    shot_spd_y = spd_y + SHOT_SPEED_D_LO;
603
                     break;
587
                     break;
604
 
588
 
605
                 case ROT_270:
589
                 case ROT_270:
606
                     shot_pos_x = -SHIP_OFF;
590
                     shot_pos_x = -SHIP_OFF;
607
                     shot_pos_y = 0;
591
                     shot_pos_y = 0;
608
-                    shot_spd_x = spd_x - SHOT_SPEED;
609
-                    shot_spd_y = spd_y;
610
                     break;
592
                     break;
611
 
593
 
612
                 case ROT_292_5:
594
                 case ROT_292_5:
613
                     shot_pos_x = -SHIP_OFF / 2 - 2;
595
                     shot_pos_x = -SHIP_OFF / 2 - 2;
614
                     shot_pos_y = -SHIP_OFF / 2 + 2;
596
                     shot_pos_y = -SHIP_OFF / 2 + 2;
615
-                    shot_spd_x = spd_x - SHOT_SPEED_D_HI;
616
-                    shot_spd_y = spd_y - SHOT_SPEED_D_LO;
617
                     break;
597
                     break;
618
 
598
 
619
                 case ROT_315:
599
                 case ROT_315:
620
                     shot_pos_x = -SHIP_OFF / 2 - 3;
600
                     shot_pos_x = -SHIP_OFF / 2 - 3;
621
                     shot_pos_y = -SHIP_OFF / 2 - 2;
601
                     shot_pos_y = -SHIP_OFF / 2 - 2;
622
-                    shot_spd_x = spd_x - SHOT_SPEED_DIAG;
623
-                    shot_spd_y = spd_y - SHOT_SPEED_DIAG;
624
                     break;
602
                     break;
625
 
603
 
626
                 case ROT_337_5:
604
                 case ROT_337_5:
627
                     shot_pos_x = -SHIP_OFF / 2 + 1;
605
                     shot_pos_x = -SHIP_OFF / 2 + 1;
628
                     shot_pos_y = -SHIP_OFF / 2 - 4;
606
                     shot_pos_y = -SHIP_OFF / 2 - 4;
629
-                    shot_spd_x = spd_x - SHOT_SPEED_D_LO;
630
-                    shot_spd_y = spd_y - SHOT_SPEED_D_HI;
631
                     break;
607
                     break;
632
             }
608
             }
633
 
609
 

+ 11
- 2
src/main.c View File

40
 #include "window.h"
40
 #include "window.h"
41
 #include "gbprinter.h"
41
 #include "gbprinter.h"
42
 #include "multiplayer.h"
42
 #include "multiplayer.h"
43
+#include "speed_table.h"
43
 #include "main.h"
44
 #include "main.h"
44
 
45
 
45
 uint8_t debug_menu_index = 0;
46
 uint8_t debug_menu_index = 0;
259
     switch (anim_state) {
260
     switch (anim_state) {
260
         case 1:
261
         case 1:
261
             if (anim_frame == 0) {
262
             if (anim_frame == 0) {
262
-                obj_add(SPR_SHOT, SHIP_OFF, -42, SHOT_SPEED, 0);
263
+                START_ROM_BANK(BANK(speed_table)) {
264
+                    obj_add(SPR_SHOT, SHIP_OFF, -42,
265
+                            speed_table[(ROT_90 * speed_table_WIDTH) + 0],
266
+                            -speed_table[(ROT_90 * speed_table_WIDTH) + 1]);
267
+                } END_ROM_BANK;
263
                 sample_play(SFX_SHOT);
268
                 sample_play(SFX_SHOT);
264
             }
269
             }
265
         case 0:
270
         case 0:
290
 
295
 
291
         case 7:
296
         case 7:
292
             if (anim_frame == 0) {
297
             if (anim_frame == 0) {
293
-                obj_add(SPR_SHOT, -SHIP_OFF, -42, -SHOT_SPEED, 0);
298
+                START_ROM_BANK(BANK(speed_table)) {
299
+                    obj_add(SPR_SHOT, -SHIP_OFF, -42,
300
+                            speed_table[(ROT_270 * speed_table_WIDTH) + 0],
301
+                            -speed_table[(ROT_270 * speed_table_WIDTH) + 1]);
302
+                } END_ROM_BANK;
294
                 sample_play(SFX_SHOT);
303
                 sample_play(SFX_SHOT);
295
             }
304
             }
296
         case 6:
305
         case 6:

+ 101
- 0
util/gen_angles.py View File

1
+#!/usr/bin/env python3
2
+
3
+import sys
4
+import os
5
+import math
6
+import argparse
7
+
8
+sGBDK = """//AUTOGENERATED FILE FROM {:s}
9
+#include <stdint.h>
10
+#include <gbdk/platform.h>
11
+#include "{:s}.h"
12
+BANKREF({:s})
13
+const {:s} {:s}[{:s}_SIZE] = {{
14
+{:s}}};
15
+"""
16
+
17
+hGBDK = """//AUTOGENERATED FILE FROM {:s}
18
+#ifndef GEN_CONST_ANGLES_{:s}_H
19
+#define GEN_CONST_ANGLES_{:s}_H
20
+#include <stdint.h>
21
+#include <gbdk/platform.h>
22
+
23
+#define {:s}_WIDTH {:d}
24
+#define {:s}_SIZE {:d}
25
+extern const {:s} {:s}[{:s}_SIZE];
26
+
27
+BANKREF_EXTERN({:s})
28
+#endif
29
+"""
30
+
31
+def calc(args):
32
+    s = ""
33
+
34
+    for i in range(0, args.steps):
35
+        angle = 360 / args.steps * i
36
+        sin_a = math.sin(math.radians(angle)) * args.max
37
+        cos_a = math.cos(math.radians(angle)) * args.max
38
+
39
+        for n in range(0, args.shift):
40
+            sin_a *= 2
41
+            cos_a *= 2
42
+
43
+        if args.width > 0:
44
+            s += f"    {round(sin_a)},"
45
+
46
+        if args.width > 1:
47
+            s += f" {round(cos_a)},"
48
+
49
+        s += f" // {angle}\n"
50
+
51
+    return (s, args.steps * args.width)
52
+
53
+def main(args):
54
+    outheader = os.path.join(args.dir, f"{args.name}.h")
55
+    outsource = os.path.join(args.dir, f"{args.name}.c")
56
+
57
+    data, length = calc(args)
58
+
59
+    source = sGBDK.format(sys.argv[0],
60
+                          args.name, args.name,
61
+                          args.type,
62
+                          args.name, args.name,
63
+                          data)
64
+
65
+    if args.verbose:
66
+        print(f"Source: {outsource}")
67
+        print(source)
68
+
69
+    if not args.dry_run:
70
+        with open(outsource, "w") as o:
71
+            o.write(source)
72
+
73
+    header = hGBDK.format(sys.argv[0],
74
+                          args.name, args.name, args.name,
75
+                          args.width,
76
+                          args.name,
77
+                          length, args.type,
78
+                          args.name, args.name, args.name)
79
+
80
+    if args.verbose:
81
+        print(f"Header: {outheader}")
82
+        print(header)
83
+
84
+    if not args.dry_run:
85
+        with open(outheader, "w") as o:
86
+            o.write(header)
87
+
88
+if __name__=='__main__':
89
+    parser = argparse.ArgumentParser()
90
+    parser.add_argument("-d", "--dir", default=os.path.realpath("."))
91
+    parser.add_argument("-n", "--name", required=True) #default="angles")
92
+    parser.add_argument("-s", "--steps", default=16, type=int)
93
+    parser.add_argument("-w", "--width", default=2, type=int)
94
+    parser.add_argument("-f", "--shift", default=0, type=int)
95
+    parser.add_argument("-m", "--max", default=42, type=int)
96
+    parser.add_argument("-t", "--type", default="int8_t")
97
+    parser.add_argument("-v", "--verbose", action="store_true")
98
+    parser.add_argument("-y", "--dry-run", action="store_true")
99
+    args = parser.parse_args()
100
+    #print(args)
101
+    main(args)

Loading…
Cancel
Save