Refactor Rendering of Objects (prepare for CPU rendering)
[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         View view(doc, bounds, c);
15         Screen scr;
16         if (input != NULL)
17                 scr.RenderBMP(input);
18         view.Render();
19         if (output != NULL)
20                 scr.ScreenShot(output);
21         scr.Present();
22 }
23
24 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))
25 {
26         // order is important... segfaults occur when screen (which inits GL) is not constructed first -_-
27         Screen scr;
28         View view(doc,bounds, c);
29         scr.DebugFontInit("DejaVuSansMono.ttf");
30         scr.SetMouseHandler([&](int x, int y, int buttons, int wheel) // [?] wtf
31         {
32                 static bool oldButtonDown = false;
33                 static int oldx, oldy;
34                 if (buttons > 1 && !oldButtonDown)
35                 {
36                         oldButtonDown = true;
37                         view.ToggleGPUTransform();
38                         oldx = x;
39                         oldy = y;
40                         return;
41                 }
42                 if (buttons && !oldButtonDown)
43                 {
44                         // We're beginning a drag.
45                         oldButtonDown = true;
46                         oldx = x;
47                         oldy = y;
48                         scr.SetMouseCursor(Screen::CursorMove);
49                 }
50                 if (buttons)
51                 {
52                         view.Translate(Real(oldx-x)/Real(scr.ViewportWidth()), Real(oldy-y)/Real(scr.ViewportHeight()));
53                 }
54                 else
55                 {
56                         oldButtonDown = false;
57                         scr.SetMouseCursor(Screen::CursorArrow);
58                 }
59                 oldx = x;
60                 oldy = y;
61                 
62                 if (wheel)
63                 {
64                         view.ScaleAroundPoint(Real(x)/Real(scr.ViewportWidth()),Real(y)/Real(scr.ViewportHeight()), expf(-wheel/20.f));
65                 }
66         }
67         );
68
69         while (scr.PumpEvents())
70         {
71                 scr.Clear();
72                 view.Render(scr.ViewportWidth(), scr.ViewportHeight());
73                 scr.DebugFontPrintF("[CPU] Render took %lf ms (%lf FPS)\n", (scr.GetLastFrameTimeCPU())* 1000.0, 1.0/scr.GetLastFrameTimeCPU());
74                 scr.DebugFontPrintF("[GPU] Render took %lf ms (%lf FPS)\n", (scr.GetLastFrameTimeGPU())* 1000.0, 1.0/scr.GetLastFrameTimeGPU());
75                 scr.DebugFontPrintF("View bounds: %s\n", view.GetBounds().Str().c_str());
76                 if (view.UsingGPUTransform())
77                 {
78                         scr.DebugFontPrint("Doing coordinate transform on the GPU.\n");
79                 }
80                 else
81                 {
82                         scr.DebugFontPrint("Doing coordinate transform on the CPU.\n");
83                 }
84                 if (view.UsingGPURendering())
85                 {
86                         scr.DebugFontPrint("Doing rendering using GPU.\n");
87                 }
88                 else
89                 {
90                         scr.DebugFontPrint("Doing rendering using CPU.\n");
91                 }
92                 scr.Present();
93         }
94 }

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