X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=display.c;h=2c8485a2e41309402405ef5d21312660308487d7;hb=a4e2c238ea47badea198132fc5d9311d1f40ca13;hp=37db1d94e0b55c442eaed0bb3d69914a27db56f4;hpb=bca632cc1c329a45dde32f476b0ef9f6ef5db05a;p=dcpu16 diff --git a/display.c b/display.c index 37db1d9..2c8485a 100644 --- a/display.c +++ b/display.c @@ -36,9 +36,14 @@ struct pixel_ { char b; }; +struct dcpu16_display_ { + char *outfile; + struct pixel_ *pixbuf; +}; + static inline -DPIX pcolor_(unsigned int c) { - DPIX p = { 0, 0, 0 }; +struct pixel_ pcolor_(unsigned int c) { + struct pixel_ p = { 0, 0, 0 }; switch (c) { case 0x1: p.r=0x00, p.g=0x00, p.b=0xaa; break; /* dark blue */ @@ -64,7 +69,7 @@ DPIX pcolor_(unsigned int c) { /* should this just flood-fill entire display? */ static inline -void display_draw_border(DPIX *pixbuf, DPIX color) { +void display_draw_border(struct pixel_ *pixbuf, struct pixel_ color) { size_t x, y; size_t i; @@ -90,17 +95,17 @@ void display_draw_border(DPIX *pixbuf, DPIX color) { /* render a character cell to the display */ static inline -void display_draw_cell(DPIX *pixbuf, DCPU16_WORD *cell_map, DCPU16_WORD index, int cell_x, int cell_y, DPIX fg, DPIX bg) { - DPIX *cellstart = pixbuf; /* start of display */ +void display_draw_cell(struct pixel_ *pixbuf, DCPU16_WORD *cell_map, DCPU16_WORD index, int cell_x, int cell_y, struct pixel_ fg, struct pixel_ bg) { + struct pixel_ *cellstart = pixbuf; /* start of display */ unsigned int pix_x, pix_y; - unsigned char *cell_bitmap = (unsigned char *)(cell_map + index); + unsigned char *cell_bitmap = (unsigned char *)(cell_map + (index * sizeof index)); assert(cell_x < CELL_X); assert(cell_y < CELL_Y); cellstart += (PIX_X * PIX_BORDER); /* skip top border */ - cellstart += (CELL_Y_SZ) * cell_y; /* skip down to row */ + cellstart += (CELL_Y_SZ * PIX_X) * cell_y; /* skip down to row */ cellstart += PIX_BORDER; /* skip side border */ @@ -109,15 +114,15 @@ void display_draw_cell(DPIX *pixbuf, DCPU16_WORD *cell_map, DCPU16_WORD index, i for (pix_x = 0; pix_x < CELL_X_SZ; pix_x++) { for (pix_y = 0; pix_y < CELL_Y_SZ; pix_y++) { if ((cell_bitmap[pix_x] >> pix_y) & 0x01) - cellstart[(pix_y * PIX_X) + pix_x] = fg; + cellstart[((CELL_Y_SZ - pix_y - 1) * PIX_X) + pix_x] = fg; else - cellstart[(pix_y * PIX_X) + pix_x] = bg; + cellstart[((CELL_Y_SZ - pix_y - 1) * PIX_X) + pix_x] = bg; } } } /* write pnm file */ -void display_pnm_write(DPIX *pixbuf, const char *filename) { +void display_pnm_write(struct pixel_ *pixbuf, const char *filename) { size_t i; FILE *f; @@ -148,34 +153,21 @@ void display_pnm_write(DPIX *pixbuf, const char *filename) { /* currently this populates the chargen map 'from rom'.. */ /* and clears the display buffers */ void display_reset_fn(struct dcpu16 *vm, dcpu16_acct_event e, DCPU16_WORD addr, void *data) { - DPIX *pixbuf = (DPIX *)data; + struct dcpu16_display_ *d = (struct dcpu16_display_ *)data; (void)e, (void)addr; - fprintf(stderr, "DEBUG: event:%u\n", e); - - fprintf(stderr, "DEBUG: loading chargen map from 0x%04x to 0x%04x (%zuo)\n", - DISPLAY_CELL_MAP, - DISPLAY_CELL_MAP + (unsigned short)(sizeof chargen_4x8_glyphs / sizeof addr), - sizeof chargen_4x8_glyphs); memcpy(vm->ram + DISPLAY_CELL_MAP, chargen_4x8_glyphs, sizeof chargen_4x8_glyphs); - fprintf(stderr, "DEBUG: clearing display buffer from 0x%04x to 0x%04x (%zuo)\n", - DISPLAY_BASE, - DISPLAY_END, - (DISPLAY_END - DISPLAY_BASE) * sizeof *(vm->ram)); memset(vm->ram + DISPLAY_BASE, 0, (DISPLAY_END - DISPLAY_BASE) * sizeof *(vm->ram)); - fprintf(stderr, "DEBUG: clearing pixel buffer (%zuo)\n", - PIX_X * PIX_Y * sizeof *pixbuf); - memset(pixbuf, 0, PIX_X * PIX_Y * sizeof *pixbuf); + memset(d->pixbuf, 0, PIX_X * PIX_Y * sizeof *(d->pixbuf)); } /* the callback to register with the cpu for watching memory updates */ /* user data is an allocated display buffer */ void display_fn(struct dcpu16 *vm, dcpu16_acct_event e, DCPU16_WORD addr, void *data) { - const char * const outfile = "dcpu16-display.pnm"; - DPIX *pixbuf = (DPIX *)data; + DCPU16_DISPLAY *d = (DCPU16_DISPLAY *)data; unsigned char index, blink, bg, fg; unsigned int cell_x, cell_y; @@ -196,21 +188,20 @@ void display_fn(struct dcpu16 *vm, dcpu16_acct_event e, DCPU16_WORD addr, void * blink = (vm->ram[addr] >> 7) & 0x01; bg = (vm->ram[addr] >> 8) & 0x0f; fg = (vm->ram[addr] >> 12) & 0x0f; - display_draw_cell(pixbuf, vm->ram + DISPLAY_CELL_MAP, index, cell_x, cell_y, pcolor_(fg), pcolor_(bg)); + display_draw_cell(d->pixbuf, vm->ram + DISPLAY_CELL_MAP, index, cell_x, cell_y, pcolor_(fg), pcolor_(bg)); } } - display_pnm_write(pixbuf, outfile); + display_pnm_write(d->pixbuf, d->outfile); return; } if (addr == DISPLAY_MISC) { /* new border color */ char border = vm->ram[addr] & 0x0f; - fprintf(stderr, "display event: new border\n"); - display_draw_border(pixbuf, pcolor_(border)); + display_draw_border(d->pixbuf, pcolor_(border)); - display_pnm_write(pixbuf, outfile); + display_pnm_write(d->pixbuf, d->outfile); return; } @@ -222,17 +213,28 @@ void display_fn(struct dcpu16 *vm, dcpu16_acct_event e, DCPU16_WORD addr, void * bg = (vm->ram[addr] >> 8) & 0x0f; fg = (vm->ram[addr] >> 12) & 0x0f; - fprintf(stderr, "display event: cell %ux%u:%u '%c'\n", cell_x, cell_y, index, index); - - display_draw_cell(pixbuf, vm->ram + DISPLAY_CELL_MAP, index, cell_x, cell_y, pcolor_(fg), pcolor_(bg)); - display_pnm_write(pixbuf, outfile); + display_draw_cell(d->pixbuf, vm->ram + DISPLAY_CELL_MAP, index, cell_x, cell_y, pcolor_(fg), pcolor_(bg)); + display_pnm_write(d->pixbuf, d->outfile); } /* init the pixel buffer */ -DPIX *display_init_pixbuf(void) { - DPIX *pixbuf; +DCPU16_DISPLAY *display_new(const char *filename) { + DCPU16_DISPLAY *d = calloc(1, sizeof *d); + if (d == NULL) + return NULL; + + d->pixbuf = calloc(PIX_X * PIX_Y, sizeof *(d->pixbuf)); + if (d->pixbuf == NULL) { + free(d); + return NULL; + } - pixbuf = calloc(PIX_X * PIX_Y, sizeof *pixbuf); + d->outfile = strdup(filename); + if (d->outfile == NULL) { + free(d->pixbuf); + free(d); + return NULL; + } - return pixbuf; + return d; }