Sign in to follow this  

Sprite Animation

This topic is 3291 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to show a sprite animation but it doesn't work, it seems as though the window will onlu refresh if I move it. What I mean is, when I compile the program I get the window with the sprite's first frame but nothing more, but when I move the window it displays another frame, if I move it quickly enough I can see all the other frames. Any what could be causing this?
#include <windows.h>
#include <windowsx.h>
#include <d3d9.h>
#include <d3dx9.h>

#define SCREEN_WIDTH  460
#define SCREEN_HEIGHT 460

#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")

IDirect3D9* d3dObject=NULL;
IDirect3DDevice9* d3dDevice=NULL;
LPD3DXSPRITE d3dSprite;   
IDirect3DTexture9* sprite;  

void initD3D(HWND);
void instance();
void update();
void render(); 
void closeD3D();

struct s_info{
	D3DXVECTOR3 pos;
	DWORD lastUpdate;
	int frame;
	int row;
}chrono;

RECT rct;

const int updateInterval = 180;

LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
    HWND hWnd;
    WNDCLASSEX wc;

    ZeroMemory(&wc, sizeof(WNDCLASSEX));

	wc.cbSize = sizeof(WNDCLASSEX);
	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = (WNDPROC)WindowProc;
	wc.hInstance = hInstance;
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.lpszClassName = "WindowClass1";

    RegisterClassEx(&wc);

    hWnd = CreateWindowEx(NULL,
                          "WindowClass1",
                          "Dungeon",
                          WS_OVERLAPPEDWINDOW,
                          0, 0,
                          SCREEN_WIDTH, SCREEN_HEIGHT,
                          NULL,
                          NULL,
                          hInstance,
                          NULL);

    ShowWindow(hWnd, nCmdShow);

    initD3D(hWnd);

    MSG msg;

	ZeroMemory( &msg, sizeof(msg) );

	instance();

	while( msg.message!=WM_QUIT )
	{
		if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
		{
			TranslateMessage( &msg );
			DispatchMessage( &msg );
		}


		update();
		render();
	}

    closeD3D();

    return (int)msg.wParam;
}

LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
        case WM_DESTROY:
            {
                PostQuitMessage(0);
                return 0;
            } break;
    }

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

void initD3D(HWND hWnd)
{
    d3dObject = Direct3DCreate9(D3D_SDK_VERSION);

    D3DPRESENT_PARAMETERS d3dpp;

    ZeroMemory(&d3dpp, sizeof(d3dpp));

	d3dpp.Windowed=TRUE;
	d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD;
	d3dpp.BackBufferFormat=D3DFMT_UNKNOWN;
	d3dpp.BackBufferCount=1;
	d3dpp.PresentationInterval=D3DPRESENT_INTERVAL_ONE;

    d3dObject->CreateDevice(D3DADAPTER_DEFAULT,
                      D3DDEVTYPE_HAL,
                      hWnd,
                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                      &d3dpp,
                      &d3dDevice);

    D3DXCreateSprite(d3dDevice, &d3dSprite);  

    D3DXCreateTextureFromFileEx(d3dDevice, 
		"chrono_walk.bmp", 
		D3DX_DEFAULT_NONPOW2,
		D3DX_DEFAULT_NONPOW2,
		D3DX_DEFAULT,
		NULL,
		D3DFMT_X8R8G8B8,
		D3DPOOL_MANAGED,
		D3DX_DEFAULT,
		D3DX_DEFAULT,
		D3DCOLOR_XRGB(255,0,255),
		NULL,
		NULL,
		&sprite);

    return;
}

void instance(){
	chrono.pos=D3DXVECTOR3(140.0f,140.0f,0.0f);
	chrono.lastUpdate=timeGetTime();
	chrono.frame=0;
	chrono.row=0;

	return;
}

void render()
{
    d3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

    d3dDevice->BeginScene(); 

    d3dSprite->Begin(NULL);  

	D3DXVECTOR2 pos = D3DXVECTOR2(128,128);
	D3DXVECTOR2 scale(2.0f,2.0f);
	D3DXMATRIX mat;

	D3DXMatrixTransformation2D(&mat, NULL, 0.0, &scale, NULL, 0, &pos);
	rct.left = 32*chrono.frame;
	rct.right = rct.left+32;
	rct.top = 40 *chrono.row;
	rct.bottom = rct.top+40;

	d3dSprite->SetTransform(&mat);

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

    d3dSprite->End(); 

    d3dDevice->EndScene(); 

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

    return;
}

void closeD3D(void){
    sprite->Release();
    d3dDevice->Release();
    d3dObject->Release();

    return;
}

void update(){
	DWORD timeNow=timeGetTime();
	if(timeNow-chrono.lastUpdate>updateInterval)
	{
		if(chrono.row==5){
			chrono.row=0;
		}
		chrono.row++;
	}

	chrono.lastUpdate=timeGetTime();

	return;
}

Share this post


Link to post
Share on other sites
One thing I noticed is you do not have a WM_PAINT message in your handler something like


case WM_PAINT://the window needs repainting
{
//a variable needed for painting information
PAINTSTRUCT ps;

//start painting
HDC hdc=BeginPaint(hwnd,&ps);

/////////////////////////////
//painting code would go here
/////////////////////////////

//end painting
EndPaint(hwnd,&ps);

//handled message, so return 0
return(0);
}break;

Share this post


Link to post
Share on other sites
windows sends it as a message when your window needs repainting if your updating every frame I don't know if you even need to do it, what did you end up doing to solve your problem?

Share this post


Link to post
Share on other sites
There was a period of time set to wait before displaying the sprite's next frame, every time it was updated it was supposed to restart the counter, however I placed the restart counter outside the check :S. All I had to do was place it inside the check so it only got restarted if the frame was updated.

Share this post


Link to post
Share on other sites

This topic is 3291 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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