Patch - Simplifie la gestion des erreurs - V14.6.1

Remplace les blocs if/else par une macro R_LOG_ERROR pour une gestion plus concise et uniforme des erreurs lors de l'initialisation des objets.

Améliore la lisibilité du code et réduit la duplication.
This commit is contained in:
2025-10-14 14:12:34 +02:00
parent ba7d0ca27e
commit ebbbd181e3

View File

@@ -103,11 +103,7 @@ bool application_class::initialize(int screenWidth, int screenHeight, HWND hwnd,
}
result = direct_3d_->initialize(screen_width_, screen_height_, vsync_enabled_, hwnd, full_screen, screen_depth, screen_near);
if (!result)
{
LOG_ERROR("Could not initialize Direct3D");
R_FALSE
}
R_LOG_ERROR(result, "Could not initialize Direct3D")
// Create the camera object.
camera_ = new camera_class;
@@ -144,67 +140,34 @@ bool application_class::initialize(int screenWidth, int screenHeight, HWND hwnd,
font_shader_ = new font_shader_class;
result = font_shader_->initialize(direct_3d_->get_device(), hwnd);
if (!result)
{
LOG_ERROR("Could not initialize the font shader object");
R_FALSE
}
R_LOG_ERROR(result , "Could not initialize the font shader object")
// Create and initialize the font object.
font_ = new font_class;
result = font_->Initialize(direct_3d_->get_device(), direct_3d_->get_device_context(), 0);
if (!result)
{
LOG_ERROR("Could not initialize the font object");
R_FALSE
}
R_LOG_ERROR(result, "Could not initialize the font object")
// Create and initialize the render to texture object.
render_texture_ = new render_texture_class;
result = render_texture_->Initialize(direct_3d_->get_device(), 256, 256, screen_depth, screen_near, 1);
if (!result)
{
LOG_ERROR("Could not initialize the render texture object");
R_FALSE
}
//ImVec2 availableSize = ImGui::GetContentRegionAvail();
// Create and initialize the scene render to texture object.
scene_texture_ = new render_texture_class();
result = scene_texture_->Initialize(direct_3d_->get_device(), 256, 256, screen_depth, screen_near, 1);
if (!result)
{
LOG_ERROR("Could not initialize the render texture object");
R_FALSE
}
R_LOG_ERROR(result , "Could not initialize the render texture object")
// Create and initialize the display plane object.
display_plane_ = new display_plane_class;
result = display_plane_->Initialize(direct_3d_->get_device(), 1.0f, 1.0f);
if (!result)
{
LOG_ERROR("Could not initialize the display plane object");
R_FALSE
}
R_LOG_ERROR(result, "Could not initialize the display plane object")
// Set the sprite info file we will be using.
//
strcpy_s(spriteFilename, "sprite_data_01.txt");
// Create and initialize the sprite object.
sprite_ = new sprite_class;
result = sprite_->Initialize(direct_3d_->get_device(), direct_3d_->get_device_context(), screenWidth, screenHeight, spriteFilename, 50, 50);
if (!result)
{
LOG_ERROR("Could not initialize the sprite object");
R_FALSE
}
R_LOG_ERROR(result, "Could not initialize the sprite object")
// Set the initial mouse strings.
strcpy_s(mouseString1, "Mouse X: 0");
@@ -218,11 +181,7 @@ bool application_class::initialize(int screenWidth, int screenHeight, HWND hwnd,
{
int y = 10 + (i * 25);
result = mouse_strings_[i].Initialize(direct_3d_->get_device(), direct_3d_->get_device_context(), screenWidth, screenHeight, 32, font_, mouseString1, 10, y, 1.0f, 1.0f, 1.0f);
if (!result)
{
LOG_ERROR("Could not initialize the mouse strings");
R_FALSE
}
R_LOG_ERROR(result , "Could not initialize the mouse string " + std::to_string(i))
}
// Set the file name of the bitmap file.
@@ -232,11 +191,7 @@ bool application_class::initialize(int screenWidth, int screenHeight, HWND hwnd,
bitmap_ = new bitmap_class;
result = bitmap_->initialize(direct_3d_->get_device(), direct_3d_->get_device_context(), screenWidth, screenHeight, bitmapFilename, 50, 50);
if (!result)
{
LOG_ERROR("Could not initialize the bitmap object");
R_FALSE
}
R_LOG_ERROR(result, "Could not initialize the bitmap object")
// Set the file name of the model.
strcpy_s(modelFilename, "assets/Model/TXT/cube.txt");
@@ -265,11 +220,7 @@ bool application_class::initialize(int screenWidth, int screenHeight, HWND hwnd,
result = model_->Initialize(direct_3d_->get_device(), direct_3d_->get_device_context(), modelFilename, CubeTextures);
if (!result)
{
LOG_ERROR("Could not initialize the model object");
R_FALSE
}
R_LOG_ERROR(result, "Could not initialize the model object")
// Create and initialize the light object.
m_light_ = new light_class;
@@ -336,11 +287,7 @@ bool application_class::initialize(int screenWidth, int screenHeight, HWND hwnd,
shader_manager_ = new shader_manager_class;
result = shader_manager_->initialize(direct_3d_->get_device(), hwnd);
if (!result)
{
LOG_ERROR("Could not initialize the shader manager object");
R_FALSE
}
R_LOG_ERROR(result, "Could not initialize the shader manager object")
// Set the initial render count string.
strcpy_s(renderString, "render Count: 0");
@@ -349,11 +296,7 @@ bool application_class::initialize(int screenWidth, int screenHeight, HWND hwnd,
render_count_string_ = new text_class;
result = render_count_string_->Initialize(direct_3d_->get_device(), direct_3d_->get_device_context(), screenWidth, screenHeight, 32, font_, renderString, 10, 10, 1.0f, 1.0f, 1.0f);
if (!result)
{
LOG_ERROR("Could not initialize the render count string");
R_FALSE
}
R_LOG_ERROR(result, "Could not initialize the render count string")
// Create and initialize the model list object.
model_list_ = new ModelListClass;
@@ -386,11 +329,7 @@ bool application_class::initialize(int screenWidth, int screenHeight, HWND hwnd,
bath_model_ = new model_class;
result = bath_model_->Initialize(direct_3d_->get_device(), direct_3d_->get_device_context(), modelFilename, BathTextures);
if (!result)
{
MessageBox(hwnd, L"Could not initialize the bath model object.", L"Error", MB_OK);
R_FALSE
}
R_LOG_ERROR(result, "Could not initialize the bath model object")
// Set the file names of the water model.
strcpy_s(modelFilename, "assets/Model/TXT/water.txt");
@@ -418,31 +357,19 @@ bool application_class::initialize(int screenWidth, int screenHeight, HWND hwnd,
water_model_ = new model_class;
result = water_model_->Initialize(direct_3d_->get_device(), direct_3d_->get_device_context(), modelFilename, WaterTextures);
if (!result)
{
MessageBox(hwnd, L"Could not initialize the water model object.", L"Error", MB_OK);
R_FALSE
}
R_LOG_ERROR(result, "Could not initialize the water model object")
// Create and initialize the refraction render to texture object.
refraction_texture_ = new render_texture_class;
result = refraction_texture_->Initialize(direct_3d_->get_device(), screenWidth, screenHeight, screen_depth, screen_near, 1);
if (!result)
{
MessageBox(hwnd, L"Could not initialize the refraction render texture object.", L"Error", MB_OK);
R_FALSE
}
R_LOG_ERROR(result, "Could not initialize the refraction render texture object")
// Create and initialize the reflection render to texture object.
reflection_texture_ = new render_texture_class;
result = reflection_texture_->Initialize(direct_3d_->get_device(), screenWidth, screenHeight, screen_depth, screen_near, 1);
if (!result)
{
MessageBox(hwnd, L"Could not initialize the reflection render texture object.", L"Error", MB_OK);
R_FALSE
}
R_LOG_ERROR(result, "Could not initialize the reflection render texture object")
// Set the height of the water.
water_height_ = -9.25f;
@@ -454,11 +381,7 @@ bool application_class::initialize(int screenWidth, int screenHeight, HWND hwnd,
timer_ = new timer_class;
result = timer_->Initialize();
if (!result)
{
LOG_ERROR("Could not initialize the timer object");
R_FALSE
}
R_LOG_ERROR(result, "Could not initialize the timer object")
// Create the position class object.
position_ = new position_class;
@@ -476,11 +399,7 @@ bool application_class::initialize(int screenWidth, int screenHeight, HWND hwnd,
fps_string_ = new text_class;
result = fps_string_->Initialize(direct_3d_->get_device(), direct_3d_->get_device_context(), screenWidth, screenHeight, 32, font_, fpsString, 10, 10, 0.0f, 1.0f, 0.0f);
if (!result)
{
LOG_ERROR("Could not initialize the fps string");
R_FALSE
}
R_LOG_ERROR(result, "Could not initialize the fps string")
shadow_map_ = new shadow_map();
if (!shadow_map_->initialize(direct_3d_->get_device(), 2048, 2048))
@@ -532,11 +451,7 @@ bool application_class::initialize(int screenWidth, int screenHeight, HWND hwnd,
// create the render textures for the shadow map
shadow_texture_ = new render_texture_class();
result = shadow_texture_->Initialize(direct_3d_->get_device(), 2048, 2048, screen_depth, screen_near, 1);
if (!result)
{
LOG_ERROR("Could not initialize the shadow map render texture object");
R_FALSE
}
R_LOG_ERROR(result, "Could not initialize the shadow render texture object")
}
catch (const std::exception& e)
@@ -885,21 +800,16 @@ bool application_class::frame(input_class* Input)
active_camera_->render();
entity_manager_->UpdateEntities(frameTime);
result = render_shadow_map();
R_LOG_ERROR(result, "Could not render the shadow map")
// render the static graphics scene.
result = render(rotation, x, y, z, textureTranslation);
if (!result)
{
LOG_ERROR("Could not render the graphics scene");
R_FALSE
}
R_LOG_ERROR(result, "Could not render the scene")
// Update the frames per second each frame.
result = update_fps();
if (!result)
{
LOG_ERROR("Could not update the frames per second");
R_FALSE
}
R_LOG_ERROR(result, "Could not update the fps")
// Update the rotation variable each frame.
rotation -= 0.0174532925f * speed_;
@@ -917,19 +827,11 @@ bool application_class::frame(input_class* Input)
// render the refraction of the scene to a texture.
result = render_refraction_to_texture();
if (!result)
{
LOG_ERROR("Could not render the refraction texture");
R_FALSE
}
R_LOG_ERROR(result, "Could not render the refraction texture")
// render the reflection of the scene to a texture.
result = render_reflection_to_texture();
if (!result)
{
LOG_ERROR("Could not render the reflection texture");
R_FALSE
}
R_LOG_ERROR(result, "Could not render the reflection texture")
inputs_.key_left = Input->IsLeftArrowPressed();
inputs_.key_right = Input->IsRightArrowPressed();
@@ -938,22 +840,11 @@ bool application_class::frame(input_class* Input)
// render the scene to a render texture.
result = render_scene_to_texture(rotation);
if (!result)
{
LOG_ERROR("Could not render the scene to the render texture");
R_FALSE
}
result = render_shadow_map();
R_LOG_ERROR(result, "Could not render the shadow map")
R_LOG_ERROR(result, "Could not render the scene to the texture")
// Update the mouse strings each frame.
result = update_mouse_strings(mouseX, mouseY, leftMouseDown);
if (!result)
{
LOG_ERROR("Could not update the mouse strings");
R_FALSE
}
R_LOG_ERROR(result, "Could not update the mouse strings")
// Update the sprite object using the frame time.
sprite_->Update(frameTime);
@@ -1009,11 +900,7 @@ bool application_class::render_refraction_to_texture()
result = shader_manager_->render_refraction_shader(direct_3d_->get_device_context(), bath_model_->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix,
bath_model_->GetTexture(TextureType::Diffuse,0), lights_[0]->GetDirection(), ambientColor, diffuseColor, lightPosition, clipPlane);
if (!result)
{
LOG_ERROR("Could not render the bath model using the refraction shader");
R_FALSE
}
R_LOG_ERROR(result, "Could not render the bath model using the refraction shader")
// Reset the render target back to the original back buffer and not the render to texture anymore. And reset the viewport back to the original.
direct_3d_->set_back_buffer_render_target();
@@ -1125,11 +1012,7 @@ bool application_class::render_scene_to_texture(float rotation)
result = shader_manager_->render_texture_shader(direct_3d_->get_device_context(), model_->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix,
model_->GetTexture(TextureType::Diffuse,0));
if (!result)
{
LOG_ERROR("Could not render the model using the texture shader");
R_FALSE
}
R_LOG_ERROR(result, "Could not render the model using the texture shader")
// Reset the render target back to the original back buffer and not the render to texture anymore. And reset the viewport back to the original.
direct_3d_->set_back_buffer_render_target();
@@ -1149,7 +1032,6 @@ bool application_class::render(float rotation, float x, float y, float z, float
active_camera_->render();
// Get the world, view, and projection matrices from the camera and d3d objects.
worldMatrix = direct_3d_->get_world_matrix();
active_camera_->get_view_matrix(viewMatrix);
projectionMatrix = direct_3d_->get_projection_matrix();
orthoMatrix = direct_3d_->get_ortho_matrix();
@@ -1167,19 +1049,6 @@ bool application_class::render(float rotation, float x, float y, float z, float
ambientColor[i] = lights_[i]->GetPosition();
}
// Redimensionner la texture de rendu si n<>cessaire
if (DEBUG_MODE)
{
if ((float)scene_texture_->GetTextureWidth() != window_size_.x || (float)scene_texture_->GetTextureHeight() != window_size_.y)
{
scene_texture_->Shutdown();
scene_texture_->Initialize(direct_3d_->get_device(), (int)window_size_.x, (int)window_size_.y, screen_depth, screen_near, 1);
}
direct_3d_->set_back_buffer_render_target();
direct_3d_->reset_viewport();
}
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.
@@ -1194,6 +1063,7 @@ bool application_class::render(float rotation, float x, float y, float z, float
// render the model using the light shader.
result = shader_manager_->renderlight_shader(direct_3d_->get_device_context(), model_->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, model_->GetTexture(TextureType::Diffuse,0),
diffuseColor, lightPosition, ambientColor);
R_LOG_ERROR(result, "Could not render the model using the light shader")
update_skybox_position(); // Update the position of the skybox to match the camera position.
@@ -1204,19 +1074,11 @@ bool application_class::render(float rotation, float x, float y, float z, float
// -------------------------------------------------------- //
result = render_pass(diffuseColor, lightPosition, ambientColor, viewMatrix, projectionMatrix);
if (!result)
{
LOG_ERROR("Could not render the model using any shader");
R_FALSE
}
R_LOG_ERROR(result, "Could not perform the render pass")
// Update the render count text.
result = update_render_count_string(get_render_count());
if (!result)
{
LOG_ERROR("Could not update the render count string");
R_FALSE
}
R_LOG_ERROR(result, "Could not update the render count string")
// Translate to where the bath model will be rendered.
@@ -1228,11 +1090,7 @@ bool application_class::render(float rotation, float x, float y, float z, float
// render the bath model using the light shader.
result = shader_manager_->renderlight_shader(direct_3d_->get_device_context(), bath_model_->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix,
bath_model_->GetTexture(TextureType::Diffuse,0), diffuseColor, lightPosition, ambientColor);
if (!result)
{
LOG_ERROR("Bath model could not be rendered using the light shader");
R_FALSE
}
R_LOG_ERROR(result, "Could not render the bath model using the light shader")
// Get the camera reflection view matrix.
camera_->get_reflection_view_matrix(reflectionMatrix);
@@ -1247,10 +1105,7 @@ bool application_class::render(float rotation, float x, float y, float z, float
result = shader_manager_->render_water_shader(direct_3d_->get_device_context(), water_model_->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, reflectionMatrix,
reflection_texture_->GetShaderResourceView(), refraction_texture_->GetShaderResourceView(), water_model_->GetTexture(TextureType::Diffuse,0),
water_translation_, 0.01f);
if (!result)
{
R_FALSE
}
R_LOG_ERROR(result, "Could not render the water model using the water shader")
// Disable the Z buffer and enable alpha blending for 2D rendering.
direct_3d_->turn_z_buffer_off();
@@ -1264,22 +1119,14 @@ bool application_class::render(float rotation, float x, float y, float z, float
result = font_shader_->render(direct_3d_->get_device_context(), render_count_string_->GetIndexCount(), worldMatrix, base_view_matrix_, orthoMatrix,
font_->GetTexture(), render_count_string_->GetPixelColor());
if (!result)
{
LOG_ERROR("Could not render the render count text string using the font shader");
R_FALSE
}
R_LOG_ERROR(result, "Could not render the render count text string using the font shader")
// render the fps text string using the font shader.
fps_string_->Render(direct_3d_->get_device_context());
result = font_shader_->render(direct_3d_->get_device_context(), fps_string_->GetIndexCount(), worldMatrix, base_view_matrix_, orthoMatrix,
font_->GetTexture(), fps_string_->GetPixelColor());
if (!result)
{
LOG_ERROR("Could not render the fps text string using the font shader");
R_FALSE
}
R_LOG_ERROR(result, "Could not render the fps text string using the font shader")
// render the mouse text strings using the font shader.
for (i = 0; i < 3; i++)
@@ -1288,28 +1135,17 @@ bool application_class::render(float rotation, float x, float y, float z, float
result = font_shader_->render(direct_3d_->get_device_context(), mouse_strings_[i].GetIndexCount(), worldMatrix, base_view_matrix_, orthoMatrix,
font_->GetTexture(), mouse_strings_[i].GetPixelColor());
if (!result)
{
LOG_ERROR("Could not render the mouse text strings using the font shader");
R_FALSE
}
R_LOG_ERROR(result, "Could not render the mouse text string using the font shader")
}
// Put the sprite vertex and index buffers on the graphics pipeline to prepare them for drawing.
result = sprite_->Render(direct_3d_->get_device_context());
if (!result)
{
R_FALSE
}
R_LOG_ERROR(result, "Could not render the sprite")
// render the sprite with the texture shader.
result = shader_manager_->render_texture_shader(direct_3d_->get_device_context(), model_->GetIndexCount(), worldMatrix, viewMatrix, orthoMatrix,
sprite_->GetTexture());
if (!result)
{
LOG_ERROR("Could not render the sprite using the texture shader");
R_FALSE
}
R_LOG_ERROR(result, "Could not render the sprite using the texture shader")
// Turn off alpha blending after rendering the 2D text.
direct_3d_->disable_alpha_blending();
@@ -1636,11 +1472,7 @@ bool application_class::update_mouse_strings(int mouseX, int mouseY, bool mouseD
// Update the sentence vertex buffer with the new string information.
result = mouse_strings_[0].UpdateText(direct_3d_->get_device_context(), font_, finalString, 10, 50, 1.0f, 1.0f, 1.0f);
if (!result)
{
LOG_ERROR("Could not update the mouse X string");
R_FALSE
}
R_LOG_ERROR(result, "Could not update the mouse X string")
// Convert the mouse Y integer to string format.
sprintf_s(tempString, "%d", mouseY);
@@ -1651,11 +1483,7 @@ bool application_class::update_mouse_strings(int mouseX, int mouseY, bool mouseD
// Update the sentence vertex buffer with the new string information.
result = mouse_strings_[1].UpdateText(direct_3d_->get_device_context(), font_, finalString, 10, 75, 1.0f, 1.0f, 1.0f);
if (!result)
{
LOG_ERROR("Could not update the mouse Y string");
R_FALSE
}
R_LOG_ERROR(result, "Could not update the mouse Y string")
// Setup the mouse button string.
if (mouseDown)
@@ -1669,12 +1497,7 @@ bool application_class::update_mouse_strings(int mouseX, int mouseY, bool mouseD
// Update the sentence vertex buffer with the new string information.
result = mouse_strings_[2].UpdateText(direct_3d_->get_device_context(), font_, finalString, 10, 100, 1.0f, 1.0f, 1.0f);
if (!result)
{
LOG_ERROR("Could not update the mouse button string");
R_FALSE
}
R_LOG_ERROR(result, "Could not update the mouse button string")
R_TRUE
}
@@ -1743,11 +1566,7 @@ bool application_class::update_fps()
// Update the sentence vertex buffer with the new string information.
result = fps_string_->UpdateText(direct_3d_->get_device_context(), font_, finalString, 10, 10, red, green, blue);
if (!result)
{
LOG_ERROR("Could not update the fps string");
R_FALSE
}
R_LOG_ERROR(result, "Could not update the fps string")
R_TRUE
}
@@ -1767,11 +1586,7 @@ bool application_class::update_render_count_string(int renderCount)
// Update the sentence vertex buffer with the new string information.
result = render_count_string_->UpdateText(direct_3d_->get_device_context(), font_, finalString, 10, 30, 1.0f, 1.0f, 1.0f);
if (!result)
{
LOG_ERROR("Could not update the render count string");
R_FALSE
}
R_LOG_ERROR(result, "Could not update the render count string")
R_TRUE
}
@@ -1978,8 +1793,7 @@ void application_class::physics_thread_function()
bool result = render_physics(deltaTime);
if (!result)
{
LOG_ERROR("Could not render the physics scene");
return;
LOG_ERROR("Failed to update physics");
}
}
// Attendre un peu pour <20>viter de surcharger le CPU