Last active
December 8, 2016 13:35
-
-
Save Hatanas/7860928d72dd99630f5c3863e34b63cc to your computer and use it in GitHub Desktop.
DrawableList
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Drawable.h" | |
Drawable::Drawable() | |
: m_isVisible(true) | |
{ | |
} | |
void Drawable::show() | |
{ | |
m_isVisible = true; | |
} | |
void Drawable::hide() | |
{ | |
m_isVisible = false; | |
} | |
bool Drawable::isVisible() const | |
{ | |
return m_isVisible; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
/// <summary> | |
/// 描画するオブジェクトのインターフェース | |
/// </summary> | |
class Drawable | |
{ | |
private: | |
bool m_isVisible; | |
public: | |
/// <summary> | |
/// コンストラクタ | |
/// </summary> | |
Drawable(); | |
/// <summary> | |
/// デストラクタ | |
/// </summary> | |
virtual ~Drawable() = default; | |
public: | |
/// <summary> | |
/// 描画する | |
/// </summary> | |
virtual void draw() const = 0; | |
/// <summary> | |
/// オブジェクトを表示する | |
/// </summary> | |
void show(); | |
/// <summary> | |
/// オブジェクトを非表示にする | |
/// </summary> | |
void hide(); | |
/// <summary> | |
/// オブジェクトの表示状態を取得する | |
/// </summary> | |
/// <returns> 表示状態 </returns> | |
bool isVisible() const; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "DrawableList.h" | |
void DrawableList::add(std::shared_ptr<Drawable> drawable, int layer) | |
{ | |
m_drawables.insert(std::make_pair(layer, drawable)); | |
} | |
void DrawableList::drawAll() const | |
{ | |
std::for_each(m_drawables.begin(), m_drawables.end(), [](auto p) { | |
if(p.second->isVisible()) { | |
p.second->draw(); | |
} | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <map> | |
#include <memory> | |
#include <algorithm> | |
#include "Drawable.h" | |
class DrawableList | |
{ | |
private: | |
std::multimap<int, std::shared_ptr<Drawable>> m_drawables; | |
public: | |
/// <summary> | |
/// コンストラクタ | |
/// </summary> | |
DrawableList() = default; | |
/// <summary> | |
/// デストラクタ | |
/// </summary> | |
~DrawableList() = default; | |
public: | |
/// <summary> | |
/// 描画リストに追加する | |
/// </summary> | |
/// <param name="drawable"> 描画するオブジェクト </param> | |
/// <param name="layer"> レイヤー番号 </param> | |
void add(std::shared_ptr<Drawable> drawable, int layer); | |
/// <summary> | |
/// 全てのオブジェクトを描画する | |
/// </summary> | |
void drawAll() const; | |
}; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Siv3D.hpp> | |
#include "Drawable.h" | |
#include "DrawableList.h" | |
using namespace std; | |
// ただの半透明な四角形オブジェクト | |
class Tile : public Drawable | |
{ | |
private: | |
const Point m_pos; | |
public: | |
Tile(Point position) : m_pos(position) {} | |
~Tile() = default; | |
public: | |
void draw() const override | |
{ | |
Rect(m_pos, {300, 200}).draw(ColorF(Palette::Orange, 0.5)); | |
} | |
}; | |
// マウスカーソル周りの円形オブジェクト | |
class MouseCircle : public Drawable | |
{ | |
private: | |
Vec2 m_pos; | |
public: | |
MouseCircle() = default; | |
~MouseCircle() = default; | |
public: | |
void update(Vec2 mousePos) | |
{ | |
m_pos = mousePos; | |
} | |
void draw() const override | |
{ | |
Circle(m_pos, 50).draw(ColorF(Palette::Blue, 0.5)); | |
} | |
}; | |
void Main() | |
{ | |
// DrawableListの生成 | |
DrawableList m_drawableList; | |
// 描画オブジェクトの生成 | |
shared_ptr<Tile> tile1 = make_shared<Tile>(Point(100, 100)); | |
shared_ptr<Tile> tile2 = make_shared<Tile>(Point(300, 200)); | |
shared_ptr<MouseCircle> circle = make_shared<MouseCircle>(); | |
// DrawableListへ登録する | |
m_drawableList.add(tile1, 3); | |
m_drawableList.add(tile2, 1); | |
m_drawableList.add(circle, 2); | |
while(System::Update()) { | |
circle->update(Mouse::Pos()); | |
// 描画処理で呼ぶ関数はこれだけ | |
m_drawableList.drawAll(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment