Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Set class (member) pointer to a existing class (to allow editing)


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 AussieSpoon   Members   -  Reputation: 184

Like
0Likes
Like

Posted 27 January 2013 - 09:16 AM

Hello,

 

I have two classes. I am trying to get the 2nd class to have a member pointer that points to an instance of the first class (Giving the 2nd class the ability to edit an instance of the first class) . But I am unsure how to set this up (when I use it the values I try to edit don't change) 

 

Here is what I have tried:

class A
{
 m_MemberA;
};

class B
{
 void SetMemberPointer(*A _a)  {m_MemberB = _a;}
 A* m_MemberB
};

void main()
{
 A Class1;
 B Class2;

 Class2.SetMemberPointer(&Class1);

}

 

Can some one explain why this doesn't work/help with a solution?

 

Thanks 


Edited by AussieSpoon, 27 January 2013 - 09:18 AM.


Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9626

Like
0Likes
Like

Posted 27 January 2013 - 09:24 AM

when I use it the values I try to edit don't change

Post the actual code you are using. What you have shouldn't even compile, which means it's very hard to determine why the runtime behavior you are describing would occur.

#3 AussieSpoon   Members   -  Reputation: 184

Like
0Likes
Like

Posted 27 January 2013 - 09:33 AM

Class 1 (.h):

 

struct DrawArea
{
	float  left, right, top, bottom; 
};

class Texture
{
public:
	Texture(void);
	Texture(IDirect3DTexture9 &_Texture);
	Texture(IDirect3DTexture9 &_Texture, float  _startX, float  _startY, float  _endX, float  _endY);
	~Texture(void);

	IDirect3DTexture9* GetDXTexture() {return m_Texture;}
	DrawArea GetDrawArea()			  {return m_DrawArea;}
	//u = x, v = y co-ordinate
	float GetTextureUMin();	//the x start (left)
	float GetTextureUMax();	//the x end (right)
	float GetTextureVMin();	//the y start (top)
	float GetTextureVMax();	//the y end (bottom)

	uINT GetWidth();		//will return the width of the texture at level 0
	uINT GetWidth(uINT _level);
	uINT GetHeight();		//will return the width of the texture at level 0
	uINT GetHeight(uINT _level);

	void SetToTexture(Texture &_texture);
	void SetToTexture(Texture *_texture);
	void SetDXTexture(IDirect3DTexture9 &_Texture);
	void SetDXTexture(IDirect3DTexture9 *_Texture);
	void SetDXTexture(IDirect3DTexture9 &_Texture, float  _startX, float  _startY, float  _endX, float  _endY);
	void SetDXTexture(IDirect3DTexture9 *_Texture, float  _startX, float  _startY, float  _endX, float  _endY);
	void SetDrawArea(float  _startX, float  _startY, float  _endX, float  _endY);


private:

	IDirect3DTexture9* m_Texture;//The DirectX texture
	DrawArea m_DrawArea;
};

 

 

Class 2 (.h):

 

class Animation
{
public:
	Animation(void);
	Animation(Texture* _Texture, float StartX, float StartY, short Rows, short Columns, float FrameWidth, float FrameHeight, short Speed);
	~Animation(void);

	void Update();

private:

	Texture* m_Texture;//????!?!!?
	float m_StartX;
	float m_StartY;
	short m_Rows;
	short m_Columns;
	short m_CurrentFrame;
	short m_CurrentFrameX;
	short m_CurrentFrameY;
	float m_FrameWidth;
	float m_FrameHeight;
	short m_Time;
	
	//short m_Counter (Might be better to make this global (To only have instance of it instead of 1 per animation)
};

 

Class 2 (.cpp):

 

Animation::Animation(Texture* _Texture, float StartX, float StartY, short Rows, short Columns, float FrameWidth, float FrameHeight, short Speed)
{
	m_Texture = _Texture;

	m_StartX = StartX;
	m_StartY = StartY;
	m_Rows = Rows;
	m_Columns = Columns;
	m_FrameWidth = FrameWidth;
	m_FrameHeight = FrameHeight;
	m_Time = Speed;

	m_CurrentFrame = 0, m_CurrentFrameX = 0, m_CurrentFrameY = 0;
}

 

Creating/using the variables:

 

Animation *TestAnimation;
............

TestAnimation = new Animation(&TestSprite1->GetTexture(), 0, 0, 4, 1, 64, 64, 1);
TestSprite1->SetAnimation(TestAnimation);


#4 SiCrane   Moderators   -  Reputation: 9626

Like
2Likes
Like

Posted 27 January 2013 - 10:04 AM

1) What is TestSprite1? Is it a Texture? It doesn't look like it. In particular what does the GetTexture() function look like?
2) You never actually try to make any changes so of course you don't see any effects.
3) It's easier to do problem solving in these kinds of cases if you can break down the problem to a minimal, but complete, compilable example. To be minimal it shouldn't include anything that isn't relevant to the problem (ex: member functions that are never called) but to be complete it should still show all the bits that show the core of what you are attempting to do.

#5 AussieSpoon   Members   -  Reputation: 184

Like
0Likes
Like

Posted 27 January 2013 - 10:23 AM

In particular what does the GetTexture() function look like?

 

Oh man, this was my problem. I was returning the texture as an object, but I have a different function for returning it as a pointer. Since the animation take a pointer I needed to return it as a pointer.

 

Thanks heaps for pointing that out, you are a legend.  It works fine now. 



#6 Ectara   Crossbones+   -  Reputation: 3015

Like
0Likes
Like

Posted 30 January 2013 - 04:07 PM

//short m_Counter (Might be better to make this global (To only have instance of it instead of 1 per animation)


Unrelated, but in regards to this, I'd suggest making it a static variable in the class, rather than having it clutter the global namespace, or be visible to classes that shouldn't see it.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS