From bc802c0e6f3434773037d0093504edbd33dbd68d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20D=C3=BCrrenberger?= Date: Thu, 22 Apr 2021 00:48:56 +0200 Subject: [PATCH] Fix conversion warnings for the Graphics module - Fix conversion and shadowing warnings - Add SYSTEM indicator for stb_*, FreeType and other headers --- cmake/Macros.cmake | 2 +- src/SFML/Graphics/CMakeLists.txt | 2 +- src/SFML/Graphics/CircleShape.cpp | 2 +- src/SFML/Graphics/Color.cpp | 2 +- src/SFML/Graphics/Font.cpp | 43 +++++++++++++++-------------- src/SFML/Graphics/Image.cpp | 34 +++++++++++------------ src/SFML/Graphics/ImageLoader.cpp | 40 ++++++++++++++------------- src/SFML/Graphics/RenderTarget.cpp | 10 +++---- src/SFML/Graphics/Shape.cpp | 8 ++++-- src/SFML/Graphics/Sprite.cpp | 12 ++++---- src/SFML/Graphics/Texture.cpp | 12 ++++---- src/SFML/Graphics/Transformable.cpp | 2 +- src/SFML/Graphics/View.cpp | 2 +- 13 files changed, 89 insertions(+), 82 deletions(-) diff --git a/cmake/Macros.cmake b/cmake/Macros.cmake index 129afe22e8..841e7865cb 100644 --- a/cmake/Macros.cmake +++ b/cmake/Macros.cmake @@ -350,7 +350,7 @@ function(sfml_add_external) if (NOT include_dir) message(FATAL_ERROR "No path given for include dir ${THIS_INCLUDE}") endif() - target_include_directories(${target} INTERFACE "$") + target_include_directories(${target} SYSTEM INTERFACE "$") endforeach() endif() diff --git a/src/SFML/Graphics/CMakeLists.txt b/src/SFML/Graphics/CMakeLists.txt index 252fe89d55..bcd9d55d33 100644 --- a/src/SFML/Graphics/CMakeLists.txt +++ b/src/SFML/Graphics/CMakeLists.txt @@ -93,7 +93,7 @@ sfml_add_library(sfml-graphics target_link_libraries(sfml-graphics PUBLIC sfml-window) # stb_image sources -target_include_directories(sfml-graphics PRIVATE "${PROJECT_SOURCE_DIR}/extlibs/headers/stb_image") +target_include_directories(sfml-graphics SYSTEM PRIVATE "${PROJECT_SOURCE_DIR}/extlibs/headers/stb_image") # glad sources target_include_directories(sfml-graphics SYSTEM PRIVATE "${PROJECT_SOURCE_DIR}/extlibs/headers/glad/include") diff --git a/src/SFML/Graphics/CircleShape.cpp b/src/SFML/Graphics/CircleShape.cpp index 7281467518..6dc41909fd 100644 --- a/src/SFML/Graphics/CircleShape.cpp +++ b/src/SFML/Graphics/CircleShape.cpp @@ -74,7 +74,7 @@ Vector2f CircleShape::getPoint(std::size_t index) const { static const float pi = 3.141592654f; - float angle = index * 2 * pi / m_pointCount - pi / 2; + float angle = static_cast(index) * 2.f * pi / static_cast(m_pointCount) - pi / 2.f; float x = std::cos(angle) * m_radius; float y = std::sin(angle) * m_radius; diff --git a/src/SFML/Graphics/Color.cpp b/src/SFML/Graphics/Color.cpp index f8cd4edf21..7de5b23e71 100644 --- a/src/SFML/Graphics/Color.cpp +++ b/src/SFML/Graphics/Color.cpp @@ -81,7 +81,7 @@ a((color & 0x000000ff) >> 0 ) //////////////////////////////////////////////////////////// Uint32 Color::toInteger() const { - return (r << 24) | (g << 16) | (b << 8) | a; + return static_cast((r << 24) | (g << 16) | (b << 8) | a); } diff --git a/src/SFML/Graphics/Font.cpp b/src/SFML/Graphics/Font.cpp index 7b16023dad..c635d71b7c 100644 --- a/src/SFML/Graphics/Font.cpp +++ b/src/SFML/Graphics/Font.cpp @@ -48,11 +48,12 @@ namespace // FreeType callbacks that operate on a sf::InputStream unsigned long read(FT_Stream rec, unsigned long offset, unsigned char* buffer, unsigned long count) { + sf::Int64 convertedOffset = static_cast(offset); sf::InputStream* stream = static_cast(rec->descriptor.pointer); - if (static_cast(stream->seek(offset)) == offset) + if (stream->seek(convertedOffset) == convertedOffset) { if (count > 0) - return static_cast(stream->read(reinterpret_cast(buffer), count)); + return static_cast(stream->read(reinterpret_cast(buffer), static_cast(count))); else return 0; } @@ -441,7 +442,7 @@ float Font::getUnderlinePosition(unsigned int characterSize) const { // Return a fixed position if font is a bitmap font if (!FT_IS_SCALABLE(face)) - return characterSize / 10.f; + return static_cast(characterSize) / 10.f; return -static_cast(FT_MulFix(face->underline_position, face->size->metrics.y_scale)) / static_cast(1 << 6); } @@ -461,7 +462,7 @@ float Font::getUnderlineThickness(unsigned int characterSize) const { // Return a fixed thickness if font is a bitmap font if (!FT_IS_SCALABLE(face)) - return characterSize / 14.f; + return static_cast(characterSize) / 14.f; return static_cast(FT_MulFix(face->underline_thickness, face->size->metrics.y_scale)) / static_cast(1 << 6); } @@ -600,7 +601,7 @@ Glyph Font::loadGlyph(Uint32 codePoint, unsigned int characterSize, bool bold, f { if (bold) { - FT_OutlineGlyph outlineGlyph = (FT_OutlineGlyph)glyphDesc; + FT_OutlineGlyph outlineGlyph = reinterpret_cast(glyphDesc); FT_Outline_Embolden(&outlineGlyph->outline, weight); } @@ -632,11 +633,11 @@ Glyph Font::loadGlyph(Uint32 codePoint, unsigned int characterSize, bool bold, f if (bold) glyph.advance += static_cast(weight) / static_cast(1 << 6); - glyph.lsbDelta = face->glyph->lsb_delta; - glyph.rsbDelta = face->glyph->rsb_delta; + glyph.lsbDelta = static_cast(face->glyph->lsb_delta); + glyph.rsbDelta = static_cast(face->glyph->rsb_delta); - int width = bitmap.width; - int height = bitmap.rows; + unsigned int width = bitmap.width; + unsigned int height = bitmap.rows; if ((width > 0) && (height > 0)) { @@ -656,8 +657,8 @@ Glyph Font::loadGlyph(Uint32 codePoint, unsigned int characterSize, bool bold, f // Make sure the texture data is positioned in the center // of the allocated texture rectangle glyph.textureRect.left += padding; - glyph.textureRect.top += padding; - glyph.textureRect.width -= 2 * padding; + glyph.textureRect.top += padding; + glyph.textureRect.width -= 2 * padding; glyph.textureRect.height -= 2 * padding; // Compute the glyph's bounding box @@ -712,10 +713,10 @@ Glyph Font::loadGlyph(Uint32 codePoint, unsigned int characterSize, bool bold, f } // Write the pixels to the texture - unsigned int x = glyph.textureRect.left - padding; - unsigned int y = glyph.textureRect.top - padding; - unsigned int w = glyph.textureRect.width + 2 * padding; - unsigned int h = glyph.textureRect.height + 2 * padding; + unsigned int x = static_cast(glyph.textureRect.left) - padding; + unsigned int y = static_cast(glyph.textureRect.top) - padding; + unsigned int w = static_cast(glyph.textureRect.width) + 2 * padding; + unsigned int h = static_cast(glyph.textureRect.height) + 2 * padding; page.texture.update(&m_pixelBuffer[0], w, h, x, y); } @@ -735,7 +736,7 @@ IntRect Font::findGlyphRect(Page& page, unsigned int width, unsigned int height) float bestRatio = 0; for (std::vector::iterator it = page.rows.begin(); it != page.rows.end() && !row; ++it) { - float ratio = static_cast(height) / it->height; + float ratio = static_cast(height) / static_cast(it->height); // Ignore rows that are either too small or too high if ((ratio < 0.7f) || (ratio > 1.f)) @@ -757,7 +758,7 @@ IntRect Font::findGlyphRect(Page& page, unsigned int width, unsigned int height) // If we didn't find a matching row, create a new one (10% taller than the glyph) if (!row) { - int rowHeight = height + height / 10; + unsigned int rowHeight = height + height / 10; while ((page.nextRow + rowHeight >= page.texture.getSize().y) || (width >= page.texture.getSize().x)) { // Not enough space: resize the texture if possible @@ -787,7 +788,7 @@ IntRect Font::findGlyphRect(Page& page, unsigned int width, unsigned int height) } // Find the glyph's rectangle on the selected row - IntRect rect(row->width, row->top, width, height); + IntRect rect(Rect(row->width, row->top, width, height)); // Update the row informations row->width += width; @@ -819,7 +820,7 @@ bool Font::setCurrentSize(unsigned int characterSize) const err() << "Available sizes are: "; for (int i = 0; i < face->num_fixed_sizes; ++i) { - const unsigned int size = (face->available_sizes[i].y_ppem + 32) >> 6; + const long size = (face->available_sizes[i].y_ppem + 32) >> 6; err() << size << " "; } err() << std::endl; @@ -846,8 +847,8 @@ nextRow(3) image.create(128, 128, Color(255, 255, 255, 0)); // Reserve a 2x2 white square for texturing underlines - for (int x = 0; x < 2; ++x) - for (int y = 0; y < 2; ++y) + for (unsigned int x = 0; x < 2; ++x) + for (unsigned int y = 0; y < 2; ++y) image.setPixel(x, y, Color(255, 255, 255, 255)); // Create the texture diff --git a/src/SFML/Graphics/Image.cpp b/src/SFML/Graphics/Image.cpp index 089ff827d9..56fad5a4b8 100644 --- a/src/SFML/Graphics/Image.cpp +++ b/src/SFML/Graphics/Image.cpp @@ -199,20 +199,20 @@ void Image::copy(const Image& source, unsigned int destX, unsigned int destY, co { srcRect.left = 0; srcRect.top = 0; - srcRect.width = source.m_size.x; - srcRect.height = source.m_size.y; + srcRect.width = static_cast(source.m_size.x); + srcRect.height = static_cast(source.m_size.y); } else { if (srcRect.left < 0) srcRect.left = 0; if (srcRect.top < 0) srcRect.top = 0; - if (srcRect.width > static_cast(source.m_size.x)) srcRect.width = source.m_size.x; - if (srcRect.height > static_cast(source.m_size.y)) srcRect.height = source.m_size.y; + if (srcRect.width > static_cast(source.m_size.x)) srcRect.width = static_cast(source.m_size.x); + if (srcRect.height > static_cast(source.m_size.y)) srcRect.height = static_cast(source.m_size.y); } // Then find the valid bounds of the destination rectangle - int width = srcRect.width; - int height = srcRect.height; + unsigned int width = static_cast(srcRect.width); + unsigned int height = static_cast(srcRect.height); if (destX + width > m_size.x) width = m_size.x - destX; if (destY + height > m_size.y) height = m_size.y - destY; @@ -221,20 +221,20 @@ void Image::copy(const Image& source, unsigned int destX, unsigned int destY, co return; // Precompute as much as possible - int pitch = width * 4; - int rows = height; - int srcStride = source.m_size.x * 4; - int dstStride = m_size.x * 4; - const Uint8* srcPixels = &source.m_pixels[0] + (srcRect.left + srcRect.top * source.m_size.x) * 4; + std::size_t pitch = static_cast(width) * 4; + unsigned int rows = height; + int srcStride = static_cast(source.m_size.x) * 4; + int dstStride = static_cast(m_size.x) * 4; + const Uint8* srcPixels = &source.m_pixels[0] + (static_cast(srcRect.left) + static_cast(srcRect.top) * source.m_size.x) * 4; Uint8* dstPixels = &m_pixels[0] + (destX + destY * m_size.x) * 4; // Copy the pixels if (applyAlpha) { // Interpolation using alpha values, pixel by pixel (slower) - for (int i = 0; i < rows; ++i) + for (unsigned int i = 0; i < rows; ++i) { - for (int j = 0; j < width; ++j) + for (unsigned int j = 0; j < width; ++j) { // Get a direct pointer to the components of the current pixel const Uint8* src = srcPixels + j * 4; @@ -255,7 +255,7 @@ void Image::copy(const Image& source, unsigned int destX, unsigned int destY, co else { // Optimized copy ignoring alpha values, row by row (faster) - for (int i = 0; i < rows; ++i) + for (unsigned int i = 0; i < rows; ++i) { std::memcpy(dstPixels, srcPixels, pitch); srcPixels += srcStride; @@ -308,8 +308,8 @@ void Image::flipHorizontally() for (std::size_t y = 0; y < m_size.y; ++y) { - std::vector::iterator left = m_pixels.begin() + y * rowSize; - std::vector::iterator right = m_pixels.begin() + (y + 1) * rowSize - 4; + std::vector::iterator left = m_pixels.begin() + static_cast::iterator::difference_type>(y * rowSize); + std::vector::iterator right = m_pixels.begin() + static_cast::iterator::difference_type>((y + 1) * rowSize - 4); for (std::size_t x = 0; x < m_size.x / 2; ++x) { @@ -328,7 +328,7 @@ void Image::flipVertically() { if (!m_pixels.empty()) { - std::size_t rowSize = m_size.x * 4; + std::vector::iterator::difference_type rowSize = static_cast::iterator::difference_type>(m_size.x * 4); std::vector::iterator top = m_pixels.begin(); std::vector::iterator bottom = m_pixels.end() - rowSize; diff --git a/src/SFML/Graphics/ImageLoader.cpp b/src/SFML/Graphics/ImageLoader.cpp index 93113fbae0..4cfc9e4b3a 100644 --- a/src/SFML/Graphics/ImageLoader.cpp +++ b/src/SFML/Graphics/ImageLoader.cpp @@ -115,13 +115,13 @@ bool ImageLoader::loadImageFromFile(const std::string& filename, std::vector(width); + size.y = static_cast(height); - if (width && height) + if (width > 0 && height > 0) { // Copy the loaded pixels to the pixel buffer - pixels.resize(width * height * 4); + pixels.resize(static_cast(width * height * 4)); memcpy(&pixels[0], ptr, pixels.size()); } @@ -159,13 +159,13 @@ bool ImageLoader::loadImageFromMemory(const void* data, std::size_t dataSize, st if (ptr) { // Assign the image properties - size.x = width; - size.y = height; + size.x = static_cast(width); + size.y = static_cast(height); - if (width && height) + if (width > 0 && height > 0) { // Copy the loaded pixels to the pixel buffer - pixels.resize(width * height * 4); + pixels.resize(static_cast(width * height * 4)); memcpy(&pixels[0], ptr, pixels.size()); } @@ -214,13 +214,13 @@ bool ImageLoader::loadImageFromStream(InputStream& stream, std::vector& p if (ptr) { // Assign the image properties - size.x = width; - size.y = height; + size.x = static_cast(width); + size.y = static_cast(height); if (width && height) { // Copy the loaded pixels to the pixel buffer - pixels.resize(width * height * 4); + pixels.resize(static_cast(width * height * 4)); memcpy(&pixels[0], ptr, pixels.size()); } @@ -250,29 +250,30 @@ bool ImageLoader::saveImageToFile(const std::string& filename, const std::vector // Extract the extension const std::size_t dot = filename.find_last_of('.'); const std::string extension = dot != std::string::npos ? toLower(filename.substr(dot + 1)) : ""; + const Vector2i convertedSize = Vector2i(size); if (extension == "bmp") { // BMP format - if (stbi_write_bmp(filename.c_str(), size.x, size.y, 4, &pixels[0])) + if (stbi_write_bmp(filename.c_str(), convertedSize.x, convertedSize.y, 4, &pixels[0])) return true; } else if (extension == "tga") { // TGA format - if (stbi_write_tga(filename.c_str(), size.x, size.y, 4, &pixels[0])) + if (stbi_write_tga(filename.c_str(), convertedSize.x, convertedSize.y, 4, &pixels[0])) return true; } else if (extension == "png") { // PNG format - if (stbi_write_png(filename.c_str(), size.x, size.y, 4, &pixels[0], 0)) + if (stbi_write_png(filename.c_str(), convertedSize.x, convertedSize.y, 4, &pixels[0], 0)) return true; } else if (extension == "jpg" || extension == "jpeg") { // JPG format - if (stbi_write_jpg(filename.c_str(), size.x, size.y, 4, &pixels[0], 90)) + if (stbi_write_jpg(filename.c_str(), convertedSize.x, convertedSize.y, 4, &pixels[0], 90)) return true; } } @@ -290,29 +291,30 @@ bool ImageLoader::saveImageToMemory(const std::string& format, std::vector(point.x) - viewport.left) / viewport.width; + normalized.y = 1.f - 2.f * (static_cast(point.y) - viewport.top) / viewport.height; // Then transform by the inverse of the view matrix return view.getInverseTransform().transformPoint(normalized); @@ -250,7 +250,7 @@ Vector2i RenderTarget::mapCoordsToPixel(const Vector2f& point, const View& view) // Then convert to viewport coordinates Vector2i pixel; - IntRect viewport = getViewport(view); + FloatRect viewport = FloatRect(getViewport(view)); pixel.x = static_cast(( normalized.x + 1.f) / 2.f * viewport.width + viewport.left); pixel.y = static_cast((-normalized.y + 1.f) / 2.f * viewport.height + viewport.top); @@ -583,7 +583,7 @@ void RenderTarget::applyCurrentView() { // Set the viewport IntRect viewport = getViewport(m_view); - int top = getSize().y - (viewport.top + viewport.height); + int top = static_cast(getSize().y) - (viewport.top + viewport.height); glCheck(glViewport(viewport.left, top, viewport.width, viewport.height)); // Set the projection matrix diff --git a/src/SFML/Graphics/Shape.cpp b/src/SFML/Graphics/Shape.cpp index ccf3ddf30c..d4cba9a0a7 100644 --- a/src/SFML/Graphics/Shape.cpp +++ b/src/SFML/Graphics/Shape.cpp @@ -67,7 +67,7 @@ void Shape::setTexture(const Texture* texture, bool resetRect) { // Recompute the texture area if requested, or if there was no texture & rect before if (resetRect || (!m_texture && (m_textureRect == IntRect()))) - setTextureRect(IntRect(0, 0, texture->getSize().x, texture->getSize().y)); + setTextureRect(IntRect(0, 0, static_cast(texture->getSize().x), static_cast(texture->getSize().y))); } // Assign the new texture @@ -238,12 +238,14 @@ void Shape::updateFillColors() //////////////////////////////////////////////////////////// void Shape::updateTexCoords() { + FloatRect convertedTextureRect = FloatRect(m_textureRect); + for (std::size_t i = 0; i < m_vertices.getVertexCount(); ++i) { float xratio = m_insideBounds.width > 0 ? (m_vertices[i].position.x - m_insideBounds.left) / m_insideBounds.width : 0; float yratio = m_insideBounds.height > 0 ? (m_vertices[i].position.y - m_insideBounds.top) / m_insideBounds.height : 0; - m_vertices[i].texCoords.x = m_textureRect.left + m_textureRect.width * xratio; - m_vertices[i].texCoords.y = m_textureRect.top + m_textureRect.height * yratio; + m_vertices[i].texCoords.x = convertedTextureRect.left + convertedTextureRect.width * xratio; + m_vertices[i].texCoords.y = convertedTextureRect.top + convertedTextureRect.height * yratio; } } diff --git a/src/SFML/Graphics/Sprite.cpp b/src/SFML/Graphics/Sprite.cpp index 6a23258c9e..100ac67346 100644 --- a/src/SFML/Graphics/Sprite.cpp +++ b/src/SFML/Graphics/Sprite.cpp @@ -68,7 +68,7 @@ void Sprite::setTexture(const Texture& texture, bool resetRect) // Recompute the texture area if requested, or if there was no valid texture & rect before if (resetRect || (!m_texture && (m_textureRect == sf::IntRect()))) { - Vector2u size = texture.getSize(); + Vector2i size = Vector2i(texture.getSize()); setTextureRect(IntRect(0, 0, size.x, size.y)); } @@ -165,10 +165,12 @@ void Sprite::updatePositions() //////////////////////////////////////////////////////////// void Sprite::updateTexCoords() { - float left = static_cast(m_textureRect.left); - float right = left + m_textureRect.width; - float top = static_cast(m_textureRect.top); - float bottom = top + m_textureRect.height; + FloatRect convertedTextureRect = FloatRect(m_textureRect); + + float left = convertedTextureRect.left; + float right = left + convertedTextureRect.width; + float top = convertedTextureRect.top; + float bottom = top + convertedTextureRect.height; m_vertices[0].texCoords = Vector2f(left, top); m_vertices[1].texCoords = Vector2f(left, bottom); diff --git a/src/SFML/Graphics/Texture.cpp b/src/SFML/Graphics/Texture.cpp index 3138d90707..42a499ef9d 100644 --- a/src/SFML/Graphics/Texture.cpp +++ b/src/SFML/Graphics/Texture.cpp @@ -273,7 +273,7 @@ bool Texture::loadFromImage(const Image& image, const IntRect& area) if (rectangle.top + rectangle.height > height) rectangle.height = height - rectangle.top; // Create the texture and upload the pixels - if (create(rectangle.width, rectangle.height)) + if (create(static_cast(rectangle.width), static_cast(rectangle.height))) { TransientContextLock lock; @@ -367,8 +367,8 @@ Image Texture::copyToImage() const // Then we copy the useful pixels from the temporary array to the final one const Uint8* src = &allPixels[0]; Uint8* dst = &pixels[0]; - int srcPitch = m_actualSize.x * 4; - int dstPitch = m_size.x * 4; + unsigned int srcPitch = m_actualSize.x * 4; + unsigned int dstPitch = m_size.x * 4; // Handle the case where source pixels are flipped vertically if (m_pixelsFlipped) @@ -753,15 +753,15 @@ void Texture::bind(const Texture* texture, CoordinateType coordinateType) // setup scale factors that convert the range [0 .. size] to [0 .. 1] if (coordinateType == Pixels) { - matrix[0] = 1.f / texture->m_actualSize.x; - matrix[5] = 1.f / texture->m_actualSize.y; + matrix[0] = 1.f / static_cast(texture->m_actualSize.x); + matrix[5] = 1.f / static_cast(texture->m_actualSize.y); } // If pixels are flipped we must invert the Y axis if (texture->m_pixelsFlipped) { matrix[5] = -matrix[5]; - matrix[13] = static_cast(texture->m_size.y) / texture->m_actualSize.y; + matrix[13] = static_cast(texture->m_size.y) / static_cast(texture->m_actualSize.y); } // Load the matrix diff --git a/src/SFML/Graphics/Transformable.cpp b/src/SFML/Graphics/Transformable.cpp index f1b1144487..27fda57f50 100644 --- a/src/SFML/Graphics/Transformable.cpp +++ b/src/SFML/Graphics/Transformable.cpp @@ -71,7 +71,7 @@ void Transformable::setPosition(const Vector2f& position) //////////////////////////////////////////////////////////// void Transformable::setRotation(float angle) { - m_rotation = static_cast(fmod(angle, 360)); + m_rotation = std::fmod(angle, 360.f); if (m_rotation < 0) m_rotation += 360.f; diff --git a/src/SFML/Graphics/View.cpp b/src/SFML/Graphics/View.cpp index 5a77b930a5..94037a5731 100644 --- a/src/SFML/Graphics/View.cpp +++ b/src/SFML/Graphics/View.cpp @@ -108,7 +108,7 @@ void View::setSize(const Vector2f& size) //////////////////////////////////////////////////////////// void View::setRotation(float angle) { - m_rotation = static_cast(fmod(angle, 360)); + m_rotation = std::fmod(angle, 360.f); if (m_rotation < 0) m_rotation += 360.f;