]> Dogcows Code - chaz/rasterize/blob - main.c
refactor the animation script a bit
[chaz/rasterize] / main.c
1
2 /*
3 * CS5600 University of Utah
4 * Charles McGarvey
5 * mcgarvey@eng.utah.edu
6 */
7
8 #define _POSIX_C_SOURCE 2
9 #include <errno.h>
10 #include <unistd.h>
11
12 #include "raster.h"
13 #include "scene.h"
14
15
16 /*
17 * Load a scene from a file.
18 */
19 static int load(scene_t** scene, const char* filename)
20 {
21 FILE* file = fopen(filename, "r");
22 if (file == NULL) {
23 fprintf(stderr, "Cannot read %s: %s\n", filename, strerror(errno));
24 return -1;
25 }
26
27 *scene = scene_alloc(file);
28 if (*scene == NULL) {
29 fclose(file);
30 return -1;
31 }
32
33 fclose(file);
34 return 0;
35 }
36
37 /*
38 * Load a scene from standard input.
39 */
40 static int load_from_stdin(scene_t** scene)
41 {
42 *scene = scene_alloc(stdin);
43 if (*scene == NULL) {
44 return -1;
45 }
46 return 0;
47 }
48
49 /*
50 * Load a scene file, render it, and export it.
51 */
52 static void draw(const char* filename)
53 {
54 scene_t* scene;
55 TRY_DO("Loading %s", load(&scene, filename), filename);
56
57 raster_t* raster = scene_render(scene);
58 scene_destroy(scene);
59 raster_printstats(raster);
60
61 char* u3d = mem_strdup(filename);
62 strcut(u3d, '.');
63 #if EXPORT_PPM
64 char* ppm = mem_strcat(u3d, ".ppm");
65 TRY_DO("Exporting to %s", raster_export_ppm(raster, ppm), ppm);
66 mem_free(ppm);
67 #endif
68 #if EXPORT_BMP
69 char* bmp = mem_strcat(u3d, ".bmp");
70 TRY_DO("Exporting to %s", raster_export_bmp(raster, bmp), bmp);
71 mem_free(bmp);
72 #endif
73
74 mem_free(u3d);
75 raster_destroy(raster);
76 }
77
78 /*
79 * Render a scene that is read from standard input, and export to BMP.
80 */
81 static void draw_from_stdin(const char* filename)
82 {
83 scene_t* scene;
84 TRY_DO("Loading from stdin", load_from_stdin(&scene));
85
86 raster_t* raster = scene_render(scene);
87 scene_destroy(scene);
88 raster_printstats(raster);
89
90 TRY_DO("Exporting to %s", raster_export_bmp(raster, filename), filename);
91
92 raster_destroy(raster);
93 }
94
95 /*
96 * Render one or more scenes from 3D scene files.
97 */
98 int main(int argc, char* argv[])
99 {
100 int out = 0;
101 int c;
102 while ((c = getopt(argc, argv, "o:")) != -1) {
103 switch (c) {
104 case 'o':
105 ++out;
106 draw_from_stdin(optarg);
107 }
108 }
109
110 if (out == 0 && argc <= 1) {
111 draw("scene.u3d");
112 }
113 else {
114 for (int i = optind; i < argc; ++i) {
115 draw(argv[i]);
116 }
117 }
118
119 #if !NDEBUG
120 int _blocks = mem_blocks();
121 if (_blocks != 0) {
122 fprintf(stderr, " *** Leaked %d blocks of memory! ***\n", _blocks);
123 return EXIT_FAILURE;
124 }
125 #endif
126 return EXIT_SUCCESS;
127 }
128
This page took 0.035373 seconds and 4 git commands to generate.