SDXFrameWork  0.07
SDXFrameWork
 全て クラス ネームスペース 関数 変数 列挙型 列挙型の値 ページ
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  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 = const_cast<std::map<std::string, Image*>*>( &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(int X座標 , int Y座標 , 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(int X座標, int Y座標, Color 色, VariadicStream 描画する文字列) const
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  }
175  bool ZMask(int X座標, int Y座標, ZMaskType Zマスクタイプ, VariadicStream 描画する文字列 ) const
176  {
177  return false;
178  }
179 
182  bool DrawRotate(int X座標, int Y座標, double 拡大率, double 角度, Color 描画色 , bool 反転フラグ, VariadicStream 描画する文字列) const
183  {
184  return false;
185  }
186 
188  bool ZMaskRotate(int X座標, int Y座標, double 拡大率, double 角度, ZMaskType Zマスクタイプ, bool 反転フラグ, VariadicStream 描画する文字列) const
189  {
190  return false;
191  }
192 
194  bool DrawExtend(int X座標, int Y座標, double X拡大率, double Y拡大率, Color 描画色, VariadicStream 描画する文字列) const
195  {
196  SDL_Surface* image;
197  SDL_Texture* moji;
198  SDL_Rect temp;
199  for (auto it:描画する文字列.StringS)
200  {
201  image = TTF_RenderUTF8_Blended(handle, it.c_str() , 描画色);
202 
203  moji = SDL_CreateTextureFromSurface(Screen::GetHandle(), image);
204  temp = { X座標, Y座標, int(image->w * X拡大率), int(image->h * Y拡大率) };
205 
206  SDL_RenderCopy(Screen::GetHandle(), moji, 0, &temp);
207 
208  Y座標 += int(this->enterHeight * Y拡大率);
209  SDL_FreeSurface(image);
210  SDL_DestroyTexture(moji);
211  }
212  }
214  bool ZMaskExtend(int X座標, int Y座標, double X拡大率, double Y拡大率, ZMaskType Zマスクタイプ, VariadicStream 描画する文字列) const
215  {
216  return false;
217  }
218 };
219 }
ZMaskType
Zマスクの種類.
Definition: Image.h:8
Font,BmpFont等のインターフェース.
Definition: IFont.h:9
bool Release() const
フォントをメモリから開放する.
Definition: HashFont.h:50
bool ZMaskRotate(int X座標, int Y座標, double 拡大率, double 角度, ZMaskType Zマスクタイプ, bool 反転フラグ, VariadicStream 描画する文字列) const
文字を回転してマスク[DXLIB].
Definition: HashFont.h:188
bool Load(const char *フォント名, int 大きさ, int 太さ=1, int 改行高さ=0, FontType フォントタイプ=FontType::Normal)
メモリ上にフォントを作成する.
Definition: HashFont.h:39
bool Draw(int X座標, int Y座標, bool 反転フラグ=false) const
指定座標に描画.
Definition: Image.h:163
bool ZMask(int X座標, int Y座標, ZMaskType Zマスクタイプ, VariadicStream 描画する文字列) const
文字をマスク[DXLIB].
Definition: HashFont.h:175
int GetDrawStringWidth(VariadicStream 幅を計算する文字列) const
描画時の幅を取得[DXLIB].
Definition: HashFont.h:135
bool ZMaskExtend(int X座標, int Y座標, double X拡大率, double Y拡大率, ZMaskType Zマスクタイプ, VariadicStream 描画する文字列) const
拡大率を指定して文字をマスク[DXLIB].
Definition: HashFont.h:214
画像データを表すクラス.
Definition: Image.h:37
int Getthick() const
太さを取得.
Definition: HashFont.h:129
色を表すクラス.
Definition: Color.h:7
bool Draw(int X座標, int Y座標, Color 色, VariadicStream 描画する文字列) const
文字を描画.
Definition: HashFont.h:163
int GetWidth() const
幅を取得.
Definition: Image.h:277
可変数引数な文字列を処理するクラス.
Definition: VariadicStream.h:8
bool DrawRotate(int X座標, int Y座標, double 拡大率, double 角度, Color 描画色, bool 反転フラグ, VariadicStream 描画する文字列) const
文字を回転して描画.
Definition: HashFont.h:182
フォントデータを表すクラス[SDL専用].
Definition: HashFont.h:16
FontType
フォントの種類.
Definition: Font.h:12
FontHandle GetHandle() const
フォントのハンドルを取得.
Definition: HashFont.h:62
static ScreenHandle GetHandle()
スクリーンハンドルを取得.
Definition: Screen.h:49
bool DrawExtend(int X座標, int Y座標, double X拡大率, double Y拡大率, Color 描画色, VariadicStream 描画する文字列) const
拡大率を指定して文字を描画.
Definition: HashFont.h:194
int GetSize() const
大きさを取得.
Definition: HashFont.h:123