Allow setting type of Real at compilation time
[ipdf/code.git] / src / view.cpp
1 #include "view.h"
2
3 #include "SDL_opengl.h"
4
5 using namespace IPDF;
6 using namespace std;
7
8 void View::Translate(Real x, Real y)
9 {
10         x *= m_bounds.w;
11         y *= m_bounds.h;
12         m_bounds.x += x;
13         m_bounds.y += y;
14         Debug("View Bounds => %s", m_bounds.Str().c_str());
15 }
16
17 void View::ScaleAroundPoint(Real x, Real y, Real scaleAmt)
18 {
19         // x and y are coordinates in the window
20         // Convert to local coords.
21         x *= m_bounds.w;
22         y *= m_bounds.h;
23         x += m_bounds.x;
24         y += m_bounds.y;
25         
26         //Debug("Mouse wheel event %f %f %f\n", Float(x), Float(y), Float(scaleAmt));
27         
28         Real top = y - m_bounds.y;
29         Real left = x - m_bounds.x;
30         
31         top *= scaleAmt;
32         left *= scaleAmt;
33         
34         m_bounds.x = x - left;
35         m_bounds.y = y - top;
36         m_bounds.w *= scaleAmt;
37         m_bounds.h *= scaleAmt;
38         Debug("View Bounds => %s", m_bounds.Str().c_str());
39 }
40
41 void View::DrawGrid()
42 {
43         // Draw some grid lines at fixed pixel positions
44         glMatrixMode(GL_PROJECTION);
45         glLoadIdentity();
46         glOrtho(0.0, 1.0, 1.0, 0.0, -1.f, 1.f);
47         glMatrixMode(GL_MODELVIEW);
48         glLoadIdentity();
49
50         glColor4f(0.9,0.9,0.9,0.1);
51         const float num_lines = 50.0;
52         for (float i = 0; i < num_lines; ++i)
53         {
54                 glBegin(GL_LINES);
55                 glVertex2f(i*(1.0/num_lines), 0.0);
56                 glVertex2f(i*(1.0/num_lines), 1.0);
57                 glEnd();
58                 glBegin(GL_LINES);
59                 glVertex2f(0.0,i*(1.0/num_lines));
60                 glVertex2f(1.0,i*(1.0/num_lines));
61                 glEnd();
62         
63         }
64 }
65
66 void View::Render()
67 {
68         static bool debug_output_done = false;
69         if (!debug_output_done)
70         {
71                 m_document.DebugDumpObjects();
72                 debug_output_done = true;
73         }
74
75
76         //DrawGrid(); // Draw the gridlines
77
78         glMatrixMode(GL_PROJECTION);
79         glLoadIdentity();
80         glOrtho(Float(m_bounds.x), Float(m_bounds.x)+Float(m_bounds.w), Float(m_bounds.y) + Float(m_bounds.h), Float(m_bounds.y), -1.f, 1.f);
81         glMatrixMode(GL_MODELVIEW);
82         glLoadIdentity();
83
84         if (m_colour.a < 1.0f)
85         {
86                 glEnable(GL_BLEND);
87                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
88         }
89         glColor4f(m_colour.r, m_colour.g, m_colour.b, m_colour.a);
90         glBegin(GL_QUADS);
91         for (unsigned id = 0; id < m_document.ObjectCount(); ++id)
92         {
93                 if (m_document.m_objects.types[id] != RECT_FILLED)
94                         continue;
95                 Rect obj_bounds = m_document.m_objects.bounds[id];
96                 glVertex2f(Float(obj_bounds.x), Float(obj_bounds.y));
97                 glVertex2f(Float(obj_bounds.x) + Float(obj_bounds.w), Float(obj_bounds.y));
98                 glVertex2f(Float(obj_bounds.x) + Float(obj_bounds.w), Float(obj_bounds.y) + Float(obj_bounds.h));
99                 glVertex2f(Float(obj_bounds.x), Float(obj_bounds.y) + Float(obj_bounds.h));
100         }
101         glEnd();
102
103         
104         for (unsigned id = 0; id < m_document.ObjectCount(); ++id)
105         {
106                 if (m_document.m_objects.types[id] != RECT_OUTLINE)
107                         continue;
108                 Rect obj_bounds = m_document.m_objects.bounds[id];
109                 glBegin(GL_LINE_LOOP);
110                 glVertex2f(Float(obj_bounds.x), Float(obj_bounds.y));
111                 glVertex2f(Float(obj_bounds.x) + Float(obj_bounds.w), Float(obj_bounds.y));
112                 glVertex2f(Float(obj_bounds.x) + Float(obj_bounds.w), Float(obj_bounds.y) + Float(obj_bounds.h));
113                 glVertex2f(Float(obj_bounds.x), Float(obj_bounds.y) + Float(obj_bounds.h));
114                 glEnd();
115         }
116
117         if (m_colour.a < 1.0f)
118         {
119                 glDisable(GL_BLEND);
120         }
121
122 }

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