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

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