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

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