Sign in to follow this  
nlraley

How come Sprite-Draw keeps sending WINAPI MsgProc DRAW?

Recommended Posts

nlraley    126
I am following tutorials that were provided with the DirectX SDK and their example had me set up to process WINAPI MsgProc DRAW message to call my render.

In my Render I call Device->BeginScene and then Call Sprite->Begin, but this triggers another WINAPI MsgProc to Draw which kicks me out of my current iteration through Render and back into it again, where Begin Scene fails and my Sprite->Draw never is reached. What can I do to avoid this?

Here is what I have:
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
d3d.Cleanup();
PostQuitMessage( 0 );
return 0;

case WM_PAINT:
d3d.Render();
ValidateRect( hWnd, NULL );
return 0;
}

return DefWindowProc( hWnd, msg, wParam, lParam );
}


Then render looks like this:
void Direct3D::Render()
{
//-------------------
D3DCOLOR vertexColour = 0xFFFFFFFF;
//-------------------
if ( NULL == d3d.d3d9Device)
return;
//-------------------
d3d.d3d9Device->Clear(
0, // Number of Rectangles in Array - Set to 0 since Rects is NULL
NULL, // Pointer to an Array of D3DRect to Clear - NULL indicates entire viewport
D3DCLEAR_TARGET, // Surface to Clear
D3DCOLOR_XRGB( 0, 0, 0 ), // Clear to this Color
1.0f, // Depth Buffer to Clear to
0 // Stencil Buffer to Clear to
);
//-------------------
if ( SUCCEEDED( d3d.d3d9Device->BeginScene() ) )
{
//-------------------
// Scene Rendering takes place Here
//-------------------
// Draw the triangles in the vertex buffer. This is broken into a few
// steps. We are passing the vertices down a "stream", so first we need
// to specify the source of that stream, which is our vertex buffer. Then
// we need to let D3D know what vertex shader to use. Full, custom vertex
// shaders are an advanced topic, but in most cases the vertex shader is
// just the FVF, so that D3D knows what type of vertices we are dealing
// with. Finally, we call DrawPrimitive() which does the actual rendering
// of our geometry (in this case, just one triangle).
//-------------------

d3d.d3d9Sprite->Begin(NULL);
d3d.d3d9Sprite->Draw(
d3d.d3dTexture,
NULL,
new D3DXVECTOR3(128.0f, 128.0f, 0.5f),
NULL,
vertexColour
);
d3d.d3d9Sprite->End();

/*
d3d.d3d9Device->SetStreamSource( 0, d3d.d3d9VertexBuffer, 0, sizeof( Vertex ) );
d3d.d3d9Device->SetTexture(0, d3d.d3dTexture);
d3d.d3d9Device->SetFVF( D3DFVF_TLVERTEX );
d3d.d3d9Device->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 ); */
//------------------- End the Scene
d3d.d3d9Device->EndScene();
}
//------------------- Present the Backbuffer contents to the display
d3d.d3d9Device->Present( NULL, NULL, NULL, NULL);
//-------------------
}
//-------------------


But as I said, I can never complete the iteration through Render due to it firing a new Draw Event. What am I doing wrong here?

Share this post


Link to post
Share on other sites
Imgelling    222
I think d3d.d3d9Sprite->Begin(NULL) sets the sprite drawing mode into immediate mode causing the window to need to be redrawn thus throwing the DRAW message.

try
->Begin(D3DXSPRITE_ALPHABLEND);

If that fixes it, then that was the problem.

Share this post


Link to post
Share on other sites
nlraley    126
I added a bunch of bool flags to skip over the parts when it recalled the render and ended up making it through all the steps; however, all I have is a plain black screen, the texture never loaded. Any ideas?

I set up the vertex and texture as follows:

//-------------------
VOID* pVertices;
RECT tRect;
LPCSTR file;
D3DSURFACE_DESC surfaceDesc;
//-------------------
D3DXIMAGE_INFO SrcInfo;
//-------------------
D3DCOLOR colorkey = 0xFFFF00FF;
//-------------------
file = "G:\\MGMapsCache\\GoogleMap\\18\\108341\\61585.png";
//-------------------
if (FAILED(D3DXCreateTextureFromFileEx (
d3d.d3d9Device, // Device
file, // File Name/Location
0, // Width - 0 Indicates use Dimensions from File to a power of 2
0, // Height - 0 Indicates use Dimensions from File to a power of 2
0, // Mip Levels
0, // Render Targer
D3DFMT_A8R8G8B8, // D3D Format
D3DPOOL_MANAGED, // D3D Pool
D3DX_FILTER_NONE, // Image Filter
D3DX_DEFAULT, // Mip Filter
colorkey, // Value to Replace Transparent Black
&SrcInfo, // Pointer to Image Information Structure to be Filled with Image Information
NULL, // Pallette Entry
&d3d.d3dTexture // Pointer to D3D9TEXTURE to hold the Texture
)))
return E_FAIL;
//-------------------
d3d.d3dTexture->GetLevelDesc(0, &surfaceDesc);
//-------------------
d3d.vertices[3];
//-------------------
d3d.vertices[0].x = 0.0f;
d3d.vertices[0].y = 0.0f;
d3d.vertices[0].z = 0.5f;
d3d.vertices[0].tu = 0.0f;
d3d.vertices[0].ty = 0.0f;
//-------------------
d3d.vertices[1].x = 256.0f;
d3d.vertices[1].y = 0.0f;
d3d.vertices[1].z = 0.5f;
d3d.vertices[1].tu = 1.0f;
d3d.vertices[1].ty = 0.0f;
//-------------------
d3d.vertices[2].x = 0.0f;
d3d.vertices[2].y = 256.0f;
d3d.vertices[2].z = 0.5f;
d3d.vertices[2].tu = 0.0f;
d3d.vertices[2].ty = 1.0f;
//-------------------
d3d.vertices[3].x = 256.0f;
d3d.vertices[3].y = 256.0f;
d3d.vertices[3].z = 0.5f;
d3d.vertices[3].tu = 1.0f;
d3d.vertices[3].ty = 1.0f;
//-------------------
//-------------------
if( FAILED( d3d.d3d9Device->CreateVertexBuffer(
4 * sizeof( Vertex ), // Length
0, // Usage -
D3DFVF_TLVERTEX, // Specifies Vertex Format
D3DPOOL_DEFAULT, // D3DPool
&d3d.d3d9VertexBuffer, // Pointer to the Direct3D Vertex Buffer
NULL // HANDLE
) ) )
{
//-------------------
return E_FAIL;
//-------------------
}
//-------------------
if( FAILED( d3d.d3d9VertexBuffer->Lock( 0, sizeof( d3d.vertices[3] ), ( void** )&pVertices, 0 ) ) )
return E_FAIL;
memcpy( pVertices, d3d.vertices, sizeof( d3d.vertices[4] ) );
d3d.d3d9VertexBuffer->Unlock();
//-------------------
return S_OK;


Then I go through my Render function as defined here:
void Direct3D::Render()
{
//-------------------
D3DCOLOR vertexColour = 0xFFFFFFFF;
//-------------------
if ( NULL == d3d.d3d9Device)
return;
//-------------------
d3d.d3d9Device->Clear(
0, // Number of Rectangles in Array - Set to 0 since Rects is NULL
NULL, // Pointer to an Array of D3DRect to Clear - NULL indicates entire viewport
D3DCLEAR_TARGET, // Surface to Clear
D3DCOLOR_XRGB( 0, 0, 0 ), // Clear to this Color
1.0f, // Depth Buffer to Clear to
0 // Stencil Buffer to Clear to
);
if (!sceneBegin && !spriteBegin && !spriteDraw)
{
sceneBegin = true;
if (FAILED(d3d.hr = d3d.d3d9Device->BeginScene() ) )
return;
}
if (!spriteBegin)
{
spriteBegin = true;
if ( FAILED(d3d.hr = d3d.d3d9Sprite->Begin(NULL) ) )
return;
}
if ( !spriteDraw)
{
spriteDraw = true;
if ( FAILED(d3d.hr = d3d.d3d9Sprite->Draw(
d3d.d3dTexture,
NULL,
NULL, //new D3DXVECTOR3(128.0f, 128.0f, 0.5f),
NULL,
vertexColour
) ) )
return;
}
if ( sceneBegin || spriteBegin || spriteDraw )
{
if (!spriteEnd)
{ spriteEnd = true; d3d.d3d9Sprite->End(); }
if ( !sceneEnd)
{ sceneEnd = true; d3d.d3d9Device->EndScene(); }
if ( spriteEnd && sceneEnd )
{ sceneBegin = sceneEnd = spriteBegin = spriteEnd = spriteDraw = true; d3d.d3d9Device->Present(NULL, NULL, NULL, NULL); }
}
}


What have I done wrong?

Share this post


Link to post
Share on other sites
Imgelling    222
Quote:
Original post by Imgelling
I think d3d.d3d9Sprite->Begin(NULL) sets the sprite drawing mode into immediate mode causing the window to need to be redrawn thus throwing the DRAW message.

try
->Begin(D3DXSPRITE_ALPHABLEND);

If that fixes it, then that was the problem.


The more I think about it, that might not be it. As your new code added error checking, try adding breakpoints on each of your returns in the FAILED() cases. That might also help narrow it down.

Share this post


Link to post
Share on other sites
nlraley    126
I finally found out what it was. I had code to initialize my sprite; however, I had forgotten to call the initialize function for the sprite

Share this post


Link to post
Share on other sites
nlraley    126
Now a question, moving on 2 loading 2 sprites. Can I load multiple sprites between the Begin and End Scene? And does the same count for the Begin and End Draw for the Sprite?

I think what I need to do is call begin scene.
Load up all my sprites using the Begin, Draw, End.
After I have done this for all of them I then call End Scene.
Then present.

Is that right?

Share this post


Link to post
Share on other sites
nlraley    126
Another odd occurrence.

When I am initializing my textures, if I try to create another texture right after creating the first such as this:

//-------------------
if (FAILED(d3d.hr = D3DXCreateTextureFromFileEx (
d3d.d3d9Device, // Device
file, // File Name/Location
0, // Width - 0 Indicates use Dimensions from File to a power of 2
0, // Height - 0 Indicates use Dimensions from File to a power of 2
0, // Mip Levels
0, // Render Targer
D3DFMT_A8R8G8B8, // D3D Format
D3DPOOL_DEFAULT, // D3D Pool
D3DX_FILTER_NONE, // Image Filter
D3DX_DEFAULT, // Mip Filter
D3DCOLOR_XRGB(255, 0, 255), // the hot-pink color key
&SrcInfo, // Pointer to Image Information Structure to be Filled with Image Information
NULL, // Pallette Entry
&d3d.d3dTexture // Pointer to D3D9TEXTURE to hold the Texture
)))
return E_FAIL;
//-------------------
if (FAILED(d3d.hr = D3DXCreateTextureFromFileEx (
d3d.d3d9Device, // Device
file2, // File Name/Location
0, // Width - 0 Indicates use Dimensions from File to a power of 2
0, // Height - 0 Indicates use Dimensions from File to a power of 2
0, // Mip Levels
0, // Render Targer
D3DFMT_A8R8G8B8, // D3D Format
D3DPOOL_DEFAULT, // D3D Pool
D3DX_FILTER_NONE, // Image Filter
D3DX_DEFAULT, // Mip Filter
D3DCOLOR_XRGB(255, 0, 255), // the hot-pink color key
&SrcInfo2, // Pointer to Image Information Structure to be Filled with Image Information
NULL, // Pallette Entry
&d3d.d3dTexture2 // Pointer to D3D9TEXTURE to hold the Texture
)))
return E_FAIL;


It fails on the 2nd one. Any idea why?

*Never mind, had a typo on the file name...

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