gosu 0.10.9.pre1 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (136) hide show
  1. checksums.yaml +4 -4
  2. data/Gosu/Audio.hpp +35 -66
  3. data/Gosu/AutoLink.hpp +14 -16
  4. data/Gosu/Bitmap.hpp +50 -37
  5. data/Gosu/Buttons.hpp +246 -265
  6. data/Gosu/Color.hpp +32 -76
  7. data/Gosu/Directories.hpp +14 -17
  8. data/Gosu/Font.hpp +28 -34
  9. data/Gosu/Fwd.hpp +27 -31
  10. data/Gosu/Gosu.hpp +2 -5
  11. data/Gosu/Graphics.hpp +31 -48
  12. data/Gosu/GraphicsBase.hpp +27 -58
  13. data/Gosu/IO.hpp +44 -56
  14. data/Gosu/Image.hpp +29 -73
  15. data/Gosu/ImageData.hpp +13 -17
  16. data/Gosu/Input.hpp +42 -57
  17. data/Gosu/Inspection.hpp +2 -6
  18. data/Gosu/Math.hpp +32 -38
  19. data/Gosu/Platform.hpp +10 -29
  20. data/Gosu/Text.hpp +30 -39
  21. data/Gosu/TextInput.hpp +29 -36
  22. data/Gosu/Timing.hpp +14 -16
  23. data/Gosu/Utility.hpp +10 -15
  24. data/Gosu/Version.hpp +13 -14
  25. data/Gosu/Window.hpp +53 -68
  26. data/README.md +23 -11
  27. data/ext/gosu/extconf.rb +31 -81
  28. data/lib/gosu/patches.rb +35 -19
  29. data/lib/gosu/run.rb +13 -4
  30. data/rdoc/gosu.rb +24 -20
  31. data/src/ALChannelManagement.hpp +119 -0
  32. data/src/{Audio/Audio.cpp → Audio.cpp} +177 -211
  33. data/src/AudioFile.hpp +57 -0
  34. data/src/AudioToolboxFile.hpp +214 -0
  35. data/src/Bitmap.cpp +159 -0
  36. data/src/BitmapIO.cpp +141 -0
  37. data/src/BlockAllocator.cpp +133 -0
  38. data/src/{Graphics/BlockAllocator.hpp → BlockAllocator.hpp} +34 -35
  39. data/src/ClipRectStack.hpp +87 -0
  40. data/src/{Graphics/Color.cpp → Color.cpp} +30 -28
  41. data/src/DirectoriesApple.cpp +68 -0
  42. data/src/DirectoriesUnix.cpp +20 -18
  43. data/src/DirectoriesWin.cpp +40 -41
  44. data/src/DrawOp.hpp +168 -0
  45. data/src/DrawOpQueue.hpp +190 -0
  46. data/src/FileUnix.cpp +40 -46
  47. data/src/FileWin.cpp +42 -38
  48. data/src/Font.cpp +165 -0
  49. data/src/{Text/FormattedString.hpp → FormattedString.hpp} +114 -114
  50. data/src/GosuAppDelegate.cpp +30 -0
  51. data/src/{UIKit/GosuAppDelegate.h → GosuAppDelegate.h} +0 -0
  52. data/src/{UIKit/GosuGLView.mm → GosuGLView.cpp} +22 -17
  53. data/src/{UIKit/GosuGLView.h → GosuGLView.h} +0 -0
  54. data/src/GosuViewController.cpp +231 -0
  55. data/src/{UIKit/GosuViewController.h → GosuViewController.h} +0 -0
  56. data/src/Graphics.cpp +464 -0
  57. data/src/{Graphics/Common.hpp → GraphicsImpl.hpp} +29 -32
  58. data/src/IO.cpp +17 -16
  59. data/src/Iconv.hpp +13 -22
  60. data/src/Image.cpp +142 -0
  61. data/src/Input.cpp +459 -0
  62. data/src/InputUIKit.cpp +197 -0
  63. data/src/Inspection.cpp +4 -5
  64. data/src/LargeImageData.cpp +151 -0
  65. data/src/LargeImageData.hpp +43 -0
  66. data/src/{Graphics/Macro.cpp → Macro.cpp} +77 -78
  67. data/src/Macro.hpp +30 -0
  68. data/src/Math.cpp +17 -29
  69. data/src/{Audio/OggFile.hpp → OggFile.hpp} +19 -24
  70. data/src/RenderState.hpp +205 -0
  71. data/src/Resolution.cpp +86 -0
  72. data/src/ResolutionApple.cpp +25 -0
  73. data/{ext/gosu/gosu_wrap.cxx → src/RubyGosu.cxx} +2256 -1707
  74. data/{ext/gosu/gosu_wrap.h → src/RubyGosu.h} +9 -9
  75. data/src/{Audio/SndFile.hpp → SndFile.hpp} +54 -43
  76. data/src/TexChunk.cpp +117 -0
  77. data/src/{Graphics/TexChunk.hpp → TexChunk.hpp} +13 -18
  78. data/src/Text.cpp +371 -0
  79. data/src/TextApple.cpp +209 -0
  80. data/src/TextInput.cpp +278 -0
  81. data/src/TextTTFWin.cpp +251 -0
  82. data/src/{Text/TextUnix.cpp → TextUnix.cpp} +96 -92
  83. data/src/TextWin.cpp +194 -0
  84. data/src/{Graphics/Texture.cpp → Texture.cpp} +35 -38
  85. data/src/{Graphics/Texture.hpp → Texture.hpp} +9 -13
  86. data/src/TimingApple.cpp +11 -7
  87. data/src/TimingUnix.cpp +13 -7
  88. data/src/TimingWin.cpp +6 -1
  89. data/src/{Graphics/Transform.cpp → Transform.cpp} +17 -12
  90. data/src/{Graphics/TransformStack.hpp → TransformStack.hpp} +24 -25
  91. data/src/Utility.cpp +29 -70
  92. data/src/UtilityApple.cpp +52 -0
  93. data/src/UtilityWin.cpp +7 -4
  94. data/src/Version.cpp +22 -0
  95. data/src/WinMain.cpp +30 -33
  96. data/src/WinUtility.cpp +24 -22
  97. data/src/WinUtility.hpp +11 -20
  98. data/src/Window.cpp +142 -112
  99. data/src/WindowUIKit.cpp +155 -0
  100. data/src/stb_image.h +384 -173
  101. data/src/stb_vorbis.c +20 -18
  102. metadata +60 -62
  103. data/Gosu/TR1.hpp +0 -56
  104. data/src/AppleUtility.hpp +0 -66
  105. data/src/Audio/ALChannelManagement.hpp +0 -114
  106. data/src/Audio/Audio.mm +0 -1
  107. data/src/Audio/AudioFile.hpp +0 -53
  108. data/src/Audio/AudioToolboxFile.hpp +0 -207
  109. data/src/Bitmap/Bitmap.cpp +0 -183
  110. data/src/Bitmap/BitmapIO.cpp +0 -176
  111. data/src/DirectoriesApple.mm +0 -71
  112. data/src/Graphics/BlockAllocator.cpp +0 -142
  113. data/src/Graphics/ClipRectStack.hpp +0 -93
  114. data/src/Graphics/DrawOp.hpp +0 -175
  115. data/src/Graphics/DrawOpQueue.hpp +0 -188
  116. data/src/Graphics/Graphics.cpp +0 -478
  117. data/src/Graphics/Image.cpp +0 -193
  118. data/src/Graphics/LargeImageData.cpp +0 -133
  119. data/src/Graphics/LargeImageData.hpp +0 -46
  120. data/src/Graphics/Macro.hpp +0 -36
  121. data/src/Graphics/RenderState.hpp +0 -211
  122. data/src/Graphics/Resolution.cpp +0 -91
  123. data/src/Graphics/ResolutionApple.mm +0 -19
  124. data/src/Graphics/TexChunk.cpp +0 -112
  125. data/src/Input/Input.cpp +0 -463
  126. data/src/Input/InputUIKit.mm +0 -190
  127. data/src/Input/TextInput.cpp +0 -261
  128. data/src/Text/Font.cpp +0 -175
  129. data/src/Text/Text.cpp +0 -391
  130. data/src/Text/TextApple.mm +0 -227
  131. data/src/Text/TextTTFWin.cpp +0 -249
  132. data/src/Text/TextWin.cpp +0 -186
  133. data/src/UIKit/GosuAppDelegate.mm +0 -24
  134. data/src/UIKit/GosuViewController.mm +0 -211
  135. data/src/UtilityApple.mm +0 -63
  136. data/src/WindowUIKit.mm +0 -139
@@ -0,0 +1,251 @@
1
+ #include <Gosu/Platform.hpp>
2
+ #if defined(GOSU_IS_WIN)
3
+
4
+ #include <Gosu/Utility.hpp>
5
+ #include <cassert>
6
+ #include <cstdio>
7
+ #include <windows.h>
8
+
9
+ // Adapted from http://www.codeproject.com/KB/GDI/xfont.aspx.
10
+ // Kudos to Philip Patrick and Hans Dietrich!
11
+
12
+ struct FONT_PROPERTIES_ANSI
13
+ {
14
+ char csName[1024];
15
+ char csCopyright[1024];
16
+ char csTrademark[1024];
17
+ char csFamily[1024];
18
+ };
19
+ struct TT_OFFSET_TABLE
20
+ {
21
+ USHORT uMajorVersion;
22
+ USHORT uMinorVersion;
23
+ USHORT uNumOfTables;
24
+ USHORT uSearchRange;
25
+ USHORT uEntrySelector;
26
+ USHORT uRangeShift;
27
+ };
28
+ struct TT_TABLE_DIRECTORY
29
+ {
30
+ char szTag[4]; //table name
31
+ ULONG uCheckSum; //Check sum
32
+ ULONG uOffset; //Offset from beginning of file
33
+ ULONG uLength; //length of the table in bytes
34
+ };
35
+ struct TT_NAME_TABLE_HEADER
36
+ {
37
+ USHORT uFSelector; //format selector. Always 0
38
+ USHORT uNRCount; //Name Records count
39
+ USHORT uStorageOffset; //Offset for strings storage, from start of the table
40
+ };
41
+ struct TT_NAME_RECORD
42
+ {
43
+ USHORT uPlatformID;
44
+ USHORT uEncodingID;
45
+ USHORT uLanguageID;
46
+ USHORT uNameID;
47
+ USHORT uStringLength;
48
+ USHORT uStringOffset; //from start of storage area
49
+ };
50
+
51
+ #define SWAPWORD(x) MAKEWORD(HIBYTE(x), LOBYTE(x))
52
+ #define SWAPLONG(x) MAKELONG(SWAPWORD(HIWORD(x)), SWAPWORD(LOWORD(x)))
53
+
54
+ #define _T(x) x
55
+ #define TRACE printf
56
+
57
+ namespace Gosu
58
+ {
59
+ std::string get_name_from_ttf_file(const std::wstring& filename)
60
+ {
61
+ FONT_PROPERTIES_ANSI fp;
62
+ FONT_PROPERTIES_ANSI * lpFontProps = &fp;
63
+ memset(lpFontProps, 0, sizeof(FONT_PROPERTIES_ANSI));
64
+
65
+ HANDLE hFile = INVALID_HANDLE_VALUE;
66
+ hFile = ::CreateFile(filename.c_str(),
67
+ GENERIC_READ,// | GENERIC_WRITE,
68
+ FILE_SHARE_READ,
69
+ NULL,
70
+ OPEN_EXISTING,
71
+ FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
72
+ NULL);
73
+
74
+ if (hFile == INVALID_HANDLE_VALUE)
75
+ {
76
+ TRACE(_T("ERROR: failed to open '%s'\n"), wstring_to_utf8(filename).c_str());
77
+ TRACE(_T("ERROR: %s failed, GetLastError() = 0x%x\n"), _T("CreateFile"), (int)GetLastError());
78
+ return wstring_to_utf8(filename);
79
+ }
80
+
81
+ // get the file size
82
+ DWORD dwFileSize = ::GetFileSize(hFile, NULL);
83
+
84
+ if (dwFileSize == INVALID_FILE_SIZE)
85
+ {
86
+ TRACE(_T("ERROR: %s failed\n"), _T("GetFileSize"));
87
+ ::CloseHandle(hFile);
88
+ return wstring_to_utf8(filename);
89
+ }
90
+
91
+ //TRACE(_T("dwFileSize = %d\n"), dwFileSize);
92
+
93
+ // Create a file mapping object that is the current size of the file
94
+ HANDLE hMappedFile = NULL;
95
+ hMappedFile = ::CreateFileMapping(hFile,
96
+ NULL,
97
+ PAGE_READONLY, //PAGE_READWRITE,
98
+ 0,
99
+ dwFileSize,
100
+ NULL);
101
+
102
+ if (hMappedFile == NULL)
103
+ {
104
+ TRACE(_T("ERROR: %s failed\n"), _T("CreateFileMapping"));
105
+ ::CloseHandle(hFile);
106
+ return wstring_to_utf8(filename);
107
+ }
108
+
109
+ LPBYTE lpMapAddress = (LPBYTE) ::MapViewOfFile(hMappedFile, // handle to file-mapping object
110
+ FILE_MAP_READ,//FILE_MAP_WRITE, // access mode
111
+ 0, // high-order DWORD of offset
112
+ 0, // low-order DWORD of offset
113
+ 0); // number of bytes to map
114
+
115
+ if (lpMapAddress == NULL)
116
+ {
117
+ TRACE(_T("ERROR: %s failed\n"), _T("MapViewOfFile"));
118
+ ::CloseHandle(hMappedFile);
119
+ ::CloseHandle(hFile);
120
+ return wstring_to_utf8(filename);
121
+ }
122
+
123
+ BOOL bRetVal = FALSE;
124
+ int index = 0;
125
+
126
+ TT_OFFSET_TABLE ttOffsetTable;
127
+ memcpy(&ttOffsetTable, &lpMapAddress[index], sizeof(TT_OFFSET_TABLE));
128
+ index += sizeof(TT_OFFSET_TABLE);
129
+
130
+ ttOffsetTable.uNumOfTables = SWAPWORD(ttOffsetTable.uNumOfTables);
131
+ ttOffsetTable.uMajorVersion = SWAPWORD(ttOffsetTable.uMajorVersion);
132
+ ttOffsetTable.uMinorVersion = SWAPWORD(ttOffsetTable.uMinorVersion);
133
+
134
+ //check is this is a true type font and the version is 1.0
135
+ if (ttOffsetTable.uMajorVersion != 1 || ttOffsetTable.uMinorVersion != 0)
136
+ return wstring_to_utf8(filename);
137
+
138
+ TT_TABLE_DIRECTORY tblDir;
139
+ memset(&tblDir, 0, sizeof(TT_TABLE_DIRECTORY));
140
+ BOOL bFound = FALSE;
141
+ char szTemp[4096];
142
+ memset(szTemp, 0, sizeof(szTemp));
143
+
144
+ for (int i = 0; i< ttOffsetTable.uNumOfTables; i++)
145
+ {
146
+ //f.Read(&tblDir, sizeof(TT_TABLE_DIRECTORY));
147
+ memcpy(&tblDir, &lpMapAddress[index], sizeof(TT_TABLE_DIRECTORY));
148
+ index += sizeof(TT_TABLE_DIRECTORY);
149
+
150
+ strncpy(szTemp, tblDir.szTag, 4);
151
+ if (stricmp(szTemp, "name") == 0)
152
+ {
153
+ bFound = TRUE;
154
+ tblDir.uLength = SWAPLONG(tblDir.uLength);
155
+ tblDir.uOffset = SWAPLONG(tblDir.uOffset);
156
+ break;
157
+ }
158
+ else if (szTemp[0] == 0)
159
+ {
160
+ break;
161
+ }
162
+ }
163
+
164
+ if (bFound)
165
+ {
166
+ index = tblDir.uOffset;
167
+
168
+ TT_NAME_TABLE_HEADER ttNTHeader;
169
+ memcpy(&ttNTHeader, &lpMapAddress[index], sizeof(TT_NAME_TABLE_HEADER));
170
+ index += sizeof(TT_NAME_TABLE_HEADER);
171
+
172
+ ttNTHeader.uNRCount = SWAPWORD(ttNTHeader.uNRCount);
173
+ ttNTHeader.uStorageOffset = SWAPWORD(ttNTHeader.uStorageOffset);
174
+ TT_NAME_RECORD ttRecord;
175
+ bFound = FALSE;
176
+
177
+ for (int i = 0;
178
+ i < ttNTHeader.uNRCount &&
179
+ (lpFontProps->csCopyright[0] == 0 ||
180
+ lpFontProps->csName[0] == 0 ||
181
+ lpFontProps->csTrademark[0] == 0 ||
182
+ lpFontProps->csFamily[0] == 0);
183
+ i++)
184
+ {
185
+ memcpy(&ttRecord, &lpMapAddress[index], sizeof(TT_NAME_RECORD));
186
+ index += sizeof(TT_NAME_RECORD);
187
+
188
+ ttRecord.uNameID = SWAPWORD(ttRecord.uNameID);
189
+ ttRecord.uStringLength = SWAPWORD(ttRecord.uStringLength);
190
+ ttRecord.uStringOffset = SWAPWORD(ttRecord.uStringOffset);
191
+
192
+ if (ttRecord.uNameID == 1 || ttRecord.uNameID == 0 || ttRecord.uNameID == 7)
193
+ {
194
+ int nPos = index; //f.GetPosition();
195
+
196
+ index = tblDir.uOffset + ttRecord.uStringOffset + ttNTHeader.uStorageOffset;
197
+
198
+ memset(szTemp, 0, sizeof(szTemp));
199
+
200
+ memcpy(szTemp, &lpMapAddress[index], ttRecord.uStringLength);
201
+ index += ttRecord.uStringLength;
202
+
203
+ if (szTemp[0] != 0)
204
+ {
205
+ assert (strlen(szTemp) < sizeof(lpFontProps->csName));
206
+
207
+ switch (ttRecord.uNameID)
208
+ {
209
+ case 0:
210
+ if (lpFontProps->csCopyright[0] == 0)
211
+ strncpy(lpFontProps->csCopyright, szTemp,
212
+ sizeof(lpFontProps->csCopyright)-1);
213
+ break;
214
+
215
+ case 1:
216
+ if (lpFontProps->csFamily[0] == 0)
217
+ strncpy(lpFontProps->csFamily, szTemp,
218
+ sizeof(lpFontProps->csFamily)-1);
219
+ bRetVal = TRUE;
220
+ break;
221
+
222
+ case 4:
223
+ if (lpFontProps->csName[0] == 0)
224
+ strncpy(lpFontProps->csName, szTemp,
225
+ sizeof(lpFontProps->csName)-1);
226
+ break;
227
+
228
+ case 7:
229
+ if (lpFontProps->csTrademark[0] == 0)
230
+ strncpy(lpFontProps->csTrademark, szTemp,
231
+ sizeof(lpFontProps->csTrademark)-1);
232
+ break;
233
+
234
+ default:
235
+ break;
236
+ }
237
+ }
238
+ index = nPos;
239
+ }
240
+ }
241
+ }
242
+
243
+ ::UnmapViewOfFile(lpMapAddress);
244
+ ::CloseHandle(hMappedFile);
245
+ ::CloseHandle(hFile);
246
+
247
+ return lpFontProps->csName[0] ? lpFontProps->csName : lpFontProps->csFamily;
248
+ }
249
+ }
250
+
251
+ #endif
@@ -1,19 +1,22 @@
1
+ #include <Gosu/Platform.hpp>
2
+ #if defined(GOSU_IS_X)
3
+
1
4
  #include <Gosu/Text.hpp>
2
5
  #include <Gosu/Bitmap.hpp>
3
6
  #include <Gosu/Utility.hpp>
4
7
 
8
+ #include <SDL_ttf.h>
9
+ #include <glib.h>
5
10
  #include <pango/pango.h>
6
11
  #include <pango/pangoft2.h>
7
- #include <glib.h>
8
- #include <SDL_ttf.h>
9
12
 
10
- #include <string>
11
13
  #include <cstring>
12
14
  #include <stdexcept>
15
+ #include <string>
13
16
 
14
- std::wstring Gosu::defaultFontName()
17
+ std::string Gosu::default_font_name()
15
18
  {
16
- return L"sans";
19
+ return "sans";
17
20
  }
18
21
 
19
22
  namespace Gosu
@@ -29,30 +32,34 @@ namespace Gosu
29
32
 
30
33
  PangoContext* context;
31
34
  PangoLayout* layout;
32
- PangoFontDescription *font_description;
35
+ PangoFontDescription* font_description;
33
36
  PangoAttribute* attr;
34
- PangoAttrList* attrList;
37
+ PangoAttrList* attr_list;
35
38
 
36
39
  public:
37
40
  PangoRenderer()
38
41
  {
39
42
  font_description = NULL;
40
43
  attr = NULL;
41
- attrList = NULL;
44
+ attr_list = NULL;
42
45
  }
46
+
43
47
  ~PangoRenderer()
44
48
  {
45
49
  g_object_unref(context);
46
50
  g_object_unref(layout);
47
51
 
48
- if(font_description)
52
+ if (font_description) {
49
53
  pango_font_description_free(font_description);
50
- if(attr)
54
+ }
55
+
56
+ if (attr) {
51
57
  pango_attribute_destroy(attr);
58
+ }
52
59
  }
53
- unsigned textWidth(const std::wstring& text,
54
- const std::wstring& fontFace, unsigned fontHeight,
55
- unsigned fontFlags)
60
+
61
+ unsigned text_width(const std::string& text, const std::string& font_face,
62
+ unsigned font_height, unsigned font_flags)
56
63
  {
57
64
  g_type_init();
58
65
 
@@ -60,47 +67,37 @@ namespace Gosu
60
67
 
61
68
  context = pango_ft2_get_context(dpi_x, dpi_y);
62
69
 
63
- pango_context_set_language(context, pango_language_from_string ("en_US"));
70
+ pango_context_set_language(context, pango_language_from_string("en_US"));
64
71
  PangoDirection init_dir = PANGO_DIRECTION_LTR;
65
72
  pango_context_set_base_dir(context, init_dir);
66
73
 
67
74
  font_description = pango_font_description_new();
68
75
 
69
- pango_font_description_set_family(font_description,
70
- g_strdup(narrow(fontFace).c_str()));
76
+ pango_font_description_set_family(font_description, g_strdup(font_face.c_str()));
71
77
  pango_font_description_set_style(font_description,
72
- (fontFlags & ffItalic) ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL);
78
+ (font_flags & FF_ITALIC) ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL);
73
79
  pango_font_description_set_variant(font_description, PANGO_VARIANT_NORMAL);
74
80
  pango_font_description_set_weight(font_description,
75
- (fontFlags & ffBold) ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL);
81
+ (font_flags & FF_BOLD) ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL);
76
82
  pango_font_description_set_stretch(font_description, PANGO_STRETCH_NORMAL);
77
- int init_scale = int(fontHeight/2.0 + 0.5);
83
+ int init_scale = int(font_height / 2.0 + 0.5);
78
84
  pango_font_description_set_size(font_description, init_scale * PANGO_SCALE);
79
85
 
80
86
  pango_context_set_font_description(context, font_description);
81
87
 
82
-
83
88
  layout = pango_layout_new(context);
84
89
 
85
-
86
- if(fontFlags & ffUnderline)
87
- {
88
- // PangoAttribute *attr;
90
+ if (font_flags & FF_UNDERLINE) {
89
91
  attr = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
90
92
  attr->start_index = 0;
91
93
  attr->end_index = text.length();
92
- // PangoAttrList* attrList;
93
- attrList = pango_attr_list_new();
94
- pango_attr_list_insert(attrList, attr);
95
- pango_layout_set_attributes(layout, attrList);
96
- pango_attr_list_unref(attrList);
94
+ attr_list = pango_attr_list_new();
95
+ pango_attr_list_insert(attr_list, attr);
96
+ pango_layout_set_attributes(layout, attr_list);
97
+ pango_attr_list_unref(attr_list);
97
98
  }
98
99
 
99
-
100
- // IMPR: Catch errors? (Last NULL-Pointer)
101
- gchar* utf8Str = g_ucs4_to_utf8((gunichar*)text.c_str(), text.length(), NULL, NULL, NULL);
102
- pango_layout_set_text(layout, utf8Str, -1);
103
- g_free(utf8Str);
100
+ pango_layout_set_text(layout, text.c_str(), -1);
104
101
 
105
102
  PangoDirection base_dir = pango_context_get_base_dir(context);
106
103
  pango_layout_set_alignment(layout,
@@ -116,11 +113,11 @@ namespace Gosu
116
113
 
117
114
  return width;
118
115
  }
119
- void drawText(Bitmap& bitmap, const std::wstring& text, int x, int y,
120
- Color c, const std::wstring& fontFace, unsigned fontHeight,
121
- unsigned fontFlags)
116
+
117
+ void draw_text(Bitmap& bitmap, const std::string& text, int x, int y, Color c,
118
+ const std::string& font_face, unsigned font_height, unsigned font_flags)
122
119
  {
123
- textWidth(text, fontFace, fontHeight, fontFlags);
120
+ text_width(text, font_face, font_height, font_flags);
124
121
 
125
122
  FT_Bitmap ft_bitmap;
126
123
 
@@ -137,22 +134,16 @@ namespace Gosu
137
134
  int x_start = 0;
138
135
  pango_ft2_render_layout(&ft_bitmap, layout, x_start, 0);
139
136
 
137
+ int min_height = std::min<int>(height, font_height);
140
138
 
141
- int min_height = height;
142
- if((unsigned)height > fontHeight) min_height = fontHeight;
143
-
144
- for(int y2 = 0; y2 < min_height; y2++)
145
- {
146
- if (y + y2 < 0 || y + y2 >= bitmap.height())
147
- break;
148
-
149
- for(int x2 = 0; x2 < width; x2++)
150
- {
151
- if (x + x2 < 0 || x + x2 >= bitmap.width())
152
- break;
153
- unsigned val = ft_bitmap.buffer[y2*width+x2];
139
+ for (int y2 = 0; y2 < min_height; y2++) {
140
+ if (y + y2 < 0 || y + y2 >= bitmap.height()) break;
141
+
142
+ for (int x2 = 0; x2 < width; x2++) {
143
+ if (x + x2 < 0 || x + x2 >= bitmap.width()) break;
144
+ unsigned val = ft_bitmap.buffer[y2 * width + x2];
154
145
  Color color = multiply(c, Gosu::Color(val, 255, 255, 255));
155
- bitmap.setPixel(x2 + x, y2 + y, color);
146
+ bitmap.set_pixel(x2 + x, y2 + y, color);
156
147
  }
157
148
  }
158
149
 
@@ -160,13 +151,14 @@ namespace Gosu
160
151
  }
161
152
  };
162
153
 
163
-
164
154
  // Used for custom TTF files
165
- // Adapted from customFont class by José Tomás Tocino García (TheOm3ga)
155
+ // Adapted from custom_font class by José Tomás Tocino García (TheOm3ga)
166
156
  class SDLTTFRenderer
167
157
  {
168
- SDLTTFRenderer(const SDLTTFRenderer&);
169
- SDLTTFRenderer& operator=(const SDLTTFRenderer&);
158
+ SDLTTFRenderer(const SDLTTFRenderer&) = delete;
159
+ SDLTTFRenderer& operator=(const SDLTTFRenderer&) = delete;
160
+ SDLTTFRenderer(SDLTTFRenderer&&) = delete;
161
+ SDLTTFRenderer& operator=(SDLTTFRenderer&&) = delete;
170
162
 
171
163
  TTF_Font* font;
172
164
 
@@ -178,14 +170,15 @@ namespace Gosu
178
170
  SDL_Surface* surface;
179
171
 
180
172
  public:
181
- SDLSurface(TTF_Font* font, const std::wstring& text, Gosu::Color c)
173
+ SDLSurface(TTF_Font* font, const std::string& text, Gosu::Color c)
182
174
  {
183
175
  // This is intentionally re-ordered to BGR. This way, the surface pixels do not
184
176
  // have to be converted from RGB to BGR later in the process.
185
177
  SDL_Color color = { c.blue(), c.green(), c.red() };
186
- surface = TTF_RenderUTF8_Blended(font, Gosu::wstringToUTF8(text).c_str(), color);
187
- if (!surface)
188
- throw std::runtime_error("Could not render text " + Gosu::wstringToUTF8(text));
178
+ surface = TTF_RenderUTF8_Blended(font, text.c_str(), color);
179
+ if (!surface) {
180
+ throw std::runtime_error("Could not render text: " + text);
181
+ }
189
182
  }
190
183
 
191
184
  ~SDLSurface()
@@ -210,24 +203,27 @@ namespace Gosu
210
203
  };
211
204
 
212
205
  public:
213
- SDLTTFRenderer(const std::wstring& fontName, unsigned fontHeight)
206
+ SDLTTFRenderer(const std::string& font_name, unsigned font_height)
214
207
  {
215
- static int initResult = TTF_Init();
216
- if (initResult < 0)
208
+ static int init_result = TTF_Init();
209
+ if (init_result < 0) {
217
210
  throw std::runtime_error("Could not initialize SDL_TTF");
211
+ }
218
212
 
219
- // Try to open the font at the given path
220
- font = TTF_OpenFont(Gosu::wstringToUTF8(fontName).c_str(), fontHeight);
221
- if (!font)
222
- throw std::runtime_error("Could not open TTF file " + Gosu::wstringToUTF8(fontName));
213
+ // Try to open the font at the given path
214
+ font = TTF_OpenFont(font_name.c_str(), font_height);
215
+ if (!font) {
216
+ throw std::runtime_error("Could not open TTF file " + font_name);
217
+ }
223
218
 
224
219
  // Re-open with scaled height so that ascenders/descenders fit
225
- int tooLargeHeight = TTF_FontHeight(font);
226
- int realHeight = fontHeight * fontHeight / tooLargeHeight;
220
+ int too_large_height = TTF_FontHeight(font);
221
+ int real_height = font_height * font_height / too_large_height;
227
222
  TTF_CloseFont(font);
228
- font = TTF_OpenFont(Gosu::wstringToUTF8(fontName).c_str(), realHeight);
229
- if (!font)
230
- throw std::runtime_error("Could not open TTF file " + Gosu::wstringToUTF8(fontName));
223
+ font = TTF_OpenFont(font_name.c_str(), real_height);
224
+ if (!font) {
225
+ throw std::runtime_error("Could not open TTF file " + font_name);
226
+ }
231
227
  }
232
228
 
233
229
  ~SDLTTFRenderer()
@@ -235,11 +231,13 @@ namespace Gosu
235
231
  TTF_CloseFont(font);
236
232
  }
237
233
 
238
- unsigned textWidth(const std::wstring& text){
234
+ unsigned text_width(const std::string& text)
235
+ {
239
236
  return SDLSurface(font, text, 0xffffff).width();
240
237
  }
241
238
 
242
- void drawText(Bitmap& bmp, const std::wstring& text, int x, int y, Gosu::Color c) {
239
+ void draw_text(Bitmap& bmp, const std::string& text, int x, int y, Gosu::Color c)
240
+ {
243
241
  SDLSurface surf(font, text, c);
244
242
  Gosu::Bitmap temp;
245
243
  temp.resize(surf.width(), surf.height());
@@ -249,28 +247,34 @@ namespace Gosu
249
247
  };
250
248
  }
251
249
 
252
- unsigned Gosu::textWidth(const std::wstring& text,
253
- const std::wstring& fontName, unsigned fontHeight,
254
- unsigned fontFlags)
250
+ unsigned Gosu::text_width(const std::string& text, const std::string& font_name,
251
+ unsigned font_height, unsigned font_flags)
255
252
  {
256
- if (text.find_first_of(L"\r\n") != std::wstring::npos)
257
- throw std::invalid_argument("the argument to textWidth cannot contain line breaks");
253
+ if (text.find_first_of("\r\n") != text.npos) {
254
+ throw std::invalid_argument("the argument to text_width cannot contain line breaks");
255
+ }
258
256
 
259
- if (fontName.find(L"/") == std::wstring::npos)
260
- return PangoRenderer().textWidth(text, fontName, fontHeight, fontFlags);
261
- else
262
- return SDLTTFRenderer(fontName, fontHeight).textWidth(text);
257
+ if (font_name.find("/") == font_name.npos) {
258
+ return PangoRenderer().text_width(text, font_name, font_height, font_flags);
259
+ }
260
+ else {
261
+ return SDLTTFRenderer(font_name, font_height).text_width(text);
262
+ }
263
263
  }
264
264
 
265
- void Gosu::drawText(Bitmap& bitmap, const std::wstring& text, int x, int y,
266
- Color c, const std::wstring& fontName, unsigned fontHeight,
267
- unsigned fontFlags)
265
+ void Gosu::draw_text(Bitmap& bitmap, const std::string& text, int x, int y, Color c,
266
+ const std::string& font_name, unsigned font_height, unsigned font_flags)
268
267
  {
269
- if (text.find_first_of(L"\r\n") != std::wstring::npos)
270
- throw std::invalid_argument("the argument to drawText cannot contain line breaks");
268
+ if (text.find_first_of("\r\n") != text.npos) {
269
+ throw std::invalid_argument("the argument to draw_text cannot contain line breaks");
270
+ }
271
271
 
272
- if (fontName.find(L"/") == std::wstring::npos)
273
- PangoRenderer().drawText(bitmap, text, x, y, c, fontName, fontHeight, fontFlags);
274
- else
275
- SDLTTFRenderer(fontName, fontHeight).drawText(bitmap, text, x, y, c);
272
+ if (font_name.find("/") == font_name.npos) {
273
+ PangoRenderer().draw_text(bitmap, text, x, y, c, font_name, font_height, font_flags);
274
+ }
275
+ else {
276
+ SDLTTFRenderer(font_name, font_height).draw_text(bitmap, text, x, y, c);
277
+ }
276
278
  }
279
+
280
+ #endif