Browse Source

remove memory leak in pokedex, update lookup entries to lazy array, resolves #26

SBird1337 6 years ago
parent
commit
a24e8bb14e

+ 1
- 1
g3headers

@@ -1 +1 @@
1
-Subproject commit ee7c8b28b58f61df864f7716c7ee9bbae82f5411
1
+Subproject commit e0402c99b51968537abb0f09ed017558e2055385

+ 12
- 28
src/pokedex/pokedex.c View File

@@ -77,7 +77,7 @@ void pdex_main_box_species_fill(s8 n, u16 species, bool seen, bool caught) {
77 77
 
78 78
 void pdex_update_balls(void) {
79 79
     for (u8 i = 0; i < 8; ++i) {
80
-        if (pokedex_context->lookup[i + pokedex_context->cursor_position_top].caught)
80
+        if (pdex_lazy_lookup_entry(i + pokedex_context->cursor_position_top)->caught)
81 81
             OBJID_SHOW(pokedex_context->ball_oams[i]);
82 82
         else
83 83
             OBJID_HIDE(pokedex_context->ball_oams[i]);
@@ -87,8 +87,8 @@ void pdex_update_balls(void) {
87 87
 void pdex_update_page_full() {
88 88
     rboxid_clear_pixels(TB_MAIN, 0);
89 89
     for (u16 i = pokedex_context->cursor_position_top; i < pokedex_context->cursor_position_top + 8; ++i) {
90
-        pdex_main_box_species_fill(i - pokedex_context->cursor_position_top, pokedex_context->lookup[i].species,
91
-                                   pokedex_context->lookup[i].seen, pokedex_context->lookup[i].caught);
90
+        pdex_main_box_species_fill(i - pokedex_context->cursor_position_top, pdex_lazy_lookup_entry(i)->species,
91
+                                   pdex_lazy_lookup_entry(i)->seen, pdex_lazy_lookup_entry(i)->caught);
92 92
     }
93 93
     pdex_update_balls();
94 94
     rboxid_update_tilemap_and_tileset(TB_MAIN);
@@ -309,23 +309,7 @@ void pdex_load_scroll_ui(void) {
309 309
 }
310 310
 
311 311
 void pdex_data_setup(void) {
312
-    /* fill the LUT */
313
-    /* TODO: get data from various pokedex lists */
314
-    pokedex_context->lookup = malloc_and_clear((PDEX_LAST_SHOWN + 1) * sizeof(struct PdexLookup));
315
-    bool first = false;
316
-    for (u32 i = 0; i <= PDEX_LAST_SHOWN; ++i) {
317
-        u16 species = pokedex_index_to_species(i);
318
-        if (species > 0) {
319
-            pokedex_context->lookup[i].species = species;
320
-            pokedex_context->lookup[i].seen = dex_flag_pokedex_index(i, DEX_FLAG_CHECK_SEEN);
321
-            pokedex_context->lookup[i].caught = dex_flag_pokedex_index(i, DEX_FLAG_CHECK_CAUGHT);
322
-            if (!first && (pokedex_context->lookup[i].seen)) {
323
-                pokedex_context->first_seen = 1; //i
324
-                //pokedex_context->cursor_position_top = 1; //i is set in region dex
325
-                first = true;
326
-            }
327
-        }
328
-    }
312
+    pokedex_context->first_seen = 1;
329 313
 }
330 314
 
331 315
 void pdex_hardware_scroll(bool up) {
@@ -351,14 +335,14 @@ void pdex_hardware_scroll(bool up) {
351 335
 
352 336
 void pdex_fill_previous_slot(void) {
353 337
     u16 pIndex = pokedex_context->cursor_position_top - 1;
354
-    pdex_main_box_species_fill(-1, pokedex_context->lookup[pIndex].species, pokedex_context->lookup[pIndex].seen,
355
-                               pokedex_context->lookup[pIndex].caught);
338
+    pdex_main_box_species_fill(-1, pdex_lazy_lookup_entry(pIndex)->species, pdex_lazy_lookup_entry(pIndex)->seen,
339
+                               pdex_lazy_lookup_entry(pIndex)->caught);
356 340
 }
357 341
 
358 342
 void pdex_fill_next_slot(void) {
359 343
     u16 pIndex = pokedex_context->cursor_position_top + 8;
360
-    pdex_main_box_species_fill(8, pokedex_context->lookup[pIndex].species, pokedex_context->lookup[pIndex].seen,
361
-                               pokedex_context->lookup[pIndex].caught);
344
+    pdex_main_box_species_fill(8, pdex_lazy_lookup_entry(pIndex)->species, pdex_lazy_lookup_entry(pIndex)->seen,
345
+                               pdex_lazy_lookup_entry(pIndex)->caught);
362 346
 }
363 347
 
364 348
 void pdex_try_advance(u8 reverse) {
@@ -385,10 +369,10 @@ void pdex_try_advance(u8 reverse) {
385 369
     }
386 370
 
387 371
     u16 pkIndexToLoad = pokedex_context->cursor_position_internal + pokedex_context->cursor_position_top;
388
-    if (pokedex_context->lookup[pkIndexToLoad].seen || pokedex_context->lookup[pkIndexToLoad].caught)
389
-        pdex_pokemon_load(pokedex_context->lookup[pkIndexToLoad].species);
372
+    if (pdex_lazy_lookup_entry(pkIndexToLoad)->seen || pdex_lazy_lookup_entry(pkIndexToLoad)->caught)
373
+        pdex_pokemon_load(pdex_lazy_lookup_entry(pkIndexToLoad)->species);
390 374
     else
391
-        pdex_pokemon_load(pokedex_context->lookup[pkIndexToLoad].species); /* debug, just display the mofo */
375
+        pdex_pokemon_load(pdex_lazy_lookup_entry(pkIndexToLoad)->species); /* debug, just display the mofo */
392 376
     pdex_update_balls();
393 377
 }
394 378
 
@@ -409,7 +393,7 @@ void pdex_loop(u8 tid) {
409 393
 
410 394
         pdex_pokeballs_init();
411 395
         pdex_load_scroll_ui();
412
-        pdex_pokemon_load(pokedex_context->lookup[pokedex_context->cursor_position_top + pokedex_context->cursor_position_internal].species);
396
+        pdex_pokemon_load(pdex_lazy_lookup_entry(pokedex_context->cursor_position_top + pokedex_context->cursor_position_internal)->species);
413 397
         pdex_load_sc();
414 398
         pdex_update_page_full();
415 399
 

+ 24
- 3
src/pokedex/pokedex_common.c View File

@@ -97,13 +97,34 @@ void pdex_vram_free_bgmaps(void) {
97 97
     }
98 98
 }
99 99
 
100
-void pdex_free_memory(void)
101
-{
102
-    if(pokedex_context->lookup != NULL)
100
+struct PdexLookup *pdex_fill_lookup(u16 dexIndex) {
101
+    u16 species = pokedex_index_to_species(dexIndex);
102
+    pokedex_context->lookup[dexIndex].species = species;
103
+    pokedex_context->lookup[dexIndex].seen = dex_flag_pokedex_index(dexIndex, DEX_FLAG_CHECK_SEEN);
104
+    pokedex_context->lookup[dexIndex].caught = dex_flag_pokedex_index(dexIndex, DEX_FLAG_CHECK_CAUGHT);
105
+    return &pokedex_context->lookup[dexIndex];
106
+}
107
+
108
+struct PdexLookup *pdex_lazy_lookup_entry(u16 dexIndex) {
109
+    if (pokedex_context->lookup[dexIndex].species != -1) {
110
+        return &pokedex_context->lookup[dexIndex];
111
+    } else {
112
+        return pdex_fill_lookup(dexIndex);
113
+    }
114
+}
115
+
116
+void pdex_free_memory(void) {
117
+    if (pokedex_context->lookup != NULL)
103 118
         free(pokedex_context->lookup);
104 119
     free(pokedex_context);
105 120
 }
106 121
 
122
+void pdex_alloc_memory(void) {
123
+    pokedex_context = malloc_and_clear(sizeof(struct PdexCtx));
124
+    pokedex_context->lookup = malloc_and_clear((PDEX_LAST_SHOWN + 1) * sizeof(struct PdexLookup));
125
+    memset(pokedex_context->lookup, 0xFF, (PDEX_LAST_SHOWN + 1) * sizeof(struct PdexLookup));
126
+}
127
+
107 128
 void pdex_vram_allocate_bgmaps(void) {
108 129
     bgid_set_tilemap(0, malloc(0x800));
109 130
     bgid_set_tilemap(1, malloc(0x800));

+ 2
- 0
src/pokedex/pokedex_common.h View File

@@ -44,6 +44,8 @@ void pdex_vblank_handler(void);
44 44
 void pdex_vram_setup(void);
45 45
 void pdex_vram_free_bgmaps(void);
46 46
 void pdex_free_memory(void);
47
+void pdex_alloc_memory(void);
48
+struct PdexLookup *pdex_lazy_lookup_entry(u16 dexIndex);
47 49
 
48 50
 void pdex_load(void);
49 51
 

+ 4
- 7
src/pokedex/pokedex_detail.c View File

@@ -63,7 +63,7 @@ s8 dexdetail_load_type_oam(u8 type, u16 tag) {
63 63
 }
64 64
 
65 65
 void dexdetail_load_pokemon(u16 dexindex) {
66
-    u16 species = pokedex_context->lookup[dexindex].species;
66
+    u16 species = pdex_lazy_lookup_entry(dexindex)->species;
67 67
     rboxid_clear_pixels(TB_PKNAME, 0);
68 68
     rboxid_clear_pixels(TB_PKSIZE, 0);
69 69
     rboxid_clear_pixels(TB_DESC, 0);
@@ -180,17 +180,14 @@ void dexdetail_loop(u8 tid) {
180 180
         case KEY_B:
181 181
             pokedex_context->state = 12;
182 182
             u16 dexindex = pokedex_context->cursor_position_top + pokedex_context->cursor_position_internal;
183
-            if(dexindex <= PDEX_LAST_SHOWN - 7)
184
-            {
183
+            if (dexindex <= PDEX_LAST_SHOWN - 7) {
185 184
                 pokedex_context->cursor_position_top = dexindex;
186 185
                 pokedex_context->cursor_position_internal = 0;
187
-            }
188
-            else
189
-            {
186
+            } else {
190 187
                 pokedex_context->cursor_position_top = PDEX_LAST_SHOWN - 7;
191 188
                 pokedex_context->cursor_position_internal = (dexindex - pokedex_context->cursor_position_top);
192 189
             }
193
-            
190
+
194 191
             fade_screen(0xFFFFFFFF, PDEX_FADEIN_SPD, 0, 16, 0x0000);
195 192
             break;
196 193
         }

+ 1
- 1
src/pokedex/pokedex_region.c View File

@@ -223,7 +223,7 @@ void region_loop(u8 tid) {
223 223
 
224 224
 void region_select_load(void) {
225 225
     pdex_vram_setup();
226
-    pokedex_context = malloc_and_clear(sizeof(struct PdexCtx));
226
+    pdex_alloc_memory();
227 227
 
228 228
     region_select_load_gfx();
229 229
     task_add(region_loop, 0);