Skip to content

Commit

Permalink
Fix conversion warnings for the Graphics module
Browse files Browse the repository at this point in the history
- Fix conversion and shadowing warnings
- Add SYSTEM indicator for stb_*, FreeType and other headers
  • Loading branch information
eXpl0it3r committed Apr 22, 2021
1 parent d88acdd commit bc802c0
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 82 deletions.
2 changes: 1 addition & 1 deletion cmake/Macros.cmake
Expand Up @@ -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 "$<BUILD_INTERFACE:${include_dir}>")
target_include_directories(${target} SYSTEM INTERFACE "$<BUILD_INTERFACE:${include_dir}>")
endforeach()
endif()

Expand Down
2 changes: 1 addition & 1 deletion src/SFML/Graphics/CMakeLists.txt
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion src/SFML/Graphics/CircleShape.cpp
Expand Up @@ -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<float>(index) * 2.f * pi / static_cast<float>(m_pointCount) - pi / 2.f;
float x = std::cos(angle) * m_radius;
float y = std::sin(angle) * m_radius;

Expand Down
2 changes: 1 addition & 1 deletion src/SFML/Graphics/Color.cpp
Expand Up @@ -81,7 +81,7 @@ a((color & 0x000000ff) >> 0 )
////////////////////////////////////////////////////////////
Uint32 Color::toInteger() const
{
return (r << 24) | (g << 16) | (b << 8) | a;
return static_cast<Uint32>((r << 24) | (g << 16) | (b << 8) | a);
}


Expand Down
43 changes: 22 additions & 21 deletions src/SFML/Graphics/Font.cpp
Expand Up @@ -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<sf::Int64>(offset);
sf::InputStream* stream = static_cast<sf::InputStream*>(rec->descriptor.pointer);
if (static_cast<unsigned long>(stream->seek(offset)) == offset)
if (stream->seek(convertedOffset) == convertedOffset)
{
if (count > 0)
return static_cast<unsigned long>(stream->read(reinterpret_cast<char*>(buffer), count));
return static_cast<unsigned long>(stream->read(reinterpret_cast<char*>(buffer), static_cast<sf::Int64>(count)));
else
return 0;
}
Expand Down Expand Up @@ -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<float>(characterSize) / 10.f;

return -static_cast<float>(FT_MulFix(face->underline_position, face->size->metrics.y_scale)) / static_cast<float>(1 << 6);
}
Expand All @@ -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<float>(characterSize) / 14.f;

return static_cast<float>(FT_MulFix(face->underline_thickness, face->size->metrics.y_scale)) / static_cast<float>(1 << 6);
}
Expand Down Expand Up @@ -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<FT_OutlineGlyph>(glyphDesc);
FT_Outline_Embolden(&outlineGlyph->outline, weight);
}

Expand Down Expand Up @@ -632,11 +633,11 @@ Glyph Font::loadGlyph(Uint32 codePoint, unsigned int characterSize, bool bold, f
if (bold)
glyph.advance += static_cast<float>(weight) / static_cast<float>(1 << 6);

glyph.lsbDelta = face->glyph->lsb_delta;
glyph.rsbDelta = face->glyph->rsb_delta;
glyph.lsbDelta = static_cast<int>(face->glyph->lsb_delta);
glyph.rsbDelta = static_cast<int>(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))
{
Expand All @@ -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
Expand Down Expand Up @@ -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<unsigned int>(glyph.textureRect.left) - padding;
unsigned int y = static_cast<unsigned int>(glyph.textureRect.top) - padding;
unsigned int w = static_cast<unsigned int>(glyph.textureRect.width) + 2 * padding;
unsigned int h = static_cast<unsigned int>(glyph.textureRect.height) + 2 * padding;
page.texture.update(&m_pixelBuffer[0], w, h, x, y);
}

Expand All @@ -735,7 +736,7 @@ IntRect Font::findGlyphRect(Page& page, unsigned int width, unsigned int height)
float bestRatio = 0;
for (std::vector<Row>::iterator it = page.rows.begin(); it != page.rows.end() && !row; ++it)
{
float ratio = static_cast<float>(height) / it->height;
float ratio = static_cast<float>(height) / static_cast<float>(it->height);

// Ignore rows that are either too small or too high
if ((ratio < 0.7f) || (ratio > 1.f))
Expand All @@ -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
Expand Down Expand Up @@ -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<unsigned int>(row->width, row->top, width, height));

// Update the row informations
row->width += width;
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down
34 changes: 17 additions & 17 deletions src/SFML/Graphics/Image.cpp
Expand Up @@ -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<int>(source.m_size.x);
srcRect.height = static_cast<int>(source.m_size.y);
}
else
{
if (srcRect.left < 0) srcRect.left = 0;
if (srcRect.top < 0) srcRect.top = 0;
if (srcRect.width > static_cast<int>(source.m_size.x)) srcRect.width = source.m_size.x;
if (srcRect.height > static_cast<int>(source.m_size.y)) srcRect.height = source.m_size.y;
if (srcRect.width > static_cast<int>(source.m_size.x)) srcRect.width = static_cast<int>(source.m_size.x);
if (srcRect.height > static_cast<int>(source.m_size.y)) srcRect.height = static_cast<int>(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<unsigned int>(srcRect.width);
unsigned int height = static_cast<unsigned int>(srcRect.height);
if (destX + width > m_size.x) width = m_size.x - destX;
if (destY + height > m_size.y) height = m_size.y - destY;

Expand All @@ -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<std::size_t>(width) * 4;
unsigned int rows = height;
int srcStride = static_cast<int>(source.m_size.x) * 4;
int dstStride = static_cast<int>(m_size.x) * 4;
const Uint8* srcPixels = &source.m_pixels[0] + (static_cast<unsigned int>(srcRect.left) + static_cast<unsigned int>(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;
Expand All @@ -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;
Expand Down Expand Up @@ -308,8 +308,8 @@ void Image::flipHorizontally()

for (std::size_t y = 0; y < m_size.y; ++y)
{
std::vector<Uint8>::iterator left = m_pixels.begin() + y * rowSize;
std::vector<Uint8>::iterator right = m_pixels.begin() + (y + 1) * rowSize - 4;
std::vector<Uint8>::iterator left = m_pixels.begin() + static_cast<std::vector<Uint8>::iterator::difference_type>(y * rowSize);
std::vector<Uint8>::iterator right = m_pixels.begin() + static_cast<std::vector<Uint8>::iterator::difference_type>((y + 1) * rowSize - 4);

for (std::size_t x = 0; x < m_size.x / 2; ++x)
{
Expand All @@ -328,7 +328,7 @@ void Image::flipVertically()
{
if (!m_pixels.empty())
{
std::size_t rowSize = m_size.x * 4;
std::vector<Uint8>::iterator::difference_type rowSize = static_cast<std::vector<Uint8>::iterator::difference_type>(m_size.x * 4);

std::vector<Uint8>::iterator top = m_pixels.begin();
std::vector<Uint8>::iterator bottom = m_pixels.end() - rowSize;
Expand Down
40 changes: 21 additions & 19 deletions src/SFML/Graphics/ImageLoader.cpp
Expand Up @@ -115,13 +115,13 @@ bool ImageLoader::loadImageFromFile(const std::string& filename, std::vector<Uin
if (ptr)
{
// Assign the image properties
size.x = width;
size.y = height;
size.x = static_cast<unsigned int>(width);
size.y = static_cast<unsigned int>(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<std::size_t>(width * height * 4));
memcpy(&pixels[0], ptr, pixels.size());
}

Expand Down Expand Up @@ -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<unsigned int>(width);
size.y = static_cast<unsigned int>(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<std::size_t>(width * height * 4));
memcpy(&pixels[0], ptr, pixels.size());
}

Expand Down Expand Up @@ -214,13 +214,13 @@ bool ImageLoader::loadImageFromStream(InputStream& stream, std::vector<Uint8>& p
if (ptr)
{
// Assign the image properties
size.x = width;
size.y = height;
size.x = static_cast<unsigned int>(width);
size.y = static_cast<unsigned int>(height);

if (width && height)
{
// Copy the loaded pixels to the pixel buffer
pixels.resize(width * height * 4);
pixels.resize(static_cast<std::size_t>(width * height * 4));
memcpy(&pixels[0], ptr, pixels.size());
}

Expand Down Expand Up @@ -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;
}
}
Expand All @@ -290,29 +291,30 @@ bool ImageLoader::saveImageToMemory(const std::string& format, std::vector<sf::U
// Choose function based on format

std::string specified = toLower(format);
const Vector2i convertedSize = Vector2i(size);

if (specified == "bmp")
{
// BMP format
if (stbi_write_bmp_to_func(&bufferFromCallback, &output, size.x, size.y, 4, &pixels[0]))
if (stbi_write_bmp_to_func(&bufferFromCallback, &output, convertedSize.x, convertedSize.y, 4, &pixels[0]))
return true;
}
else if (specified == "tga")
{
// TGA format
if (stbi_write_tga_to_func(&bufferFromCallback, &output, size.x, size.y, 4, &pixels[0]))
if (stbi_write_tga_to_func(&bufferFromCallback, &output, convertedSize.x, convertedSize.y, 4, &pixels[0]))
return true;
}
else if (specified == "png")
{
// PNG format
if (stbi_write_png_to_func(&bufferFromCallback, &output, size.x, size.y, 4, &pixels[0], 0))
if (stbi_write_png_to_func(&bufferFromCallback, &output, convertedSize.x, convertedSize.y, 4, &pixels[0], 0))
return true;
}
else if (specified == "jpg" || specified == "jpeg")
{
// JPG format
if (stbi_write_jpg_to_func(&bufferFromCallback, &output, size.x, size.y, 4, &pixels[0], 90))
if (stbi_write_jpg_to_func(&bufferFromCallback, &output, convertedSize.x, convertedSize.y, 4, &pixels[0], 90))
return true;
}
}
Expand Down

0 comments on commit bc802c0

Please sign in to comment.