From 8b87881b2ae870b1176fc5c0d4d300dfa13acd67 Mon Sep 17 00:00:00 2001 From: Alex Foster <123afoster@gmail.com> Date: Mon, 4 Dec 2023 00:49:50 +0800 Subject: [PATCH] std::optional in objmeshgroup::load for #268 --- src/engine/model/obj.cpp | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/engine/model/obj.cpp b/src/engine/model/obj.cpp index b1251257d..166146cae 100644 --- a/src/engine/model/obj.cpp +++ b/src/engine/model/obj.cpp @@ -79,7 +79,7 @@ bool obj::objmeshgroup::load(const char *filename, float smooth) numframes = 1; std::array, 3> attrib; char buf[512]; - std::unordered_map verthash; + std::unordered_map verthash; std::vector verts; std::vector tcverts; std::vector tris; @@ -154,8 +154,8 @@ bool obj::objmeshgroup::load(const char *filename, float smooth) tcverts.clear(); tris.clear(); } - int v0 = -1, - v1 = -1; + std::optional v0 = std::nullopt, + v1 = std::nullopt; while(std::isalpha(*c)) { c++; @@ -193,7 +193,7 @@ bool obj::objmeshgroup::load(const char *filename, float smooth) c++; } auto itr = verthash.find(vkey); - int *index; + uint *index; if(itr == verthash.end()) { index = &verthash[vkey]; @@ -212,11 +212,11 @@ bool obj::objmeshgroup::load(const char *filename, float smooth) { index = &(*itr).second; } - if(v0 < 0) + if(!v0) { v0 = *index; } - else if(v1 < 0) + else if(!v1) { v1 = *index; } @@ -224,9 +224,10 @@ bool obj::objmeshgroup::load(const char *filename, float smooth) { tris.emplace_back(); tri &t = tris.back(); - t.vert[0] = static_cast(*index); - t.vert[1] = static_cast(v1); - t.vert[2] = static_cast(v0); + t.vert[0] = *index; + t.vert[1] = v1.value(); + t.vert[2] = v0.value(); + v1 = *index; } }