X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Frasterize;a=blobdiff_plain;f=main.c;fp=main.c;h=8f4ca4d4d7ab52636fcb4d3ed66ffe186a668b03;hp=83878bf5e70e0dc29dddbfc93460d96312c20f3e;hb=e16cf0578f4baaf879e4ab9d3528a765bfd29be0;hpb=db44364a1fabda81d3f6c842c4e4778eac6495a5 diff --git a/main.c b/main.c index 83878bf..8f4ca4d 100644 --- a/main.c +++ b/main.c @@ -5,52 +5,60 @@ * mcgarvey@eng.utah.edu */ -#include +#define _POSIX_C_SOURCE 2 +#include +#include #include "raster.h" #include "scene.h" -#ifndef EXPORT_PPM -#define EXPORT_PPM 1 -#endif -#ifndef EXPORT_BMP -#define EXPORT_BMP 1 -#endif - - /* * Load a scene from a file. */ static int load(scene_t** scene, const char* filename) { - *scene = scene_alloc(filename); + FILE* file = fopen(filename, "r"); + if (file == NULL) { + fprintf(stderr, "Cannot read %s: %s\n", filename, strerror(errno)); + return -1; + } + + *scene = scene_alloc(file); if (*scene == NULL) { - return 1; + fclose(file); + return -1; } + + fclose(file); return 0; } /* - * Load a scene file, render it, and export it to PPM and BMP formats. + * Load a scene from standard input. */ -static void draw(const char* filename) +static int load_from_stdin(scene_t** scene) { - char* u3d; - if (strcmp(filename, "-") == 0) { - u3d = mem_strdup("stdin"); - } - else { - u3d = mem_strdup(filename); + *scene = scene_alloc(stdin); + if (*scene == NULL) { + return -1; } + return 0; +} +/* + * Load a scene file, render it, and export it. + */ +static void draw(const char* filename) +{ scene_t* scene; - TRY_DO("Loading %s", load(&scene, filename), u3d); + 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, '.'); #if EXPORT_PPM char* ppm = mem_strcat(u3d, ".ppm"); @@ -67,16 +75,43 @@ static void draw(const char* filename) raster_destroy(raster); } +/* + * Render a scene that is read from standard input, and export to BMP. + */ +static void draw_from_stdin(const char* filename) +{ + scene_t* scene; + TRY_DO("Loading from stdin", load_from_stdin(&scene)); + + raster_t* raster = scene_render(scene); + scene_destroy(scene); + raster_printstats(raster); + + TRY_DO("Exporting to %s", raster_export_bmp(raster, filename), filename); + + raster_destroy(raster); +} + /* * Render one or more scenes from 3D scene files. */ int main(int argc, char* argv[]) { - if (argc <= 1) { + int out = 0; + int c; + while ((c = getopt(argc, argv, "o:")) != -1) { + switch (c) { + case 'o': + ++out; + draw_from_stdin(optarg); + } + } + + if (out == 0 && argc <= 1) { draw("scene.u3d"); } else { - for (int i = 1; i < argc; ++i) { + for (int i = optind; i < argc; ++i) { draw(argv[i]); } }