Merge branch 'Sprites'
This commit is contained in:
@@ -8,6 +8,10 @@ ApplicationClass::ApplicationClass()
|
||||
m_Model = 0;
|
||||
m_LightShader = 0;
|
||||
m_Light = 0;
|
||||
m_TextureShader = 0;
|
||||
m_Bitmap = 0;
|
||||
m_Sprite = 0;
|
||||
m_Timer = 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -23,7 +27,10 @@ ApplicationClass::~ApplicationClass()
|
||||
|
||||
bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
|
||||
{
|
||||
char modelFilename[128], textureFilename1[128], textureFilename2[128];
|
||||
char modelFilename[128];
|
||||
char textureFilename1[128], textureFilename2[128];
|
||||
char bitmapFilename[128];
|
||||
char spriteFilename[128];
|
||||
bool result;
|
||||
|
||||
|
||||
@@ -50,8 +57,51 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
|
||||
}
|
||||
|
||||
// Set the initial position of the camera.
|
||||
m_Camera->SetPosition(0.0f, 0.0f, -5.0f);
|
||||
m_Camera->SetRotation(0.0f, 0.0f, 10.0f);
|
||||
m_Camera->SetPosition(0.0f, 0.0f, -10.0f);
|
||||
m_Camera->SetRotation(0.0f, 0.0f, 0.0f);
|
||||
|
||||
// Create and initialize the texture shader object.
|
||||
m_TextureShader = new TextureShaderClass;
|
||||
|
||||
result = m_TextureShader->Initialize(m_Direct3D->GetDevice(), hwnd);
|
||||
if (!result)
|
||||
{
|
||||
MessageBox(hwnd, L"Could not initialize the texture shader object.", L"Error", MB_OK);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set the sprite info file we will be using.
|
||||
strcpy_s(spriteFilename, "sprite_data_01.txt");
|
||||
|
||||
// Create and initialize the sprite object.
|
||||
m_Sprite = new SpriteClass;
|
||||
|
||||
result = m_Sprite->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), screenWidth, screenHeight, spriteFilename, 50, 50);
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create and initialize the timer object.
|
||||
m_Timer = new TimerClass;
|
||||
|
||||
result = m_Timer->Initialize();
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set the file name of the bitmap file.
|
||||
strcpy_s(bitmapFilename, "stone01.tga");
|
||||
|
||||
// Create and initialize the bitmap object.
|
||||
m_Bitmap = new BitmapClass;
|
||||
|
||||
result = m_Bitmap->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), screenWidth, screenHeight, bitmapFilename, 50, 50);
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create and initialize the multitexture shader object.
|
||||
m_MultiTextureShader = new MultiTextureShaderClass;
|
||||
@@ -90,6 +140,7 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
|
||||
MessageBox(hwnd, L"Could not initialize the light shader object.", L"Error", MB_OK);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create and initialize the light object.
|
||||
m_Light = new LightClass;
|
||||
|
||||
@@ -103,6 +154,21 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
|
||||
|
||||
void ApplicationClass::Shutdown()
|
||||
{
|
||||
// Release the timer object.
|
||||
if (m_Timer)
|
||||
{
|
||||
delete m_Timer;
|
||||
m_Timer = 0;
|
||||
}
|
||||
|
||||
// Release the sprite object.
|
||||
if (m_Sprite)
|
||||
{
|
||||
m_Sprite->Shutdown();
|
||||
delete m_Sprite;
|
||||
m_Sprite = 0;
|
||||
}
|
||||
|
||||
// Release the light object.
|
||||
if (m_Light)
|
||||
{
|
||||
@@ -118,6 +184,14 @@ void ApplicationClass::Shutdown()
|
||||
m_LightShader = 0;
|
||||
}
|
||||
|
||||
// Release the light shader object.
|
||||
if (m_LightShader)
|
||||
{
|
||||
m_LightShader->Shutdown();
|
||||
delete m_LightShader;
|
||||
m_LightShader = 0;
|
||||
}
|
||||
|
||||
// Release the model object.
|
||||
if (m_Model)
|
||||
{
|
||||
@@ -132,33 +206,51 @@ void ApplicationClass::Shutdown()
|
||||
m_MultiTextureShader->Shutdown();
|
||||
delete m_MultiTextureShader;
|
||||
m_MultiTextureShader = 0;
|
||||
}
|
||||
// Release the bitmap object.
|
||||
if (m_Bitmap)
|
||||
{
|
||||
m_Bitmap->Shutdown();
|
||||
delete m_Bitmap;
|
||||
m_Bitmap = 0;
|
||||
}
|
||||
|
||||
// Release the camera object.
|
||||
if (m_Camera)
|
||||
{
|
||||
delete m_Camera;
|
||||
m_Camera = 0;
|
||||
}
|
||||
// Release the texture shader object.
|
||||
if (m_TextureShader)
|
||||
{
|
||||
m_TextureShader->Shutdown();
|
||||
delete m_TextureShader;
|
||||
m_TextureShader = 0;
|
||||
}
|
||||
|
||||
// Release the D3D object.
|
||||
if (m_Direct3D)
|
||||
{
|
||||
m_Direct3D->Shutdown();
|
||||
delete m_Direct3D;
|
||||
m_Direct3D = 0;
|
||||
}
|
||||
// Release the camera object.
|
||||
if (m_Camera)
|
||||
{
|
||||
delete m_Camera;
|
||||
m_Camera = 0;
|
||||
}
|
||||
|
||||
return;
|
||||
// Release the D3D object.
|
||||
if (m_Direct3D)
|
||||
{
|
||||
m_Direct3D->Shutdown();
|
||||
delete m_Direct3D;
|
||||
m_Direct3D = 0;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool ApplicationClass::Frame()
|
||||
{
|
||||
float frameTime;
|
||||
static float rotation = 0.0f;
|
||||
static float x = 2.f;
|
||||
static float y = 0.f;
|
||||
static float z = 0.f;
|
||||
bool result;
|
||||
|
||||
|
||||
// Update the rotation variable each frame.
|
||||
rotation -= 0.0174532925f * 0.1f;
|
||||
if (rotation < 0.0f)
|
||||
@@ -166,20 +258,38 @@ bool ApplicationClass::Frame()
|
||||
rotation += 360.0f;
|
||||
}
|
||||
|
||||
// Update the x position variable each frame.
|
||||
x -= 0.0174532925f * 0.54672f;
|
||||
|
||||
y -= 0.0174532925f * 0.8972f;
|
||||
|
||||
// Update the z position variable each frame.
|
||||
z -= 0.0174532925f * 0.8972f;
|
||||
|
||||
|
||||
// Render the graphics scene.
|
||||
result = Render(rotation);
|
||||
result = Render(rotation, x, y, z);
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Update the system stats.
|
||||
m_Timer->Frame();
|
||||
|
||||
// Get the current frame time.
|
||||
frameTime = m_Timer->GetTime();
|
||||
|
||||
// Update the sprite object using the frame time.
|
||||
m_Sprite->Update(frameTime);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool ApplicationClass::Render(float rotation)
|
||||
bool ApplicationClass::Render(float rotation, float x, float y, float z)
|
||||
{
|
||||
XMMATRIX worldMatrix, viewMatrix, projectionMatrix;
|
||||
XMMATRIX worldMatrix, viewMatrix, orthoMatrix, projectionMatrix, rotateMatrix, translateMatrix, scaleMatrix, srMatrix;
|
||||
bool result;
|
||||
|
||||
// Clear the buffers to begin the scene.
|
||||
@@ -192,8 +302,54 @@ bool ApplicationClass::Render(float rotation)
|
||||
m_Direct3D->GetWorldMatrix(worldMatrix);
|
||||
m_Camera->GetViewMatrix(viewMatrix);
|
||||
m_Direct3D->GetProjectionMatrix(projectionMatrix);
|
||||
// Rotate the world matrix by the rotation value so that the triangle will spin.
|
||||
worldMatrix = XMMatrixRotationY(rotation);
|
||||
m_Direct3D->GetOrthoMatrix(orthoMatrix);
|
||||
|
||||
// Turn off the Z buffer to begin all 2D rendering.
|
||||
m_Direct3D->TurnZBufferOff();
|
||||
|
||||
// Put the sprite vertex and index buffers on the graphics pipeline to prepare them for drawing.
|
||||
result = m_Sprite->Render(m_Direct3D->GetDeviceContext());
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Render the sprite with the texture shader.
|
||||
result = m_TextureShader->Render(m_Direct3D->GetDeviceContext(), m_Sprite->GetIndexCount(), worldMatrix, viewMatrix, orthoMatrix, m_Sprite->GetTexture());
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Put the bitmap vertex and index buffers on the graphics pipeline to prepare them for drawing.
|
||||
result = m_Bitmap->Render(m_Direct3D->GetDeviceContext());
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
m_Bitmap->SetRenderLocation(1200, 50);
|
||||
|
||||
// Render the bitmap with the texture shader.
|
||||
result = m_TextureShader->Render(m_Direct3D->GetDeviceContext(), m_Bitmap->GetIndexCount(), worldMatrix, viewMatrix, orthoMatrix, m_Bitmap->GetTexture());
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Render the model using the multitexture shader.
|
||||
result = m_MultiTextureShader->Render(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix,
|
||||
m_Model->GetTexture(0), m_Model->GetTexture(1));
|
||||
|
||||
scaleMatrix = XMMatrixScaling(0.5f, 0.5f, 0.5f); // Build the scaling matrix.
|
||||
rotateMatrix = XMMatrixRotationY(rotation); // Build the rotation matrix.
|
||||
translateMatrix = XMMatrixTranslation(x, y, z); // Build the translation matrix.
|
||||
|
||||
// Multiply the scale, rotation, and translation matrices together to create the final world transformation matrix.
|
||||
srMatrix = XMMatrixMultiply(scaleMatrix, rotateMatrix);
|
||||
worldMatrix = XMMatrixMultiply(srMatrix, translateMatrix);
|
||||
|
||||
// Render the model using the multitexture shader.
|
||||
m_Model->Render(m_Direct3D->GetDeviceContext());
|
||||
@@ -206,14 +362,28 @@ bool ApplicationClass::Render(float rotation)
|
||||
return false;
|
||||
}
|
||||
|
||||
// Render the model using the multitexture shader.
|
||||
result = m_MultiTextureShader->Render(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix,
|
||||
m_Model->GetTexture(0), m_Model->GetTexture(1));
|
||||
scaleMatrix = XMMatrixScaling(2.0f, 2.0f, 2.0f); // Build the scaling matrix.
|
||||
rotateMatrix = XMMatrixRotationY(-rotation); // Build the rotation matrix.
|
||||
translateMatrix = XMMatrixTranslation(-x, -y, -z); // Build the translation matrix.
|
||||
|
||||
// Multiply the scale, rotation, and translation matrices together to create the final world transformation matrix.
|
||||
srMatrix = XMMatrixMultiply(scaleMatrix, rotateMatrix);
|
||||
worldMatrix = XMMatrixMultiply(srMatrix, translateMatrix);
|
||||
|
||||
// Put the model vertex and index buffers on the graphics pipeline to prepare them for drawing.
|
||||
m_Model->Render(m_Direct3D->GetDeviceContext());
|
||||
|
||||
// Render the model using the light shader.
|
||||
result = m_LightShader->Render(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, m_Model->GetTexture(0),
|
||||
m_Light->GetDirection(), m_Light->GetAmbientColor(), m_Light->GetDiffuseColor());
|
||||
if (!result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Turn the Z buffer back on now that all 2D rendering has completed.
|
||||
m_Direct3D->TurnZBufferOn();
|
||||
|
||||
// Present the rendered scene to the screen.
|
||||
m_Direct3D->EndScene();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user