Prefixed globals with uxn_

This commit is contained in:
neauoire 2021-12-29 09:11:03 -08:00
parent 23a514b3ba
commit 385fa44f9d
6 changed files with 61 additions and 58 deletions

View File

@ -23,12 +23,12 @@ static Uint32 advances[12] = {
0xb504f, 0xbfc88, 0xcb2ff, 0xd7450, 0xe411f, 0xf1a1c
};
Audio audio[POLYPHONY];
UxnAudio uxn_audio[POLYPHONY];
/* clang-format on */
static Sint32
envelope(Audio *c, Uint32 age)
envelope(UxnAudio *c, Uint32 age)
{
if(!c->r) return 0x0888;
if(age < c->a) return 0x0888 * age / c->a;
@ -40,7 +40,7 @@ envelope(Audio *c, Uint32 age)
}
int
audio_render(Audio *c, Sint16 *sample, Sint16 *end)
audio_render(UxnAudio *c, Sint16 *sample, Sint16 *end)
{
Sint32 s;
if(!c->advance || !c->period) return 0;
@ -64,7 +64,7 @@ audio_render(Audio *c, Sint16 *sample, Sint16 *end)
}
void
audio_start(Audio *c, Uint16 adsr, Uint8 pitch)
audio_start(UxnAudio *c, Uint16 adsr, Uint8 pitch)
{
if(pitch < 108 && c->len)
c->advance = advances[pitch % 12] >> (8 - pitch / 12);
@ -85,7 +85,7 @@ audio_start(Audio *c, Uint16 adsr, Uint8 pitch)
}
Uint8
audio_get_vu(Audio *c)
audio_get_vu(UxnAudio *c)
{
int i;
Sint32 sum[2] = {0, 0};

View File

@ -21,11 +21,11 @@ typedef struct {
Uint16 i, len;
Sint8 volume[2];
Uint8 pitch, repeat;
} Audio;
} UxnAudio;
extern Audio audio[POLYPHONY];
extern UxnAudio uxn_audio[POLYPHONY];
Uint8 audio_get_vu(Audio *c);
int audio_render(Audio *c, Sint16 *sample, Sint16 *end);
void audio_start(Audio *c, Uint16 adsr, Uint8 pitch);
void audio_finished_handler(Audio *c);
Uint8 audio_get_vu(UxnAudio *c);
int audio_render(UxnAudio *c, Sint16 *sample, Sint16 *end);
void audio_start(UxnAudio *c, Uint16 adsr, Uint8 pitch);
void audio_finished_handler(UxnAudio *c);

View File

@ -13,7 +13,7 @@ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE.
*/
Screen screen;
UxnScreen uxn_screen;
static Uint8 blending[5][16] = {
{0, 0, 0, 0, 1, 0, 1, 1, 2, 2, 0, 2, 3, 3, 3, 0},
@ -41,7 +41,7 @@ static Uint8 font[][8] = {
{0x00, 0x7c, 0x82, 0x80, 0xf0, 0x80, 0x80, 0x80}};
static void
screen_write(Screen *p, Layer *layer, Uint16 x, Uint16 y, Uint8 color)
screen_write(UxnScreen *p, Layer *layer, Uint16 x, Uint16 y, Uint8 color)
{
if(x < p->width && y < p->height) {
Uint32 i = x + y * p->width;
@ -53,7 +53,7 @@ screen_write(Screen *p, Layer *layer, Uint16 x, Uint16 y, Uint8 color)
}
static void
screen_blit(Screen *p, Layer *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy, Uint8 twobpp)
screen_blit(UxnScreen *p, Layer *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy, Uint8 twobpp)
{
int v, h, opaque = blending[4][color];
for(v = 0; v < 8; ++v) {
@ -71,7 +71,7 @@ screen_blit(Screen *p, Layer *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 co
}
void
screen_palette(Screen *p, Uint8 *addr)
screen_palette(UxnScreen *p, Uint8 *addr)
{
int i, shift;
for(i = 0, shift = 4; i < 4; ++i, shift ^= 4) {
@ -86,7 +86,7 @@ screen_palette(Screen *p, Uint8 *addr)
}
void
screen_resize(Screen *p, Uint16 width, Uint16 height)
screen_resize(UxnScreen *p, Uint16 width, Uint16 height)
{
Uint8
*bg = realloc(p->bg.pixels, width * height),
@ -106,7 +106,7 @@ screen_resize(Screen *p, Uint16 width, Uint16 height)
}
void
screen_clear(Screen *p, Layer *layer)
screen_clear(UxnScreen *p, Layer *layer)
{
Uint32 i, size = p->width * p->height;
for(i = 0; i < size; ++i)
@ -115,7 +115,7 @@ screen_clear(Screen *p, Layer *layer)
}
void
screen_redraw(Screen *p, Uint32 *pixels)
screen_redraw(UxnScreen *p, Uint32 *pixels)
{
Uint32 i, size = p->width * p->height, palette[16];
for(i = 0; i < 16; ++i)
@ -126,7 +126,7 @@ screen_redraw(Screen *p, Uint32 *pixels)
}
void
screen_debug(Screen *p, Uint8 *stack, Uint8 wptr, Uint8 rptr, Uint8 *memory)
screen_debug(UxnScreen *p, Uint8 *stack, Uint8 wptr, Uint8 rptr, Uint8 *memory)
{
Uint8 i, x, y, b;
for(i = 0; i < 0x20; ++i) {
@ -160,10 +160,10 @@ Uint8
screen_dei(Device *d, Uint8 port)
{
switch(port) {
case 0x2: return screen.width >> 8;
case 0x3: return screen.width;
case 0x4: return screen.height >> 8;
case 0x5: return screen.height;
case 0x2: return uxn_screen.width >> 8;
case 0x3: return uxn_screen.width;
case 0x4: return uxn_screen.height >> 8;
case 0x5: return uxn_screen.height;
default: return d->dat[port];
}
}
@ -180,7 +180,7 @@ screen_deo(Device *d, Uint8 port)
Uint16 x = peek16(d->dat, 0x8);
Uint16 y = peek16(d->dat, 0xa);
Uint8 layer = d->dat[0xe] & 0x40;
screen_write(&screen, layer ? &screen.fg : &screen.bg, x, y, d->dat[0xe] & 0x3);
screen_write(&uxn_screen, layer ? &uxn_screen.fg : &uxn_screen.bg, x, y, d->dat[0xe] & 0x3);
if(d->dat[0x6] & 0x01) poke16(d->dat, 0x8, x + 1); /* auto x+1 */
if(d->dat[0x6] & 0x02) poke16(d->dat, 0xa, y + 1); /* auto y+1 */
break;
@ -188,10 +188,10 @@ screen_deo(Device *d, Uint8 port)
case 0xf: {
Uint16 x = peek16(d->dat, 0x8);
Uint16 y = peek16(d->dat, 0xa);
Layer *layer = (d->dat[0xf] & 0x40) ? &screen.fg : &screen.bg;
Layer *layer = (d->dat[0xf] & 0x40) ? &uxn_screen.fg : &uxn_screen.bg;
Uint8 *addr = &d->mem[peek16(d->dat, 0xc)];
Uint8 twobpp = !!(d->dat[0xf] & 0x80);
screen_blit(&screen, layer, x, y, addr, d->dat[0xf] & 0xf, d->dat[0xf] & 0x10, d->dat[0xf] & 0x20, twobpp);
screen_blit(&uxn_screen, layer, x, y, addr, d->dat[0xf] & 0xf, d->dat[0xf] & 0x10, d->dat[0xf] & 0x20, twobpp);
if(d->dat[0x6] & 0x04) poke16(d->dat, 0xc, peek16(d->dat, 0xc) + 8 + twobpp * 8); /* auto addr+8 / auto addr+16 */
if(d->dat[0x6] & 0x01) poke16(d->dat, 0x8, x + 8); /* auto x+8 */
if(d->dat[0x6] & 0x02) poke16(d->dat, 0xa, y + 8); /* auto y+8 */

View File

@ -19,22 +19,22 @@ typedef struct Layer {
Uint8 changed;
} Layer;
typedef struct Screen {
typedef struct UxnScreen {
Uint32 palette[4], *pixels;
Uint16 width, height;
Layer fg, bg;
} Screen;
} UxnScreen;
extern Screen screen;
extern UxnScreen uxn_screen;
/* this should probably be done differently */
int set_size(Uint16 width, Uint16 height, int is_resize);
void screen_palette(Screen *p, Uint8 *addr);
void screen_resize(Screen *p, Uint16 width, Uint16 height);
void screen_clear(Screen *p, Layer *layer);
void screen_redraw(Screen *p, Uint32 *pixels);
void screen_debug(Screen *p, Uint8 *stack, Uint8 wptr, Uint8 rptr, Uint8 *memory);
void screen_palette(UxnScreen *p, Uint8 *addr);
void screen_resize(UxnScreen *p, Uint16 width, Uint16 height);
void screen_clear(UxnScreen *p, Layer *layer);
void screen_redraw(UxnScreen *p, Uint32 *pixels);
void screen_debug(UxnScreen *p, Uint8 *stack, Uint8 wptr, Uint8 rptr, Uint8 *memory);
Uint8 screen_dei(Device *d, Uint8 port);
void screen_deo(Device *d, Uint8 port);

View File

@ -246,9 +246,12 @@ parse(char *w, FILE *f)
if(slen(w) != 1) fprintf(stderr, "-- Malformed comment: %s\n", w);
i = 1; /* track nested comment depth */
while(fscanf(f, "%63s", word) == 1) {
if(slen(word) != 1) continue;
else if(word[0] == '(') i++;
else if(word[0] == ')' && --i < 1) break;
if(slen(word) != 1)
continue;
else if(word[0] == '(')
i++;
else if(word[0] == ')' && --i < 1)
break;
}
break;
case '~': /* include */

View File

@ -66,17 +66,17 @@ audio_callback(void *u, Uint8 *stream, int len)
Sint16 *samples = (Sint16 *)stream;
SDL_memset(stream, 0, len);
for(i = 0; i < POLYPHONY; ++i)
running += audio_render(&audio[i], samples, samples + len / 2);
running += audio_render(&uxn_audio[i], samples, samples + len / 2);
if(!running)
SDL_PauseAudioDevice(audio_id, 1);
(void)u;
}
void
audio_finished_handler(Audio *c)
audio_finished_handler(UxnAudio *c)
{
SDL_Event event;
event.type = audio0_event + (c - audio);
event.type = audio0_event + (c - uxn_audio);
SDL_PushEvent(&event);
}
@ -104,20 +104,20 @@ set_window_size(SDL_Window *window, int w, int h)
int
set_size(Uint16 width, Uint16 height, int is_resize)
{
screen_resize(&screen, width, height);
screen_resize(&uxn_screen, width, height);
gRect.x = PAD;
gRect.y = PAD;
gRect.w = screen.width;
gRect.h = screen.height;
gRect.w = uxn_screen.width;
gRect.h = uxn_screen.height;
if(gTexture != NULL) SDL_DestroyTexture(gTexture);
SDL_RenderSetLogicalSize(gRenderer, screen.width + PAD * 2, screen.height + PAD * 2);
gTexture = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, screen.width + PAD * 2, screen.height + PAD * 2);
SDL_RenderSetLogicalSize(gRenderer, uxn_screen.width + PAD * 2, uxn_screen.height + PAD * 2);
gTexture = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, uxn_screen.width + PAD * 2, uxn_screen.height + PAD * 2);
if(gTexture == NULL || SDL_SetTextureBlendMode(gTexture, SDL_BLENDMODE_NONE))
return error("gTexture", SDL_GetError());
if(SDL_UpdateTexture(gTexture, NULL, screen.pixels, sizeof(Uint32)) != 0)
if(SDL_UpdateTexture(gTexture, NULL, uxn_screen.pixels, sizeof(Uint32)) != 0)
return error("SDL_UpdateTexture", SDL_GetError());
if(is_resize)
set_window_size(gWindow, (screen.width + PAD * 2) * zoom, (screen.height + PAD * 2) * zoom);
set_window_size(gWindow, (uxn_screen.width + PAD * 2) * zoom, (uxn_screen.height + PAD * 2) * zoom);
return 1;
}
@ -125,9 +125,9 @@ static void
redraw(Uxn *u)
{
if(devsystem->dat[0xe])
screen_debug(&screen, u->wst.dat, u->wst.ptr, u->rst.ptr, u->ram.dat);
screen_redraw(&screen, screen.pixels);
if(SDL_UpdateTexture(gTexture, &gRect, screen.pixels, screen.width * sizeof(Uint32)) != 0)
screen_debug(&uxn_screen, u->wst.dat, u->wst.ptr, u->rst.ptr, u->ram.dat);
screen_redraw(&uxn_screen, uxn_screen.pixels);
if(SDL_UpdateTexture(gTexture, &gRect, uxn_screen.pixels, uxn_screen.width * sizeof(Uint32)) != 0)
error("SDL_UpdateTexture", SDL_GetError());
SDL_RenderClear(gRenderer);
SDL_RenderCopy(gRenderer, gTexture, NULL, NULL);
@ -188,7 +188,7 @@ system_deo(Device *d, Uint8 port)
case 0x3: d->u->rst.ptr = d->dat[port]; break;
}
if(port > 0x7 && port < 0xe)
screen_palette(&screen, &d->dat[0x8]);
screen_palette(&uxn_screen, &d->dat[0x8]);
}
static void
@ -203,7 +203,7 @@ console_deo(Device *d, Uint8 port)
static Uint8
audio_dei(Device *d, Uint8 port)
{
Audio *c = &audio[d - devaudio0];
UxnAudio *c = &uxn_audio[d - devaudio0];
if(!audio_id) return d->dat[port];
switch(port) {
case 0x4: return audio_get_vu(c);
@ -215,7 +215,7 @@ audio_dei(Device *d, Uint8 port)
static void
audio_deo(Device *d, Uint8 port)
{
Audio *c = &audio[d - devaudio0];
UxnAudio *c = &uxn_audio[d - devaudio0];
if(!audio_id) return;
if(port == 0xf) {
SDL_LockAudioDevice(audio_id);
@ -317,14 +317,14 @@ static void
set_zoom(Uint8 scale)
{
zoom = clamp(scale, 1, 3);
set_window_size(gWindow, (screen.width + PAD * 2) * zoom, (screen.height + PAD * 2) * zoom);
set_window_size(gWindow, (uxn_screen.width + PAD * 2) * zoom, (uxn_screen.height + PAD * 2) * zoom);
}
static void
toggle_debugger(void)
{
devsystem->dat[0xe] = !devsystem->dat[0xe];
screen_clear(&screen, &screen.fg);
screen_clear(&uxn_screen, &uxn_screen.fg);
}
static void
@ -449,8 +449,8 @@ run(Uxn *u)
/* Mouse */
else if(event.type == SDL_MOUSEMOTION)
mouse_pos(devmouse,
clamp(event.motion.x - PAD, 0, screen.width - 1),
clamp(event.motion.y - PAD, 0, screen.height - 1));
clamp(event.motion.x - PAD, 0, uxn_screen.width - 1),
clamp(event.motion.y - PAD, 0, uxn_screen.height - 1));
else if(event.type == SDL_MOUSEBUTTONUP)
mouse_up(devmouse, 0x1 << (event.button.button - 1));
else if(event.type == SDL_MOUSEBUTTONDOWN)
@ -484,7 +484,7 @@ run(Uxn *u)
console_input(u, event.cbutton.button);
}
uxn_eval(u, devscreen->vector);
if(screen.fg.changed || screen.bg.changed || devsystem->dat[0xe])
if(uxn_screen.fg.changed || uxn_screen.bg.changed || devsystem->dat[0xe])
redraw(u);
if(!BENCH) {
elapsed = (SDL_GetPerformanceCounter() - begin) / (double)SDL_GetPerformanceFrequency() * 1000.0f;