summaryrefslogtreecommitdiff
path: root/src/vehicle.cpp
diff options
context:
space:
mode:
authorMatt Kimber <github@mattkimber.org.uk>2021-01-02 22:39:30 +0000
committerPatric Stout <github@truebrain.nl>2021-01-05 11:42:25 +0100
commiteeb88e87d8c7b62e0bac94ede44cceee987b8d09 (patch)
treeb00a93d08d32763f0eddc31df0a2ed7739dbc5c3 /src/vehicle.cpp
parent979b4af6cae925e7426ab9e4b4c5bdc9084545fd (diff)
downloadopenttd-eeb88e87d8c7b62e0bac94ede44cceee987b8d09.tar.xz
Codechange: improve performance for complex vehicle chains by resolving sprites less often
Diffstat (limited to 'src/vehicle.cpp')
-rw-r--r--src/vehicle.cpp35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/vehicle.cpp b/src/vehicle.cpp
index e014944a9..8dead6cee 100644
--- a/src/vehicle.cpp
+++ b/src/vehicle.cpp
@@ -1091,6 +1091,23 @@ static void DoDrawVehicle(const Vehicle *v)
if (to != TO_INVALID && (IsTransparencySet(to) || IsInvisibilitySet(to))) return;
}
+ /*
+ * If the vehicle sprite was not updated despite further viewport changes, we need
+ * to update it before drawing.
+ *
+ * I'm not keen on casting to mutable - it's the approach JGR uses in JGRPP but I
+ * wonder if there's a cleaner option (even though we can only take the decision
+ * whether to update once we already know the vehicle is going to appear in a
+ * viewport)
+ */
+ if (v->rstate.sprite_has_viewport_changes) {
+ Vehicle* v_mutable = const_cast<Vehicle*>(v);
+ VehicleSpriteSeq seq;
+ v_mutable->GetImage(v_mutable->direction, EIT_ON_MAP, &seq);
+ v_mutable->sprite_seq = seq;
+ v_mutable->rstate.sprite_has_viewport_changes = false;
+ }
+
StartSpriteCombine();
for (uint i = 0; i < v->sprite_seq.count; ++i) {
PaletteID pal2 = v->sprite_seq.seq[i].pal;
@@ -1139,6 +1156,7 @@ void ViewportAddVehicles(DrawPixelInfo *dpi)
const Vehicle *v = _vehicle_viewport_hash[x + y]; // already masked & 0xFFF
while (v != nullptr) {
+
if (!(v->vehstatus & VS_HIDDEN) &&
l <= v->coord.right &&
t <= v->coord.bottom &&
@@ -1146,6 +1164,23 @@ void ViewportAddVehicles(DrawPixelInfo *dpi)
b >= v->coord.top) {
DoDrawVehicle(v);
}
+ else {
+ /*
+ * Indicate that this vehicle was considered for rendering in a viewport,
+ * and we therefore need to update sprites more frequently in case a callback
+ * will change the bounding box to one which will cause the sprite to be
+ * displayed.
+ *
+ * This reduces the chances of flicker when sprites enter the screen, if they
+ * are part of a newgrf vehicle set which changes bounding boxes within a
+ * single vehicle direction.
+ *
+ * TODO: is there a cleaner solution than casting to a mutable type?
+ */
+ Vehicle* v_mutable = const_cast<Vehicle*>(v);
+ v_mutable->rstate.is_viewport_candidate = true;
+ }
+
v = v->hash_viewport_next;
}