Mostly features added to DebugScript
[ipdf/code.git] / src / view.h
1 #ifndef _VIEW_H
2 #define _VIEW_H
3
4 #include "ipdf.h"
5 #include "document.h"
6 #include "framebuffer.h"
7 #include "objectrenderer.h"
8 #include "path.h"
9 #include "transformationtype.h"
10
11 #define USE_GPU_TRANSFORM false 
12 #define USE_GPU_RENDERING true
13 #define USE_SHADING !(USE_GPU_RENDERING) && true
14
15
16 #include "gmprat.h"
17
18
19 namespace IPDF
20 {
21         #ifdef TRANSFORM_BEZIERS_TO_PATH
22                 typedef Gmprat VReal;
23         #else
24                 typedef Real VReal;
25         #endif
26         typedef TRect<VReal> VRect;
27         
28         class Screen;
29         /**
30          * The View class manages a rectangular view into the document.
31          * It is responsible for coordinate transforms and rendering the document.
32          * ObjectRenderer's for each type of Object should be created in the constructor.
33          */
34         class View
35         {
36                 public:
37                         View(Document & document, Screen & screen, const VRect & bounds = VRect(0,0,1,1), const Colour & colour = Colour(0.f,0.f,0.f,1.f));
38                         virtual ~View();
39
40                         void Render(int width = 0, int height = 0);
41                         
42                         void Translate(Real x, Real y);
43                         void ScaleAroundPoint(Real x, Real y, Real scale_amount);
44                         void SetBounds(const Rect & new_bounds);
45                         
46                         Rect TransformToViewCoords(const Rect& inp) const;
47                         
48                         const VRect& GetBounds() const { return m_bounds; }
49                         
50                         
51                         const bool UsingGPUTransform() const { return m_use_gpu_transform; } // whether view transform calculated on CPU or GPU
52                         const bool UsingGPURendering() const { return m_use_gpu_rendering; } // whether GPU shaders are used or CPU rendering
53                         void ToggleGPUTransform() { m_use_gpu_transform = (!m_use_gpu_transform); m_bounds_dirty = true; m_buffer_dirty = true; }
54                         void ToggleGPURendering() { m_use_gpu_rendering = (!m_use_gpu_rendering); m_bounds_dirty = true; m_buffer_dirty = true; }
55                         void SetGPUTransform(bool state) {m_use_gpu_transform = state; m_bounds_dirty = true; m_buffer_dirty = true;}
56                         
57                         void SetGPURendering(bool state) {m_use_gpu_rendering = state; m_bounds_dirty = true; m_buffer_dirty = true;}
58
59                         bool ShowingBezierBounds() const {return m_show_bezier_bounds;} // render bounds rectangles
60                         void ShowBezierBounds(bool state) {m_show_bezier_bounds = state; m_bounds_dirty = true; m_buffer_dirty = true;}
61                         bool ShowingBezierType() const {return m_show_bezier_type;}
62                         void ShowBezierType(bool state) {m_show_bezier_type = state; m_bounds_dirty = true; m_buffer_dirty = true;}
63                         bool ShowingFillPoints() const {return m_show_fill_points;}
64                         void ShowFillPoints(bool state) {m_show_fill_points = state; m_bounds_dirty = true; m_buffer_dirty = true;}
65                         bool ShowingFillBounds() const {return m_show_fill_bounds;}
66                         void ShowFillBounds(bool state) {m_show_fill_bounds = true;}
67                         
68                         bool PerformingShading() const {return m_perform_shading;}
69                         void PerformShading(bool state) {m_perform_shading = state; m_bounds_dirty = true; m_buffer_dirty = true;}
70
71                         void ForceBoundsDirty() {m_bounds_dirty = true;}                
72                         void ForceBufferDirty() {m_buffer_dirty = true;}                
73                         void ForceRenderDirty() {m_render_dirty = true;}
74                         
75                         void QueryGPUBounds(const char * filename, const char * mode="r");
76                         
77                         void SetLazyRendering(bool state = true) {m_lazy_rendering = state;}
78                         bool UsingLazyRendering() const {return m_lazy_rendering;}
79                         
80                         void SaveBMP(const char * filename) {if (UsingGPURendering()) SaveGPUBMP(filename); else SaveCPUBMP(filename);}
81                         
82                         void SaveCPUBMP(const char * filename);
83                         void SaveGPUBMP(const char * filename);
84
85                         Document & Doc() {return m_document;}
86
87                 private:
88                         struct GPUObjBounds
89                         {
90                                 float x0, y0;
91                                 float x1, y1;
92                         } __attribute__((packed));
93                         
94                         
95
96                         void PrepareRender(); // call when m_render_dirty is true
97                         void UpdateObjBoundsVBO(unsigned first_obj, unsigned last_obj); // call when m_buffer_dirty is true
98
99                         void RenderRange(int width, int height, unsigned first_obj, unsigned last_obj);
100
101                         bool m_use_gpu_transform;
102                         bool m_use_gpu_rendering;
103                         bool m_bounds_dirty; // the view bounds has changed (occurs when changing view)
104                         bool m_buffer_dirty; // the object bounds have changed (also occurs when changing view, but only when not using GPU transforms)
105                         bool m_render_dirty; // the document has changed (occurs when document first loaded)
106                         Document & m_document;
107                         Screen & m_screen;
108                         FrameBuffer m_cached_display;
109                         VRect m_bounds;
110                         Colour m_colour;
111
112                         // Stores the view bounds.
113                         GraphicsBuffer m_bounds_ubo; //bounds_dirty means this one has changed
114                         // Stores the bounds for _all_ objects.
115                         GraphicsBuffer m_objbounds_vbo; //buffer_dirty means this one has changed
116
117                         // ObjectRenderers to be initialised in constructor
118                         // Trust me it will be easier to generalise things this way. Even though there are pointers.
119                         std::vector<ObjectRenderer*> m_object_renderers; 
120                         uint8_t * m_cpu_rendering_pixels; // pixels to be used for CPU rendering
121
122                         
123                         // shading
124                         bool m_perform_shading;
125                         
126                         // Debug rendering
127                         bool m_show_bezier_bounds;
128                         bool m_show_bezier_type;
129                         bool m_show_fill_points;
130                         bool m_show_fill_bounds;
131                         
132                         bool m_lazy_rendering;// don't redraw frames unless we need to
133                         
134                         FILE * m_query_gpu_bounds_on_next_frame;
135
136
137 #ifndef QUADTREE_DISABLED
138                         QuadTreeIndex m_current_quadtree_node;  // The highest node we will traverse.
139                         int m_quadtree_max_depth;               // The maximum quadtree depth.
140                         void RenderQuadtreeNode(int width, int height, QuadTreeIndex node, int remaining_depth);
141
142 #endif
143         };
144 }
145
146 #endif //_VIEW_H

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