I'm trying to dynamically allocate a float array (distances
) but stepping through the debugger shows that it only allocates just one for some reason.
I have already tried out std::vector and it works ok but it then fails at Vec2 * points = Util::BCurvePerpPoint(i, p1, p2, p3, this->distances, this->m_NumGeoms * 2)
. The thing is that distances will not change size for the life of the object so it's kind of overkill to use std::vector. The only thing I'm using std::vector in is the Draw
function (which I'm thinking I can fix that up to be static in size or something but if step
changes, then the size of the array would change so... yeah...).
Note: Util::BCurvePerpPoint returns an array of Vec2 where the first one is the Bézier curve point at time i
. All it does is provide points at a perpendicular line of the tangent of the Bézier curve. Function prototype:
BCurvePerpPoint(float time, Vec2 p1, Vec2 p2, Vec2 p3, float * distance, const int numDists);
Header:
#ifndef _H_HOLDTAIL_
#define _H_HOLDTAIL_
#pragma once
#include "../../OD_Draw2d.h"
namespace Game {
struct HoldTailGeom {
float d1,d2;
ColorF color1, color2;
uint32 packedCol1, packedCol2;
HoldTailGeom() :
d1(0.0f),
d2(0.0f),
color1(0.0f, 0.0f, 0.0f, 0.0f),
color2(0.0f, 0.0f, 0.0f, 0.0f),
packedCol1(0),
packedCol2(0)
{};
};
class HoldTail {
public:
HoldTail(HoldTailGeom * geoms, const unsigned int numGeoms);
~HoldTail();
void Draw(Vec2 p1, Vec2 p2, Vec2 p3, float tStart, float tEnd, float step = 0.05f, bool isolatedDraw = false);
void Draw(Vec2 p1, Vec2 p2, Vec2 p3, float tStart, float tEnd, int numPoints, bool isolatedDraw = false);
private:
HoldTailGeom * m_Geoms;
unsigned int m_NumGeoms;
float * distances;
//std::vector<float> distances;
std::shared_ptr<OD_Draw2d> OD_Draw2dPtr;
std::vector<SVF_P3F_C4B_T2F> *line;
};
}
#endif //_H_HOLDTAIL_
Code:
#include <StdAfx.h>
#include "HoldTail.h"
namespace Game {
HoldTail::HoldTail(HoldTailGeom * geoms, const unsigned int numGeoms) : m_Geoms(geoms), m_NumGeoms(numGeoms) {
this->line = new std::vector<SVF_P3F_C4B_T2F>[numGeoms];
this->distances = new float[numGeoms * 2]();
//distances = new std::vector<float>();
for (int i = 0; i < numGeoms; i++) { //for each geometry
//convert the colors to uint32 for quicker assignment to vector data.
this->m_Geoms[i].packedCol1 = this->m_Geoms[i].color1.pack_argb8888();
this->m_Geoms[i].packedCol2 = this->m_Geoms[i].color2.pack_argb8888();
//convert the distances to an array that we can use.
//int i1 = i * 2;
//int i2 = (i * 2) + 1;
this->distances[i * 2] = m_Geoms[i].d1;
this->distances[(i * 2) + 1] = m_Geoms[i].d2;
//this->distances.push_back(m_Geoms[i].d1);
//this->distances.push_back(m_Geoms[i].d2);
}
//this->distances.shrink_to_fit();
}
HoldTail::~HoldTail() {
for (int i = 0; i < this->m_NumGeoms; i++) this->line[i].clear(); //clear the data just to be sure.
delete[] this->line;
delete[] this->distances;
//this->distances.clear();
}
void HoldTail::Draw(Vec2 p1, Vec2 p2, Vec2 p3, float tStart, float tEnd, int numPoints, bool isolatedDraw) {
if (tStart >= tEnd) return;
this->Draw(p1, p2, p3, tStart, tEnd, (float)((float)(tEnd - tStart) / (float)(numPoints)), isolatedDraw);
}
void HoldTail::Draw(Vec2 p1, Vec2 p2, Vec2 p3, float tStart, float tEnd, float step, bool isolatedDraw) {
if (tStart >= tEnd) return;
for (float i = tStart; i < tEnd+step; i += step) { //from start time to end time with a step between each
Vec2 * points = Util::BCurvePerpPoint(i, p1, p2, p3, this->distances, this->m_NumGeoms * 2); //calculate the distances at time i.
for (int i2 = 0; i2 < this->m_NumGeoms; i2++) { //for each geometry
SVF_P3F_C4B_T2F tmp;
//push back the vectors
tmp.xyz = Vec3(points[(i2 * 2)+1].x, points[(i2 * 2)+1].y, 1);
tmp.color.dcolor = this->m_Geoms[i2].packedCol1;
tmp.st = Vec2(0, 0);
this->line[i2].push_back(tmp);
tmp.xyz = Vec3(points[(i2 * 2)+2].x, points[(i2 * 2)+2].y, 1);
tmp.color.dcolor = this->m_Geoms[i2].packedCol2;
this->line[i2].push_back(tmp);
}
}
if (isolatedDraw) this->OD_Draw2dPtr->BeginDraw2d(1280, 720);
for (int i = 0; i < this->m_NumGeoms; i++) { //for each geometry
this->OD_Draw2dPtr->DrawTriangleStrip(&this->line[i][0], this->line[i].size()); //draw the line
this->line[i].clear(); //done using the line, clear it for next pass.
}
if (isolatedDraw) this->OD_Draw2dPtr->EndDraw2d();
}
}