Jump to content
  • Advertisement
Sign in to follow this  
Slyfox

Passing Arrays

This topic is 3804 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Slyfox
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.
Why not? What's the error you get? Does it compile but fail to run? What do you chaneg the code to?

Also, your particles::init function isn't safe. If new fails, it throws an exception, not return 0. And if it does return 0, you still call LoadXFile on it.

Share this post


Link to post
Share on other sites
When I move the creation of the array stParticles data[] into the init function
I get the error "'data' : undeclared identifier" from the line stParticles * temp = &data[0]; which is found in the positionParticles function. This is obviously because the array has gone out of scope once init has finished but i'm not sure how i can pass it from init to positionParticles without it going out of scope.


bool particles::init ()
{
stParticles data = {{D3DXVECTOR3(0.0f, 10.0f, 0.0f)}};
}

void particles::positionParticles()
{
stParticles * temp = &data[0]; // throws up the error
}



Thanks for pointing out that init is unsafe, I'll look into sorting that out. Cheers!

Share this post


Link to post
Share on other sites
Data is a local variable, it only exists in the init function. If you want to use it elsewhere, you'll need to make it a member variable.

Also, I'd strongly advise getting a good grip with C++ before doing DirectX stuff or you'll just keep running into issues like this. It's assumed that you have a reasonable understanding of C++ before using D3D, and this is fairly basic stuff.

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
If new fails, it throws an exception, not return 0. And if it does return 0, you still call LoadXFile on it.


I'm usually not one to nitpick, but this depends on what version of "new" you're using. I know in older versions of VC++ (<= 7.1) the CRT version of new just returned 0 on a failed allocation, and who knows what other compilers do. VC++ 8.0 and 9.0 still support a non-throwing version of new, but I think you need to explicitly link to it.


Share this post


Link to post
Share on other sites
Thanks again for your help i totally understand that data is a local variable that goes out of scope at the end of init and I tried to make that clear in my post. The problem is I don't know how I can keep data[] safe and stop it going out of scope. I would be quite happy leaving it as a global because I know that by using a pointer I can pass data to positionParticles. However because I want to assign random values to the vectors contained i cannot achieve this because typing the code

stParticles data[] = {{D3DXVECTOR3(generateVector(0,2))}};

in the globals throws up errors as it obviously cannot access the function generateVector. Is there another way of going about this? This is why I have tried to move the creation of the array into init so that it access the function generateVector.

Share this post


Link to post
Share on other sites
Quote:
Original post by Slyfox
Thanks again for your help i totally understand that data is a local variable that goes out of scope at the end of init and I tried to make that clear in my post. The problem is I don't know how I can keep data[] safe and stop it going out of scope. I would be quite happy leaving it as a global because I know that by using a pointer I can pass data to positionParticles. However because I want to assign random values to the vectors contained i cannot achieve this because typing the code

stParticles data[] = {{D3DXVECTOR3(generateVector(0,2))}};

in the globals throws up errors as it obviously cannot access the function generateVector. Is there another way of going about this? This is why I have tried to move the creation of the array into init so that it access the function generateVector.
Just make data a member variable.

EDIT:
Quote:
Original post by MJP
I'm usually not one to nitpick, but this depends on what version of "new" you're using. I know in older versions of VC++ (<= 7.1) the CRT version of new just returned 0 on a failed allocation, and who knows what other compilers do. VC++ 8.0 and 9.0 still support a non-throwing version of new, but I think you need to explicitly link to it.
Ay yes, true. I assumed the OP was using VC8 or VC9. I wasn't aware of the nothrow version of new actually...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!