+ m_cached_display.Bind(); //NOTE: This is redundant; Clear already calls Bind
+ m_cached_display.Clear();
+
+#ifndef QUADTREE_DISABLED
+ // I'm going to write this out in comments, so hopefully then I'll understand it. :/
+ //
+ // This code looks at the current bounds and tries to work out how they need to change
+ // to keep the view looking at the correct quadtree node.
+ //
+ // The idea is that the width/height of the view bounds are always 0.5<=wh<=1.0. We then always
+ // try to keep the bottom-right corner of the node on-screen, changing nodes to suit. Why bottom-right,
+ // you may ask. It's an excellent question, with a dubious, hand-wavey answer: because we're manipulating
+ // the bounds, it was easier to do it that way. (The top-left corner of the bounds are within the main
+ // quadtree node).
+ if (m_bounds_dirty || !m_lazy_rendering)
+ {
+ // If we're too far zoomed out, become the parent of the current node.
+ if ( m_bounds.w > 1.0 || m_bounds.h > 1.0)
+ {
+ // If a parent node exists, we'll become it.
+ //TODO: Generate a new parent node if none exists, and work out when to change child_type
+ // away from QTC_UNKNOWN
+ if (m_document.GetQuadTree().nodes[m_current_quadtree_node].parent != QUADTREE_EMPTY)
+ {
+ m_bounds = TransformFromQuadChild(m_bounds, m_document.GetQuadTree().nodes[m_current_quadtree_node].child_type);
+ m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].parent;
+ }
+ }
+
+ // If we have a parent... (This prevents some crashes, but should disappear.)
+ if (m_document.GetQuadTree().nodes[m_current_quadtree_node].parent != QUADTREE_EMPTY)
+ {
+ // If the current node is off the left-hand side of the screen...
+ while (m_bounds.x > 1)
+ {
+ //... the current node becomes the node to its right.
+ m_bounds = Rect(m_bounds.x - 1, m_bounds.y, m_bounds.w, m_bounds.h);
+ m_current_quadtree_node = m_document.GetQuadTree().GetNeighbour(m_current_quadtree_node, 1, 0, &m_document);
+ }
+ while (m_bounds.y > 1)
+ {
+ m_bounds = Rect(m_bounds.x, m_bounds.y - 1, m_bounds.w, m_bounds.h);
+ m_current_quadtree_node = m_document.GetQuadTree().GetNeighbour(m_current_quadtree_node, 0, 1, &m_document);
+ }
+ while (m_bounds.x < 0)
+ {
+ m_bounds = Rect(m_bounds.x + 1, m_bounds.y, m_bounds.w, m_bounds.h);
+ m_current_quadtree_node = m_document.GetQuadTree().GetNeighbour(m_current_quadtree_node, -1, 0, &m_document);
+ }
+ while (m_bounds.y < 0)
+ {
+ m_bounds = Rect(m_bounds.x, m_bounds.y + 1, m_bounds.w, m_bounds.h);
+ m_current_quadtree_node = m_document.GetQuadTree().GetNeighbour(m_current_quadtree_node, 0, -1, &m_document);
+ }
+ }
+
+ // Recurse into a node if we are completely within it. (If we're okay with having an invalid frame or two, we can remove this.)
+ if (ContainedInQuadChild(m_bounds, QTC_TOP_LEFT))
+ {
+ if (m_document.GetQuadTree().nodes[m_current_quadtree_node].top_left == QUADTREE_EMPTY)
+ {
+ // We want to reparent into a child node, but none exist. Get the document to create one.
+ m_document.GenQuadChild(m_current_quadtree_node, QTC_TOP_LEFT);
+ m_render_dirty = true;
+ }
+ m_bounds = TransformToQuadChild(m_bounds, QTC_TOP_LEFT);
+ m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].top_left;
+ }
+ if (ContainedInQuadChild(m_bounds, QTC_TOP_RIGHT))
+ {
+ if (m_document.GetQuadTree().nodes[m_current_quadtree_node].top_right == QUADTREE_EMPTY)
+ {
+ // We want to reparent into a child node, but none exist. Get the document to create one.
+ m_document.GenQuadChild(m_current_quadtree_node, QTC_TOP_RIGHT);
+ m_render_dirty = true;
+ }
+ m_bounds = TransformToQuadChild(m_bounds, QTC_TOP_RIGHT);
+ m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].top_right;
+ }
+ if (ContainedInQuadChild(m_bounds, QTC_BOTTOM_LEFT))
+ {
+ if (m_document.GetQuadTree().nodes[m_current_quadtree_node].bottom_left == QUADTREE_EMPTY)
+ {
+ // We want to reparent into a child node, but none exist. Get the document to create one.
+ m_document.GenQuadChild(m_current_quadtree_node, QTC_BOTTOM_LEFT);
+ m_render_dirty = true;
+ }
+ m_bounds = TransformToQuadChild(m_bounds, QTC_BOTTOM_LEFT);
+ m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].bottom_left;
+ }
+ if (ContainedInQuadChild(m_bounds, QTC_BOTTOM_RIGHT))
+ {
+ if (m_document.GetQuadTree().nodes[m_current_quadtree_node].bottom_right == QUADTREE_EMPTY)
+ {
+ // We want to reparent into a child node, but none exist. Get the document to create one.
+ m_document.GenQuadChild(m_current_quadtree_node, QTC_BOTTOM_RIGHT);
+ m_render_dirty = true;
+ }
+ m_bounds = TransformToQuadChild(m_bounds, QTC_BOTTOM_RIGHT);
+ m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].bottom_right;
+ }
+
+ // Otherwise, we'll arbitrarily select the bottom-right.
+ // TODO: Perhaps select based on greatest area?
+ if (m_bounds.w < 0.5 || m_bounds.h < 0.5)
+ {
+ if (m_document.GetQuadTree().nodes[m_current_quadtree_node].bottom_right == QUADTREE_EMPTY)
+ {
+ // We want to reparent into a child node, but none exist. Get the document to create one.
+ m_document.GenQuadChild(m_current_quadtree_node, QTC_BOTTOM_RIGHT);
+ m_render_dirty = true;
+ }
+ m_bounds = TransformToQuadChild(m_bounds, QTC_BOTTOM_RIGHT);
+ m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].bottom_right;
+ }
+ }
+
+ m_screen.DebugFontPrintF("Current View QuadTree");
+ QuadTreeIndex overlay = m_current_quadtree_node;
+ while (overlay != -1)
+ {
+ m_screen.DebugFontPrintF(" Node: %d (objs: %d -> %d)", overlay, m_document.GetQuadTree().nodes[overlay].object_begin,
+ m_document.GetQuadTree().nodes[overlay].object_end);
+ overlay = m_document.GetQuadTree().nodes[overlay].next_overlay;
+ }
+ m_screen.DebugFontPrintF("\n");
+ m_screen.DebugFontPrintF("Left: %d, Right: %d, Up: %d, Down: %d\n",
+ m_document.GetQuadTree().GetNeighbour(m_current_quadtree_node, -1, 0, 0),
+ m_document.GetQuadTree().GetNeighbour(m_current_quadtree_node, 1, 0, 0),
+ m_document.GetQuadTree().GetNeighbour(m_current_quadtree_node, 0, -1, 0),
+ m_document.GetQuadTree().GetNeighbour(m_current_quadtree_node, 0, 1, 0));
+
+
+ Rect view_top_bounds = m_bounds;
+ QuadTreeIndex tmp = m_current_quadtree_node;
+ while (tmp != -1)
+ {
+ view_top_bounds = TransformFromQuadChild(view_top_bounds, m_document.GetQuadTree().nodes[tmp].child_type);
+ tmp = m_document.GetQuadTree().nodes[tmp].parent;
+ }
+ m_screen.DebugFontPrintF("Equivalent View Bounds: %s\n", view_top_bounds.Str().c_str());
+#endif
+
+ if (!m_use_gpu_rendering)
+ {
+ // Dynamically resize CPU rendering target pixels if needed
+ if (m_cpu_rendering_pixels == NULL || width*height > prev_width*prev_height)
+ {
+ delete [] m_cpu_rendering_pixels;
+ m_cpu_rendering_pixels = new uint8_t[width*height*4];
+ if (m_cpu_rendering_pixels == NULL)
+ Fatal("Could not allocate %d*%d*4 = %d bytes for cpu rendered pixels", width, height, width*height*4);
+ }
+ // Clear CPU rendering pixels
+ for (int i = 0; i < width*height*4; ++i)
+ m_cpu_rendering_pixels[i] = 255;
+ }
+#ifdef QUADTREE_DISABLED
+ RenderRange(width, height, 0, m_document.ObjectCount());
+#else
+ RenderQuadtreeNode(width, height, m_current_quadtree_node, m_quadtree_max_depth);
+#endif
+ if (!m_use_gpu_rendering)
+ {
+ m_screen.RenderPixels(0,0,width, height, m_cpu_rendering_pixels); //TODO: Make this work :(
+ // Debug for great victory (do something similar for GPU and compare?)
+ //ObjectRenderer::SaveBMP({m_cpu_rendering_pixels, width, height}, "cpu_rendering_last_frame.bmp");
+ }
+ m_cached_display.UnBind(); // resets render target to the screen
+ m_cached_display.Blit(); // blit FrameBuffer to screen
+ m_buffer_dirty = false;
+ glPopDebugGroup();
+
+#ifndef CONTROLPANEL_DISABLED
+ // The powers that be suggest that this may be causing of the segfaults.
+ //ControlPanel::Update();
+#endif //CONTROLPANEL_DISABLED
+ //Debug("Completed Render");
+
+}
+
+#ifndef QUADTREE_DISABLED
+void View::RenderQuadtreeNode(int width, int height, QuadTreeIndex node, int remaining_depth)
+{
+ Rect old_bounds = m_bounds;
+ if (node == QUADTREE_EMPTY) return;
+ if (!remaining_depth) return;
+ //Debug("Rendering QT node %d, (objs: %d -- %d)\n", node, m_document.GetQuadTree().nodes[node].object_begin, m_document.GetQuadTree().nodes[node].object_end);
+ m_bounds_dirty = true;
+ m_render_dirty = m_buffer_dirty = true;
+ QuadTreeIndex overlay = node;
+ while(overlay != -1)
+ {
+ RenderRange(width, height, m_document.GetQuadTree().nodes[overlay].object_begin, m_document.GetQuadTree().nodes[overlay].object_end);
+ overlay = m_document.GetQuadTree().nodes[overlay].next_overlay;
+ }