diff options
author | Ben Bridle <ben@derelict.engineering> | 2024-12-16 14:49:22 +1300 |
---|---|---|
committer | Ben Bridle <ben@derelict.engineering> | 2024-12-16 14:49:56 +1300 |
commit | f98e8576cc844e7be676ac653266e21ef54873d5 (patch) | |
tree | 80eadbd31e87d08f8f3c0cedbf1abd9c26adcdd4 | |
parent | c488e189637d72227b3c19ecbed1fc4a2a2e5439 (diff) | |
download | bedrock-nds-f98e8576cc844e7be676ac653266e21ef54873d5.zip |
Prevent null pointer errors
Many functions which used the scr->nds pointer were not first checking
that the pointer was not null.
-rw-r--r-- | arm9/source/devices/screen.c | 68 |
1 files changed, 40 insertions, 28 deletions
diff --git a/arm9/source/devices/screen.c b/arm9/source/devices/screen.c index 5c16d8f..f1aa7f9 100644 --- a/arm9/source/devices/screen.c +++ b/arm9/source/devices/screen.c @@ -78,7 +78,9 @@ void set_palette_low(ScreenDevice *scr, u8 low) { u8 g = scr->palette_write >> 3 & 0x1e; u8 b = scr->palette_write << 1 & 0x1e; scr->palette[i] = RGB15(r,g,b); - scr->nds->pal[i] = RGB15(r,g,b); + if (scr->nds) { + scr->nds->pal[i] = RGB15(r,g,b); + } } void push_sprite(SpriteBuffer *b, u8 row) { @@ -145,47 +147,57 @@ void fill_layer(u16 *layer, u8 colour) { } void erase_screen(Screen *nds) { - dmaFillWords(0, nds->bg, TILES_MEM); - dmaFillWords(0, nds->fg, TILES_MEM); + if (nds) { + dmaFillWords(0, nds->bg, TILES_MEM); + dmaFillWords(0, nds->fg, TILES_MEM); + } }; void flip_buffer(Screen *nds) { - dmaCopyWords(0, nds->bg, nds->bgv, TILES_MEM); - dmaCopyWords(0, nds->fg, nds->fgv, TILES_MEM); - for (int i=0; i<16; i++) { - nds->palv[i] = nds->pal[i]; + if (nds) { + dmaCopyWords(0, nds->bg, nds->bgv, TILES_MEM); + dmaCopyWords(0, nds->fg, nds->fgv, TILES_MEM); + for (int i=0; i<16; i++) { + nds->palv[i] = nds->pal[i]; + } } } void black_screen(Screen *nds) { - nds->pal[0] = RGB15(0,0,0); - dmaFillWords(0, nds->bgv, TILES_MEM); - dmaFillWords(0, nds->fgv, TILES_MEM); + if (nds) { + for (int i=0; i<16; i++) { + nds->palv[i] = RGB15(0,0,0); + } + dmaFillWords(0, nds->bgv, TILES_MEM); + dmaFillWords(0, nds->fgv, TILES_MEM); + } } // --------------------------------------------------------------------------- void draw_dispatch(ScreenDevice *scr, u8 draw) { - switch (draw >> 4) { - case 0x0: scr_draw_pixel(scr, scr->nds->bg, draw); break; - case 0x1: scr_draw_sprite(scr, scr->nds->bg, draw); break; - case 0x2: scr_fill_layer(scr, scr->nds->bg, draw); break; - case 0x3: scr_draw_sprite(scr, scr->nds->bg, draw); break; - case 0x4: scr_draw_line(scr, scr->nds->bg, draw); break; - case 0x5: scr_draw_line(scr, scr->nds->bg, draw); break; - case 0x6: scr_draw_rect(scr, scr->nds->bg, draw); break; - case 0x7: scr_draw_rect(scr, scr->nds->bg, draw); break; - case 0x8: scr_draw_pixel(scr, scr->nds->fg, draw); break; - case 0x9: scr_draw_sprite(scr, scr->nds->fg, draw); break; - case 0xA: scr_fill_layer(scr, scr->nds->fg, draw); break; - case 0xB: scr_draw_sprite(scr, scr->nds->fg, draw); break; - case 0xC: scr_draw_line(scr, scr->nds->fg, draw); break; - case 0xD: scr_draw_line(scr, scr->nds->fg, draw); break; - case 0xE: scr_draw_rect(scr, scr->nds->fg, draw); break; - case 0xF: scr_draw_rect(scr, scr->nds->fg, draw); break; + if (scr->nds) { + switch (draw >> 4) { + case 0x0: scr_draw_pixel(scr, scr->nds->bg, draw); break; + case 0x1: scr_draw_sprite(scr, scr->nds->bg, draw); break; + case 0x2: scr_fill_layer(scr, scr->nds->bg, draw); break; + case 0x3: scr_draw_sprite(scr, scr->nds->bg, draw); break; + case 0x4: scr_draw_line(scr, scr->nds->bg, draw); break; + case 0x5: scr_draw_line(scr, scr->nds->bg, draw); break; + case 0x6: scr_draw_rect(scr, scr->nds->bg, draw); break; + case 0x7: scr_draw_rect(scr, scr->nds->bg, draw); break; + case 0x8: scr_draw_pixel(scr, scr->nds->fg, draw); break; + case 0x9: scr_draw_sprite(scr, scr->nds->fg, draw); break; + case 0xA: scr_fill_layer(scr, scr->nds->fg, draw); break; + case 0xB: scr_draw_sprite(scr, scr->nds->fg, draw); break; + case 0xC: scr_draw_line(scr, scr->nds->fg, draw); break; + case 0xD: scr_draw_line(scr, scr->nds->fg, draw); break; + case 0xE: scr_draw_rect(scr, scr->nds->fg, draw); break; + case 0xF: scr_draw_rect(scr, scr->nds->fg, draw); break; + } + scr->dirty = true; } scr->px = scr->x; scr->py = scr->y; - scr->dirty = true; } void scr_draw_pixel(ScreenDevice *scr, u16 *layer, u8 draw) { |