e866bc1d36e199d3b0d56198448ad9f427599f48
[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         scr.RenderBMP(input);
17         view.Render();
18         scr.Present();
19         sleep(5);
20         scr.RenderBMP(input);
21         view.Render();
22         scr.ScreenShot(output);
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         View view(doc,bounds, c);
28         Screen scr;
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 && !oldButtonDown)
35                 {
36                         // We're beginning a drag.
37                         oldButtonDown = true;
38                         oldx = x;
39                         oldy = y;
40                         scr.SetMouseCursor(Screen::CursorMove);
41                 }
42                 if (buttons)
43                 {
44                         view.Translate(Real(oldx-x)/Real(scr.ViewportWidth()), Real(oldy-y)/Real(scr.ViewportHeight()));
45                 }
46                 else
47                 {
48                         oldButtonDown = false;
49                         scr.SetMouseCursor(Screen::CursorArrow);
50                 }
51                 oldx = x;
52                 oldy = y;
53                 
54                 if (wheel)
55                 {
56                         view.ScaleAroundPoint(Real(x)/Real(scr.ViewportWidth()),Real(y)/Real(scr.ViewportHeight()), expf(-wheel/20.f));
57                 }
58         }
59         );
60
61         double init_time = SDL_GetPerformanceCounter();
62         while (scr.PumpEvents())
63         {
64                 scr.Clear();
65                 view.Render();
66                 scr.DebugFontPrintF("[CPU] Render took %lf ms (%lf FPS)\n", (SDL_GetPerformanceCounter() - init_time)* 1000.0/SDL_GetPerformanceFrequency(), SDL_GetPerformanceFrequency()/(SDL_GetPerformanceCounter() - init_time));
67                 scr.DebugFontPrintF("View bounds: (%f, %f) - (%f, %f)\n", view.GetBounds().x, view.GetBounds().y, view.GetBounds().w, view.GetBounds().h);
68                 scr.Present();
69                 init_time = SDL_GetPerformanceCounter();
70         }
71 }

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