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

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