d65dc6018945cfb0157723c33f158772da04650c
[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
11
12 int main(int argc, char ** argv)
13 {       
14         #if REALTYPE == REAL_IRRAM
15           iRRAM_initialize(argc,argv);
16         #endif
17         
18         #ifndef __STDC_IEC_559__
19         Warn("__STDC_IEC_559__ not defined. IEEE 754 floating point not fully supported.\n");
20         #endif
21
22         // We want to crash if we ever get a NaN.
23         feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
24
25         Debug("Compiled with REAL = %d => \"%s\" sizeof(Real) == %d bytes", REALTYPE, g_real_name[REALTYPE], sizeof(Real));
26
27         Document doc("","fonts/ComicSans.ttf");
28         srand(time(NULL));
29
30         enum {OUTPUT_TO_BMP, LOOP} mode = LOOP;
31         
32         
33         Colour c(0,0,0,1);
34         const char * input_bmp = NULL;
35         const char * output_bmp = NULL;
36         const char * input_filename = NULL;
37         float b[4] = {0,0,1,1};
38
39         int i = 0;
40         while (++i < argc)
41         {
42                 if (argv[i][0] != '-')
43                 {
44                         input_filename = argv[i];
45                         continue;               
46                 }
47                 switch (argv[i][1])
48                 {
49                         case 'o':
50                                 mode = OUTPUT_TO_BMP;
51                                 if (++i >= argc)
52                                         Fatal("No input argument following -o switch");
53                                 input_bmp = argv[i];
54                                 if (++i >= argc)
55                                         Fatal("No output argument following -o switch");
56                                 output_bmp = argv[i];
57
58                                 break;
59                         case 'b':
60                         {
61                                 Debug("Reading view bounds");
62                                 for (int j = 1; j <= 4; ++j)
63                                 {
64                                         if (i+j >= argc)
65                                                 Fatal("No %d bounds component following -b switch", j);
66                                         char * e;
67                                         b[j-1] = strtof(argv[i+j], &e);
68                                         if (*e != '\0')
69                                                 Fatal("Bounds component %d not a valid float", j); 
70                                 }
71                                 i += 4;
72                                 break;
73                         }
74                 }       
75         }
76
77         if (input_filename != NULL)
78         {
79                 doc.LoadSVG(input_filename, Rect(0,0,Real(1)/Real(800),Real(1)/Real(600)));
80         }
81         else 
82         {
83                 doc.Add(RECT_OUTLINE, Rect(0,0,0,0),0); // hack to stop segfault if document is empty (:S)
84         }
85         Debug("Start!");
86         Rect bounds(b[0],b[1],b[2],b[3]);
87         
88         Screen scr;
89         View view(doc,scr, bounds);
90
91         #ifndef CONTROLPANEL_DISABLED
92                 ControlPanel::RunArgs args = {argc, argv, view, doc, scr};
93                 SDL_Thread * cp_thread = SDL_CreateThread(ControlPanel::Run, "ControlPanel", &args);
94                 if (cp_thread == NULL)
95                 {
96                         Error("Couldn't create ControlPanel thread: %s", SDL_GetError());
97                 }
98         #endif //CONTROLPANEL_DISABLED
99
100         if (mode == LOOP)
101                 MainLoop(doc, scr, view);
102         else if (mode == OUTPUT_TO_BMP) //TODO: Remove this shit
103                 OverlayBMP(doc, input_bmp, output_bmp, bounds, c);
104                 
105         #ifndef CONTROLPANEL_DISABLED
106                 
107                 if (cp_thread != NULL)
108                 {
109                         int cp_return;
110                         qApp->quit(); // will close the control panel
111                         // (seems to not explode if the qApp has already been quit)
112                         SDL_WaitThread(cp_thread, &cp_return);
113                         Debug("ControlPanel thread returned %d", cp_return);
114                 }
115         #endif //CONTROLPANEL_DISABLED
116         return 0;
117 }

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