文字を回転させて描画する方法
おがめ 2023/04/03(Mon) 03:17
Reputeless 2023/04/06(Thu) 18:14
@ テキスト全体を回転させる場合
# include <Siv3D.hpp> // OpenSiv3D v0.6.8
void Main()
{
const Font font{ FontMethod::MSDF, 48, Typeface::Bold };
while (System::Update())
{
const Vec2 center{ 400, 300 };
const double angle = (Scene::Time() * 30_deg);
Circle{ center, 10 }.draw(Palette::Red);
{
const Transformer2D tr{ Mat3x2::Rotate(angle, center) };
font(U"Hello, Siv3D!").drawAt(80, center);
}
}
}
A 文章中の文字を個別に回転させる場合
# include <Siv3D.hpp> // OpenSiv3D v0.6.8
void Main()
{
const Font font{ 32, Typeface::Bold };
const String text = U"Lorem ipsum dolor sit amet, consectetur\n"
U"adipiscing elit, sed do eiusmod tempor\n"
U"incididunt ut labore et dolore magna aliqua.";
while (System::Update())
{
const double angle = (Scene::Time() * 30_deg);
{
const Vec2 pos{ 40, 40 };
Vec2 penPos = pos;
for (const auto& glyph : font.getGlyphs(text))
{
if (glyph.codePoint == U'\n')
{
penPos.x = pos.x;
penPos.y += font.height();
continue;
}
glyph.texture.rotated(angle).draw(penPos + glyph.getOffset());
penPos.x += glyph.xAdvance;
}
}
}
}
B 1 つの文字を回転させる場合
# include <Siv3D.hpp> // OpenSiv3D v0.6.8
void Main()
{
const Font font{ FontMethod::MSDF, 48, Typeface::Bold };
const Glyph glyph = font.getGlyph(U'転');
while (System::Update())
{
const double angle = (Scene::Time() * 30_deg);
{
// MSDF フォントの描画のための設定
const ScopedCustomShader2D shader{ Font::GetPixelShader(font.method()) };
glyph.texture.scaled(2.0 + Periodic::Sine0_1(2s) * 4.0).rotated(angle).drawAt(Cursor::Pos());
}
}
}