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