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 <Framework/ImagePack.h>
11 #include <Multimedia/Window.h>
26 TTF_Font* handle =
nullptr;
30 mutable std::map<int, Image*> hash;
32 int style = TTF_STYLE_NORMAL;
34 static bool GetUTFSize(
unsigned char 一文字目,
int &文字長さ )
44 if (一文字目 < 0x80){ 文字長さ = 1; }
45 else if (一文字目 < 0xE0){ 文字長さ = 2; }
46 else if (一文字目 < 0xF0){ 文字長さ = 3; }
52 void DrawUTFString(
const Point &座標,
const std::string &文字列 ,
const Color &描画色)
const
57 for (
auto it = 文字列.begin(); it != 文字列.end(); it += charSize)
59 if (!GetUTFSize(*it, charSize)){
continue; }
60 if (handle ==
nullptr && *it ==
' ')
66 Image* str = GetHash(文字列.substr(std::distance(文字列.begin(), it), charSize).c_str() , charSize);
67 if (str ==
nullptr){
continue; }
75 void DrawUTFString(
const Point &座標,
double X拡大率,
double Y拡大率,
const std::string &文字列,
const Color &描画色)
const
80 for (
auto it = 文字列.begin(); it != 文字列.end(); it += charSize)
82 if (!GetUTFSize(*it, charSize)){
continue; }
83 if (handle ==
nullptr && *it ==
' ')
89 Image* str = GetHash(文字列.substr(std::distance(文字列.begin(), it), charSize).c_str(),charSize);
90 if (str ==
nullptr){
continue; }
98 void DrawRotateUTFString(
const Point &座標,
int X補正,
int Y補正,
double 拡大率,
double 角度,
const std::string &文字列,
const Color &描画色)
const
103 for (
auto it = 文字列.begin(); it != 文字列.end(); it += charSize)
105 if (!GetUTFSize(*it, charSize)){
continue; }
106 if (handle ==
nullptr && *it ==
' ')
112 Image* str = GetHash(文字列.substr(std::distance(文字列.begin(), it), charSize).c_str(),charSize);
113 if (str ==
nullptr){
continue; }
115 double x = 位置.
x + std::cos(角度) * X補正 + std::cos(角度 +
PAI / 2) * Y補正;
116 double y = 位置.
y + std::sin(角度) * X補正 + std::sin(角度 +
PAI / 2) * Y補正;
125 Image* GetHash(
const char* 文字 ,
int 文字長さ)
const
128 if (文字長さ >= 2){ ID += 文字[1]*256; }
129 if (文字長さ >= 3){ ID += 文字[2]*256*256; }
130 if (文字長さ >= 4){ ID += 文字[3] * 256 * 256 * 256; }
132 auto it = hash.find(ID);
134 if (it == hash.end())
136 if (handle ==
nullptr){
return nullptr; }
138 SDL_Surface* surface;
142 surface = TTF_RenderUTF8_Blended(handle, 文字, { 255, 255, 255 });
146 surface = TTF_RenderUTF8_Solid(handle, 文字, { 255, 255, 255 });
150 Image* image =
new Image(moji, surface->w, surface->h);
152 SDL_FreeSurface(surface);
159 void SetHash(
const char* 文字,
int 文字長さ,
Image *対応Image)
162 if (文字長さ >= 2){ ID += 文字[1] * 256; }
163 if (文字長さ >= 3){ ID += 文字[2] * 256 * 256; }
164 if (文字長さ >= 4){ ID += 文字[3] * 256 * 256 * 256; }
174 Font(
const char *フォント名,
int 大きさ,
int 行間 = 0 ,
bool 高品質レンダリングフラグ =
true )
176 Load(フォント名, 大きさ, 行間, 高品質レンダリングフラグ);
182 bool Load(
const char *フォント名,
int 大きさ,
int 行間 = 0,
bool 高品質レンダリングフラグ =
true )
184 if (Loading::isLoading)
191 if (handle !=
nullptr){
return false; }
194 this->enterHeight = 行間 + 大きさ;
195 isBlendRender = 高品質レンダリングフラグ;
196 handle = TTF_OpenFont(フォント名, 大きさ);
198 return (handle !=
nullptr);
205 if (handle !=
nullptr){
return false; }
206 TTF_CloseFont(handle);
207 for (
auto && it : hash)
209 it.second->Release();
223 if (handle ==
nullptr){
return Image(); }
226 int 高さ = ((int)描画する文字列.
StringS.size() - 1) * enterHeight + size;
229 std::vector<SDL_Surface*> surfaces;
230 SDL_Surface* surface;
232 for (
auto it : 描画する文字列.
StringS)
236 surface = TTF_RenderUTF8_Blended(handle, it.c_str(), 文字色);
240 surface = TTF_RenderUTF8_Solid(handle, it.c_str(), 文字色);
243 幅 = std::max(幅, surface->w);
244 surfaces.push_back(surface);
247 SDL_Surface* toRend = SDL_CreateRGBSurface(0, 幅, 高さ, 32, 0, 0, 0, 0);
248 SDL_Renderer* render = SDL_CreateSoftwareRenderer(toRend);
250 for (
auto it : surfaces)
252 SDL_Texture* texture = SDL_CreateTextureFromSurface(render, it);
254 SDL_Rect temp = { 0, Y座標, it->w, it->h };
255 SDL_RenderCopy(render, texture, 0, &temp);
257 Y座標 += this->enterHeight;
260 SDL_DestroyTexture(texture);
265 SDL_FreeSurface(toRend);
266 SDL_DestroyRenderer(render);
280 if (handle ==
nullptr){
return 0; }
284 for (
auto 文字列 : 幅を計算する文字列.
StringS)
290 for (
auto it = 文字列.begin(); it != 文字列.end(); it += charSize)
293 if (lead < 0x20){
continue; }
294 else if (lead < 0x80){ charSize = 1; }
295 else if (lead < 0xE0){ charSize = 2; }
296 else if (lead < 0xF0){ charSize = 3; }
297 else { charSize = 4; }
299 幅 += GetHash(文字列.substr(std::distance(文字列.begin(), it), charSize).c_str(),charSize)->GetWidth();
302 最大幅 = std::max(幅, 最大幅);
313 for (
auto it : 描画する文字列.
StringS)
315 DrawUTFString(位置, it, 描画色);
316 位置.
y += this->enterHeight;
325 Draw({ 座標.
x + 1, 座標.
y + 1 }, 影色, 描画する文字列);
326 return Draw(座標, 表色, 描画する文字列);
333 int 行数 = 描画する文字列.
StringS.size();
336 int Y補正 = int(-enterHeight * 拡大率 * 0.5*行数);
338 for (
auto it : 描画する文字列.
StringS)
340 DrawRotateUTFString(座標, X補正, Y補正, 拡大率, 角度, it, 描画色);
341 Y補正 += int(enterHeight * 拡大率);
353 for (
auto it : 描画する文字列.
StringS)
355 DrawUTFString(位置, X拡大率, Y拡大率, it, 描画色);
356 位置.
y += this->enterHeight * Y拡大率;
366 if (Loading::isLoading)
373 auto it = 文字.begin();
375 if (!GetUTFSize(*it, charSize)){
return; }
376 SetHash(文字.substr(std::distance(文字.begin(), it), charSize).c_str(), charSize, 対応画像);
384 if (Loading::isLoading)
392 auto it = 文字列.begin();
393 if (!GetUTFSize(*it, charSize)){
return; }
394 std::string str = 文字列.substr(0, charSize);
396 for (
int a = 0; a < 登録数;++a)
398 if (!GetUTFSize(*it, charSize)){
continue; }
399 SetHash(str.c_str(),charSize,対応画像[0][a]);
400 if (str[charSize - 1] == 0xff)
403 str[charSize - 1] = 0;
bool Draw(const Point &座標, const Color &描画色, const VariadicStream &描画する文字列, bool 反転フラグ=false) const override
文字を描画.
Definition: Font.h:309
const double PAI
円周率
Definition: SDX.h:26
double y
座標
Definition: Point.h:26
Fontのインターフェース.
Definition: IFont.h:12
bool DrawExtend(const Rect &描画領域, bool 反転フラグ=false) const override
指定矩形内に描画.
Definition: Image.h:201
void SetImageS(const std::string &文字列, ImagePack *対応画像, int 登録数)
指定した文字から連続してに対応するImageをまとめて設定.
Definition: Font.h:382
点を表す図形クラス.
Definition: Point.h:22
TrueTypeFontとBMPFontをまとめて扱うクラス.
Definition: Font.h:23
std::vector< std::string > StringS
一行ずつの文字列.
Definition: VariadicStream.h:53
bool DrawRotate(const Point &座標, double 拡大率, double 角度, bool 反転フラグ=false) const override
角度、拡大率を指定して描画.
Definition: Image.h:229
bool DrawRotate(const Point &座標, double 拡大率, double 角度, const Color &描画色, const VariadicStream &描画する文字列, bool 反転フラグ=false) const override
文字を回転して描画.
Definition: Font.h:331
int GetSize() const
大きさを取得.
Definition: Font.h:272
画像データを表すクラス.
Definition: Image.h:17
色を表すクラス.
Definition: Color.h:11
bool Load(const char *フォント名, int 大きさ, int 行間=0, bool 高品質レンダリングフラグ=true)
フォントを作成する.
Definition: Font.h:182
static void AddLoading(std::function< void(void)> &&読み込み関数)
非同期読み込み処理に追加.
Definition: Loading.h:96
int GetHeight() const
高さを取得.
Definition: Image.h:310
int GetWidth() const
幅を取得.
Definition: Image.h:304
bool Release() const
フォントハンドルをメモリから開放する.
Definition: Font.h:202
TTF_Font * GetHandle() const
フォントのハンドルを取得.
Definition: Font.h:215
static SDL_Renderer * GetHandle()
スクリーンハンドルを取得.
Definition: Screen.h:26
Image MakeImage(Color 文字色, bool 反転フラグ, const VariadicStream &描画する文字列) const
FontからImageを生成.
Definition: Font.h:221
bool DrawShadow(const Point &座標, Color 表色, Color 影色, const VariadicStream &描画する文字列) const
文字を影付きで描画.
Definition: Font.h:323
可変数引数な文字列を処理するクラス.
Definition: VariadicStream.h:25
bool DrawExtend(const Point &座標, double X拡大率, double Y拡大率, const Color &描画色, const VariadicStream &描画する文字列, bool 反転フラグ=false) const override
拡大率を指定して文字を描画.
Definition: Font.h:349
double x
座標
Definition: Point.h:25
複数のImageをまとめるクラス.
Definition: ImagePack.h:14
void SetColor(const Color &描画色)
描画色を指定.
Definition: Image.h:316
bool Draw(const Point &座標, bool 反転フラグ=false) const override
指定座標に描画.
Definition: Image.h:181
int GetDrawStringWidth(const VariadicStream &幅を計算する文字列) const
描画時の幅を取得.
Definition: Font.h:278
Font(const char *フォント名, int 大きさ, int 行間=0, bool 高品質レンダリングフラグ=true)
コンストラクタ
Definition: Font.h:174
void SetImage(const std::string &文字, Image *対応画像)
指定した文字に対応するImageを設定.
Definition: Font.h:364