332b021495ed3e8f842fc474d8a2f2c62152709e
[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         View view(doc,bounds, c);
27         Screen scr;
28         scr.DebugFontInit("DejaVuSansMono.ttf");
29         scr.SetMouseHandler([&](int x, int y, int buttons, int wheel) // [?] wtf
30         {
31                 static bool oldButtonDown = false;
32                 static int oldx, oldy;
33                 if (buttons > 1 && !oldButtonDown)
34                 {
35                         oldButtonDown = true;
36                         view.ToggleGPUTransform();
37                         oldx = x;
38                         oldy = y;
39                         return;
40                 }
41                 if (buttons && !oldButtonDown)
42                 {
43                         // We're beginning a drag.
44                         oldButtonDown = true;
45                         oldx = x;
46                         oldy = y;
47                         scr.SetMouseCursor(Screen::CursorMove);
48                 }
49                 if (buttons)
50                 {
51                         view.Translate(Real(oldx-x)/Real(scr.ViewportWidth()), Real(oldy-y)/Real(scr.ViewportHeight()));
52                 }
53                 else
54                 {
55                         oldButtonDown = false;
56                         scr.SetMouseCursor(Screen::CursorArrow);
57                 }
58                 oldx = x;
59                 oldy = y;
60                 
61                 if (wheel)
62                 {
63                         view.ScaleAroundPoint(Real(x)/Real(scr.ViewportWidth()),Real(y)/Real(scr.ViewportHeight()), expf(-wheel/20.f));
64                 }
65         }
66         );
67
68         while (scr.PumpEvents())
69         {
70                 scr.Clear();
71                 view.Render(scr.ViewportWidth(), scr.ViewportHeight());
72                 scr.DebugFontPrintF("[CPU] Render took %lf ms (%lf FPS)\n", (scr.GetLastFrameTimeCPU())* 1000.0, 1.0/scr.GetLastFrameTimeCPU());
73                 scr.DebugFontPrintF("[GPU] Render took %lf ms (%lf FPS)\n", (scr.GetLastFrameTimeGPU())* 1000.0, 1.0/scr.GetLastFrameTimeGPU());
74                 scr.DebugFontPrintF("View bounds: %s\n", view.GetBounds().Str().c_str());
75                 if (view.UsingGPUTransform())
76                 {
77                         scr.DebugFontPrint("Doing coordinate transform on the GPU.\n");
78                 }
79                 else
80                 {
81                         scr.DebugFontPrint("Doing coordinate transform on the CPU.\n");
82                 }
83                 scr.Present();
84         }
85 }

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