From 5a20eb69b0ad09e763c0183dd830e2bc1740875f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sigrid=20Solveig=20Hafl=C3=ADnud=C3=B3ttir?= Date: Wed, 3 Nov 2021 23:03:33 +0100 Subject: [PATCH] ppu: simplify ppu_set_size --- src/devices/ppu.c | 19 ++++++------------- src/devices/ppu.h | 3 ++- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/src/devices/ppu.c b/src/devices/ppu.c index 9d04511..b1d328a 100644 --- a/src/devices/ppu.c +++ b/src/devices/ppu.c @@ -19,23 +19,16 @@ static Uint8 blending[5][16] = { {2, 3, 1, 2, 2, 3, 1, 2, 2, 3, 1, 2, 2, 3, 1, 2}, {1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0}}; -static void -ppu_clear(Ppu *p) -{ - Uint32 row, bound = p->height * p->width / 2; - for(row = 0; row < bound; ++row) - p->pixels[row] = 0; -} - -Uint8 +void ppu_set_size(Ppu *p, Uint16 width, Uint16 height) { - ppu_clear(p); + Uint8 *pixels; + if(!(pixels = realloc(p->pixels, width * height / 2))) + return; + memset(pixels, 0, width * height / 2); + p->pixels = pixels; p->width = width; p->height = height; - p->pixels = realloc(p->pixels, p->width * p->height * sizeof(Uint8) / 2); - ppu_clear(p); - return !!p->pixels; } Uint8 diff --git a/src/devices/ppu.h b/src/devices/ppu.h index 1ae47c8..ebf9cdd 100644 --- a/src/devices/ppu.h +++ b/src/devices/ppu.h @@ -1,5 +1,6 @@ #include #include +#include /* Copyright (c) 2021 Devine Lu Linvega @@ -22,7 +23,7 @@ typedef struct Ppu { Uint16 width, height; } Ppu; -Uint8 ppu_set_size(Ppu *p, Uint16 width, Uint16 height); +void ppu_set_size(Ppu *p, Uint16 width, Uint16 height); Uint8 ppu_read(Ppu *p, Uint16 x, Uint16 y); void ppu_write(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 color); void ppu_frame(Ppu *p);