Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Sprite rendering

$
0
0
Im having trouble understanding how to display sprites on the screen... my assignment is to display 5 sprites at random locations with random scaling and random rotations. I know how to display 1 sprite... but not control where and how its displayed heres my code.. could someone give me a push in the right direction.

#include "settings.h"

#include <windows.h>
#include <stdio.h>

#include MakePath(DXIncludePath, d3d9.h)
#include MakePath(DXIncludePath, d3dx9.h)

// macro for user input
#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
const char g_szClassName[] = "myWindowClass";
const char winTitle[] = "DirectX Demo";

HWND			hwnd;
HINSTANCE       hInstance;                          // Holds The Instance Of The Application

int width = 800;
int height = 600;


LPDIRECT3D9			pD3DObject;
LPDIRECT3DDEVICE9	pD3DDevice;
LPD3DXFONT			pFont;

D3DXVECTOR2			pos;
float zAngle, xSize, ySize, zSize ;
class CSprite {
public:
	LPD3DXSPRITE		pSprite;
	LPDIRECT3DTEXTURE9	pTexture;
	D3DXIMAGE_INFO		imageInfo;



	void Init(char imageFilename[]) {
		D3DXCreateSprite(pD3DDevice, &pSprite);
				
		D3DXCreateTextureFromFileEx(pD3DDevice, imageFilename, 0, 0, 0, 0,
                  D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, 
                  D3DX_DEFAULT, D3DCOLOR_ARGB(0, 0, 0, 255), 
                  &imageInfo, 0, &pTexture);

		pos = D3DXVECTOR2(0, 0);
		zAngle = 0;
		xSize = 1;
		ySize = 1;
		zSize = 1;

	}
	void Move(float dx, float dy)
	{
		pos.x += dx;
		pos.y += dy;
	}

	void Rotate(float dAngle)
	{
		zAngle += dAngle;
	}
	void Scale(float dxs, float dys, float dzs)
	{
		xSize += dxs;
		ySize += dys;
		zSize += dzs;
	}

	void Draw() {

		D3DXVECTOR3 center(imageInfo.Width/2, imageInfo.Height/2, 0);
		D3DXMATRIX t, rz, c, s;
		

		D3DXMatrixTranslation(&t, pos.x, pos.y, 0);
		D3DXMatrixRotationZ(&rz, zAngle * D3DX_PI/180);
		D3DXMatrixScaling(&s, xSize, ySize, zSize);

		c = s * rz * t;
		pSprite->Begin(D3DXSPRITE_ALPHABLEND);

		pSprite->SetTransform(&c);

		pSprite->Draw(pTexture, NULL, &center, NULL, D3DCOLOR_ARGB(255, 255, 255, 255));

		pSprite->End();
	}

	CSprite() {

	}

	~CSprite() {
		if (pSprite) {
			pSprite->Release();
		}
		if (pTexture) {
			pTexture->Release();
		}
	}

};


CSprite Sprite1;

void Init() {

	// create direct3d object
	pD3DObject = Direct3DCreate9(D3D_SDK_VERSION);

	if (pD3DObject) {

		// create direct3d device
		D3DPRESENT_PARAMETERS d3dpp;
		d3dpp.BackBufferCount = 1;
		d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
		d3dpp.BackBufferWidth = width;
		d3dpp.BackBufferHeight = height;
		d3dpp.Windowed = true;
		d3dpp.hDeviceWindow = hwnd;
		d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
		d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
		d3dpp.MultiSampleQuality = 0;
		d3dpp.EnableAutoDepthStencil = true;
		d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
		d3dpp.Flags = 0;
		d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
		d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;

		pD3DObject->CreateDevice(
			D3DADAPTER_DEFAULT,
			D3DDEVTYPE_HAL,
			hwnd,
			D3DCREATE_SOFTWARE_VERTEXPROCESSING,
			&d3dpp,
			&pD3DDevice);


		D3DXCreateFont(pD3DDevice, 30, 0, FW_BOLD, 0, false, 
                  DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY,
                  DEFAULT_PITCH | FF_DONTCARE, TEXT("Times New Roman"), 
                  &pFont);

		Sprite1.Init("test.tga");

	}


}

void Update(float dt) {


	char str[128];
	sprintf(str, "GSP381 Beau Altman");
	SetWindowText(hwnd, str);
}

void Render() {

	pD3DDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(122, 85, 0) , 1.0f, 0);

	pD3DDevice->BeginScene();

	Sprite1.Draw();


	char str[128];
	RECT rect;
	rect.left = 100;
	rect.top = 100;
	rect.right = 300;
	rect.bottom = 120;
	sprintf(str, "Transformations");
	pFont->DrawText(NULL, str, -1, &rect,
		DT_TOP | DT_LEFT | DT_NOCLIP , D3DCOLOR_ARGB(255, 
		rand() % 200 + 50,
		rand() % 200 + 50,
		rand() % 200 + 50
		//255, 
		//255, 
		//255

		));

	pD3DDevice->EndScene();

	pD3DDevice->Present(0, 0, 0, 0);


}

void Shutdown() {

	if (pD3DDevice) {
		pD3DDevice->Release();
	}
	if (pD3DObject) {
		pD3DObject->Release();
	}
}

// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{

	switch(msg)
	{
		case WM_CLOSE:
		case WM_DESTROY:
			PostQuitMessage(0);
		break;
		default:
			return DefWindowProc(hwnd, msg, wParam, lParam);
	}
	return 0;
}

int WINAPI WinMain(HINSTANCE _hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
	WNDCLASSEX wc;
	
	MSG Msg;

	hInstance = _hInstance;

	//Step 1: Registering the Window Class
	wc.cbSize		 = sizeof(WNDCLASSEX);
	wc.style		 = 0;
	wc.lpfnWndProc	 = WndProc;
	wc.cbClsExtra	 = 0;
	wc.cbWndExtra	 = 0;
	wc.hInstance	 = hInstance;
	wc.hIcon		 = LoadIcon(NULL, IDI_APPLICATION);
	wc.hCursor		 = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	wc.lpszMenuName  = NULL;
	wc.lpszClassName = g_szClassName;
	wc.hIconSm		 = LoadIcon(NULL, IDI_APPLICATION);

	if(!RegisterClassEx(&wc))
	{
		MessageBox(NULL, "Window Registration Failed!", "Error!",
			MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}

	// Step 2: Creating the Window
	hwnd = CreateWindowEx(
		WS_EX_CLIENTEDGE,
		g_szClassName,
		winTitle,
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, CW_USEDEFAULT, width, height,
		NULL, NULL, hInstance, NULL);

	if(hwnd == NULL)
	{
		MessageBox(NULL, "Window Creation Failed!", "Error!",
			MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}

	ShowWindow(hwnd, nCmdShow);
	UpdateWindow(hwnd);

	Init();

	// Step 3: The Message Loop
	while(true) {

		
		if (PeekMessage(&Msg,NULL,0,0,PM_REMOVE));
		{
		if (Msg.message == WM_QUIT) 
		{
			break;
		}
		TranslateMessage(&Msg);
		DispatchMessage(&Msg);
		}
		Update(0);

		Render();

	}

	Shutdown();

	return Msg.wParam;
}




Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>