SDXFrameWork  0.10
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 
28  FontOld(const char *フォント名, int 大きさ, int 太さ = 1, int 改行高さ = 0)
29  {
30  Load( フォント名 , 大きさ , 太さ , 改行高さ);
31  }
32 
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 幅 = GetDrawStringWidth(描画する文字列);
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 
105  int GetSize() const
106  {
107  return this->size;
108  }
109 
111  int Getthick() const
112  {
113  return this->thick;
114  }
115 
117  int GetDrawStringWidth( VariadicStream 幅を計算する文字列 ) const
118  {
119  int 幅 = 0;
120 
121  return 幅;
122  }
123 
125  bool Draw(const Point &座標, const Color &描画色, const VariadicStream &描画する文字列, bool 反転フラグ = false) const override
126  {
127  if( !handle ) return false;
128 
129  SDL_Surface* image;
130  SDL_Texture* moji;
131  SDL_Rect temp;
132 
133  int y = (int)座標.y;
134 
135  for( auto it : 描画する文字列.StringS)
136  {
137  if(it.size() > 0)
138  {
139  image = TTF_RenderUTF8_Blended(handle, it.c_str() , 描画色);
140 
141  moji = SDL_CreateTextureFromSurface(Screen::GetHandle(), image);
142  temp = { (int)座標.x, y, image->w, image->h };
143  SDL_RenderCopy(Screen::GetHandle(), moji, 0, &temp);
144 
145  SDL_FreeSurface(image);
146  SDL_DestroyTexture(moji);
147  }
148  y += this->enterHeight;
149  }
150 
151  return true;
152  }
153 
156  bool DrawRotate(const Point &座標, double 拡大率, double 角度, const Color &描画色, const VariadicStream &描画する文字列, bool 反転フラグ = false) const override
157  {
158  Image 文字イメージ = MakeImage(描画色, 反転フラグ, 描画する文字列);
159  文字イメージ.DrawRotate(座標,拡大率,角度,反転フラグ);
160  文字イメージ.Release();
161  return true;
162  }
163 
165  bool DrawExtend(const Point &座標, double X軸拡大率, double Y軸拡大率, const Color &描画色, const VariadicStream &描画する文字列, bool 反転フラグ = false) const override
166  {
167  if( !handle ) return false;
168 
169  SDL_Surface* image;
170  SDL_Texture* moji;
171  SDL_Rect temp;
172 
173  int y = (int)座標.y;
174 
175  for (auto it:描画する文字列.StringS)
176  {
177  if(it.size() > 0 )
178  {
179  image = TTF_RenderUTF8_Blended(handle, it.c_str() , 描画色);
180 
181  moji = SDL_CreateTextureFromSurface(Screen::GetHandle(), image);
182  temp = { (int)座標.x, y, int(image->w * X軸拡大率), int(image->h * Y軸拡大率) };
183 
184  SDL_RenderCopy(Screen::GetHandle(), moji, 0, &temp);
185  SDL_FreeSurface(image);
186  SDL_DestroyTexture(moji);
187  }
188 
189  y += int(this->enterHeight * Y軸拡大率);
190  }
191 
192  return true;
193  }
194 
195 };
196 }
double y
座標
Definition: Point.h:26
Fontのインターフェース.
Definition: IFont.h:12
Image MakeImage(Color 文字色, bool 反転フラグ, VariadicStream 描画する文字列) const
フォントから画像を生成
Definition: Font_Old.h:65
bool Load(const char *フォント名, int 大きさ, int 太さ=1, int 改行高さ=0)
メモリ上にフォントを作成する.
Definition: Font_Old.h:38
bool DrawRotate(const Point &座標, double 拡大率, double 角度, const Color &描画色, const VariadicStream &描画する文字列, bool 反転フラグ=false) const override
文字を回転して描画.
Definition: Font_Old.h:156
TTF_Font * GetHandle() const
フォントのハンドルを取得.
Definition: Font_Old.h:59
点を表す図形クラス.
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:111
int GetDrawStringWidth(VariadicStream 幅を計算する文字列) const
描画時の幅を取得[DXLIB].
Definition: Font_Old.h:117
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:125
可変数引数な文字列を処理するクラス.
Definition: VariadicStream.h:25
double x
座標
Definition: Point.h:25
int GetSize() const
大きさを取得.
Definition: Font_Old.h:105
bool Release() const
フォントをメモリから開放する.
Definition: Font_Old.h:50
bool DrawExtend(const Point &座標, double X軸拡大率, double Y軸拡大率, const Color &描画色, const VariadicStream &描画する文字列, bool 反転フラグ=false) const override
拡大率を指定して文字を描画.
Definition: Font_Old.h:165