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