gosu 1.4.5 → 1.5.0

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 (332) hide show
  1. checksums.yaml +4 -4
  2. data/COPYING +2 -1
  3. data/README.md +0 -8
  4. data/dependencies/SDL/include/SDL3/SDL.h +91 -0
  5. data/dependencies/SDL/include/SDL3/SDL_assert.h +710 -0
  6. data/dependencies/SDL/include/SDL3/SDL_asyncio.h +550 -0
  7. data/dependencies/SDL/include/SDL3/SDL_atomic.h +692 -0
  8. data/dependencies/SDL/include/SDL3/SDL_audio.h +2382 -0
  9. data/dependencies/SDL/include/SDL3/SDL_begin_code.h +553 -0
  10. data/dependencies/SDL/include/{SDL_bits.h → SDL3/SDL_bits.h} +47 -27
  11. data/dependencies/SDL/include/{SDL_blendmode.h → SDL3/SDL_blendmode.h} +59 -55
  12. data/dependencies/SDL/include/SDL3/SDL_camera.h +535 -0
  13. data/dependencies/SDL/include/SDL3/SDL_clipboard.h +331 -0
  14. data/dependencies/SDL/include/{close_code.h → SDL3/SDL_close_code.h} +9 -8
  15. data/dependencies/SDL/include/{SDL_copying.h → SDL3/SDL_copying.h} +3 -1
  16. data/dependencies/SDL/include/SDL3/SDL_cpuinfo.h +374 -0
  17. data/dependencies/SDL/include/SDL3/SDL_dialog.h +343 -0
  18. data/dependencies/SDL/include/SDL3/SDL_dlopennote.h +234 -0
  19. data/dependencies/SDL/include/{SDL_egl.h → SDL3/SDL_egl.h} +11 -8
  20. data/dependencies/SDL/include/SDL3/SDL_endian.h +645 -0
  21. data/dependencies/SDL/include/SDL3/SDL_error.h +226 -0
  22. data/dependencies/SDL/include/SDL3/SDL_events.h +1651 -0
  23. data/dependencies/SDL/include/SDL3/SDL_filesystem.h +537 -0
  24. data/dependencies/SDL/include/SDL3/SDL_gamepad.h +1657 -0
  25. data/dependencies/SDL/include/SDL3/SDL_gpu.h +4637 -0
  26. data/dependencies/SDL/include/SDL3/SDL_guid.h +106 -0
  27. data/dependencies/SDL/include/SDL3/SDL_haptic.h +1461 -0
  28. data/dependencies/SDL/include/SDL3/SDL_hidapi.h +571 -0
  29. data/dependencies/SDL/include/SDL3/SDL_hints.h +5041 -0
  30. data/dependencies/SDL/include/SDL3/SDL_init.h +508 -0
  31. data/dependencies/SDL/include/SDL3/SDL_intrin.h +410 -0
  32. data/dependencies/SDL/include/SDL3/SDL_iostream.h +1379 -0
  33. data/dependencies/SDL/include/SDL3/SDL_joystick.h +1385 -0
  34. data/dependencies/SDL/include/SDL3/SDL_keyboard.h +615 -0
  35. data/dependencies/SDL/include/SDL3/SDL_keycode.h +348 -0
  36. data/dependencies/SDL/include/SDL3/SDL_loadso.h +145 -0
  37. data/dependencies/SDL/include/{SDL_locale.h → SDL3/SDL_locale.h} +40 -24
  38. data/dependencies/SDL/include/SDL3/SDL_log.h +541 -0
  39. data/dependencies/SDL/include/SDL3/SDL_main.h +704 -0
  40. data/dependencies/SDL/include/SDL3/SDL_main_impl.h +149 -0
  41. data/dependencies/SDL/include/{SDL_messagebox.h → SDL3/SDL_messagebox.h} +85 -48
  42. data/dependencies/SDL/include/{SDL_metal.h → SDL3/SDL_metal.h} +29 -29
  43. data/dependencies/SDL/include/{SDL_misc.h → SDL3/SDL_misc.h} +13 -12
  44. data/dependencies/SDL/include/SDL3/SDL_mouse.h +813 -0
  45. data/dependencies/SDL/include/SDL3/SDL_mutex.h +1117 -0
  46. data/dependencies/SDL/include/SDL3/SDL_oldnames.h +1339 -0
  47. data/dependencies/SDL/include/{SDL_opengl.h → SDL3/SDL_opengl.h} +1247 -278
  48. data/dependencies/SDL/include/{SDL_opengl_glext.h → SDL3/SDL_opengl_glext.h} +5 -1
  49. data/dependencies/SDL/include/{SDL_opengles.h → SDL3/SDL_opengles.h} +6 -7
  50. data/dependencies/SDL/include/{SDL_opengles2.h → SDL3/SDL_opengles2.h} +10 -11
  51. data/dependencies/SDL/include/SDL3/SDL_pen.h +198 -0
  52. data/dependencies/SDL/include/SDL3/SDL_pixels.h +1441 -0
  53. data/dependencies/SDL/include/{SDL_test_log.h → SDL3/SDL_platform.h} +25 -26
  54. data/dependencies/SDL/include/SDL3/SDL_platform_defines.h +497 -0
  55. data/dependencies/SDL/include/{SDL_power.h → SDL3/SDL_power.h} +40 -20
  56. data/dependencies/SDL/include/SDL3/SDL_process.h +441 -0
  57. data/dependencies/SDL/include/SDL3/SDL_properties.h +572 -0
  58. data/dependencies/SDL/include/SDL3/SDL_rect.h +528 -0
  59. data/dependencies/SDL/include/SDL3/SDL_render.h +3034 -0
  60. data/dependencies/SDL/include/SDL3/SDL_revision.h +56 -0
  61. data/dependencies/SDL/include/{SDL_scancode.h → SDL3/SDL_scancode.h} +60 -68
  62. data/dependencies/SDL/include/SDL3/SDL_sensor.h +321 -0
  63. data/dependencies/SDL/include/SDL3/SDL_stdinc.h +6186 -0
  64. data/dependencies/SDL/include/SDL3/SDL_storage.h +686 -0
  65. data/dependencies/SDL/include/SDL3/SDL_surface.h +1769 -0
  66. data/dependencies/SDL/include/SDL3/SDL_system.h +840 -0
  67. data/dependencies/SDL/include/{SDL_test.h → SDL3/SDL_test.h} +16 -22
  68. data/dependencies/SDL/include/{SDL_test_assert.h → SDL3/SDL_test_assert.h} +27 -34
  69. data/dependencies/SDL/include/{SDL_test_common.h → SDL3/SDL_test_common.h} +109 -52
  70. data/dependencies/SDL/include/{SDL_test_compare.h → SDL3/SDL_test_compare.h} +22 -14
  71. data/dependencies/SDL/include/{SDL_test_crc32.h → SDL3/SDL_test_crc32.h} +27 -30
  72. data/dependencies/SDL/include/{SDL_test_font.h → SDL3/SDL_test_font.h} +50 -49
  73. data/dependencies/SDL/include/{SDL_test_fuzzer.h → SDL3/SDL_test_fuzzer.h} +64 -79
  74. data/dependencies/SDL/include/{SDL_test_harness.h → SDL3/SDL_test_harness.h} +47 -30
  75. data/dependencies/SDL/include/SDL3/SDL_test_log.h +83 -0
  76. data/dependencies/SDL/include/{SDL_test_md5.h → SDL3/SDL_test_md5.h} +21 -28
  77. data/dependencies/SDL/include/{SDL_test_memory.h → SDL3/SDL_test_memory.h} +19 -16
  78. data/dependencies/SDL/include/SDL3/SDL_thread.h +602 -0
  79. data/dependencies/SDL/include/SDL3/SDL_time.h +249 -0
  80. data/dependencies/SDL/include/SDL3/SDL_timer.h +454 -0
  81. data/dependencies/SDL/include/SDL3/SDL_touch.h +184 -0
  82. data/dependencies/SDL/include/SDL3/SDL_tray.h +544 -0
  83. data/dependencies/SDL/include/SDL3/SDL_version.h +198 -0
  84. data/dependencies/SDL/include/SDL3/SDL_video.h +3497 -0
  85. data/dependencies/SDL/include/SDL3/SDL_vulkan.h +287 -0
  86. data/dependencies/SDL/lib/x64/libSDL3.dll.a +0 -0
  87. data/dependencies/SDL/lib/x86/libSDL3.dll.a +0 -0
  88. data/dependencies/SDL_sound/SDL3_sound/SDL_sound.h +755 -0
  89. data/dependencies/SDL_sound/SDL_sound.c +136 -215
  90. data/dependencies/SDL_sound/SDL_sound_aiff.c +55 -54
  91. data/dependencies/SDL_sound/SDL_sound_au.c +32 -32
  92. data/dependencies/SDL_sound/SDL_sound_coreaudio.c +24 -41
  93. data/dependencies/SDL_sound/SDL_sound_flac.c +32 -12
  94. data/dependencies/SDL_sound/SDL_sound_internal.h +34 -49
  95. data/dependencies/SDL_sound/SDL_sound_modplug.c +21 -29
  96. data/dependencies/SDL_sound/SDL_sound_mp3.c +35 -13
  97. data/dependencies/SDL_sound/SDL_sound_raw.c +14 -15
  98. data/dependencies/SDL_sound/SDL_sound_shn.c +82 -82
  99. data/dependencies/SDL_sound/SDL_sound_voc.c +30 -30
  100. data/dependencies/SDL_sound/SDL_sound_vorbis.c +34 -25
  101. data/dependencies/SDL_sound/SDL_sound_wav.c +90 -81
  102. data/dependencies/SDL_sound/dr_flac.h +1890 -1301
  103. data/dependencies/SDL_sound/dr_mp3.h +1098 -660
  104. data/dependencies/SDL_sound/libmodplug/fastmix.c +1 -1
  105. data/dependencies/SDL_sound/libmodplug/libmodplug.h +5 -4
  106. data/dependencies/SDL_sound/libmodplug/load_ams.c +6 -3
  107. data/dependencies/SDL_sound/libmodplug/load_dsm.c +2 -1
  108. data/dependencies/SDL_sound/libmodplug/load_mt2.c +2 -2
  109. data/dependencies/SDL_sound/libmodplug/modplug.h +2 -1
  110. data/dependencies/SDL_sound/libmodplug/snd_fx.c +3 -3
  111. data/dependencies/SDL_sound/libmodplug/sndfile.c +3 -0
  112. data/dependencies/SDL_sound/libmodplug/sndmix.c +7 -3
  113. data/dependencies/SDL_sound/stb_vorbis.h +397 -342
  114. data/dependencies/mojoAL/AL/alc.h +2 -2
  115. data/dependencies/mojoAL/mojoal.c +2126 -1880
  116. data/dependencies/stb/stb_image.h +1261 -1360
  117. data/dependencies/stb/stb_image_write.h +329 -129
  118. data/dependencies/stb/stb_truetype.h +304 -60
  119. data/dependencies/utf8proc/utf8proc.c +133 -75
  120. data/dependencies/utf8proc/utf8proc.h +131 -50
  121. data/dependencies/utf8proc/utf8proc_data.h +16213 -15658
  122. data/ext/gosu-ffi/extconf.rb +103 -0
  123. data/ext/gosu-ffi/gosu-ffi.def +467 -0
  124. data/ffi/Gosu.cpp +330 -0
  125. data/ffi/Gosu.h +84 -0
  126. data/ffi/Gosu_Channel.cpp +62 -0
  127. data/ffi/Gosu_Channel.h +17 -0
  128. data/ffi/Gosu_Color.cpp +176 -0
  129. data/ffi/Gosu_Color.h +31 -0
  130. data/ffi/Gosu_Constants.cpp +334 -0
  131. data/ffi/Gosu_FFI.h +34 -0
  132. data/ffi/Gosu_FFI_internal.h +160 -0
  133. data/ffi/Gosu_Font.cpp +97 -0
  134. data/ffi/Gosu_Font.h +32 -0
  135. data/ffi/Gosu_Image.cpp +239 -0
  136. data/ffi/Gosu_Image.h +71 -0
  137. data/ffi/Gosu_Sample.cpp +29 -0
  138. data/ffi/Gosu_Sample.h +14 -0
  139. data/ffi/Gosu_Song.cpp +69 -0
  140. data/ffi/Gosu_Song.h +18 -0
  141. data/ffi/Gosu_TextInput.cpp +94 -0
  142. data/ffi/Gosu_TextInput.h +25 -0
  143. data/ffi/Gosu_Window.cpp +314 -0
  144. data/ffi/Gosu_Window.h +78 -0
  145. data/include/Gosu/Audio.hpp +6 -11
  146. data/include/Gosu/Bitmap.hpp +38 -53
  147. data/include/Gosu/Buffer.hpp +54 -0
  148. data/include/Gosu/Color.hpp +29 -36
  149. data/include/Gosu/Directories.hpp +25 -28
  150. data/include/Gosu/Drawable.hpp +58 -0
  151. data/include/Gosu/Font.hpp +6 -5
  152. data/include/Gosu/Fwd.hpp +4 -6
  153. data/include/Gosu/Gosu.hpp +5 -5
  154. data/include/Gosu/Graphics.hpp +53 -61
  155. data/include/Gosu/GraphicsBase.hpp +1 -11
  156. data/include/Gosu/Image.hpp +12 -15
  157. data/include/Gosu/Math.hpp +50 -72
  158. data/include/Gosu/Transform.hpp +32 -0
  159. data/include/Gosu/Utility.hpp +77 -1
  160. data/include/Gosu/Version.hpp +2 -2
  161. data/include/Gosu/Window.hpp +15 -9
  162. data/lib/SDL3.dll +0 -0
  163. data/lib/gosu/channel.rb +50 -0
  164. data/lib/gosu/color.rb +173 -0
  165. data/lib/gosu/compat.rb +130 -17
  166. data/lib/gosu/constants.rb +386 -0
  167. data/lib/gosu/ffi.rb +269 -0
  168. data/lib/gosu/font.rb +62 -0
  169. data/lib/gosu/gl_tex_info.rb +33 -0
  170. data/lib/gosu/gosu.rb +250 -0
  171. data/lib/gosu/image.rb +179 -0
  172. data/lib/gosu/numeric.rb +17 -0
  173. data/lib/gosu/preview.rb +6 -6
  174. data/lib/gosu/sample.rb +25 -0
  175. data/lib/gosu/song.rb +57 -0
  176. data/lib/gosu/text_input.rb +83 -0
  177. data/lib/gosu/window.rb +249 -0
  178. data/lib/gosu.rb +29 -8
  179. data/lib64/SDL3.dll +0 -0
  180. data/rdoc/gosu.rb +2 -2
  181. data/src/Audio.cpp +30 -24
  182. data/src/AudioFile.hpp +6 -5
  183. data/src/AudioFileAudioToolbox.cpp +8 -8
  184. data/src/AudioFileSDLSound.cpp +13 -14
  185. data/src/AudioImpl.hpp +2 -2
  186. data/src/BinPacker.cpp +188 -0
  187. data/src/BinPacker.hpp +55 -0
  188. data/src/Bitmap.cpp +172 -143
  189. data/src/BitmapIO.cpp +60 -86
  190. data/src/Buffer.cpp +150 -0
  191. data/src/ClipRectStack.cpp +38 -0
  192. data/src/ClipRectStack.hpp +17 -83
  193. data/src/Color.cpp +75 -80
  194. data/src/Directories.cpp +47 -0
  195. data/src/DirectoriesUIKit.cpp +50 -0
  196. data/src/DrawOp.hpp +20 -18
  197. data/src/DrawOpQueue.hpp +27 -33
  198. data/src/Drawable.cpp +95 -0
  199. data/src/EmptyDrawable.hpp +38 -0
  200. data/src/FPS.cpp +31 -0
  201. data/src/Font.cpp +104 -74
  202. data/src/GosuGLView.cpp +13 -11
  203. data/src/GosuViewController.cpp +2 -10
  204. data/src/Graphics.cpp +86 -145
  205. data/src/GraphicsImpl.hpp +14 -68
  206. data/src/Image.cpp +41 -35
  207. data/src/Input.cpp +89 -90
  208. data/src/Macro.cpp +46 -29
  209. data/src/Macro.hpp +23 -17
  210. data/src/MarkupParser.cpp +5 -5
  211. data/src/Math.cpp +48 -22
  212. data/src/OffScreenTarget.cpp +47 -50
  213. data/src/OffScreenTarget.hpp +13 -11
  214. data/src/OpenGLContext.cpp +116 -0
  215. data/src/OpenGLContext.hpp +41 -0
  216. data/src/RenderState.hpp +49 -71
  217. data/src/Resolution.cpp +65 -53
  218. data/src/TexChunk.cpp +35 -80
  219. data/src/TexChunk.hpp +44 -35
  220. data/src/Text.cpp +1 -1
  221. data/src/TextBuilder.cpp +35 -21
  222. data/src/TextBuilder.hpp +6 -9
  223. data/src/TextInput.cpp +9 -9
  224. data/src/Texture.cpp +62 -80
  225. data/src/Texture.hpp +25 -23
  226. data/src/TiledDrawable.cpp +149 -0
  227. data/src/TiledDrawable.hpp +47 -0
  228. data/src/TimingApple.cpp +1 -1
  229. data/src/Transform.cpp +45 -50
  230. data/src/TransformStack.hpp +16 -16
  231. data/src/TrueTypeFont.cpp +59 -51
  232. data/src/TrueTypeFont.hpp +6 -7
  233. data/src/TrueTypeFontApple.cpp +28 -19
  234. data/src/TrueTypeFontUnix.cpp +39 -32
  235. data/src/TrueTypeFontWin.cpp +30 -30
  236. data/src/Utility.cpp +113 -37
  237. data/src/UtilityWin.cpp +49 -0
  238. data/src/Window.cpp +121 -151
  239. data/src/WindowUIKit.cpp +14 -14
  240. metadata +165 -128
  241. data/dependencies/SDL/include/SDL.h +0 -233
  242. data/dependencies/SDL/include/SDL_assert.h +0 -326
  243. data/dependencies/SDL/include/SDL_atomic.h +0 -415
  244. data/dependencies/SDL/include/SDL_audio.h +0 -1500
  245. data/dependencies/SDL/include/SDL_clipboard.h +0 -141
  246. data/dependencies/SDL/include/SDL_config.h +0 -331
  247. data/dependencies/SDL/include/SDL_config_android.h +0 -157
  248. data/dependencies/SDL/include/SDL_config_iphoneos.h +0 -166
  249. data/dependencies/SDL/include/SDL_config_macosx.h +0 -197
  250. data/dependencies/SDL/include/SDL_config_minimal.h +0 -82
  251. data/dependencies/SDL/include/SDL_config_pandora.h +0 -128
  252. data/dependencies/SDL/include/SDL_config_psp.h +0 -144
  253. data/dependencies/SDL/include/SDL_config_windows.h +0 -225
  254. data/dependencies/SDL/include/SDL_config_winrt.h +0 -215
  255. data/dependencies/SDL/include/SDL_config_wiz.h +0 -121
  256. data/dependencies/SDL/include/SDL_cpuinfo.h +0 -594
  257. data/dependencies/SDL/include/SDL_endian.h +0 -348
  258. data/dependencies/SDL/include/SDL_error.h +0 -163
  259. data/dependencies/SDL/include/SDL_events.h +0 -1166
  260. data/dependencies/SDL/include/SDL_filesystem.h +0 -149
  261. data/dependencies/SDL/include/SDL_gamecontroller.h +0 -1074
  262. data/dependencies/SDL/include/SDL_gesture.h +0 -117
  263. data/dependencies/SDL/include/SDL_guid.h +0 -100
  264. data/dependencies/SDL/include/SDL_haptic.h +0 -1341
  265. data/dependencies/SDL/include/SDL_hidapi.h +0 -451
  266. data/dependencies/SDL/include/SDL_hints.h +0 -2569
  267. data/dependencies/SDL/include/SDL_joystick.h +0 -1066
  268. data/dependencies/SDL/include/SDL_keyboard.h +0 -353
  269. data/dependencies/SDL/include/SDL_keycode.h +0 -358
  270. data/dependencies/SDL/include/SDL_loadso.h +0 -115
  271. data/dependencies/SDL/include/SDL_log.h +0 -404
  272. data/dependencies/SDL/include/SDL_main.h +0 -275
  273. data/dependencies/SDL/include/SDL_mouse.h +0 -465
  274. data/dependencies/SDL/include/SDL_mutex.h +0 -471
  275. data/dependencies/SDL/include/SDL_name.h +0 -33
  276. data/dependencies/SDL/include/SDL_pixels.h +0 -644
  277. data/dependencies/SDL/include/SDL_platform.h +0 -261
  278. data/dependencies/SDL/include/SDL_quit.h +0 -58
  279. data/dependencies/SDL/include/SDL_rect.h +0 -376
  280. data/dependencies/SDL/include/SDL_render.h +0 -1919
  281. data/dependencies/SDL/include/SDL_revision.h +0 -7
  282. data/dependencies/SDL/include/SDL_rwops.h +0 -841
  283. data/dependencies/SDL/include/SDL_sensor.h +0 -322
  284. data/dependencies/SDL/include/SDL_shape.h +0 -155
  285. data/dependencies/SDL/include/SDL_stdinc.h +0 -830
  286. data/dependencies/SDL/include/SDL_surface.h +0 -997
  287. data/dependencies/SDL/include/SDL_system.h +0 -623
  288. data/dependencies/SDL/include/SDL_syswm.h +0 -386
  289. data/dependencies/SDL/include/SDL_test_images.h +0 -78
  290. data/dependencies/SDL/include/SDL_test_random.h +0 -115
  291. data/dependencies/SDL/include/SDL_thread.h +0 -464
  292. data/dependencies/SDL/include/SDL_timer.h +0 -222
  293. data/dependencies/SDL/include/SDL_touch.h +0 -150
  294. data/dependencies/SDL/include/SDL_types.h +0 -29
  295. data/dependencies/SDL/include/SDL_version.h +0 -204
  296. data/dependencies/SDL/include/SDL_video.h +0 -2150
  297. data/dependencies/SDL/include/SDL_vulkan.h +0 -215
  298. data/dependencies/SDL/include/begin_code.h +0 -187
  299. data/dependencies/SDL/lib/x64/libSDL2.dll.a +0 -0
  300. data/dependencies/SDL/lib/x86/libSDL2.dll.a +0 -0
  301. data/dependencies/SDL_sound/SDL_sound.h +0 -725
  302. data/ext/gosu/extconf.rb +0 -119
  303. data/include/Gosu/IO.hpp +0 -254
  304. data/include/Gosu/ImageData.hpp +0 -53
  305. data/include/Gosu/Inspection.hpp +0 -7
  306. data/lib/SDL2.dll +0 -0
  307. data/lib/gosu/patches.rb +0 -66
  308. data/lib/gosu/run.rb +0 -20
  309. data/lib/gosu/swig_patches.rb +0 -110
  310. data/lib64/SDL2.dll +0 -0
  311. data/src/BlockAllocator.cpp +0 -131
  312. data/src/BlockAllocator.hpp +0 -32
  313. data/src/DirectoriesApple.cpp +0 -69
  314. data/src/DirectoriesUnix.cpp +0 -46
  315. data/src/DirectoriesWin.cpp +0 -65
  316. data/src/EmptyImageData.hpp +0 -52
  317. data/src/FileUnix.cpp +0 -99
  318. data/src/FileWin.cpp +0 -88
  319. data/src/IO.cpp +0 -60
  320. data/src/Iconv.hpp +0 -51
  321. data/src/Inspection.cpp +0 -27
  322. data/src/LargeImageData.cpp +0 -215
  323. data/src/LargeImageData.hpp +0 -39
  324. data/src/Log.hpp +0 -19
  325. data/src/RubyGosu.cxx +0 -13047
  326. data/src/RubyGosu.h +0 -51
  327. data/src/WinUtility.cpp +0 -61
  328. data/src/WinUtility.hpp +0 -27
  329. /data/dependencies/SDL/include/{SDL_opengles2_gl2.h → SDL3/SDL_opengles2_gl2.h} +0 -0
  330. /data/dependencies/SDL/include/{SDL_opengles2_gl2ext.h → SDL3/SDL_opengles2_gl2ext.h} +0 -0
  331. /data/dependencies/SDL/include/{SDL_opengles2_gl2platform.h → SDL3/SDL_opengles2_gl2platform.h} +0 -0
  332. /data/dependencies/SDL/include/{SDL_opengles2_khrplatform.h → SDL3/SDL_opengles2_khrplatform.h} +0 -0
data/src/Macro.cpp CHANGED
@@ -2,6 +2,7 @@
2
2
  #include <Gosu/Image.hpp>
3
3
  #include <Gosu/Utility.hpp>
4
4
  #include "DrawOpQueue.hpp"
5
+ #include <optional>
5
6
  #include <stdexcept>
6
7
 
7
8
  struct Gosu::Macro::Impl : private Gosu::Noncopyable
@@ -17,14 +18,16 @@ struct Gosu::Macro::Impl : private Gosu::Noncopyable
17
18
  double b1, double b2, double& x1, double& x2)
18
19
  {
19
20
  const double det = a11 * a22 - a21 * a12;
20
- if (det == 0) return false;
21
+ if (det == 0) {
22
+ return false;
23
+ }
21
24
  x1 = (a22 * b1 - a12 * b2) / det;
22
25
  x2 = (a11 * b2 - a21 * b1) / det;
23
26
  return true;
24
27
  }
25
28
 
26
- Transform find_transform_for_target(double x1, double y1, double x2, double y2, //
27
- double x3, double y3, double x4, double y4) const
29
+ std::optional<Transform> find_transform(double x1, double y1, double x2, double y2, //
30
+ double x3, double y3, double x4, double y4) const
28
31
  {
29
32
  // Transformation logic follows a discussion on the ImageMagick mailing
30
33
  // list (on which ImageMagick's perspective_transform.pl is based).
@@ -64,7 +67,9 @@ struct Gosu::Macro::Impl : private Gosu::Noncopyable
64
67
 
65
68
  // Solve:
66
69
  double qx, qy;
67
- if (!solve_2x2(a11, a12, a21, a22, b1, b2, qx, qy)) return Transform{{0}};
70
+ if (!solve_2x2(a11, a12, a21, a22, b1, b2, qx, qy)) {
71
+ return std::nullopt;
72
+ }
68
73
 
69
74
  // Updating the last two rows with the computed solution yields
70
75
 
@@ -88,7 +93,7 @@ struct Gosu::Macro::Impl : private Gosu::Noncopyable
88
93
  // 0, 0, 0, 0, 0, 0, 1, 0 | qx
89
94
  // 0, 0, 0, 0, 0, 0, 0, 1 | qy
90
95
 
91
- // Normalize and reorder rows so we can read off the solution:
96
+ // Normalize and reorder rows, so we can read off the solution:
92
97
 
93
98
  // 1, 0, 0, 0, 0, 0, 0, 0 | (x2-x1) / w + qx x2
94
99
  // 0, 1, 0, 0, 0, 0, 0, 0 | (x3-x1) / h + qy x3
@@ -109,10 +114,10 @@ struct Gosu::Macro::Impl : private Gosu::Noncopyable
109
114
  c[6] = qx;
110
115
  c[7] = qy;
111
116
 
112
- Transform result = {{c[0], c[3], 0, c[6], //
113
- c[1], c[4], 0, c[7], //
114
- 0, 0, 1, 0, //
115
- c[2], c[5], 0, 1}};
117
+ Transform result = { { c[0], c[3], 0, c[6], //
118
+ c[1], c[4], 0, c[7], //
119
+ 0, 0, 1, 0, //
120
+ c[2], c[5], 0, 1 } };
116
121
  return result;
117
122
  }
118
123
 
@@ -124,29 +129,35 @@ struct Gosu::Macro::Impl : private Gosu::Noncopyable
124
129
  // transformation only has to be performed once.
125
130
 
126
131
  #ifndef GOSU_IS_OPENGLES
132
+ const std::optional<Transform> transform
133
+ = find_transform(x1, y1, x2, y2, x3, y3, x4, y4);
134
+ if (!transform.has_value()) {
135
+ return;
136
+ }
137
+
127
138
  glEnable(GL_BLEND);
128
139
  glMatrixMode(GL_MODELVIEW);
129
140
 
130
- Transform transform = find_transform_for_target(x1, y1, x2, y2, x3, y3, x4, y4);
131
-
132
141
  for (const auto& vertex_array : vertex_arrays) {
133
142
  glPushMatrix();
143
+ const auto pop_matrix = finally([] { glPopMatrix(); });
144
+
134
145
  vertex_array.render_state.apply();
135
- glMultMatrixd(&transform[0]);
136
- glInterleavedArrays(GL_T2F_C4UB_V3F, 0, &vertex_array.vertices[0]);
137
- glDrawArrays(GL_QUADS, 0, (GLsizei) vertex_array.vertices.size());
138
- glPopMatrix();
146
+ glMultMatrixd(transform->matrix.data());
147
+ glInterleavedArrays(GL_T2F_C4UB_V3F, 0, vertex_array.vertices.data());
148
+ glDrawArrays(GL_QUADS, 0, (GLsizei)vertex_array.vertices.size());
139
149
  }
140
150
  #endif
141
151
  }
142
152
  };
143
153
 
144
154
  Gosu::Macro::Macro(DrawOpQueue& queue, int width, int height)
145
- : pimpl(new Impl)
146
155
  {
147
- pimpl->width = width;
148
- pimpl->height = height;
149
- queue.compile_to(pimpl->vertex_arrays);
156
+ auto impl = std::make_shared<Impl>();
157
+ impl->width = width;
158
+ impl->height = height;
159
+ queue.compile_to(impl->vertex_arrays);
160
+ pimpl = std::move(impl);
150
161
  }
151
162
 
152
163
  int Gosu::Macro::width() const
@@ -161,15 +172,17 @@ int Gosu::Macro::height() const
161
172
 
162
173
  void Gosu::Macro::draw(double x1, double y1, Color c1, double x2, double y2, Color c2, //
163
174
  double x3, double y3, Color c3, double x4, double y4, Color c4, //
164
- ZPos z, BlendMode mode) const
175
+ ZPos z, BlendMode) const
165
176
  {
166
177
  if (c1 != Color::WHITE || c2 != Color::WHITE || c3 != Color::WHITE || c4 != Color::WHITE) {
167
- throw std::invalid_argument{"Macros cannot be tinted with colors"};
178
+ throw std::invalid_argument("Macros cannot be tinted with colors");
168
179
  }
169
180
 
170
181
  normalize_coordinates(x1, y1, x2, y2, x3, y3, c3, x4, y4, c4);
171
182
 
172
- Gosu::Graphics::gl(z, [=] { pimpl->draw_vertex_arrays(x1, y1, x2, y2, x3, y3, x4, y4); });
183
+ // Capture pimpl, not this: The GL block only runs when the queue is flushed, and this Macro
184
+ // may have been destroyed by then.
185
+ gl(z, [=, impl = pimpl] { impl->draw_vertex_arrays(x1, y1, x2, y2, x3, y3, x4, y4); });
173
186
  }
174
187
 
175
188
  const Gosu::GLTexInfo* Gosu::Macro::gl_tex_info() const
@@ -179,20 +192,24 @@ const Gosu::GLTexInfo* Gosu::Macro::gl_tex_info() const
179
192
 
180
193
  Gosu::Bitmap Gosu::Macro::to_bitmap() const
181
194
  {
195
+ Viewport viewport(pimpl->width, pimpl->height);
196
+ const auto current = viewport.make_current();
197
+
182
198
  const auto render_this = [this] {
183
- draw(0, 0, Color::WHITE, pimpl->width, 0, Color::WHITE, 0, pimpl->height, Color::WHITE,
184
- pimpl->width, pimpl->height, Color::WHITE, 0, BM_DEFAULT);
199
+ draw(0, 0, Color::WHITE, pimpl->width, 0, Color::WHITE, //
200
+ 0, pimpl->height, Color::WHITE, pimpl->width, pimpl->height, Color::WHITE, //
201
+ 0, BM_DEFAULT);
185
202
  };
186
203
 
187
- return Gosu::Graphics::render(pimpl->width, pimpl->height, render_this).data().to_bitmap();
204
+ return render(pimpl->width, pimpl->height, render_this).drawable().to_bitmap();
188
205
  }
189
206
 
190
- std::unique_ptr<Gosu::ImageData> Gosu::Macro::subimage(int x, int y, int width, int height) const
207
+ std::unique_ptr<Gosu::Drawable> Gosu::Macro::subimage(const Rect&) const
191
208
  {
192
- return std::unique_ptr<ImageData>{};
209
+ return nullptr;
193
210
  }
194
211
 
195
- void Gosu::Macro::insert(const Bitmap& bitmap, int x, int y)
212
+ void Gosu::Macro::insert(const Bitmap&, int, int)
196
213
  {
197
- throw std::logic_error{"Gosu::Macro cannot be updated with a Gosu::Bitmap yet"};
214
+ throw std::logic_error("Gosu::Macro cannot be updated with a Gosu::Bitmap yet");
198
215
  }
data/src/Macro.hpp CHANGED
@@ -1,30 +1,36 @@
1
1
  #pragma once
2
2
 
3
3
  #include <Gosu/Fwd.hpp>
4
- #include <Gosu/ImageData.hpp>
5
- #include "GraphicsImpl.hpp"
4
+ #include <Gosu/Drawable.hpp>
6
5
  #include <memory>
7
6
 
8
- class Gosu::Macro : public Gosu::ImageData
7
+ namespace Gosu
9
8
  {
10
- struct Impl;
11
- std::shared_ptr<Impl> pimpl;
9
+ class DrawOpQueue;
12
10
 
13
- public:
14
- Macro(DrawOpQueue& queue, int width, int height);
11
+ class Macro : public Drawable
12
+ {
13
+ // This is a shared_ptr so that enqueued DrawOps can keep the macro data alive even after
14
+ // the Macro object itself has been destroyed.
15
+ struct Impl;
16
+ std::shared_ptr<const Impl> pimpl;
15
17
 
16
- int width() const override;
17
- int height() const override;
18
+ public:
19
+ Macro(DrawOpQueue& queue, int width, int height);
18
20
 
19
- void draw(double x1, double y1, Color c1, double x2, double y2, Color c2, //
20
- double x3, double y3, Color c3, double x4, double y4, Color c4, //
21
- ZPos z, BlendMode mode) const override;
21
+ int width() const override;
22
+ int height() const override;
22
23
 
23
- const Gosu::GLTexInfo* gl_tex_info() const override;
24
+ void draw(double x1, double y1, Color c1, double x2, double y2, Color c2, //
25
+ double x3, double y3, Color c3, double x4, double y4, Color c4, //
26
+ ZPos z, BlendMode mode) const override;
24
27
 
25
- Gosu::Bitmap to_bitmap() const override;
28
+ const GLTexInfo* gl_tex_info() const override;
26
29
 
27
- std::unique_ptr<ImageData> subimage(int x, int y, int width, int height) const override;
30
+ Bitmap to_bitmap() const override;
28
31
 
29
- void insert(const Bitmap& bitmap, int x, int y) override;
30
- };
32
+ std::unique_ptr<Drawable> subimage(const Rect&) const override;
33
+
34
+ void insert(const Bitmap&, int x, int y) override;
35
+ };
36
+ }
data/src/MarkupParser.cpp CHANGED
@@ -157,29 +157,29 @@ void Gosu::MarkupParser::add_current_substring()
157
157
  void Gosu::MarkupParser::add_composed_substring(u32string&& substring)
158
158
  {
159
159
  FormattedString fstr;
160
- fstr.text = substring;
160
+ fstr.text = std::move(substring);
161
161
  fstr.flags = flags();
162
162
  fstr.color = c.back();
163
163
 
164
164
  if (!substrings.empty() && substrings.back().can_be_merged_with(fstr)) {
165
- substrings.back().text.append(move(fstr.text));
165
+ substrings.back().text.append(fstr.text);
166
166
  }
167
167
  else {
168
- substrings.emplace_back(move(fstr));
168
+ substrings.emplace_back(std::move(fstr));
169
169
  }
170
170
  }
171
171
 
172
172
  void Gosu::MarkupParser::flush_to_consumer()
173
173
  {
174
174
  if (!substrings.empty()) {
175
- consumer(move(substrings));
175
+ consumer(std::move(substrings));
176
176
  substrings.clear();
177
177
  }
178
178
  }
179
179
 
180
180
  Gosu::MarkupParser::MarkupParser(unsigned base_flags, bool split_words,
181
181
  function<void (vector<FormattedString>)> consumer)
182
- : consumer(move(consumer))
182
+ : consumer(std::move(consumer))
183
183
  {
184
184
  word_state = (split_words ? ADDING_WORD : IGNORE_WORDS);
185
185
 
data/src/Math.cpp CHANGED
@@ -1,38 +1,57 @@
1
1
  #include <Gosu/Math.hpp>
2
- #include <cstdlib>
3
- using namespace std;
2
+ #include <algorithm>
3
+ #include <cmath>
4
+ #include <limits> // for std::numeric_limits::quiet_NaN
5
+ #include <random>
6
+ #include <stdexcept>
4
7
 
5
8
  double Gosu::random(double min, double max)
6
9
  {
7
- double rnd = rand();
8
- return rnd / (static_cast<double>(RAND_MAX) + 1) * (max - min) + min;
10
+ if (!std::isfinite(min) || !std::isfinite(max)) {
11
+ return std::numeric_limits<double>::quiet_NaN();
12
+ }
13
+ if (min >= max) {
14
+ throw std::invalid_argument("Gosu::random: min >= max");
15
+ }
16
+
17
+ thread_local std::mt19937_64 generator(std::random_device { }());
18
+ std::uniform_real_distribution<double> distribution(min, max);
19
+ const double result = distribution(generator);
20
+
21
+ // Make sure that we do not return "max" even when uniform_real_distribution is buggy.
22
+ // See "Notes": https://en.cppreference.com/w/cpp/numeric/random/uniform_real_distribution
23
+ return result == max ? min : result;
9
24
  }
10
25
 
11
26
  double Gosu::offset_x(double angle, double radius)
12
27
  {
13
- return +sin(angle / 180 * M_PI) * radius;
28
+ return +std::sin(degrees_to_radians(angle)) * radius;
14
29
  }
15
30
 
16
31
  double Gosu::offset_y(double angle, double radius)
17
32
  {
18
- return -cos(angle / 180 * M_PI) * radius;
33
+ return -std::cos(degrees_to_radians(angle)) * radius;
19
34
  }
20
35
 
21
- double Gosu::angle(double from_x, double from_y, double to_x, double to_y, double def)
36
+ double Gosu::angle(double from_x, double from_y, double to_x, double to_y, double fallback)
22
37
  {
23
38
  double dist_x = to_x - from_x;
24
39
  double dist_y = to_y - from_y;
25
40
 
41
+ // Special-case the four cardinal directions values so that we don't run into subtle rounding
42
+ // errors for these obvious values.
26
43
  if (dist_x == 0) {
27
- // Special-case these values so that for a result of 0°/360° we never
28
- // run into subtle precision errors by converting from radian to angles.
29
- if (dist_y < 0) return 0;
30
- if (dist_y > 0) return 180;
31
- // dist_y == 0
32
- return 0;
44
+ if (dist_y == 0) {
45
+ return fallback;
46
+ }
47
+
48
+ return dist_y < 0 ? 0 : 180;
49
+ }
50
+ else if (dist_y == 0) {
51
+ return dist_x < 0 ? 270 : 90;
33
52
  }
34
53
 
35
- return normalize_angle(radians_to_gosu(atan2(dist_y, dist_x)));
54
+ return normalize_angle(radians_to_angle(std::atan2(dist_y, dist_x)));
36
55
  }
37
56
 
38
57
  double Gosu::angle_diff(double from, double to)
@@ -47,23 +66,30 @@ double Gosu::normalize_angle(double angle)
47
66
 
48
67
  int Gosu::wrap(int value, int min, int max)
49
68
  {
50
- int result = (value - min) % (max - min);
51
- return result < 0 ? result + max : result + min;
52
- }
69
+ if (min >= max) {
70
+ throw std::invalid_argument("Gosu::wrap: min >= max");
71
+ }
53
72
 
54
- float Gosu::wrap(float value, float min, float max)
55
- {
56
- float result = fmodf(value - min, max - min);
73
+ int result = (value - min) % (max - min);
57
74
  return result < 0 ? result + max : result + min;
58
75
  }
59
76
 
60
77
  double Gosu::wrap(double value, double min, double max)
61
78
  {
62
- double result = fmod(value - min, max - min);
79
+ if (!std::isfinite(value) || !std::isfinite(min) || !std::isfinite(max)) {
80
+ return std::numeric_limits<double>::quiet_NaN();
81
+ }
82
+ if (min >= max) {
83
+ throw std::invalid_argument("Gosu::wrap: min >= max");
84
+ }
85
+
86
+ double result = std::fmod(value - min, max - min);
63
87
  return result < 0 ? result + max : result + min;
64
88
  }
65
89
 
66
90
  double Gosu::distance(double x1, double y1, double x2, double y2)
67
91
  {
68
- return sqrt(distance_sqr(x1, y1, x2, y2));
92
+ double dist_x = (x2 - x1);
93
+ double dist_y = (y2 - y1);
94
+ return std::sqrt(dist_x * dist_x + dist_y * dist_y);
69
95
  }
@@ -1,99 +1,96 @@
1
1
  #include "OffScreenTarget.hpp"
2
- #include "Texture.hpp"
3
2
  #include <Gosu/Image.hpp>
4
3
  #include <Gosu/Platform.hpp>
4
+ #include "OpenGLContext.hpp"
5
+ #include "Texture.hpp"
5
6
  #ifndef GOSU_IS_IPHONE
6
- #include <SDL.h>
7
+ #include <SDL3/SDL.h> // for SDL_GL_GetProcAddress
7
8
  #endif
8
- using namespace std;
9
9
 
10
10
  #ifdef GOSU_IS_OPENGLES
11
- #define GOSU_LOAD_GL_EXT(fn, type) \
12
- static auto fn = fn ## OES;
13
-
14
- #define GOSU_GL_CONST(name) \
15
- name ## _OES
16
-
17
- #define GOSU_GL_DEPTH_COMPONENT \
18
- GL_DEPTH_COMPONENT16_OES
11
+ #define GOSU_LOAD_GL_EXT(fn, type) static auto fn = fn##OES
12
+ #define GOSU_GL_CONST(name) name##_OES
13
+ #define GOSU_GL_DEPTH_COMPONENT GL_DEPTH_COMPONENT16_OES
19
14
  #else
20
- #define GOSU_LOAD_GL_EXT(fn, type) \
21
- static auto fn = (type) SDL_GL_GetProcAddress(#fn); \
22
- if (!fn) throw runtime_error("Unable to load " #fn);
23
-
24
- #define GOSU_GL_CONST(name) \
25
- name
26
-
27
- #define GOSU_GL_DEPTH_COMPONENT \
28
- GL_DEPTH_COMPONENT
15
+ #define GOSU_LOAD_GL_EXT(fn, type) \
16
+ static auto fn = reinterpret_cast<type>(SDL_GL_GetProcAddress(#fn)); \
17
+ if ((fn) == nullptr) { \
18
+ throw std::runtime_error("Unable to load " #fn); \
19
+ }
20
+ #define GOSU_GL_CONST(name) name
21
+ #define GOSU_GL_DEPTH_COMPONENT GL_DEPTH_COMPONENT
29
22
  #endif
30
23
 
31
24
  Gosu::OffScreenTarget::OffScreenTarget(int width, int height, unsigned image_flags)
25
+ : r_renderbuffer(static_cast<GLuint>(-1)),
26
+ m_framebuffer(static_cast<GLuint>(-1))
32
27
  {
33
28
  #ifndef GOSU_IS_IPHONE
29
+ // GCOV_EXCL_START
34
30
  if (!SDL_GL_ExtensionSupported("GL_EXT_framebuffer_object")) {
35
- throw runtime_error("Missing GL_EXT_framebuffer_object extension");
31
+ throw std::runtime_error("Missing GL_EXT_framebuffer_object extension");
36
32
  }
33
+ // GCOV_EXCL_end
37
34
  #endif
38
-
35
+
39
36
  // Create a new texture that will be our rendering target.
40
- texture = make_shared<Texture>(width, height, image_flags & IF_RETRO);
41
- // Mark the full texture as blocked for our TexChunk.
42
- texture->block(0, 0, width, height);
43
-
37
+ m_texture = std::make_shared<Texture>(width, height, image_flags & IF_RETRO);
38
+
44
39
  // Besides the texture, also create a renderbuffer for depth information.
45
- // Gosu doesn't use this, but custom OpenGL code could might.
40
+ // Gosu doesn't use this, but custom OpenGL code might.
46
41
  GOSU_LOAD_GL_EXT(glGenRenderbuffers, PFNGLGENRENDERBUFFERSPROC);
47
- glGenRenderbuffers(1, &renderbuffer);
48
-
42
+ glGenRenderbuffers(1, &r_renderbuffer);
43
+
49
44
  GOSU_LOAD_GL_EXT(glBindRenderbuffer, PFNGLBINDRENDERBUFFERPROC);
50
- glBindRenderbuffer(GOSU_GL_CONST(GL_RENDERBUFFER), renderbuffer);
51
-
45
+ glBindRenderbuffer(GOSU_GL_CONST(GL_RENDERBUFFER), r_renderbuffer);
46
+
52
47
  GOSU_LOAD_GL_EXT(glRenderbufferStorage, PFNGLRENDERBUFFERSTORAGEPROC);
53
48
  glRenderbufferStorage(GOSU_GL_CONST(GL_RENDERBUFFER), GOSU_GL_DEPTH_COMPONENT, width, height);
54
49
  glBindRenderbuffer(GOSU_GL_CONST(GL_RENDERBUFFER), 0);
55
-
50
+
56
51
  // Now tie everything together.
57
52
  GOSU_LOAD_GL_EXT(glGenFramebuffers, PFNGLGENFRAMEBUFFERSPROC);
58
- glGenFramebuffers(1, &framebuffer);
59
-
53
+ glGenFramebuffers(1, &m_framebuffer);
54
+
60
55
  GOSU_LOAD_GL_EXT(glBindFramebuffer, PFNGLBINDFRAMEBUFFERPROC);
61
- glBindFramebuffer(GOSU_GL_CONST(GL_FRAMEBUFFER), framebuffer);
62
-
56
+ glBindFramebuffer(GOSU_GL_CONST(GL_FRAMEBUFFER), m_framebuffer);
57
+
63
58
  GOSU_LOAD_GL_EXT(glFramebufferTexture2D, PFNGLFRAMEBUFFERTEXTURE2DPROC);
64
59
  glFramebufferTexture2D(GOSU_GL_CONST(GL_FRAMEBUFFER), GOSU_GL_CONST(GL_COLOR_ATTACHMENT0),
65
- GL_TEXTURE_2D, texture->tex_name(), 0);
66
-
60
+ GL_TEXTURE_2D, m_texture->tex_name(), 0);
61
+
67
62
  GOSU_LOAD_GL_EXT(glFramebufferRenderbuffer, PFNGLFRAMEBUFFERRENDERBUFFERPROC);
68
63
  glFramebufferRenderbuffer(GOSU_GL_CONST(GL_FRAMEBUFFER), GOSU_GL_CONST(GL_DEPTH_ATTACHMENT),
69
- GOSU_GL_CONST(GL_RENDERBUFFER), renderbuffer);
64
+ GOSU_GL_CONST(GL_RENDERBUFFER), r_renderbuffer);
70
65
  }
71
66
 
72
67
  Gosu::OffScreenTarget::~OffScreenTarget()
73
68
  {
74
69
  try {
75
70
  GOSU_LOAD_GL_EXT(glDeleteRenderbuffers, PFNGLDELETERENDERBUFFERSPROC);
76
- glDeleteRenderbuffers(1, &renderbuffer);
77
-
71
+ glDeleteRenderbuffers(1, &r_renderbuffer);
72
+
78
73
  GOSU_LOAD_GL_EXT(glDeleteFramebuffers, PFNGLDELETEFRAMEBUFFERSPROC);
79
- glDeleteFramebuffers(1, &framebuffer);
74
+ glDeleteFramebuffers(1, &m_framebuffer);
75
+ // GCOV_EXCL_START
80
76
  } catch (...) {
81
77
  // If we can't load these functions, just accept the resource leak.
82
78
  }
79
+ // GCOV_EXCL_END
83
80
  }
84
81
 
85
82
  Gosu::Image Gosu::OffScreenTarget::render(const std::function<void ()>& f)
86
83
  {
87
84
  GOSU_LOAD_GL_EXT(glBindFramebuffer, PFNGLBINDFRAMEBUFFERPROC);
88
- glBindFramebuffer(GOSU_GL_CONST(GL_FRAMEBUFFER), framebuffer);
89
-
85
+ glBindFramebuffer(GOSU_GL_CONST(GL_FRAMEBUFFER), m_framebuffer);
86
+
90
87
  GOSU_LOAD_GL_EXT(glCheckFramebufferStatus, PFNGLCHECKFRAMEBUFFERSTATUSPROC);
91
88
  GLenum status = glCheckFramebufferStatus(GOSU_GL_CONST(GL_FRAMEBUFFER));
92
- if (status != GOSU_GL_CONST(GL_FRAMEBUFFER_COMPLETE)) throw runtime_error("Incomplete framebuffer");
93
-
94
- f();
95
- glBindFramebuffer(GOSU_GL_CONST(GL_FRAMEBUFFER), 0);
89
+ if (status != GOSU_GL_CONST(GL_FRAMEBUFFER_COMPLETE)) {
90
+ throw std::runtime_error("Incomplete framebuffer");
91
+ }
96
92
 
97
- unique_ptr<ImageData> tex_chunk(new TexChunk(texture, 0, 0, texture->width(), texture->height(), 0));
98
- return Image(move(tex_chunk));
93
+ auto reset_framebuffer = finally([] { glBindFramebuffer(GOSU_GL_CONST(GL_FRAMEBUFFER), 0); });
94
+ f();
95
+ return Image(std::make_unique<TexChunk>(m_texture, Rect::covering(*m_texture), nullptr));
99
96
  }
@@ -1,23 +1,25 @@
1
1
  #pragma once
2
2
 
3
- #include "GraphicsImpl.hpp"
3
+ #include <Gosu/Utility.hpp>
4
+ #include <cstdint>
5
+ #include <functional>
6
+ #include <memory>
4
7
 
5
8
  namespace Gosu
6
9
  {
7
- class OffScreenTarget
10
+ class Image;
11
+ class Texture;
12
+
13
+ class OffScreenTarget : private Noncopyable
8
14
  {
9
- std::shared_ptr<Texture> texture;
10
- GLuint renderbuffer;
11
- GLuint framebuffer;
12
-
13
- OffScreenTarget(const OffScreenTarget& other) = delete;
14
- OffScreenTarget& operator=(const OffScreenTarget& other) = delete;
15
- OffScreenTarget(OffScreenTarget&& other) = delete;
16
- OffScreenTarget& operator=(OffScreenTarget&& other) = delete;
17
-
15
+ std::shared_ptr<Texture> m_texture;
16
+ std::uint32_t r_renderbuffer;
17
+ std::uint32_t m_framebuffer;
18
+
18
19
  public:
19
20
  OffScreenTarget(int width, int height, unsigned image_flags);
20
21
  ~OffScreenTarget();
22
+
21
23
  Gosu::Image render(const std::function<void ()>& f);
22
24
  };
23
25
  }
@@ -0,0 +1,116 @@
1
+ #include "OpenGLContext.hpp"
2
+ #if !defined(GOSU_IS_IPHONE) // on iPhone, GosuGLView.cpp implements OpenGLContext.
3
+
4
+ #if defined(GOSU_IS_MAC)
5
+ #include <Foundation/Foundation.h> // for -[NSThread isMainThread]
6
+ #endif
7
+
8
+ #include <stdexcept>
9
+ #include <thread>
10
+
11
+ namespace
12
+ {
13
+ [[noreturn]] void throw_sdl_error(const std::string& operation)
14
+ {
15
+ throw std::runtime_error(operation + ": " + SDL_GetError());
16
+ }
17
+
18
+ /// Groups the global SDL variables into a single struct so that they all either exist, or not.
19
+ struct SDLWindowAndGLContext : Gosu::Noncopyable
20
+ {
21
+ SDL_Window* window;
22
+ /// We only use one OpenGL context, even from background threads. It would be possible to
23
+ /// set up shared OpenGL contexts here (c.f. git history of this file), but according to
24
+ /// various sources on the internet, sharing OpenGL contexts does not help performance.
25
+ /// https://discourse.libsdl.org/t/feasibility-correctness-of-calling-gl-in-another-thread/20292/6
26
+ SDL_GLContext gl_context;
27
+
28
+ SDLWindowAndGLContext()
29
+ {
30
+ static const int initialized = SDL_Init(SDL_INIT_VIDEO);
31
+ if (initialized < 0) {
32
+ throw_sdl_error("Could not initialize SDL");
33
+ }
34
+
35
+ #ifdef GOSU_IS_MAC
36
+ if (![NSThread isMainThread]) {
37
+ throw std::logic_error("Gosu::Window/OpenGL must be initialized on main thread");
38
+ }
39
+ #endif
40
+ #ifdef GOSU_IS_OPENGLES
41
+ // We want to use OpenGL ES 1.1, which does not yet require us to use shaders.
42
+ SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
43
+ SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
44
+ #endif
45
+
46
+ window = //
47
+ SDL_CreateWindow("", 64, 64,
48
+ SDL_WINDOW_OPENGL | SDL_WINDOW_HIDDEN
49
+ | SDL_WINDOW_HIGH_PIXEL_DENSITY);
50
+ if (!window) {
51
+ throw_sdl_error("Could not create SDL window");
52
+ }
53
+
54
+ gl_context = SDL_GL_CreateContext(window);
55
+ if (!gl_context) {
56
+ SDL_DestroyWindow(window);
57
+ throw_sdl_error("Could not create main OpenGL context");
58
+ }
59
+ }
60
+
61
+ static SDLWindowAndGLContext& instance()
62
+ {
63
+ static SDLWindowAndGLContext instance;
64
+ return instance;
65
+ }
66
+ };
67
+
68
+ /// The current nesting level of OpenGLContext objects.
69
+ thread_local int current_nesting_depth = 0; // NOLINT(*-avoid-non-const-global-variables)
70
+ }
71
+
72
+ Gosu::OpenGLContext::OpenGLContext(bool for_rendering_to_window)
73
+ {
74
+ static const auto& window = SDLWindowAndGLContext::instance();
75
+
76
+ // All threads use a single mutex to coordinate access to their OpenGL context.
77
+ static std::mutex mutex;
78
+ if (current_nesting_depth == 0) {
79
+ // ...but only the innermost OpenGLContext in a thread owns the lock.
80
+ m_lock = std::unique_lock(mutex);
81
+ }
82
+
83
+ // Explicitly make the context current every time for_rendering_to_window is set, so that we set
84
+ // the current SDL_Window even if we are somehow nested into something else.
85
+ if (for_rendering_to_window) {
86
+ if (!SDL_GL_MakeCurrent(window.window, window.gl_context)) {
87
+ throw_sdl_error("Could not set current GL context for rendering to the window");
88
+ }
89
+ }
90
+ else if (current_nesting_depth == 0) {
91
+ // On other platforms, make the context current together with its (hidden) window.
92
+ // A context without a Window is broken under Linux with SDL3 (cannot find extensions).
93
+ if (!SDL_GL_MakeCurrent(window.window, window.gl_context)) {
94
+ throw_sdl_error("Could not set current GL context");
95
+ }
96
+ }
97
+
98
+ ++current_nesting_depth;
99
+ }
100
+
101
+ Gosu::OpenGLContext::~OpenGLContext()
102
+ {
103
+ --current_nesting_depth;
104
+
105
+ if (m_lock) {
106
+ SDL_GL_MakeCurrent(nullptr, nullptr);
107
+ }
108
+ }
109
+
110
+ SDL_Window* Gosu::OpenGLContext::shared_sdl_window()
111
+ {
112
+ static const auto& window = SDLWindowAndGLContext::instance();
113
+ return window.window;
114
+ }
115
+
116
+ #endif