All pastes #2097898 Raw Edit

Something

public cpp v1 · immutable
#2097898 ·published 2011-12-31 20:19 UTC
rendered paste body
// Include the necessary libs.#pragma comment (lib, "d3d9.lib")#pragma comment (lib, "d3dx9.lib")// Include the windows header file.#include <windows.h>#include <d3d9.h>#include <d3dx9.h>#include <iostream>#include <vector>#include <ctime>#include <sstream>// Include definitions.#define SPRITE_WIDTH 48;#define SPRITE_HEIGHT 48;#define SCRN_WIDTH 1600;#define SCRN_HEIGHT 900;using namespace std;HINSTANCE hInst; // Global handle to hold the application instance.HWND wndHandle; // Global variable to hold the window handle.// Direct 3D stuffLPD3DXFONT d3dFont;LPDIRECT3D9 pD3D;	// The Direct3D object.LPDIRECT3DDEVICE9 pd3dDevice;	// The Direct3D device.struct {	RECT srcRect;	// Position	float X;	float Y;	// Movement	float moveX;	float moveY;	short int dirX; // Non standard, this is to move the sprite in a given direction on the Y axis.	short int dirY; // Non standard, this is to move the sprite in a given direction on the X axis.	// Animation 	int numFrames;	int curFrame;} spriteStruct[3];// Define some function prototypes.bool initWindow(HINSTANCE hInstance);LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);// Direct3D prototypes.bool initDirect3D(void);void render(void);IDirect3DSurface9* getSurfaceFromBitmap(std::string filename);bool initSprites(void);// This is the WinMain function, it's the main() of a windows app.int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow){	//srand((unsigned int)time(0));	// Initialize the window.	if (!initWindow(hInstance))		return false;	if (!initDirect3D())		return false;	if (!initSprites())		return false;	// The message loop.	MSG msg;	ZeroMemory(&msg, sizeof(msg));	while (msg.message != WM_QUIT)	{		// Close the program if any key is pressed.		if (msg.message == WM_KEYDOWN)		{			PostQuitMessage(0);			return 0;		}		// Windows related.		if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))		{			TranslateMessage(&msg);			DispatchMessage(&msg);		}		else		{			// This here, is the game loop. ~epic music~			for (int i = 0; i <10; i++)			{				spriteStruct[i].X  = 0;				// Move the sprite in the correct direction. 				//if (spriteStruct[i].dirX == 1) spriteStruct[i].moveX += .5;				//else spriteStruct[i].moveX -= .5;				//if (spriteStruct[i].dirY == 1) spriteStruct[i].moveY -= 1;				//else spriteStruct[i].moveY += 1;				//--OLD--				spriteStruct[i].moveX += .25;				spriteStruct[i].moveY += .25;				spriteStruct[i].X += spriteStruct[i].moveX;				spriteStruct[i].Y += spriteStruct[i].moveY;				// Not greater than 640.				if ((spriteStruct[i].X + 48) >= (1600 - 20))					spriteStruct[i].dirX *= -1;				// Not greater than 480.				if ((spriteStruct[i].Y + 48) >= (900 - 20))					spriteStruct[i].dirY *= -1;				// Check 0 now.				if (spriteStruct[i].X <= 20)					spriteStruct[i].dirX *= -1;				if (spriteStruct[i].Y <= 20)					spriteStruct[i].dirY *= -1;			}			render();		}	}	return (int)msg.wParam;}// bool initWindow(): registers the window class for the application and creates the window.bool initWindow(HINSTANCE hInstance){	WNDCLASSEX wcex;	// Fill in the WNDCLASSEX structure.	wcex.cbSize				= sizeof(WNDCLASSEX); // The size of the structure.	wcex.style				= CS_HREDRAW | CS_VREDRAW; // The class style.	wcex.lpfnWndProc		= (WNDPROC)WndProc; // The windows procedure callback/	wcex.cbClsExtra			= 0; // Extra bytes to allocate for this class.	wcex.cbWndExtra			= 0; // Extra bytes to allocate for this instance.	wcex.hInstance			= hInstance; // Handle to the application instance.	wcex.hIcon				= 0; // Icon to associate with this application.	wcex.hCursor			= LoadCursor(NULL, IDC_ARROW); // The default cursor.	wcex.hbrBackground		= (HBRUSH)(COLOR_WINDOW+1); // The background color.	wcex.lpszMenuName		= NULL; // The resource name for the menu.	wcex.lpszClassName		= "DirectX Example"; // The class name being created.	wcex.hIconSm			= 0; // The handle to the small icon.	RegisterClassEx(&wcex);	wndHandle = CreateWindow(					"DirectX Example",	// The window class to use.					"DirectX Example",	// The title bar text.					WS_EX_TOPMOST | WS_POPUP | WS_VISIBLE,	// The window style to use. *** WS_OVERLAPPEDWINDOW - Windowed; WS_EX_TOPMOST | WS_POPUP | WS_VISIBLE - Full screen ~ Rune4234;					CW_USEDEFAULT,	// The default X coordinate.					CW_USEDEFAULT,	// The default Y coordinate.					640,	// The pixel width of the window.					480,	// The pixel height of the window.					NULL,	// The parent window. NULL for desktop.					NULL,	// The menu for the application. NULL for none.					hInstance,	// The handle to the application instance.					NULL);	// No values passed to the window.	// Check to make sure the handle created is valid.	if (!wndHandle)		return false;	// Display the window.	ShowWindow(wndHandle, SW_SHOW);	UpdateWindow(wndHandle);	return true;}// The window procedure.LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){	// Check any available messages from the queue.	switch (message)	{	case WM_DESTROY:		PostQuitMessage(0);		break;	}	// Always return the message to the default window procedure for further processing.	return DefWindowProc(hWnd, message, wParam, lParam);}// The initDirect3D function.bool initDirect3D(void){	pD3D = NULL;	pd3dDevice = NULL;	//d3dFont = NULL;	// Create the DirectX object.	if (NULL == (pD3D = Direct3DCreate9(D3D_SDK_VERSION))) return false;	// Fill the presentation parameters structure.	D3DPRESENT_PARAMETERS d3dpp;	ZeroMemory(&d3dpp, sizeof(d3dpp));	d3dpp.Windowed			= FALSE; // *** TRUE - Windowed; FALSE - Fullscreen ~ Rune4234	d3dpp.SwapEffect		= D3DSWAPEFFECT_DISCARD;	d3dpp.BackBufferFormat	= D3DFMT_X8R8G8B8; // *** D3DFMT_UNKNOWN - Windowed; D3DFMT_X8R8G8B8 - Fullscreen ~ Rune4234	d3dpp.BackBufferCount	= 1;	d3dpp.BackBufferWidth	= 1600;	d3dpp.BackBufferHeight	= 900;	d3dpp.hDeviceWindow		= wndHandle;	// Create the default DirectX device.	if (FAILED(pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, wndHandle, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pd3dDevice)))		return false;	// Create the font object.	//if (FAILED(D3DXCreateFont(pd3dDevice, 48, 0, FW_NORMAL, 1, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH|FF_DONTCARE, "Arial", &d3dFont))) 		//return NULL;		return true;}// Direct3D render function.void render(void){	string spStr;	stringstream strX;	stringstream strY;	// This will hold the back buffer.	IDirect3DSurface9* backbuffer = NULL;	// Check to make sure you have a valid Direct3D device.		if ((pd3dDevice == NULL)/* || (d3dFont == NULL)*/)		return;	// Set the back buffer to a blue color.	pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0); 	// Get the back buffer.	pd3dDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);	for (int i = 0; i < 10; i++)	{		// Increment the animation frame.		if (spriteStruct[i].curFrame < spriteStruct[i].numFrames)			spriteStruct[i].curFrame++;		else			spriteStruct[i].curFrame = 0;		// Set the source rectangle to the correct frame.		spriteStruct[i].srcRect.left = spriteStruct[i].curFrame * 48;		spriteStruct[i].srcRect.right = spriteStruct[i].srcRect.left + 48;		RECT destRect;		// A temporary rectangle.		RECT textRect;		IDirect3DSurface9* spriteSurface;				spriteSurface = getSurfaceFromBitmap("text.bmp");		if (spriteSurface == NULL) return;				destRect.left = (long)spriteStruct[i].X;		destRect.top = (long)spriteStruct[i].Y;		destRect.bottom = destRect.top + SPRITE_HEIGHT;		destRect.right = destRect.left + SPRITE_WIDTH;		/*textRect.left = (long)spriteStruct[i].X;		textRect.top = (long)spriteStruct[i].Y;		textRect.bottom = textRect.top + SPRITE_HEIGHT;		textRect.right = textRect.left + 100;*/		/*strX << spriteStruct[i].X;		strY << spriteStruct[i].Y;		spStr = "X: "; 		spStr += strX.out ;		spStr += " Y: "; 		spStr += strY.out;*/		// Draw the sprite to the back buffer.		pd3dDevice->StretchRect(spriteSurface, &spriteStruct[i].srcRect, backbuffer, &destRect, D3DTEXF_NONE);		// Draw the coords of the sprite.		//d3dFont->DrawText(NULL, spStr.c_str(), -1, &textRect, DT_LEFT|DT_NOCLIP, 0xFFFFFFFF);	}	// Present the back buffer to the display.	pd3dDevice->Present(NULL, NULL, NULL, NULL);}// Direct3D clean up function.void cleanUp(void){	// Release the device and the Direct3D object.	if (pd3dDevice != NULL)		pd3dDevice->Release();	if (pD3D != NULL)		pD3D->Release();	//if (d3dFont != NULL)	//	d3dFont->Relase();}IDirect3DSurface9* getSurfaceFromBitmap(string filename){	HRESULT hResult;	IDirect3DSurface9* surface = NULL;	int lol = NULL;	D3DXIMAGE_INFO imgInfo;		// Holds information about the loaded bitmap.	if (pd3dDevice == NULL) return NULL;	// Get the width and height info from this bitmap	hResult = D3DXGetImageInfoFromFile(filename.c_str(), &imgInfo);	if (FAILED(hResult)) return NULL;	int width = imgInfo.Width, height = imgInfo.Height;	// Create an offscreen surface to hold the bitmap. 	hResult = pd3dDevice->CreateOffscreenPlainSurface(width, height, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &surface, NULL);	if (FAILED(hResult)) return NULL;	// Load the bitmap into the surface that we created.	hResult = D3DXLoadSurfaceFromFile(surface, NULL, NULL, filename.c_str(), NULL, D3DX_DEFAULT, 0, NULL);	if (FAILED(hResult)) return NULL;	return surface;}bool initSprites(void){	srand((unsigned int)time(0));	// Loop through 10 sprite structures and initialize them.	for (int i = 0; i < 3; i++)	{		// Set the location data.		spriteStruct[i].srcRect.top			= 0;		spriteStruct[i].srcRect.left		= i * SPRITE_WIDTH;		spriteStruct[i].srcRect.right		= spriteStruct[i].srcRect.left + SPRITE_WIDTH;		spriteStruct[i].srcRect.bottom		= SPRITE_HEIGHT;		spriteStruct[i].X = (int)rand() % SCRN_WIDTH - SPRITE_WIDTH;		spriteStruct[i].Y = (int)rand() % SCRN_HEIGHT - SPRITE_HEIGHT;		spriteStruct[i].dirX = rand() % 2 + 1; // This isn't a standard parameter, I added this to make it move a random direction upon creation.		spriteStruct[i].dirY = rand() % 2 + 1; // ^^^				// Set the animation data.		spriteStruct[i].curFrame = 0;		spriteStruct[i].numFrames = 3;	}	return true;}