From a17f5031bf0edc8c807fcff8454a5fdf1744bba6 Mon Sep 17 00:00:00 2001 From: FSCyborg Date: Sun, 27 Oct 2019 15:46:01 -0400 Subject: [PATCH] Fix the unique pointer and constructor Unique pointers need to be constructed in very particular ways, but so did this NGON radar class, so we just had to put the constructor function inside unique pointer constructor. And to keep that special pointer from going out of scope, we switched to the ternery operator. Also made these changes: Change to hypotf() Fix Shadowing, Zero Vector macro not working at all. Offset changed to offset_incoming blip changed to blip_instance Offset changed to = {} to initialize the struct without a problem. clampBlip() required an override Give offset a default initializer. Switch to (Apparently was deprecated and needs to be replaced with .) One last complaint from travis about a redundant default value. Thank you Travis! --- code/hud/hudparse.cpp | 19 ++++++++++--------- code/radar/radar.cpp | 2 +- code/radar/radarngon.cpp | 20 ++++++++++---------- code/radar/radarngon.h | 4 ++-- 4 files changed, 23 insertions(+), 22 deletions(-) diff --git a/code/hud/hudparse.cpp b/code/hud/hudparse.cpp index 95962b7dd9..934844e182 100644 --- a/code/hud/hudparse.cpp +++ b/code/hud/hudparse.cpp @@ -2714,16 +2714,17 @@ void load_gauge_radar_std(gauge_settings* settings) } } - HudGaugeRadarStd* hud_gauge; - if (num_sides == 0) { - // Standard radar - hud_gauge = gauge_load_common(settings); - } else { - // Ngon radar - hud_gauge = new HudGaugeRadarNgon(num_sides, offset); - hud_gauge = gauge_load_common(settings, static_cast(hud_gauge)); - } + //======================================================================================================= + // Cyborg17 - not using auto here is a huge pain to get exactly right and to read. If we put this in a + // normal if block, either auto will break or hud_gauge will go out of scope before the last block below. + // The ternery operator lets us circumvent this problem by allowing us to use auto, while also loading + // the radar and creating the unique_ptr. + // did we have a normal radar? If so, load the regular radar. + auto hud_gauge = (num_sides == 0) ? gauge_load_common(settings) + // if not *construct* and then load the ngon radar + : gauge_load_common(settings, new HudGaugeRadarNgon(num_sides, offset)); + // Only load this if the user hasn't specified a preference if (Cmdline_orb_radar == 0) { hud_gauge->initBitmaps(fname); diff --git a/code/radar/radar.cpp b/code/radar/radar.cpp index e11aad367c..dfc16d0cf0 100644 --- a/code/radar/radar.cpp +++ b/code/radar/radar.cpp @@ -394,7 +394,7 @@ void HudGaugeRadarStd::plotBlip(blip *b, int *x, int *y) zdist = hypotf(pos->xyz.x, pos->xyz.y); - vec3d new_pos = ZERO_VECTOR; + vec3d new_pos = {}; if (zdist >= 0.01f) { diff --git a/code/radar/radarngon.cpp b/code/radar/radarngon.cpp index 99ce5b753c..2d74b861b7 100644 --- a/code/radar/radarngon.cpp +++ b/code/radar/radarngon.cpp @@ -2,31 +2,31 @@ #include "math/vecmat.h" -#include +#include -void HudGaugeRadarNgon::clampBlip(vec3d* blip) { +void HudGaugeRadarNgon::clampBlip(vec3d* blip_instance) { float hypotenuse; - hypotenuse = _hypotf(blip->xyz.x, blip->xyz.y); + hypotenuse = hypotf(blip_instance->xyz.x, blip_instance->xyz.y); // Only do this math if the blip hypotenuse is longer than or equal to the apothem if (hypotenuse >= r_min) { - float angle = atan2(blip->xyz.y, blip->xyz.x); + float angle = atan2(blip_instance->xyz.y, blip_instance->xyz.x); vec3d Line_p; vec3d Line_v; formLine(&Line_p, &Line_v, angle); float s; - int success = find_intersection(&s, &vmd_zero_vector, &Line_p, blip, &Line_v); + int success = find_intersection(&s, &vmd_zero_vector, &Line_p, blip_instance, &Line_v); if ((success == 0) && (s < 1.0f)) { // Clamp the blip to the edge - vm_vec_scale(blip, s); + vm_vec_scale(blip_instance, s); } } // Use Std's method of clamping and scaling, we'll get some nice rounded corners in the process - HudGaugeRadarStd::clampBlip(blip); + HudGaugeRadarStd::clampBlip(blip_instance); } void HudGaugeRadarNgon::formLine(vec3d* p, vec3d* v, float angle) { @@ -50,9 +50,9 @@ void HudGaugeRadarNgon::formLine(vec3d* p, vec3d* v, float angle) { // Default to a hexagon HudGaugeRadarNgon::HudGaugeRadarNgon() - : arclen(PI2 / i2fl(6)), offset(0.0f), r_min(cos(PI / i2fl(6))) {} + : arclen(PI2 / i2fl(6)), r_min(cos(PI / i2fl(6))) {} -HudGaugeRadarNgon::HudGaugeRadarNgon(int num_sides, float offset) - : arclen(PI2 / i2fl(num_sides)), offset(offset * (PI / 180.0f)), r_min(cos(PI / i2fl(num_sides))) +HudGaugeRadarNgon::HudGaugeRadarNgon(int num_sides, float offset_incoming) + : arclen(PI2 / i2fl(num_sides)), offset(offset_incoming * (PI / 180.0f)), r_min(cos(PI / i2fl(num_sides))) { } diff --git a/code/radar/radarngon.h b/code/radar/radarngon.h index b34da7b6e2..31c0cbeaa2 100644 --- a/code/radar/radarngon.h +++ b/code/radar/radarngon.h @@ -20,7 +20,7 @@ const int RADAR_NGON_MIN_SIDES = 3; //!< Required, things break if n < 3 class HudGaugeRadarNgon : public HudGaugeRadarStd { float arclen; // The arc length of each n-gon sector (radians) - float offset; // The angle offset of the plot area (+ is counter-clockwise) (radians) + float offset {0.0f}; // The angle offset of the plot area (+ is counter-clockwise) (radians), float r_min; // The apothem of the ngon (0.0f < r_min <= 1.0f) protected: @@ -36,7 +36,7 @@ protected: * @brief Clamps and scales the blip to be within the plot area * @param[in] vec The blip coordinates (only x and y are nonzero) */ - void clampBlip(vec3d* blip); + void clampBlip(vec3d* blip) override; public: /**