Sign in to follow this  
ole_92

Change image file that LPDIRECT3DTEXTURE9 texture is using

Recommended Posts

Hi people!
I'm trying to change the images that LPDIRECT3DTEXTURE9 is pointing to, in a for loop, so I can render them all.

Here's some code:
[code]void InitializeObject(string file) {

D3DXCreateTextureFromFile(D3D_Device, file.c_str(), &Texture);
D3DXCreateSprite(D3D_Device, &g_pSprite);

}[/code]


And in my RenderScene() method I'm trying to do the following:
[code]void RenderScene() {

// Clear the screen to black.
D3D_Device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);

// Tell Direct 3D we are now going to start drawing.
D3D_Device->BeginScene();

// SetTexture will add the image in the Texture object to all
// things draw after this. To Turn if off send SetTexture(0, NULL);
D3D_Device->SetTexture(0, Texture);
RECT Rect;
Rect.top = 15;
Rect.left = 15;
Rect.bottom = 115;
Rect.right = 115;
D3DXVECTOR3 vCenter (0, 0, 0);
D3DXVECTOR3 vPosition(90, 10, 0);

string files[8] = {
"Square_1.png", "Square_2.png", "Square_3.png", "Square_4.png",
"Square_5.png", "Square_6.png", "Square_7.png", "BlankSquare.png"
};

g_pSprite->Begin(D3DXSPRITE_ALPHABLEND);

for (int k = 0; k < 6; k++) {

for (int i = 0; i < 7; i++) {

if (k == 0)
InitializeObject(files[i]);

g_pSprite->Draw(Texture, &Rect, &vCenter, &vPosition, D3DCOLOR_COLORVALUE(1.0f,1.0f,1.0f,1.0f));
vPosition.x += 120;

}

if (k == 0)
InitializeObject(files[7]);

// reset for the next row
vPosition.x = 90;
vPosition.y += 120;
}

g_pSprite->End();

// We are done drawing for this scene.
D3D_Device->EndScene();

// Swap the old frame with the new one.
D3D_Device->Present(NULL, NULL, NULL, NULL);
}[/code]


It works with multiple images that are the same, but I can't change them.
Can someone help please, I can't find the solution in documentation...
Thank you!

Share this post


Link to post
Share on other sites
Oh, and by the way, in my .txt file the InitialiseDirect3D method's head should look like this:

[code]void InitializeDirect3D(HWND hwnd, bool fullscreen)[/code]


was just a typo.



The error says: D3DX: ID3DXSprite::Draw called outside a Begin/End pair,
but my for loop is clearly between g_pSprite->Begin() and g_pSprite->End()

Share this post


Link to post
Share on other sites
Fixed it! The call to InitializeObject() HAD to be outside Begin-End thing...
This is the correct syntax:


[code]for (int k = 0; k < 6; k++) {

for (int i = 0; i < 7; i++) {

if (k == 0)
InitializeObject(files[i]);

g_pSprite->Begin(D3DXSPRITE_ALPHABLEND);

g_pSprite->Draw(Texture, &Rect, &vCenter, &vPosition, D3DCOLOR_COLORVALUE(1.0f,1.0f,1.0f,1.0f));
vPosition.x += 120;

g_pSprite->End();
}

if (k == 0)
InitializeObject(files[7]);

// reset for the next row
vPosition.x = 90;
vPosition.y += 120;
}[/code]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this