Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix portability use of std::clamp #596

Merged
merged 1 commit into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions libultraship/libultraship/Lib/Fast3D/gfx_pc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "../../Environment.h"
#include "../../GameVersions.h"
#include "../../ResourceMgr.h"
#include "../../Utils.h"

// OTRTODO: fix header files for these
extern "C" {
Expand Down Expand Up @@ -1062,8 +1063,8 @@ static void gfx_sp_vertex(size_t n_vertices, size_t dest_index, const Vtx *verti
dotx /= 127.0f;
doty /= 127.0f;

std::clamp(dotx, -1.0f, 1.0f);
std::clamp(doty, -1.0f, 1.0f);
dotx = math::clamp(dotx, -1.0f, 1.0f);
doty = math::clamp(doty, -1.0f, 1.0f);

if (rsp.geometry_mode & G_TEXTURE_GEN_LINEAR) {
// Not sure exactly what formula we should use to get accurate values
Expand Down Expand Up @@ -1115,7 +1116,7 @@ static void gfx_sp_vertex(size_t n_vertices, size_t dest_index, const Vtx *verti
if (winv < 0.0f) winv = std::numeric_limits<int16_t>::max();

float fog_z = z * winv * rsp.fog_mul + rsp.fog_offset;
std::clamp(fog_z, 0.0f, 255.0f);
fog_z = math::clamp(fog_z, 0.0f, 255.0f);
d->color.a = fog_z; // Use alpha variable to store fog factor
} else {
d->color.a = v->cn[3];
Expand Down
7 changes: 7 additions & 0 deletions libultraship/libultraship/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
#define strdup _strdup
#endif

namespace math {
float clamp(float d, float min, float max) {
const float t = d < min ? min : d;
return t > max ? max : t;
}
}

namespace Utils {
std::vector<std::string> SplitText(const std::string text, char separator = ' ', bool keep_quotes = false) {
std::vector<std::string> args;
Expand Down
6 changes: 5 additions & 1 deletion libultraship/libultraship/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
#include <string>
#include <vector>

namespace math {
float clamp(float d, float min, float max);
}

namespace Utils {
std::vector<std::string> SplitText(const std::string& text, char separator, bool keep_quotes);
std::vector<std::string> SplitText(const std::string& text, char separator, bool keep_quotes);
}