SDXFrameWork  0.09
SDXFrameWork
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
HashFont.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 
9 #include <iomanip>
10 
11 namespace SDX
12 {
16 class HashFont : public IFont
17 {
18 private:
19  FontHandle handle = NULL_HANDLE;
20  int size = 0;
21  int thick = 0;
22  int enterHeight = 0;
23  mutable std::map<std::string, Image*> hash;
24 
25 public:
26 
27  HashFont(){}
28 
29  HashFont(const char *フォント名, int 大きさ, int 太さ = 1, int 改行高さ = 0, FontType フォントタイプ = FontType::Normal)
30  {
31  Load( フォント名 , 大きさ , 太さ , 改行高さ , フォントタイプ);
32  }
33 
39  bool Load(const char *フォント名,int 大きさ ,int 太さ = 1 , int 改行高さ = 0, FontType フォントタイプ = FontType::Normal)
40  {
41  Release();
42  this->size = 大きさ;
43  this->enterHeight = 改行高さ + 大きさ;
44  this->thick = 太さ;
45  handle = TTF_OpenFont(フォント名,大きさ);
46  return true;
47  }
48 
50  bool Release() const
51  {
52  if(handle != NULL_HANDLE) return false;
53  TTF_CloseFont(handle);
54  for(auto && it: hash)
55  {
56  it.second->Release();
57  }
58  return true;
59  }
60 
62  FontHandle GetHandle() const
63  {
64  return handle;
65  }
66 
67  Image MakeImage(Color 文字色, bool 反転フラグ, VariadicStream 描画する文字列) const
68  {
69  std::vector<SDL_Surface*> surfaces;
70  SDL_Surface* surface;
71 
72  for (auto it : 描画する文字列.StringS)
73  {
74  surface = TTF_RenderUTF8_Blended(handle, it.c_str(), 文字色);
75  幅 = std::max(幅, surface->w);
76  surfaces.push_back( surface );
77  }
78 
79  SDL_Surface* toRend = SDL_CreateRGBSurface(0, 幅, 高さ, 32, 0, 0, 0, 0);
80  SDL_Renderer* render = SDL_CreateSoftwareRenderer( toRend );
81 
82  for (auto it : surfaces)
83  {
84  SDL_Texture* texture = SDL_CreateTextureFromSurface(render, it);
85 
86  SDL_Rect temp = { 0, Y座標, it->w, it->h };
87  SDL_RenderCopy( render , texture , 0, &temp);
88 
89  Y座標 += this->enterHeight;
90 
91  SDL_FreeSurface(it);
92  SDL_DestroyTexture( texture );
93  }
94  //描画先を戻す
95  image.handle = SDL_CreateTextureFromSurface( Screen::GetHandle() , toRend);
96  image.part.w = 幅;
97  image.part.h = 高さ;
98 
99  SDL_FreeSurface(toRend);
100  SDL_DestroyRenderer(render);
101 
102  return image;
103  }
104 
105  Image* GetHash(const char* 文字) const
106  {
107  auto it = hash.find(文字);
108 
109  if (it == hash.end())
110  {
111  SDL_Surface* surface = TTF_RenderUTF8_Blended(handle, 文字, { 255, 255, 255 });
112  SDL_Texture* moji = SDL_CreateTextureFromSurface(Screen::GetHandle(), surface);
113  Image* image = new Image(moji, surface->w, surface->h);
114  std::map<std::string, Image*> *map = &hash;
115  map->operator[](文字) = image;
116  SDL_FreeSurface(surface);
117  return image;
118  }
119  return it->second;
120  }
121 
123  int GetSize() const
124  {
125  return this->size;
126  }
127 
129  int Getthick() const
130  {
131  return this->thick;
132  }
133 
135  int GetDrawStringWidth( VariadicStream 幅を計算する文字列 ) const
136  {
137  for (auto it : 幅を計算する文字列.StringS)
138  {
139  幅 = std::max( 幅 , DxLib::GetDrawStringWidthToHandle(it.c_str(), strlen(it.c_str()), this->handle) );
140  }
141  return 幅;
142  }
143 
144  void DrawUTFString(const Point &座標 , const std::string &文字列) const
145  {
146  unsigned char lead;
147  int charSize = 0;
148  for (auto it = 文字列.begin(); it != 文字列.end(); it += charSize)
149  {
150  lead = *it;
151  if(lead < 0x80) charSize = 1;
152  else if (lead < 0xE0) charSize = 2;
153  else if (lead < 0xF0) charSize = 3;
154  else charSize = 4;
155 
156  Image* str = GetHash( 文字列.substr(std::distance(文字列.begin() , it) , charSize).c_str() );
157  str->Draw(X座標, Y座標);
158  X座標 += str->GetWidth();
159  }
160  }
161 
163  bool Draw(const Point &座標, Color 色, VariadicStream 描画する文字列) const override
164  {
165  if( !handle ) return false;
166  for (auto it : 描画する文字列.StringS)
167  {
168  DrawUTFString(X座標, Y座標, it);
169  Y座標 += this->enterHeight;
170  }
171 
172  return true;
173  }
174 
177  bool DrawRotate(const Point &座標, double 拡大率, double 角度, Color 描画色 , bool 反転フラグ, VariadicStream 描画する文字列) const override
178  {
179  return false;
180  }
181 
183  bool DrawExtend(const Point &座標, double X拡大率, double Y拡大率, Color 描画色, VariadicStream 描画する文字列) const override
184  {
185  SDL_Surface* image;
186  SDL_Texture* moji;
187  SDL_Rect temp;
188  for (auto it:描画する文字列.StringS)
189  {
190  image = TTF_RenderUTF8_Blended(handle, it.c_str() , 描画色);
191 
192  moji = SDL_CreateTextureFromSurface(Screen::GetHandle(), image);
193  temp = { X座標, Y座標, int(image->w * X拡大率), int(image->h * Y拡大率) };
194 
195  SDL_RenderCopy(Screen::GetHandle(), moji, 0, &temp);
196 
197  Y座標 += int(this->enterHeight * Y拡大率);
198  SDL_FreeSurface(image);
199  SDL_DestroyTexture(moji);
200  }
201  }
202 };
203 }
bool Draw(const Point &座標, bool 反転フラグ=false) const
指定座標に描画.
Definition: Image.h:164
static RendererHandle GetHandle()
スクリーンハンドルを取得.
Definition: Screen.h:77
Font,BmpFont等のインターフェース.
Definition: IFont.h:9
bool Release() const
フォントをメモリから開放する.
Definition: HashFont.h:50
bool Load(const char *フォント名, int 大きさ, int 太さ=1, int 改行高さ=0, FontType フォントタイプ=FontType::Normal)
メモリ上にフォントを作成する.
Definition: HashFont.h:39
bool Draw(const Point &座標, Color 色, VariadicStream 描画する文字列) const override
文字を描画.
Definition: HashFont.h:163
点を表す図形クラス.
Definition: Shape.h:129
int GetDrawStringWidth(VariadicStream 幅を計算する文字列) const
描画時の幅を取得.
Definition: HashFont.h:135
画像データを表すクラス.
Definition: Image.h:38
int Getthick() const
太さを取得.
Definition: HashFont.h:129
色を表すクラス.
Definition: Color.h:7
int GetWidth() const
幅を取得.
Definition: Image.h:242
可変数引数な文字列を処理するクラス.
Definition: VariadicStream.h:8
フォントデータを表すクラス[未実装].
Definition: HashFont.h:16
bool DrawExtend(const Point &座標, double X拡大率, double Y拡大率, Color 描画色, VariadicStream 描画する文字列) const override
拡大率を指定して文字を描画.
Definition: HashFont.h:183
bool DrawRotate(const Point &座標, double 拡大率, double 角度, Color 描画色, bool 反転フラグ, VariadicStream 描画する文字列) const override
文字を回転して描画.
Definition: HashFont.h:177
FontType
フォントの種類.
Definition: Font.h:13
FontHandle GetHandle() const
フォントのハンドルを取得.
Definition: HashFont.h:62
int GetSize() const
大きさを取得.
Definition: HashFont.h:123