Paranoia is setting in
[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 <unistd.h>
7
8
9 using namespace std;
10 using namespace IPDF;
11
12 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))
13 {
14
15         Screen scr;
16         View view(doc, scr, bounds, c);
17         if (input != NULL)
18                 scr.RenderBMP(input);
19         view.Render();
20         if (output != NULL)
21                 scr.ScreenShot(output);
22         scr.Present();
23 }
24
25 // It is the only way.
26 // Dear god what have I done
27 void RatCatcher(int x, int y, int buttons, int wheel, Screen * scr, View * view)
28 {
29         static bool oldButtonDown = false;
30         static int oldx, oldy;
31         if (buttons == 3 && !oldButtonDown)
32         {
33                 oldButtonDown = true;
34                 view->ToggleGPUTransform();
35                 oldx = x;
36                 oldy = y;
37                 return;
38         }
39         if (buttons == 2 && !oldButtonDown)
40         {
41                 oldButtonDown = true;
42                 view->ToggleGPURendering();
43                 oldx = x;
44                 oldy = y;
45         }
46         if (buttons && !oldButtonDown)
47         {
48                 // We're beginning a drag.
49                 oldButtonDown = true;
50                 oldx = x;
51                 oldy = y;
52                 scr->SetMouseCursor(Screen::CursorMove);
53         }
54         if (buttons)
55         {
56                 #if REALTYPE == REAL_RATIONAL
57                         view->Translate(Real(oldx, scr->ViewportWidth()) -Real(x,scr->ViewportWidth()), Real(oldy, scr->ViewportHeight()) - Real(y,scr->ViewportHeight()));
58                 #else                   
59                         view->Translate(Real(oldx-x)/Real(scr->ViewportWidth()), Real(oldy-y)/Real(scr->ViewportHeight()));
60                 #endif
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                 #if REALTYPE == REAL_RATIONAL
73                         view->ScaleAroundPoint(Real(x,scr->ViewportWidth()), Real(y,scr->ViewportHeight()), Real(20-wheel, 20));
74                 #else
75                         view->ScaleAroundPoint(Real(x)/Real(scr->ViewportWidth()),Real(y)/Real(scr->ViewportHeight()), Real(expf(-wheel/20.f)));
76                 #endif
77         
78         }
79 }
80
81
82 inline void MainLoop(Document & doc, Screen & scr, View & view)
83 {
84         // order is important... segfaults occur when screen (which inits GL) is not constructed first -_-
85
86         scr.DebugFontInit("fonts/DejaVuSansMono.ttf");
87         scr.SetMouseHandler(RatCatcher);
88
89         double total_cpu_time = 0;
90         double total_gpu_time = 0;
91         double total_real_time = 0;
92         struct timespec real_clock_start;
93         struct timespec real_clock_now;
94         struct timespec real_clock_prev;
95         clock_gettime(CLOCK_MONOTONIC_RAW, &real_clock_start);
96         real_clock_now = real_clock_start;
97         double frames = 0;
98         double data_rate = 0; // period between data output to stdout (if <= 0 there will be no output)
99         uint64_t data_points = 0;
100         setbuf(stdout, NULL);
101         while (scr.PumpEvents())
102         {
103                 real_clock_prev = real_clock_now;
104                 ++frames;
105                 scr.Clear();
106                 //view.ForceBoundsDirty();
107                 //view.ForceBufferDirty();
108                 //view.ForceRenderDirty();
109
110                 view.Render(scr.ViewportWidth(), scr.ViewportHeight());
111
112                 double cpu_frame = scr.GetLastFrameTimeCPU();
113                 double gpu_frame = scr.GetLastFrameTimeGPU();
114                 clock_gettime(CLOCK_MONOTONIC_RAW, &real_clock_now);
115                 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);
116
117
118                 total_real_time += real_frame; total_cpu_time += cpu_frame; total_gpu_time += gpu_frame;
119                 if (data_rate > 0 && total_real_time > data_rate*(data_points+1)) 
120                 {
121                         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);
122                         data_points++;
123                 }
124                 scr.DebugFontPrintF("Rendered frame %lu\n", (uint64_t)frames);
125                 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);
126                 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);
127                 scr.DebugFontPrintF("[REALTIME] Render+Present+Cruft 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);
128                 scr.DebugFontPrintF("View bounds: %s\n", view.GetBounds().Str().c_str());
129                 scr.DebugFontPrintF("type of Real == %s\n", g_real_name[REALTYPE]);
130                 #if REALTYPE == REAL_MPFRCPP
131                         scr.DebugFontPrintf("Precision: %s\nRounding: %s\n");
132                 #endif 
133
134                 if (view.UsingGPUTransform())
135                 {
136                         scr.DebugFontPrint("Doing coordinate transform on the GPU.\n");
137                 }
138                 else
139                 {
140                         scr.DebugFontPrint("Doing coordinate transform on the CPU.\n");
141                 }
142                 if (view.UsingGPURendering())
143                 {
144                         scr.DebugFontPrint("Doing rendering using GPU.\n");
145                 }
146                 else
147                 {
148                         scr.DebugFontPrint("Doing rendering using CPU.\n");
149                 }
150                 scr.Present();
151         }
152 }

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