Fix debugscript, some quadtree stuff and don't intersect vertical/horz lines when...
[ipdf/code.git] / src / main.h
1 #include "common.h"
2
3 #include "document.h"
4 #include "view.h"
5 #include "screen.h"
6 #include "debugscript.h"
7 #include <unistd.h>
8
9
10 using namespace std;
11 using namespace IPDF;
12
13
14 extern const char *script_filename;
15
16 inline void OverlayBMP(Document & doc, const char * input, const char * output, const Rect & bounds = Rect(0,0,1,1), const Colour & c = Colour(0.f,0.f,0.f,1.f))
17 {
18
19         Screen scr;
20         View view(doc, scr, bounds, c);
21         if (input != NULL)
22                 scr.RenderBMP(input);
23         view.Render();
24         if (output != NULL)
25                 scr.ScreenShot(output);
26         scr.Present();
27 }
28
29 // It is the only way.
30 // Dear god what have I done
31 void RatCatcher(int x, int y, int buttons, int wheel, Screen * scr, View * view)
32 {
33         static bool oldButtonDown = false;
34         static int oldx, oldy;
35         if (buttons == 3 && !oldButtonDown)
36         {
37                 oldButtonDown = true;
38                 view->ToggleGPUTransform();
39                 oldx = x;
40                 oldy = y;
41                 return;
42         }
43         if (buttons == 2 && !oldButtonDown)
44         {
45                 oldButtonDown = true;
46                 view->ToggleGPURendering();
47                 oldx = x;
48                 oldy = y;
49         }
50         if (buttons && !oldButtonDown)
51         {
52                 // We're beginning a drag.
53                 oldButtonDown = true;
54                 oldx = x;
55                 oldy = y;
56                 scr->SetMouseCursor(Screen::CursorMove);
57         }
58         if (buttons)
59         {
60                 view->Translate(Real(oldx-x)/Real(scr->ViewportWidth()), Real(oldy-y)/Real(scr->ViewportHeight()));
61         }
62         else
63         {
64                 oldButtonDown = false;
65                 scr->SetMouseCursor(Screen::CursorArrow);
66         }
67         oldx = x;
68         oldy = y;
69                 
70         if (wheel)
71         {
72                 view->ScaleAroundPoint(Real(x)/Real(scr->ViewportWidth()),Real(y)/Real(scr->ViewportHeight()), Real(expf(-wheel/20.f)));
73         }
74 }
75
76
77 void MainLoop(Document & doc, Screen & scr, View & view, int max_frames = -1)
78 {
79         // order is important... segfaults occur when screen (which inits GL) is not constructed first -_-
80         
81
82         scr.DebugFontInit("fonts/DejaVuSansMono.ttf", 12);
83         //scr.DebugFontInit("fonts/DejaVuSansMono.ttf", 18);
84         scr.SetMouseHandler(RatCatcher);
85
86         ifstream tmp;
87         istream * script_input = NULL;
88         if (script_filename != NULL)
89         {
90                 if (strcmp(script_filename, "stdin") == 0)
91                         script_input = &cin;
92                 else
93                 {
94                         tmp.open(script_filename);
95                         script_input = &tmp;
96                 }
97         }
98         DebugScript script(script_input);
99
100         double total_cpu_time = 0;
101         double total_gpu_time = 0;
102         double total_real_time = 0;
103
104         // MINGW doesn't support a lot of ctime stuff here
105         #ifndef __MINGW32__     
106         struct timespec real_clock_start;
107         struct timespec real_clock_now;
108         struct timespec real_clock_prev;
109         clock_gettime(CLOCK_MONOTONIC_RAW, &real_clock_start);
110         real_clock_now = real_clock_start;
111         #endif
112
113
114         double frames = 0;
115         double data_rate = 0; // period between data output to stdout (if <= 0 there will be no output)
116         uint64_t data_points = 0;
117         setbuf(stdout, NULL);
118         int frame_number = 0;
119         while (scr.PumpEvents() && (max_frames < 0 || frame_number++ < max_frames))
120         {
121                 #ifndef __MINGW32__
122                 real_clock_prev = real_clock_now;
123                 #endif
124                 ++frames;
125                 scr.Clear();
126                 //view.ForceBoundsDirty();
127                 //view.ForceBufferDirty();
128                 //view.ForceRenderDirty();
129
130                 if (script_filename)
131                 {
132                         if (script.Execute(&view, &scr))
133                                 return;
134                 }
135
136                 view.Render(scr.ViewportWidth(), scr.ViewportHeight());
137
138                 double cpu_frame = scr.GetLastFrameTimeCPU();
139                 double gpu_frame = scr.GetLastFrameTimeGPU();
140                 total_cpu_time += cpu_frame; total_gpu_time += gpu_frame;
141                 
142                 #ifndef __MINGW32__
143                 clock_gettime(CLOCK_MONOTONIC_RAW, &real_clock_now);
144                 double real_frame = (real_clock_now.tv_sec - real_clock_prev.tv_sec) + 1e-9*(real_clock_now.tv_nsec - real_clock_prev.tv_nsec);
145                 #else
146                 double real_frame = cpu_frame;
147                 #endif
148                 
149                 total_real_time += real_frame; 
150                 if (data_rate > 0 && total_real_time > data_rate*(data_points+1)) 
151                 {
152                         printf("%lu\t%f\t%f\t%f\t%f\t%f\t%f\n", (long unsigned int)frames, total_real_time, total_cpu_time, total_gpu_time, real_frame, cpu_frame, gpu_frame);
153                         data_points++;
154                 }
155                 
156
157                 
158
159                 scr.DebugFontPrintF("Top Left: (%s,%s)\n", Str(view.GetBounds().x).c_str(),Str(view.GetBounds().y).c_str());
160                 scr.DebugFontPrintF("Width: %s\n", Str(view.GetBounds().w).c_str());
161                 scr.DebugFontPrintF("Zoom: %s %%\n", Str(VReal(100)/VReal(view.GetBounds().w)).c_str());
162                 //scr.DebugFontPrintF("Similar size: %s\n", HumanScale(view.GetBounds().w * VReal(22e-3)));
163                 
164                 #if 1
165                 scr.DebugFontPrintF("Rendered frame %lu\n", (uint64_t)frames);
166                 scr.DebugFontPrintF("Lazy Rendering = %d\n", view.UsingLazyRendering());
167                 /*if (cpu_frame > 0 && total_cpu_time > 0)
168                         scr.DebugFontPrintF("[CPU] Render took %lf ms (%lf FPS) (total %lf s, avg FPS %lf)\n", cpu_frame*1e3, 1.0/cpu_frame, total_cpu_time,frames/total_cpu_time);
169                 if (gpu_frame > 0 && total_gpu_time > 0)
170                         scr.DebugFontPrintF("[GPU] Render took %lf ms (%lf FPS) (total %lf s, avg FPS %lf)\n", gpu_frame*1e3, 1.0/gpu_frame, total_gpu_time, frames/total_gpu_time);
171                         */
172                 if (real_frame > 0 && total_real_time > 0)
173                         scr.DebugFontPrintF("[REALTIME] Render took %lf ms (%lf FPS) (total %lf s, avg FPS %lf)\n", real_frame*1e3, 1.0/real_frame, total_real_time,frames/total_real_time);
174
175                 //scr.DebugFontPrintF("View bounds: %s\n", view.GetBounds().Str().c_str());
176                 scr.DebugFontPrintF("type of Real == %s\n", g_real_name[REALTYPE]);
177                 //#if REALTYPE == REAL_MPFRCPP
178                 //      scr.DebugFontPrintf("Precision: %s\nRounding: %s\n");
179                 //#endif 
180
181                 #ifdef TRANSFORM_OBJECTS_NOT_VIEW
182                 scr.DebugFontPrint("Doing cumulative coordinate transforms on Objects.\n");
183                 #else
184                 if (view.UsingGPUTransform())
185                 {
186                         scr.DebugFontPrint("Doing coordinate transform on the GPU.\n");
187                 }
188                 else
189                 {
190                         scr.DebugFontPrint("Doing coordinate transform on the CPU.\n");
191                 }
192                 #endif
193                 
194                 #ifdef TRANSFORM_BEZIERS_TO_PATH
195                         scr.DebugFontPrint("Beziers have been transformed to Path\n");
196                 #endif
197
198                 
199                 if (view.UsingGPURendering())
200                 {
201                         scr.DebugFontPrint("Doing rendering using GPU.\n");
202                 }
203                 else
204                 {
205                         scr.DebugFontPrint("Doing rendering using CPU.\n");
206                 }
207                 #endif // 0
208                 
209                 scr.Present();
210         }
211 }

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