Skip to content

Commit

Permalink
change findtag() functions to optionals (#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
no-lex committed Aug 1, 2024
1 parent c138323 commit 38a69a3
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/engine/model/animmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ void animmodel::part::genshadowmesh(std::vector<triangle> &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<size_t> i = meshes ? meshes->findtag(tag) : std::nullopt;
if(i<0)
{
for(const linkedpart &i : links)
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/engine/model/animmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<size_t> findtag(std::string_view name) = 0;
virtual int totalframes() const = 0;
virtual void *animkey() = 0;
virtual void cleanup() = 0;
Expand Down
14 changes: 7 additions & 7 deletions src/engine/model/skelmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,28 +184,28 @@ std::optional<size_t> skelmodel::skeleton::findbone(const std::string &name) con
return std::nullopt;
}

int skelmodel::skeleton::findtag(std::string_view name) const
std::optional<size_t> 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<size_t> idx = findtag(name);
if(idx)
{
if(!testtags)
{
return false;
}
tag &t = tags[idx];
tag &t = tags[*idx];
t.bone = bone;
t.matrix = matrix;
}
Expand Down Expand Up @@ -1800,7 +1800,7 @@ skelmodel::skeleton::boneinfo::~boneinfo()

// skelmeshgroup

int skelmodel::skelmeshgroup::findtag(std::string_view name)
std::optional<size_t> skelmodel::skelmeshgroup::findtag(std::string_view name)
{
return skel->findtag(name);
}
Expand Down
4 changes: 2 additions & 2 deletions src/engine/model/skelmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<size_t> findbone(const std::string &name) const;
int findtag(std::string_view name) const;
std::optional<size_t> 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;
Expand Down Expand Up @@ -549,7 +549,7 @@ struct skelmodel : animmodel

virtual ~skelmeshgroup();

int findtag(std::string_view) override final;
std::optional<size_t> 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;
Expand Down
16 changes: 8 additions & 8 deletions src/engine/model/vertmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,30 +206,30 @@ void vertmodel::vertmeshgroup::concattagtransform(int i, const matrix4x3 &m, mat
{
}

int vertmodel::vertmeshgroup::findtag(std::string_view name)
std::optional<size_t> 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<size_t> idx = findtag(name);
if(idx)
{
if(!testtags)
{
return false;
}
for(int i = 0; i < numframes; ++i)
{
tag &t = tags[i*numtags + idx];
tag &t = tags[i*numtags + *idx];
t.matrix = matrix;
}
}
Expand All @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions src/engine/model/vertmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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<size_t> 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;
Expand Down

0 comments on commit 38a69a3

Please sign in to comment.