長方形をランダムで表示
の続き。
折角正方形が表示できたので色々といじってみた。
50×50が最小サイズのランダムな大きさと色の長方形をランダムな位置に表示する。
また長方形はアルファブレンドを使って半透明表示させる。
半透明度度合いもランダムに設定してあり、時間とともに透過値をあげ、見えなくなった時点でその長方形をランダムな情報を持って再度初期化し表示する。
とりあえず長方形を管理するクラスを作ってみた。宣言はこんな感じ
// testbox.h #pragma once class TestBox { private: static const int BOX_SIZE = 50; bool m_active; float m_x; float m_y; float m_width; float m_height; DWORD m_rgba; int m_count; public: TestBox (); void Init (); void Update (); bool isActive () const; DWORD getRGBA () const; float getX () const; float getY () const; float getWidht () const; float getHeight () const; };
定義は一部抜粋
TestBox::TestBox () { m_count = 0; m_active = false; } void TestBox::Init () { m_count = (rand()%200)+55; m_active = true; m_rgba = (m_count<<24) + // A ((rand()%255)<<16) + // R ((rand()%255)<<8) + // G (rand()%255); // B m_x = static_cast<float>(rand() % (SCREEN_WIDTH - (BOX_SIZE + 50))); m_y = static_cast<float>(rand() % (SCREEN_HEIGHT - (BOX_SIZE + 50))); m_width = rand() % BOX_SIZE + 50.0f; m_height = rand() % BOX_SIZE + 50.0f; } void TestBox::Update () { --m_count; if ( m_count <= 0 ) { m_active = false; } else { m_rgba = m_rgba & 0x00ffffff; m_rgba += m_count<<24; } }
RGBAを扱う部分がちょっと見栄えが悪いかも。毎回こんなコード書きたくないのでなんとかしないとだめだな。
実行結果
ぶっちゃけ結果だけ画像でみてもどんな感じのものかわかりにくいね。