From 73a5daab82ccfd30e73560b7b6bd218d1399d3c9 Mon Sep 17 00:00:00 2001 From: Filip Gawin Date: Fri, 26 Jan 2018 16:47:07 +0100 Subject: [PATCH] Use range loop --- rwengine/src/ai/AIGraph.cpp | 3 +-- rwengine/src/data/GameTexts.cpp | 4 ++-- rwengine/src/dynamics/CollisionInstance.cpp | 6 ++---- rwengine/src/engine/GameState.cpp | 6 +++--- rwengine/src/engine/GameWorld.cpp | 3 +-- rwengine/src/engine/ScreenText.cpp | 8 ++++---- rwengine/src/loaders/LoaderIFP.cpp | 6 +++--- rwengine/src/loaders/WeatherLoader.cpp | 4 ++-- rwengine/src/objects/CharacterObject.cpp | 10 +++++----- rwengine/src/render/TextRenderer.cpp | 4 +--- rwengine/src/render/TextRenderer.hpp | 2 +- rwengine/src/render/ViewFrustum.hpp | 4 ++-- rwengine/src/script/SCMFile.cpp | 4 ++-- rwgame/MenuSystem.hpp | 4 ++-- rwgame/RWGame.cpp | 6 ++---- rwlib/source/loaders/LoaderIMG.cpp | 6 +++--- 16 files changed, 36 insertions(+), 44 deletions(-) diff --git a/rwengine/src/ai/AIGraph.cpp b/rwengine/src/ai/AIGraph.cpp index 0ae455b3..9865a76b 100644 --- a/rwengine/src/ai/AIGraph.cpp +++ b/rwengine/src/ai/AIGraph.cpp @@ -28,8 +28,7 @@ void AIGraph::createPathNodes(const glm::vec3& position, glm::vec3 nodePosition = position + (rotation * node.position); if (node.type == PathNode::EXTERNAL) { - for (size_t rn = 0; rn < externalNodes.size(); ++rn) { - auto& realNode = externalNodes[rn]; + for (auto &realNode : externalNodes) { auto d = glm::distance2(realNode->position, nodePosition); if (d < 1.f) { pathNodes.push_back(realNode); diff --git a/rwengine/src/data/GameTexts.cpp b/rwengine/src/data/GameTexts.cpp index ee30ab8d..cf84239a 100644 --- a/rwengine/src/data/GameTexts.cpp +++ b/rwengine/src/data/GameTexts.cpp @@ -2,8 +2,8 @@ GameString GameStringUtil::fromString(const std::string& str) { GameString s; - for (std::string::size_type i = 0u; i < str.size(); ++i) { - s += str[i]; + for (const char &i : str) { + s += i; } return s; } diff --git a/rwengine/src/dynamics/CollisionInstance.cpp b/rwengine/src/dynamics/CollisionInstance.cpp index a5de6ec8..e6472864 100644 --- a/rwengine/src/dynamics/CollisionInstance.cpp +++ b/rwengine/src/dynamics/CollisionInstance.cpp @@ -78,8 +78,7 @@ bool CollisionInstance::createPhysicsBody(GameObject* object, t.setIdentity(); // Boxes - for (size_t i = 0; i < collision->boxes.size(); ++i) { - auto& box = collision->boxes[i]; + for (const auto &box : collision->boxes) { auto size = (box.max - box.min) / 2.f; auto mid = (box.min + box.max) / 2.f; btCollisionShape* bshape = @@ -94,8 +93,7 @@ bool CollisionInstance::createPhysicsBody(GameObject* object, } // Spheres - for (size_t i = 0; i < collision->spheres.size(); ++i) { - auto& sphere = collision->spheres[i]; + for (const auto &sphere : collision->spheres) { btCollisionShape* sshape = new btSphereShape(sphere.radius); t.setOrigin( btVector3(sphere.center.x, sphere.center.y, sphere.center.z)); diff --git a/rwengine/src/engine/GameState.cpp b/rwengine/src/engine/GameState.cpp index dd02330d..47aea005 100644 --- a/rwengine/src/engine/GameState.cpp +++ b/rwengine/src/engine/GameState.cpp @@ -121,9 +121,9 @@ GameState::GameState() int GameState::addRadarBlip(BlipData& blip) { int l = 0; - for (auto x = radarBlips.begin(); x != radarBlips.end(); ++x) { - if ((x->first) != l) { - l = x->first - 1; + for (const auto &radarBlip : radarBlips) { + if ((radarBlip.first) != l) { + l = radarBlip.first - 1; } else { l++; } diff --git a/rwengine/src/engine/GameWorld.cpp b/rwengine/src/engine/GameWorld.cpp index ed6e91c8..66e53e56 100644 --- a/rwengine/src/engine/GameWorld.cpp +++ b/rwengine/src/engine/GameWorld.cpp @@ -86,8 +86,7 @@ bool GameWorld::placeItems(const std::string& name) { if (ipll.load(path)) { // Find the object. - for (size_t i = 0; i < ipll.m_instances.size(); ++i) { - std::shared_ptr inst = ipll.m_instances[i]; + for (auto inst : ipll.m_instances) { if (!createInstance(inst->id, inst->pos, inst->rot)) { logger->error("World", "No object data for instance " + std::to_string(inst->id) + " in " + diff --git a/rwengine/src/engine/ScreenText.cpp b/rwengine/src/engine/ScreenText.cpp index ecd6f599..3eb069dc 100644 --- a/rwengine/src/engine/ScreenText.cpp +++ b/rwengine/src/engine/ScreenText.cpp @@ -8,13 +8,13 @@ void ScreenText::tick(float dt) { // Remove all the immedate text m_textQueues[static_cast(ScreenTextType::Immediate)].clear(); - for (unsigned int t = 0; t < m_textQueues.size(); ++t) { - for (unsigned int i = 0; i < m_textQueues[t].size();) { - auto& big = m_textQueues[t][i]; + for (auto &textQueue : m_textQueues) { + for (unsigned int i = 0; i < textQueue.size();) { + auto& big = textQueue[i]; big.displayedMS += millis; if (big.displayedMS >= big.durationMS) { - m_textQueues[t].erase(m_textQueues[t].begin() + i); + textQueue.erase(textQueue.begin() + i); } else { ++i; } diff --git a/rwengine/src/loaders/LoaderIFP.cpp b/rwengine/src/loaders/LoaderIFP.cpp index 7981c2c4..c2c2a6ab 100644 --- a/rwengine/src/loaders/LoaderIFP.cpp +++ b/rwengine/src/loaders/LoaderIFP.cpp @@ -48,9 +48,9 @@ AnimationKeyframe AnimationBone::getInterpolatedKeyframe(float time) { } AnimationKeyframe AnimationBone::getKeyframe(float time) { - for (size_t f = 0; f < frames.size(); ++f) { - if (time >= frames[f].starttime) { - return frames[f]; + for (auto &frame : frames) { + if (time >= frame.starttime) { + return frame; } } return frames.back(); diff --git a/rwengine/src/loaders/WeatherLoader.cpp b/rwengine/src/loaders/WeatherLoader.cpp index a0c2da60..8adcdea8 100644 --- a/rwengine/src/loaders/WeatherLoader.cpp +++ b/rwengine/src/loaders/WeatherLoader.cpp @@ -47,9 +47,9 @@ bool WeatherLoader::load(const std::string& filename, Weather& outWeather) { weather.bottomCloudColor = readRGB(ss); int d; - for (size_t i = 0; i < 4; i++) { + for (auto &i : weather.unknown) { ss >> d; - weather.unknown[i] = d; + i = d; } outWeather.entries.push_back(weather); diff --git a/rwengine/src/objects/CharacterObject.cpp b/rwengine/src/objects/CharacterObject.cpp index 647f840f..99331024 100644 --- a/rwengine/src/objects/CharacterObject.cpp +++ b/rwengine/src/objects/CharacterObject.cpp @@ -521,16 +521,16 @@ void CharacterObject::resetToAINode() { bool vehicleNode = !!getCurrentVehicle(); AIGraphNode* nearest = nullptr; float d = std::numeric_limits::max(); - for (auto it = nodes.begin(); it != nodes.end(); ++it) { + for (const auto &node : nodes) { if (vehicleNode) { - if ((*it)->type == AIGraphNode::Pedestrian) continue; + if (node->type == AIGraphNode::Pedestrian) continue; } else { - if ((*it)->type == AIGraphNode::Vehicle) continue; + if (node->type == AIGraphNode::Vehicle) continue; } - float dist = glm::length((*it)->position - getPosition()); + float dist = glm::length(node->position - getPosition()); if (dist < d) { - nearest = *it; + nearest = node; d = dist; } } diff --git a/rwengine/src/render/TextRenderer.cpp b/rwengine/src/render/TextRenderer.cpp index c36fb11b..bb533781 100644 --- a/rwengine/src/render/TextRenderer.cpp +++ b/rwengine/src/render/TextRenderer.cpp @@ -91,9 +91,7 @@ TextRenderer::TextRenderer(GameRenderer* renderer) : renderer(renderer) { textShader = renderer->getRenderer()->createShader(TextVertexShader, TextFragmentShader); - for (int g = 0; g < GAME_GLYPHS; g++) { - glyphData[g] = {.9f}; - } + std::fill(glyphData.begin(), glyphData.end(), GlyphInfo{.9f}); glyphData[charToIndex(' ')].widthFrac = 0.4f; glyphData[charToIndex('-')].widthFrac = 0.5f; diff --git a/rwengine/src/render/TextRenderer.hpp b/rwengine/src/render/TextRenderer.hpp index 0428d12b..6abe784b 100644 --- a/rwengine/src/render/TextRenderer.hpp +++ b/rwengine/src/render/TextRenderer.hpp @@ -66,7 +66,7 @@ public: private: std::string fonts[GAME_FONTS]; - GlyphInfo glyphData[GAME_GLYPHS]; + std::array glyphData; GameRenderer* renderer; std::unique_ptr textShader; diff --git a/rwengine/src/render/ViewFrustum.hpp b/rwengine/src/render/ViewFrustum.hpp index 63d396a3..a690ca17 100644 --- a/rwengine/src/render/ViewFrustum.hpp +++ b/rwengine/src/render/ViewFrustum.hpp @@ -50,8 +50,8 @@ public: float d; bool result = true; - for (size_t i = 0; i < 6; ++i) { - d = glm::dot(planes[i].normal, center) + planes[i].distance; + for (const auto &plane : planes) { + d = glm::dot(plane.normal, center) + plane.distance; if (d < -radius) result = false; } diff --git a/rwengine/src/script/SCMFile.cpp b/rwengine/src/script/SCMFile.cpp index 5834f2fb..dfb8872f 100644 --- a/rwengine/src/script/SCMFile.cpp +++ b/rwengine/src/script/SCMFile.cpp @@ -26,8 +26,8 @@ void SCMFile::loadFile(char *data, unsigned int size) { int i = modelSectionOffset + sizeof(uint32_t); for (unsigned int m = 0; m < model_count; ++m) { char model_name[24]; - for (size_t c = 0; c < 24; ++c) { - model_name[c] = read(i++); + for (char &c : model_name) { + c = read(i++); } models.push_back(model_name); } diff --git a/rwgame/MenuSystem.hpp b/rwgame/MenuSystem.hpp index ceb342ed..4a7c63a0 100644 --- a/rwgame/MenuSystem.hpp +++ b/rwgame/MenuSystem.hpp @@ -126,9 +126,9 @@ public: void click(const float x, const float y) { glm::vec2 c(x - offset.x, y - offset.y); - for (auto it = entries.begin(); it != entries.end(); ++it) { + for (auto &entry : entries) { if (c.y > 0.f && c.y < size) { - (*it).activate(c.x, c.y); + entry.activate(c.x, c.y); return; } else { c.y -= size; diff --git a/rwgame/RWGame.cpp b/rwgame/RWGame.cpp index 121af500..3ad9d4d4 100644 --- a/rwgame/RWGame.cpp +++ b/rwgame/RWGame.cpp @@ -683,8 +683,7 @@ void RWGame::renderDebugPaths(float time) { } // Draw Garage bounds - for (size_t g = 0; g < state.garages.size(); ++g) { - auto& garage = state.garages[g]; + for (const auto &garage : state.garages) { btVector3 minColor(1.f, 0.f, 0.f); btVector3 maxColor(0.f, 1.f, 0.f); btVector3 min(garage.min.x, garage.min.y, garage.min.z); @@ -699,8 +698,7 @@ void RWGame::renderDebugPaths(float time) { } // Draw vehicle generators - for (size_t v = 0; v < state.vehicleGenerators.size(); ++v) { - auto& generator = state.vehicleGenerators[v]; + for (const auto &generator : state.vehicleGenerators) { btVector3 color(1.f, 0.f, 0.f); btVector3 position(generator.position.x, generator.position.y, generator.position.z); diff --git a/rwlib/source/loaders/LoaderIMG.cpp b/rwlib/source/loaders/LoaderIMG.cpp index dd32525a..e4fa7cf4 100644 --- a/rwlib/source/loaders/LoaderIMG.cpp +++ b/rwlib/source/loaders/LoaderIMG.cpp @@ -40,9 +40,9 @@ bool LoaderIMG::load(const rwfs::path& filepath) { /// Get the information of a asset in the examining archive bool LoaderIMG::findAssetInfo(const std::string& assetname, LoaderIMGFile& out) { - for (size_t i = 0; i < m_assets.size(); ++i) { - if (boost::iequals(m_assets[i].name, assetname)) { - out = m_assets[i]; + for (auto &asset : m_assets) { + if (boost::iequals(asset.name, assetname)) { + out = asset; return true; } }