Skip to content

Commit

Permalink
Fix some clang-tidy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
jhasse committed Oct 3, 2024
1 parent fd25a86 commit e10dcce
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
1 change: 1 addition & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Checks: '
,-cert-flp30-c,
,-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling,
,-clang-diagnostic-unused-command-line-argument,
,-concurrency-mt-unsafe,
,-cppcoreguidelines-avoid-c-arrays,
,-cppcoreguidelines-avoid-magic-numbers,
,-cppcoreguidelines-avoid-non-const-global-variables,
Expand Down
2 changes: 1 addition & 1 deletion src/jngl/Vec2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ std::ostream& operator<<(std::ostream&, const Vec2&);

#if __has_include(<format>) && (!defined(_LIBCPP_VERSION) || _LIBCPP_VERSION >= 170000)
template <> struct std::formatter<jngl::Vec2> {
constexpr auto parse(std::format_parse_context& ctx) {
constexpr static auto parse(std::format_parse_context& ctx) {
return ctx.begin();
}
auto format(const jngl::Vec2& v, auto& ctx) const {
Expand Down
28 changes: 18 additions & 10 deletions src/jngl/sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
#if __cplusplus < 202002L
#include <boost/algorithm/string/predicate.hpp>
#endif
#include <cstddef>
#include <sstream>
#include <thread>
#ifndef NOWEBP
#include "../ImageDataWebP.hpp"
#endif
Expand Down Expand Up @@ -145,7 +145,9 @@ Sprite::Sprite(const std::string& filename, LoadType loadType) : texture(getText
auto loadTexture = std::make_shared<Finally>(loadFunction(this, filename, pFile, halfLoad));
loader = std::make_shared<Finally>([pFile, loadTexture, this]() mutable {
loadTexture.reset(); // call ~Finally
fclose(pFile);
if (fclose(pFile) != 0) {
internal::warn("Error closing file: {}", strerror(errno));
}
setCenter(0, 0);
});
if (loadType != LoadType::THREADED) {
Expand Down Expand Up @@ -310,7 +312,8 @@ void Sprite::drawMesh(const std::vector<Vertex>& vertexes,
}

void Sprite::setBytes(const unsigned char* const bytes) {
texture->setBytes(bytes, int(std::lround(width)), int(std::lround(height)));
texture->setBytes(bytes, static_cast<int>(std::lround(width)),
static_cast<int>(std::lround(height)));
}

const Shader& Sprite::vertexShader() {
Expand All @@ -324,7 +327,7 @@ Finally Sprite::LoadPNG(const std::string& filename, FILE* const fp, const bool

// Read in some of the signature bytes
if (fread(buf, 1, PNG_BYTES_TO_CHECK, fp) != PNG_BYTES_TO_CHECK ||
png_sig_cmp(buf, png_size_t(0), PNG_BYTES_TO_CHECK) != 0) {
png_sig_cmp(buf, static_cast<png_size_t>(0), PNG_BYTES_TO_CHECK) != 0) {
throw std::runtime_error(std::string("Error reading signature bytes. (" + filename + ")"));
}

Expand Down Expand Up @@ -387,7 +390,9 @@ void Sprite::cleanUpRowPointers(std::vector<unsigned char*>& buf) {
}

Finally Sprite::LoadBMP(const std::string& filename, FILE* const fp, const bool halfLoad) {
fseek(fp, 10, SEEK_SET);
if (fseek(fp, 10, SEEK_SET) != 0) {
throw std::runtime_error(std::string("Error seeking file. (" + filename + ")"));
}
BMPHeader header{};
if (!fread(&header, sizeof(header), 1, fp)) {
throw std::runtime_error(std::string("Error reading file. (" + filename + ")"));
Expand All @@ -406,26 +411,29 @@ Finally Sprite::LoadBMP(const std::string& filename, FILE* const fp, const bool
}
std::vector<unsigned char*> buf(header.height);
for (auto& row : buf) {
row = new unsigned char[header.width * 3];
row = new unsigned char[static_cast<unsigned long>(header.width * 3)];
}
Finally cleanUp([&buf]() { cleanUpRowPointers(buf); });

if (header.height < 0) {
header.height = -header.height;
for (int i = 0; i < header.height; ++i) {
if (fseek(fp, header.dataOffset + i * header.width * 3, SEEK_SET) != 0) {
if (fseek(fp, header.dataOffset + static_cast<long>(i) * header.width * 3, SEEK_SET) !=
0) {
throw std::runtime_error(std::string("Error reading file. (" + filename + ")"));
}
if (!fread(buf[i], header.width * 3, 1, fp)) {
if (!fread(buf[i], static_cast<size_t>(header.width) * 3, 1, fp)) {
throw std::runtime_error(std::string("Error reading data. (" + filename + ")"));
}
}
} else { // "bottom-up"-Bitmap
for (int i = header.height - 1; i >= 0; --i) {
if (fseek(fp, header.dataOffset + i * header.width * 3, SEEK_SET) != 0) {
if (fseek(fp, header.dataOffset + static_cast<long>(i) * header.width * 3, SEEK_SET) !=
0) {
throw std::runtime_error(std::string("Error reading file. (" + filename + ")"));
}
if (!fread(buf[(header.height - 1) - i], header.width * 3, 1, fp)) {
if (!fread(buf[(header.height - 1) - i], static_cast<size_t>(header.width) * 3, 1,
fp)) {
throw std::runtime_error(std::string("Error reading data. (" + filename + ")"));
}
}
Expand Down

0 comments on commit e10dcce

Please sign in to comment.