]> Dogcows Code - chaz/rasterize/blobdiff - main.c
add support for 3d scenes, depth testing, lighting
[chaz/rasterize] / main.c
diff --git a/main.c b/main.c
index ba2c5777db4f5c16009fc606f76f913752bd2ed7..08a752e0494f9eb17139588a68bead39f7cce363 100644 (file)
--- a/main.c
+++ b/main.c
@@ -7,37 +7,69 @@
 
 #include <stdio.h>
 
-#include "pixmap.h"
+#include "raster.h"
 #include "scene.h"
 
 
 /*
- * Read a scene file, construct the scene object, draw the scene to a pixmap,
- * and export the pixmap in PPM and BMP formats.
+ * Load a scene from a file.
  */
-int main(int argc, char* argv[])
+static int load(scene_t** scene, const char* filename)
 {
-    scene_t* scene = scene_alloc("scene.u2d");
-    if (scene == NULL) {
-        fprintf(stderr, "An error prevented the scene from loading. Aborting!\n");
+    *scene = scene_alloc(filename);
+    if (*scene == NULL) {
         return 1;
     }
+    return 0;
+}
 
-    pixmap_t* raster = scene_render(scene);
+/*
+ * Load a scene file, render it, and export it to PPM and BMP formats.
+ */
+static void draw(const char* filename)
+{
+    scene_t* scene;
+    TRY_DO("Loading %s", load(&scene, filename), filename);
+
+    raster_t* raster = scene_render(scene);
     scene_destroy(scene);
+    raster_printstats(raster);
+
+    char* u3d = mem_strdup(filename);
+    strcut(u3d, '.');
+    char* ppm = mem_strcat(u3d, ".ppm");
+    char* bmp = mem_strcat(u3d, ".bmp");
+
+    TRY_DO("Exporting to %s", raster_export_ppm(raster, ppm), ppm);
+    TRY_DO("Exporting to %s", raster_export_bmp(raster, bmp), bmp);
 
-    pixmap_export_ppm(raster, "scene.ppm");
-    pixmap_export_bmp(raster, "scene.bmp");
+    mem_free(u3d);
+    mem_free(ppm);
+    mem_free(bmp);
+    raster_destroy(raster);
+}
 
-    pixmap_destroy(raster);
+/*
+ * Render one or more scenes from 3D scene files.
+ */
+int main(int argc, char* argv[])
+{
+    if (argc <= 1) {
+        draw("scene.u3d");
+    }
+    else {
+        for (int i = 1; i < argc; ++i) {
+            draw(argv[i]);
+        }
+    }
 
-#ifndef NDEBUG
+#if !NDEBUG
     int _blocks = mem_blocks();
     if (_blocks != 0) {
         fprintf(stderr, " *** Leaked %d blocks of memory! ***\n", _blocks);
-        return 1;
+        return EXIT_FAILURE;
     }
 #endif
-    return 0;
+    return EXIT_SUCCESS;
 }
 
This page took 0.02374 seconds and 4 git commands to generate.