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

Trouble implementing random scaling and rotation

$
0
0
My assignment is to Create an array of 6 instances of these structures to be filled out in Init() once.
Assign random positions (within screen space), rotation (0 to 360 degrees, use conversion macro D3DXToRadian(float degree) helper function), and uniform scale (a float value to be applied to the x and y scale of the object), as well as a color modulation value of type D3DCOLOR.
however color modulation is optional. I have been able to get the random positions but when i try to rotate or scale it doesn't stop. Im wondering if i need to make a new function like Init() that doesn't execute every frame.. or what... any help would be great

Main.cpp
#include "settings.h"

#include <ctime>
#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;




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

	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;
				Sprite1[3].Rotate(0.04);
	}
	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);
		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();
		}
	}

};

const int SpriteCnt = 6;
CSprite Sprite1[SpriteCnt];
CSprite Sprite2;

void Transformation()
{
		Sprite1[3].Rotate(0.04);
}

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

		srand (time(NULL));

		Sprite1[0].Init("test.png");
		Sprite1[0].pos= D3DXVECTOR2(rand() % 700 + 50, rand() % 500 + 50);

		Sprite1[1].Init("test.png");
		Sprite1[1].pos= D3DXVECTOR2(rand() % 700 + 50, rand() % 500 + 50);

		Sprite1[2].Init("test.png");
		Sprite1[2].pos= D3DXVECTOR2(rand() % 700 + 50, rand() % 500 + 50);

		Sprite1[3].Init("test.png");
		Sprite1[3].pos= D3DXVECTOR2(rand() % 700 + 50, rand() % 500 + 50);

		Sprite1[4].Init("test.png");
		Sprite1[4].pos= D3DXVECTOR2(rand() % 700 + 50, rand() % 500 + 50);

		Sprite1[5].Init("test.png");
		Sprite1[5].pos= D3DXVECTOR2(rand() % 700 + 50, rand() % 500 + 50);
		
	}

}

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[0].Draw();
	Sprite1[1].Draw();
	Sprite1[2].Draw();
	Sprite1[3].Draw();
	Sprite1[4].Draw();
	Sprite1[5].Draw();
	Sprite1[5].Rotate(.002);

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





Settings.h
// settings.h

/////////////////////// remove warnings
#pragma warning(disable:4244)
#pragma warning(disable:4305)
#pragma warning(disable:4996)
#pragma warning(disable:4018)
#pragma warning(disable:4101)


// set up include and library paths

// paths for Decatur 6105 - replace with your specific DirectX path
//#define DXIncludePath C:/Program Files/Microsoft DirectX SDK (March 2009)/Include
//#define DXLibraryPath "C:/Program Files/Microsoft DirectX SDK (March 2009)/lib/x86/"
#define DXIncludePath C:/Program Files (x86)/Microsoft DirectX SDK (June 2010)/Include
#define DXLibraryPath "C:/Program Files (x86)/Microsoft DirectX SDK (June 2010)/lib/x86/"

#define MakePath(a, B)/>/> <a/b>



/////////////////////////// libs
#pragma comment (lib, DXLibraryPath "d3d9.lib")
#pragma comment (lib, DXLibraryPath "d3dx9.lib")
//#pragma comment (lib, DXLibraryPath "dxerr9.lib")

//#pragma comment (lib, "opengl32.lib")
//#pragma comment (lib, "glu32.lib")

///////////////////////// don't use unicode - don't have to change project charset property :)/>/>
#undef UNICODE
#undef _UNICODE



Viewing all articles
Browse latest Browse all 51036

Trending Articles



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