c9d236232c9c0e4a3bc5decacecb813ee4b6a83a
[ipdf/code.git] / src / main.cpp
1 #include "main.h"
2 #include <unistd.h> // Because we can.
3
4 #include "controlpanel.h"
5
6 #ifndef _GNU_SOURCE
7 #define _GNU_SOURCE
8 #endif
9 #include <fenv.h>
10 #include <signal.h>
11
12 bool ignore_sigfpe = false;
13 const char *script_filename;
14 bool make_movie = false;
15 const char * program_name;
16
17 void sigfpe_handler(int sig)
18 {
19         if (!ignore_sigfpe)
20                 Fatal("Floating point exception!");
21         exit(EXIT_SUCCESS);
22 }
23
24 int main(int argc, char ** argv)
25 {       
26         program_name = argv[0];
27         
28         //Debug("Main!");
29         signal(SIGFPE, sigfpe_handler);
30         #if REALTYPE == REAL_IRRAM
31           iRRAM_initialize(argc,argv);
32         #endif
33         
34         #ifndef __STDC_IEC_559__
35         Warn("__STDC_IEC_559__ not defined. IEEE 754 floating point not fully supported.\n");
36         #endif
37
38         // We want to crash if we ever get a NaN.
39         // Not any more
40         #ifndef __MINGW32__
41         //feenableexcept(FE_DIVBYZERO | FE_INVALID); // | FE_OVERFLOW);
42         #endif
43         #if REALTYPE == REAL_MPFRCPP
44         
45                 #ifdef MPFR_PRECISION
46                 mpfr_set_default_prec(MPFR_PRECISION);
47                 #else
48                 mpfr_set_default_prec(23);
49                 #endif
50                 
51         #endif
52         DebugRealInfo();
53
54
55
56         Document doc("","fonts/ComicSans.ttf");
57         srand(time(NULL));
58
59         enum {OUTPUT_TO_BMP, LOOP} mode = LOOP;
60         
61         
62         Colour c(0,0,0,1);
63         
64         const char * output_bmp = NULL;
65         const char * input_filename = NULL;
66         const char * input_text = NULL;
67         Real b[4] = {0,0,1,1};
68         int max_frames = -1;
69         bool hide_control_panel = false;
70         bool lazy_rendering = true;
71         bool window_visible = true;
72         bool gpu_transform = USE_GPU_TRANSFORM;
73         bool gpu_rendering = USE_GPU_RENDERING;
74         #ifdef TRANSFORM_OBJECTS_NOT_VIEW
75                 gpu_transform = true;
76         #endif
77         #ifdef TRANSFORM_BEZIERS_TO_PATH
78                 gpu_transform = true;
79         #endif
80
81         
82         int i = 0;
83         while (++i < argc)
84         {
85                 if (argv[i][0] != '-')
86                 {
87                         input_filename = argv[i];
88                         continue;               
89                 }
90                 switch (argv[i][1])
91                 {
92                         case 'o':
93                                 mode = OUTPUT_TO_BMP;
94                                 if (++i >= argc)
95                                         Fatal("No output argument following -o switch");
96                                 output_bmp = argv[i];
97                                 hide_control_panel = true;
98                                 break;
99                         case 'b':
100                         {
101                                 Debug("Reading view bounds");
102                                 for (int j = 1; j <= 4; ++j)
103                                 {
104                                         if (i+j >= argc)
105                                                 Fatal("No %d bounds component following -b switch", j);
106                                         b[j-1] = RealFromStr(argv[i+j]);
107                                 }
108                                 i += 4;
109                                 break;
110                         }
111                         case 't':
112                         {
113                                 if (++i >= argc)
114                                         Fatal("No text input following -t switch");
115                                 input_text = argv[i];
116                                 Debug("Insert text: %s", input_text);
117                                 break;
118                         }
119                         
120                         case 'r':
121                         {
122                                 if (++i >= argc)
123                                         Fatal("Expected \"gpu\" or \"cpu\" after -r switch");
124                                 if (strcmp(argv[i], "gpu") == 0)
125                                 {
126                                         gpu_rendering = true;
127                                 }
128                                 else if (strcmp(argv[i], "cpu") == 0)
129                                 {
130                                         gpu_rendering = false;
131                                 }
132                                 else
133                                 {
134                                         Fatal("Expected \"gpu\" or \"cpu\" after -r switch, not \"%s\"", argv[i]);
135                                 }
136                                 break;
137                         }
138                         
139                         case 'T':
140                         {
141                                 if (++i >= argc)
142                                         Fatal("Expected \"gpu\" or \"cpu\" after -T switch");
143                                 if (strcmp(argv[i], "gpu") == 0)
144                                 {
145                                         gpu_transform = true;
146                                 }
147                                 else if (strcmp(argv[i], "cpu") == 0)
148                                 {
149                                         gpu_transform = false;
150                                 }
151                                 else
152                                 {
153                                         Fatal("Expected \"gpu\" or \"cpu\" after -T switch, not \"%s\"", argv[i]);
154                                 }
155                                 break;
156                         }
157                         
158                         
159                         case 'l':
160                                 lazy_rendering = !lazy_rendering;
161                                 break;
162                         
163                         case 'f':
164                                 if (++i >= argc)
165                                         Fatal("No frame number following -f switch");
166                                 max_frames = strtol(argv[i], NULL, 10);
167                                 hide_control_panel = true;
168                                 break;
169                                 
170                         case 'q':
171                                 hide_control_panel = true;
172                                 break;
173                                         
174                         case 'Q':
175                                 hide_control_panel = true;
176                                 window_visible = !window_visible;
177                                 break;
178                         case 's':
179                                 hide_control_panel = true;
180                                 if (++i >= argc)
181                                         Fatal("Expected filename after -s switch");
182                                 script_filename = argv[i];
183                                 break;
184                         case 'm':
185                                 make_movie = true;
186                                 break;
187                 }       
188         }
189
190         Rect bounds(b[0],b[1],b[2],b[3]);
191         Screen scr(window_visible);
192         View view(doc,scr, bounds);
193         
194         view.SetLazyRendering(lazy_rendering);
195         view.SetGPURendering(gpu_rendering);
196         view.SetGPUTransform(gpu_transform);
197
198         if (input_filename != NULL)
199         {
200                 #ifdef TRANSFORM_OBJECTS_NOT_VIEW
201                         doc.LoadSVG(input_filename, Rect(Real(1)/Real(2),Real(1)/Real(2),Real(1)/Real(800),Real(1)/Real(600)));         
202                 #else
203                         doc.LoadSVG(input_filename, Rect(bounds.x+bounds.w/Real(2),bounds.y+bounds.h/Real(2),bounds.w/Real(800),bounds.h/Real(600)));
204                 #endif
205         }
206         else if (input_text != NULL)
207         {
208                 doc.AddText(input_text, bounds.h/Real(2), bounds.x, bounds.y+bounds.h/Real(2));
209         }
210
211
212
213         #ifndef CONTROLPANEL_DISABLED
214         if (!scr.Valid()) hide_control_panel = true;
215         SDL_Thread * cp_thread = NULL;
216         if (!hide_control_panel)
217         {
218                 ControlPanel::RunArgs args = {argc, argv, view, doc, scr};
219                 cp_thread = SDL_CreateThread(ControlPanel::Run, "ControlPanel", &args);
220                 if (cp_thread == NULL)
221                 {
222                         Error("Couldn't create ControlPanel thread: %s", SDL_GetError());
223                 }
224         }
225         #else //CONTROLPANEL_DISABLED
226                 Debug("No control panel, hide_control_panel is %d", hide_control_panel);
227         #endif 
228
229         if (mode == LOOP)
230                 MainLoop(doc, scr, view, max_frames);
231         else if (mode == OUTPUT_TO_BMP)
232         {
233                 view.SaveBMP(output_bmp);
234         }
235                 
236         #ifndef CONTROLPANEL_DISABLED
237                 if (cp_thread != NULL)
238                 {
239                         int cp_return;
240                         qApp->quit(); // will close the control panel
241                         // (seems to not explode if the qApp has already been quit)
242                         SDL_WaitThread(cp_thread, &cp_return);
243                         Debug("ControlPanel thread returned %d", cp_return);
244                 }
245         #endif //CONTROLPANEL_DISABLED
246         
247         ignore_sigfpe = true;
248         return 0;
249 }

UCC git Repository :: git.ucc.asn.au