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

FPS not displaying correctly

$
0
0
I put some code in to display FPS in the corner... but i can't seem to get it to display the FPS... i have some code in Update() and i think it should work, but im may be doing something wrong. maybe i just need a second pair of eyes

#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;
LPD3DXFONT			mFont;
float FPS;

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;

	}
	void Scale(float dxs)
	{
		float Scale = xSize;
		xSize += dxs;
		ySize = xSize;
		zSize = xSize;
	}

	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[0].Rotate((float)rand()/(float) RAND_MAX * 6);
	Sprite1[0].Scale((float)rand()/(float) RAND_MAX * 3 - 0.5);
	Sprite1[1].Rotate((float)rand()/(float) RAND_MAX * 6);
	Sprite1[1].Scale((float)rand()/(float) RAND_MAX * 3 - 0.5);
	Sprite1[2].Rotate((float)rand()/(float) RAND_MAX * 6);
	Sprite1[2].Scale((float)rand()/(float) RAND_MAX * 3 - 0.5);
	Sprite1[3].Rotate((float)rand()/(float) RAND_MAX * 6);
	Sprite1[3].Scale((float)rand()/(float) RAND_MAX * 3 - 0.5);
	Sprite1[4].Rotate((float)rand()/(float) RAND_MAX * 6);
	Sprite1[4].Scale((float)rand()/(float) RAND_MAX * 3 - 0.5);
	Sprite1[5].Rotate((float)rand()/(float) RAND_MAX * 6);
	Sprite1[5].Scale((float)rand()/(float) RAND_MAX * 3 - 0.5);


}

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);
		D3DXCreateFont(pD3DDevice, 20, 0, FW_BOLD, 0, false, 
			DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY,
			DEFAULT_PITCH | FF_DONTCARE, TEXT("Times New Roman"), 
			&mFont);

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

	static float FrameCnt   = 0.0f;
	static float timeElapsed = 0.0f;

	FrameCnt += 1.0f;

	timeElapsed += dt;

	if( timeElapsed >= 1.0f )
	{
		FPS = FrameCnt;

		timeElapsed = 0.0f;
		FrameCnt   = 0.0f;
	}



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


	rect.left = 5;
	rect.top = 5;
	rect.right = 0;
	rect.bottom = 0;
	sprintf(str, "FPS = %0.2f", FPS);
	mFont->DrawText(NULL, str, -1, &rect,
		DT_TOP | DT_LEFT | DT_NOCLIP , D3DCOLOR_ARGB(255, 
		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();
	Transformation();
	// 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>