Merge branch 'main' into Axel-G-Input

This commit is contained in:
GolfOcean334
2024-03-29 17:15:14 +01:00
48 changed files with 7240 additions and 315 deletions

View File

@@ -5,14 +5,24 @@ ApplicationClass::ApplicationClass()
m_Direct3D = 0;
m_Camera = 0;
m_MultiTextureShader = 0;
m_AlphaMapShader = 0;
m_Model = 0;
m_LightShader = 0;
m_LightMapShader = 0;
m_Light = 0;
m_TextureShader = 0;
m_Bitmap = 0;
m_Sprite = 0;
m_Timer = 0;
m_MouseStrings = 0;
m_FontShader = 0;
m_Font = 0;
m_TextString1 = 0;
m_TextString2 = 0;
m_TextString3 = 0;
m_Fps = 0;
m_FpsString = 0;
}
@@ -29,10 +39,11 @@ ApplicationClass::~ApplicationClass()
bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
{
char mouseString1[32], mouseString2[32], mouseString3[32];
char modelFilename[128];
char textureFilename1[128], textureFilename2[128];
char testString1[32], testString2[32], testString3[32];
char modelFilename[128], textureFilename1[128], textureFilename2[128], textureFilename3[128];
char bitmapFilename[128];
char spriteFilename[128];
char fpsString[32];
bool result;
@@ -59,9 +70,60 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
}
// Set the initial position of the camera.
m_Camera->SetPosition(0.0f, 0.0f, -10.0f);
m_Camera->SetPosition(0.0f, 0.0f, -12.0f);
m_Camera->SetRotation(0.0f, 0.0f, 0.0f);
// Create and initialize the font shader object.
m_FontShader = new FontShaderClass;
result = m_FontShader->Initialize(m_Direct3D->GetDevice(), hwnd);
if (!result)
{
MessageBox(hwnd, L"Could not initialize the font shader object.", L"Error", MB_OK);
return false;
}
// Create and initialize the font object.
m_Font = new FontClass;
result = m_Font->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), 0);
if (!result)
{
return false;
}
// Set the strings we want to display.
strcpy_s(testString1, "Yo");
strcpy_s(testString2, "Les");
strcpy_s(testString3, "Noobs !");
// Create and initialize the first text object.
m_TextString1 = new TextClass;
result = m_TextString1->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), screenWidth, screenHeight, 32, m_Font, testString1, 25, screenHeight / 2 - m_Font->GetFontHeight(), 1.0f, 1.0f, 1.0f);
if (!result)
{
return false;
}
// Create and initialize the second text object.
m_TextString2 = new TextClass;
result = m_TextString2->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), screenWidth, screenHeight, 32, m_Font, testString2, 250, screenHeight / 2 - m_Font->GetFontHeight(), 0.0f, 1.0f, 1.0f);
if (!result)
{
return false;
}
// Create and initialize the second text object.
m_TextString3 = new TextClass;
result = m_TextString3->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), screenWidth, screenHeight, 32, m_Font, testString3, screenWidth / 2 - m_Font->GetSentencePixelLength(testString3), screenHeight / 2 - m_Font->GetFontHeight(), 1.0f, 1.0f, 0.0f);
if (!result)
{
return false;
}
// Create and initialize the texture shader object.
m_TextureShader = new TextureShaderClass;
@@ -142,16 +204,17 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
}
// Set the file name of the model.
strcpy_s(modelFilename, "sphere.txt");
strcpy_s(modelFilename, "cube.txt");
// Set the file name of the textures.
strcpy_s(textureFilename1, "stone01.tga");
strcpy_s(textureFilename2, "moss01.tga");
strcpy_s(textureFilename2, "dirt01.tga");
strcpy_s(textureFilename3, "alpha01.tga");
// Create and initialize the model object.
m_Model = new ModelClass;
result = m_Model->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), modelFilename, textureFilename1, textureFilename2);
result = m_Model->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), modelFilename, textureFilename1, textureFilename2, textureFilename3);
if (!result)
{
MessageBox(hwnd, L"Could not initialize the model object.", L"Error", MB_OK);
@@ -169,14 +232,63 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
return false;
}
// Create and initialize the light object.
m_Light = new LightClass;
// Set the number of lights we will use.
m_numLights = 4;
// Create and initialize the light objects array.
m_Lights = new LightClass[m_numLights];
// Manually set the color and position of each light.
m_Lights[0].SetDiffuseColor(1.0f, 0.0f, 0.0f, 1.0f); // Red
m_Lights[0].SetPosition(-3.0f, 1.0f, 3.0f);
m_Lights[1].SetDiffuseColor(0.0f, 1.0f, 0.0f, 1.0f); // Green
m_Lights[1].SetPosition(3.0f, 1.0f, 3.0f);
m_Lights[2].SetDiffuseColor(0.0f, 0.0f, 1.0f, 1.0f); // Blue
m_Lights[2].SetPosition(-3.0f, 1.0f, -3.0f);
m_Lights[3].SetDiffuseColor(1.0f, 1.0f, 1.0f, 1.0f); // White
m_Lights[3].SetPosition(3.0f, 1.0f, -3.0f);
// Create and initialize the light map shader object.
m_LightMapShader = new LightMapShaderClass;
result = m_LightMapShader->Initialize(m_Direct3D->GetDevice(), hwnd);
if (!result)
{
MessageBox(hwnd, L"Could not initialize the light map shader object.", L"Error", MB_OK);
return false;
}
// Create and initialize the alpha map shader object.
m_AlphaMapShader = new AlphaMapShaderClass;
result = m_AlphaMapShader->Initialize(m_Direct3D->GetDevice(), hwnd);
if (!result)
{
MessageBox(hwnd, L"Could not initialize the alpha map shader object.", L"Error", MB_OK);
return false;
}
// Create and initialize the fps object.
m_Fps = new FpsClass();
m_Fps->Initialize();
// Set the initial fps and fps string.
m_previousFps = -1;
strcpy_s(fpsString, "Fps: 0");
// Create and initialize the text object for the fps string.
m_FpsString = new TextClass;
result = m_FpsString->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), screenWidth, screenHeight, 32, m_Font, fpsString, 10, 10, 0.0f, 1.0f, 0.0f);
if (!result)
{
return false;
}
m_Light->SetAmbientColor(0.15f, 0.15f, 0.15f, 1.0f);
m_Light->SetDiffuseColor(1.0f, 1.0f, 1.0f, 1.0f);
m_Light->SetDirection(1.0f, 0.0f, 1.0f);
m_Light->SetSpecularColor(1.0f, 1.0f, 1.0f, 1.0f);
m_Light->SetSpecularPower(32.0f);
return true;
}
@@ -193,6 +305,57 @@ void ApplicationClass::Shutdown()
delete[] m_MouseStrings;
m_MouseStrings = 0;
// Release the text object for the fps string.
if (m_FpsString)
{
m_FpsString->Shutdown();
delete m_FpsString;
m_FpsString = 0;
}
// Release the fps object.
if (m_Fps)
{
delete m_Fps;
m_Fps = 0;
}
// Release the text string objects.
if (m_TextString3)
{
m_TextString3->Shutdown();
delete m_TextString3;
m_TextString3 = 0;
}
if (m_TextString2)
{
m_TextString2->Shutdown();
delete m_TextString2;
m_TextString2 = 0;
}
if (m_TextString1)
{
m_TextString1->Shutdown();
delete m_TextString1;
m_TextString1 = 0;
}
// Release the font object.
if (m_Font)
{
m_Font->Shutdown();
delete m_Font;
m_Font = 0;
}
// Release the font shader object.
if (m_FontShader)
{
m_FontShader->Shutdown();
delete m_FontShader;
m_FontShader = 0;
}
// Release the timer object.
@@ -210,12 +373,12 @@ void ApplicationClass::Shutdown()
m_Sprite = 0;
}
// Release the light object.
if (m_Light)
{
delete m_Light;
m_Light = 0;
}
// Release the light objects.
if(m_Lights)
{
delete [] m_Lights;
m_Lights = 0;
}
// Release the light shader object.
if (m_LightShader)
@@ -233,6 +396,14 @@ void ApplicationClass::Shutdown()
m_LightShader = 0;
}
// Release the light map shader object.
if (m_LightMapShader)
{
m_LightMapShader->Shutdown();
delete m_LightMapShader;
m_LightMapShader = 0;
}
// Release the model object.
if (m_Model)
{
@@ -280,6 +451,14 @@ void ApplicationClass::Shutdown()
return;
}
// Release the alpha map shader object.
if (m_AlphaMapShader)
{
m_AlphaMapShader->Shutdown();
delete m_AlphaMapShader;
m_AlphaMapShader = 0;
}
}
@@ -290,31 +469,35 @@ bool ApplicationClass::Frame(InputClass* Input)
float frameTime;
static float rotation = 0.0f;
static float x = 2.f;
static float y = 0.f;
static float x = 6.f;
static float y = 3.f;
static float z = 0.f;
bool result;
// Check if the user pressed escape and wants to exit the application.
if (Input->IsEscapePressed())
// Update the frames per second each frame.
result = UpdateFps();
if (!result)
{
return false;
}
// Update the rotation variable each frame.
rotation -= 0.0174532925f * 0.1f;
rotation -= 0.0174532925f * 0.8f;
if (rotation < 0.0f)
{
rotation += 360.0f;
}
// Update the x position variable each frame.
x -= 0.0174532925f * 0.54672f;
x -= 0.0174532925f * 0.6f;
y -= 0.0174532925f * 0.8972f;
y -= 0.0174532925f * 0.2f;
// Update the z position variable each frame.
z -= 0.0174532925f * 0.8972f;
z -= 0.0174532925f * 0.2f;
// Render the graphics scene.
@@ -340,6 +523,8 @@ bool ApplicationClass::Frame(InputClass* Input)
bool ApplicationClass::Render(float rotation, float x, float y, float z)
{
XMMATRIX worldMatrix, viewMatrix, orthoMatrix, projectionMatrix, rotateMatrix, translateMatrix, scaleMatrix, srMatrix;
XMFLOAT4 diffuseColor[4], lightPosition[4];
int i;
bool result;
// Clear the buffers to begin the scene.
@@ -354,8 +539,49 @@ bool ApplicationClass::Render(float rotation, float x, float y, float z)
m_Direct3D->GetProjectionMatrix(projectionMatrix);
m_Direct3D->GetOrthoMatrix(orthoMatrix);
// Turn off the Z buffer to begin all 2D rendering.
// Disable the Z buffer and enable alpha blending for 2D rendering.
m_Direct3D->TurnZBufferOff();
m_Direct3D->EnableAlphaBlending();
// Render the fps text string using the font shader.
m_FpsString->Render(m_Direct3D->GetDeviceContext());
result = m_FontShader->Render(m_Direct3D->GetDeviceContext(), m_FpsString->GetIndexCount(), worldMatrix, viewMatrix, orthoMatrix,
m_Font->GetTexture(), m_FpsString->GetPixelColor());
if (!result)
{
return false;
}
// Render the first text string using the font shader.
m_TextString1->Render(m_Direct3D->GetDeviceContext());
result = m_FontShader->Render(m_Direct3D->GetDeviceContext(), m_TextString1->GetIndexCount(), worldMatrix, viewMatrix, orthoMatrix,
m_Font->GetTexture(), m_TextString1->GetPixelColor());
if (!result)
{
return false;
}
// Render the second text string using the font shader.
m_TextString2->Render(m_Direct3D->GetDeviceContext());
result = m_FontShader->Render(m_Direct3D->GetDeviceContext(), m_TextString2->GetIndexCount(), worldMatrix, viewMatrix, orthoMatrix,
m_Font->GetTexture(), m_TextString2->GetPixelColor());
if (!result)
{
return false;
}
// Render the second text string using the font shader.
m_TextString3->Render(m_Direct3D->GetDeviceContext());
result = m_FontShader->Render(m_Direct3D->GetDeviceContext(), m_TextString3->GetIndexCount(), worldMatrix, viewMatrix, orthoMatrix,
m_Font->GetTexture(), m_TextString3->GetPixelColor());
if (!result)
{
return false;
}
// Render the mouse text strings using the font shader.
for (i = 0; i < 3; i++)
@@ -400,13 +626,17 @@ bool ApplicationClass::Render(float rotation, float x, float y, float z)
return false;
}
// Get the light properties.
for (i = 0; i < m_numLights; i++)
{
// Create the diffuse color array from the four light colors.
diffuseColor[i] = m_Lights[i].GetDiffuseColor();
// Create the light position array from the four light positions.
lightPosition[i] = m_Lights[i].GetPosition();
}
// 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.
scaleMatrix = XMMatrixScaling(0.75f, 0.75f, 0.75f); // Build the scaling matrix.
rotateMatrix = XMMatrixRotationY(rotation); // Build the rotation matrix.
translateMatrix = XMMatrixTranslation(x, y, z); // Build the translation matrix.
@@ -417,28 +647,42 @@ bool ApplicationClass::Render(float rotation, float x, float y, float z)
// Render the model using the multitexture shader.
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(),
m_Camera->GetPosition(), m_Light->GetSpecularColor(), m_Light->GetSpecularPower());
// Lighting, utilise plusieurs lights donc Multiple Points Lighting
//result = m_LightShader->Render(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, m_Model->GetTexture(0),
// diffuseColor, lightPosition);
//if (!result)
//{
// return false;
//}
// Lightmapping, utiliser light01.tga en deuxieme texture
//result = m_LightMapShader->Render(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix,
// m_Model->GetTexture(0), m_Model->GetTexture(1));
//if (!result)
//{
// return false;
//}
// MultiTexturing
//result = m_MultiTextureShader->Render(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix,
// m_Model->GetTexture(0), m_Model->GetTexture(1));
//if (!result)
//{
// return false;
//}
// Alphamapping
result = m_AlphaMapShader->Render(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix,
m_Model->GetTexture(0), m_Model->GetTexture(1), m_Model->GetTexture(2));
if (!result)
{
return false;
}
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());
// Turn the Z buffer back on now that all 2D rendering has completed.
// Enable the Z buffer and disable alpha blending now that 2D rendering is complete.
m_Direct3D->TurnZBufferOn();
m_Direct3D->DisableAlphaBlending();
// Present the rendered scene to the screen.
m_Direct3D->EndScene();
@@ -492,12 +736,73 @@ bool ApplicationClass::UpdateMouseStrings(int mouseX, int mouseY, bool mouseDown
// Update the sentence vertex buffer with the new string information.
result = m_MouseStrings[2].UpdateText(m_Direct3D->GetDeviceContext(), m_Font, finalString, 10, 60, 1.0f, 1.0f, 1.0f);
bool ApplicationClass::UpdateFps()
{
int fps;
char tempString[16], finalString[16];
float red, green, blue;
bool result;
// Update the fps each frame.
m_Fps->Frame();
// Get the current fps.
fps = m_Fps->GetFps();
// Check if the fps from the previous frame was the same, if so don't need to update the text string.
if (m_previousFps == fps)
{
return true;
}
// Store the fps for checking next frame.
m_previousFps = fps;
// Truncate the fps to below 100,000.
if (fps > 99999)
{
fps = 99999;
}
// Convert the fps integer to string format.
sprintf_s(tempString, "%d", fps);
// Setup the fps string.
strcpy_s(finalString, "Fps: ");
strcat_s(finalString, tempString);
// If fps is 60 or above set the fps color to green.
if (fps >= 60)
{
red = 0.0f;
green = 1.0f;
blue = 0.0f;
}
// If fps is below 60 set the fps color to yellow.
if (fps < 60)
{
red = 1.0f;
green = 1.0f;
blue = 0.0f;
}
// If fps is below 30 set the fps color to red.
if (fps < 30)
{
red = 1.0f;
green = 0.0f;
blue = 0.0f;
}
// Update the sentence vertex buffer with the new string information.
result = m_FpsString->UpdateText(m_Direct3D->GetDeviceContext(), m_Font, finalString, 10, 10, red, green, blue);
if (!result)
{
return false;
}
return true;
}
}