(screen) Use uxn_screen global

This commit is contained in:
Devine Lu Linvega 2023-05-04 17:43:44 -07:00
parent 1fdb91ecb8
commit 98bd2a7849
3 changed files with 51 additions and 50 deletions

View File

@ -25,27 +25,27 @@ static Uint8 blending[4][16] = {
{2, 3, 1, 2, 2, 3, 1, 2, 2, 3, 1, 2, 2, 3, 1, 2}}; {2, 3, 1, 2, 2, 3, 1, 2, 2, 3, 1, 2, 2, 3, 1, 2}};
static void static void
screen_change(UxnScreen *s, Uint16 x1, Uint16 y1, Uint16 x2, Uint16 y2) screen_change(Uint16 x1, Uint16 y1, Uint16 x2, Uint16 y2)
{ {
if(x1 < s->x1) s->x1 = x1; if(x1 < uxn_screen.x1) uxn_screen.x1 = x1;
if(y1 < s->y1) s->y1 = y1; if(y1 < uxn_screen.y1) uxn_screen.y1 = y1;
if(x2 > s->x2) s->x2 = x2; if(x2 > uxn_screen.x2) uxn_screen.x2 = x2;
if(y2 > s->y2) s->y2 = y2; if(y2 > uxn_screen.y2) uxn_screen.y2 = y2;
} }
static void static void
screen_fill(UxnScreen *s, Uint8 *pixels, Uint16 x1, Uint16 y1, Uint16 x2, Uint16 y2, Uint8 color) screen_fill(Uint8 *pixels, Uint16 x1, Uint16 y1, Uint16 x2, Uint16 y2, Uint8 color)
{ {
int x, y, width = s->width, height = s->height; int x, y, width = uxn_screen.width, height = uxn_screen.height;
for(y = y1; y < y2 && y < height; y++) for(y = y1; y < y2 && y < height; y++)
for(x = x1; x < x2 && x < width; x++) for(x = x1; x < x2 && x < width; x++)
pixels[x + y * width] = color; pixels[x + y * width] = color;
} }
static void static void
screen_blit(UxnScreen *s, Uint8 *pixels, Uint16 x1, Uint16 y1, Uint8 *ram, Uint16 addr, Uint8 color, Uint8 flipx, Uint8 flipy, Uint8 twobpp) screen_blit(Uint8 *pixels, Uint16 x1, Uint16 y1, Uint8 *ram, Uint16 addr, Uint8 color, Uint8 flipx, Uint8 flipy, Uint8 twobpp)
{ {
int v, h, width = s->width, height = s->height, opaque = (color % 5) || !color; int v, h, width = uxn_screen.width, height = uxn_screen.height, opaque = (color % 5) || !color;
for(v = 0; v < 8; v++) { for(v = 0; v < 8; v++) {
Uint16 c = ram[(addr + v) & 0xffff] | (twobpp ? (ram[(addr + v + 8) & 0xffff] << 8) : 0); Uint16 c = ram[(addr + v) & 0xffff] | (twobpp ? (ram[(addr + v + 8) & 0xffff] << 8) : 0);
Uint16 y = y1 + (flipy ? 7 - v : v); Uint16 y = y1 + (flipy ? 7 - v : v);
@ -61,7 +61,7 @@ screen_blit(UxnScreen *s, Uint8 *pixels, Uint16 x1, Uint16 y1, Uint8 *ram, Uint1
} }
void void
screen_palette(UxnScreen *p, Uint8 *addr) screen_palette(Uint8 *addr)
{ {
int i, shift; int i, shift;
for(i = 0, shift = 4; i < 4; ++i, shift ^= 4) { for(i = 0, shift = 4; i < 4; ++i, shift ^= 4) {
@ -69,49 +69,51 @@ screen_palette(UxnScreen *p, Uint8 *addr)
r = (addr[0 + i / 2] >> shift) & 0xf, r = (addr[0 + i / 2] >> shift) & 0xf,
g = (addr[2 + i / 2] >> shift) & 0xf, g = (addr[2 + i / 2] >> shift) & 0xf,
b = (addr[4 + i / 2] >> shift) & 0xf; b = (addr[4 + i / 2] >> shift) & 0xf;
p->palette[i] = 0x0f000000 | r << 16 | g << 8 | b; uxn_screen.palette[i] = 0x0f000000 | r << 16 | g << 8 | b;
p->palette[i] |= p->palette[i] << 4; uxn_screen.palette[i] |= uxn_screen.palette[i] << 4;
} }
screen_change(&uxn_screen, 0, 0, p->width, p->height); screen_change(0, 0, uxn_screen.width, uxn_screen.height);
} }
void void
screen_resize(UxnScreen *p, Uint16 width, Uint16 height) screen_resize(Uint16 width, Uint16 height)
{ {
Uint8 *bg, *fg; Uint8 *bg, *fg;
Uint32 *pixels; Uint32 *pixels;
if(width < 0x8 || height < 0x8 || width >= 0x400 || height >= 0x400) if(width < 0x8 || height < 0x8 || width >= 0x400 || height >= 0x400)
return; return;
bg = realloc(p->bg, width * height), bg = realloc(uxn_screen.bg, width * height),
fg = realloc(p->fg, width * height); fg = realloc(uxn_screen.fg, width * height);
pixels = realloc(p->pixels, width * height * sizeof(Uint32)); pixels = realloc(uxn_screen.pixels, width * height * sizeof(Uint32));
if(!bg || !fg || !pixels) if(!bg || !fg || !pixels)
return; return;
p->bg = bg; uxn_screen.bg = bg;
p->fg = fg; uxn_screen.fg = fg;
p->pixels = pixels; uxn_screen.pixels = pixels;
p->width = width; uxn_screen.width = width;
p->height = height; uxn_screen.height = height;
screen_fill(p, p->bg, 0, 0, p->width, p->height, 0); screen_fill(uxn_screen.bg, 0, 0, uxn_screen.width, uxn_screen.height, 0);
screen_fill(p, p->fg, 0, 0, p->width, p->height, 0); screen_fill(uxn_screen.fg, 0, 0, uxn_screen.width, uxn_screen.height, 0);
} }
void void
screen_redraw(UxnScreen *p) screen_redraw(void)
{ {
Uint32 i, x, y, w = p->width, palette[16], *pixels = p->pixels; Uint32 i, x, y, w = uxn_screen.width, palette[16], *pixels = uxn_screen.pixels;
Uint8 *fg = p->fg, *bg = p->bg; Uint8 *fg = uxn_screen.fg, *bg = uxn_screen.bg;
int x1 = p->x1, y1 = p->y1; int x1 = uxn_screen.x1;
int x2 = p->x2 > p->width ? p->width : p->x2, y2 = p->y2 > p->height ? p->height : p->y2; int y1 = uxn_screen.y1;
int x2 = uxn_screen.x2 > uxn_screen.width ? uxn_screen.width : uxn_screen.x2;
int y2 = uxn_screen.y2 > uxn_screen.height ? uxn_screen.height : uxn_screen.y2;
for(i = 0; i < 16; i++) for(i = 0; i < 16; i++)
palette[i] = p->palette[(i >> 2) ? (i >> 2) : (i & 3)]; palette[i] = uxn_screen.palette[(i >> 2) ? (i >> 2) : (i & 3)];
for(y = y1; y < y2; y++) for(y = y1; y < y2; y++)
for(x = x1; x < x2; x++) { for(x = x1; x < x2; x++) {
i = x + y * w; i = x + y * w;
pixels[i] = palette[fg[i] << 2 | bg[i]]; pixels[i] = palette[fg[i] << 2 | bg[i]];
} }
p->x1 = p->y1 = 0xffff; uxn_screen.x1 = uxn_screen.y1 = 0xffff;
p->x2 = p->y2 = 0; uxn_screen.x2 = uxn_screen.y2 = 0;
} }
Uint8 Uint8
@ -131,10 +133,10 @@ screen_deo(Uint8 *ram, Uint8 *d, Uint8 port)
{ {
switch(port) { switch(port) {
case 0x3: case 0x3:
screen_resize(&uxn_screen, PEEK2(d + 2), uxn_screen.height); screen_resize(PEEK2(d + 2), uxn_screen.height);
break; break;
case 0x5: case 0x5:
screen_resize(&uxn_screen, uxn_screen.width, PEEK2(d + 4)); screen_resize(uxn_screen.width, PEEK2(d + 4));
break; break;
case 0xe: { case 0xe: {
Uint8 ctrl = d[0xe]; Uint8 ctrl = d[0xe];
@ -148,8 +150,8 @@ screen_deo(Uint8 *ram, Uint8 *d, Uint8 port)
Uint16 y2 = uxn_screen.height; Uint16 y2 = uxn_screen.height;
if(ctrl & 0x10) x2 = x, x = 0; if(ctrl & 0x10) x2 = x, x = 0;
if(ctrl & 0x20) y2 = y, y = 0; if(ctrl & 0x20) y2 = y, y = 0;
screen_fill(&uxn_screen, layer, x, y, x2, y2, color); screen_fill(layer, x, y, x2, y2, color);
screen_change(&uxn_screen, x, y, x2, y2); screen_change(x, y, x2, y2);
} }
/* pixel mode */ /* pixel mode */
else { else {
@ -157,7 +159,7 @@ screen_deo(Uint8 *ram, Uint8 *d, Uint8 port)
Uint16 height = uxn_screen.height; Uint16 height = uxn_screen.height;
if(x < width && y < height) if(x < width && y < height)
layer[x + y * width] = color; layer[x + y * width] = color;
screen_change(&uxn_screen, x, y, x + 1, y + 1); screen_change(x, y, x + 1, y + 1);
if(d[0x6] & 0x1) POKE2(d + 0x8, x + 1); /* auto x+1 */ if(d[0x6] & 0x1) POKE2(d + 0x8, x + 1); /* auto x+1 */
if(d[0x6] & 0x2) POKE2(d + 0xa, y + 1); /* auto y+1 */ if(d[0x6] & 0x2) POKE2(d + 0xa, y + 1); /* auto y+1 */
} }
@ -176,10 +178,10 @@ screen_deo(Uint8 *ram, Uint8 *d, Uint8 port)
Uint16 dy = (move & 0x2) << 2; Uint16 dy = (move & 0x2) << 2;
Uint8 *layer = (ctrl & 0x40) ? uxn_screen.fg : uxn_screen.bg; Uint8 *layer = (ctrl & 0x40) ? uxn_screen.fg : uxn_screen.bg;
for(i = 0; i <= length; i++) { for(i = 0; i <= length; i++) {
screen_blit(&uxn_screen, layer, x + dy * i, y + dx * i, ram, addr, ctrl & 0xf, ctrl & 0x10, ctrl & 0x20, twobpp); screen_blit(layer, x + dy * i, y + dx * i, ram, addr, ctrl & 0xf, ctrl & 0x10, ctrl & 0x20, twobpp);
addr += (move & 0x04) << (1 + twobpp); addr += (move & 0x04) << (1 + twobpp);
} }
screen_change(&uxn_screen, x, y, x + dy * length + 8, y + dx * length + 8); screen_change(x, y, x + dy * length + 8, y + dx * length + 8);
if(move & 0x1) POKE2(d + 0x8, x + dx); /* auto x+8 */ if(move & 0x1) POKE2(d + 0x8, x + dx); /* auto x+8 */
if(move & 0x2) POKE2(d + 0xa, y + dy); /* auto y+8 */ if(move & 0x2) POKE2(d + 0xa, y + dy); /* auto y+8 */
if(move & 0x4) POKE2(d + 0xc, addr); /* auto addr+length */ if(move & 0x4) POKE2(d + 0xc, addr); /* auto addr+length */

View File

@ -17,10 +17,8 @@ typedef struct UxnScreen {
} UxnScreen; } UxnScreen;
extern UxnScreen uxn_screen; extern UxnScreen uxn_screen;
void screen_palette(Uint8 *addr);
void screen_palette(UxnScreen *p, Uint8 *addr); void screen_resize(Uint16 width, Uint16 height);
void screen_resize(UxnScreen *p, Uint16 width, Uint16 height); void screen_redraw(void);
void screen_redraw(UxnScreen *p);
Uint8 screen_dei(Uxn *u, Uint8 addr); Uint8 screen_dei(Uxn *u, Uint8 addr);
void screen_deo(Uint8 *ram, Uint8 *d, Uint8 port); void screen_deo(Uint8 *ram, Uint8 *d, Uint8 port);

View File

@ -115,7 +115,7 @@ uxn_deo(Uxn *u, Uint8 addr)
case 0x00: case 0x00:
system_deo(u, &u->dev[d], p); system_deo(u, &u->dev[d], p);
if(p > 0x7 && p < 0xe) if(p > 0x7 && p < 0xe)
screen_palette(&uxn_screen, &u->dev[0x8]); screen_palette(&u->dev[0x8]);
break; break;
case 0x10: console_deo(&u->dev[d], p); break; case 0x10: console_deo(&u->dev[d], p); break;
case 0x20: screen_deo(u->ram, &u->dev[d], p); break; case 0x20: screen_deo(u->ram, &u->dev[d], p); break;
@ -194,8 +194,9 @@ set_size(void)
static void static void
redraw(void) redraw(void)
{ {
if(gRect.w != uxn_screen.width || gRect.h != uxn_screen.height) set_size(); if(gRect.w != uxn_screen.width || gRect.h != uxn_screen.height)
screen_redraw(&uxn_screen); set_size();
screen_redraw();
if(SDL_UpdateTexture(gTexture, NULL, uxn_screen.pixels, uxn_screen.width * sizeof(Uint32)) != 0) if(SDL_UpdateTexture(gTexture, NULL, uxn_screen.pixels, uxn_screen.width * sizeof(Uint32)) != 0)
system_error("SDL_UpdateTexture", SDL_GetError()); system_error("SDL_UpdateTexture", SDL_GetError());
SDL_RenderClear(gRenderer); SDL_RenderClear(gRenderer);
@ -289,7 +290,7 @@ capture_screen(void)
static void static void
restart(Uxn *u) restart(Uxn *u)
{ {
screen_resize(&uxn_screen, WIDTH, HEIGHT); screen_resize(WIDTH, HEIGHT);
if(!start(u, "launcher.rom", 0)) if(!start(u, "launcher.rom", 0))
start(u, rom_path, 0); start(u, rom_path, 0);
} }
@ -366,7 +367,7 @@ handle_events(Uxn *u)
else if(event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_EXPOSED) else if(event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_EXPOSED)
redraw(); redraw();
else if(event.type == SDL_DROPFILE) { else if(event.type == SDL_DROPFILE) {
screen_resize(&uxn_screen, WIDTH, HEIGHT); screen_resize(WIDTH, HEIGHT);
start(u, event.drop.file, 0); start(u, event.drop.file, 0);
SDL_free(event.drop.file); SDL_free(event.drop.file);
} }
@ -489,7 +490,7 @@ main(int argc, char **argv)
if(!init()) if(!init())
return system_error("Init", "Failed to initialize emulator."); return system_error("Init", "Failed to initialize emulator.");
/* default resolution */ /* default resolution */
screen_resize(&uxn_screen, WIDTH, HEIGHT); screen_resize(WIDTH, HEIGHT);
/* default zoom */ /* default zoom */
if(argc > 1 && (strcmp(argv[i], "-1x") == 0 || strcmp(argv[i], "-2x") == 0 || strcmp(argv[i], "-3x") == 0)) if(argc > 1 && (strcmp(argv[i], "-1x") == 0 || strcmp(argv[i], "-2x") == 0 || strcmp(argv[i], "-3x") == 0))
set_zoom(argv[i++][1] - '0'); set_zoom(argv[i++][1] - '0');