Removed capsize on vertices. Fixed memory leak

This commit is contained in:
Robert 2020-09-22 22:42:24 +02:00
parent b2d23a0992
commit a05558794d
3 changed files with 28 additions and 20 deletions

View file

@ -9,42 +9,50 @@
BezierCurve::BezierCurve() BezierCurve::BezierCurve()
{ {
vertices.fill(nullptr);
lines.fill(nullptr);
} }
void BezierCurve::AddVertex(int x, int y) void BezierCurve::AddVertex(int x, int y)
{ {
vertices[numVertices++] = new Vertex(x, y); numVertices++;
if(numVertices != vertices.size())
vertices.push_back(new Vertex(x, y));
else
{
vertices.insert(vertices.end() - 1, new Vertex(x, y));
}
if (numVertices > 1) if (numVertices > 1)
lines[numLines++] = new Line(vertices[numVertices - 2], vertices[numVertices - 1]); lines.push_back(new Line(vertices[numVertices - 2], vertices[numVertices - 1]));
} }
void BezierCurve::Confirm() void BezierCurve::Confirm()
{ {
if (numLines > 1) if (vertices.size() != numVertices)
{ {
lines[numLines] = new Line(vertices[numLines], vertices[0]); delete vertices.back();
numLines++; vertices.pop_back();
}
if (lines.size() > 1)
{
lines.push_back(new Line(vertices.back(), vertices[0]));
} }
inProgress = false; inProgress = false;
ConstructBezier(0.05); ConstructBezier(0.01);
} }
void BezierCurve::SetPreviewVertex(int x, int y) void BezierCurve::SetPreviewVertex(int x, int y)
{ {
if (vertices[numVertices] == nullptr) if (numVertices == vertices.size())
vertices[numVertices] = new Vertex(x, y, Vertex::Cursor); vertices.push_back(new Vertex(x, y, Vertex::Cursor));
else else
vertices[numVertices]->SetPos(x, y); vertices[numVertices]->SetPos(x, y);
} }
void BezierCurve::ConstructBezier(float resolution) void BezierCurve::ConstructBezier(float resolution)
{ {
int grade = numVertices - 1; int grade = vertices.size() - 1;
if (vertices[grade] != nullptr && inProgress)
grade++;
if (grade < 1) if (grade < 1)
return; return;
@ -78,8 +86,8 @@ void BezierCurve::Draw(SDL_Renderer* renderer)
for (int i = 0; i < numVertices; i++) for (int i = 0; i < numVertices; i++)
vertices[i]->Draw(renderer); vertices[i]->Draw(renderer);
for (int i = 0; i < numLines; i++) for (Line* line : lines)
lines[i]->Draw(renderer); line->Draw(renderer);
if (bezier.size() != 0) if (bezier.size() != 0)
{ {

View file

@ -1,6 +1,5 @@
#pragma once #pragma once
#include <array>
#include <vector> #include <vector>
#include "UI/Vertex.hpp" #include "UI/Vertex.hpp"
@ -29,9 +28,8 @@ public:
private: private:
uint8_t numVertices = 0; uint8_t numVertices = 0;
uint8_t numLines = 0; std::vector<Vertex*> vertices;
std::array<Vertex*, 4> vertices; std::vector<Line*> lines;
std::array<Line*, 4> lines;
std::vector<SDL_FPoint> bezier; std::vector<SDL_FPoint> bezier;

View file

@ -64,7 +64,7 @@ int main(int argc, char** argv)
if (currentCurve != nullptr) if (currentCurve != nullptr)
{ {
currentCurve->SetPreviewVertex(mouseX, mouseY); currentCurve->SetPreviewVertex(mouseX, mouseY);
currentCurve->ConstructBezier(0.05); currentCurve->ConstructBezier(0.01);
} }
break; break;
@ -117,10 +117,12 @@ void AddVertex(int x, int y, BezierCurve** curve)
(*curve)->AddVertex(x, y); (*curve)->AddVertex(x, y);
/*
if ((*curve)->GetVertexCount() == 4) if ((*curve)->GetVertexCount() == 4)
{ {
(*curve)->Confirm(); (*curve)->Confirm();
curves.push_back(*curve); curves.push_back(*curve);
*curve = nullptr; *curve = nullptr;
} }
*/
} }