Skip to content

Commit

Permalink
Fix some MSVC warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
mogemimi committed Aug 8, 2018
1 parent f68ffbd commit fde05cb
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/Experimental/Graphics/LineBatch.cpp
Expand Up @@ -314,7 +314,7 @@ void LineBatch::DrawCircle(const Vector2& position, float radius, const Color& c
auto colorVector = color.ToVector4();

for (int i = 0; i < segments; ++i) {
auto rad = centralAngle * (i + 1);
auto rad = centralAngle * static_cast<float>(i + 1);
auto cos = std::cos(rad.value);
auto sin = std::sin(rad.value);
auto nextPoint = position + (radius * Vector2{cos, sin});
Expand Down
4 changes: 2 additions & 2 deletions src/Experimental/Graphics/PolygonShapeBuilder.cpp
Expand Up @@ -85,7 +85,7 @@ void PolygonShapeBuilder::DrawArc(
std::min(arcAngle.value, Math::TwoPi<float>) / segments;

auto computePoint = [&](int index) {
auto rad = startAngle + centralAngle * index;
auto rad = startAngle + centralAngle * static_cast<float>(index);
auto cos = std::cos(rad.value);
auto sin = std::sin(rad.value);
return center + Vector3{radius * Vector2{cos, sin}, 0};
Expand Down Expand Up @@ -221,7 +221,7 @@ void PolygonShapeBuilder::DrawCircle(
auto colorVector = color.ToVector4();

for (int i = 0; i < segments; ++i) {
auto rad = centralAngle * (i + 1);
auto rad = centralAngle * static_cast<float>(i + 1);
auto cos = std::cos(rad.value);
auto sin = std::sin(rad.value);
auto nextPoint = position + Vector3{radius * Vector2{cos, sin}, 0};
Expand Down
10 changes: 5 additions & 5 deletions src/Experimental/Graphics/SpriteFont.cpp
Expand Up @@ -175,7 +175,7 @@ void SpriteFont::Impl::PrepareFonts(const std::string& text)
}
if (currentPoint.Y + glyphHeight + 1 >= TextureHeight) {
fetchTextureData();
std::fill(std::begin(pixelData), std::end(pixelData), 0);
std::fill(std::begin(pixelData), std::end(pixelData), static_cast<std::uint8_t>(0));

auto textureNew = std::make_shared<Texture2D>(graphicsDevice,
TextureWidth, TextureHeight, false, SurfaceFormat::R8G8B8A8_UNorm);
Expand All @@ -199,7 +199,7 @@ void SpriteFont::Impl::PrepareFonts(const std::string& text)
bottomY = std::max(bottomY, currentPoint.Y + glyph->Subrect.Height + 1);

POMDOG_ASSERT(!textures.empty() && textures.size() > 0);
glyph->TexturePage = textures.size() - 1;
glyph->TexturePage = static_cast<std::int16_t>(textures.size()) - 1;

spriteFontMap.emplace(glyph->Character, *glyph);
needToFetchPixelData = true;
Expand Down Expand Up @@ -296,7 +296,7 @@ void SpriteFont::Impl::Draw(
POMDOG_ASSERT(glyph.TexturePage < static_cast<int>(textures.size()));

spriteBatch.Draw(textures[glyph.TexturePage],
currentPosition + Vector2(glyph.XOffset, -glyph.YOffset),
currentPosition + Vector2{static_cast<float>(glyph.XOffset), static_cast<float>(-glyph.YOffset)},
glyph.Subrect, color, 0.0f, Vector2{0.0f, 1.0f}, Vector2{1.0f, 1.0f});
}

Expand Down Expand Up @@ -353,8 +353,8 @@ void SpriteFont::Impl::Draw(
POMDOG_ASSERT(glyph.TexturePage < static_cast<int>(textures.size()));

spriteBatch.Draw(textures[glyph.TexturePage],
currentPosition + Vector2(glyph.XOffset, -glyph.YOffset) * scale,
glyph.Subrect, color, 0.0f, Vector2{0.0f, 1.0f}, scale);
currentPosition + Vector2{static_cast<float>(glyph.XOffset), static_cast<float>(-glyph.YOffset)} *scale,
glyph.Subrect, color, rotation, Vector2{0.0f, 1.0f}, scale);
}

currentPosition.X += ((glyph.XAdvance - spacing) * scale.X);
Expand Down
77 changes: 57 additions & 20 deletions src/Experimental/Graphics/SpriteFontLoader.cpp
Expand Up @@ -110,7 +110,8 @@ BitmapFontInfo ParseInfo(std::istream & stream)
}
}
else if (name == "size") {
info.Size = std::stoul(arguments);
static_assert(std::is_same<decltype(info.Size), std::uint16_t>::value, "");
info.Size = static_cast<std::uint16_t>(std::stoi(arguments));
}
else if (name == "bold") {
info.Bold = std::stoul(arguments) != 0;
Expand All @@ -128,7 +129,8 @@ BitmapFontInfo ParseInfo(std::istream & stream)
info.Unicode = std::stoul(arguments) != 0;
}
else if (name == "stretchH") {
info.StretchHeight = std::stoi(arguments);
static_assert(std::is_same<decltype(info.StretchHeight), std::uint16_t>::value, "");
info.StretchHeight = static_cast<std::uint16_t>(std::stoi(arguments));
}
else if (name == "smooth") {
info.Smooth = std::stoul(arguments) != 0;
Expand All @@ -138,18 +140,24 @@ BitmapFontInfo ParseInfo(std::istream & stream)
}
else if (name == "padding") {
if (std::regex_match(arguments, match2, exprVector4)) {
static_assert(std::is_same<decltype(info.PaddingTop), std::uint16_t>::value, "");
static_assert(std::is_same<decltype(info.PaddingRight), std::uint16_t>::value, "");
static_assert(std::is_same<decltype(info.PaddingBottom), std::uint16_t>::value, "");
static_assert(std::is_same<decltype(info.PaddingLeft), std::uint16_t>::value, "");
POMDOG_ASSERT(match2.size() >= 5);
info.PaddingTop = std::stoi(match2[1]);
info.PaddingRight = std::stoi(match2[2]);
info.PaddingBottom = std::stoi(match2[3]);
info.PaddingLeft = std::stoi(match2[4]);
info.PaddingTop = static_cast<std::uint16_t>(std::stoi(match2[1]));
info.PaddingRight = static_cast<std::uint16_t>(std::stoi(match2[2]));
info.PaddingBottom = static_cast<std::uint16_t>(std::stoi(match2[3]));
info.PaddingLeft = static_cast<std::uint16_t>(std::stoi(match2[4]));
}
}
else if (name == "spacing") {
if (std::regex_match(arguments, match2, exprVector2)) {
static_assert(std::is_same<decltype(info.SpacingX), std::uint16_t>::value, "");
static_assert(std::is_same<decltype(info.SpacingY), std::uint16_t>::value, "");
POMDOG_ASSERT(match2.size() >= 3);
info.SpacingX = std::stoi(match2[1]);
info.SpacingY = std::stoi(match2[2]);
info.SpacingX = static_cast<std::uint16_t>(std::stoi(match2[1]));
info.SpacingY = static_cast<std::uint16_t>(std::stoi(match2[2]));
}
}
}
Expand All @@ -174,19 +182,24 @@ BitmapFontCommon ParseCommon(std::istream & stream)
auto arguments = match[3].str();

if (name == "lineHeight") {
result.LineHeight = std::stoul(arguments);
static_assert(std::is_same<decltype(result.LineHeight), std::uint16_t>::value, "");
result.LineHeight = static_cast<std::uint16_t>(std::stoi(arguments));
}
else if (name == "base") {
result.Base = std::stoul(arguments);
static_assert(std::is_same<decltype(result.Base), std::uint16_t>::value, "");
result.Base = static_cast<std::uint16_t>(std::stoi(arguments));
}
else if (name == "scaleW") {
result.ScaleWidth = std::stoul(arguments);
static_assert(std::is_same<decltype(result.ScaleWidth), std::uint16_t>::value, "");
result.ScaleWidth = static_cast<std::uint16_t>(std::stoi(arguments));
}
else if (name == "scaleH") {
result.ScaleHeight = std::stoul(arguments);
static_assert(std::is_same<decltype(result.ScaleHeight), std::uint16_t>::value, "");
result.ScaleHeight = static_cast<std::uint16_t>(std::stoi(arguments));
}
else if (name == "pages") {
result.Pages = std::stoul(arguments);
static_assert(std::is_same<decltype(result.Pages), std::uint16_t>::value, "");
result.Pages = static_cast<std::uint16_t>(std::stoi(arguments));
}
else if (name == "packed") {
result.Packed = std::stoul(arguments) != 0;
Expand Down Expand Up @@ -222,7 +235,8 @@ BitmapFontPage ParsePage(std::istream & stream)
}
}
else if (name == "id") {
result.Id = std::stoul(arguments);
static_assert(std::is_same<decltype(result.Id), std::uint16_t>::value, "");
result.Id = static_cast<std::uint16_t>(std::stoul(arguments));
}
}
}
Expand All @@ -233,6 +247,15 @@ BitmapFontPage ParsePage(std::istream & stream)
FontGlyph ParseGlyph(std::istream & stream)
{
FontGlyph result;
result.Subrect.X = 0;
result.Subrect.Y = 0;
result.Subrect.Width = 1;
result.Subrect.Height = 1;
result.Character = 0;
result.XOffset = 0;
result.YOffset = 0;
result.XAdvance = 0;
result.TexturePage = 0;

std::string source;
while (stream >> source && !stream.fail()) {
Expand All @@ -247,7 +270,8 @@ FontGlyph ParseGlyph(std::istream & stream)
result.Character = std::stoi(arguments);
}
else if (name == "page") {
result.TexturePage = std::stoi(arguments);
static_assert(std::is_same<decltype(result.TexturePage), std::int16_t>::value, "");
result.TexturePage = static_cast<std::int16_t>(std::stoi(arguments));
}
else if (name == "x") {
result.Subrect.X = std::stoi(arguments);
Expand All @@ -262,13 +286,16 @@ FontGlyph ParseGlyph(std::istream & stream)
result.Subrect.Height = std::stoi(arguments);
}
else if (name == "xoffset") {
result.XOffset = std::stoi(arguments);
static_assert(std::is_same<decltype(result.XOffset), std::int16_t>::value, "");
result.XOffset = static_cast<std::int16_t>(std::stoi(arguments));
}
else if (name == "yoffset") {
result.YOffset = std::stoi(arguments);
static_assert(std::is_same<decltype(result.YOffset), std::int16_t>::value, "");
result.YOffset = static_cast<std::int16_t>(std::stoi(arguments));
}
else if (name == "xadvance") {
result.XAdvance = std::stoi(arguments);
static_assert(std::is_same<decltype(result.XAdvance), std::int16_t>::value, "");
result.XAdvance = static_cast<std::int16_t>(std::stoi(arguments));
}
}
}
Expand Down Expand Up @@ -359,8 +386,18 @@ std::shared_ptr<SpriteFont> SpriteFontLoader::Load(
POMDOG_ASSERT(!glyphs.empty());
auto defaultCharacter = glyphs.front().Character;

auto spriteFont = std::make_shared<SpriteFont>(std::move(textures), glyphs,
defaultCharacter, info.PaddingLeft + info.PaddingRight, common.LineHeight);
// FIXME: Replace the types of the following variables with signed 16-bit integer (std::int16_t).
static_assert(std::is_same<decltype(info.PaddingLeft), std::uint16_t>::value, "");
static_assert(std::is_same<decltype(info.PaddingRight), std::uint16_t>::value, "");
static_assert(std::is_same<decltype(common.LineHeight), std::uint16_t>::value, "");

auto spriteFont = std::make_shared<SpriteFont>(
std::move(textures),
std::move(glyphs),
defaultCharacter,
static_cast<std::int16_t>(info.PaddingLeft + info.PaddingRight),
static_cast<std::int16_t>(common.LineHeight));

return spriteFont;
}

Expand Down
11 changes: 7 additions & 4 deletions src/Experimental/Graphics/TrueTypeFont.cpp
Expand Up @@ -98,8 +98,9 @@ Optional<FontGlyph> TrueTypeFont::RasterizeGlyph(

const int g = stbtt_FindGlyphIndex(&f, codePoint);

// FIXME: Use `UnicodeData.txt`-generated character table instead of std::locale and std::isspace.
std::locale defaultLocale;
const bool isSpace = (std::isspace<char>(codePoint, defaultLocale) != 0) && (codePoint != '\n');
const bool isSpace = (std::isspace(static_cast<char>(codePoint), defaultLocale) != 0) && (codePoint != '\n');

if (g <= 0 && !isSpace) {
// error: not found
Expand Down Expand Up @@ -128,15 +129,17 @@ Optional<FontGlyph> TrueTypeFont::RasterizeGlyph(
glyphWidth, glyphHeight, textureWidth, scale, scale, g);
}

POMDOG_ASSERT(static_cast<int>(scale * advance) <= static_cast<int>(std::numeric_limits<std::int16_t>::max()));

FontGlyph glyph;
glyph.Subrect.X = point.X;
glyph.Subrect.Y = point.Y;
glyph.Subrect.Width = glyphWidth;
glyph.Subrect.Height = glyphHeight;
glyph.TexturePage = 0;
glyph.XAdvance = scale * advance;
glyph.XOffset = x0;
glyph.YOffset = y0;
glyph.XAdvance = static_cast<std::int16_t>(scale * advance);
glyph.XOffset = static_cast<std::int16_t>(x0);
glyph.YOffset = static_cast<std::int16_t>(y0);
glyph.Character = codePoint;
return std::move(glyph);
}
Expand Down

0 comments on commit fde05cb

Please sign in to comment.