From 3f69faaecfa7069c17e51c05e84f6ddc27e56814 Mon Sep 17 00:00:00 2001 From: ephphatha Date: Thu, 1 Jul 2021 22:14:39 +1000 Subject: [PATCH] Refactor DrawLifeFlask to minimise bounds checks, add comments --- Source/control.cpp | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/Source/control.cpp b/Source/control.cpp index d5a3d9d67..af412e9a2 100644 --- a/Source/control.cpp +++ b/Source/control.cpp @@ -582,19 +582,17 @@ void DrawLifeFlask(const CelOutputBuffer &out) { auto &myPlayer = plr[myplr]; - int filled = myPlayer.UpdateHitPointPercentage(); + int emptyPortion = 80 - myPlayer.UpdateHitPointPercentage(); - if (filled > 80) - filled = 80; - - filled = 80 - filled; - if (filled > 11) - filled = 11; - filled += 2; + // clamping because this function only draws the top 12% of the HP display + emptyPortion = clamp(emptyPortion, 0, 11) + 2; // +2 to account for the frame being included in the sprite - DrawFlask(out, pLifeBuff, { 13, 3 }, { PANEL_LEFT + 109, PANEL_TOP - 13 }, filled); - if (filled != 13) - DrawFlask(out, pBtmBuff, { 109, filled + 3 }, { PANEL_LEFT + 109, PANEL_TOP - 13 + filled }, 13 - filled); + // Draw the empty part of the flask + DrawFlask(out, pLifeBuff, { 13, 3 }, { PANEL_LEFT + 109, PANEL_TOP - 13 }, emptyPortion); + if (emptyPortion < 13) { + // Draw the filled part of the flask + DrawFlask(out, pBtmBuff, { 109, emptyPortion + 3 }, { PANEL_LEFT + 109, PANEL_TOP - 13 + emptyPortion }, 13 - emptyPortion); + } } void UpdateLifeFlask(const CelOutputBuffer &out)