support png output, buffered file writing
[dcpu16] / vm-dcpu16.c
index 1e3a49656757f8e68cd90ebae13a12bd75018ed1..5924074aa468ce5c5370538fd819864ef69d53c2 100644 (file)
@@ -365,43 +365,61 @@ COMMAND_HELP(run) {
                "May be interrupted with SIGINT.\n");
 }
 
+static const char * const display_filename_default_ =
+#ifdef HAVE_LIBPNG
+    "dcpu16-display.png"
+#else /* HAVE_LIBPNG */
+    "dcpu16-display.pnm"
+#endif /* HAVE_LIBPNG */
+;
 COMMAND_IMPL(display) {
-    (void)arg_count, (void)arg_vector;
+    static DCPU16_DISPLAY *display = NULL;
+    const char *filename = display_filename_default_;
 
-    static DPIX *pixbuf = NULL;
+    if (arg_count == 2) {
+        filename = arg_vector[1];
+    }
 
-    if (pixbuf) {
+    if (display) {
         printf("display already enabled..\n");
         return 0;
     }
 
-    pixbuf = display_init_pixbuf();
-    if (pixbuf == NULL) {
+    display = display_new(filename);
+
+    if (display == NULL) {
         fprintf(stderr, "failed to initialize display buffer\n");
         return 0;
     }
 
-    if (dcpu16_acct_add(vm, DCPU16_ACCT_EV_WRITE, display_fn, pixbuf)) {
+    if (dcpu16_acct_add(vm, DCPU16_ACCT_EV_WRITE, display_fn, display)) {
         fprintf(stderr, "failed to register display update callback\n");
         return 0;
     }
 
-    if (dcpu16_acct_add(vm, DCPU16_ACCT_EV_RESET, display_reset_fn, pixbuf)) {
+    if (dcpu16_acct_add(vm, DCPU16_ACCT_EV_RESET, display_reset_fn, display)) {
         fprintf(stderr, "failed to register display reset callback\n");
         return 0;
     }
 
+    if (dcpu16_acct_add(vm, DCPU16_ACCT_EV_CYCLE, display_cycle_fn, display)) {
+        fprintf(stderr, "failed to register display cycle callback\n");
+        return 0;
+    }
+
     /* init display as if reset occurred */
-    display_reset_fn(vm, DCPU16_ACCT_EV_RESET, 0, pixbuf);
+    display_reset_fn(vm, DCPU16_ACCT_EV_RESET, 0, display);
 
     return 0;
 }
 COMMAND_HELP(display) {
-    fprintf(f, "\tdisplay\n");
+    fprintf(f, "\tdisplay [file]\n");
     if (summary) return;
 
-    fprintf(f, "Begins updating a PNM of current display contents...\n"
-               "This is not a fully-baked feature...\n");
+    fprintf(f, "Attaches display interface, begins updating an image file of display contents...\n"
+               "Image filename may be specified, defaults to '%s'\n",
+               display_filename_default_
+            );
 }
 
 /* gather all these together into a searchable table */
@@ -419,7 +437,7 @@ static struct command_ command_table_[] = {
     COMMAND_ENTRY(step, 0, 1),
     COMMAND_ENTRY(run, 0, 0),
     COMMAND_ENTRY(reset, 0, 0),
-    COMMAND_ENTRY(display, 0, 0),
+    COMMAND_ENTRY(display, 0, 1),
     { NULL, 0, 0, NULL, NULL }
 };