+#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)
+ {
+ g_profiler.BeginZone("View::Render -- Quadtree view bounds management");
+ // If we're too far zoomed out, become the parent of the current node.
+ while ( 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;
+ }
+ else break;
+ }
+
+ // 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?
+ while (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;
+ }
+ g_profiler.EndZone();
+ }
+
+ 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