Merge branch 'master' of git.ucc.asn.au:ipdf/code
[ipdf/code.git] / src / view.cpp
1 #include "view.h"
2 #include "bufferbuilder.h"
3 #include "screen.h"
4 #include "gl_core44.h"
5
6 #ifndef CONTROLPANEL_DISABLED
7         #include "controlpanel.h"
8 #endif //CONTROLPANEL_DISABLED
9
10 using namespace IPDF;
11 using namespace std;
12
13 /**
14  * Constructs a view
15  * Allocates memory for ObjectRenderers
16  * @param document - The document to associate the View with
17  * @param bounds - Initial bounds of the View
18  * @param colour - Colour to use for rendering this view. TODO: Make sure this actually works, or just remove it
19  */
20 View::View(Document & document, Screen & screen, const Rect & bounds, const Colour & colour)
21         : m_use_gpu_transform(USE_GPU_TRANSFORM), m_use_gpu_rendering(USE_GPU_RENDERING), m_bounds_dirty(true), m_buffer_dirty(true), 
22                 m_render_dirty(true), m_document(document), m_screen(screen), m_cached_display(), m_bounds(bounds), m_colour(colour), m_bounds_ubo(), 
23                 m_objbounds_vbo(), m_object_renderers(NUMBER_OF_OBJECT_TYPES), m_cpu_rendering_pixels(NULL),
24                 m_perform_shading(USE_SHADING), m_show_bezier_bounds(false), m_show_bezier_type(false),
25                 m_show_fill_points(false), m_show_fill_bounds(false), m_lazy_rendering(true)
26 {
27         Debug("View Created - Bounds => {%s}", m_bounds.Str().c_str());
28
29         screen.SetView(this); // oh dear...
30
31         
32
33         // Create ObjectRenderers - new's match delete's in View::~View
34         //TODO: Don't forget to put new renderers here or things will be segfaultastic
35         if (screen.Valid())
36         {
37                 m_object_renderers[RECT_FILLED] = new RectFilledRenderer();
38                 m_object_renderers[RECT_OUTLINE] = new RectOutlineRenderer();
39                 m_object_renderers[CIRCLE_FILLED] = new CircleFilledRenderer();
40                 m_object_renderers[BEZIER] = new BezierRenderer();
41                 m_object_renderers[PATH] = new PathRenderer();
42         }
43         else
44         {
45                 for (int i = RECT_FILLED; i <= PATH; ++i)
46                         m_object_renderers[i] = new FakeRenderer();
47         }
48
49         // To add rendering for a new type of object;
50         // 1. Add enum to ObjectType in ipdf.h
51         // 2. Implement class inheriting from ObjectRenderer using that type in objectrenderer.h and objectrenderer.cpp
52         // 3. Add it here
53         // 4. Profit
54
55
56 #ifndef QUADTREE_DISABLED
57         m_quadtree_max_depth = 2;
58         m_current_quadtree_node = document.GetQuadTree().root_id;
59 #endif
60 }
61
62 /**
63  * Destroy a view
64  * Frees memory used by ObjectRenderers
65  */
66 View::~View()
67 {
68         for (unsigned i = 0; i < m_object_renderers.size(); ++i)
69         {
70                 delete m_object_renderers[i]; // delete's match new's in constructor
71         }
72         m_object_renderers.clear();
73         delete [] m_cpu_rendering_pixels;
74 }
75
76 /**
77  * Translate the view
78  * @param x, y - Amount to translate
79  */
80 void View::Translate(Real x, Real y)
81 {
82         if (!m_use_gpu_transform)
83                 m_buffer_dirty = true;
84         m_bounds_dirty = true;
85         #ifdef TRANSFORM_OBJECTS_NOT_VIEW
86         m_document.TranslateObjects(-x, -y);
87         #endif
88         x *= m_bounds.w;
89         y *= m_bounds.h;
90         m_bounds.x += x;
91         m_bounds.y += y;
92         //Debug("View Bounds => %s", m_bounds.Str().c_str());
93
94         
95 }
96
97 /**
98  * Set View bounds
99  * @param bounds - New bounds
100  */
101 void View::SetBounds(const Rect & bounds)
102 {
103         m_bounds.x = bounds.x;
104         m_bounds.y = bounds.y;
105         m_bounds.w = bounds.w;
106         m_bounds.h = bounds.h;
107         if (!m_use_gpu_transform)
108                 m_buffer_dirty = true;
109         m_bounds_dirty = true;
110 }
111
112 /**
113  * Scale the View at a point
114  * @param x, y - Coordinates to scale at (eg: Mouse cursor position)
115  * @param scale_amount - Amount to scale by
116  */
117 void View::ScaleAroundPoint(Real x, Real y, Real scale_amount)
118 {
119         
120         // (x0, y0, w, h) -> (x*w - (x*w - x0)*s, y*h - (y*h - y0)*s, w*s, h*s)
121         // x and y are coordinates in the window
122         // Convert to local coords.
123         if (!m_use_gpu_transform)
124                 m_buffer_dirty = true;
125         m_bounds_dirty = true;
126         
127         
128         #ifdef TRANSFORM_OBJECTS_NOT_VIEW
129         m_document.ScaleObjectsAboutPoint(x, y, scale_amount);
130         #endif
131         x *= m_bounds.w;
132         y *= m_bounds.h;
133         x += m_bounds.x;
134         y += m_bounds.y;
135         
136         Real top = y - m_bounds.y;
137         Real left = x - m_bounds.x;
138         
139         top *= scale_amount;
140         left *= scale_amount;
141         
142         m_bounds.x = x - left;
143         m_bounds.y = y - top;
144         m_bounds.w *= scale_amount;
145         m_bounds.h *= scale_amount;
146         //Debug("Scale at {%s, %s} by %s View Bounds => %s", x.Str().c_str(), y.Str().c_str(), scale_amount.Str().c_str(), m_bounds.Str().c_str());
147         
148         
149 }
150
151 /**
152  * Transform a point in the document to a point relative to the top left corner of the view
153  * This is the CPU coordinate transform code; used only if the CPU is doing coordinate transforms
154  * @param inp - Input Rect {x,y,w,h} in the document
155  * @returns output Rect {x,y,w,h} in the View
156  */
157 Rect View::TransformToViewCoords(const Rect& inp) const
158 {
159         #ifdef TRANSFORM_OBJECTS_NOT_VIEW
160                 return inp;
161         #endif
162         Rect out;
163         out.x = (inp.x - m_bounds.x) / m_bounds.w;
164         out.y = (inp.y - m_bounds.y) / m_bounds.h;
165         out.w = inp.w / m_bounds.w;
166         out.h = inp.h / m_bounds.h;
167         return out;
168 }
169
170 /**
171  * Render the view
172  * Updates FrameBuffer if the document, object bounds, or view bounds have changed, then Blits it
173  * Otherwise just Blits the cached FrameBuffer
174  * @param width - Width of View to render
175  * @param height - Height of View to render
176  */
177 void View::Render(int width, int height)
178 {
179         if (!m_screen.Valid()) return;
180         glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION,42,-1, "Beginning View::Render()");
181         // View dimensions have changed (ie: Window was resized)
182         int prev_width = m_cached_display.GetWidth();
183         int prev_height = m_cached_display.GetHeight();
184         if (width != prev_width || height != prev_height)
185         {
186                 m_cached_display.Create(width, height);
187                 m_bounds_dirty = true;
188         }
189
190         // View bounds have not changed; blit the FrameBuffer as it is
191         if (!m_bounds_dirty && m_lazy_rendering)
192         {
193                 m_cached_display.UnBind();
194                 m_cached_display.Blit();
195                 glPopDebugGroup();
196                 return;
197         }
198         m_cached_display.Bind(); //NOTE: This is redundant; Clear already calls Bind
199         m_cached_display.Clear();
200
201 #ifndef QUADTREE_DISABLED
202         if (m_bounds_dirty || !m_lazy_rendering)
203         {
204                 if ( (m_bounds.x > 1.0 || m_bounds.x < 0.0 || m_bounds.y > 1.0 || m_bounds.y < 0.0 || m_bounds.w > 1.0 || m_bounds.h > 1.0))
205                 {
206                         //TODO: Generate a new parent node.
207                         if (m_document.GetQuadTree().nodes[m_current_quadtree_node].parent != QUADTREE_EMPTY)
208                         {
209                                 m_bounds = TransformFromQuadChild(m_bounds, m_document.GetQuadTree().nodes[m_current_quadtree_node].child_type);
210                                 m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].parent;
211                         }
212                 }
213                 if (ContainedInQuadChild(m_bounds, QTC_TOP_LEFT))
214                 {
215                         if (m_document.GetQuadTree().nodes[m_current_quadtree_node].top_left == QUADTREE_EMPTY)
216                         {
217                                 // We want to reparent into a child node, but none exist. Get the document to create one.
218                                 m_document.GenQuadChild(m_current_quadtree_node, QTC_TOP_LEFT);
219                                 m_render_dirty = true;
220                         }
221                         m_bounds = TransformToQuadChild(m_bounds, QTC_TOP_LEFT);
222                         m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].top_left;
223                 }
224                 if (ContainedInQuadChild(m_bounds, QTC_TOP_RIGHT))
225                 {
226                         if (m_document.GetQuadTree().nodes[m_current_quadtree_node].top_right == QUADTREE_EMPTY)
227                         {
228                                 // We want to reparent into a child node, but none exist. Get the document to create one.
229                                 m_document.GenQuadChild(m_current_quadtree_node, QTC_TOP_RIGHT);
230                                 m_render_dirty = true;
231                         }
232                         m_bounds = TransformToQuadChild(m_bounds, QTC_TOP_RIGHT);
233                         m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].top_right;
234                 }
235                 if (ContainedInQuadChild(m_bounds, QTC_BOTTOM_LEFT))
236                 {
237                         if (m_document.GetQuadTree().nodes[m_current_quadtree_node].bottom_left == QUADTREE_EMPTY)
238                         {
239                                 // We want to reparent into a child node, but none exist. Get the document to create one.
240                                 m_document.GenQuadChild(m_current_quadtree_node, QTC_BOTTOM_LEFT);
241                                 m_render_dirty = true;
242                         }
243                         m_bounds = TransformToQuadChild(m_bounds, QTC_BOTTOM_LEFT);
244                         m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].bottom_left;
245                 }
246                 if (ContainedInQuadChild(m_bounds, QTC_BOTTOM_RIGHT))
247                 {
248                         if (m_document.GetQuadTree().nodes[m_current_quadtree_node].bottom_right == QUADTREE_EMPTY)
249                         {
250                                 // We want to reparent into a child node, but none exist. Get the document to create one.
251                                 m_document.GenQuadChild(m_current_quadtree_node, QTC_BOTTOM_RIGHT);
252                                 m_render_dirty = true;
253                         }
254                         m_bounds = TransformToQuadChild(m_bounds, QTC_BOTTOM_RIGHT);
255                         m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].bottom_right;
256                 }
257         }
258
259         m_screen.DebugFontPrintF("Current View QuadTree");
260         QuadTreeIndex overlay = m_current_quadtree_node;
261         while (overlay != -1)
262         {
263                 m_screen.DebugFontPrintF(" Node: %d (objs: %d -> %d)", overlay, m_document.GetQuadTree().nodes[overlay].object_begin,
264                                         m_document.GetQuadTree().nodes[overlay].object_end);
265                 overlay = m_document.GetQuadTree().nodes[overlay].next_overlay;
266         }
267         m_screen.DebugFontPrintF("\n");
268
269         Rect view_top_bounds = m_bounds;
270         QuadTreeIndex tmp = m_current_quadtree_node;
271         while (tmp != -1)
272         {
273                 view_top_bounds = TransformFromQuadChild(view_top_bounds, m_document.GetQuadTree().nodes[tmp].child_type);
274                 tmp = m_document.GetQuadTree().nodes[tmp].parent;
275         }
276         m_screen.DebugFontPrintF("Equivalent View Bounds: %s\n", view_top_bounds.Str().c_str());
277 #endif
278
279         if (!m_use_gpu_rendering)
280         {
281                 // Dynamically resize CPU rendering target pixels if needed
282                 if (m_cpu_rendering_pixels == NULL || width*height > prev_width*prev_height)
283                 {
284                         delete [] m_cpu_rendering_pixels;
285                         m_cpu_rendering_pixels = new uint8_t[width*height*4];
286                         if (m_cpu_rendering_pixels == NULL)
287                                 Fatal("Could not allocate %d*%d*4 = %d bytes for cpu rendered pixels", width, height, width*height*4);
288                 }
289                 // Clear CPU rendering pixels
290                 for (int i = 0; i < width*height*4; ++i)
291                         m_cpu_rendering_pixels[i] = 255;
292         }
293 #ifdef QUADTREE_DISABLED
294         RenderRange(width, height, 0, m_document.ObjectCount());
295 #else
296         RenderQuadtreeNode(width, height, m_current_quadtree_node, m_quadtree_max_depth);
297 #endif
298         if (!m_use_gpu_rendering)
299         {
300                 m_screen.RenderPixels(0,0,width, height, m_cpu_rendering_pixels); //TODO: Make this work :(
301                 // Debug for great victory (do something similar for GPU and compare?)
302                 //ObjectRenderer::SaveBMP({m_cpu_rendering_pixels, width, height}, "cpu_rendering_last_frame.bmp");
303         }
304         m_cached_display.UnBind(); // resets render target to the screen
305         m_cached_display.Blit(); // blit FrameBuffer to screen
306         m_buffer_dirty = false;
307         glPopDebugGroup();
308         
309 #ifndef CONTROLPANEL_DISABLED
310         ControlPanel::Update();
311 #endif //CONTROLPANEL_DISABLED
312         //Debug("Completed Render");
313         
314 }
315
316 #ifndef QUADTREE_DISABLED
317 void View::RenderQuadtreeNode(int width, int height, QuadTreeIndex node, int remaining_depth)
318 {
319         Rect old_bounds = m_bounds;
320         if (node == QUADTREE_EMPTY) return;
321         if (!remaining_depth) return;
322         //Debug("Rendering QT node %d, (objs: %d -- %d)\n", node, m_document.GetQuadTree().nodes[node].object_begin, m_document.GetQuadTree().nodes[node].object_end);
323         m_bounds_dirty = true;
324         QuadTreeIndex overlay = node;
325         while(overlay != -1)
326         {
327                 RenderRange(width, height, m_document.GetQuadTree().nodes[overlay].object_begin, m_document.GetQuadTree().nodes[overlay].object_end);
328                 overlay = m_document.GetQuadTree().nodes[overlay].next_overlay;
329         }
330
331         if (m_bounds.Intersects(Rect(-1,-1,1,1)))
332         {
333                 m_bounds = Rect(m_bounds.x - 1, m_bounds.y - 1, m_bounds.w, m_bounds.h);
334                 m_bounds_dirty = true;
335                 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, -1, -1), remaining_depth - 1);
336         }
337         if (m_bounds.Intersects(Rect(-1,0,1,1)))
338         {
339                 m_bounds = Rect(m_bounds.x - 1, m_bounds.y, m_bounds.w, m_bounds.h);
340                 m_bounds_dirty = true;
341                 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, -1, 0), remaining_depth - 1);
342         }
343         if (m_bounds.Intersects(Rect(-1,1,1,1)))
344         {
345                 m_bounds = Rect(m_bounds.x - 1, m_bounds.y + 1, m_bounds.w, m_bounds.h);
346                 m_bounds_dirty = true;
347                 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, -1, 1), remaining_depth - 1);
348         }
349         if (m_bounds.Intersects(Rect(0,-1,1,1)))
350         {
351                 m_bounds = Rect(m_bounds.x, m_bounds.y - 1, m_bounds.w, m_bounds.h);
352                 m_bounds_dirty = true;
353                 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 0, -1), remaining_depth - 1);
354         }
355         if (m_bounds.Intersects(Rect(0,1,1,1)))
356         {
357                 m_bounds = Rect(m_bounds.x, m_bounds.y + 1, m_bounds.w, m_bounds.h);
358                 m_bounds_dirty = true;
359                 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 0, 1), remaining_depth - 1);
360         }
361         if (m_bounds.Intersects(Rect(1,-1,1,1)))
362         {
363                 m_bounds = Rect(m_bounds.x + 1, m_bounds.y - 1, m_bounds.w, m_bounds.h);
364                 m_bounds_dirty = true;
365                 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 1, -1), remaining_depth - 1);
366         }
367         if (m_bounds.Intersects(Rect(1,0,1,1)))
368         {
369                 m_bounds = Rect(m_bounds.x + 1, m_bounds.y, m_bounds.w, m_bounds.h);
370                 m_bounds_dirty = true;
371                 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 1, 0), remaining_depth - 1);
372         }
373         if (m_bounds.Intersects(Rect(1,1,1,1)))
374         {
375                 m_bounds = Rect(m_bounds.x + 1, m_bounds.y + 1, m_bounds.w, m_bounds.h);
376                 m_bounds_dirty = true;
377                 RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 1, 1), remaining_depth - 1);
378         }
379         m_bounds = old_bounds;
380         m_bounds_dirty = true;
381
382 #if 0
383         m_bounds = TransformToQuadChild(old_bounds, QTC_TOP_LEFT);
384         m_bounds_dirty = true;
385         RenderQuadtreeNode(width, height, m_document.GetQuadTree().nodes[node].top_left, remaining_depth-1);
386         m_bounds = TransformToQuadChild(old_bounds, QTC_TOP_RIGHT);
387         m_bounds_dirty = true;
388         RenderQuadtreeNode(width, height, m_document.GetQuadTree().nodes[node].top_right, remaining_depth-1);
389         m_bounds = TransformToQuadChild(old_bounds, QTC_BOTTOM_LEFT);
390         m_bounds_dirty = true;
391         RenderQuadtreeNode(width, height, m_document.GetQuadTree().nodes[node].bottom_left, remaining_depth-1);
392         m_bounds = TransformToQuadChild(old_bounds, QTC_BOTTOM_RIGHT);
393         m_bounds_dirty = true;
394         RenderQuadtreeNode(width, height, m_document.GetQuadTree().nodes[node].bottom_right, remaining_depth-1);
395         m_bounds = old_bounds;
396         m_bounds_dirty = true;
397 #endif
398 }
399 #endif
400
401 void View::RenderRange(int width, int height, unsigned first_obj, unsigned last_obj)
402 {
403         glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 43, -1, "View::RenderRange()");
404         if (m_render_dirty) // document has changed
405                 PrepareRender();
406
407         if (m_buffer_dirty || m_bounds_dirty || !m_lazy_rendering) // object bounds have changed
408         {
409                 if (m_use_gpu_rendering)
410                         UpdateObjBoundsVBO(first_obj, last_obj);
411         }
412
413         if (m_use_gpu_transform)
414         {
415                 #ifdef TRANSFORM_OBJECTS_NOT_VIEW
416                                 GLfloat glbounds[] = {0.0f, 0.0f, 1.0f, 1.0f,
417                                         0.0f, 0.0f, float(width), float(height)};
418                 #else
419                 GLfloat glbounds[] = {static_cast<GLfloat>(Float(m_bounds.x)), static_cast<GLfloat>(Float(m_bounds.y)), static_cast<GLfloat>(Float(m_bounds.w)), static_cast<GLfloat>(Float(m_bounds.h)),
420                                         0.0, 0.0, static_cast<GLfloat>(width), static_cast<GLfloat>(height)};
421                 #endif
422                 m_bounds_ubo.Upload(sizeof(float)*8, glbounds);
423         }
424         else
425         {
426                 GLfloat glbounds[] = {0.0f, 0.0f, 1.0f, 1.0f,
427                                         0.0f, 0.0f, float(width), float(height)};
428                 m_bounds_ubo.Upload(sizeof(float)*8, glbounds);
429         }
430         m_bounds_dirty = false;
431
432
433         // Render using GPU
434         if (m_use_gpu_rendering) 
435         {
436                 if (m_colour.a < 1.0f)
437                 {
438                         glEnable(GL_BLEND);
439                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
440                 }
441                 m_objbounds_vbo.Bind();
442                 m_bounds_ubo.Bind();
443                 glEnableVertexAttribArray(0);
444                 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
445         
446                 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
447                 {
448                         m_object_renderers[i]->RenderUsingGPU(first_obj, last_obj);
449                 }
450                 
451                 glDisableVertexAttribArray(0);
452                 if (m_colour.a < 1.0f)
453                 {
454                         glDisable(GL_BLEND);
455                 }
456         }
457         else // Rasterise on CPU then blit texture to GPU
458         {
459
460                 for (unsigned i = 0; i < m_object_renderers.size(); ++i)
461                 {
462                         m_object_renderers[i]->RenderUsingCPU(m_document.m_objects, *this, {m_cpu_rendering_pixels, width, height}, first_obj, last_obj);
463                 }
464         }
465         glPopDebugGroup();
466 }
467
468 void View::UpdateObjBoundsVBO(unsigned first_obj, unsigned last_obj)
469 {
470         //m_objbounds_vbo.Invalidate();
471         m_objbounds_vbo.SetType(GraphicsBuffer::BufferTypeVertex);
472         m_objbounds_vbo.SetName("Object Bounds VBO");
473         if (m_use_gpu_transform)
474         {
475                 m_objbounds_vbo.SetUsage(GraphicsBuffer::BufferUsageStaticDraw);
476         }
477         else
478         {
479                 m_objbounds_vbo.SetUsage(GraphicsBuffer::BufferUsageDynamicCopy);
480         }
481         m_objbounds_vbo.Resize(m_document.ObjectCount()*sizeof(GPUObjBounds));
482
483         BufferBuilder<GPUObjBounds> obj_bounds_builder(m_objbounds_vbo.MapRange(first_obj*sizeof(GPUObjBounds), (last_obj-first_obj)*sizeof(GPUObjBounds), false, true, true), m_objbounds_vbo.GetSize());
484
485         for (unsigned id = first_obj; id < last_obj; ++id)
486         {
487                 Rect obj_bounds;
488                 if (m_use_gpu_transform)
489                 {
490                         obj_bounds = m_document.m_objects.bounds[id];
491                 }
492                 else
493                 {
494                         obj_bounds = TransformToViewCoords(m_document.m_objects.bounds[id]);
495                 }
496                 GPUObjBounds gpu_bounds = {
497                         (float)Float(obj_bounds.x),
498                         (float)Float(obj_bounds.y),
499                         (float)Float(obj_bounds.x + obj_bounds.w),
500                         (float)Float(obj_bounds.y + obj_bounds.h)
501                 };
502                 obj_bounds_builder.Add(gpu_bounds);
503
504         }
505         m_objbounds_vbo.UnMap();
506 }
507 /**
508  * Prepare the document for rendering
509  * Will be called on View::Render if m_render_dirty is set
510  * (Called at least once, on the first Render)
511  */
512 void View::PrepareRender()
513 {
514         Debug("Recreate buffers with %u objects", m_document.ObjectCount());
515         // Prepare bounds vbo
516         if (UsingGPURendering())
517         {
518                 m_bounds_ubo.Invalidate();
519                 m_bounds_ubo.SetType(GraphicsBuffer::BufferTypeUniform);
520                 m_bounds_ubo.SetUsage(GraphicsBuffer::BufferUsageStreamDraw);
521                 m_bounds_ubo.SetName("m_bounds_ubo: Screen bounds.");
522         }
523         
524         // Instead of having each ObjectRenderer go through the whole document
525         //  we initialise them, go through the document once adding to the appropriate Renderers
526         //  and then finalise them
527         // This will totally be efficient if we have like, a lot of distinct ObjectTypes. Which could totally happen. You never know.
528
529         // Prepare the buffers
530         for (unsigned i = 0; i < m_object_renderers.size(); ++i)
531         {
532                 m_object_renderers[i]->PrepareBuffers(m_document.ObjectCount());
533         }
534
535         // Add objects from Document to buffers
536         for (unsigned id = 0; id < m_document.ObjectCount(); ++id)
537         {
538                 ObjectType type = m_document.m_objects.types[id];
539                 m_object_renderers.at(type)->AddObjectToBuffers(id); // Use at() in case the document is corrupt TODO: Better error handling?
540                 // (Also, Wow I just actually used std::vector::at())
541                 // (Also, I just managed to make it throw an exception because I'm a moron)
542                 //Debug("Object of type %d", type);
543         }
544
545
546         // Finish the buffers
547         for (unsigned i = 0; i < m_object_renderers.size(); ++i)
548         {
549                 m_object_renderers[i]->FinaliseBuffers();
550         }
551         if (UsingGPURendering())
552                 dynamic_cast<BezierRenderer*>(m_object_renderers[BEZIER])->PrepareBezierGPUBuffer(m_document.m_objects);
553         m_render_dirty = false;
554 }
555
556 void View::SaveCPUBMP(const char * filename)
557 {
558         bool prev = UsingGPURendering();
559         SetGPURendering(false);
560         Render(800, 600);
561         ObjectRenderer::SaveBMP({m_cpu_rendering_pixels, 800, 600}, filename);
562         SetGPURendering(prev);
563 }
564
565 void View::SaveGPUBMP(const char * filename)
566 {
567         bool prev = UsingGPURendering();
568         SetGPURendering(true);
569         Render(800,600);
570         m_screen.ScreenShot(filename);
571         SetGPURendering(prev);  
572 }

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