Browse Source

add spawning code. causes bank0 overflow, so put score code in BANKED. but crashes. why?!

Thomas B 1 month ago
parent
commit
0b95475146
6 changed files with 78 additions and 26 deletions
  1. 1
    6
      src/game.c
  2. 3
    3
      src/main.c
  3. 57
    0
      src/obj.c
  4. 1
    0
      src/obj.h
  5. 9
    11
      src/score.ba0.c
  6. 7
    6
      src/score.h

+ 1
- 6
src/game.c View File

@@ -152,12 +152,7 @@ int32_t game(void) NONBANKED {
152 152
     int32_t score = 0;
153 153
 
154 154
     obj_init();
155
-
156
-    // TODO remove
157
-    obj_add(SPR_LIGHT, 64, 64, 0, 0);
158
-    obj_add(SPR_DARK, -64, -64, 0, 0);
159
-    obj_add(SPR_SHOT_LIGHT, 32, 32, 0, 0);
160
-    obj_add(SPR_SHOT_DARK, -32, -32, 0, 0);
155
+    //obj_spawn();
161 156
 
162 157
     win_init(0);
163 158
     uint8_t x_off = win_game_draw(score);

+ 3
- 3
src/main.c View File

@@ -105,9 +105,9 @@ static void splash_win(void) NONBANKED {
105 105
         move_win(MINWNDPOSX, MINWNDPOSY);
106 106
     } else {
107 107
         // initially show the top 1 scores
108
-        int32_t low = score_lowest(0).score;
109
-        int32_t high = score_highest(0).score;
110
-        win_splash_draw(-low, high);
108
+        //int32_t low = score_lowest(0).score;
109
+        //int32_t high = score_highest(0).score;
110
+        //win_splash_draw(-low, high);
111 111
 
112 112
         move_win(MINWNDPOSX, MINWNDPOSY + DEVICE_SCREEN_PX_HEIGHT - (8 * 4));
113 113
     }

+ 57
- 0
src/obj.c View File

@@ -21,6 +21,7 @@
21 21
 #include <stdint.h>
22 22
 #include <string.h>
23 23
 #include <stdlib.h>
24
+#include <rand.h>
24 25
 
25 26
 #include "gb/hardware.h"
26 27
 #include "sprites.h"
@@ -76,6 +77,8 @@
76 77
 
77 78
 #define DESPAWN_RANGE (250 << POS_SCALE_OBJS)
78 79
 
80
+#define PLACEMENT_DISTANCE 42
81
+
79 82
 struct obj {
80 83
     uint8_t active;
81 84
     enum SPRITES sprite;
@@ -87,9 +90,63 @@ struct obj {
87 90
 };
88 91
 
89 92
 static struct obj objs[MAX_OBJ];
93
+static uint8_t obj_cnt[SPRITE_COUNT];
90 94
 
91 95
 void obj_init(void) NONBANKED {
92 96
     memset(objs, 0, sizeof(objs));
97
+    memset(obj_cnt, 0, sizeof(obj_cnt));
98
+}
99
+
100
+static uint8_t is_too_close(int8_t x, int8_t y, uint8_t n, int8_t *x_c, int8_t *y_c) NONBANKED {
101
+    for (uint8_t i = 0; i < n; i++) {
102
+        int dst_x = abs(x_c[i] - x);
103
+        int dst_y = abs(y_c[i] - y);
104
+        if ((dst_x < PLACEMENT_DISTANCE) && (dst_y < PLACEMENT_DISTANCE)) {
105
+            return 1;
106
+        }
107
+    }
108
+    return 0;
109
+}
110
+
111
+static void generate_coords(uint8_t n, int8_t *x_c, int8_t *y_c) NONBANKED {
112
+    int8_t x = 0;
113
+    int8_t y = 0;
114
+
115
+    do {
116
+        x = arand();
117
+        y = arand();
118
+    } while (is_too_close(x, y, n, x_c, y_c));
119
+
120
+    x_c[n] = x;
121
+    y_c[n] = y;
122
+}
123
+
124
+void obj_spawn(void) NONBANKED {
125
+    int8_t x_coords[MAX_DARK + MAX_LIGHT + MAX_SHOT_DARK + MAX_SHOT_LIGHT];
126
+    int8_t y_coords[MAX_DARK + MAX_LIGHT + MAX_SHOT_DARK + MAX_SHOT_LIGHT];
127
+    memset(x_coords, 0, sizeof(x_coords));
128
+    memset(y_coords, 0, sizeof(y_coords));
129
+
130
+    for (uint8_t i = 0; i < MAX_DARK; i++) {
131
+        uint8_t n = i;
132
+        generate_coords(n, x_coords, y_coords);
133
+        obj_add(SPR_DARK, x_coords[n], y_coords[n], 0, 0);
134
+    }
135
+    for (uint8_t i = 0; i < MAX_LIGHT; i++) {
136
+        uint8_t n = MAX_DARK + i;
137
+        generate_coords(n, x_coords, y_coords);
138
+        obj_add(SPR_LIGHT, x_coords[n], y_coords[n], 0, 0);
139
+    }
140
+    for (uint8_t i = 0; i < MAX_SHOT_DARK; i++) {
141
+        uint8_t n = MAX_DARK + MAX_LIGHT + i;
142
+        generate_coords(n, x_coords, y_coords);
143
+        obj_add(SPR_SHOT_DARK, x_coords[n], y_coords[n], 0, 0);
144
+    }
145
+    for (uint8_t i = 0; i < MAX_SHOT_LIGHT; i++) {
146
+        uint8_t n = MAX_DARK + MAX_LIGHT + MAX_SHOT_LIGHT + i;
147
+        generate_coords(n, x_coords, y_coords);
148
+        obj_add(SPR_SHOT_LIGHT, x_coords[n], y_coords[n], 0, 0);
149
+    }
93 150
 }
94 151
 
95 152
 enum OBJ_STATE obj_add(enum SPRITES sprite, int16_t off_x, int16_t off_y, int16_t spd_x, int16_t spd_y) NONBANKED {

+ 1
- 0
src/obj.h View File

@@ -29,6 +29,7 @@ enum OBJ_STATE {
29 29
 };
30 30
 
31 31
 void obj_init(void);
32
+void obj_spawn(void);
32 33
 enum OBJ_STATE obj_add(enum SPRITES sprite, int16_t off_x, int16_t off_y, int16_t spd_x, int16_t spd_y);
33 34
 int16_t obj_do(int16_t *spd_off_x, int16_t *spd_off_y, int32_t *score, uint8_t *hiwater);
34 35
 

+ 9
- 11
src/score.ba0.c View File

@@ -17,11 +17,9 @@
17 17
  * See <http://www.gnu.org/licenses/>.
18 18
  */
19 19
 
20
-#include <gbdk/platform.h>
21 20
 #include <string.h>
22 21
 
23 22
 #include "score.h"
24
-#include "gb/gb.h"
25 23
 
26 24
 static struct scores scores[SCORE_NUM * 2];
27 25
 static uint32_t scores_crc;
@@ -54,7 +52,7 @@ static const struct scores initial_scores[SCORE_NUM * 2] = {
54 52
     //{ .name = NAME('d', 'j', '.'), .score = -10000 },
55 53
 };
56 54
 
57
-uint16_t convert_name(char a, char b, char c) NONBANKED {
55
+uint16_t convert_name(char a, char b, char c) BANKED {
58 56
     // convert to lowercase
59 57
     if ((a >= 'A') && (a <= 'Z')) a = a - 'A' + 'a';
60 58
     if ((b >= 'A') && (b <= 'Z')) b = b - 'A' + 'a';
@@ -73,7 +71,7 @@ uint16_t convert_name(char a, char b, char c) NONBANKED {
73 71
     return (a << 10) | (b << 5) | c;
74 72
 }
75 73
 
76
-static uint32_t calc_crc(void) NONBANKED {
74
+static uint32_t calc_crc(void) BANKED {
77 75
     const uint8_t *d = (const uint8_t *)scores;
78 76
 
79 77
     uint32_t c = 0xFFFFFFFF;
@@ -89,7 +87,7 @@ static uint32_t calc_crc(void) NONBANKED {
89 87
     return ~c;
90 88
 }
91 89
 
92
-static uint8_t check_crc(void) NONBANKED {
90
+static uint8_t check_crc(void) BANKED {
93 91
     return (calc_crc() == scores_crc) ? 1 : 0;
94 92
 }
95 93
 
@@ -99,7 +97,7 @@ static void score_init(void) NONBANKED {
99 97
     scores_crc = calc_crc();
100 98
 }
101 99
 
102
-static uint8_t score_pos(int32_t score) NONBANKED {
100
+static uint8_t score_pos(int32_t score) BANKED {
103 101
     if (score > 0) {
104 102
         for (uint8_t i = 0; i < SCORE_NUM; i++) {
105 103
             if (score > scores[i].score) {
@@ -117,7 +115,7 @@ static uint8_t score_pos(int32_t score) NONBANKED {
117 115
     return 0xFF;
118 116
 }
119 117
 
120
-uint8_t score_ranking(int32_t score) NONBANKED {
118
+uint8_t score_ranking(int32_t score) BANKED {
121 119
     ENABLE_RAM;
122 120
     SWITCH_RAM(0);
123 121
 
@@ -132,7 +130,7 @@ uint8_t score_ranking(int32_t score) NONBANKED {
132 130
     return r;
133 131
 }
134 132
 
135
-void score_add(struct scores score) NONBANKED {
133
+void score_add(struct scores score) BANKED {
136 134
     ENABLE_RAM;
137 135
     SWITCH_RAM(0);
138 136
 
@@ -157,7 +155,7 @@ void score_add(struct scores score) NONBANKED {
157 155
     DISABLE_RAM;
158 156
 }
159 157
 
160
-struct scores score_highest(uint8_t off) NONBANKED {
158
+struct scores score_highest(uint8_t off) BANKED {
161 159
     ENABLE_RAM;
162 160
     SWITCH_RAM(0);
163 161
 
@@ -175,7 +173,7 @@ struct scores score_highest(uint8_t off) NONBANKED {
175 173
     return r;
176 174
 }
177 175
 
178
-struct scores score_lowest(uint8_t off) NONBANKED {
176
+struct scores score_lowest(uint8_t off) BANKED {
179 177
     ENABLE_RAM;
180 178
     SWITCH_RAM(0);
181 179
 
@@ -193,7 +191,7 @@ struct scores score_lowest(uint8_t off) NONBANKED {
193 191
     return r;
194 192
 }
195 193
 
196
-void score_reset(void) NONBANKED {
194
+void score_reset(void) BANKED {
197 195
     ENABLE_RAM;
198 196
     SWITCH_RAM(0);
199 197
     score_init();

+ 7
- 6
src/score.h View File

@@ -20,6 +20,7 @@
20 20
 #ifndef __SCORE_H__
21 21
 #define __SCORE_H__
22 22
 
23
+#include <gbdk/platform.h>
23 24
 #include <stdint.h>
24 25
 
25 26
 #define SCORE_NUM 5
@@ -29,12 +30,12 @@ struct scores {
29 30
     int32_t score;
30 31
 };
31 32
 
32
-uint16_t convert_name(char a, char b, char c);
33
-uint8_t score_ranking(int32_t score);
34
-void score_add(struct scores score);
35
-struct scores score_highest(uint8_t off);
36
-struct scores score_lowest(uint8_t off);
37
-void score_reset(void);
33
+uint16_t convert_name(char a, char b, char c) BANKED;
34
+uint8_t score_ranking(int32_t score) BANKED;
35
+void score_add(struct scores score) BANKED;
36
+struct scores score_highest(uint8_t off) BANKED;
37
+struct scores score_lowest(uint8_t off) BANKED;
38
+void score_reset(void) BANKED;
38 39
 
39 40
 BANKREF_EXTERN(score)
40 41
 

Loading…
Cancel
Save