SDXFrameWork  0.04
SDXFrameWork
 全て クラス ネームスペース 関数 変数 ページ
Font.h
1 #pragma once
2 #include <Multimedia/SDX.h>
3 #include <Multimedia/Screen.h>
4 #include <Multimedia/IFont.h>
5 
6 namespace SDX
7 {
9 enum class FontType
10 {
11 #ifdef DXLIB
12  Normal = DX_FONTTYPE_NORMAL,
13  Edge = DX_FONTTYPE_EDGE,
14  AntiAliase = DX_FONTTYPE_ANTIALIASING,
15  AntiAliaseEdge = DX_FONTTYPE_ANTIALIASING_EDGE,
16 #elif defined(SDL)
17  Normal,
18  Edge,
19  AntiAliase,
20  AntiAliaseEdge
21 #endif
22 };
23 
25 class Font : public IFont
27 {
28 private:
29  FontHandle handle = NULL_HANDLE;
30  int size = 0;
31  int thick = 0;
32  int enterHeight = 0;
33 public:
34 
35  Font(){}
36 
37  Font(const char *フォント名,int 大きさ ,int 太さ , int 改行高さ, FontType フォントタイプ = FontType::Normal)
38  {
39  Font::Load( フォント名 , 大きさ , 太さ , 改行高さ , フォントタイプ);
40  }
41 
43  bool Load(const char *フォント名,int 大きさ ,int 太さ , int 改行高さ, FontType フォントタイプ = FontType::Normal)
46  {
47  Release();
48  this->size = 大きさ;
49  this->enterHeight = 改行高さ + 大きさ;
50  this->thick = 太さ;
51  #ifdef DXLIB
52  this->handle = DxLib::CreateFontToHandle(フォント名,大きさ,太さ,(int)フォントタイプ);
53  return ( handle != -1 );
54  #elif defined(SDL)
55  handle = TTF_OpenFont(フォント名,大きさ);
56  return true;
57  #endif
58  }
59 
61  bool Release() const
62  {
63  if(handle != NULL_HANDLE) return false;
64  #ifdef DXLIB
65  return !DxLib::DeleteFontToHandle(this->handle);
66  #elif defined(SDL)
67  TTF_CloseFont(handle);
68  return true;
69  #endif
70  }
71 
73  FontHandle GetHandle() const
74  {
75  return handle;
76  }
77 
79  int GetSize() const
80  {
81  return this->size;
82  }
83 
85  int Getthick() const
86  {
87  return this->thick;
88  }
89 
91  int GetDrawStringWidth(const char *文字列 , ... ) const
92  {
93  #ifdef DXLIB
94  char bufstr[1024];
95 
96  va_list args;
97  va_start(args, 文字列);
98  vsprintf_s(bufstr, 1024, 文字列, args);
99  va_end(args);
100 
101  return DxLib::GetDrawStringWidthToHandle(bufstr,strlen(bufstr),this->handle);
102  #elif defined(SDL)
103  return 0;
104  #endif
105  }
106 
108  bool Draw(int X座標 , int Y座標 , Color 色 ,const char *描画文字列 , ...) const
109  {
110  //改行コード有効
111  char bufstr[1024];
112  va_list args;
113  va_start(args, 描画文字列);
114  vsprintf_s(bufstr, 1024, 描画文字列, args);
115  va_end(args);
116 
117  char *pstr,*stro;
118  stro = strtok_s(bufstr,"\n",&pstr);
119 
120  #ifdef DXLIB
121  while(stro != NULL)
122  {
123  DxLib::DrawStringToHandle(X座標, Y座標, stro, 色, handle);
124  stro = strtok_s(NULL,"\n",&pstr);
125  Y座標 += this->enterHeight;
126  }
127  #elif defined(SDL)
128  SDL_Surface* image;
129  SDL_Texture* moji;
130  SDL_Rect temp;
131 
132  while (stro != NULL)
133  {
134  size_t ret;
135  wchar_t wc[100];//とりあえず50文字まで
136  mbstowcs_s(&ret,wc,100, stro,_TRUNCATE);
137  Uint16 text[100];
138  for (unsigned int i = 0; i < strlen(stro); ++i)
139  {
140  text[i] = wc[i];
141  }
142 
143  image = TTF_RenderUNICODE_Blended(handle, text, 色);
144  moji = SDL_CreateTextureFromSurface(Screen::GetHandle(), image);
145  temp = { X座標, Y座標, image->w, image->h };
146  SDL_RenderCopy(Screen::GetHandle(), moji, 0, &temp);
147 
148  stro = strtok_s(NULL, "\n", &pstr);
149  Y座標 += this->enterHeight;
150  SDL_FreeSurface(image);
151  SDL_DestroyTexture(moji);
152  }
153  #endif
154 
155  return true;
156  }
157 
158  bool ZMask(int X座標 , int Y座標 , ZMaskType Zマスクタイプ , const char *str , ...) const
159  {
160  #ifdef DXLIB
161  //改行コード有効
162  char bufstr[1024];
163  va_list args;
164  va_start(args,str);
165  vsprintf_s( bufstr , 1024 ,str, args );
166  va_end(args);
167 
168  char *pstr,*stro;
169  strcpy_s(bufstr,1024,str);
170 
171  stro = strtok_s(bufstr,"\n",&pstr);
172 
173  while(stro != NULL)
174  {
175  DxLib::DrawStringToHandleToZBuffer(X座標,Y座標,stro,handle, (int)Zマスクタイプ);
176  stro = strtok_s(NULL,"\n",&pstr);
177  Y座標 += this->enterHeight;
178  }
179  return true;
180  #elif defined(SDL)
181  return false;
182  #endif
183  }
184 
185 
187  bool DrawExtend(int X座標, int Y座標, double X拡大率, double Y拡大率 , Color 描画色, const char *描画文字列, ...) const
188  {
189  char bufstr[1024];
190  va_list args;
191  va_start(args,描画文字列);
192  vsprintf_s(bufstr, 1024, 描画文字列, args);
193  va_end(args);
194 
195  char *pstr,*stro;
196  strcpy_s(bufstr, 1024, 描画文字列);
197 
198  stro = strtok_s(bufstr,"\n",&pstr);
199  #ifdef DXLIB
200  while(stro != NULL)
201  {
202  DxLib::DrawExtendStringToHandle(X座標,Y座標,X拡大率 , Y拡大率 , stro , 描画色, handle );
203  stro = strtok_s(NULL,"\n",&pstr);
204  Y座標 += int( this->enterHeight * Y拡大率 );
205  }
206  #elif defined(SDL)
207  SDL_Surface* image;
208  SDL_Texture* moji;
209  SDL_Rect temp;
210 
211  while (stro != NULL)
212  {
213  size_t ret;
214  wchar_t wc[100];//とりあえず50文字まで
215  mbstowcs_s(&ret, wc, 100, stro, _TRUNCATE);
216  Uint16 text[100];
217  for (unsigned int i = 0; i < strlen(stro); ++i)
218  {
219  text[i] = wc[i];
220  }
221 
222  image = TTF_RenderUNICODE_Blended(handle, text, 描画色);
223  moji = SDL_CreateTextureFromSurface(Screen::GetHandle(), image);
224  temp = { X座標, Y座標, int(image->w * X拡大率), int(image->h * Y拡大率) };
225 
226  SDL_RenderCopy(Screen::GetHandle(), moji, 0, &temp);
227 
228  stro = strtok_s(NULL, "\n", &pstr);
229  Y座標 += int(this->enterHeight * Y拡大率);
230  SDL_FreeSurface(image);
231  SDL_DestroyTexture(moji);
232  }
233  #endif
234  return true;
235  }
236  bool ZMaskExtend(int X座標, int Y座標, double X拡大率, double Y拡大率, ZMaskType Zマスクタイプ, const char *str, ...) const
237  {
238  #ifdef DXLIB
239  char bufstr[1024];
240  va_list args;
241  va_start(args,str);
242  vsprintf_s( bufstr , 1024 ,str, args );
243  va_end(args);
244 
245  char *pstr,*stro;
246  strcpy_s(bufstr,1024,str);
247 
248  stro = strtok_s(bufstr,"\n",&pstr);
249 
250  while(stro != NULL)
251  {
252  DxLib::DrawExtendStringToHandleToZBuffer(X座標, Y座標, X拡大率, Y拡大率, stro, handle, (int)Zマスクタイプ);
253  stro = strtok_s(NULL,"\n",&pstr);
254  Y座標 += this->enterHeight;
255  }
256  return true;
257  #elif defined(SDL)
258  return false;
259  #endif
260  }
261 };
262 }
FontHandle GetHandle() const
フォントのハンドルを取得.
Definition: Font.h:73
bool DrawExtend(int X座標, int Y座標, double X拡大率, double Y拡大率, Color 描画色, const char *描画文字列,...) const
拡大率を指定して文字を描画.
Definition: Font.h:187
bool Load(const char *フォント名, int 大きさ, int 太さ, int 改行高さ, FontType フォントタイプ=FontType::Normal)
メモリ上にフォントを作成する.
Definition: Font.h:45
int GetSize() const
大きさを取得.
Definition: Font.h:79
色を表すクラス.
Definition: Color.h:7
int Getthick() const
太さを取得.
Definition: Font.h:85
int GetDrawStringWidth(const char *文字列,...) const
描画時の幅を取得.
Definition: Font.h:91
bool Release() const
フォントをメモリから開放する.
Definition: Font.h:61
bool Draw(int X座標, int Y座標, Color 色, const char *描画文字列,...) const
書式付きで文字を描画.
Definition: Font.h:108