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

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