From 99cfe9aefd4cff40de3e5950455be30001121c48 Mon Sep 17 00:00:00 2001 From: Jonas Kulla Date: Sun, 13 Oct 2013 12:58:56 +0200 Subject: [PATCH] Tilemap: Optimize scanrow z ordering --- src/scene.cpp | 18 ++++++++++++++++++ src/scene.h | 1 + src/tilemap.cpp | 23 ++++++++++++++++++----- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/scene.cpp b/src/scene.cpp index 31a7957..2c1154f 100644 --- a/src/scene.cpp +++ b/src/scene.cpp @@ -59,6 +59,24 @@ void Scene::insert(SceneElement &element) elements.append(element.link); } +void Scene::insertAfter(SceneElement &element, SceneElement &after) +{ + IntruListLink *iter; + + for (iter = &after.link; iter != elements.end(); iter = iter->next) + { + SceneElement *e = iter->data; + + if (element < *e) + { + elements.insertBefore(element.link, *iter); + return; + } + } + + elements.append(element.link); +} + void Scene::reinsert(SceneElement &element) { elements.remove(element.link); diff --git a/src/scene.h b/src/scene.h index 2913003..fca0cd3 100644 --- a/src/scene.h +++ b/src/scene.h @@ -51,6 +51,7 @@ public: protected: void insert(SceneElement &element); + void insertAfter(SceneElement &element, SceneElement &after); void reinsert(SceneElement &element); /* Notify all elements that geometry has changed */ diff --git a/src/tilemap.cpp b/src/tilemap.cpp index 9e660c3..efe48e4 100644 --- a/src/tilemap.cpp +++ b/src/tilemap.cpp @@ -230,7 +230,7 @@ struct ScanRow : public ViewportElement void drawInt(); void initUpdateZ(); - void finiUpdateZ(); + void finiUpdateZ(ScanRow *prev); }; struct TilemapPrivate @@ -1005,11 +1005,21 @@ struct TilemapPrivate void updateZOrder() { + if (elem.scanrows.isEmpty()) + return; + for (int i = 0; i < elem.scanrows.count(); ++i) elem.scanrows[i]->initUpdateZ(); - for (int i = 0; i < elem.scanrows.count(); ++i) - elem.scanrows[i]->finiUpdateZ(); + ScanRow *prev = elem.scanrows.first(); + prev->finiUpdateZ(0); + + for (int i = 1; i < elem.scanrows.count(); ++i) + { + ScanRow *row = elem.scanrows[i]; + row->finiUpdateZ(prev); + prev = row; + } } void prepare() @@ -1186,11 +1196,14 @@ void ScanRow::initUpdateZ() unlink(); } -void ScanRow::finiUpdateZ() +void ScanRow::finiUpdateZ(ScanRow *prev) { z = 32 * (index+1) - p->offset.y; - scene->insert(*this); + if (prev) + scene->insertAfter(*this, *prev); + else + scene->insert(*this); } void Tilemap::Autotiles::set(int i, Bitmap *bitmap)