SDXFrameWork  0.13
SDXFrameWork
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
Drawing.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/Color.h>
7 #include <Multimedia/Image.h>
8 #include <Multimedia/Font.h>
9 #include <Utility/VariadicStream.h>
10 #include <Framework/Shape.h>
11 #include <Framework/Camera.h>
12 
13 namespace SDX
14 {
17  class Drawing
18  {
19  private:
20  //生成、削除、コピー等を禁止
21  Drawing() = default;
22  ~Drawing() = default;
23  void operator =(const Drawing& src){}
24  Drawing(const Drawing& src){}
25 
27  static void RGBACulculate( const Color &色 )
28  {
29  if (色.GetAlpha() == 255)
30  {
31  SDL_SetRenderDrawBlendMode(Screen::GetHandle(), (SDL_BlendMode)Screen::GetRenderer()->blendMode);
32  }
33  else
34  {
35  SDL_SetRenderDrawBlendMode(Screen::GetHandle(), (SDL_BlendMode)BlendMode::Alpha);
36  }
37 
38  SDL_SetRenderDrawColor
39  (
41  Screen::GetRenderer()->rgba.GetRed() * 色.GetRed() / 255,
42  Screen::GetRenderer()->rgba.GetGreen() * 色.GetGreen() / 255,
43  Screen::GetRenderer()->rgba.GetBlue() * 色.GetBlue() / 255,
44  Screen::GetRenderer()->rgba.GetAlpha() * 色.GetAlpha() / 255
45  );
46  }
47 
49  static void MakeCircle(SDL_Renderer* renderer,int x1, int y1,int x2, int y2)
50  {
51  //実装参考[http://hp.vector.co.jp/authors/VA028002/sdl/]
52  //パブリックドメインなのでわりとコピペしてます
53  int r;
54  int dx, dy;
55  int xa, ya, xb, yb;
56  int f;
57 
58  SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
59  SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
60  SDL_RenderClear(renderer);
61  SDL_SetRenderDrawColor(renderer,255,255,255,255);
62 
63  dx = x2 - x1 < 0 ? x1 - x2 : x2 - x1;
64  dy = y2 - y1 < 0 ? y1 - y2 : y2 - y1;
65 
66  r = dx / 2;
67  xa = r;
68  ya = 0;
69  f = 3 - 2 * r;
70  x1 += dx / 2;
71  y1 += dy / 2;
72  x2 = x1 + dx % 2;
73  y2 = y1 + dy % 2;
74  while (xa >= ya){
75  xb = (xa * dy) / dx;
76  yb = (ya * dy) / dx;
77 
78  SDL_RenderDrawLine(renderer, xa + x2, -yb + y1,xa + x2, yb + y2); /*A*/
79  SDL_RenderDrawLine(renderer, ya + x2, -xb + y1, ya + x2, xb + y2); /*B*/
80  SDL_RenderDrawLine(renderer, -ya + x1, -xb + y1, -ya + x1, xb + y2); /*C*/
81  SDL_RenderDrawLine(renderer, -xa + x1, -yb + y1, -xa + x1, yb + y2); /*D*/
82 
83  if (f >= 0){
84  xa--;
85  f -= 4 * xa;
86  }
87  ya++;
88  f += 4 * ya + 2;
89  }
90  }
91 
93  static Image& GetCircle(const Color &描画色, const Rect &矩形 = {0,0,0,0}, int 太さ = 0)
94  {
95  static Image circleA;
96  static Image circleB;
97  static Image circleC;
98  const int CIRCLE_SIZE = 512;
99 
100  if (circleA.GetHandle() == nullptr)
101  {
102  circleA.Make(CIRCLE_SIZE, CIRCLE_SIZE);
103  circleB.Make(CIRCLE_SIZE, CIRCLE_SIZE);
104  circleC.Make(CIRCLE_SIZE, CIRCLE_SIZE);
105 
106  auto mode = Screen::GetRenderer()->blendMode;
107  auto col = Screen::GetRenderer()->rgba;
108  int alpha = Screen::GetRenderer()->rgba.GetAlpha();//Alpha値の場合ここの減算率が変化する
109  auto cam = Camera::Get();
110 
111  Camera::Set();
113  auto prev = Screen::GetTarget();
114 
115  Screen::SetTarget(&circleA);
116  MakeCircle(Screen::GetHandle(), 0, 0, CIRCLE_SIZE, CIRCLE_SIZE);
117  Screen::SetTarget(&circleB);
118  SDL_SetRenderDrawColor(Screen::GetHandle(), 0, 0, 0, 0);
119  SDL_RenderClear(Screen::GetHandle());
120  Screen::SetTarget(&circleC);
121  Drawing::Rect({ 0, 0, CIRCLE_SIZE, CIRCLE_SIZE }, Color::White, true);
122  Screen::SetTarget(prev);
123 
124  Screen::SetDrawMode(col,mode);
125  Camera::Set(cam);
126  }
127 
128  if (太さ <= 0)
129  {
130  circleA.SetColor(描画色);
131  return circleA;
132  }
133  else
134  {
135  太さ = std::max(太さ * CIRCLE_SIZE * 2 / int(矩形.GetW() + 矩形.GetH()), 2);//最低2
136 
137  auto prev = Screen::GetTarget();
138  auto mode = Screen::GetRenderer()->blendMode;
139  auto col = Screen::GetRenderer()->rgba;
140  int alpha = Screen::GetRenderer()->rgba.GetAlpha();//Alpha値の場合ここの減算率が変化する
141  auto cam = Camera::Get();
142 
143  Camera::Set();
145 
146  Screen::SetTarget(&circleB);
147  circleA.SetColor(Color::White);
148  circleA.Draw({ 0, 0 });
149  circleA.SetColor(Color::Black);
150  circleA.DrawExtend({ 太さ, 太さ , CIRCLE_SIZE - 太さ*2 , CIRCLE_SIZE - 太さ*2 });
151  Screen::SetTarget(&circleC);
152  circleA.SetColor({255-alpha,255-alpha,255-alpha});
153  circleA.Draw({ 0, 0 });
154  circleA.SetColor(Color::White);
155  circleA.DrawExtend({ 太さ, 太さ , CIRCLE_SIZE - 太さ*2, CIRCLE_SIZE - 太さ*2 });
156  Screen::SetTarget(prev);
157  Camera::Set(cam);
158 
159 
160  if (mode == BlendMode::Mula)
161  {
162  Screen::SetDrawMode({255,255,255,alpha},BlendMode::Mula);
163  circleB.DrawExtend(矩形);
164  }
165  else
166  {
167  if (mode != BlendMode::Add)
168  {
169  Screen::SetDrawMode({ 255, 255, 255, alpha }, BlendMode::Mula);
170  circleC.DrawExtend(矩形);
171  }
172  Screen::SetDrawMode({ 描画色.GetRed(), 描画色.GetGreen(), 描画色.GetBlue(), alpha }, BlendMode::Add);
173  circleB.DrawExtend(矩形);
174  }
175 
176  Screen::SetDrawMode(col, mode);
177 
178  return circleB;
179  }
180  }
181 
183  static void GetTriangle(const Color &描画色, const Point &中心座標 = {0,0}, int 辺の長さ = 0, double 角度 = 0, int 太さ = 0)
184  {
185  static Image imageA;
186  static Image imageB;
187  static Image imageC;
188 
189  const int WSIZE = 173*2;
190  const int HSIZE = 200*2;
191 
192  if (imageA.GetHandle() == nullptr)
193  {
194  imageA.Make(WSIZE, HSIZE);//高さ=√3の近似
195  imageB.Make(WSIZE, HSIZE);
196  imageC.Make(WSIZE, HSIZE);
197 
198  auto prev = Screen::GetTarget();
199  auto mode = Screen::GetRenderer()->blendMode;
200  auto col = Screen::GetRenderer()->rgba;
201  auto cam = Camera::Get();
202 
203  Camera::Set();
205 
206  Screen::SetTarget(&imageA);
207  //横向きの三角形を描画
208  SDL_SetRenderDrawColor(Screen::GetHandle(), 0, 0, 0, 0);
209  SDL_RenderClear(Screen::GetHandle());
210  SDL_SetRenderDrawBlendMode(Screen::GetHandle(), SDL_BLENDMODE_BLEND);
211  SDL_SetRenderDrawColor(Screen::GetHandle(), 255, 255, 255, 255);
212  for (int y = 0; y < HSIZE/2; ++y)
213  {
214  int x = WSIZE * y * 2 / HSIZE;
215  SDL_RenderDrawLine(Screen::GetHandle(), 0, y, x, y);
216  SDL_RenderDrawLine(Screen::GetHandle(), 0, HSIZE-y-1, x, HSIZE-y-1);
217  }
218  Screen::SetTarget(&imageB);
219  SDL_SetRenderDrawColor(Screen::GetHandle(), 0, 0, 0, 0);
220  SDL_RenderClear(Screen::GetHandle());
221  Screen::SetTarget(&imageC);
222  Drawing::Rect({ 0, 0, WSIZE, HSIZE }, Color::White, true);
223  Screen::SetTarget(prev);
224 
225  Screen::SetDrawMode(col, mode);
226  Camera::Set(cam);
227  }
228 
229  const double 拡大率 = double(辺の長さ) / HSIZE;
230 
231  if (太さ <= 0)
232  {
233  imageA.SetColor(描画色);
234  imageA.DrawRotateAxis({ 中心座標.x ,中心座標.y}, { WSIZE/3, HSIZE/2 }, 拡大率, 角度);
235  }
236  else
237  {
238  太さ = std::max(太さ * WSIZE* 2 / (辺の長さ), 2);//最低2
239 
240  auto prev = Screen::GetTarget();
241  int alpha = Screen::GetRenderer()->rgba.GetAlpha();//Alpha値によりここの減算率が変化する
242  auto mode = Screen::GetRenderer()->blendMode;
243  auto col = Screen::GetRenderer()->rgba;
244  auto cam = Camera::Get();
245 
246  Camera::Set();
248 
249  Screen::SetTarget(&imageB);
250  imageA.SetColor(Color::White);
251  imageA.Draw({ 0, 0 });
252  imageA.SetColor(Color::Black);
253  imageA.DrawExtend({ 太さ/2, 太さ , WSIZE - 太さ*3/2, HSIZE - 太さ*2 });
254  Screen::SetTarget(&imageC);
255  imageA.SetColor({ 255 - alpha, 255 - alpha, 255 - alpha });
256  imageA.Draw({ 0, 0 });
257  imageA.SetColor(Color::White);
258  imageA.DrawExtend({ 太さ/2, 太さ , WSIZE - 太さ*3/2, HSIZE - 太さ*2 });
259  Screen::SetTarget(prev);
260  Camera::Set(cam);
261 
262  if (mode == BlendMode::Mula)
263  {
264  Screen::SetDrawMode({ 255, 255, 255, alpha }, BlendMode::Mula);
265  imageB.DrawRotateAxis({ 中心座標.x, 中心座標.y }, { WSIZE / 3, HSIZE / 2 }, 拡大率, 角度);
266  }
267  else
268  {
269  if ( mode != BlendMode::Add)
270  {
271  Screen::SetDrawMode({ 255, 255, 255, alpha }, BlendMode::Mula);
272  imageC.DrawRotateAxis({ 中心座標.x, 中心座標.y }, { WSIZE / 3, HSIZE / 2 }, 拡大率, 角度);
273  }
274 
275  Screen::SetDrawMode({ 描画色.GetRed(), 描画色.GetGreen(), 描画色.GetBlue(), alpha }, BlendMode::Add);
276  imageB.DrawRotateAxis({ 中心座標.x, 中心座標.y }, { WSIZE / 3, HSIZE / 2 }, 拡大率, 角度);
277  }
278 
279  Screen::SetDrawMode(col, mode);
280  }
281  }
282 public:
284  static Font& GetFont()
285  {
286  static Font defaultFont;
287  return defaultFont;
288  }
289 
291  static void SetDefaultFont(const char* フォント名, int 大きさ,int 行間 = 0 , bool 高品質レンダリング = true)
292  {
293  GetFont().Load(フォント名, 大きさ, 行間, 高品質レンダリング);
294  }
295 
297  static void Line(const Point &始点, const Point &終点, const Color &色, int 太さ = 1)
298  {
299  if (太さ <= 1)
300  {
301  int xA,xB,yA,yB;
302  RGBACulculate(色);
303  if (Camera::Get())
304  {
305  xA = (int)Camera::Get()->TransX(始点.x);
306  xB = (int)Camera::Get()->TransX(終点.x);
307  yA = (int)Camera::Get()->TransY(始点.y);
308  yB = (int)Camera::Get()->TransY(終点.y);
309  }
310  else
311  {
312  xA = (int)始点.x;
313  xB = (int)終点.x;
314  yA = (int)始点.y;
315  yB = (int)終点.y;
316  }
317 
318  SDL_RenderDrawLine(Screen::GetHandle(), xA, yA, xB, yB);
319  }
320  else
321  {
322  static Image image;
323  if (image.GetHandle() == nullptr)
324  {
325  auto prev = Screen::GetRenderer()->target;
326  auto mode = Screen::GetRenderer()->blendMode;
327  auto col = Screen::GetRenderer()->rgba;
328  auto cam = Camera::Get();
329 
330  image.Make(256, 256);
331  Camera::Set();
333  Screen::SetTarget(&image);
334  Drawing::Rect({ 0, 0, 256, 256 }, Color::White, true);
335  Screen::SetTarget(prev);
336  Screen::SetDrawMode(col,mode);
337  Camera::Set(cam);
338  }
339 
340  double rateX = std::sqrt((終点.x - 始点.x)*(終点.x - 始点.x) + (終点.y - 始点.y) * (終点.y - 始点.y));//太さ
341  double rateY = 太さ;//長さ
342  double angle = std::atan2( 終点.y - 始点.y , 終点.x - 始点.x);
343 
344  image.SetColor(色);
345  image.DrawRotateAxis({ (始点.x + 終点.x)/2, (始点.y + 終点.y)/2 }, { 128, 128 }, rateX/256, rateY/256, angle);
346  }
347  }
348 
350  static void Rect(const Rect &領域, const Color &色, bool 塗りつぶしフラグ = true)
351  {
352  SDL_Rect buf = { (int)領域.GetLeft(), (int)領域.GetTop(), (int)領域.GetW(), (int)領域.GetH() };
353 
354  if (Camera::Get())
355  {
356  buf = Camera::Get()->TransRect(buf);
357  }
358 
359  RGBACulculate(色);
360  if (塗りつぶしフラグ)
361  {
362  SDL_RenderFillRect(Screen::GetHandle(), &buf);
363  }
364  else
365  {
366  SDL_RenderDrawRect(Screen::GetHandle(), &buf);
367  }
368  }
369 
373  static void Circle(const Circle &円形, const Color& 色, int 太さ = 0)
374  {
375  if (円形.radius <= 0){ return; }
376 
377  if (太さ <= 0)
378  {
379  GetCircle(色).DrawExtend({ 円形.x - 円形.radius, 円形.y - 円形.radius , 円形.radius*2, 円形.radius*2 });
380  }
381  else
382  {
383  GetCircle(色, { 円形.x - 円形.radius, 円形.y - 円形.radius, 円形.radius * 2, 円形.radius * 2 }, 太さ);
384  }
385  }
386 
390  static void Oval(const Point &中心, int 幅, int 高さ, const Color& 色, int 太さ = 0)
391  {
392  if (太さ <= 0)
393  {
394  GetCircle(色).DrawExtend({ 中心.x - 幅 / 2, 中心.y - 高さ / 2, (double)幅, (double)高さ });
395  }
396  else
397  {
398  GetCircle(色, { 中心.x - 幅 / 2, 中心.y - 高さ / 2, 幅, 高さ }, 太さ);
399  }
400  }
401 
403  static void Polygon(std::vector<Point> 頂点, const Color &色 ,int 太さ = 1)
404  {
405  for (unsigned int a = 0; a < 頂点.size() ; ++a)
406  {
407  if (a == 頂点.size() - 1)
408  {
409  //最後の辺
410  Drawing::Line(頂点[a],頂点[0],色,太さ);
411  }
412  else
413  {
414  Drawing::Line(頂点[a], 頂点[a+1], 色, 太さ);
415  }
416 
417  }
418  }
419 
423  static void Triangle(const Point &中心座標,int 辺の長さ , double 角度 , const Color &描画色 , int 太さ = 0)
424  {
425  if (太さ <= 0)
426  {
427  GetTriangle(描画色, 中心座標, 辺の長さ, 角度);
428  }
429  else
430  {
431  GetTriangle(描画色, 中心座標, 辺の長さ, 角度, 太さ);
432  }
433  }
434 
436  static void Pixel(const Point &座標, const Color &色)
437  {
438  RGBACulculate(色);
439 
440  double x = 座標.x;
441  double y = 座標.y;
442 
443  if (Camera::Get())
444  {
445  x = Camera::Get()->TransX(x);
446  y = Camera::Get()->TransY(y);
447  }
448 
449  SDL_RenderDrawPoint(Screen::GetHandle(), (int)x, (int)y);
450  }
451 
454  static void ImageFile(const Point &座標, const char *ファイル名, bool 透過フラグ = true)
455  {
456  Image buf(ファイル名);
457  buf.Draw(座標, false);
458  buf.Release();
459  }
460 
463  static void String(const Point &座標, const Color &色, const VariadicStream &描画する文字列)
464  {
465  GetFont().Draw(座標, 色, 描画する文字列);
466  }
467  };
468 }
bool Draw(const Point &座標, const Color &描画色, const VariadicStream &描画する文字列, bool 反転フラグ=false) const override
文字を描画.
Definition: Font.h:615
SDL_Texture * Make(int 幅, int 高さ)
空のイメージを作成.
Definition: Image.h:120
int GetGreen() const
緑の要素を取得.
Definition: Color.h:36
矩形を表す図形クラス.
Definition: Rect.h:22
static Image * GetTarget()
描画先Imageを取得.
Definition: Screen.h:55
double y
座標
Definition: Point.h:26
static void Pixel(const Point &座標, const Color &色)
指定座標に点を描画.
Definition: Drawing.h:436
bool DrawRotateAxis(const Point &座標, const Point &回転軸座標, double 拡大率, double 角度, bool 反転フラグ=false) const override
回転軸、角度、拡大率を指定して描画.
Definition: Image.h:244
Color rgba
描画輝度とα値
Definition: Renderer.h:53
bool DrawExtend(const Rect &描画領域, bool 反転フラグ=false) const override
指定矩形内に描画.
Definition: Image.h:201
double GetTop() const
上端のY座標を取得.
Definition: Rect.h:124
int GetBlue() const
青の要素を取得.
Definition: Color.h:42
リソースを読み込まずに描画を行う関数群.
Definition: Drawing.h:17
double GetLeft() const
左端のX座標を取得.
Definition: Rect.h:118
static void Circle(const Circle &円形, const Color &色, int 太さ=0)
中心と半径を指定して円を描画.
Definition: Drawing.h:373
SDL_Rect TransRect(const SDL_Rect &変換前矩形) const
領域を画面上のどこに相当するか変換.
Definition: Camera.h:102
double x
座標
Definition: Circle.h:23
static void Line(const Point &始点, const Point &終点, const Color &色, int 太さ=1)
始点と終点を結ぶ直線を描画.
Definition: Drawing.h:297
点を表す図形クラス.
Definition: Point.h:22
BlendMode blendMode
描画モード
Definition: Renderer.h:51
static void String(const Point &座標, const Color &色, const VariadicStream &描画する文字列)
文字を描画.
Definition: Drawing.h:463
TrueTypeFontとBMPFontをまとめて扱うクラス.
Definition: Font.h:25
double TransX(double X座標変換前) const
カメラの位置と拡大率に応じてX座標を変換.
Definition: Camera.h:84
static Renderer * GetRenderer()
アクティブなRendererを取得.
Definition: Screen.h:33
double y
座標
Definition: Circle.h:24
double TransY(double Y座標変換前) const
カメラの位置と拡大率に応じてY座標を変換.
Definition: Camera.h:90
static bool SetTarget(Image *描画対象=nullptr)
描画先を変更.
Definition: Screen.h:48
static void Triangle(const Point &中心座標, int 辺の長さ, double 角度, const Color &描画色, int 太さ=0)
正三角形を描画.
Definition: Drawing.h:423
static const Color White
白 [RGB]255,255,255
Definition: Color.h:81
画像データを表すクラス.
Definition: Image.h:17
色を表すクラス.
Definition: Color.h:11
bool Load(const char *フォント名, int 大きさ, int 行間=0, bool 高品質レンダリングフラグ=true)
フォントを作成する.
Definition: Font.h:183
積算ブレンド
SDL_Texture * GetHandle() const
ハンドルを取得.
Definition: Image.h:175
static const Color Black
黒 [RGB]0,0,0
Definition: Color.h:78
bool Release()
イメージをメモリから開放.
Definition: Image.h:109
static SDL_Renderer * GetHandle()
スクリーンハンドルを取得.
Definition: Screen.h:26
static Camera * Get()
現在アクティブなカメラを取得.
Definition: Camera.h:57
static void SetDrawMode(const Color &輝度+α値=Color::White, BlendMode ブレンドモード=BlendMode::Alpha)
描画輝度と描画モードをまとめて設定
Definition: Screen.h:100
int GetRed() const
赤の要素を取得.
Definition: Color.h:30
可変数引数な文字列を処理するクラス.
Definition: VariadicStream.h:25
加算ブレンド
static void ImageFile(const Point &座標, const char *ファイル名, bool 透過フラグ=true)
画像を一時的にメモリに読み込んで描画.
Definition: Drawing.h:454
αブレンド
double x
座標
Definition: Point.h:25
static void Set(Camera *アクティブにするCamera=nullptr)
現在アクティブなカメラを設定.
Definition: Camera.h:63
static void Polygon(std::vector< Point > 頂点, const Color &色, int 太さ=1)
頂点を指定して多角形を描画.
Definition: Drawing.h:403
void SetColor(const Color &描画色)
描画色を指定.
Definition: Image.h:316
int GetAlpha() const
透明度を取得.
Definition: Color.h:48
static Font & GetFont()
デフォルトのフォントを取得する.
Definition: Drawing.h:284
円を表す図形クラス.
Definition: Circle.h:20
bool Draw(const Point &座標, bool 反転フラグ=false) const override
指定座標に描画.
Definition: Image.h:181
static void SetDefaultFont(const char *フォント名, int 大きさ, int 行間=0, bool 高品質レンダリング=true)
デフォルトのフォントを設定する.
Definition: Drawing.h:291
double GetW() const override
幅を取得.
Definition: Rect.h:107
static void Rect(const Rect &領域, const Color &色, bool 塗りつぶしフラグ=true)
左上の座標と大きさを指定して矩形を描画.
Definition: Drawing.h:350
double GetH() const override
高さを取得.
Definition: Rect.h:112
double radius
半径
Definition: Circle.h:25
static void Oval(const Point &中心, int 幅, int 高さ, const Color &色, int 太さ=0)
中心と外接する四角形の大きさを指定して楕円を描画.
Definition: Drawing.h:390