Help us understand the problem. What is going on with this article?

Siv3D のコードを短くする

More than 1 year has passed since last update.

Siv3D でコードを短く読みやすくするために知っておきたい Tips を紹介します。

1. RGB の省略

グレー

before.cpp
rect.draw(Color(127, 127, 127));
after.cpp
rect.draw(Color(127));
// または
rect.draw(ColorF(0.5));

半透明

before.cpp
texture.draw(ColorF(255, 255, 255, 127));
after.cpp
texture.draw(Alpha(127));
// または
texture.draw(AlphaF(0.5));

2. 画面と同じ大きさの Rect

before.cpp
const Rect rect(0, 0, Window::Width(), Window::Height());
after.cpp
const Rect rect = Window::ClientRect();

3. 正方形

before.cpp
const Rect rect(200, 300, 80, 80);

const Rect rect2(0, 0, 80, 80);
after.cpp
const Rect rect(200, 300, 80);

const Rect rect2(80);

4. 一回り大きい / 小さい Rect

before.cpp
const Rect rect2(rect.x - 10, rect.y - 5. rect.w + 20, rect.h + 10);
after.cpp
const Rect rect2 = rect.stretched(10, 5);

5. 指定した条件を満たす要素の削除

before.cpp
Array<Vec2> v;

for (auto it = v.begin(); it != v.end();)
{
    if (it->y < 0)
    {
        it = v.erase(it);
    }
    else
    {
        ++it;
    }
}
after.cpp
Array<Vec2> v;

Erase_if(v, [](const Vec2& p) { return p.y < 0; });

OpenSiv3D では

after.cpp
Array<Vec2> v;

v.remove_if([](const Vec2& p) { return p.y < 0; });

6. たくさんの数値の文字列化

before.cpp
font(L"ステージ ", stage, L" 得点: ", score, L" タイム: ", time, L" 評価: ", result).draw(); 
after.cpp
font(L"ステージ {} 得点: {} タイム: {} 評価: {}"_fmt, stage, score, time, result).draw(); 

OpenSiv3D では

after.cpp
font(L"ステージ {} 得点: {} タイム: {} 評価: {}"_fmt(stage, score, time, result)).draw(); 

7. ストップウォッチを即座にスタート

before.cpp
Stopwatch stopwatch;

stopwatch.start();
after.cpp
Stopwatch stopwatch(true);

8. emplace_back()

before.cpp
Array<Vec3> v;

v.push_back(Vec3(0.1, 0.2, 0.3));
after.cpp
Array<Vec3> v;

v.emplace_back(0.1, 0.2, 0.3);

9. 画像の全ピクセルへのアクセス

before.cpp
for (int32 y = 0; y < image.height; ++y)
{
    for (int32 x = 0; x < image.width; ++x)
    {
        image[y][x].r = 0;
    }
}
after.cpp
for (auto& pixel : image)
{
    pixel.r = 0;
}

10. 時刻や日付の文字列化

before.cpp
const Date today = Date::Today();

font(today.month, L"月", today.day, L"日").draw();
after.cpp
const Date today = Date::Today();

font(today.format(L"M月d日")).draw();

11. 確率

before.cpp
if (Random(1.0) < 0.33)
{

}
after.cpp
if (RandomBool(0.33))
{

}

12. draw の戻り値

before.cpp
const Rect rect(200, 200, 300, 100);

rect.draw(Palette::Orange);

rect.drawFrame(2, 0, Palette::White);
after.cpp
const Rect rect(200, 200, 300, 100);

rect.draw(Palette::Orange).drawFrame(2, 0, Palette::White);

Siv3D でこのコードをもっと短く書きたい!というリクエストは、OpenSiv3D の GitHub issue や Siv3D Slack で受け付けています。

Reputeless
小さくなってもコードはモダン!▼ 未定義動作なしの名コーダー!▼ 実装はいつも 3 つ!!(GCC/Clang/MSVC)▼ OpenSiv3D, cppmap 作者
https://ryo-suzuki-contact.github.io/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした