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,6 +30,9 @@ SRCS := $(wildcard $(SRC_DIR)/*.c)
30 30
 GIT := $(BUILD_DIR)/$(DATA_DIR)/git.c
31 31
 SRCS += $(GIT)
32 32
 
33
+SPEED_TABLE := $(BUILD_DIR)/$(DATA_DIR)/speed_table.c
34
+SRCS += $(SPEED_TABLE)
35
+
33 36
 OBJS := $(SRCS:%.c=$(BUILD_DIR)/%.o)
34 37
 
35 38
 IMAGES := $(wildcard $(DATA_DIR)/*.png)
@@ -78,7 +81,7 @@ $(info BUILD_TYPE is $(BUILD_TYPE))
78 81
 DEPS=$(OBJS:%.o=%.d)
79 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 85
 .PRECIOUS: $(BUILD_DIR)/$(DATA_DIR)/%.c $(BUILD_DIR)/$(DATA_DIR)/%.h
83 86
 
84 87
 all: $(BIN)
@@ -99,6 +102,10 @@ $(GIT): $(DATA_DIR)/git.c
99 102
 	@echo Generating $@ from $<
100 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 109
 usage: $(BUILD_DIR)/$(BIN)
103 110
 	@echo Analyzing $<
104 111
 	@$(ROMU) $(BUILD_DIR)/$(BIN:%.gb=%.map)

+ 10
- 34
src/game.c View File

@@ -22,6 +22,7 @@
22 22
 #include <rand.h>
23 23
 #include <stdint.h>
24 24
 
25
+#include "banks.h"
25 26
 #include "config.h"
26 27
 #include "maps.h"
27 28
 #include "obj.h"
@@ -32,6 +33,7 @@
32 33
 #include "sample.h"
33 34
 #include "window.h"
34 35
 #include "multiplayer.h"
36
+#include "speed_table.h"
35 37
 #include "game.h"
36 38
 
37 39
 #define BAR_OFFSET_X (4 - 80)
@@ -514,120 +516,94 @@ int32_t game(enum GAME_MODE mode) NONBANKED {
514 516
         }
515 517
 
516 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 527
             int16_t shot_pos_x = 0, shot_pos_y = 0;
518
-            int16_t shot_spd_x = 0, shot_spd_y = 0;
519
-
520 528
             switch (rot) {
521 529
                 case ROT_0:
522 530
                     shot_pos_x = 0;
523 531
                     shot_pos_y = -SHIP_OFF;
524
-                    shot_spd_x = spd_x;
525
-                    shot_spd_y = spd_y - SHOT_SPEED;
526 532
                     break;
527 533
 
528 534
                 case ROT_22_5:
529 535
                     shot_pos_x = SHIP_OFF / 2 - 1;
530 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 537
                     break;
534 538
 
535 539
                 case ROT_45:
536 540
                     shot_pos_x = SHIP_OFF / 2 + 3;
537 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 542
                     break;
541 543
 
542 544
                 case ROT_67_5:
543 545
                     shot_pos_x = SHIP_OFF / 2 + 5;
544 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 547
                     break;
548 548
 
549 549
                 case ROT_90:
550 550
                     shot_pos_x = SHIP_OFF;
551 551
                     shot_pos_y = 0;
552
-                    shot_spd_x = spd_x + SHOT_SPEED;
553
-                    shot_spd_y = spd_y;
554 552
                     break;
555 553
 
556 554
                 case ROT_112_5:
557 555
                     shot_pos_x = SHIP_OFF / 2 + 5;
558 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 557
                     break;
562 558
 
563 559
                 case ROT_135:
564 560
                     shot_pos_x = SHIP_OFF / 2 + 3;
565 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 562
                     break;
569 563
 
570 564
                 case ROT_157_5:
571 565
                     shot_pos_x = SHIP_OFF / 2 + 1;
572 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 567
                     break;
576 568
 
577 569
                 case ROT_180:
578 570
                     shot_pos_x = 0;
579 571
                     shot_pos_y = SHIP_OFF;
580
-                    shot_spd_x = spd_x;
581
-                    shot_spd_y = spd_y + SHOT_SPEED;
582 572
                     break;
583 573
 
584 574
                 case ROT_202_5:
585 575
                     shot_pos_x = -SHIP_OFF / 2 + 2;
586 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 577
                     break;
590 578
 
591 579
                 case ROT_225:
592 580
                     shot_pos_x = -SHIP_OFF / 2 - 3;
593 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 582
                     break;
597 583
 
598 584
                 case ROT_247_5:
599 585
                     shot_pos_x = -SHIP_OFF / 2 - 5;
600 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 587
                     break;
604 588
 
605 589
                 case ROT_270:
606 590
                     shot_pos_x = -SHIP_OFF;
607 591
                     shot_pos_y = 0;
608
-                    shot_spd_x = spd_x - SHOT_SPEED;
609
-                    shot_spd_y = spd_y;
610 592
                     break;
611 593
 
612 594
                 case ROT_292_5:
613 595
                     shot_pos_x = -SHIP_OFF / 2 - 2;
614 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 597
                     break;
618 598
 
619 599
                 case ROT_315:
620 600
                     shot_pos_x = -SHIP_OFF / 2 - 3;
621 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 602
                     break;
625 603
 
626 604
                 case ROT_337_5:
627 605
                     shot_pos_x = -SHIP_OFF / 2 + 1;
628 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 607
                     break;
632 608
             }
633 609
 

+ 11
- 2
src/main.c View File

@@ -40,6 +40,7 @@
40 40
 #include "window.h"
41 41
 #include "gbprinter.h"
42 42
 #include "multiplayer.h"
43
+#include "speed_table.h"
43 44
 #include "main.h"
44 45
 
45 46
 uint8_t debug_menu_index = 0;
@@ -259,7 +260,11 @@ static void splash_anim(uint8_t *hiwater) NONBANKED {
259 260
     switch (anim_state) {
260 261
         case 1:
261 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 268
                 sample_play(SFX_SHOT);
264 269
             }
265 270
         case 0:
@@ -290,7 +295,11 @@ static void splash_anim(uint8_t *hiwater) NONBANKED {
290 295
 
291 296
         case 7:
292 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 303
                 sample_play(SFX_SHOT);
295 304
             }
296 305
         case 6:

+ 101
- 0
util/gen_angles.py View File

@@ -0,0 +1,101 @@
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