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