Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

jimiwa

complete code for simple animation

This topic is 5230 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

Here''s my code for a simple animation. It''s taken me a LONG time to get to the point where I could write this, part of it is slightly copied but I rearranged things so it''s presented in my own format. Hope it helps someone figure out what took me over a year to learn.
#include <windows.h>
// complete source code for a simple animation

// Done with DirectX 9 for Visual C++ .NET


// Animation.h file


#pragma once

#include <d3d9.h>
#include <d3dx9tex.h>

// *******************************************************************


// Animation.cpp file


// compiler settings for Visual C++ .NET:


// remember to add your directx include directory to additional include directories

// on your compiler and add directx library directory to additional library directories.

// add d3d9.lib, d3dx9.lib, and dxerr9.lib to additional dependencies



#include "Animation.h"

HINSTANCE hInst;				// holds the instance for this app

HWND hWnd;			    		// global window handle

	
#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1: 0)
#define KEYUP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)

#define GAME_SPEED 30
LPDIRECT3D9 pD3D;
LPDIRECT3DDEVICE9 pd3dDevice;

// timer stuff

LARGE_INTEGER timeStart;
LARGE_INTEGER timeEnd;
LARGE_INTEGER timerFreq;
float         anim_rate;

// Direct 3d Surface

IDirect3DSurface9* spriteSurface;
RECT Rect;

int TimerCount;

// structure for the sprite structure

struct {
	RECT SprRect; 
	int X;
	int Y;
} TheSprite;

////////////////////////////////////////////// forward declarations

bool    initWindow(HINSTANCE hInstance);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

void InitDirect3D()
{
		if (NULL == (pD3D = Direct3DCreate9(D3D_SDK_VERSION)))
		{
			exit(1);
		}
		D3DPRESENT_PARAMETERS D3DPresentParms;
		D3DFORMAT dsf;
		D3DFORMAT colorf;
		D3DMULTISAMPLE_TYPE multis;
		dsf = D3DFMT_D16; 
		// set color mode

		colorf = D3DFMT_A8R8G8B8;
		multis = D3DMULTISAMPLE_NONE;
		ZeroMemory(&D3DPresentParms, sizeof(D3DPresentParms));
		D3DPresentParms.Windowed = FALSE;
		D3DPresentParms.SwapEffect = D3DSWAPEFFECT_DISCARD;
		D3DPresentParms.EnableAutoDepthStencil = true;
		D3DPresentParms.AutoDepthStencilFormat = dsf;
		D3DPresentParms.BackBufferFormat = colorf;
		D3DPresentParms.BackBufferCount = 1;
		D3DPresentParms.BackBufferWidth = 640;
		D3DPresentParms.BackBufferHeight = 480;
		D3DPresentParms.hDeviceWindow = hWnd;
		D3DPresentParms.MultiSampleType = multis;

		if (FAILED(pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_REF,
			hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &D3DPresentParms, &pd3dDevice)))
		{
			exit(2);
		}

}

void ShutDownDirect3D()
{
		if (pd3dDevice != NULL)
		{
			pd3dDevice->Release();
			pd3dDevice = NULL;
		}
		if (pD3D != NULL)
		{
			pD3D->Release();
			pD3D = NULL;
		}
}

void initSprites(void)
{
	TheSprite.X = 0;
	TheSprite.Y = 64;
	// Create Surface 

	pd3dDevice->CreateOffscreenPlainSurface(32,32,
		D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &spriteSurface, NULL);
	// you may not use a bitmap for the file and get transparency

	// a png, however, supports transparency.  Make sure you create

	// your png with a transparent background and make sure it is

	// in the same color format used in your program

	D3DXLoadSurfaceFromFile(spriteSurface,NULL,NULL, "ball.png", NULL, D3DX_DEFAULT,0,NULL);
	
}


IDirect3DSurface9* GetBackBuffer(void)
{
	IDirect3DSurface9* backbuffer= NULL;
	if (!pd3dDevice) return NULL;
	pd3dDevice->GetBackBuffer(0,0,D3DBACKBUFFER_TYPE_MONO, &backbuffer);
	return backbuffer;
}


void BlitToSurface(IDirect3DSurface9* srcSurface, const RECT * Rect, const RECT *SprRect)
{
	pd3dDevice->StretchRect(srcSurface,Rect,GetBackBuffer(),SprRect,D3DTEXF_NONE);
}

void drawSprites(void)
{
	TheSprite.SprRect.left = TheSprite.X;
	TheSprite.SprRect.top = TheSprite.Y;
	TheSprite.SprRect.right = TheSprite.X +31;
	TheSprite.SprRect.bottom = TheSprite.Y + 31;
	Rect.left = 0;
	Rect.top = 0;
	Rect.right = 31;
	Rect.bottom = 31;
	BlitToSurface(spriteSurface, &Rect, &TheSprite.SprRect);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
	// call our function to init and create our window

	if (!initWindow(hInstance))
	{
		MessageBox(NULL, "Unable to create window", "ERROR", MB_OK);
		return false;
	}
	InitDirect3D();
	initSprites();
	
	
	TimerCount = 0;
	//QueryPerformanceFrequency(&timerFreq);


	// Main message loop:

	// Enter the message loop

    MSG msg; 
    ZeroMemory( &msg, sizeof(msg) );
    while( msg.message!=WM_QUIT )
    {
		// check for messages

		if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
        {
			TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
		// this is called when no messages are pending

		else
		{
			
			pd3dDevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),
							  1.0f, 0);
			drawSprites();
			pd3dDevice->Present(NULL,NULL,NULL,NULL);
			if (TimerCount == 0) TheSprite.X += 4;
			if (TheSprite.X>607) TheSprite.X = 0;
			if (KEYDOWN(VK_ESCAPE)) 
			{
				ShutDownDirect3D();
				exit (0);
			}
			while (GetTickCount() < GAME_SPEED);

		
		}
    }
	
	ShutDownDirect3D();
	return (int) msg.wParam;
}

bool initWindow(HINSTANCE hInstance)
{
	WNDCLASSEX winclass;

	winclass.cbSize = sizeof(WNDCLASSEX);
	winclass.style			= CS_HREDRAW | CS_VREDRAW;
	winclass.lpfnWndProc	= (WNDPROC)WndProc;
	winclass.cbClsExtra		= 0;
	winclass.cbWndExtra		= 0;
	winclass.hInstance		= hInstance;
	winclass.hIcon			= 0;
	winclass.hCursor		= LoadCursor(NULL, IDC_ARROW);
	winclass.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	winclass.lpszMenuName	= NULL;
	winclass.lpszClassName	= "Animation Demo";
	winclass.hIconSm		= 0;
	RegisterClassEx(&winclass);

	hWnd = CreateWindow("Animation Demo", 
							 "Animation Demo", 
							 WS_POPUP,
							 CW_USEDEFAULT, 
							 CW_USEDEFAULT, 
							 640, 
							 480, 
							 NULL, 
							 NULL, 
							 hInstance, 
							 NULL);
   if (!hWnd)
      return false;
   
   ShowWindow(hWnd, SW_SHOW);
   UpdateWindow(hWnd);

   return true;
}

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

Share this post


Link to post
Share on other sites
Advertisement

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!