Moved some extra things into PPU

This commit is contained in:
neauoire 2021-04-07 17:42:30 -07:00
parent 31a9e3d64c
commit 33a660aef8
3 changed files with 61 additions and 57 deletions

View File

@ -20,24 +20,6 @@ int initapu(Uxn *u, Uint8 id);
static Ppu screen;
Uint8 font[][8] = {
{0x00, 0x3c, 0x46, 0x4a, 0x52, 0x62, 0x3c, 0x00},
{0x00, 0x18, 0x28, 0x08, 0x08, 0x08, 0x3e, 0x00},
{0x00, 0x3c, 0x42, 0x02, 0x3c, 0x40, 0x7e, 0x00},
{0x00, 0x3c, 0x42, 0x1c, 0x02, 0x42, 0x3c, 0x00},
{0x00, 0x08, 0x18, 0x28, 0x48, 0x7e, 0x08, 0x00},
{0x00, 0x7e, 0x40, 0x7c, 0x02, 0x42, 0x3c, 0x00},
{0x00, 0x3c, 0x40, 0x7c, 0x42, 0x42, 0x3c, 0x00},
{0x00, 0x7e, 0x02, 0x04, 0x08, 0x10, 0x10, 0x00},
{0x00, 0x3c, 0x42, 0x3c, 0x42, 0x42, 0x3c, 0x00},
{0x00, 0x3c, 0x42, 0x42, 0x3e, 0x02, 0x3c, 0x00},
{0x00, 0x3c, 0x42, 0x42, 0x7e, 0x42, 0x42, 0x00},
{0x00, 0x7c, 0x42, 0x7c, 0x42, 0x42, 0x7c, 0x00},
{0x00, 0x3c, 0x42, 0x40, 0x40, 0x42, 0x3c, 0x00},
{0x00, 0x78, 0x44, 0x42, 0x42, 0x44, 0x78, 0x00},
{0x00, 0x7e, 0x40, 0x7c, 0x40, 0x40, 0x7e, 0x00},
{0x00, 0x7e, 0x40, 0x40, 0x7c, 0x40, 0x40, 0x00}};
static SDL_Window *gWindow;
static SDL_Renderer *gRenderer;
static SDL_Texture *gTexture;
@ -67,8 +49,6 @@ getflag(Uint8 *a, char flag)
return *a & flag;
}
#pragma mark - Helpers
#pragma mark - Core
int
@ -78,25 +58,6 @@ error(char *msg, const char *err)
return 0;
}
void
drawdebugger(Uint32 *dst, Uxn *u)
{
Uint8 i, x, y, b;
for(i = 0; i < 0x10; ++i) { /* memory */
x = ((i % 8) * 3 + 3) * 8, y = screen.x1 + 8 + i / 8 * 8, b = u->wst.dat[i];
drawicn(&screen, x, y, font[(b >> 4) & 0xf], 1 + (u->wst.ptr == i), 0);
drawicn(&screen, x + 8, y, font[b & 0xf], 1 + (u->wst.ptr == i), 0);
}
for(x = 0; x < 32; ++x) {
drawpixel(&screen, x, HEIGHT / 2, 2);
drawpixel(&screen, WIDTH - x, HEIGHT / 2, 2);
drawpixel(&screen, WIDTH / 2, HEIGHT - x, 2);
drawpixel(&screen, WIDTH / 2, x, 2);
drawpixel(&screen, WIDTH / 2 - 16 + x, HEIGHT / 2, 2);
drawpixel(&screen, WIDTH / 2, HEIGHT / 2 - 16 + x, 2);
}
}
void
redraw(Uint32 *dst, Uxn *u)
{
@ -108,7 +69,7 @@ redraw(Uint32 *dst, Uxn *u)
drawchr(&screen, (x + PAD) * 8, (y + PAD) * 8, &screen.fg[key], 1);
}
if(screen.debugger)
drawdebugger(dst, u);
drawdebugger(&screen, u->wst.dat, u->wst.ptr);
SDL_UpdateTexture(gTexture, NULL, dst, WIDTH * sizeof(Uint32));
SDL_RenderClear(gRenderer);
SDL_RenderCopy(gRenderer, gTexture, NULL, NULL);
@ -159,15 +120,10 @@ init(void)
gTexture = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, WIDTH, HEIGHT);
if(gTexture == NULL)
return error("Texture", SDL_GetError());
if(!(screen.output = (Uint32 *)malloc(WIDTH * HEIGHT * sizeof(Uint32))))
return error("Pixels", "Failed to allocate memory");
clear(&screen);
SDL_StartTextInput();
SDL_ShowCursor(SDL_DISABLE);
screen.x1 = PAD * 8;
screen.x2 = WIDTH - PAD * 8 - 1;
screen.y1 = PAD * 8;
screen.y2 = HEIGHT - PAD * 8 - 1;
if(!initppu(&screen))
return error("PPU", "Init failure");
return 1;
}

View File

@ -12,6 +12,24 @@ WITH REGARD TO THIS SOFTWARE.
#include "ppu.h"
Uint8 font[][8] = {
{0x00, 0x3c, 0x46, 0x4a, 0x52, 0x62, 0x3c, 0x00},
{0x00, 0x18, 0x28, 0x08, 0x08, 0x08, 0x3e, 0x00},
{0x00, 0x3c, 0x42, 0x02, 0x3c, 0x40, 0x7e, 0x00},
{0x00, 0x3c, 0x42, 0x1c, 0x02, 0x42, 0x3c, 0x00},
{0x00, 0x08, 0x18, 0x28, 0x48, 0x7e, 0x08, 0x00},
{0x00, 0x7e, 0x40, 0x7c, 0x02, 0x42, 0x3c, 0x00},
{0x00, 0x3c, 0x40, 0x7c, 0x42, 0x42, 0x3c, 0x00},
{0x00, 0x7e, 0x02, 0x04, 0x08, 0x10, 0x10, 0x00},
{0x00, 0x3c, 0x42, 0x3c, 0x42, 0x42, 0x3c, 0x00},
{0x00, 0x3c, 0x42, 0x42, 0x3e, 0x02, 0x3c, 0x00},
{0x00, 0x3c, 0x42, 0x42, 0x7e, 0x42, 0x42, 0x00},
{0x00, 0x7c, 0x42, 0x7c, 0x42, 0x42, 0x7c, 0x00},
{0x00, 0x3c, 0x42, 0x40, 0x40, 0x42, 0x3c, 0x00},
{0x00, 0x78, 0x44, 0x42, 0x42, 0x44, 0x78, 0x00},
{0x00, 0x7e, 0x40, 0x7c, 0x40, 0x40, 0x7e, 0x00},
{0x00, 0x7e, 0x40, 0x40, 0x7c, 0x40, 0x40, 0x00}};
void
clear(Ppu *p)
{
@ -52,6 +70,25 @@ drawicn(Ppu *p, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 fg, Uint8 bg)
}
}
void
drawdebugger(Ppu *p, Uint8 *stack, Uint8 ptr)
{
Uint8 i, x, y, b;
for(i = 0; i < 0x10; ++i) { /* memory */
x = ((i % 8) * 3 + 3) * 8, y = p->x1 + 8 + i / 8 * 8, b = stack[i];
drawicn(p, x, y, font[(b >> 4) & 0xf], 1 + (ptr == i), 0);
drawicn(p, x + 8, y, font[b & 0xf], 1 + (ptr == i), 0);
}
for(x = 0; x < 32; ++x) {
drawpixel(p, x, HEIGHT / 2, 2);
drawpixel(p, WIDTH - x, HEIGHT / 2, 2);
drawpixel(p, WIDTH / 2, HEIGHT - x, 2);
drawpixel(p, WIDTH / 2, x, 2);
drawpixel(p, WIDTH / 2 - 16 + x, HEIGHT / 2, 2);
drawpixel(p, WIDTH / 2, HEIGHT / 2 - 16 + x, 2);
}
}
void
paintpixel(Uint8 *dst, Uint16 x, Uint16 y, Uint8 color)
{
@ -80,4 +117,17 @@ loadtheme(Ppu *p, Uint8 *addr)
p->colors[i] = (r << 20) + (r << 16) + (g << 12) + (g << 8) + (b << 4) + b;
}
p->reqdraw = 1;
}
int
initppu(Ppu *p)
{
if(!(p->output = (Uint32 *)malloc(WIDTH * HEIGHT * sizeof(Uint32))))
return 0;
clear(p);
p->x1 = PAD * 8;
p->x2 = WIDTH - PAD * 8 - 1;
p->y1 = PAD * 8;
p->y2 = HEIGHT - PAD * 8 - 1;
return 1;
}

View File

@ -1,4 +1,5 @@
#include <stdio.h>
#include <stdlib.h>
/*
Copyright (c) 2021 Devine Lu Linvega
@ -31,14 +32,11 @@ typedef struct Ppu {
Uint32 *output, colors[4];
} Ppu;
void
clear(Ppu *p);
void
drawpixel(Ppu *p, Uint16 x, Uint16 y, Uint8 color);
void
drawchr(Ppu *p, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 alpha);
void
drawicn(Ppu *p, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 fg, Uint8 bg);
void clear(Ppu *p);
void drawpixel(Ppu *p, Uint16 x, Uint16 y, Uint8 color);
void drawchr(Ppu *p, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 alpha);
void drawicn(Ppu *p, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 fg, Uint8 bg);
void drawdebugger(Ppu *p, Uint8 *stack, Uint8 ptr);
void paintpixel(Uint8 *dst, Uint16 x, Uint16 y, Uint8 color);
void loadtheme(Ppu *p, Uint8 *addr);
void loadtheme(Ppu *p, Uint8 *addr);
int initppu(Ppu *p);