Sie sind auf Seite 1von 7

Assignment Cover Sheet

Faculty of Science and Technology


NAME: Nick Iannelli______________________________
STUDENT ID: 900207768__________________________
UNIT CODE: SIT252______________________________
ASSIGNMENT/PRAC No.: 1_________________________
ASSIGNMENT/PRAC NAME: Assignment 1_____________
DUE DATE: 30/7/2010____________________________

Plagiarism and collusion


Plagiarism occurs when a student passes off as the student’s own work, or copies without
acknowledgment as to its authorship, the work of any other person.

Collusion occurs when a student obtains the agreement of another person for a
fraudulent purpose with the intent of obtaining an advantage in submitting an
assignment or other work

Declaration
I certify that the attached work is entirely my own (or where submitted to meet the
requirements of an approved group assignment is the work of the group), except where
work quoted or paraphrased is acknowledged in the text. I also certify that it has not
been submitted for assessment in any other unit or course.

I agree that Deakin University may make and retain copies of this work for the purposes
of marking and review, and may submit this work to an external plagiarism-detection
service who may retain a copy for future plagiarism detection but will not release it or
use it for any other purpose.

DATE: 29/7/2010________________________________

An assignment will not be accepted for assessment if the declaration appearing above
has not been duly completed by the author.
/*
*******************************************************************
******************* READ BEFORE VIEWING ********************
*******************************************************************
THIS TUTORIAL TEACHES A STUDENT HOW TO INITIALIZE THE GRAPHICS
DEVICES FOR DIRECT3D. THEY ARE ASSUMED TO HAVE BASIC KNOWLEDGE
OF C++ AS WELL AS KNOW HOW TO SET UP VISUAL STUDIO DIRECT3D SDK
THANKYOU AND ENJOY
*******************************************************************
*******************************************************************
*/
#include<windows.h>
#include<d3d9.h>
#include<d3dx9.h>
/* These first few lines are used to import the standard libraries
into your winMain.cpp file. It includes the windows.h file, which
has the basic windows functionality as well as the direct9 and
directX9 libraries for doing any directX work. */
#define WINDOW_NAME L"Creating a triangle with DirectX"
#define WINDOW_CLASS L"UPGCLASS"
#define WINDOW_WIDTH 1280
#define WINDOW_HEIGHT 1024
/* These lines define the constant values within the program.
Defining the window name (the name that appears on the taskbar),
the width and height of the window and the base windows class
being used. */

HWND g_hwnd; // This creates the window object

/* This creates null directX objects that can be initialized


later on*/
LPDIRECT3D9 g_d3dObject = NULL;
LPDIRECT3DDEVICE9 g_d3dDevice = NULL;

/* Creates the vertex structure that will be used to create each


vertex for our triangles later on. */
struct Vertex
{
FLOAT x, y, z;
DWORD color;
};

// This defines how the verticies interact and on what plane


#define D3DFVF_D3DVertex (D3DFVF_XYZ | D3DFVF_DIFFUSE)

// Creates a vertex buffer, and makes sure it's empty


LPDIRECT3DVERTEXBUFFER9 g_vertexBuffer = NULL;

// Create the function that will be called any time the window gets
resized.
void ResizeD3D9Window(int width, int height)
{
if(g_d3dDevice == NULL)
return;
// If there is no direct3d object, jump out of the function.
D3DVIEWPORT9 viewport;
viewport.X = 0;
viewport.Y = 0;
viewport.Width = width;
viewport.Height = height;
viewport.MinZ = 0.0f;
viewport.MaxZ = 1.0f;
/*Create and define a viewport setting the new screen to the top
left hand corner and making it the size of the resized window */
g_d3dDevice->SetViewport(&viewport);
// Set the direct3d object's viewport to the one we just created

D3DXMATRIX projMat; // Create a transformation matrix

// set up the matrix


D3DXMatrixPerspectiveFovLH(&projMat /* which transformation matrix */,
D3DXToRadian(60.0f) /* amount to rotate */,
(float)width / (float)height /* aspect ratio
of new view */,
0.1f /* minimum checksum */, 1000.0f /*
maximum checksum*/);

//apply the matrix to the direct3d device


g_d3dDevice->SetTransform(D3DTS_PROJECTION, &projMat);
}

// Initializes direct3d, and returns true if successful


bool InitializeD3D9()
{
D3DDISPLAYMODE displayMode;
D3DPRESENT_PARAMETERS params;
D3DCAPS9 caps;
// creates some basic variables that will be altered later

ZeroMemory(&params, sizeof(params));

g_d3dObject = Direct3DCreate9(D3D_SDK_VERSION);
// Attempts to create a direct3d object
if(g_d3dObject == NULL) // If it can't create the direct3d object,
return false
return false;

HRESULT hr;

hr = g_d3dObject->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,
&displayMode); // finds out the
graphics cards display mode
if(FAILED(hr))
return false; // if it can't find the display mode, exit.

hr = g_d3dObject->GetDeviceCaps(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL, &caps); // finds the
limitations of the device
if(FAILED(hr))
return false; // if it can't find any, exit.

DWORD flags = 0;
// Use hardware if it was found, else software.
if(caps.VertexProcessingCaps != 0)
flags |= D3DCREATE_HARDWARE_VERTEXPROCESSING;
else
flags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;

params.Windowed = TRUE;
params.SwapEffect = D3DSWAPEFFECT_DISCARD;
params.BackBufferFormat = displayMode.Format;
params.BackBufferCount = 1;
params.EnableAutoDepthStencil = TRUE;
params.AutoDepthStencilFormat = D3DFMT_D16;
// Just setting up some base values for the parameters
hr = g_d3dObject->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
g_hwnd, flags, &params, &g_d3dDevice);
// Sets the direct3d object to the device that was just created
if(FAILED(hr) || g_d3dDevice == NULL)
return false; // once again, checking if it's all okay and if not -
exiting.

ResizeD3D9Window(WINDOW_WIDTH, WINDOW_HEIGHT); // sets the window to the


initial size

return true;
}

bool InitializeDemo()
{
// sets the device so that their aren't any light sources
g_d3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
// States that there will be no culling
g_d3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);

Vertex obj[] =
{
{-0.3f, -0.3f, 1.0f, D3DCOLOR_XRGB(255, 0, 00)},
{ 0.3f, -0.3f, 1.0f, D3DCOLOR_XRGB(0, 255, 0)},
{ 0.0f, 0.3f, 1.0f, D3DCOLOR_XRGB(0, 0, 255)}
}; // Creates the verticies for our amazing triangle

// Create the vertex buffer.


int numVerts = sizeof(obj) / sizeof(obj[0]);
int size = numVerts * sizeof(Vertex); // simple math to work out how
many verticies and size of memory required
HRESULT hr = g_d3dDevice->CreateVertexBuffer(size, 0,
D3DFVF_D3DVertex, D3DPOOL_DEFAULT, &g_vertexBuffer, NULL);
// Creates the vertex buffer, allocating enough memory to process all
the verticies
if(FAILED(hr)) // checks that the buffer was created correctly
return false;

// Load data into vertex buffer.


Vertex *ptr = NULL;
// locks off the memory required to save the verticies
hr = g_vertexBuffer->Lock(0, sizeof(obj), (void**)&ptr, 0);

if(FAILED(hr))
return false;
// copies the verticies over to the memory
memcpy(ptr, obj, sizeof(obj));
g_vertexBuffer->Unlock(); // unlocks the memory

return true;
}

void Update()
{
// This is where any real-time updates to the scene are done
}

void RenderScene() // This is the function to render the scene to the


direct3d device
{
g_d3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
D3DCOLOR_XRGB(255,255,255), 1.0f, 0);
// clears off the direct3d device
g_d3dDevice->BeginScene(); // now that the screen is cleared, it
prepares
// the screen for more direct3d
issues

// Setup geometry to render.


g_d3dDevice->SetStreamSource(0, g_vertexBuffer,
0, sizeof(Vertex)); // streams the
vertex buffer to the direct3d device

g_d3dDevice->SetFVF(D3DFVF_D3DVertex);

// This draws everything in the buffer.


g_d3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
g_d3dDevice->EndScene(); // now that all the objects are drawn, we end
the scene

g_d3dDevice->Present(NULL, NULL, NULL, NULL); // puts the scene to the


device
}

// release all memory in buffers, device information, and object


information from the program
void Shutdown()
{
if(g_d3dDevice != NULL) g_d3dDevice->Release();
g_d3dDevice = NULL;

if(g_d3dObject != NULL) g_d3dObject->Release();


g_d3dObject = NULL;

if(g_vertexBuffer != NULL) g_vertexBuffer->Release();


g_vertexBuffer = NULL;
}

LRESULT CALLBACK WndProc(HWND g_hwnd, UINT m, WPARAM wp, LPARAM lp)


{
// Window width and height.
int width, height;
switch(m) // the message distribution loop
{
case WM_CLOSE:
case WM_DESTROY: // exit the main program
PostQuitMessage(0);
return 0;
break;

case WM_SIZE: // resize


height = HIWORD(lp);
width = LOWORD(lp);
if(height == 0)
height = 1;

ResizeD3D9Window(width, height);
return 0;
break;

case WM_KEYDOWN: // if any key is pressed


switch(wp) // retrieve the key
{
case VK_ESCAPE:
PostQuitMessage(0); // if it's the escape
key, exit
break;

default:
break;
}
break;

default:
break;
}

// Pass remaining messages to default handler.


return (DefWindowProc(g_hwnd, m, wp, lp));
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prev, LPSTR cmd, int


show)
{ // standard WinMain function for any windows 32 application
MSG msg; // empty message that will be used to send any messages to the
message loop

// Describes a window.
WNDCLASSEX windowClass;
memset(&windowClass, 0, sizeof(WNDCLASSEX));
windowClass.cbSize = sizeof(WNDCLASSEX);
windowClass.style = CS_HREDRAW | CS_VREDRAW;
windowClass.lpfnWndProc = WndProc;
windowClass.hInstance = hInstance;
windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
windowClass.lpszClassName = WINDOW_CLASS;
windowClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
//just some basic variables for the window
if(!RegisterClassEx(&windowClass))
return 0;
// if the class couldn't be registered, exit.

// create the window previously discribed


g_hwnd = CreateWindowEx(NULL, WINDOW_CLASS, WINDOW_NAME,
WS_EX_TOPMOST | WS_POPUP, 0, 0,
WINDOW_WIDTH, WINDOW_HEIGHT, 0, 0, hInstance, NULL);

if(!g_hwnd)
return 0;
// if the window wasn't successfully created, exit
ShowWindow(g_hwnd, SW_SHOW); // Present the window
UpdateWindow(g_hwnd); // Updates the window

// If initialization fails don't run the program.


if(InitializeD3D9() == true)
{
if(InitializeDemo() == true)
{
// Begin the message loop
while(true)
{
if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) // checks if
there is a message
{
// If a quit message then break;
if(msg.message == WM_QUIT) break;
TranslateMessage(&msg); // translate and
dispatch message
DispatchMessage(&msg);
}
else
{
Update(); // if there's no message, update the
scene
RenderScene(); // and render the scene
}
}
}
}

// Release all resources and unregister class.


Shutdown();
UnregisterClass(WINDOW_CLASS, windowClass.hInstance);
// finally return a value because every function has to return a value
return (int)msg.wParam;
}

Das könnte Ihnen auch gefallen