-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTextureManager.c
318 lines (267 loc) · 10.7 KB
/
TextureManager.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Copyright (C) 2022 鏡原 尚 <[email protected]>
// This file is part of RaylibIMEInputSampleApp.
// RaylibIMEInputSampleApp is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// RaylibIMEInputSampleApp is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "TextureManager.h"
#if defined(WIN32)
#include <libloaderapi.h>
#include <stringapiset.h>
#elif defined(APPLE)
#include <mach-o/dyld.h>
#endif
#define FONT_FILEPATH_MAX_LEN 550
#define FILEPATH_BUFFER_LEN 500 // Need to make this smaller than FONT_FILEPATH_MAX_LEN to suppress warning...
static unsigned int NumOfPixelsInOneRow(TextureManager* manager);
static void WriteOneChar(TextureManager* manager, unsigned char* data, int character, bool wordWrap);
static char* GetFontFilepath();
bool TextureManager_Initialize(TextureManager* manager, int width, int height, int fontSize)
{
setlocale(LC_CTYPE, "ja_JP");
manager->m_TextureWidth = width;
manager->m_TextureHeight = height;
manager->m_FontSize = fontSize;
//freetypeライブラリ初期化
int error = FT_Init_FreeType(&manager->m_ftLibrary);
if (error != 0)
{
printf("FT_Init_FreeType failed with error: %d\n", error);
return false;
}
const char* fontFilepath = GetFontFilepath();
if (!fontFilepath)
{
printf("GetFontFilepath failed\n");
return false;
}
//読み込んだフォントからface作成
error = FT_New_Face(manager->m_ftLibrary, fontFilepath, 0, &manager->m_ftFace);
if (error != 0)
{
printf("FT_New_Face failed with error: %d, fontFilepath: %s\n", error, fontFilepath);
return false;
}
//生成するグリフ用のフォント設定
error = FT_Set_Char_Size(manager->m_ftFace, 0, 16 * manager->m_FontSize, 300, 300);
if (error != 0)
{
printf("FT_Set_Char_Size failed with error: %d\n", error);
return false;
}
unsigned int bufferSize = manager->m_TextureWidth * manager->m_TextureHeight * 4;
//出力画像用の設定
manager->m_Image.data = (unsigned char*)malloc(bufferSize);
manager->m_Image.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8;
manager->m_Image.mipmaps = 1;
manager->m_Image.width = manager->m_TextureWidth;
manager->m_Image.height = manager->m_TextureHeight;
//出力画像用のバッファの初期化
manager->m_InitData = (unsigned char*)malloc(bufferSize);
for (int i = 0; i < bufferSize; i += 4)
{
manager->m_InitData[i] = 255;
manager->m_InitData[i + 1] = 255;
manager->m_InitData[i + 2] = 255;
manager->m_InitData[i + 3] = 0;
}
return true;
}
Texture2D TextureManager_CreateTexture(TextureManager* manager, const int* text, int textLength)
{
// 画像データ初期化
unsigned char* data = (unsigned char*)manager->m_Image.data;
memcpy(data, manager->m_InitData, manager->m_TextureWidth * manager->m_TextureHeight * 4);
manager->m_currentX = 0;
manager->m_currentRow = 0;
// 1文字ずつ画像バッファに書き込む
for (int i = 0; i < textLength; ++i)
WriteOneChar(manager, data, text[i], true);
// TODO カーソル位置を動かせるようにする場合は、ここの計算処理も改良する
manager->m_CursorPosX = manager->m_currentX;
manager->m_CursorPosY = manager->m_currentRow * manager->m_FontSize;
return LoadTextureFromImage(manager->m_Image);
}
#ifdef MANAGE_PREEDIT_CANDIDATE
Texture2D TextureManager_CreateTextureForCandidate(TextureManager* manager, int** candidates, int* lengths,
int pageSize, int selectedIndex)
{
// 画像データ初期化
unsigned char* data = (unsigned char*)manager->m_Image.data;
memcpy(data, manager->m_InitData, manager->m_TextureWidth * manager->m_TextureHeight * 4);
manager->m_currentX = 0;
manager->m_currentRow = 0;
for (int index = 0; index < pageSize; ++index)
{
if (index == selectedIndex)
{
WriteOneChar(manager, data, '>', false);
WriteOneChar(manager, data, '>', false);
}
const int* text = candidates[index];
for (int i = 0; i < lengths[index]; ++i)
WriteOneChar(manager, data, text[i], false);
manager->m_currentRow++;
manager->m_currentX = 0;
}
// 使い道を想定していないが、とりあえず終端の位置にしておく
manager->m_CursorPosX = manager->m_currentX;
manager->m_CursorPosY = manager->m_currentRow * manager->m_FontSize;
return LoadTextureFromImage(manager->m_Image);
}
#endif
static unsigned int NumOfPixelsInOneRow(TextureManager* manager)
{
return manager->m_FontSize * manager->m_TextureWidth;
}
static void WriteOneChar(TextureManager* manager, unsigned char* data, int character, bool wordWrap)
{
// グリフを読み込み
int error = FT_Load_Char(manager->m_ftFace, character, FT_LOAD_RENDER);
if (error != 0)
{
printf("FT_Load_Char failed with error: %d\n", error);
return;
}
FT_GlyphSlot slot = manager->m_ftFace->glyph;
// 文字を表示する横幅の調整
manager->m_currentX += slot->bitmap_left;
if (wordWrap)
{
// テクスチャ幅をオーバーする場合は先に改行しておく
if (manager->m_currentX + slot->bitmap.width >= manager->m_TextureWidth)
{
manager->m_currentRow++;
//描画スタート位置を次の行の先頭に
manager->m_currentX = slot->bitmap_left;
}
}
// この文字を書き込むべきオフセット位置
unsigned int bufferOffset = 4 * (manager->m_currentX + manager->m_currentRow * NumOfPixelsInOneRow(manager));
// オフセットを起点として、この文字を書き込み処理を始める高さ
int glyfY = manager->m_FontSize - slot->bitmap_top;
// バッファにグリフの情報を書き込む
for (int i = 0, glyfX = 0; i < slot->bitmap.width * slot->bitmap.rows; ++i, ++glyfX)
{
// 今の行(1文字の内部における1ピクセル行)の書き込みが終わったら、次の行へ
if (glyfX >= slot->bitmap.width)
{
glyfX = 0;
glyfY++;
}
// 1ピクセル書き込み
unsigned char pixColor = 255 - slot->bitmap.buffer[i];
if (pixColor != 255)
{
unsigned int writeIndex = bufferOffset + 4 * (glyfX + glyfY * manager->m_TextureWidth);
data[writeIndex] = pixColor;
data[writeIndex + 1] = pixColor;
data[writeIndex + 2] = pixColor;
data[writeIndex + 3] = slot->bitmap.buffer[i];
}
}
// 次の文字の書き込み位置の計算
manager->m_currentX += slot->bitmap.width;
}
static char* GetFontFilepath()
{
#if defined(WIN32)
static char fontFilepath[FONT_FILEPATH_MAX_LEN];
static char filepathBuffer[FILEPATH_BUFFER_LEN];
static wchar_t filepathBufferW[FILEPATH_BUFFER_LEN];
static bool hasInitialized = false;
if (hasInitialized)
return fontFilepath;
DWORD size = GetModuleFileNameW(NULL, filepathBufferW, FILEPATH_BUFFER_LEN);
if (size < 0)
return NULL;
for (int i = size - 1; 0 <= i; --i)
{
if (filepathBufferW[i] == L'\\')
{
filepathBufferW[i] = '\0';
break;
}
}
int mbSize = WideCharToMultiByte(CP_ACP,
0,
filepathBufferW,
-1,
filepathBuffer,
FILEPATH_BUFFER_LEN,
NULL,
NULL);
if (mbSize == 0)
return NULL;
snprintf(fontFilepath, FONT_FILEPATH_MAX_LEN, "%s\\%s", filepathBuffer, FONT_FILENAME);
hasInitialized = true;
return fontFilepath;
#elif defined(APPLE)
static char fontFilepath[FONT_FILEPATH_MAX_LEN];
static char filepathBuffer[FILEPATH_BUFFER_LEN];
static bool hasInitialized = false;
if (hasInitialized)
return fontFilepath;
uint32_t bufSize = FILEPATH_BUFFER_LEN;
if (_NSGetExecutablePath(filepathBuffer, &bufSize) != 0)
return NULL;
// `_NSGetExecutablePath()` sets `bufSize` only when the size is not enough
// and it returns `-1`. So, in this case, we have to take the actual size
// by ourselves.
// https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/dyld.3.html
bufSize = strnlen(filepathBuffer, FILEPATH_BUFFER_LEN);
// RaylibIMEInputSampleApp.app/Contents/MacOS/RaylibIMEInputSampleApp
// RaylibIMEInputSampleApp.app/Contents/Resources/
int depthBacked = 0;
for (int i = bufSize - 1; 0 <= i; --i)
{
if (filepathBuffer[i] == '/')
{
depthBacked++;
if (depthBacked == 2)
{
filepathBuffer[i] = '\0';
break;
}
}
}
snprintf(fontFilepath, FONT_FILEPATH_MAX_LEN, "%s/Resources/%s", filepathBuffer, FONT_FILENAME);
hasInitialized = true;
return fontFilepath;
#elif defined(UNIX)
static char fontFilepath[FONT_FILEPATH_MAX_LEN];
static char filepathBuffer[FILEPATH_BUFFER_LEN];
static bool hasInitialized = false;
if (hasInitialized)
return fontFilepath;
ssize_t size = readlink("/proc/self/exe", filepathBuffer, FILEPATH_BUFFER_LEN);
if (size < 0)
return NULL;
bool hasSlashFound = false;
for (int i = size - 1; 0 <= i; --i)
{
if (filepathBuffer[i] == '/')
{
filepathBuffer[i] = '\0';
hasSlashFound = true;
break;
}
}
if (hasSlashFound)
snprintf(fontFilepath, FONT_FILEPATH_MAX_LEN, "%s/%s", filepathBuffer, FONT_FILENAME);
else
// Consider this as a relative path from the current dir although I don't know if this case is possible.
snprintf(fontFilepath, FONT_FILEPATH_MAX_LEN, "%s", FONT_FILENAME);
hasInitialized = true;
return fontFilepath;
#else
printf("GetFontFilepath: Unexpected platform.\n");
return NULL;
#endif
}