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

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