gosu 0.7.10.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (249) hide show
  1. data/COPYING.txt +29 -0
  2. data/Gosu/Async.hpp +48 -0
  3. data/Gosu/Audio.hpp +145 -0
  4. data/Gosu/AutoLink.hpp +16 -0
  5. data/Gosu/Bitmap.hpp +85 -0
  6. data/Gosu/ButtonsMac.hpp +114 -0
  7. data/Gosu/ButtonsWin.hpp +111 -0
  8. data/Gosu/ButtonsX.hpp +115 -0
  9. data/Gosu/Color.hpp +172 -0
  10. data/Gosu/Directories.hpp +36 -0
  11. data/Gosu/Font.hpp +59 -0
  12. data/Gosu/Fwd.hpp +31 -0
  13. data/Gosu/Gosu.hpp +26 -0
  14. data/Gosu/Graphics.hpp +86 -0
  15. data/Gosu/GraphicsBase.hpp +45 -0
  16. data/Gosu/IO.hpp +255 -0
  17. data/Gosu/Image.hpp +148 -0
  18. data/Gosu/ImageData.hpp +45 -0
  19. data/Gosu/Input.hpp +116 -0
  20. data/Gosu/Math.hpp +95 -0
  21. data/Gosu/Platform.hpp +61 -0
  22. data/Gosu/RotFlip.hpp +116 -0
  23. data/Gosu/Sockets.hpp +129 -0
  24. data/Gosu/Text.hpp +47 -0
  25. data/Gosu/TextInput.hpp +57 -0
  26. data/Gosu/Timing.hpp +16 -0
  27. data/Gosu/Utility.hpp +24 -0
  28. data/Gosu/WinUtility.hpp +76 -0
  29. data/Gosu/Window.hpp +84 -0
  30. data/GosuImpl/Async.cpp +37 -0
  31. data/GosuImpl/AudioFmod.cpp +417 -0
  32. data/GosuImpl/AudioSDL.cpp +255 -0
  33. data/GosuImpl/DirectoriesMac.mm +38 -0
  34. data/GosuImpl/DirectoriesUnix.cpp +48 -0
  35. data/GosuImpl/DirectoriesWin.cpp +42 -0
  36. data/GosuImpl/FileUnix.cpp +100 -0
  37. data/GosuImpl/FileWin.cpp +83 -0
  38. data/GosuImpl/Graphics/Bitmap.cpp +116 -0
  39. data/GosuImpl/Graphics/BitmapBMP.cpp +232 -0
  40. data/GosuImpl/Graphics/BitmapColorKey.cpp +39 -0
  41. data/GosuImpl/Graphics/BitmapPNG.cpp +276 -0
  42. data/GosuImpl/Graphics/BitmapUtils.cpp +67 -0
  43. data/GosuImpl/Graphics/BlockAllocator.cpp +127 -0
  44. data/GosuImpl/Graphics/BlockAllocator.hpp +34 -0
  45. data/GosuImpl/Graphics/Color.cpp +126 -0
  46. data/GosuImpl/Graphics/Common.hpp +21 -0
  47. data/GosuImpl/Graphics/DrawOp.hpp +154 -0
  48. data/GosuImpl/Graphics/Font.cpp +110 -0
  49. data/GosuImpl/Graphics/Graphics.cpp +295 -0
  50. data/GosuImpl/Graphics/Image.cpp +159 -0
  51. data/GosuImpl/Graphics/LargeImageData.cpp +115 -0
  52. data/GosuImpl/Graphics/LargeImageData.hpp +37 -0
  53. data/GosuImpl/Graphics/RotFlip.cpp +184 -0
  54. data/GosuImpl/Graphics/TexChunk.cpp +77 -0
  55. data/GosuImpl/Graphics/TexChunk.hpp +40 -0
  56. data/GosuImpl/Graphics/Text.cpp +223 -0
  57. data/GosuImpl/Graphics/TextMac.cpp +242 -0
  58. data/GosuImpl/Graphics/TextPangoFT.cpp +186 -0
  59. data/GosuImpl/Graphics/TextWin.cpp +172 -0
  60. data/GosuImpl/Graphics/Texture.cpp +104 -0
  61. data/GosuImpl/Graphics/Texture.hpp +34 -0
  62. data/GosuImpl/IO.cpp +48 -0
  63. data/GosuImpl/InputMac.mm +677 -0
  64. data/GosuImpl/InputWin.cpp +444 -0
  65. data/GosuImpl/InputX.cpp +158 -0
  66. data/GosuImpl/MacUtility.hpp +48 -0
  67. data/GosuImpl/Math.cpp +49 -0
  68. data/GosuImpl/RubyGosu.swg +474 -0
  69. data/GosuImpl/RubyGosuStub.mm +17 -0
  70. data/GosuImpl/RubyGosu_DllMain.cxx +30 -0
  71. data/GosuImpl/RubyGosu_wrap.cxx +8521 -0
  72. data/GosuImpl/RubyGosu_wrap.h +31 -0
  73. data/GosuImpl/Sockets/CommSocket.cpp +304 -0
  74. data/GosuImpl/Sockets/ListenerSocket.cpp +60 -0
  75. data/GosuImpl/Sockets/MessageSocket.cpp +136 -0
  76. data/GosuImpl/Sockets/Socket.cpp +145 -0
  77. data/GosuImpl/Sockets/Sockets.hpp +66 -0
  78. data/GosuImpl/TextInputMac.mm +207 -0
  79. data/GosuImpl/TextInputWin.cpp +197 -0
  80. data/GosuImpl/TextInputX.cpp +201 -0
  81. data/GosuImpl/TextTTFWin.cpp +247 -0
  82. data/GosuImpl/TimingUnix.cpp +17 -0
  83. data/GosuImpl/TimingWin.cpp +28 -0
  84. data/GosuImpl/Utility.cpp +140 -0
  85. data/GosuImpl/WinMain.cpp +69 -0
  86. data/GosuImpl/WinUtility.cpp +137 -0
  87. data/GosuImpl/WindowMac.mm +466 -0
  88. data/GosuImpl/WindowWin.cpp +447 -0
  89. data/GosuImpl/WindowX.cpp +392 -0
  90. data/GosuImpl/X11vroot.h +118 -0
  91. data/README.txt +13 -0
  92. data/Rakefile +178 -0
  93. data/examples/ChipmunkIntegration.rb +275 -0
  94. data/examples/CptnRuby.rb +231 -0
  95. data/examples/MoreChipmunkAndRMagick.rb +155 -0
  96. data/examples/OpenGLIntegration.rb +232 -0
  97. data/examples/RMagickIntegration.rb +449 -0
  98. data/examples/TextInput.cpp +170 -0
  99. data/examples/TextInput.rb +139 -0
  100. data/examples/Tutorial.cpp +215 -0
  101. data/examples/Tutorial.rb +137 -0
  102. data/examples/media/Beep.wav +0 -0
  103. data/examples/media/CptnRuby Gem.png +0 -0
  104. data/examples/media/CptnRuby Map.txt +25 -0
  105. data/examples/media/CptnRuby Tileset.png +0 -0
  106. data/examples/media/CptnRuby.png +0 -0
  107. data/examples/media/Cursor.png +0 -0
  108. data/examples/media/Earth.png +0 -0
  109. data/examples/media/Explosion.wav +0 -0
  110. data/examples/media/LargeStar.png +0 -0
  111. data/examples/media/Sky.jpg +0 -0
  112. data/examples/media/Smoke.png +0 -0
  113. data/examples/media/Soldier.png +0 -0
  114. data/examples/media/Space.png +0 -0
  115. data/examples/media/Star.png +0 -0
  116. data/examples/media/Starfighter.bmp +0 -0
  117. data/linux/Makefile.in +98 -0
  118. data/linux/configure +5658 -0
  119. data/linux/configure.ac +126 -0
  120. data/linux/extconf.rb +11 -0
  121. data/mac/English.lproj/InfoPlist.strings +0 -0
  122. data/mac/Gosu-Info.plist +26 -0
  123. data/mac/Gosu.xcodeproj/project.pbxproj +1194 -0
  124. data/mac/RubyGosu Template-Info.plist +26 -0
  125. data/mac/libboost_thread_1_34_1_universal.a +0 -0
  126. data/mac/libboost_thread_d_1_34_1_universal.a +0 -0
  127. data/mac/libfmod_universal.a +0 -0
  128. data/mac/libpng_universal.a +0 -0
  129. data/mac/libz_universal.a +0 -0
  130. data/reference/Async_8hpp-source.html +70 -0
  131. data/reference/Audio_8hpp-source.html +114 -0
  132. data/reference/Audio_8hpp.html +50 -0
  133. data/reference/AutoLink_8hpp-source.html +38 -0
  134. data/reference/AutoLink_8hpp.html +34 -0
  135. data/reference/Bitmap_8hpp-source.html +85 -0
  136. data/reference/Bitmap_8hpp.html +58 -0
  137. data/reference/ButtonsMac_8hpp-source.html +133 -0
  138. data/reference/ButtonsWin_8hpp-source.html +133 -0
  139. data/reference/ButtonsX_8hpp-source.html +134 -0
  140. data/reference/Color_8hpp-source.html +169 -0
  141. data/reference/Color_8hpp.html +85 -0
  142. data/reference/Directories_8hpp-source.html +42 -0
  143. data/reference/Directories_8hpp.html +46 -0
  144. data/reference/Font_8hpp-source.html +65 -0
  145. data/reference/Font_8hpp.html +41 -0
  146. data/reference/Fwd_8hpp-source.html +52 -0
  147. data/reference/Fwd_8hpp.html +37 -0
  148. data/reference/Gosu_8hpp-source.html +48 -0
  149. data/reference/Gosu_8hpp.html +34 -0
  150. data/reference/GraphicsBase_8hpp-source.html +57 -0
  151. data/reference/GraphicsBase_8hpp.html +56 -0
  152. data/reference/Graphics_8hpp-source.html +96 -0
  153. data/reference/Graphics_8hpp.html +53 -0
  154. data/reference/IO_8hpp-source.html +255 -0
  155. data/reference/IO_8hpp.html +74 -0
  156. data/reference/ImageData_8hpp-source.html +62 -0
  157. data/reference/ImageData_8hpp.html +43 -0
  158. data/reference/Image_8hpp-source.html +126 -0
  159. data/reference/Image_8hpp.html +48 -0
  160. data/reference/Input_8hpp-source.html +118 -0
  161. data/reference/Input_8hpp.html +50 -0
  162. data/reference/Math_8hpp-source.html +92 -0
  163. data/reference/Math_8hpp.html +74 -0
  164. data/reference/Platform_8hpp-source.html +83 -0
  165. data/reference/Platform_8hpp.html +73 -0
  166. data/reference/RotFlip_8hpp-source.html +138 -0
  167. data/reference/RotFlip_8hpp.html +77 -0
  168. data/reference/Sockets_8hpp-source.html +130 -0
  169. data/reference/Sockets_8hpp.html +66 -0
  170. data/reference/TextInput_8hpp-source.html +64 -0
  171. data/reference/TextInput_8hpp.html +41 -0
  172. data/reference/Text_8hpp-source.html +51 -0
  173. data/reference/Text_8hpp.html +46 -0
  174. data/reference/Timing_8hpp-source.html +36 -0
  175. data/reference/Timing_8hpp.html +42 -0
  176. data/reference/Utility_8hpp-source.html +44 -0
  177. data/reference/Utility_8hpp.html +48 -0
  178. data/reference/WinUtility_8hpp-source.html +79 -0
  179. data/reference/WinUtility_8hpp.html +64 -0
  180. data/reference/Window_8hpp-source.html +91 -0
  181. data/reference/Window_8hpp.html +41 -0
  182. data/reference/annotated.html +51 -0
  183. data/reference/classGosu_1_1Audio-members.html +34 -0
  184. data/reference/classGosu_1_1Audio.html +46 -0
  185. data/reference/classGosu_1_1Bitmap-members.html +44 -0
  186. data/reference/classGosu_1_1Bitmap.html +263 -0
  187. data/reference/classGosu_1_1Buffer-members.html +44 -0
  188. data/reference/classGosu_1_1Buffer.html +78 -0
  189. data/reference/classGosu_1_1Buffer.png +0 -0
  190. data/reference/classGosu_1_1Button-members.html +36 -0
  191. data/reference/classGosu_1_1Button.html +143 -0
  192. data/reference/classGosu_1_1Color-members.html +56 -0
  193. data/reference/classGosu_1_1Color.html +387 -0
  194. data/reference/classGosu_1_1File-members.html +41 -0
  195. data/reference/classGosu_1_1File.html +69 -0
  196. data/reference/classGosu_1_1File.png +0 -0
  197. data/reference/classGosu_1_1Font-members.html +39 -0
  198. data/reference/classGosu_1_1Font.html +309 -0
  199. data/reference/classGosu_1_1Graphics-members.html +50 -0
  200. data/reference/classGosu_1_1Graphics.html +234 -0
  201. data/reference/classGosu_1_1Image-members.html +45 -0
  202. data/reference/classGosu_1_1Image.html +518 -0
  203. data/reference/classGosu_1_1ImageData-members.html +37 -0
  204. data/reference/classGosu_1_1ImageData.html +60 -0
  205. data/reference/classGosu_1_1Input-members.html +44 -0
  206. data/reference/classGosu_1_1Input.html +223 -0
  207. data/reference/classGosu_1_1MessageSocket-members.html +40 -0
  208. data/reference/classGosu_1_1MessageSocket.html +233 -0
  209. data/reference/classGosu_1_1Resource-members.html +39 -0
  210. data/reference/classGosu_1_1Resource.html +116 -0
  211. data/reference/classGosu_1_1Resource.png +0 -0
  212. data/reference/classGosu_1_1Sample-members.html +37 -0
  213. data/reference/classGosu_1_1Sample.html +200 -0
  214. data/reference/classGosu_1_1SampleInstance-members.html +38 -0
  215. data/reference/classGosu_1_1SampleInstance.html +169 -0
  216. data/reference/classGosu_1_1Song-members.html +43 -0
  217. data/reference/classGosu_1_1Song.html +260 -0
  218. data/reference/classGosu_1_1TextInput-members.html +38 -0
  219. data/reference/classGosu_1_1TextInput.html +121 -0
  220. data/reference/classGosu_1_1Window-members.html +50 -0
  221. data/reference/classGosu_1_1Window.html +271 -0
  222. data/reference/doxyfile +233 -0
  223. data/reference/doxygen.css +433 -0
  224. data/reference/doxygen.png +0 -0
  225. data/reference/files.html +54 -0
  226. data/reference/functions.html +236 -0
  227. data/reference/functions_enum.html +45 -0
  228. data/reference/functions_func.html +227 -0
  229. data/reference/functions_vars.html +47 -0
  230. data/reference/hierarchy.html +53 -0
  231. data/reference/index.html +26 -0
  232. data/reference/namespaceGosu.html +2890 -0
  233. data/reference/namespaceGosu_1_1Colors.html +70 -0
  234. data/reference/namespaceGosu_1_1Win.html +275 -0
  235. data/reference/namespacemembers.html +216 -0
  236. data/reference/namespacemembers_enum.html +52 -0
  237. data/reference/namespacemembers_eval.html +54 -0
  238. data/reference/namespacemembers_func.html +185 -0
  239. data/reference/namespacemembers_type.html +46 -0
  240. data/reference/namespacemembers_vars.html +46 -0
  241. data/reference/namespaces.html +35 -0
  242. data/reference/tab_b.gif +0 -0
  243. data/reference/tab_l.gif +0 -0
  244. data/reference/tab_r.gif +0 -0
  245. data/reference/tabs.css +102 -0
  246. data/windows/Gosu.sln +29 -0
  247. data/windows/Gosu.vcproj +553 -0
  248. data/windows/RubyGosu.vcproj +138 -0
  249. metadata +305 -0
@@ -0,0 +1,186 @@
1
+ #include <Gosu/Text.hpp>
2
+ #include <Gosu/Bitmap.hpp>
3
+ #include <Gosu/Utility.hpp>
4
+
5
+ #include <pango/pango.h>
6
+ #include <pango/pangoft2.h>
7
+
8
+ #include <glib.h>
9
+
10
+ #include <string>
11
+
12
+ std::wstring Gosu::defaultFontName()
13
+ {
14
+ return L"sans";
15
+ }
16
+
17
+ namespace Gosu
18
+ {
19
+ class pango
20
+ {
21
+ int width, height;
22
+
23
+ PangoContext* context;
24
+ PangoLayout* layout;
25
+ /*static*/ PangoFontDescription *font_description;
26
+ PangoAttribute* attr;
27
+ PangoAttrList* attrList;
28
+
29
+ public:
30
+ pango();
31
+ ~pango();
32
+ unsigned textWidth(const std::wstring& text,
33
+ const std::wstring& fontFace, unsigned fontHeight,
34
+ unsigned fontFlags);
35
+ void drawText(Bitmap& bitmap, const std::wstring& text, int x, int y,
36
+ Color c, const std::wstring& fontFace, unsigned fontHeight,
37
+ unsigned fontFlags);
38
+ };
39
+ }
40
+
41
+ Gosu::pango::pango()
42
+ {
43
+ font_description = NULL;
44
+ attr = NULL;
45
+ attrList = NULL;
46
+ }
47
+
48
+ Gosu::pango::~pango()
49
+ {
50
+ g_object_unref(context);
51
+ g_object_unref(layout);
52
+
53
+ if(font_description)
54
+ pango_font_description_free(font_description);
55
+ if(attr)
56
+ pango_attribute_destroy(attr);
57
+ }
58
+
59
+ unsigned Gosu::pango::textWidth(const std::wstring& text,
60
+ const std::wstring& fontFace, unsigned fontHeight,
61
+ unsigned fontFlags)
62
+ {
63
+ g_type_init();
64
+
65
+ int dpi_x = 100, dpi_y = 100;
66
+
67
+ context = pango_ft2_get_context(dpi_x, dpi_y);
68
+
69
+ pango_context_set_language(context, pango_language_from_string ("en_US"));
70
+ PangoDirection init_dir = PANGO_DIRECTION_LTR;
71
+ pango_context_set_base_dir(context, init_dir);
72
+
73
+ // static PangoFontDescription *font_description;
74
+ font_description = pango_font_description_new();
75
+
76
+ pango_font_description_set_family(font_description,
77
+ g_strdup(narrow(fontFace).c_str()));
78
+ pango_font_description_set_style(font_description,
79
+ (fontFlags & ffItalic) ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL);
80
+ pango_font_description_set_variant(font_description, PANGO_VARIANT_NORMAL);
81
+ pango_font_description_set_weight(font_description,
82
+ (fontFlags & ffBold) ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL);
83
+ pango_font_description_set_stretch(font_description, PANGO_STRETCH_NORMAL);
84
+ int init_scale = int(fontHeight/2.0 + 0.5);
85
+ pango_font_description_set_size(font_description, init_scale * PANGO_SCALE);
86
+
87
+ pango_context_set_font_description(context, font_description);
88
+
89
+
90
+ layout = pango_layout_new(context);
91
+
92
+
93
+ if(fontFlags & ffUnderline)
94
+ {
95
+ // PangoAttribute *attr;
96
+ attr = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
97
+ attr->start_index = 0;
98
+ attr->end_index = text.length();
99
+ // PangoAttrList* attrList;
100
+ attrList = pango_attr_list_new();
101
+ pango_attr_list_insert(attrList, attr);
102
+ pango_layout_set_attributes(layout, attrList);
103
+ pango_attr_list_unref(attrList);
104
+ }
105
+
106
+
107
+ // IMPR: Catch errors? (Last NULL-Pointer)
108
+ gchar* utf8Str = g_ucs4_to_utf8((gunichar*)text.c_str(), text.length(), NULL, NULL, NULL);
109
+ pango_layout_set_text(layout, utf8Str, -1);
110
+ g_free(utf8Str);
111
+
112
+ PangoDirection base_dir = pango_context_get_base_dir(context);
113
+ pango_layout_set_alignment(layout,
114
+ base_dir == PANGO_DIRECTION_LTR ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT);
115
+
116
+ pango_layout_set_width(layout, -1);
117
+
118
+ PangoRectangle logical_rect;
119
+
120
+ pango_layout_get_pixel_extents(layout, NULL, &logical_rect);
121
+ height = logical_rect.height;
122
+ width = logical_rect.width;
123
+
124
+ return width;
125
+ }
126
+
127
+ void Gosu::pango::drawText(Bitmap& bitmap, const std::wstring& text, int x, int y,
128
+ Color c, const std::wstring& fontFace, unsigned fontHeight,
129
+ unsigned fontFlags)
130
+ {
131
+ textWidth(text, fontFace, fontHeight, fontFlags);
132
+
133
+ FT_Bitmap ft_bitmap;
134
+
135
+ guchar* buf = new guchar[width * height];
136
+ std::fill(buf, buf + width * height, 0x00);
137
+
138
+ ft_bitmap.rows = height;
139
+ ft_bitmap.width = width;
140
+ ft_bitmap.pitch = ft_bitmap.width;
141
+ ft_bitmap.buffer = buf;
142
+ ft_bitmap.num_grays = 256;
143
+ ft_bitmap.pixel_mode = ft_pixel_mode_grays;
144
+
145
+ int x_start = 0;
146
+ pango_ft2_render_layout(&ft_bitmap, layout, x_start, 0);
147
+
148
+
149
+ int min_height = height;
150
+ if((unsigned)height > fontHeight) min_height = fontHeight;
151
+
152
+ for(int y2 = 0; y2 < min_height; y2++)
153
+ {
154
+ if (y + y2 < 0 || y + y2 >= bitmap.height())
155
+ break;
156
+
157
+ for(int x2 = 0; x2 < width; x2++)
158
+ {
159
+ if (x + x2 < 0 || x + x2 >= bitmap.width())
160
+ break;
161
+ unsigned val = ft_bitmap.buffer[y2*width+x2];
162
+ Color color = c;
163
+ color.setAlpha(val);
164
+ bitmap.setPixel(x2 + x, y2 + y, color);
165
+ }
166
+ }
167
+
168
+ delete[] buf;
169
+ }
170
+
171
+
172
+ unsigned Gosu::textWidth(const std::wstring& text,
173
+ const std::wstring& fontFace, unsigned fontHeight,
174
+ unsigned fontFlags)
175
+ {
176
+ pango helper;
177
+ return helper.textWidth(text, fontFace, fontHeight, fontFlags);
178
+ }
179
+
180
+ void Gosu::drawText(Bitmap& bitmap, const std::wstring& text, int x, int y,
181
+ Color c, const std::wstring& fontFace, unsigned fontHeight,
182
+ unsigned fontFlags)
183
+ {
184
+ pango helper;
185
+ helper.drawText(bitmap, text, x, y, c, fontFace, fontHeight, fontFlags);
186
+ }
@@ -0,0 +1,172 @@
1
+ #define _WIN32_WINNT 0x0500
2
+ #include <windows.h>
3
+
4
+ #include <Gosu/Bitmap.hpp>
5
+ #include <Gosu/Text.hpp>
6
+ #include <Gosu/Utility.hpp>
7
+ #include <Gosu/WinUtility.hpp>
8
+ #include <boost/utility.hpp>
9
+ #include <cstdlib>
10
+ #include <cwchar>
11
+ #include <algorithm>
12
+ #include <stdexcept>
13
+ #include <map>
14
+ #include <set>
15
+
16
+ std::wstring Gosu::defaultFontName()
17
+ {
18
+ return L"Arial";
19
+ }
20
+
21
+ // IMPR: throwLastError/check is used a lot here, Win9x doesn't provide GDI
22
+ // error codes though. Wait until 9x is dead or fix this!
23
+
24
+ namespace Gosu
25
+ {
26
+ std::wstring getNameFromTTFFile(const std::wstring& filename);
27
+
28
+ namespace
29
+ {
30
+ class WinBitmap : boost::noncopyable
31
+ {
32
+ HDC dc;
33
+ HBITMAP bitmap;
34
+ char* pixels;
35
+
36
+ public:
37
+ WinBitmap(unsigned width, unsigned height)
38
+ {
39
+ dc = Win::check(::CreateCompatibleDC(0),
40
+ "creating a device context");
41
+
42
+ BITMAPCOREHEADER coreHeader;
43
+ coreHeader.bcSize = sizeof coreHeader;
44
+ coreHeader.bcWidth = width;
45
+ coreHeader.bcHeight = height;
46
+ coreHeader.bcPlanes = 1;
47
+ coreHeader.bcBitCount = 24;
48
+
49
+ bitmap = ::CreateDIBSection(dc,
50
+ reinterpret_cast<BITMAPINFO*>(&coreHeader), DIB_RGB_COLORS,
51
+ reinterpret_cast<VOID**>(&pixels), 0, 0);
52
+ if (bitmap == 0)
53
+ {
54
+ ::DeleteDC(dc);
55
+ Win::throwLastError("creating a bitmap");
56
+ }
57
+
58
+ ::SelectObject(dc, bitmap);
59
+
60
+ HBRUSH brush = ::CreateSolidBrush(0x000000);
61
+ RECT rc = { 0, 0, width, height };
62
+ ::FillRect(dc, &rc, brush);
63
+ ::DeleteObject(brush);
64
+ }
65
+
66
+ ~WinBitmap()
67
+ {
68
+ ::DeleteObject(bitmap);
69
+ ::DeleteObject(::SelectObject(dc, ::GetStockObject(SYSTEM_FONT)));
70
+ ::DeleteDC(dc);
71
+ }
72
+
73
+ HDC context() const
74
+ {
75
+ return dc;
76
+ }
77
+
78
+ HBITMAP handle() const
79
+ {
80
+ return bitmap;
81
+ }
82
+
83
+ /*char* data() const
84
+ {
85
+ return pixels;
86
+ }*/
87
+
88
+ void selectFont(std::wstring fontName, unsigned fontHeight,
89
+ unsigned fontFlags) const
90
+ {
91
+ static std::map<std::wstring, std::wstring> customFonts;
92
+
93
+ if (fontName.find(L"/") != std::wstring::npos)
94
+ {
95
+ if (customFonts.count(fontName) == 0)
96
+ {
97
+ AddFontResourceEx(fontName.c_str(), FR_PRIVATE, 0);
98
+ fontName = customFonts[fontName] = getNameFromTTFFile(fontName);
99
+ }
100
+ else
101
+ fontName = customFonts[fontName];
102
+ }
103
+
104
+ // IMPR: Maybe remember the last font we had and don't
105
+ // recreate it every time?
106
+
107
+ LOGFONT logfont = { fontHeight, 0, 0, 0,
108
+ fontFlags & ffBold ? FW_BOLD : FW_NORMAL,
109
+ fontFlags & ffItalic ? TRUE : FALSE,
110
+ fontFlags & ffUnderline ? TRUE : FALSE,
111
+ FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
112
+ CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY,
113
+ DEFAULT_PITCH | FF_DONTCARE };
114
+
115
+ // IMPR: This truncates. Is this reasonable? Should we rather
116
+ // throw something?
117
+ /*std::*/wcsncpy(logfont.lfFaceName, fontName.c_str(), LF_FACESIZE);
118
+ logfont.lfFaceName[LF_FACESIZE - 1] = 0;
119
+
120
+ HFONT newFont = Win::check(::CreateFontIndirect(&logfont),
121
+ "creating font object for " + narrow(fontName));
122
+
123
+ HFONT oldFont = reinterpret_cast<HFONT>(::SelectObject(dc, newFont));
124
+ Win::check(oldFont, "selecting the font object for " + narrow(fontName));
125
+ ::DeleteObject(oldFont);
126
+ }
127
+ };
128
+ }
129
+ };
130
+
131
+ unsigned Gosu::textWidth(const std::wstring& text,
132
+ const std::wstring& fontName, unsigned fontHeight, unsigned fontFlags)
133
+ {
134
+ WinBitmap helper(1, 1);
135
+ helper.selectFont(fontName, fontHeight, fontFlags);
136
+
137
+ SIZE size;
138
+ if (!::GetTextExtentPoint32(helper.context(), text.c_str(), text.length(), &size))
139
+ Win::throwLastError("calculating the width of a text");
140
+
141
+ return size.cx;
142
+ }
143
+
144
+ void Gosu::drawText(Bitmap& bitmap, const std::wstring& text, int x, int y,
145
+ Color c, const std::wstring& fontName, unsigned fontHeight,
146
+ unsigned fontFlags)
147
+ {
148
+ unsigned width = textWidth(text, fontName, fontHeight, fontFlags);
149
+
150
+ WinBitmap helper(width, fontHeight);
151
+ helper.selectFont(fontName, fontHeight, fontFlags);
152
+
153
+ if (::SetTextColor(helper.context(), 0xffffff) == CLR_INVALID)
154
+ Win::throwLastError("setting the text color");
155
+
156
+ Win::check(::SetBkMode(helper.context(), TRANSPARENT),
157
+ "setting a bitmap's background mode to TRANSPARENT");
158
+
159
+ ::ExtTextOut(helper.context(), 0, 0, 0, 0, text.c_str(), text.length(), 0);
160
+
161
+ for (unsigned relY = 0; relY < fontHeight; ++relY)
162
+ for (unsigned relX = 0; relX < width; ++relX)
163
+ {
164
+ Color pixel = c;
165
+ pixel.setAlpha(GetPixel(helper.context(), relX, relY) & 0xff);
166
+ // IMPR: Make better use of c.alpha()?
167
+ // Maybe even add an AlphaMode parameter?
168
+ if (pixel != 0 && x + relX >= 0 && x + relX < bitmap.width() &&
169
+ y + relY >= 0 && y + relY < bitmap.height())
170
+ bitmap.setPixel(x + relX, y + relY, pixel);
171
+ }
172
+ }
@@ -0,0 +1,104 @@
1
+ #include <GosuImpl/Graphics/Texture.hpp>
2
+ #include <GosuImpl/Graphics/TexChunk.hpp>
3
+ #include <Gosu/Bitmap.hpp>
4
+ #include <Gosu/Platform.hpp>
5
+ #include <stdexcept>
6
+
7
+ // TODO: Not threadsafe.
8
+ unsigned Gosu::Texture::maxTextureSize()
9
+ {
10
+ const static unsigned MIN_SIZE = 256, MAX_SIZE = 1024;
11
+
12
+ static unsigned size = 0;
13
+ if (size == 0)
14
+ {
15
+ size = MIN_SIZE / 2;
16
+ GLint width = 1;
17
+ do
18
+ {
19
+ size *= 2;
20
+ glTexImage2D(GL_PROXY_TEXTURE_2D, 0, 4, size * 2, size * 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
21
+ glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);
22
+ } while (width != 0 && size < MAX_SIZE);
23
+ }
24
+
25
+ return size;
26
+ }
27
+
28
+ Gosu::Texture::Texture(unsigned size)
29
+ : allocator(size, size), num(0)
30
+ {
31
+ // Create texture name.
32
+ glGenTextures(1, &name);
33
+ if (name == static_cast<GLuint>(-1))
34
+ throw std::runtime_error("Couldn't create OpenGL texture");
35
+
36
+ // Create empty texture.
37
+ glBindTexture(GL_TEXTURE_2D, name);
38
+ glTexImage2D(GL_TEXTURE_2D, 0, 4, allocator.width(), allocator.height(), 0,
39
+ GL_RGBA, GL_UNSIGNED_BYTE, 0);
40
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
41
+ }
42
+
43
+ Gosu::Texture::~Texture()
44
+ {
45
+ glDeleteTextures(1, &name);
46
+ }
47
+
48
+ unsigned Gosu::Texture::size() const
49
+ {
50
+ return allocator.width(); // == height
51
+ }
52
+
53
+ GLuint Gosu::Texture::texName() const
54
+ {
55
+ return name;
56
+ }
57
+
58
+ std::auto_ptr<Gosu::TexChunk>
59
+ Gosu::Texture::tryAlloc(Graphics& graphics,
60
+ DrawOpQueue& queue, boost::shared_ptr<Texture> ptr,
61
+ const Bitmap& bmp, unsigned srcX,
62
+ unsigned srcY, unsigned srcWidth,
63
+ unsigned srcHeight, unsigned padding)
64
+ {
65
+ std::auto_ptr<Gosu::TexChunk> result;
66
+
67
+ boost::optional<BlockAllocator::Block> block = allocator.alloc(srcWidth, srcHeight);
68
+ if (!block)
69
+ return result;
70
+
71
+ result.reset(new TexChunk(graphics, queue, ptr, block->left + padding, block->top + padding,
72
+ block->width - 2 * padding, block->height - 2 * padding, padding));
73
+
74
+ #ifdef __BIG_ENDIAN__
75
+ std::vector<unsigned> pixelData(srcWidth * srcHeight);
76
+ for (unsigned y = 0; y < srcHeight; ++y)
77
+ for (unsigned x = 0; x < srcWidth; ++x)
78
+ {
79
+ boost::uint32_t pixVal = (bmp.getPixel(x, y).argb() & 0x00ffffff) << 8 | bmp.getPixel(x, y).alpha();
80
+ pixVal = bigToNative(pixVal);
81
+ pixelData[y * srcWidth + x] = pixVal;
82
+ }
83
+ const unsigned* texData = &pixelData[0];
84
+ unsigned format = GL_RGBA;
85
+ #else
86
+ #ifndef GL_BGRA
87
+ #define GL_BGRA 0x80E1
88
+ #endif
89
+ const unsigned* texData = bmp.glCompatibleData();
90
+ unsigned format = GL_BGRA;
91
+ #endif
92
+
93
+ glBindTexture(GL_TEXTURE_2D, name);
94
+ glTexSubImage2D(GL_TEXTURE_2D, 0, block->left, block->top, block->width, block->height,
95
+ format, GL_UNSIGNED_BYTE, texData);
96
+ num += 1;
97
+ return result;
98
+ }
99
+
100
+ void Gosu::Texture::free(unsigned x, unsigned y)
101
+ {
102
+ allocator.free(x, y);
103
+ num -= 1;
104
+ }