Skip to content

Commit 8b62a5d

Browse files
committed
Fixed MSVC warning for potential mod by 0 (C4724)
1 parent be3ecae commit 8b62a5d

File tree

1 file changed

+37
-29
lines changed

1 file changed

+37
-29
lines changed

editor/plugins/gizmos/collision_polygon_3d_gizmo_plugin.cpp

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ CollisionPolygon3DGizmoPlugin::CollisionPolygon3DGizmoPlugin() {
4141
create_collision_material("shape_material_arraymesh_disabled", 0.015625);
4242
}
4343

44-
void CollisionPolygon3DGizmoPlugin::create_collision_material(const String &p_name, float p_alpha) {
44+
void CollisionPolygon3DGizmoPlugin::create_collision_material(const String& p_name, float p_alpha) {
4545
Vector<Ref<StandardMaterial3D>> mats;
4646

4747
const Color collision_color(1.0, 1.0, 1.0, p_alpha);
@@ -70,7 +70,7 @@ void CollisionPolygon3DGizmoPlugin::create_collision_material(const String &p_na
7070
materials[p_name] = mats;
7171
}
7272

73-
bool CollisionPolygon3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
73+
bool CollisionPolygon3DGizmoPlugin::has_gizmo(Node3D* p_spatial) {
7474
return Object::cast_to<CollisionPolygon3D>(p_spatial) != nullptr;
7575
}
7676

@@ -82,24 +82,26 @@ int CollisionPolygon3DGizmoPlugin::get_priority() const {
8282
return -1;
8383
}
8484

85-
void CollisionPolygon3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
86-
CollisionPolygon3D *polygon = Object::cast_to<CollisionPolygon3D>(p_gizmo->get_node_3d());
85+
void CollisionPolygon3DGizmoPlugin::redraw(EditorNode3DGizmo* p_gizmo) {
86+
CollisionPolygon3D* polygon = Object::cast_to<CollisionPolygon3D>(p_gizmo->get_node_3d());
8787

8888
p_gizmo->clear();
8989

9090
const Ref<StandardMaterial3D> material =
91-
get_material(!polygon->is_disabled() ? "shape_material" : "shape_material_disabled", p_gizmo);
91+
get_material(!polygon->is_disabled() ? "shape_material" : "shape_material_disabled", p_gizmo);
9292
const Ref<StandardMaterial3D> material_arraymesh =
93-
get_material(!polygon->is_disabled() ? "shape_material_arraymesh" : "shape_material_arraymesh_disabled", p_gizmo);
93+
get_material(!polygon->is_disabled() ? "shape_material_arraymesh" : "shape_material_arraymesh_disabled", p_gizmo);
9494

9595
const Color collision_color = polygon->is_disabled() ? Color(1.0, 1.0, 1.0, 0.75) : polygon->get_debug_color();
9696

9797
Vector<Vector2> points = polygon->get_polygon();
9898
float depth = polygon->get_depth() * 0.5;
9999

100100
Vector<Vector3> lines;
101-
for (int i = 0; i < points.size(); i++) {
102-
int n = (i + 1) % points.size();
101+
const uint64_t points_size = points.size();
102+
103+
for (int i = 0; i < points_size; i++) {
104+
int n = (i + 1) % points_size;
103105
lines.push_back(Vector3(points[i].x, points[i].y, depth));
104106
lines.push_back(Vector3(points[n].x, points[n].y, depth));
105107
lines.push_back(Vector3(points[i].x, points[i].y, -depth));
@@ -119,30 +121,32 @@ void CollisionPolygon3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
119121
// Determine orientation of the 2D polygon's vertices to determine
120122
// which direction to draw outer polygons.
121123
float signed_area = 0.0f;
122-
for (int i = 0; i < points.size(); i++) {
123-
const int j = (i + 1) % points.size();
124+
for (int i = 0; i < points_size; i++) {
125+
const int j = (i + 1) % points_size;
124126
signed_area += points[i].x * points[j].y - points[j].x * points[i].y;
125127
}
126128

127129
// Generate triangles for the sides of the extruded polygon.
128-
for (int i = 0; i < points.size(); i++) {
130+
for (int i = 0; i < points_size; i++) {
129131
verts.push_back(Vector3(points[i].x, points[i].y, depth));
130132
verts.push_back(Vector3(points[i].x, points[i].y, -depth));
131133

132134
colors.push_back(collision_color);
133135
colors.push_back(collision_color);
134136
}
135137

136-
for (int i = 0; i < verts.size(); i += 2) {
137-
const int j = (i + 1) % verts.size();
138-
const int k = (i + 2) % verts.size();
139-
const int l = (i + 3) % verts.size();
138+
const uint64_t verts_size = verts.size();
139+
for (int i = 0; i < verts_size; i += 2) {
140+
const int j = (i + 1) % verts_size;
141+
const int k = (i + 2) % verts_size;
142+
const int l = (i + 3) % verts_size;
140143

141144
indices.push_back(i);
142145
if (signed_area < 0) {
143146
indices.push_back(j);
144147
indices.push_back(k);
145-
} else {
148+
}
149+
else {
146150
indices.push_back(k);
147151
indices.push_back(j);
148152
}
@@ -151,32 +155,35 @@ void CollisionPolygon3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
151155
if (signed_area < 0) {
152156
indices.push_back(l);
153157
indices.push_back(k);
154-
} else {
158+
}
159+
else {
155160
indices.push_back(k);
156161
indices.push_back(l);
157162
}
158163
}
159164

160165
Vector<Vector<Vector2>> decomp = Geometry2D::decompose_polygon_in_convex(polygon->get_polygon());
161166

167+
162168
// Generate triangles for the bottom cap of the extruded polygon.
163169
for (int i = 0; i < decomp.size(); i++) {
164170
Vector<Vector3> cap_verts_bottom;
165171
Vector<Color> cap_colours_bottom;
166172
Vector<int> cap_indices_bottom;
167173

168-
const int index_offset = verts.size();
174+
const int index_offset = verts_size;
169175

170-
const Vector<Vector2> &convex = decomp[i];
176+
const Vector<Vector2>& convex = decomp[i];
177+
const uint64_t convex_size = convex.size();
171178

172-
for (int j = 0; j < convex.size(); j++) {
179+
for (int j = 0; j < convex_size; j++) {
173180
cap_verts_bottom.push_back(Vector3(convex[j].x, convex[j].y, -depth));
174181
cap_colours_bottom.push_back(collision_color);
175182
}
176183

177-
if (convex.size() >= 3) {
178-
for (int j = 1; j < convex.size(); j++) {
179-
const int k = (j + 1) % convex.size();
184+
if (convex_size >= 3) {
185+
for (int j = 1; j < convex_size; j++) {
186+
const int k = (j + 1) % convex_size;
180187

181188
cap_indices_bottom.push_back(index_offset + 0);
182189
cap_indices_bottom.push_back(index_offset + j);
@@ -194,18 +201,19 @@ void CollisionPolygon3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
194201
Vector<Color> cap_colours_top;
195202
Vector<int> cap_indices_top;
196203

197-
const int index_offset = verts.size();
204+
const int index_offset = verts_size;
198205

199-
const Vector<Vector2> &convex = decomp[i];
206+
const Vector<Vector2>& convex = decomp[i];
207+
const uint64_t convex_size = convex.size();
200208

201-
for (int j = 0; j < convex.size(); j++) {
209+
for (int j = 0; j < convex_size; j++) {
202210
cap_verts_top.push_back(Vector3(convex[j].x, convex[j].y, depth));
203211
cap_colours_top.push_back(collision_color);
204212
}
205213

206-
if (convex.size() >= 3) {
207-
for (int j = 1; j < convex.size(); j++) {
208-
const int k = (j + 1) % convex.size();
214+
if (convex_size >= 3) {
215+
for (int j = 1; j < convex_size; j++) {
216+
const int k = (j + 1) % convex_size;
209217

210218
cap_indices_top.push_back(index_offset + k);
211219
cap_indices_top.push_back(index_offset + j);

0 commit comments

Comments
 (0)