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

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