From fbc5bfc27f7e7ba860c1e62a66af07fc16c0a07e Mon Sep 17 00:00:00 2001 From: whitequark Date: Wed, 18 May 2016 09:40:50 +0000 Subject: [PATCH] Enable -Wfloat-conversion on Clang. This is a high-SNR warning that's enabled by default on MSVC and it has highlighted some bugs in glhelper.cpp (that are also fixed in this commit). Unfortunately GCC does not have an equivalent for that warning, and -Wconversion is very noisy. --- CMakeLists.txt | 3 +++ src/glhelper.cpp | 10 +++++----- src/mouse.cpp | 2 +- src/platform/gtkmain.cpp | 8 ++++---- src/resource.cpp | 2 +- 5 files changed, 14 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7a11806b..27716d62 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -55,6 +55,9 @@ endif() if(CMAKE_CXX_COMPILER_ID STREQUAL GNU OR CMAKE_CXX_COMPILER_ID STREQUAL Clang) set(WARNING_FLAGS "-Wall -Wextra -Wno-unused-parameter") + if(CMAKE_CXX_COMPILER_ID STREQUAL Clang) + set(WARNING_FLAGS "${WARNING_FLAGS} -Wfloat-conversion") + endif() set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${WARNING_FLAGS}") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 ${WARNING_FLAGS}") endif() diff --git a/src/glhelper.cpp b/src/glhelper.cpp index 08564b73..e08aa073 100644 --- a/src/glhelper.cpp +++ b/src/glhelper.cpp @@ -713,7 +713,7 @@ double ssglStrWidth(const std::string &str, double h) { LoadVectorFont(); - int width = 0; + double width = 0; for(char32_t codepoint : ReadUTF8(str)) { width += BuiltinVectorFont.GetGlyph(codepoint).advanceWidth; } @@ -728,11 +728,11 @@ static Vector PixelAlign(Vector v) { return v; } -static int DrawCharacter(const VectorFont::Glyph &glyph, Vector t, Vector o, Vector u, Vector v, - double scale, ssglLineFn *fn, void *fndata, bool gridFit) { - int advanceWidth = glyph.advanceWidth; +static double DrawCharacter(const VectorFont::Glyph &glyph, Vector t, Vector o, Vector u, Vector v, + double scale, ssglLineFn *fn, void *fndata, bool gridFit) { + double advanceWidth = glyph.advanceWidth; - int actualWidth, offsetX; + double actualWidth, offsetX; if(gridFit) { o.x += glyph.leftSideBearing; offsetX = glyph.leftSideBearing; diff --git a/src/mouse.cpp b/src/mouse.cpp index d013d4e0..a77e1e0f 100644 --- a/src/mouse.cpp +++ b/src/mouse.cpp @@ -1294,7 +1294,7 @@ void GraphicsWindow::MouseLeftDoubleClick(double mx, double my) { hStyle hs = c->disp.style; if(hs.v == 0) hs.v = Style::CONSTRAINT; ShowGraphicsEditControl((int)p2.x, (int)p2.y, - ssglStrFontSize(Style::TextHeight(hs)) * scale, + (int)(ssglStrFontSize(Style::TextHeight(hs)) * scale), editMinWidthChar, editValue); } } diff --git a/src/platform/gtkmain.cpp b/src/platform/gtkmain.cpp index dd4cef3a..97659f70 100644 --- a/src/platform/gtkmain.cpp +++ b/src/platform/gtkmain.cpp @@ -637,11 +637,11 @@ protected: private: int _w, _h; - void ij_to_xy(int i, int j, int &x, int &y) { + void ij_to_xy(double i, double j, int &x, int &y) { // Convert to xy (vs. ij) style coordinates, // with (0, 0) at center - x = i - _w / 2; - y = _h / 2 - j; + x = (int)i - _w / 2; + y = _h / 2 - (int)j; } }; @@ -1376,7 +1376,7 @@ protected: } virtual void on_scrollbar_value_changed() { - SS.TW.ScrollbarEvent(_scrollbar.get_adjustment()->get_value()); + SS.TW.ScrollbarEvent((int)_scrollbar.get_adjustment()->get_value()); } virtual void on_editing_done(Glib::ustring value) { diff --git a/src/resource.cpp b/src/resource.cpp index a271da94..808a7d57 100644 --- a/src/resource.cpp +++ b/src/resource.cpp @@ -88,7 +88,7 @@ static Pixmap ReadPNGIntoPixmap(png_struct *png_ptr, png_info *info_ptr) { Pixmap pixmap = {}; pixmap.width = png_get_image_width(png_ptr, info_ptr); pixmap.height = png_get_image_height(png_ptr, info_ptr); - pixmap.hasAlpha = png_get_color_type(png_ptr, info_ptr) & PNG_COLOR_MASK_ALPHA; + pixmap.hasAlpha = (png_get_color_type(png_ptr, info_ptr) & PNG_COLOR_MASK_ALPHA) != 0; size_t stride = pixmap.width * pixmap.GetBytesPerPixel(); if(stride % 4 != 0) stride += 4 - stride % 4;