SDXFrameWork  0.13
SDXFrameWork
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
Sprite.h
1 //Copyright © 2014 SDXFramework
2 //[License]GNU Affero General Public License, version 3
3 //[Contact]http://sourceforge.jp/projects/dxframework/
4 #pragma once
5 #include <Framework/ISprite.h>
6 #include <Framework/Anime.h>
7 
8 namespace SDX
9 {
12  class SpImage : public ISprite
13  {
14  private:
15  const Image *image;
16 
17  public:
19  SpImage(const Image *描画Image) :
20  image(描画Image)
21  {}
22 
23  void Draw(const IShape &座標) const override
24  {
25  const Point pos = { 座標.GetX() + gap.x, 座標.GetY() + gap.x };
26  const Point center = { axis.x + image->GetHeight() / 2, axis.y + image->GetHeight() / 2 };
27  image->DrawRotateAxis(pos, center, zoomX, zoomY, angle, isTurn);
28  }
29  };
30 
33  class SpImageS : public ISprite
34  {
35  private:
36  const ImagePack *imageS;
37  int index = 0;
38  public:
39 
41  SpImageS(const ImagePack *描画ImagePack) :
42  imageS(描画ImagePack)
43  {}
44 
46  void SetIndex(int コマ番号)
47  {
48  index = コマ番号;
49  }
50 
52  int GetIndex()
53  {
54  return index;
55  }
56 
57  void Draw(const IShape &座標) const override
58  {
59  const auto image = imageS->operator[](index);
60 
61  const Point pos = { 座標.GetX() + gap.x, 座標.GetY() + gap.y };
62  const Point center = { axis.x + image->GetHeight() / 2, axis.y + image->GetHeight() / 2 };
63 
64  image->DrawRotateAxis(pos, center, zoomX, zoomY, angle, isTurn);
65  }
66  };
67 
70  class SpAnime : public ISprite
71  {
72  private:
73  const Film *film;
74  Anime anime;
75  double aniSpeed;
76 
77  public:
79  SpAnime(const Film *再生対象, double 再生速度 = 1) :
80  film(再生対象),
81  anime(再生対象),
82  aniSpeed(再生速度)
83  {}
84 
86  void Update() override
87  {
88  anime.Update(aniSpeed);
89  }
90 
91  void Draw(const IShape &座標) const override
92  {
93  const Point pos = { 座標.GetX() + gap.x, 座標.GetY() + gap.y };
94  const Point center = { axis.x + film->GetHeight() / 2, axis.y + film->GetHeight() / 2 };
95 
96  anime.DrawRotateAxis(pos, center, zoomX, zoomY, angle, isTurn);
97  }
98  };
99 
102  class SpFont : public ISprite
103  {
104  private:
105  const IFont *font;
106  std::string str;
107 
108  public:
110  SpFont(const IFont *フォント, const char* 描画する文字列) :
111  font(フォント),
112  str(描画する文字列)
113  {}
114 
115  void Draw(const IShape &座標) const override
116  {
117  font->DrawRotate({ 座標.GetX() + gap.x, 座標.GetY() + gap.y }, zoomX, angle, color, str, isTurn);
118 
119  //font->DrawExtend({ 座標.GetX() + gap.x, 座標.GetY() + gap.y }, zoomX, zoomY, color, str );
120  }
121 
123  void SetText(const char* 表示する文字)
124  {
125  str = 表示する文字;
126  }
127  };
128 
131  class SpFrame : public ISprite
132  {
133  private:
134  const IFrame *bmpFrame;
135 
136  public:
138  SpFrame(const IFrame *描画する枠) :
139  bmpFrame(描画する枠)
140  {}
141 
142  void Draw(const IShape &座標) const override
143  {
144  bmpFrame->Draw({ 座標.GetX() + gap.x, 座標.GetY() + gap.y, 座標.GetW() * zoomX, 座標.GetH() * zoomY });
145  }
146  };
147 
150  class SpMap : public ISprite
151  {
152  private:
153  ImagePack &chip;
154  int width;
155  int height;
156  int rotate;
157  std::vector< std::vector<int> > data;
158  public:
159 
161  SpMap(ImagePack& マップチップ, const char* ファイル名, int 幅, int 高さ, int 角度) :
162  width(幅),
163  height(高さ),
164  rotate(角度),
165  chip(マップチップ)
166  {
167  File csvFile(ファイル名, FileMode::Read);
168 
169  data.resize(幅);
170 
171  for (int a = 0; a < 幅; ++a)
172  {
173  data[a].resize(高さ);
174  }
175 
176  auto lineS = csvFile.GetCsvToInt2();
177 
178  for (int a = 0; a < 高さ; ++a)
179  {
180  for (int b = 0; b < 幅; ++b)
181  {
182  data[b][a] = lineS[a][b];
183  }
184  }
185  }
186 
187  void Draw(const IShape &座標) const override
188  {
189  const int chipW = chip.GetWidth();
190  const int chipH = chip.GetHeight();
191 
192  if (Camera::Get())
193  {
194  //隙間が出来るのを防ぐためCameraを一旦切って処理
195  const int baseY = int(Camera::Get()->TransY(座標.GetY()) );
196  const int baseX = int(Camera::Get()->TransX(座標.GetX()));
197 
198  double posXA;
199  double posYA;
200  double posXB;
201  double posYB;
202  double cam_zoom = Camera::Get()->zoom;
203 
204  auto camera = Camera::Get();
205  Camera::Set();
206 
207  for (int a = 0; a < width; ++a)
208  {
209  posXA = baseX + chipW * a * cam_zoom;
210  posXB = baseX + chipW * (a + 1) * cam_zoom;
211 
212  for (int b = 0; b < height; ++b)
213  {
214  posYA = baseY + chipH * b * cam_zoom;
215  posYB = baseY + chipH * (b + 1) * cam_zoom;
216  const int no = data[a][b];
217  if (no == 0) continue;
218  chip[no]->DrawExtend({ posXA, posYA, posXB - posXA, posYB - posYA });
219  }
220  }
221 
222  Camera::Set(camera);
223  }
224  else{
225  for (int a = 0; a < width; ++a)
226  {
227  for (int b = 0; b < height; ++b)
228  {
229  const int no = data[a][b];
230  if (no == 0) continue;
231  chip[no]->Draw({ 座標.GetX() + a*chipW, 座標.GetY() + b*chipH });
232  }
233  }
234  }
235  }
236  };
237 
240  class SpNull : public ISprite
241  {
242  public:
243 
246  {}
247 
248  void Draw(const IShape &座標) const override
249  {}
250  };
251 }
double angle
角度
Definition: ISprite.h:23
SpMap(ImagePack &マップチップ, const char *ファイル名, int 幅, int 高さ, int 角度)
コンストラクタ.
Definition: Sprite.h:161
double y
座標
Definition: Point.h:26
void Draw(const IShape &座標) const override
ISpriteを描画する.
Definition: Sprite.h:91
Fontのインターフェース.
Definition: IFont.h:12
void Draw(const IShape &座標) const override
ISpriteを描画する.
Definition: Sprite.h:115
bool DrawRotateAxis(const Point &座標, const Point &回転軸座標, double 拡大率, double 角度, bool 反転フラグ=false) const override
回転軸、角度、拡大率を指定して描画.
Definition: Image.h:244
Point axis
回転軸のずれ
Definition: ISprite.h:29
virtual double GetW() const =0
幅を取得.
int GetWidth() const
最大幅を取得.
Definition: ImagePack.h:192
virtual double GetH() const =0
高さを取得.
bool Update(double 経過フレーム=1)
カウンタの更新.
Definition: Anime.h:27
void Draw(const IShape &座標) const override
ISpriteを描画する.
Definition: Sprite.h:57
void SetText(const char *表示する文字)
描画する文字列を変更.
Definition: Sprite.h:123
void Draw(const IShape &座標) const override
ISpriteを描画する.
Definition: Sprite.h:23
点を表す図形クラス.
Definition: Point.h:22
SpImageS(const ImagePack *描画ImagePack)
コンストラクタ.
Definition: Sprite.h:41
IFrameスプライト.
Definition: Sprite.h:131
入出力可能なテキストかバイナリファイルを表すクラス.
Definition: File.h:29
衝突判定可能な図形の抽象クラス.
Definition: IShape.h:21
読込のみ
double zoomY
描画倍率
Definition: ISprite.h:22
void Draw(const IShape &座標) const override
ISpriteを描画する.
Definition: Sprite.h:187
SpFrame(const IFrame *描画する枠)
コンストラクタ.
Definition: Sprite.h:138
マップチップスプライト.
Definition: Sprite.h:150
Animeスプライト.
Definition: Sprite.h:70
Color color
描画色
Definition: ISprite.h:27
2Dモデルに貼り付けるスプライトを表す抽象クラス.
Definition: ISprite.h:17
virtual double GetY() const =0
Y座標を取得.
画像データを表すクラス.
Definition: Image.h:17
int GetIndex()
現在のindexを取得.
Definition: Sprite.h:52
int GetHeight() const
高さを取得.
Definition: Image.h:310
ImagePackスプライト.
Definition: Sprite.h:33
ImagePackにコマ毎の再生時間を追加したクラス.
Definition: Film.h:32
static Camera * Get()
現在アクティブなカメラを取得.
Definition: Camera.h:57
void SetIndex(int コマ番号)
表示するindexの切り替え.
Definition: Sprite.h:46
描画しないNullスプライト.
Definition: Sprite.h:240
void Update() override
アニメの更新.
Definition: Sprite.h:86
SpNull()
コンストラクタ.
Definition: Sprite.h:245
Point gap
Shape中心とSpriteとの位置差
Definition: ISprite.h:28
int GetHeight() const
最大高さを取得.
Definition: ImagePack.h:198
Filmに再生状態を付与したクラス.
Definition: Anime.h:11
void Draw(const IShape &座標) const override
ISpriteを描画する.
Definition: Sprite.h:248
double zoom
拡大率、マイナスになると描画が狂う
Definition: Camera.h:45
double x
座標
Definition: Point.h:25
複数のImageをまとめるクラス.
Definition: ImagePack.h:17
Imageスプライト.
Definition: Sprite.h:12
static void Set(Camera *アクティブにするCamera=nullptr)
現在アクティブなカメラを設定.
Definition: Camera.h:63
virtual bool DrawRotate(const Point &座標, double 拡大率, double 角度, const Color &描画色, const VariadicStream &描画する文字列, bool 反転フラグ=false) const =0
文字を回転して描画.
SpFont(const IFont *フォント, const char *描画する文字列)
コンストラクタ.
Definition: Sprite.h:110
virtual double GetX() const =0
X座標を取得.
bool isTurn
反転フラグ
Definition: ISprite.h:26
Fontスプライト.
Definition: Sprite.h:102
std::vector< std::vector< int > > GetCsvToInt2()
カンマ区切りのCSVファイルを二次元配列に整数として一括読込.
Definition: File.h:528
SpImage(const Image *描画Image)
コンストラクタ.
Definition: Sprite.h:19
void Draw(const IShape &座標) const override
ISpriteを描画する.
Definition: Sprite.h:142
double zoomX
描画倍率
Definition: ISprite.h:21
virtual void Draw(const Rect &領域, const Color &描画色=Color::White) const =0
枠を描画.
bool DrawRotateAxis(const Point &座標, const Point &回転軸座標, double 拡大率, double 角度, bool 反転フラグ=false) const override
回転軸、角度、拡大率を指定して描画.
Definition: Anime.h:102
描画用枠のインターフェース.
Definition: IFrame.h:11
SpAnime(const Film *再生対象, double 再生速度=1)
コンストラクタ.
Definition: Sprite.h:79