From 38a69a36b305490725ece24b80ab8b3f9628dfca Mon Sep 17 00:00:00 2001 From: Alex Foster <123afoster@gmail.com> Date: Wed, 31 Jul 2024 23:45:24 -0700 Subject: [PATCH] change findtag() functions to optionals (#268) --- src/engine/model/animmodel.cpp | 4 ++-- src/engine/model/animmodel.h | 2 +- src/engine/model/skelmodel.cpp | 14 +++++++------- src/engine/model/skelmodel.h | 4 ++-- src/engine/model/vertmodel.cpp | 16 ++++++++-------- src/engine/model/vertmodel.h | 6 +++--- 6 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/engine/model/animmodel.cpp b/src/engine/model/animmodel.cpp index 0d939be1..4d5b0c54 100644 --- a/src/engine/model/animmodel.cpp +++ b/src/engine/model/animmodel.cpp @@ -749,7 +749,7 @@ void animmodel::part::genshadowmesh(std::vector &tris, const matrix4x3 bool animmodel::part::link(part *p, const char *tag, const vec &translate, int anim, int basetime, vec *pos) { - int i = meshes ? meshes->findtag(tag) : -1; + std::optional i = meshes ? meshes->findtag(tag) : std::nullopt; if(i<0) { for(const linkedpart &i : links) @@ -764,7 +764,7 @@ bool animmodel::part::link(part *p, const char *tag, const vec &translate, int a links.emplace_back(); linkedpart &l = links.back(); l.p = p; - l.tag = i; + l.tag = *i; l.anim = anim; l.basetime = basetime; l.translate = translate; diff --git a/src/engine/model/animmodel.h b/src/engine/model/animmodel.h index 7e112cb1..2cc9520c 100644 --- a/src/engine/model/animmodel.h +++ b/src/engine/model/animmodel.h @@ -410,7 +410,7 @@ class animmodel : public model virtual ~meshgroup(); virtual void concattagtransform(int i, const matrix4x3 &m, matrix4x3 &n) const = 0; - virtual int findtag(std::string_view name) = 0; + virtual std::optional findtag(std::string_view name) = 0; virtual int totalframes() const = 0; virtual void *animkey() = 0; virtual void cleanup() = 0; diff --git a/src/engine/model/skelmodel.cpp b/src/engine/model/skelmodel.cpp index be11e05d..ccff8194 100644 --- a/src/engine/model/skelmodel.cpp +++ b/src/engine/model/skelmodel.cpp @@ -184,28 +184,28 @@ std::optional skelmodel::skeleton::findbone(const std::string &name) con return std::nullopt; } -int skelmodel::skeleton::findtag(std::string_view name) const +std::optional skelmodel::skeleton::findtag(std::string_view name) const { - for(uint i = 0; i < tags.size(); i++) + for(size_t i = 0; i < tags.size(); i++) { if(!std::strcmp(tags[i].name.c_str(), name.data())) { return i; } } - return -1; + return std::nullopt; } bool skelmodel::skeleton::addtag(std::string_view name, int bone, const matrix4x3 &matrix) { - int idx = findtag(name); - if(idx >= 0) + std::optional idx = findtag(name); + if(idx) { if(!testtags) { return false; } - tag &t = tags[idx]; + tag &t = tags[*idx]; t.bone = bone; t.matrix = matrix; } @@ -1800,7 +1800,7 @@ skelmodel::skeleton::boneinfo::~boneinfo() // skelmeshgroup -int skelmodel::skelmeshgroup::findtag(std::string_view name) +std::optional skelmodel::skelmeshgroup::findtag(std::string_view name) { return skel->findtag(name); } diff --git a/src/engine/model/skelmodel.h b/src/engine/model/skelmodel.h index ca0a8c80..e1899575 100644 --- a/src/engine/model/skelmodel.h +++ b/src/engine/model/skelmodel.h @@ -431,7 +431,7 @@ struct skelmodel : animmodel const skelanimspec *findskelanim(std::string_view name, char sep = '\0') const; skelanimspec &addskelanim(const std::string &name, int numframes, int animframes); std::optional findbone(const std::string &name) const; - int findtag(std::string_view name) const; + std::optional findtag(std::string_view name) const; bool addtag(std::string_view name, int bone, const matrix4x3 &matrix); void addpitchdep(int bone, int frame); int findpitchdep(int bone) const; @@ -549,7 +549,7 @@ struct skelmodel : animmodel virtual ~skelmeshgroup(); - int findtag(std::string_view) override final; + std::optional findtag(std::string_view) override final; void *animkey() override final; int totalframes() const override final; void concattagtransform(int i, const matrix4x3 &m, matrix4x3 &n) const override final; diff --git a/src/engine/model/vertmodel.cpp b/src/engine/model/vertmodel.cpp index 6e5244c6..6490289c 100644 --- a/src/engine/model/vertmodel.cpp +++ b/src/engine/model/vertmodel.cpp @@ -206,22 +206,22 @@ void vertmodel::vertmeshgroup::concattagtransform(int i, const matrix4x3 &m, mat { } -int vertmodel::vertmeshgroup::findtag(std::string_view name) +std::optional vertmodel::vertmeshgroup::findtag(std::string_view name) { - for(int i = 0; i < numtags; ++i) + for(size_t i = 0; i < numtags; ++i) { if(tags[i].name == name.data()) { return i; } } - return -1; + return std::nullopt; } bool vertmodel::vertmeshgroup::addtag(std::string_view name, const matrix4x3 &matrix) { - int idx = findtag(name); - if(idx >= 0) + std::optional idx = findtag(name); + if(idx) { if(!testtags) { @@ -229,7 +229,7 @@ bool vertmodel::vertmeshgroup::addtag(std::string_view name, const matrix4x3 &ma } for(int i = 0; i < numframes; ++i) { - tag &t = tags[i*numtags + idx]; + tag &t = tags[i*numtags + *idx]; t.matrix = matrix; } } @@ -242,13 +242,13 @@ bool vertmodel::vertmeshgroup::addtag(std::string_view name, const matrix4x3 &ma *src = &tags[numtags*i]; if(!i) { - for(int j = 0; j < numtags; ++j) + for(size_t j = 0; j < numtags; ++j) { std::swap(dst[j].name, src[j].name); } dst[numtags].name = name.data(); } - for(int j = 0; j < numtags; ++j) + for(size_t j = 0; j < numtags; ++j) { dst[j].matrix = src[j].matrix; } diff --git a/src/engine/model/vertmodel.h b/src/engine/model/vertmodel.h index 760eff7e..3cca0c71 100644 --- a/src/engine/model/vertmodel.h +++ b/src/engine/model/vertmodel.h @@ -164,14 +164,14 @@ class vertmodel : public animmodel std::string name; matrix4x3 matrix; - tag() : name(nullptr) {} + tag() {} }; struct vertmeshgroup : meshgroup { int numframes; tag *tags; - int numtags; + size_t numtags; static constexpr int maxvbocache = 16; vbocacheentry vbocache[maxvbocache]; @@ -185,7 +185,7 @@ class vertmodel : public animmodel virtual void concattagtransform(int i, const matrix4x3 &m, matrix4x3 &n) const override final; bool addtag(std::string_view name, const matrix4x3 &matrix); - int findtag(std::string_view name) override final; + std::optional findtag(std::string_view name) override final; int totalframes() const override final; void calctagmatrix(const part *p, int i, const AnimState &as, matrix4 &matrix) const;