gosu 0.7.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,276 @@
1
+ #include <Gosu/Bitmap.hpp>
2
+ #include <Gosu/IO.hpp>
3
+ #if 0
4
+ #include <boost/thread.hpp>
5
+ #endif
6
+ #include <cassert>
7
+ #include <stdexcept>
8
+ #include <png.h>
9
+
10
+ namespace Gosu
11
+ {
12
+ namespace
13
+ {
14
+ #if 0
15
+ // Stupid, but everyone would expect loadFromPNG/saveToPNG to be thread-safe.
16
+ boost::mutex pngMutex;
17
+ #endif
18
+
19
+ Reader* tmpReadStream;
20
+ Writer* tmpWriteStream;
21
+
22
+ void readPNGdata(png_structp png_ptr, png_bytep data, png_size_t length)
23
+ {
24
+ tmpReadStream->read(data, length);
25
+ }
26
+ void writePNGdata(png_structp png_ptr, png_bytep data, png_size_t length)
27
+ {
28
+ tmpWriteStream->write(data, length);
29
+ }
30
+ void flushPNGdata(png_structp png_ptr)
31
+ {
32
+ }
33
+ }
34
+ }
35
+
36
+ Gosu::Reader Gosu::loadFromPNG(Bitmap& out, Reader reader)
37
+ {
38
+ #if 0
39
+ boost::mutex::scoped_lock lock(pngMutex);
40
+ #endif
41
+
42
+ Bitmap newBitmap;
43
+
44
+ char header[8];
45
+ reader.read(header, 8);
46
+ if (png_sig_cmp(reinterpret_cast<png_byte*>(&header[0]), 0, 8))
47
+ throw std::runtime_error("Invalid PNG file");
48
+
49
+ png_structp pngPtr;
50
+ // IMPR: Error checking.
51
+ pngPtr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
52
+ if (!pngPtr)
53
+ throw std::runtime_error("Can't create png_structp");
54
+
55
+ png_infop infoPtr;
56
+ infoPtr = png_create_info_struct(pngPtr);
57
+ if (!infoPtr)
58
+ {
59
+ png_destroy_read_struct(&pngPtr, 0, 0);
60
+ throw std::runtime_error("Can't create png_infop");
61
+ }
62
+
63
+ if (setjmp(png_jmpbuf(pngPtr)))
64
+ {
65
+ png_destroy_read_struct(&pngPtr, 0, 0);
66
+ throw std::runtime_error("libpng error");
67
+ }
68
+
69
+ tmpReadStream = &reader; // Highly experimental!
70
+ png_set_read_fn(pngPtr, png_get_io_ptr(pngPtr), readPNGdata);
71
+
72
+ png_set_sig_bytes(pngPtr, 8);
73
+
74
+ png_read_png(pngPtr, infoPtr, PNG_TRANSFORM_PACKING |
75
+ PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_SWAP_ALPHA, 0);
76
+
77
+ // Get general information about the PNG.
78
+ png_bytepp rows = png_get_rows(pngPtr, infoPtr);
79
+ unsigned width = png_get_image_width(pngPtr, infoPtr),
80
+ height = png_get_image_height(pngPtr, infoPtr),
81
+ channels = png_get_channels(pngPtr, infoPtr),
82
+ colorType = png_get_color_type(pngPtr, infoPtr),
83
+ bitDepth = png_get_bit_depth(pngPtr, infoPtr);
84
+
85
+ png_bytep trans;
86
+ int numTrans = 0;
87
+ if (png_get_valid(pngPtr, infoPtr, PNG_INFO_tRNS))
88
+ png_get_tRNS(pngPtr, infoPtr, &trans, &numTrans, NULL);
89
+ // This function only understands palette images with a bit depth <= 8 and
90
+ // non-palette images with a bit depth of 8 (the latter is no problem
91
+ // though, since the transformations given to png_read_png should change
92
+ // all non-palette images to a bit depth of 8).
93
+ if ((colorType == PNG_COLOR_TYPE_PALETTE && bitDepth > 8) ||
94
+ (colorType != PNG_COLOR_TYPE_PALETTE && bitDepth != 8))
95
+ {
96
+ png_destroy_read_struct(&pngPtr, &infoPtr, 0);
97
+ throw std::runtime_error("Unsupported bit depth");
98
+ }
99
+
100
+ // Get palette-related information about the PNG.
101
+ unsigned palSize;
102
+ png_colorp palColors;
103
+ if (colorType == PNG_COLOR_TYPE_PALETTE)
104
+ {
105
+ if (!png_get_valid(pngPtr, infoPtr, PNG_INFO_PLTE))
106
+ {
107
+ png_destroy_read_struct(&pngPtr, &infoPtr, 0);
108
+ throw std::runtime_error("PNG palette missing");
109
+ }
110
+ png_get_PLTE(pngPtr, infoPtr, &palColors,
111
+ reinterpret_cast<int*>(&palSize));
112
+
113
+ // This should be true after the bit-depth check above.
114
+ assert(palSize <= 256);
115
+ }
116
+
117
+ try
118
+ {
119
+ newBitmap.resize(width, height);
120
+ }
121
+ catch (...)
122
+ {
123
+ png_destroy_read_struct(&pngPtr, &infoPtr, 0);
124
+ throw;
125
+ }
126
+
127
+ for (unsigned y = 0; y < height; ++y)
128
+ for (unsigned x = 0; x < width; ++x)
129
+ {
130
+ Color c;
131
+
132
+ switch (colorType)
133
+ {
134
+ case PNG_COLOR_TYPE_GRAY:
135
+ {
136
+ c.setAlpha(255);
137
+ c.setRed (rows[y][x * channels + 0]);
138
+ c.setGreen(rows[y][x * channels + 0]);
139
+ c.setBlue (rows[y][x * channels + 0]);
140
+ break;
141
+ }
142
+ case PNG_COLOR_TYPE_GRAY_ALPHA:
143
+ {
144
+ c.setAlpha(rows[y][x * channels + 0]);
145
+ c.setRed (rows[y][x * channels + 1]);
146
+ c.setGreen(rows[y][x * channels + 1]);
147
+ c.setBlue (rows[y][x * channels + 1]);
148
+ break;
149
+ }
150
+ case PNG_COLOR_TYPE_RGB:
151
+ {
152
+ c.setAlpha(255);
153
+ c.setRed (rows[y][x * channels + 0]);
154
+ c.setGreen(rows[y][x * channels + 1]);
155
+ c.setBlue (rows[y][x * channels + 2]);
156
+ break;
157
+ }
158
+ case PNG_COLOR_TYPE_RGB_ALPHA:
159
+ {
160
+ c.setAlpha(rows[y][x * channels + 0]);
161
+ c.setRed (rows[y][x * channels + 1]);
162
+ c.setGreen(rows[y][x * channels + 2]);
163
+ c.setBlue (rows[y][x * channels + 3]);
164
+ break;
165
+ }
166
+ case PNG_COLOR_TYPE_PALETTE:
167
+ {
168
+ // First get the whole byte in which the palette index is
169
+ // stored (but possibly also adjacent indices).
170
+ png_byte palIndex = rows[y][x * bitDepth / 8];
171
+
172
+ // Shift the interesting index to the right-most bitDepth
173
+ // bits.
174
+ palIndex >>= 8 - (x % (8 / bitDepth) + 1) * bitDepth;
175
+
176
+ // Filter out the other indices.
177
+ palIndex &= (1 << bitDepth) - 1;
178
+
179
+ if (palIndex >= palSize)
180
+ {
181
+ png_destroy_read_struct(&pngPtr, &infoPtr, 0);
182
+ throw std::runtime_error("Palette index out of range");
183
+ }
184
+
185
+ int alpha = 255;
186
+ for (int i = 0; i < numTrans; ++i)
187
+ {
188
+ if (trans[i] == palIndex)
189
+ alpha = 0;
190
+ }
191
+ c.setAlpha(alpha);
192
+ c.setRed (palColors[palIndex].red);
193
+ c.setGreen(palColors[palIndex].green);
194
+ c.setBlue (palColors[palIndex].blue);
195
+ break;
196
+ }
197
+ default:
198
+ {
199
+ png_destroy_read_struct(&pngPtr, &infoPtr, 0);
200
+ throw std::runtime_error("Unsupported PNG color type");
201
+ }
202
+ }
203
+
204
+ // IMPR: Assumes non-throwing setPixel, which is not yet officially
205
+ // guaranteed.
206
+ newBitmap.setPixel(x, y, c);
207
+ }
208
+
209
+ // At last!
210
+ png_destroy_read_struct(&pngPtr, &infoPtr, 0);
211
+
212
+ newBitmap.swap(out);
213
+
214
+ return reader; // TODO: Does this point to the right location?
215
+ }
216
+
217
+ Gosu::Writer Gosu::saveToPNG(const Bitmap& bmp, Writer writer)
218
+ {
219
+ #if 0
220
+ boost::mutex::scoped_lock lock(pngMutex);
221
+ #endif
222
+
223
+ png_structp pngPtr = png_create_write_struct(
224
+ PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
225
+ if (!pngPtr)
226
+ throw std::runtime_error("Can't create png_structp");
227
+
228
+ png_infop infoPtr = png_create_info_struct(pngPtr);
229
+ if (!infoPtr)
230
+ {
231
+ png_destroy_write_struct(&pngPtr, (png_infopp)NULL);
232
+ throw std::runtime_error("Can't create png_infop");
233
+ }
234
+
235
+ if (setjmp(png_jmpbuf(pngPtr))) // error handling
236
+ {
237
+ png_destroy_write_struct(&pngPtr, &infoPtr);
238
+ throw std::runtime_error("setjmp() failed");
239
+ }
240
+
241
+ tmpWriteStream = &writer; // No more experimental.
242
+ png_set_write_fn(pngPtr, png_get_io_ptr(pngPtr), writePNGdata, flushPNGdata);
243
+
244
+ png_set_IHDR(pngPtr, infoPtr, bmp.width(), bmp.height(), 8, PNG_COLOR_TYPE_RGB,
245
+ PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
246
+
247
+ png_write_info(pngPtr, infoPtr);
248
+
249
+ png_bytepp rows;
250
+ rows = new png_bytep[bmp.height()];
251
+ for(unsigned int y = 0; y < bmp.height(); y++)
252
+ {
253
+ rows[y] = new png_byte[bmp.width() * 3];
254
+ for(unsigned int x = 0; x < bmp.width()*3; x += 3)
255
+ {
256
+ rows[y][x] = bmp.getPixel(x/3, y).red();
257
+ rows[y][x+1] = bmp.getPixel(x/3, y).green();
258
+ rows[y][x+2] = bmp.getPixel(x/3, y).blue();
259
+ }
260
+ }
261
+
262
+ png_set_rows(pngPtr, infoPtr, rows);
263
+ png_write_png(pngPtr, infoPtr, PNG_TRANSFORM_IDENTITY, NULL);
264
+
265
+ for(unsigned int i = 0; i < bmp.height(); i++)
266
+ delete[] rows[i];
267
+ delete[] rows;
268
+
269
+ /* png_write_image(pngPtr, row_pointers); // row_pointers == ?
270
+ png_write_end(pngPtr, NULL);
271
+ png_destroy_write_struct(&pngPtr, &infoPtr);*/
272
+ // TODO: Hey, why is destroy_write_struct commented out?
273
+
274
+ return writer; // TODO: Does this point to the right location?
275
+ }
276
+
@@ -0,0 +1,67 @@
1
+ #include <Gosu/Graphics.hpp>
2
+ #include <Gosu/Bitmap.hpp>
3
+ #include <Gosu/IO.hpp>
4
+
5
+ Gosu::Bitmap Gosu::quickLoadBitmap(const std::wstring& filename)
6
+ {
7
+ Buffer buf;
8
+ loadFile(buf, filename);
9
+ Bitmap bmp;
10
+
11
+ char formatTester[2];
12
+ buf.frontReader().read(formatTester, sizeof formatTester);
13
+ if (formatTester[0] == 'B' && formatTester[1] == 'M')
14
+ {
15
+ loadFromBMP(bmp, buf.frontReader());
16
+ applyColorKey(bmp, Colors::fuchsia);
17
+ }
18
+ else
19
+ loadFromPNG(bmp, buf.frontReader());
20
+ return bmp;
21
+ }
22
+
23
+ void Gosu::applyBorderFlags(Bitmap& dest, const Bitmap& source,
24
+ unsigned srcX, unsigned srcY, unsigned srcWidth, unsigned srcHeight,
25
+ unsigned borderFlags)
26
+ {
27
+ dest.resize(srcWidth + 2, srcHeight + 2);
28
+ dest.fill(Colors::none);
29
+
30
+ // The borders are made "harder" by duplicating the original bitmap's
31
+ // borders.
32
+
33
+ // Top.
34
+ if (borderFlags & bfHardTop)
35
+ dest.insert(source, 1, 0, srcX, srcY, srcWidth, 1);
36
+ // Bottom.
37
+ if (borderFlags & bfHardBottom)
38
+ dest.insert(source, 1, dest.height() - 1,
39
+ srcX, srcY + srcHeight - 1, srcWidth, 1);
40
+ // Left.
41
+ if (borderFlags & bfHardLeft)
42
+ dest.insert(source, 0, 1, srcX, srcY, 1, srcHeight);
43
+ // Right.
44
+ if (borderFlags & bfHardRight)
45
+ dest.insert(source, dest.width() - 1, 1,
46
+ srcX + srcWidth - 1, srcY, 1, srcHeight);
47
+
48
+ // Top left.
49
+ if ((borderFlags & bfHardTop) && (borderFlags & bfHardLeft))
50
+ dest.setPixel(0, 0,
51
+ source.getPixel(srcX, srcY));
52
+ // Top right.
53
+ if ((borderFlags & bfHardTop) && (borderFlags & bfHardRight))
54
+ dest.setPixel(dest.width() - 1, 0,
55
+ source.getPixel(srcX + srcWidth - 1, srcY));
56
+ // Bottom left.
57
+ if ((borderFlags & bfHardBottom) && (borderFlags & bfHardLeft))
58
+ dest.setPixel(0, dest.height() - 1,
59
+ source.getPixel(srcX, srcY + srcHeight - 1));
60
+ // Bottom right.
61
+ if ((borderFlags & bfHardBottom) && (borderFlags & bfHardRight))
62
+ dest.setPixel(dest.width() - 1, dest.height() - 1,
63
+ source.getPixel(srcX + srcWidth - 1, srcY + srcHeight - 1));
64
+
65
+ // Now put the final image into the prepared borders.
66
+ dest.insert(source, 1, 1, srcX, srcY, srcWidth, srcHeight);
67
+ }
@@ -0,0 +1,127 @@
1
+ #include <GosuImpl/Graphics/BlockAllocator.hpp>
2
+ #include <stdexcept>
3
+ #include <vector>
4
+
5
+ struct Gosu::BlockAllocator::Impl
6
+ {
7
+ unsigned width, height;
8
+
9
+ typedef std::vector<Block> Blocks;
10
+ Blocks blocks;
11
+ unsigned firstX, firstY;
12
+ unsigned maxW, maxH;
13
+
14
+ bool isBlockFree(const Block& block) const
15
+ {
16
+ // (The right-th column and the bottom-th row are outside of the block.)
17
+ unsigned right = block.left + block.width;
18
+ unsigned bottom = block.top + block.height;
19
+
20
+ // Block isn't valid.
21
+ if (right > width || bottom > height)
22
+ return false;
23
+
24
+ // Test if the block collides with any existing rects.
25
+ Blocks::const_iterator i, end = blocks.end();
26
+ for (i = blocks.begin(); i != end; ++i)
27
+ if (i->left < right && block.left < i->left + i->width &&
28
+ i->top < bottom && block.top < i->top + i->height)
29
+ {
30
+ return false;
31
+ }
32
+
33
+ return true;
34
+ }
35
+ };
36
+
37
+ Gosu::BlockAllocator::BlockAllocator(unsigned width, unsigned height)
38
+ : pimpl(new Impl)
39
+ {
40
+ pimpl->width = width;
41
+ pimpl->height = height;
42
+
43
+ pimpl->firstX = 0;
44
+ pimpl->firstY = 0;
45
+
46
+ pimpl->maxW = width;
47
+ pimpl->maxH = height;
48
+ }
49
+
50
+ Gosu::BlockAllocator::~BlockAllocator()
51
+ {
52
+ }
53
+
54
+ unsigned Gosu::BlockAllocator::width() const
55
+ {
56
+ return pimpl->width;
57
+ }
58
+
59
+ unsigned Gosu::BlockAllocator::height() const
60
+ {
61
+ return pimpl->height;
62
+ }
63
+
64
+ boost::optional<Gosu::BlockAllocator::Block>
65
+ Gosu::BlockAllocator::alloc(unsigned aWidth, unsigned aHeight)
66
+ {
67
+ // The rect wouldn't even fit onto the texture!
68
+ if (aWidth > width() || aHeight > height())
69
+ return boost::optional<Block>();
70
+
71
+ // It could theoretically fit, but there's no space left.
72
+ if (aWidth > pimpl->maxW && aHeight > pimpl->maxH)
73
+ return boost::optional<Block>();
74
+
75
+ // Start to look for a place next to the last returned rect. Chances are
76
+ // good we'll find a place there.
77
+ Block b = Block(pimpl->firstX, pimpl->firstY, aWidth, aHeight);
78
+ if (pimpl->isBlockFree(b))
79
+ {
80
+ pimpl->blocks.push_back(b);
81
+ return b;
82
+ }
83
+
84
+ // Brute force: Look for a free place on this texture.
85
+ unsigned& x = b.left;
86
+ unsigned& y = b.top;
87
+ for (y = 0; y <= height() - aHeight; y += 16)
88
+ for (x = 0; x <= width() - aWidth; x += 8)
89
+ {
90
+ if (!pimpl->isBlockFree(b))
91
+ continue;
92
+
93
+ // Found a nice place!
94
+
95
+ // Try to make up for the large for()-stepping.
96
+ while (y > 0 && pimpl->isBlockFree(Block(x, y - 1, aWidth, aHeight)))
97
+ --y;
98
+ while (x > 0 && pimpl->isBlockFree(Block(x - 1, y, aWidth, aHeight)))
99
+ --x;
100
+
101
+ pimpl->blocks.push_back(b);
102
+ return b;
103
+ }
104
+
105
+ // So there was no space for the bitmap. Remember this for later.
106
+ pimpl->maxW = aWidth - 1;
107
+ pimpl->maxH = aHeight - 1;
108
+ return boost::optional<Block>();
109
+ }
110
+
111
+ void Gosu::BlockAllocator::free(unsigned left, unsigned top)
112
+ {
113
+ for (Impl::Blocks::iterator i = pimpl->blocks.begin();
114
+ i != pimpl->blocks.end(); ++i)
115
+ {
116
+ if (i->left == left && i->top == top)
117
+ {
118
+ pimpl->blocks.erase(i);
119
+ // Be optimistic again!
120
+ pimpl->maxW = pimpl->width - 1;
121
+ pimpl->maxH = pimpl->height - 1;
122
+ return;
123
+ }
124
+ }
125
+
126
+ throw std::logic_error("Tried to free an invalid block");
127
+ }