SDXFrameWork  0.09
SDXFrameWork
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
Font.h
1 #pragma once//☀SDL
2 #include <Multimedia/SDX.h>
3 #include <Multimedia/Screen.h>
4 #include <Multimedia/IFont.h>
5 #include <Multimedia/SystemFont.h>
6 #include <Multimedia/Image.h>
7 #include <Multimedia/Window.h>
8 #include <Framework/Shape.h>
9 
10 namespace SDX
11 {
13 enum class FontType
14 {
15  Normal,
16  Edge,
17  AntiAliase,
18  AntiAliaseEdge
19 };
20 
23 class Font : public IFont
24 {
25 private:
26  FontHandle handle = NULL_HANDLE;
27  int size = 0;
28  int thick = 0;
29  int enterHeight = 0;
30 public:
31  Font(){}
32 
33  Font(const char *フォント名,int 大きさ ,int 太さ = 1 , int 改行高さ = 0 , FontType フォントタイプ = FontType::Normal)
34  {
35  Font::Load( フォント名 , 大きさ , 太さ , 改行高さ , フォントタイプ);
36  }
37 
43  bool Load(const char *フォント名,int 大きさ ,int 太さ = 1 , int 改行高さ = 0, FontType フォントタイプ = FontType::Normal)
44  {
45  Release();
46  this->size = 大きさ;
47  this->enterHeight = 改行高さ + 大きさ;
48  this->thick = 太さ;
49 
50  handle = TTF_OpenFont(フォント名,大きさ);
51  return (handle != nullptr);
52  }
53 
55  bool Release() const
56  {
57  if(handle != nullptr) return false;
58 
59  TTF_CloseFont(handle);
60  return true;
61  }
62 
64  FontHandle GetHandle() const
65  {
66  return handle;
67  }
68 
70  Image MakeImage(Color 文字色, bool 反転フラグ, VariadicStream 描画する文字列) const
71  {
72  Image image;
73  int 幅 = GetDrawStringWidth(描画する文字列);
74  int 高さ = ((int)描画する文字列.StringS.size()-1) * enterHeight + size;
75  int Y座標 = 0;
76 
77  std::vector<SDL_Surface*> surfaces;
78 
79  for (auto it : 描画する文字列.StringS)
80  {
81  SDL_Surface* surface = TTF_RenderUTF8_Blended(handle, it.c_str(), 文字色);
82  幅 = std::max(幅, surface->w);
83  surfaces.push_back( surface );
84  }
85 
86  SDL_Surface* toRend = SDL_CreateRGBSurface(0, 幅, 高さ, 32, 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff);
87  SDL_Renderer* render = SDL_CreateSoftwareRenderer( toRend );
88 
89  for (auto it : surfaces)
90  {
91  SDL_Texture* texture = SDL_CreateTextureFromSurface( render , it);
92 
93  SDL_Rect temp = { 0, Y座標, it->w, it->h };
94  SDL_RenderCopy( render , texture , 0, &temp);
95 
96  Y座標 += this->enterHeight;
97 
98  SDL_FreeSurface(it);
99  SDL_DestroyTexture( texture );
100  }
101  //描画先を戻す
102  image.handle = SDL_CreateTextureFromSurface( Screen::GetHandle() , toRend);
103  image.part.x = 0;
104  image.part.y = 0;
105  image.part.w = 幅;
106  image.part.h = 高さ;
107 
108  SDL_FreeSurface(toRend);
109  SDL_DestroyRenderer(render);
110 
111  return image;
112  }
113 
115  int GetSize() const
116  {
117  return this->size;
118  }
119 
121  int Getthick() const
122  {
123  return this->thick;
124  }
125 
127  int GetDrawStringWidth( VariadicStream 幅を計算する文字列 ) const
128  {
129  int 幅 = 0;
130 
131  return 幅;
132  }
133 
135  bool Draw(const Point &座標, Color 色, VariadicStream 描画する文字列) const override
136  {
137  if( !handle ) return false;
138 
139  SDL_Surface* image;
140  SDL_Texture* moji;
141  SDL_Rect temp;
142  int Y座標 = (int)座標.y;
143 
144  for( auto it : 描画する文字列.StringS)
145  {
146  if(it.size() > 0)
147  {
148  image = TTF_RenderUTF8_Blended(handle, it.c_str() , 色);
149 
150  moji = SDL_CreateTextureFromSurface(Screen::GetHandle(), image);
151  temp = { (int)座標.x, Y座標, image->w, image->h };
152  SDL_RenderCopy(Screen::GetHandle(), moji, 0, &temp);
153 
154  SDL_FreeSurface(image);
155  SDL_DestroyTexture(moji);
156  }
157  Y座標 += this->enterHeight;
158  }
159 
160  return true;
161  }
162 
163  bool DrawShadow(const Point &座標 , Color 表色 , Color 影色 , VariadicStream 描画する文字列 ) const
164  {
165  Draw({ 座標.x + 1, 座標.y + 1 }, 影色, 描画する文字列);
166  return Draw(座標,表色,描画する文字列);
167  }
168 
171  bool DrawRotate(const Point &座標, double 拡大率, double 角度, Color 描画色, bool 反転フラグ, VariadicStream 描画する文字列) const override
172  {
173  Image 文字イメージ = MakeImage(描画色, 反転フラグ, 描画する文字列);
174  文字イメージ.DrawRotate(座標,拡大率,角度,反転フラグ);
175  文字イメージ.Release();
176  return true;
177  }
178 
180  bool DrawExtend(const Point &座標, double X拡大率, double Y拡大率, Color 描画色, VariadicStream 描画する文字列) const override
181  {
182  if( !handle ) return false;
183 
184  SDL_Surface* image;
185  SDL_Texture* moji;
186  SDL_Rect temp;
187  int Y座標 = (int)座標.y;
188 
189  for (auto it:描画する文字列.StringS)
190  {
191  if(it.size() > 0 )
192  {
193  image = TTF_RenderUTF8_Blended(handle, it.c_str() , 描画色);
194 
195  moji = SDL_CreateTextureFromSurface(Screen::GetHandle(), image);
196  temp = { (int)座標.x, Y座標, int(image->w * X拡大率), int(image->h * Y拡大率) };
197 
198  SDL_RenderCopy(Screen::GetHandle(), moji, 0, &temp);
199  SDL_FreeSurface(image);
200  SDL_DestroyTexture(moji);
201  }
202 
203  Y座標 += int(this->enterHeight * Y拡大率);
204  }
205 
206  return true;
207  }
208 
209 };
210 }
次のコマに進む
static RendererHandle GetHandle()
スクリーンハンドルを取得.
Definition: Screen.h:77
Font,BmpFont等のインターフェース.
Definition: IFont.h:9
FontHandle GetHandle() const
フォントのハンドルを取得.
Definition: Font.h:64
bool DrawRotate(const Point &座標, double 拡大率, double 角度, Color 描画色, bool 反転フラグ, VariadicStream 描画する文字列) const override
文字を回転して描画.
Definition: Font.h:171
bool DrawExtend(const Point &座標, double X拡大率, double Y拡大率, Color 描画色, VariadicStream 描画する文字列) const override
拡大率を指定して文字を描画.
Definition: Font.h:180
点を表す図形クラス.
Definition: Shape.h:129
フォントデータを表すクラス.
Definition: Font.h:23
int GetDrawStringWidth(VariadicStream 幅を計算する文字列) const
描画時の幅を取得[未実装].
Definition: Font.h:127
int GetSize() const
大きさを取得.
Definition: Font.h:115
画像データを表すクラス.
Definition: Image.h:38
色を表すクラス.
Definition: Color.h:7
int Getthick() const
太さを取得.
Definition: Font.h:121
bool Draw(const Point &座標, Color 色, VariadicStream 描画する文字列) const override
文字を描画.
Definition: Font.h:135
bool Release() const
フォントをメモリから開放する.
Definition: Font.h:55
bool Release()
イメージをメモリから開放.
Definition: Image.h:113
可変数引数な文字列を処理するクラス.
Definition: VariadicStream.h:8
bool DrawRotate(const Point &座標, double 拡大率, double 角度, bool 反転フラグ=false) const
角度、拡大率を指定して描画.
Definition: Image.h:186
Image MakeImage(Color 文字色, bool 反転フラグ, VariadicStream 描画する文字列) const
フォントから画像を生成
Definition: Font.h:70
FontType
フォントの種類.
Definition: Font.h:13
bool Load(const char *フォント名, int 大きさ, int 太さ=1, int 改行高さ=0, FontType フォントタイプ=FontType::Normal)
メモリ上にフォントを作成する.
Definition: Font.h:43