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

View file

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

View file

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