minor cleanups to display and callback routines
[dcpu16] / display.c
index c1e6c20aa650b1404b9b51baf308fe28b7b6fc60..2c8485a2e41309402405ef5d21312660308487d7 100644 (file)
--- 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,8 +95,8 @@ 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 * sizeof index));
 
@@ -117,7 +122,7 @@ void display_draw_cell(DPIX *pixbuf, DCPU16_WORD *cell_map, DCPU16_WORD index, i
 }
 
 /* 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;
 }