Replaced duplicate parts of PPU init with resize functions

This commit is contained in:
Andrew Alderwick 2021-09-19 23:34:03 +01:00
parent a2305f2d64
commit f77fa80d35
3 changed files with 33 additions and 46 deletions

View File

@ -93,22 +93,13 @@ ppu_2bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, U
/* output */ /* output */
int int
ppu_init(Ppu *p, Uint8 hor, Uint8 ver) ppu_set_size(Ppu *p, Uint16 width, Uint16 height)
{ {
p->width = 8 * hor; /* round width and height up to nearest multiple of 8 */
p->height = 8 * ver; width = ((width - 1) | 0x7) + 1;
p->bg = calloc(1, p->width / 4 * p->height * sizeof(Uint8)); height = ((height - 1) | 0x7) + 1;
p->fg = calloc(1, p->width / 4 * p->height * sizeof(Uint8)); p->width = width;
ppu_clear(p); p->height = height;
return p->bg && p->fg;
}
int
ppu_resize(Ppu *p, Uint8 hor, Uint8 ver)
{
ppu_clear(p);
p->width = 8 * hor;
p->height = 8 * ver;
p->bg = realloc(p->bg, p->width / 4 * p->height * sizeof(Uint8)); p->bg = realloc(p->bg, p->width / 4 * p->height * sizeof(Uint8));
p->fg = realloc(p->fg, p->width / 4 * p->height * sizeof(Uint8)); p->fg = realloc(p->fg, p->width / 4 * p->height * sizeof(Uint8));
ppu_clear(p); ppu_clear(p);

View File

@ -22,8 +22,7 @@ typedef struct Ppu {
Uint8 *bg, *fg; Uint8 *bg, *fg;
} Ppu; } Ppu;
int ppu_init(Ppu *p, Uint8 hor, Uint8 ver); int ppu_set_size(Ppu *p, Uint16 width, Uint16 height);
int ppu_resize(Ppu *p, Uint8 hor, Uint8 ver);
int ppu_pixel(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 color); int ppu_pixel(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 color);
int ppu_1bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy); int ppu_1bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
int ppu_2bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy); int ppu_2bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);

View File

@ -185,9 +185,32 @@ quit(void)
exit(0); exit(0);
} }
static int
set_size(Uint16 width, Uint16 height, int is_resize)
{
ppu_set_size(&ppu, width, height);
gRect.x = PAD;
gRect.y = PAD;
gRect.w = ppu.width;
gRect.h = ppu.height;
if(!(ppu_screen = realloc(ppu_screen, ppu.width * ppu.height * sizeof(Uint32))))
return error("ppu_screen", "Memory failure");
memset(ppu_screen, 0, ppu.width * ppu.height * sizeof(Uint32));
if(gTexture != NULL) SDL_DestroyTexture(gTexture);
SDL_RenderSetLogicalSize(gRenderer, ppu.width + PAD * 2, ppu.height + PAD * 2);
gTexture = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, ppu.width + PAD * 2, ppu.height + PAD * 2);
if(gTexture == NULL || SDL_SetTextureBlendMode(gTexture, SDL_BLENDMODE_NONE))
return error("sdl_texture", SDL_GetError());
SDL_UpdateTexture(gTexture, NULL, ppu_screen, sizeof(Uint32));
if(is_resize) SDL_SetWindowSize(gWindow, (ppu.width + PAD * 2) * zoom, (ppu.height + PAD * 2) * zoom);
reqdraw = 1;
return 1;
}
static int static int
init(void) init(void)
{ {
const Uint16 width = 64 * 8, height = 40 * 8;
SDL_AudioSpec as; SDL_AudioSpec as;
SDL_zero(as); SDL_zero(as);
as.freq = SAMPLE_FREQUENCY; as.freq = SAMPLE_FREQUENCY;
@ -196,8 +219,6 @@ init(void)
as.callback = audio_callback; as.callback = audio_callback;
as.samples = 512; as.samples = 512;
as.userdata = NULL; as.userdata = NULL;
if(!ppu_init(&ppu, 64, 40))
return error("ppu", "Init failure");
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) { if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
error("sdl", SDL_GetError()); error("sdl", SDL_GetError());
if(SDL_Init(SDL_INIT_VIDEO) < 0) if(SDL_Init(SDL_INIT_VIDEO) < 0)
@ -207,25 +228,16 @@ init(void)
if(!audio_id) if(!audio_id)
error("sdl_audio", SDL_GetError()); error("sdl_audio", SDL_GetError());
} }
gWindow = SDL_CreateWindow("Uxn", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, (ppu.width + PAD * 2) * zoom, (ppu.height + PAD * 2) * zoom, SDL_WINDOW_SHOWN); gWindow = SDL_CreateWindow("Uxn", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, (width + PAD * 2) * zoom, (height + PAD * 2) * zoom, SDL_WINDOW_SHOWN);
if(gWindow == NULL) if(gWindow == NULL)
return error("sdl_window", SDL_GetError()); return error("sdl_window", SDL_GetError());
gRenderer = SDL_CreateRenderer(gWindow, -1, 0); gRenderer = SDL_CreateRenderer(gWindow, -1, 0);
if(gRenderer == NULL) if(gRenderer == NULL)
return error("sdl_renderer", SDL_GetError()); return error("sdl_renderer", SDL_GetError());
SDL_RenderSetLogicalSize(gRenderer, ppu.width + PAD * 2, ppu.height + PAD * 2); if(!set_size(width, height, 0))
gTexture = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, ppu.width + PAD * 2, ppu.height + PAD * 2);
if(gTexture == NULL || SDL_SetTextureBlendMode(gTexture, SDL_BLENDMODE_NONE))
return error("sdl_texture", SDL_GetError());
if(!(ppu_screen = calloc(1, ppu.width * ppu.height * sizeof(Uint32))))
return 0; return 0;
SDL_UpdateTexture(gTexture, NULL, ppu_screen, sizeof(Uint32));
SDL_StartTextInput(); SDL_StartTextInput();
SDL_ShowCursor(SDL_DISABLE); SDL_ShowCursor(SDL_DISABLE);
gRect.x = PAD;
gRect.y = PAD;
gRect.w = ppu.width;
gRect.h = ppu.height;
return 1; return 1;
} }
@ -296,21 +308,6 @@ update_palette(Uint8 *addr)
reqdraw = 1; reqdraw = 1;
} }
void
set_size(Uint16 width, Uint16 height)
{
ppu_resize(&ppu, width / 8, height / 8);
gRect.w = ppu.width;
gRect.h = ppu.height;
if(!(ppu_screen = realloc(ppu_screen, ppu.width * ppu.height * sizeof(Uint32))))
return;
SDL_DestroyTexture(gTexture);
SDL_RenderSetLogicalSize(gRenderer, ppu.width + PAD * 2, ppu.height + PAD * 2);
gTexture = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, ppu.width + PAD * 2, ppu.height + PAD * 2);
SDL_SetWindowSize(gWindow, (ppu.width + PAD * 2) * zoom, (ppu.height + PAD * 2) * zoom);
reqdraw = 1;
}
#pragma mark - Devices #pragma mark - Devices
static int static int
@ -353,7 +350,7 @@ screen_talk(Device *d, Uint8 b0, Uint8 w)
else else
switch(b0) { switch(b0) {
case 0x5: case 0x5:
if(!FIXED_SIZE) set_size(peek16(d->dat, 0x2), peek16(d->dat, 0x4)); if(!FIXED_SIZE) return set_size(peek16(d->dat, 0x2), peek16(d->dat, 0x4), 1);
break; break;
case 0xe: { case 0xe: {
Uint16 x = peek16(d->dat, 0x8); Uint16 x = peek16(d->dat, 0x8);