problems making a dx9 interdace

Started by
9 comments, last by aarddave74 17 years, 6 months ago
i've been struggling with this bug for 2 days now and i'm close to giving up, i just can't see where the problem lies. i'll post the relevant functions below. class CXTexture { private: LPDIRECT3DTEXTURE9 m_Texture; LPDIRECT3DDEVICE9 m_pDevice; D3DXVECTOR2* m_RotationCenter; D3DXVECTOR2* m_Translation; . . . public: D3DXVECTOR2* GetTranslation() {return m_Translation;} void SetTranslation (D3DXVECTOR2* Translation) {m_Translation = Translation;} . . . }; in the cpp file itself -> D3DXVECTOR2* Translation; g_Texture = new CXTexture(g_pd3dDevice); g_Texture->LoadFromFile("logo.jpg"); Translation->x = 150; Translation->y = 200; g_Texture->SetTranslation(*Translation); the problem lies somewhere with the translation/d3dxvector2 pointer, returning the error -> CXTexture::SetTranslation' : cannot convert parameter 1 from 'D3DXVECTOR2' to 'D3DXVECTOR2 *' Isn't parameter 1 (*Translation) already in d3dxvector2* format, and shouldn't need converting? I must be missing something obvious, but i can't for the life of me see what it is.
Advertisement
You are declaring Translation as a pointer to a D3DXVECTOR2, and then using it without initializing it. Finally, you are dereferencing it before passing it to a method that expects a pointer.

Here's the bit of code fixed up a bit. I'd recommend you examine it, and even read some about pointers and C++ to make sure you understand what you're doing wrong:
D3DXVECTOR2* Translation;g_Texture = new CXTexture(g_pd3dDevice);g_Texture->LoadFromFile("logo.jpg");Translation.x = 150;Translation.y = 200;g_Texture->SetTranslation(&Translation);
Sirob Yes.» - status: Work-O-Rama.
Quote:Original post by sirob
Here's the bit of code fixed up a bit. I'd recommend you examine it, and even read some about pointers and C++ to make sure you understand what you're doing wrong:


D3DXVECTOR2* Translation;
g_Texture = new CXTexture(g_pd3dDevice);
g_Texture->LoadFromFile("logo.jpg");
Translation.x = 150;
Translation.y = 200;
g_Texture->SetTranslation(&Translation);



typo I'm guessing?
[size=2]aliak.net
Ok, i tried these changes, but these didn't work either.

First of all, it expects
Translation->x = 150; rather than
Translation.x = 150;


The line g_Texture->SetTranslation(&Translation);
also gives the error 'CXTexture::SetTranslation' : cannot convert parameter 1 from 'D3DXVECTOR2 **' to 'D3DXVECTOR2 *'


Having gone back to the original code which comes on the cd, and making a few minor changes, the code becomes...

g_Texture->LoadFromFile("Background.jpg");
D3DXVECTOR2 *Translation;
Translation = g_Texture->GetTranslation();

g_Texture->SetTranslation(Translation);

which works fine and shows the logo in the top left position, however when i try to set anything like
Translation->x = 100.0f or any other value, the program compiles but crashes as soon as it is run.

Not being able to set the x,y position of the 2d texture kind of nulifies the whole reason for using it to begin with.

If it helps, the initial value assignment in the constructor is
this->SetTranslation(NULL);

Quote:Original post by aarddave74
Ok, i tried these changes, but these didn't work either.

First of all, it expects
Translation->x = 150; rather than
Translation.x = 150;


That's because Translation is a pointer to an object. So if you want to access members of an object througha pointer, you have to use the -> operator. If Translation was not a pointer to an object, then you could access the object's members with the . operator.

In this canse it's a pointer to a D3DXVECTOR2 object.


Quote:
The line g_Texture->SetTranslation(&Translation);
also gives the error 'CXTexture::SetTranslation' : cannot convert parameter 1 from 'D3DXVECTOR2 **' to 'D3DXVECTOR2 *'


That's because SCTexture::SetTranslation expects a pointer to a D3DXVECTOR2. If you pass in '&Translation' then you are taking the address of a pointer, which is also called a pointer to pointer, ie: double pointer. SetTranslation is not expecting a double pointer, it just needs a pointer, which Translation alreay is, so you just pass that in.

Quote:
Having gone back to the original code which comes on the cd, and making a few minor changes, the code becomes...

g_Texture->LoadFromFile("Background.jpg");
D3DXVECTOR2 *Translation;
Translation = g_Texture->GetTranslation();

g_Texture->SetTranslation(Translation);

which works fine and shows the logo in the top left position, however when i try to set anything like
Translation->x = 100.0f or any other value, the program compiles but crashes as soon as it is run.


You can't access members of an object thorugh it's pointer unless that pointer actually points to something! If you have the following code:

D3DXVECTOR2* Translation;Translation->x = 150;Translation->y = 200;


Then what exactly is "Translation" pointing to here? yeah, nothing! so you'll get an access violation. Translation has to be pointing to a valid D3DXVECTOR2 object that exists somewhere in the program, then you can make changes to it.

[size=2]aliak.net
Thanks for the help.


I have now created a global pointer g_Translation in the same was as g_Texture and others are managed and initialised it with

D3DXVECTOR2* g_Translation = NULL;


in the directx initialisation i have

g_Translation = new D3DXVECTOR2(0,0);

this should create a structure with a valid x,y values, which can then be reassigned and used right?
I ask, because even tho the code compiles and the program runs without crashing, i now don't get the image displayed anywhere in the window ;)
Quote:this should create a structure with a valid x,y values, which can then be reassigned and used right?


Yeah, that should work.

Now the problem appears to be somewhere else. what does your SetTranslation funciton do? Can we see the internals? And where in your code are you changing the values of Translation?
[size=2]aliak.net
Quote:Original post by IFooBar
Quote:Original post by sirob
Here's the bit of code fixed up a bit. I'd recommend you examine it, and even read some about pointers and C++ to make sure you understand what you're doing wrong:


D3DXVECTOR2* Translation;
g_Texture = new CXTexture(g_pd3dDevice);
g_Texture->LoadFromFile("logo.jpg");
Translation.x = 150;
Translation.y = 200;
g_Texture->SetTranslation(&Translation);



typo I'm guessing?


Actually, the typo was in the first line, and just made the whole thing a mess. Calling new for something like this is silly, you should just init a temporary variable on the stack.
Sirob Yes.» - status: Work-O-Rama.
Quote:Original post by IFooBar
Quote:this should create a structure with a valid x,y values, which can then be reassigned and used right?


Yeah, that should work.

Now the problem appears to be somewhere else. what does your SetTranslation funciton do? Can we see the internals? And where in your code are you changing the values of Translation?



i don't actually have the full code infront of me right now, being at work. i dd try and email it to my work address before i left his morning but its got lost somewhere. anyway, the settranslation function is

void SetTranslation (D3DXVECTOR2* Translation) {m_Translation = Translation;}

with m_Translation being a member variable pointer of the CXTexture class


the last stage in the render is using a CXPen object to draw the texture with a call to D3DXMatrixTransformation2D


The code in that, if my memory serves me right is

D3DXMatrixTransformation2D(
&mat,
0,
NULL,
g_Texture->GetScaling(),
g_Texture->GetRotationCenter(),
g_Texture->GetRotation(),
g_Texture->GetTranslation()
);

Also, i am assigning new values to the Translation right after loading the texture from the file.
If i don't update it with a call to settranslation, the image displays as it should, but at the top left (o,0). if i change to any other values (tried a range from 0.5f to 300) it just displays the cleared background without the image.
Quote:Original post by sirob


Actually, the typo was in the first line, and just made the whole thing a mess. Calling new for something like this is silly, you should just init a temporary variable on the stack.


I know its silly, but i'm just really learning directx and trying to get a halfdecent beginning graphics engine/library which i can then use to further my learning. The problem is, i have now bought 4 different books dealing purely with directx, and none have worked by following the examples in the books themselves.
In the book i'm currently working from (dx9 interfaces design & implementation) the code on the cd works, but is very different from the examples given in the book, and trying to piece the two together to achieve the result i want is becoming very problematic.

In this instance, the code on the cd works, but as stated it does not set the transformation for the newly loaded texture, and displays the image correctly in the top left.

Following the book example, updating the settransformation() with new values in the place in the code which is shown in the book results in the image not being displayed at all.

This topic is closed to new replies.

Advertisement