Hi I've been having problems trying to pass an array to a function. I've tried researching it but have still been unsuccessfull.
I'll post my code first and then try and explain it.
particles.h
#include "GameMain.h"
#include "randomNumber.h"
extern GameMain *game;
#pragma once
class Model;
class randomNumber;
struct stParticles
{
D3DXVECTOR3 particlePosition;
D3DXVECTOR3 particleVelocity;
};
class particles
{
public:
particles (void);
~particles(void);
bool init(LPDIRECT3DDEVICE9 particleDevice);
void positionParticles(D3DXVECTOR3 position);
D3DXVECTOR3 generateVector(float lowest, float highest);
private:
Model* particlesModel;
randomNumber* numberGenerator;
};
particles.cpp
#include "particles.h"
#include "dxMgr.h"
#include "XMesh.h"
float particlesStartTime = 0;
//matrices
D3DXMATRIX particlesWorldMatrix;
D3DXMATRIX particlesRotationMatrix; // the rotation matrix
D3DXMATRIX particlesScaleMatrix; // the scale matrix
stParticles data[] = {{D3DXVECTOR3(0.0f, 2.0f, 0.0f)}};
//stParticles particles[];
/*stParticles data[] = {{D3DXVECTOR3(generateVector(0,2))},
{D3DXVECTOR3 (generateVector(0,2))},
{D3DXVECTOR3 (generateVector(0,2))},
{D3DXVECTOR3 (generateVector(0,2))},
{D3DXVECTOR3 (generateVector(0,2))},
{D3DXVECTOR3 (generateVector(0,2))},
{D3DXVECTOR3 (generateVector(0,2))},
{D3DXVECTOR3 (generateVector(0,2))},
{D3DXVECTOR3 (generateVector(0,2))},
{D3DXVECTOR3 (0.0f,0.0f,0.0f)}};*/
particles::particles(void)
{
//empty
}
particles::~particles(void)
{
//empty
}
bool particles::init(LPDIRECT3DDEVICE9 particleDevice)
{
numberGenerator = new randomNumber();
particlesModel = new Model (particleDevice);
particlesModel->LoadXFile("snowball.x");
if(!particlesModel){return false;}
}
void particles::positionParticles(D3DXVECTOR3 position)
{
stParticles * temp = &data[0];
// Get time for time-based updates.
float particlesTime = (float)timeGetTime();
particlesTime = (particlesTime - particlesStartTime) * 0.001f;
particlesStartTime = (float)timeGetTime();
// draw 3 particles
while(temp->particlePosition != D3DXVECTOR3(0.0f, 0.0f, 0.0f))
{
// Set model scale, position and rotation in the world
D3DXMatrixScaling(&particlesScaleMatrix, 100.0f, 100.0f, 100.0f);
D3DXMatrixTranslation(&particlesWorldMatrix, temp->particlePosition.x, temp->particlePosition.y, temp->particlePosition.z);
D3DXMatrixRotationYawPitchRoll(&particlesRotationMatrix, 0.0f, 0.0f, 0.0f);
D3DXMatrixMultiply(&particlesWorldMatrix, &particlesScaleMatrix, &particlesWorldMatrix);
D3DXMatrixMultiply(&particlesWorldMatrix, &particlesRotationMatrix, &particlesWorldMatrix);
// Update and then render the model
particlesModel->Update(particlesTime, &particlesWorldMatrix);
particlesModel->Render();
temp++;
}
}
D3DXVECTOR3 particles::generateVector(float lowest, float highest)
{
D3DXVECTOR3 tempVector;
tempVector.x = numberGenerator->generateNumber(lowest, highest);
tempVector.y = numberGenerator->generateNumber(lowest, highest);
tempVector.z = numberGenerator->generateNumber(lowest, highest);
return tempVector;
}
Basically I've set up a class in my engine called particles the idea being that once it is finished I can use it to create particle effects. As the code stands it does compile and work. However when I try to move the creation of the array into the init function i can't get it to pass into the positionParticles function. The reason I want to do this is because I want to feed the array with random numbers and I can't do that leaving it as a global.
essentially what I'm looking for is
bool particles::init()
{
stParticles data = {{D3DXVECTOR3 (generateVector(0.0f,2.0f))}};
}
void particles::positionParticles()
{
// position models using data[]
}
positionParticles is called in my game update function and so is called each frame so I can't just create the array in that function. Any help would be appreciated, sorry if I have explained this poorly but I will provide any additional information if it is needed. Cheers!