SDXFrameWork  0.13
SDXFrameWork
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
ImagePack.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 <Multimedia/SDX.h>
6 #include <Multimedia/Image.h>
7 #include <Multimedia/Loading.h>
8 #include <Tiled/GetTag.h>
9 #include <Multimedia/File.h>
10 #include <string>
11 
12 namespace SDX
13 {
17  class ImagePack
18  {
19  protected:
20  std::vector<Image*> imageS;
21  int widthMax = 0;
22  int heightMax = 0;
23  public:
24  ImagePack() = default;
25 
27  ImagePack(const char *ファイル名, int 総コマ数, int コマ割り横, int コマ割り縦)
28  {
29  ImagePack::Load(ファイル名, 総コマ数, コマ割り横, コマ割り縦);
30  }
31 
35  ImagePack(const char *ファイル名, const char *拡張子, int 総コマ数, const char* 書式 = "%03d.")
36  {
37  Load(ファイル名, 拡張子, 総コマ数, 書式);
38  }
39 
47  bool Load(const char *ファイル名, int 総コマ数, int コマ割り横, int コマ割り縦)
48  {
49  if (Loading::isLoading)
50  {
51  Loading::AddLoading([=]{ Load(ファイル名,総コマ数,コマ割り横,コマ割り縦); });
52  return true;
53  }
54 
55  int x = 0, y = 0, count = 0;
56  Image image(ファイル名);
57 
58  const int width = image.GetWidth() / コマ割り横;
59  const int height = image.GetHeight() / コマ割り縦;
60 
61  for (int a = 0; a < コマ割り縦; ++a)
62  {
63  x = 0;
64  for (int b = 0; b < コマ割り横; ++b)
65  {
66  if (count >= 総コマ数) break;
67  this->Add(new Image(image, { x, y, width, height }));
68  x += width;
69  count++;
70  }
71  y += height;
72  }
73 
74  return true;
75  }
76 
80  bool Load(const char *ファイル名, const char *拡張子, int 総コマ数, const char* 書式 = "%03d.")
81  {
82  if (Loading::isLoading)
83  {
84  Loading::AddLoading([=]{ Load(ファイル名, 拡張子 ,総コマ数, 書式); });
85  return true;
86  }
87 
88  for (int a = 0; a < 総コマ数; ++a)
89  {
90  char fileBuf[8];
91  sprintf_s(fileBuf, 8, 書式, a);
92 
93  std::string fileName = ファイル名;
94  fileName += fileBuf;
95  fileName += 拡張子;
96 
97  this->Add(new Image(fileName.c_str()));
98  }
99  return true;
100  }
101 
103  bool LoadTmx(const char *tmxファイル名)
104  {
105  if (Loading::isLoading)
106  {
107  Loading::AddLoading([=]{ LoadTmx(tmxファイル名); });
108  return true;
109  }
110 
111 
112  if (imageS.size() != 0)
113  {
114  return false;
115  }
116 
117  File file(tmxファイル名, FileMode::Read);
118  if (file.GetFileMode() != FileMode::Read)
119  {
120  return false;
121  }
122 
123  auto strS = file.GetLineS();//全文字読み込むのでわりと効率無視してる
124 
125  int tileW;
126  int tileH;
127 
128  //0番目はgidで無視されるので空データを入れる
129  imageS.push_back(new Image());
130 
131  for (auto & str : strS)
132  {
133  if (str.find("<objectgroup") != std::string::npos)
134  {
135  //以降オブジェクト情報なので終了
136  break;
137  }
138  else if (str.find("<tileset") != std::string::npos)
139  {
140  tileW = std::atoi(GetTag(str, "tilewidth=").c_str());
141  tileH = std::atoi(GetTag(str, "tileheight=").c_str());
142  }
143  else if (str.find("<image width") != std::string::npos)
144  {
145  std::string sttt = GetTag(str, "source=");
146 
147  imageS.push_back(new Image(GetTag(str,"source=").c_str()) );
148  }
149  else if (str.find("<image source") != std::string::npos)
150  {
151  int w = std::atoi(GetTag(str, "width=").c_str()) / tileW;
152  int h = std::atoi(GetTag(str, "height=").c_str()) / tileH;
153 
154  Load(GetTag(str, "source=").c_str(), w*h, w, h);
155  }
156  }
157 
158  return true;
159  }
160 
162  void Add(Image *追加イメージ)
163  {
164  imageS.push_back(追加イメージ);
165  this->widthMax = std::max(this->widthMax, 追加イメージ->GetWidth());
166  this->heightMax = std::max(this->heightMax, 追加イメージ->GetHeight());
167  }
169  void Add(const char *ファイル名)
170  {
171  Add(new Image(ファイル名));
172  }
173 
175  virtual void Release()
176  {
177  for (auto it : imageS)
178  {
179  it->Release();
180  }
181 
182  imageS.clear();
183  }
184 
186  int GetSize() const
187  {
188  return (int)imageS.size();
189  }
190 
192  int GetWidth() const
193  {
194  return this->widthMax;
195  }
196 
198  int GetHeight() const
199  {
200  return this->heightMax;
201  }
202 
204  void SetColor(const Color &描画色)
205  {
206  for (auto && it : imageS)
207  {
208  it->SetColor(描画色);
209  }
210  }
211 
215  void AdjustWidth(std::vector<int> 幅)
216  {
217  if (Loading::isLoading)
218  {
219  Loading::AddLoading([=]{ AdjustWidth(幅); });
220  return;
221  }
222 
223  for (unsigned int a = 0; a < 幅.size();++a)
224  {
225  imageS[a]->part.w -= 幅[a];
226  }
227  }
228 
230  Image* operator[](int index)
231  {
232  return imageS[index];
233  }
234 
236  Image* operator[](int index) const
237  {
238  return imageS[index];
239  }
240 
242  auto begin() ->decltype(imageS.begin())
243  {
244  return imageS.begin();
245  }
246 
248  auto end() ->decltype(imageS.end())
249  {
250  return imageS.end();
251  }
252 
254  auto begin() const ->decltype(imageS.begin())
255  {
256  return imageS.begin();
257  }
258 
260  auto end() const ->decltype(imageS.end())
261  {
262  return imageS.end();
263  }
264 
265 
266  };
267 }
int widthMax
最大幅
Definition: ImagePack.h:21
Image * operator[](int index) const
オペレータ.
Definition: ImagePack.h:236
ImagePack(const char *ファイル名, const char *拡張子, int 総コマ数, const char *書式="%03d.")
連番ファイルを一括して読み込む.
Definition: ImagePack.h:35
virtual void Release()
Imageをメモリから開放.
Definition: ImagePack.h:175
int GetWidth() const
最大幅を取得.
Definition: ImagePack.h:192
std::vector< Image * > imageS
保持するImage
Definition: ImagePack.h:20
入出力可能なテキストかバイナリファイルを表すクラス.
Definition: File.h:29
std::vector< std::string > GetLineS()
ファイルを改行区切りで一括して読み込む.
Definition: File.h:390
読込のみ
bool LoadTmx(const char *tmxファイル名)
tmxファイルのタイルセット情報を元に画像を読み込む.
Definition: ImagePack.h:103
auto end() -> decltype(imageS.end())
イテレータ用.
Definition: ImagePack.h:248
bool Load(const char *ファイル名, const char *拡張子, int 総コマ数, const char *書式="%03d.")
連番ファイルを一括して読み込む.
Definition: ImagePack.h:80
画像データを表すクラス.
Definition: Image.h:17
Image * operator[](int index)
オペレータ.
Definition: ImagePack.h:230
色を表すクラス.
Definition: Color.h:11
void Add(const char *ファイル名)
Imageを末尾に追加.
Definition: ImagePack.h:169
FileMode GetFileMode()
ファイルモードを取得.
Definition: File.h:135
bool Load(const char *ファイル名, int 総コマ数, int コマ割り横, int コマ割り縦)
1つの画像を分割して読み込む.
Definition: ImagePack.h:47
static void AddLoading(std::function< void(void)> &&読み込み関数)
非同期読み込み処理に追加.
Definition: Loading.h:96
int GetHeight() const
高さを取得.
Definition: Image.h:310
int GetWidth() const
幅を取得.
Definition: Image.h:304
void AdjustWidth(std::vector< int > 幅)
先頭からimageの幅を差分修正.
Definition: ImagePack.h:215
int GetSize() const
要素数を取得.
Definition: ImagePack.h:186
auto end() const -> decltype(imageS.end())
イテレータ用.
Definition: ImagePack.h:260
auto begin() -> decltype(imageS.begin())
イテレータ用.
Definition: ImagePack.h:242
int GetHeight() const
最大高さを取得.
Definition: ImagePack.h:198
ImagePack(const char *ファイル名, int 総コマ数, int コマ割り横, int コマ割り縦)
1つの画像を分割して読み込む.
Definition: ImagePack.h:27
複数のImageをまとめるクラス.
Definition: ImagePack.h:17
void Add(Image *追加イメージ)
Imageを末尾に追加.
Definition: ImagePack.h:162
int heightMax
最大高さ
Definition: ImagePack.h:22
void SetColor(const Color &描画色)
描画色をまとめて変更.
Definition: ImagePack.h:204
auto begin() const -> decltype(imageS.begin())
イテレータ用.
Definition: ImagePack.h:254