Skip to content

Commit

Permalink
Merge pull request #158 from JacobDomagala/152-address-staticanalysis…
Browse files Browse the repository at this point in the history
…-issues

[#152]: Address `StaticAnalysis` issues
  • Loading branch information
JacobDomagala authored Nov 20, 2023
2 parents 5c14f16 + d69e485 commit 0be5072
Show file tree
Hide file tree
Showing 15 changed files with 44 additions and 90 deletions.
7 changes: 3 additions & 4 deletions editor/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ Editor::HandleMouseDrag(const glm::vec2& currentCursorPos, const glm::vec2& axis
}

void
Editor::SetMouseOnObject()
Editor::SetMouseOnObject() const
{
if (m_mouseDrag && (m_movementOnEditorObject || m_movementOnGameObject))
{
Expand Down Expand Up @@ -467,9 +467,8 @@ Editor::SetRenderLayerToDraw(int32_t layer)
renderLayerToDraw_ = layer;
}


void
Editor::SetupRendererData()
Editor::SetupRendererData() const
{
renderer::VulkanRenderer::SetupData();

Expand Down Expand Up @@ -715,7 +714,7 @@ Editor::DrawBoundingBoxes()
}

void
Editor::DrawGrid()
Editor::DrawGrid() const
{
const auto levelSize = m_currentLevel->GetSize();
const auto grad = m_gridCellSize;
Expand Down
6 changes: 3 additions & 3 deletions editor/editor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class Editor : public Application
GetRenderNodes() const;

void
DrawGrid();
DrawGrid() const;

void
DrawBoundingBoxes();
Expand Down Expand Up @@ -178,7 +178,7 @@ class Editor : public Application
// GetEditorObjectByID(Object::ID ID);

void
SetupRendererData();
SetupRendererData() const;

void
DrawEditorObjects();
Expand Down Expand Up @@ -217,7 +217,7 @@ class Editor : public Application
ShowCursor(bool choice);

void
SetMouseOnObject();
SetMouseOnObject() const;

void
FreeLevelData();
Expand Down
2 changes: 1 addition & 1 deletion engine/core/work_queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace looper {
void
WorkQueue::PushWorkUnit(const WorkQueue::Precondition& prec, const WorkQueue::WorkUnit& work)
{
queue_.push_back({prec, work});
queue_.emplace_back(prec, work);
}

void
Expand Down
18 changes: 7 additions & 11 deletions engine/game/animatable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,15 @@ Animatable::SetCorrectAnimationPoint(time::milliseconds& updateTime)
auto animationDurationMs =
time::Timer::ConvertToMs(currentState_.m_currentAnimationPoint->m_timeDuration);

if (updateTime >= animationDurationMs)
while (updateTime >= animationDurationMs
&& currentState_.m_currentAnimationPoint != m_animationPoints.begin())
{
do
{
updateTime -= animationDurationMs;
animationValue +=
currentState_.m_currentAnimationEnd - currentState_.m_currentAnimationBegin;
updateTime -= animationDurationMs;
animationValue += currentState_.m_currentAnimationEnd - currentState_.m_currentAnimationBegin;

UpdateAnimationPoint();
animationDurationMs =
time::Timer::ConvertToMs(currentState_.m_currentAnimationPoint->m_timeDuration);
} while (updateTime >= animationDurationMs
&& currentState_.m_currentAnimationPoint != m_animationPoints.begin());
UpdateAnimationPoint();
animationDurationMs =
time::Timer::ConvertToMs(currentState_.m_currentAnimationPoint->m_timeDuration);
}

return animationValue;
Expand Down
8 changes: 3 additions & 5 deletions engine/game/level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,8 @@ Level::GenerateTextureForCollision()
const size_t numChannels = 4;
const auto size = static_cast< size_t >(width * height * numChannels);

auto* data = new unsigned char[size];
auto data = FileManager::ImageHandleType{new unsigned char[size],
[](const uint8_t* ptr) { delete[] ptr; }};
const auto& nodes = m_pathFinder.GetAllNodes();

for (size_t h = 0; h < height; ++h)
Expand All @@ -488,10 +489,7 @@ Level::GenerateTextureForCollision()
}
}

collisionTextureData_ = {FileManager::ImageHandleType{reinterpret_cast< unsigned char* >(data),
[](const uint8_t* ptr) { delete[] ptr; }},
{width, height},
numChannels};
collisionTextureData_ = {std::move(data), {width, height}, numChannels};

// To avoid blurry edges
renderer::TextureProperties props;
Expand Down
5 changes: 3 additions & 2 deletions engine/game/object.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "object.hpp"

namespace looper {

static std::string
namespace {
std::string
TypeToString(ObjectType type)
{
std::string typeStr;
Expand Down Expand Up @@ -41,6 +41,7 @@ TypeToString(ObjectType type)

return typeStr;
}
} // namespace

ObjectType
Object::GetTypeFromString(const std::string& stringType)
Expand Down
5 changes: 0 additions & 5 deletions engine/game/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,6 @@ class Player : public GameObject
/*void
CreateSprite(const glm::vec2& position, const glm::ivec2& size, const std::string& fileName);*/

void
Hit(int32_t /*dmg*/) override
{
}

private:
void
UpdateInternal(bool isReverse) override;
Expand Down
6 changes: 6 additions & 0 deletions engine/input/input_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ BroadcastEvent(const auto& listeners, auto& event)
}

void
// cppcheck-suppress constParameterCallback
InputManager::InternalKeyCallback(GLFWwindow* window, int32_t key, int32_t scancode,
int32_t action, int32_t mods)
{
Expand All @@ -37,6 +38,7 @@ InputManager::InternalKeyCallback(GLFWwindow* window, int32_t key, int32_t scanc
}

void
// cppcheck-suppress constParameterCallback
InputManager::InternalCharCallback(GLFWwindow* window, uint32_t key)
{
Logger::Trace("GLFW char {}", key);
Expand All @@ -47,6 +49,7 @@ InputManager::InternalCharCallback(GLFWwindow* window, uint32_t key)
}

void
// cppcheck-suppress constParameterCallback
InputManager::InternalMouseButtonCallback(GLFWwindow* window, int32_t button, int32_t action,
int32_t mods)
{
Expand All @@ -58,6 +61,7 @@ InputManager::InternalMouseButtonCallback(GLFWwindow* window, int32_t button, in
}

void
// cppcheck-suppress constParameterCallback
InputManager::InternalCursorPositionCallback(GLFWwindow* window, double xPos, double yPos)
{
Logger::Trace("GLFW cursor pos {} {}", xPos, yPos);
Expand All @@ -69,6 +73,7 @@ InputManager::InternalCursorPositionCallback(GLFWwindow* window, double xPos, do
}

void
// cppcheck-suppress constParameterCallback
InputManager::InternalMouseScrollCallback(GLFWwindow* window, double xOffset, double yOffset)
{
Logger::Trace("GLFW scroll {} {}", xOffset, yOffset);
Expand All @@ -79,6 +84,7 @@ InputManager::InternalMouseScrollCallback(GLFWwindow* window, double xOffset, do
}

void
// cppcheck-suppress constParameterCallback
InputManager::InternalWindowFocusCallback(GLFWwindow* window, int32_t focused)
{
Logger::Trace("GLFW window focus {}", focused);
Expand Down
4 changes: 2 additions & 2 deletions engine/renderer/buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Buffer::CopyData(const void* data) const
}

void
Buffer::CopyDataWithStaging(void* data, size_t dataSize) const
Buffer::CopyDataWithStaging(const void* data, const size_t dataSize) const
{
VkBuffer stagingBuffer = {};
VkDeviceMemory stagingBufferMemory = {};
Expand All @@ -72,7 +72,7 @@ Buffer::CopyDataWithStaging(void* data, size_t dataSize) const
}

void
Buffer::CopyDataToImageWithStaging(VkImage image, void* data, size_t dataSize,
Buffer::CopyDataToImageWithStaging(VkImage image, const void* data, const size_t dataSize,
const std::vector< VkBufferImageCopy >& copyRegions)
{
VkBuffer stagingBuffer = {};
Expand Down
4 changes: 2 additions & 2 deletions engine/renderer/buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ class Buffer
CopyData(const void* data) const;

void
CopyDataWithStaging(void* data, size_t dataSize) const;
CopyDataWithStaging(const void* data, const size_t dataSize) const;

static void
CopyDataToImageWithStaging(VkImage image, void* data, size_t dataSize,
CopyDataToImageWithStaging(VkImage image, const void* data, const size_t dataSize,
const std::vector< VkBufferImageCopy >& copyRegions);

void
Expand Down
3 changes: 2 additions & 1 deletion engine/renderer/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,8 @@ VulkanRenderer::Initialize(GLFWwindow* windowHandle, ApplicationType type)
for (uint32_t layer = 0; layer < NUM_LAYERS; ++layer)
{
renderData.vertices.at(layer).resize(MAX_NUM_VERTICES_PER_LAYER);
renderData.indices.at(layer).resize(MAX_SPRITES_PER_LAYER * INDICES_PER_SPRITE);
renderData.indices.at(layer).resize(static_cast< size_t >(MAX_SPRITES_PER_LAYER)
* static_cast< size_t >(INDICES_PER_SPRITE));
}

renderData.perInstance.resize(MAX_NUM_SPRITES);
Expand Down
34 changes: 8 additions & 26 deletions engine/renderer/texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,16 @@ Texture::CreateTextureImage(const FileManager::ImageData& data)
CreateImageView(m_textureImage, m_format, VK_IMAGE_ASPECT_COLOR_BIT, m_mips, false);

CreateTextureSampler();

TransitionImageLayout(VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, m_mips);

CopyBufferToImage(data.m_bytes.get());

// transitioned to VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL while generating mipmaps
GenerateMipmaps(m_textureImage, VK_FORMAT_R8G8B8A8_SRGB, static_cast< int32_t >(m_width),
static_cast< int32_t >(m_height), m_mips);
UpdateTexture(data);
}

void
Texture::UpdateTexture(const FileManager::ImageData& data)
Texture::UpdateTexture(const FileManager::ImageData& data) const
{
TransitionImageLayout(VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, m_mips);
TransitionImageLayout(m_textureImage, VK_IMAGE_LAYOUT_UNDEFINED,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, m_mips);

CopyBufferToImage(data.m_bytes.get());
CopyBufferToImage(m_textureImage, m_width, m_height, data.m_bytes.get());

// transitioned to VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL while generating mipmaps
GenerateMipmaps(m_textureImage, VK_FORMAT_R8G8B8A8_SRGB, static_cast< int32_t >(m_width),
Expand Down Expand Up @@ -340,7 +334,8 @@ Texture::CreateTextureSampler()
}

void
Texture::CopyBufferToImage(VkImage image, uint32_t texWidth, uint32_t texHeight, uint8_t* data)
Texture::CopyBufferToImage(VkImage image, uint32_t texWidth, uint32_t texHeight,
const uint8_t* data)
{
VkBufferImageCopy region = {};
region.bufferOffset = 0;
Expand All @@ -360,7 +355,7 @@ Texture::CopyBufferToImage(VkImage image, uint32_t texWidth, uint32_t texHeight,

void
Texture::CopyBufferToCubemapImage(VkImage image, uint32_t texWidth, uint32_t texHeight,
uint8_t* data)
const uint8_t* data)
{
constexpr auto num_faces = 6;
const auto single_face_size =
Expand Down Expand Up @@ -394,12 +389,6 @@ Texture::CopyBufferToCubemapImage(VkImage image, uint32_t texWidth, uint32_t tex
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, 1, true);
}

void
Texture::CopyBufferToImage(uint8_t* data)
{
CopyBufferToImage(m_textureImage, m_width, m_height, data);
}

void
Texture::TransitionImageLayout(VkImage image, VkImageLayout oldLayout, VkImageLayout newLayout,
uint32_t mipLevels, bool cubemap)
Expand Down Expand Up @@ -450,13 +439,6 @@ Texture::TransitionImageLayout(VkImage image, VkImageLayout oldLayout, VkImageLa
Command::EndSingleTimeCommands(commandBuffer);
}

void
Texture::TransitionImageLayout(VkImageLayout oldLayout, VkImageLayout newLayout, uint32_t mipLevels)
{
TransitionImageLayout(m_textureImage, oldLayout, newLayout, mipLevels);
}


/**************************************************************************************************
*************************************** TEXTURE LIBRARY ******************************************
*************************************************************************************************/
Expand Down
13 changes: 4 additions & 9 deletions engine/renderer/texture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Texture
Destroy();

void
UpdateTexture(const FileManager::ImageData& data);
UpdateTexture(const FileManager::ImageData& data) const;

static std::pair< VkImage, VkDeviceMemory >
CreateImage(uint32_t width, uint32_t height, uint32_t mipLevels,
Expand All @@ -57,10 +57,11 @@ class Texture
uint32_t mipLevels, bool cubemap = false);

static void
CopyBufferToImage(VkImage image, uint32_t texWidth, uint32_t texHeight, uint8_t* data);
CopyBufferToImage(VkImage image, uint32_t texWidth, uint32_t texHeight, const uint8_t* data);

static void
CopyBufferToCubemapImage(VkImage image, uint32_t texWidth, uint32_t texHeight, uint8_t* data);
CopyBufferToCubemapImage(VkImage image, uint32_t texWidth, uint32_t texHeight,
const uint8_t* data);

static VkDescriptorSet
CreateDescriptorSet(VkSampler sampler, VkImageView image_view, VkImageLayout image_layout,
Expand Down Expand Up @@ -91,12 +92,6 @@ class Texture
void
CreateTextureImage(const FileManager::ImageData& data);

void
TransitionImageLayout(VkImageLayout oldLayout, VkImageLayout newLayout, uint32_t mipLevels);

void
CopyBufferToImage(uint8_t* data);

private:
TextureID id_ = {};
TextureType m_type = {};
Expand Down
16 changes: 0 additions & 16 deletions engine/utils/file_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,22 +82,6 @@ FileManager::LoadImageData(std::string_view fileName)
return {std::move(textureData), {w, h}, n};
}

void
FileManager::SaveImageFile(std::string_view fileName, const ImageData& image)
{
const auto pathToImage = std::filesystem::path(IMAGES_DIR / fileName).string();

std::ofstream file(pathToImage, std::ios::out | std::ios::binary | std::ios::trunc);
if (!file)
{
Logger::Fatal("FileManager::SaveImageFile -> {} can't be opened!", pathToImage);
}

file.write(reinterpret_cast< const char* >(image.m_bytes.get()),
image.m_size.x * image.m_size.y * image.m_format);
file.close();
}

nlohmann::json
FileManager::LoadJsonFile(std::string_view pathToFile)
{
Expand Down
3 changes: 0 additions & 3 deletions engine/utils/file_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ class FileManager
static ImageData
LoadImageData(std::string_view fileName);

static void
SaveImageFile(std::string_view fileName, const ImageData& image);

static nlohmann::json
LoadJsonFile(std::string_view pathToFile);

Expand Down

0 comments on commit 0be5072

Please sign in to comment.