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

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