5 Replies - 616 Views - Last Post: 19 January 2013 - 07:21 PM Rate Topic: -----

#1 Altbeau  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 07-July 09

Sprite rendering

Posted 19 January 2013 - 02:44 PM

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;
}





Is This A Good Question/Topic? 0
  • +

Replies To: Sprite rendering

#2 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Sprite rendering

Posted 19 January 2013 - 04:11 PM

Who really wrote this code? I can see where you need to manipulate the code to do what you want.

Where do you think it is? Just a rough idea will do.
Was This Post Helpful? 0
  • +
  • -

#3 Altbeau  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 07-July 09

Re: Sprite rendering

Posted 19 January 2013 - 05:33 PM

View PostButchDean, on 19 January 2013 - 05:11 PM, said:

Who really wrote this code? I can see where you need to manipulate the code to do what you want.

Where do you think it is? Just a rough idea will do.


oh sorry... this is what i have to draw random.... it will not stop. i am somewhat certain that that its because of the message loop at the bottom. im having trouble rotating and resizing the sprites as well. i assumed i could use the rand() to do those as well but it doesn't work with float values.


	Sprite1.pos= D3DXVECTOR2(rand() % 800, rand() % 600);
	Sprite1.Draw();



and my professor wrote the outline of the code, and i wrote the rest with help from the book and the lecture

This post has been edited by Altbeau: 19 January 2013 - 05:36 PM

Was This Post Helpful? 0
  • +
  • -

#4 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Sprite rendering

Posted 19 January 2013 - 06:41 PM

This line of code

c = s * rz * t;



is where your professor is performing his transformations (scaling, rotation and translation).

What this code is doing is simple

Sprite1.pos= D3DXVECTOR2(rand() % 800, rand() % 600);
Sprite1.Draw();



This code places your sprite anywhere with pixel accuracy in the window because of rand(). My immediate concern is that the transformation calculations above should be performed from the origin of the world then repositioned by pos(). That should give you better results.
Was This Post Helpful? 0
  • +
  • -

#5 Altbeau  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 07-July 09

Re: Sprite rendering

Posted 19 January 2013 - 06:46 PM

View PostButchDean, on 19 January 2013 - 07:41 PM, said:

This line of code

c = s * rz * t;



is where your professor is performing his transformations (scaling, rotation and translation).

What this code is doing is simple

Sprite1.pos= D3DXVECTOR2(rand() % 800, rand() % 600);
Sprite1.Draw();



This code places your sprite anywhere with pixel accuracy in the window because of rand(). My immediate concern is that the transformation calculations above should be performed from the origin of the world then repositioned by pos(). That should give you better results.


I created the transformation matrices. i think i figured it out.... im not too used to jumping into my code using the debugging tool.. but that line is in the render() which is being called every frame.... if i put that up in the Init() then it doesn't constantly recreate itself.... i guess i could create an array and init them all separately. but the problem is im still not quite sure how to put random rotation and scales on them.
Was This Post Helpful? 0
  • +
  • -

#6 Altbeau  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 07-July 09

Re: Sprite rendering

Posted 19 January 2013 - 07:21 PM

it wont let me edit my post...
const int SpriteCnt[2]

CSprite.Sprite1[SpriteCnt]

and then i i tried to do this in render but both sprites on screen rotated

Sprite1[1].Rotate(1)

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1