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
     int32_t score = 0;
152
     int32_t score = 0;
153
 
153
 
154
     obj_init();
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
     win_init(0);
157
     win_init(0);
163
     uint8_t x_off = win_game_draw(score);
158
     uint8_t x_off = win_game_draw(score);

+ 3
- 3
src/main.c View File

105
         move_win(MINWNDPOSX, MINWNDPOSY);
105
         move_win(MINWNDPOSX, MINWNDPOSY);
106
     } else {
106
     } else {
107
         // initially show the top 1 scores
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
         move_win(MINWNDPOSX, MINWNDPOSY + DEVICE_SCREEN_PX_HEIGHT - (8 * 4));
112
         move_win(MINWNDPOSX, MINWNDPOSY + DEVICE_SCREEN_PX_HEIGHT - (8 * 4));
113
     }
113
     }

+ 57
- 0
src/obj.c View File

21
 #include <stdint.h>
21
 #include <stdint.h>
22
 #include <string.h>
22
 #include <string.h>
23
 #include <stdlib.h>
23
 #include <stdlib.h>
24
+#include <rand.h>
24
 
25
 
25
 #include "gb/hardware.h"
26
 #include "gb/hardware.h"
26
 #include "sprites.h"
27
 #include "sprites.h"
76
 
77
 
77
 #define DESPAWN_RANGE (250 << POS_SCALE_OBJS)
78
 #define DESPAWN_RANGE (250 << POS_SCALE_OBJS)
78
 
79
 
80
+#define PLACEMENT_DISTANCE 42
81
+
79
 struct obj {
82
 struct obj {
80
     uint8_t active;
83
     uint8_t active;
81
     enum SPRITES sprite;
84
     enum SPRITES sprite;
87
 };
90
 };
88
 
91
 
89
 static struct obj objs[MAX_OBJ];
92
 static struct obj objs[MAX_OBJ];
93
+static uint8_t obj_cnt[SPRITE_COUNT];
90
 
94
 
91
 void obj_init(void) NONBANKED {
95
 void obj_init(void) NONBANKED {
92
     memset(objs, 0, sizeof(objs));
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
 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 {
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
 };
29
 };
30
 
30
 
31
 void obj_init(void);
31
 void obj_init(void);
32
+void obj_spawn(void);
32
 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
 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
 int16_t obj_do(int16_t *spd_off_x, int16_t *spd_off_y, int32_t *score, uint8_t *hiwater);
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
  * See <http://www.gnu.org/licenses/>.
17
  * See <http://www.gnu.org/licenses/>.
18
  */
18
  */
19
 
19
 
20
-#include <gbdk/platform.h>
21
 #include <string.h>
20
 #include <string.h>
22
 
21
 
23
 #include "score.h"
22
 #include "score.h"
24
-#include "gb/gb.h"
25
 
23
 
26
 static struct scores scores[SCORE_NUM * 2];
24
 static struct scores scores[SCORE_NUM * 2];
27
 static uint32_t scores_crc;
25
 static uint32_t scores_crc;
54
     //{ .name = NAME('d', 'j', '.'), .score = -10000 },
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
     // convert to lowercase
56
     // convert to lowercase
59
     if ((a >= 'A') && (a <= 'Z')) a = a - 'A' + 'a';
57
     if ((a >= 'A') && (a <= 'Z')) a = a - 'A' + 'a';
60
     if ((b >= 'A') && (b <= 'Z')) b = b - 'A' + 'a';
58
     if ((b >= 'A') && (b <= 'Z')) b = b - 'A' + 'a';
73
     return (a << 10) | (b << 5) | c;
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
     const uint8_t *d = (const uint8_t *)scores;
75
     const uint8_t *d = (const uint8_t *)scores;
78
 
76
 
79
     uint32_t c = 0xFFFFFFFF;
77
     uint32_t c = 0xFFFFFFFF;
89
     return ~c;
87
     return ~c;
90
 }
88
 }
91
 
89
 
92
-static uint8_t check_crc(void) NONBANKED {
90
+static uint8_t check_crc(void) BANKED {
93
     return (calc_crc() == scores_crc) ? 1 : 0;
91
     return (calc_crc() == scores_crc) ? 1 : 0;
94
 }
92
 }
95
 
93
 
99
     scores_crc = calc_crc();
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
     if (score > 0) {
101
     if (score > 0) {
104
         for (uint8_t i = 0; i < SCORE_NUM; i++) {
102
         for (uint8_t i = 0; i < SCORE_NUM; i++) {
105
             if (score > scores[i].score) {
103
             if (score > scores[i].score) {
117
     return 0xFF;
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
     ENABLE_RAM;
119
     ENABLE_RAM;
122
     SWITCH_RAM(0);
120
     SWITCH_RAM(0);
123
 
121
 
132
     return r;
130
     return r;
133
 }
131
 }
134
 
132
 
135
-void score_add(struct scores score) NONBANKED {
133
+void score_add(struct scores score) BANKED {
136
     ENABLE_RAM;
134
     ENABLE_RAM;
137
     SWITCH_RAM(0);
135
     SWITCH_RAM(0);
138
 
136
 
157
     DISABLE_RAM;
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
     ENABLE_RAM;
159
     ENABLE_RAM;
162
     SWITCH_RAM(0);
160
     SWITCH_RAM(0);
163
 
161
 
175
     return r;
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
     ENABLE_RAM;
177
     ENABLE_RAM;
180
     SWITCH_RAM(0);
178
     SWITCH_RAM(0);
181
 
179
 
193
     return r;
191
     return r;
194
 }
192
 }
195
 
193
 
196
-void score_reset(void) NONBANKED {
194
+void score_reset(void) BANKED {
197
     ENABLE_RAM;
195
     ENABLE_RAM;
198
     SWITCH_RAM(0);
196
     SWITCH_RAM(0);
199
     score_init();
197
     score_init();

+ 7
- 6
src/score.h View File

20
 #ifndef __SCORE_H__
20
 #ifndef __SCORE_H__
21
 #define __SCORE_H__
21
 #define __SCORE_H__
22
 
22
 
23
+#include <gbdk/platform.h>
23
 #include <stdint.h>
24
 #include <stdint.h>
24
 
25
 
25
 #define SCORE_NUM 5
26
 #define SCORE_NUM 5
29
     int32_t score;
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
 BANKREF_EXTERN(score)
40
 BANKREF_EXTERN(score)
40
 
41
 

Loading…
Cancel
Save