Sign in to follow this  
gmax

Sprite Problem(not working)

Recommended Posts

[SOURCE]
#include <d3d9.h>
#include <d3dx9.h>
#include <d3dx9math.h> 

LPDIRECT3D9					d3d			=	NULL;
LPDIRECT3DDEVICE9			device		=	NULL;
//LPDIRECT3DVERTEXBUFFER9		vbuffer		=	NULL;
//LPDIRECT3DTEXTURE9			texture		=	NULL;
//LPDIRECT3DTEXTURE9			texture2	=	NULL;
LPD3DXSPRITE				sprite		=	NULL;
LPDIRECT3DTEXTURE9			spTex		=	NULL;


HRESULT InitD3D(HWND hWnd)
{
	d3d		=	Direct3DCreate9(	D3D_SDK_VERSION		);

	D3DPRESENT_PARAMETERS	params;
	ZeroMemory(&params, sizeof(params));
	params.Windowed			=	TRUE;
	params.SwapEffect		=	D3DSWAPEFFECT_DISCARD;
	params.BackBufferFormat	=	D3DFMT_UNKNOWN;




	d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
		D3DCREATE_HARDWARE_VERTEXPROCESSING, &params, &device);



	return S_OK;
}


VOID Render()
{

	device->Clear(0, NULL, D3DCLEAR_TARGET , D3DCOLOR_XRGB(90,100,60), 1.0f, 0);
	//D3DXCreateTextureFromFile(device, "my.bmp", &texture);
	//D3DXCreateTextureFromFile(device, "my2.bmp", &texture2);
	D3DXCreateTextureFromFile(device, "sprite.bmp", &spTex);
	D3DXCreateSprite(device,&sprite);


	
device->BeginScene();
sprite->Begin(D3DXSPRITE_ALPHABLEND);

RECT rct;
rct.left=0;
rct.right=64;
rct.top=0;
rct.bottom=64;

D3DXVECTOR3 pos;
pos.x=10.0f;
pos.y=20.0f;
pos.z=0.0f;	

sprite->Draw(spTex,&rct,NULL,&pos,0xFFFFFFFF);

rct.left++;
rct.right++;

sprite->End();
device->EndScene();


device->Present(NULL,NULL,NULL,NULL);

}

VOID Cleanup()
{
	d3d->Release();
	device->Release();
	//vbuffer->Release();
	//texture->Release();
	//texture2->Release();
	sprite->Release();
}

LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
    switch( msg )
    {
        case WM_DESTROY:
            Cleanup();
            PostQuitMessage( 0 );
            return 0;
    }

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





INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
    // Register the window class
    WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
                      GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
                      "D3D", NULL };
    RegisterClassEx( &wc );

    // Create the application's window
    HWND hWnd = CreateWindow( "D3D", "Sprite",
                              WS_OVERLAPPEDWINDOW, 100, 100, 256, 256,
                              NULL, NULL, wc.hInstance, NULL );

    // Initialize Direct3D
    if( SUCCEEDED( InitD3D( hWnd ) ) )
    {
        // Create the scene geometry
        
            // Show the window
            ShowWindow( hWnd, SW_SHOWDEFAULT );
            UpdateWindow( hWnd );

            // Enter the message loop
            MSG msg;
            ZeroMemory( &msg, sizeof(msg) );
            while( msg.message!=WM_QUIT )
            {
                if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
                {
                    TranslateMessage( &msg );
                    DispatchMessage( &msg );
                }
                else
                    Render();
            
        }
    }

    UnregisterClass( "D3D", wc.hInstance );
    return 0;
}
[/SOURCE]
I cant see my sprite(i can see only first frame of sprite) And my sprite image:D http://mburakaydin.mb.funpic.de/sprite.bmp Whats the problem?

Share this post


Link to post
Share on other sites
You should NOT be creating your texture and sprite every frame! Move the texture loading and sprite creation into your initialization function (make the texture and sprite pointers global). Then just do the rendering in your render function.

Share this post


Link to post
Share on other sites
Also, you're incrementing rec.left (rec.left++) after drawing your sprite, but then rec is recreated next time through Render() and it sets rec.left back to 0 (rec.left = 0;). I don't think that's what you want to do, but I could be wrong.

Share this post


Link to post
Share on other sites
Quote:
Original post by slowmike
Also, you're incrementing rec.left (rec.left++) after drawing your sprite, but then rec is recreated next time through Render() and it sets rec.left back to 0 (rec.left = 0;). I don't think that's what you want to do, but I could be wrong.


Oops. Missed that. You will need to make your rec variable global, initialize it in your initialization function and increment it in your render function.

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