Implemented CPU rendering for current ObjectTypes
[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 inline void MainLoop(Document & doc, const Rect & bounds = Rect(0,0,1,1), const Colour & c = Colour(0.f,0.f,0.f,1.f))
26 {
27         // order is important... segfaults occur when screen (which inits GL) is not constructed first -_-
28         Screen scr;
29         View view(doc,scr, bounds, c);
30         scr.DebugFontInit("DejaVuSansMono.ttf");
31         scr.SetMouseHandler([&](int x, int y, int buttons, int wheel) // [?] wtf
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()), expf(-wheel/20.f));
73                 }
74         }
75         );
76
77         while (scr.PumpEvents())
78         {
79                 scr.Clear();
80                 view.Render(scr.ViewportWidth(), scr.ViewportHeight());
81                 scr.DebugFontPrintF("[CPU] Render took %lf ms (%lf FPS)\n", (scr.GetLastFrameTimeCPU())* 1000.0, 1.0/scr.GetLastFrameTimeCPU());
82                 scr.DebugFontPrintF("[GPU] Render took %lf ms (%lf FPS)\n", (scr.GetLastFrameTimeGPU())* 1000.0, 1.0/scr.GetLastFrameTimeGPU());
83                 scr.DebugFontPrintF("View bounds: %s\n", view.GetBounds().Str().c_str());
84                 if (view.UsingGPUTransform())
85                 {
86                         scr.DebugFontPrint("Doing coordinate transform on the GPU.\n");
87                 }
88                 else
89                 {
90                         scr.DebugFontPrint("Doing coordinate transform on the CPU.\n");
91                 }
92                 if (view.UsingGPURendering())
93                 {
94                         scr.DebugFontPrint("Doing rendering using GPU.\n");
95                 }
96                 else
97                 {
98                         scr.DebugFontPrint("Doing rendering using CPU.\n");
99                 }
100                 scr.Present();
101         }
102 }

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