gosu 1.4.6 → 2.0.0.pre6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (170) hide show
  1. checksums.yaml +4 -4
  2. data/COPYING +2 -1
  3. data/dependencies/SDL/include/SDL_atomic.h +2 -3
  4. data/dependencies/SDL/include/SDL_audio.h +7 -7
  5. data/dependencies/SDL/include/SDL_blendmode.h +1 -1
  6. data/dependencies/SDL/include/SDL_endian.h +3 -3
  7. data/dependencies/SDL/include/SDL_gamecontroller.h +4 -4
  8. data/dependencies/SDL/include/SDL_hints.h +72 -28
  9. data/dependencies/SDL/include/SDL_joystick.h +8 -5
  10. data/dependencies/SDL/include/SDL_keycode.h +1 -1
  11. data/dependencies/SDL/include/SDL_main.h +7 -0
  12. data/dependencies/SDL/include/SDL_mouse.h +6 -7
  13. data/dependencies/SDL/include/SDL_mutex.h +79 -5
  14. data/dependencies/SDL/include/SDL_opengl_glext.h +5 -1
  15. data/dependencies/SDL/include/SDL_power.h +7 -8
  16. data/dependencies/SDL/include/SDL_render.h +5 -0
  17. data/dependencies/SDL/include/SDL_revision.h +2 -2
  18. data/dependencies/SDL/include/SDL_sensor.h +1 -1
  19. data/dependencies/SDL/include/SDL_stdinc.h +19 -11
  20. data/dependencies/SDL/include/SDL_thread.h +2 -2
  21. data/dependencies/SDL/include/SDL_version.h +2 -2
  22. data/dependencies/SDL/include/SDL_video.h +30 -2
  23. data/dependencies/SDL/include/begin_code.h +7 -7
  24. data/dependencies/SDL/include/close_code.h +2 -2
  25. data/dependencies/SDL/lib/x64/libSDL2.dll.a +0 -0
  26. data/dependencies/SDL/lib/x86/libSDL2.dll.a +0 -0
  27. data/dependencies/SDL_sound/SDL_sound.h +1 -1
  28. data/dependencies/SDL_sound/dr_flac.h +48 -23
  29. data/dependencies/SDL_sound/dr_mp3.h +34 -14
  30. data/dependencies/SDL_sound/stb_vorbis.h +3 -2
  31. data/dependencies/mojoAL/mojoal.c +1 -1
  32. data/ext/{gosu → gosu-ffi}/extconf.rb +34 -33
  33. data/ext/gosu-ffi/gosu-ffi.def +464 -0
  34. data/ffi/Gosu.cpp +307 -0
  35. data/ffi/Gosu.h +84 -0
  36. data/ffi/Gosu_Channel.cpp +62 -0
  37. data/ffi/Gosu_Channel.h +17 -0
  38. data/ffi/Gosu_Color.cpp +132 -0
  39. data/ffi/Gosu_Color.h +31 -0
  40. data/ffi/Gosu_Constants.cpp +334 -0
  41. data/ffi/Gosu_FFI.h +34 -0
  42. data/ffi/Gosu_FFI_internal.h +161 -0
  43. data/ffi/Gosu_Font.cpp +92 -0
  44. data/ffi/Gosu_Font.h +32 -0
  45. data/ffi/Gosu_Image.cpp +206 -0
  46. data/ffi/Gosu_Image.h +60 -0
  47. data/ffi/Gosu_Sample.cpp +29 -0
  48. data/ffi/Gosu_Sample.h +14 -0
  49. data/ffi/Gosu_Song.cpp +69 -0
  50. data/ffi/Gosu_Song.h +18 -0
  51. data/ffi/Gosu_TextInput.cpp +94 -0
  52. data/ffi/Gosu_TextInput.h +25 -0
  53. data/ffi/Gosu_Window.cpp +314 -0
  54. data/ffi/Gosu_Window.h +78 -0
  55. data/include/Gosu/Audio.hpp +6 -11
  56. data/include/Gosu/Bitmap.hpp +38 -53
  57. data/include/Gosu/Buffer.hpp +54 -0
  58. data/include/Gosu/Color.hpp +27 -35
  59. data/include/Gosu/Directories.hpp +25 -28
  60. data/include/Gosu/Drawable.hpp +58 -0
  61. data/include/Gosu/Font.hpp +6 -5
  62. data/include/Gosu/Fwd.hpp +4 -6
  63. data/include/Gosu/Gosu.hpp +5 -5
  64. data/include/Gosu/Graphics.hpp +51 -61
  65. data/include/Gosu/GraphicsBase.hpp +1 -11
  66. data/include/Gosu/Image.hpp +11 -14
  67. data/include/Gosu/Math.hpp +50 -72
  68. data/include/Gosu/Transform.hpp +32 -0
  69. data/include/Gosu/Utility.hpp +51 -1
  70. data/include/Gosu/Version.hpp +3 -3
  71. data/include/Gosu/Window.hpp +15 -9
  72. data/lib/SDL2.dll +0 -0
  73. data/lib/gosu/channel.rb +49 -0
  74. data/lib/gosu/color.rb +150 -0
  75. data/lib/gosu/compat.rb +29 -8
  76. data/lib/gosu/constants.rb +386 -0
  77. data/lib/gosu/ffi.rb +258 -0
  78. data/lib/gosu/font.rb +56 -0
  79. data/lib/gosu/gl_tex_info.rb +33 -0
  80. data/lib/gosu/gosu.rb +210 -0
  81. data/lib/gosu/image.rb +141 -0
  82. data/lib/gosu/numeric.rb +17 -0
  83. data/lib/gosu/preview.rb +6 -6
  84. data/lib/gosu/sample.rb +24 -0
  85. data/lib/gosu/song.rb +56 -0
  86. data/lib/gosu/text_input.rb +69 -0
  87. data/lib/gosu/window.rb +228 -0
  88. data/lib/gosu.rb +29 -8
  89. data/lib64/SDL2.dll +0 -0
  90. data/rdoc/gosu.rb +0 -2
  91. data/src/Audio.cpp +12 -12
  92. data/src/AudioFile.hpp +5 -4
  93. data/src/AudioFileAudioToolbox.cpp +8 -8
  94. data/src/AudioFileSDLSound.cpp +7 -10
  95. data/src/BinPacker.cpp +187 -0
  96. data/src/BinPacker.hpp +55 -0
  97. data/src/Bitmap.cpp +166 -144
  98. data/src/BitmapIO.cpp +60 -86
  99. data/src/Buffer.cpp +159 -0
  100. data/src/Color.cpp +75 -80
  101. data/src/Directories.cpp +47 -0
  102. data/src/DirectoriesUIKit.cpp +50 -0
  103. data/src/DrawOp.hpp +9 -4
  104. data/src/DrawOpQueue.hpp +2 -2
  105. data/src/Drawable.cpp +95 -0
  106. data/src/EmptyDrawable.hpp +38 -0
  107. data/src/FPS.cpp +31 -0
  108. data/src/Font.cpp +104 -74
  109. data/src/GosuGLView.cpp +14 -6
  110. data/src/GosuViewController.cpp +2 -10
  111. data/src/Graphics.cpp +60 -126
  112. data/src/GraphicsImpl.hpp +17 -47
  113. data/src/Image.cpp +41 -35
  114. data/src/Input.cpp +7 -8
  115. data/src/Macro.cpp +6 -6
  116. data/src/Macro.hpp +4 -4
  117. data/src/MarkupParser.cpp +5 -5
  118. data/src/Math.cpp +35 -22
  119. data/src/OffScreenTarget.cpp +53 -49
  120. data/src/OffScreenTarget.hpp +13 -11
  121. data/src/OpenGLContext.cpp +117 -0
  122. data/src/OpenGLContext.hpp +41 -0
  123. data/src/RenderState.hpp +21 -19
  124. data/src/Resolution.cpp +23 -21
  125. data/src/TexChunk.cpp +35 -80
  126. data/src/TexChunk.hpp +44 -35
  127. data/src/Text.cpp +1 -1
  128. data/src/TextBuilder.cpp +35 -21
  129. data/src/TextBuilder.hpp +6 -9
  130. data/src/Texture.cpp +62 -80
  131. data/src/Texture.hpp +25 -23
  132. data/src/TiledDrawable.cpp +150 -0
  133. data/src/TiledDrawable.hpp +47 -0
  134. data/src/TimingApple.cpp +1 -1
  135. data/src/Transform.cpp +45 -50
  136. data/src/TransformStack.hpp +16 -16
  137. data/src/TrueTypeFont.cpp +59 -51
  138. data/src/TrueTypeFont.hpp +6 -7
  139. data/src/TrueTypeFontApple.cpp +28 -19
  140. data/src/TrueTypeFontUnix.cpp +27 -23
  141. data/src/TrueTypeFontWin.cpp +30 -30
  142. data/src/Utility.cpp +84 -21
  143. data/src/UtilityWin.cpp +45 -0
  144. data/src/Window.cpp +92 -142
  145. data/src/WindowUIKit.cpp +14 -14
  146. metadata +72 -31
  147. data/include/Gosu/IO.hpp +0 -254
  148. data/include/Gosu/ImageData.hpp +0 -53
  149. data/include/Gosu/Inspection.hpp +0 -7
  150. data/lib/gosu/patches.rb +0 -66
  151. data/lib/gosu/run.rb +0 -20
  152. data/lib/gosu/swig_patches.rb +0 -110
  153. data/src/BlockAllocator.cpp +0 -131
  154. data/src/BlockAllocator.hpp +0 -32
  155. data/src/DirectoriesApple.cpp +0 -69
  156. data/src/DirectoriesUnix.cpp +0 -46
  157. data/src/DirectoriesWin.cpp +0 -65
  158. data/src/EmptyImageData.hpp +0 -52
  159. data/src/FileUnix.cpp +0 -99
  160. data/src/FileWin.cpp +0 -88
  161. data/src/IO.cpp +0 -60
  162. data/src/Iconv.hpp +0 -51
  163. data/src/Inspection.cpp +0 -27
  164. data/src/LargeImageData.cpp +0 -215
  165. data/src/LargeImageData.hpp +0 -39
  166. data/src/Log.hpp +0 -19
  167. data/src/RubyGosu.cxx +0 -13100
  168. data/src/RubyGosu.h +0 -49
  169. data/src/WinUtility.cpp +0 -61
  170. data/src/WinUtility.hpp +0 -27
data/src/Graphics.cpp CHANGED
@@ -5,47 +5,42 @@
5
5
  #include "DrawOp.hpp"
6
6
  #include "DrawOpQueue.hpp"
7
7
  #include "GraphicsImpl.hpp"
8
- #include "LargeImageData.hpp"
9
8
  #include "Macro.hpp"
10
9
  #include "OffScreenTarget.hpp"
11
- #include "Texture.hpp"
10
+ #include "OpenGLContext.hpp"
12
11
  #include <algorithm>
13
- #include <cmath>
14
- #include <functional>
15
12
  #include <memory>
16
13
 
17
14
  namespace Gosu
18
15
  {
19
16
  namespace
20
17
  {
21
- Graphics* current_graphics_pointer = nullptr;
18
+ Viewport* current_viewport_pointer = nullptr;
22
19
 
23
- Graphics& current_graphics()
20
+ Viewport& current_viewport()
24
21
  {
25
- if (current_graphics_pointer == nullptr) {
26
- throw std::logic_error{"Gosu::Graphics can only be drawn to while rendering"};
22
+ if (current_viewport_pointer == nullptr) {
23
+ throw std::logic_error("Gosu::Graphics can only be drawn to while rendering");
27
24
  }
28
- return *current_graphics_pointer;
25
+ return *current_viewport_pointer;
29
26
  }
30
27
 
31
- std::vector<std::shared_ptr<Texture>> textures;
32
-
33
28
  DrawOpQueueStack queues;
34
29
 
35
30
  DrawOpQueue& current_queue()
36
31
  {
37
32
  if (queues.empty()) {
38
- throw std::logic_error{"There is no rendering queue for this operation"};
33
+ throw std::logic_error("There is no rendering queue for this operation");
39
34
  }
40
35
  return queues.back();
41
36
  }
42
37
  }
43
38
  }
44
39
 
45
- struct Gosu::Graphics::Impl : private Gosu::Noncopyable
40
+ struct Gosu::Viewport::Impl : private Gosu::Noncopyable
46
41
  {
47
- unsigned virt_width = 0, virt_height = 0;
48
- unsigned phys_width = 0, phys_height = 0;
42
+ int virt_width = 0, virt_height = 0;
43
+ int phys_width = 0, phys_height = 0;
49
44
  double black_width = 0.0, black_height = 0.0;
50
45
  Transform base_transform;
51
46
 
@@ -57,9 +52,9 @@ struct Gosu::Graphics::Impl : private Gosu::Noncopyable
57
52
  double scale_y = 1.0 * phys_height / virt_height;
58
53
  double scale_factor = std::min(scale_x, scale_y);
59
54
 
60
- Transform scale_transform = scale(scale_factor);
61
- Transform translate_transform = translate(black_width, black_height);
62
- base_transform = concat(translate_transform, scale_transform);
55
+ Transform scale_transform = Transform::scale(scale_factor);
56
+ Transform translate_transform = Transform::translate(black_width, black_height);
57
+ base_transform = translate_transform * scale_transform;
63
58
  }
64
59
 
65
60
  #ifndef GOSU_IS_OPENGLES
@@ -93,8 +88,8 @@ struct Gosu::Graphics::Impl : private Gosu::Noncopyable
93
88
  #endif
94
89
  };
95
90
 
96
- Gosu::Graphics::Graphics(unsigned phys_width, unsigned phys_height)
97
- : m_impl(new Impl)
91
+ Gosu::Viewport::Viewport(int phys_width, int phys_height)
92
+ : m_impl(new Impl)
98
93
  {
99
94
  m_impl->virt_width = phys_width;
100
95
  m_impl->virt_height = phys_height;
@@ -102,6 +97,7 @@ Gosu::Graphics::Graphics(unsigned phys_width, unsigned phys_height)
102
97
  m_impl->black_height = 0;
103
98
 
104
99
  // TODO: Should be merged into RenderState and removed from Graphics.
100
+ const OpenGLContext current_context;
105
101
  glMatrixMode(GL_MODELVIEW);
106
102
  glLoadIdentity();
107
103
  glEnable(GL_BLEND);
@@ -109,29 +105,29 @@ Gosu::Graphics::Graphics(unsigned phys_width, unsigned phys_height)
109
105
  set_physical_resolution(phys_width, phys_height);
110
106
  }
111
107
 
112
- Gosu::Graphics::~Graphics()
108
+ Gosu::Viewport::~Viewport()
113
109
  {
114
- if (current_graphics_pointer == this) {
115
- current_graphics_pointer = nullptr;
110
+ if (current_viewport_pointer == this) {
111
+ current_viewport_pointer = nullptr;
116
112
  }
117
113
  }
118
114
 
119
- unsigned Gosu::Graphics::width() const
115
+ int Gosu::Viewport::width() const
120
116
  {
121
117
  return m_impl->virt_width;
122
118
  }
123
119
 
124
- unsigned Gosu::Graphics::height() const
120
+ int Gosu::Viewport::height() const
125
121
  {
126
122
  return m_impl->virt_height;
127
123
  }
128
124
 
129
- void Gosu::Graphics::set_resolution(unsigned virtual_width, unsigned virtual_height,
125
+ void Gosu::Viewport::set_resolution(int virtual_width, int virtual_height,
130
126
  double horizontal_black_bar_width,
131
127
  double vertical_black_bar_height)
132
128
  {
133
- if (virtual_width == 0 || virtual_height == 0) {
134
- throw std::invalid_argument{"Invalid virtual resolution."};
129
+ if (virtual_width <= 0 || virtual_height <= 0) {
130
+ throw std::invalid_argument("Invalid virtual resolution.");
135
131
  }
136
132
 
137
133
  m_impl->virt_width = virtual_width;
@@ -142,10 +138,10 @@ void Gosu::Graphics::set_resolution(unsigned virtual_width, unsigned virtual_hei
142
138
  m_impl->update_base_transform();
143
139
  }
144
140
 
145
- void Gosu::Graphics::frame(const std::function<void()>& f)
141
+ void Gosu::Viewport::frame(const std::function<void()>& f)
146
142
  {
147
- if (current_graphics_pointer != nullptr) {
148
- throw std::logic_error{"Cannot nest calls to Gosu::Graphics::begin()"};
143
+ if (current_viewport_pointer != nullptr) {
144
+ throw std::logic_error("Cannot nest calls to Gosu::Graphics::frame()");
149
145
  }
150
146
 
151
147
  // Cancel all recording or whatever that might still be in progress...
@@ -165,16 +161,19 @@ void Gosu::Graphics::frame(const std::function<void()>& f)
165
161
 
166
162
  queues.back().set_base_transform(m_impl->base_transform);
167
163
 
168
- ensure_current_context();
164
+ const OpenGLContext current_context(true);
165
+
169
166
  glClearColor(0, 0, 0, 1);
170
167
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
171
168
 
172
- current_graphics_pointer = this;
169
+ current_viewport_pointer = this;
173
170
 
174
171
  f();
175
172
 
176
173
  // Cancel all intermediate queues that have not been cleaned up.
177
- while (queues.size() > 1) queues.pop_back();
174
+ while (queues.size() > 1) {
175
+ queues.pop_back();
176
+ }
178
177
 
179
178
  flush();
180
179
 
@@ -194,9 +193,7 @@ void Gosu::Graphics::frame(const std::function<void()>& f)
194
193
  flush();
195
194
  }
196
195
 
197
- glFlush();
198
-
199
- current_graphics_pointer = nullptr;
196
+ current_viewport_pointer = nullptr;
200
197
 
201
198
  // Clear leftover transforms, clip rects etc.
202
199
  if (queues.size() == 1) {
@@ -208,22 +205,22 @@ void Gosu::Graphics::frame(const std::function<void()>& f)
208
205
  }
209
206
  }
210
207
 
211
- void Gosu::Graphics::flush()
208
+ void Gosu::flush()
212
209
  {
213
210
  current_queue().perform_draw_ops_and_code();
214
211
  current_queue().clear_queue();
215
212
  }
216
213
 
217
- void Gosu::Graphics::gl(const std::function<void()>& f)
214
+ void Gosu::gl(const std::function<void()>& f)
218
215
  {
219
216
  if (current_queue().mode() == QM_RECORD_MACRO) {
220
- throw std::logic_error{"Custom OpenGL is not allowed while creating a macro"};
217
+ throw std::logic_error("Custom OpenGL is not allowed while creating a macro");
221
218
  }
222
219
 
223
220
  #ifdef GOSU_IS_OPENGLES
224
- throw std::logic_error{"Custom OpenGL ES is not supported yet"};
221
+ throw std::logic_error("Custom OpenGL ES is not supported yet");
225
222
  #else
226
- Graphics& cg = current_graphics();
223
+ Viewport& cg = current_viewport();
227
224
 
228
225
  flush();
229
226
 
@@ -235,13 +232,13 @@ void Gosu::Graphics::gl(const std::function<void()>& f)
235
232
  #endif
236
233
  }
237
234
 
238
- void Gosu::Graphics::gl(Gosu::ZPos z, const std::function<void()>& f)
235
+ void Gosu::gl(Gosu::ZPos z, const std::function<void()>& f)
239
236
  {
240
237
  #ifdef GOSU_IS_OPENGLES
241
- throw std::logic_error{"Custom OpenGL ES is not supported yet"};
238
+ throw std::logic_error("Custom OpenGL ES is not supported yet");
242
239
  #else
243
240
  const auto wrapped_f = [f] {
244
- Graphics& cg = current_graphics();
241
+ Viewport& cg = current_viewport();
245
242
  cg.m_impl->begin_gl();
246
243
  f();
247
244
  cg.m_impl->end_gl();
@@ -250,19 +247,19 @@ void Gosu::Graphics::gl(Gosu::ZPos z, const std::function<void()>& f)
250
247
  #endif
251
248
  }
252
249
 
253
- void Gosu::Graphics::clip_to(double x, double y, double width, double height,
250
+ void Gosu::clip_to(double x, double y, double width, double height,
254
251
  const std::function<void()>& f)
255
252
  {
256
- double screen_height = current_graphics().m_impl->phys_height;
253
+ double screen_height = current_viewport().m_impl->phys_height;
257
254
  current_queue().begin_clipping(x, y, width, height, screen_height);
258
255
  f();
259
256
  current_queue().end_clipping();
260
257
  }
261
258
 
262
- Gosu::Image Gosu::Graphics::render(int width, int height, const std::function<void()>& f,
259
+ Gosu::Image Gosu::render(int width, int height, const std::function<void()>& f,
263
260
  unsigned image_flags)
264
261
  {
265
- ensure_current_context();
262
+ const OpenGLContext current_context;
266
263
 
267
264
  // Prepare for rendering at the requested size, but save the previous matrix and viewport.
268
265
  glMatrixMode(GL_PROJECTION);
@@ -271,8 +268,8 @@ Gosu::Image Gosu::Graphics::render(int width, int height, const std::function<vo
271
268
  GLint prev_viewport[4];
272
269
  glGetIntegerv(GL_VIEWPORT, prev_viewport);
273
270
  glViewport(0, 0, width, height);
274
- // Note the flipped vertical axis in the glOrtho call - this is so we don't have to vertically
275
- // flip the texture afterwards.
271
+ // Note the flipped vertical axis in the glOrtho call, so we don't have to vertically
272
+ // flip the texture afterward.
276
273
  #ifdef GOSU_IS_OPENGLES
277
274
  glOrthof(0, width, 0, height, -1, 1); // NOLINT(readability-suspicious-call-argument)
278
275
  #else
@@ -291,7 +288,6 @@ Gosu::Image Gosu::Graphics::render(int width, int height, const std::function<vo
291
288
  f();
292
289
  queues.back().perform_draw_ops_and_code();
293
290
  queues.pop_back();
294
- glFlush();
295
291
  #ifndef GOSU_IS_OPENGLES
296
292
  glPopAttrib();
297
293
  #endif
@@ -305,25 +301,25 @@ Gosu::Image Gosu::Graphics::render(int width, int height, const std::function<vo
305
301
  return result;
306
302
  }
307
303
 
308
- Gosu::Image Gosu::Graphics::record(int width, int height, const std::function<void()>& f)
304
+ Gosu::Image Gosu::record(int width, int height, const std::function<void()>& f)
309
305
  {
310
306
  queues.emplace_back(QM_RECORD_MACRO);
311
307
 
312
308
  f();
313
309
 
314
- std::unique_ptr<ImageData> result(new Macro(current_queue(), width, height));
310
+ std::unique_ptr<Drawable> result(new Macro(current_queue(), width, height));
315
311
  queues.pop_back();
316
- return Image(move(result));
312
+ return Image(std::move(result));
317
313
  }
318
314
 
319
- void Gosu::Graphics::transform(const Gosu::Transform& transform, const std::function<void()>& f)
315
+ void Gosu::transform(const Gosu::Transform& transform, const std::function<void()>& f)
320
316
  {
321
317
  current_queue().push_transform(transform);
322
318
  f();
323
319
  current_queue().pop_transform();
324
320
  }
325
321
 
326
- void Gosu::Graphics::draw_line(double x1, double y1, Color c1, double x2, double y2, Color c2,
322
+ void Gosu::draw_line(double x1, double y1, Color c1, double x2, double y2, Color c2,
327
323
  ZPos z, BlendMode mode)
328
324
  {
329
325
  DrawOp op;
@@ -336,7 +332,7 @@ void Gosu::Graphics::draw_line(double x1, double y1, Color c1, double x2, double
336
332
  current_queue().schedule_draw_op(op);
337
333
  }
338
334
 
339
- void Gosu::Graphics::draw_triangle(double x1, double y1, Color c1, double x2, double y2, Color c2,
335
+ void Gosu::draw_triangle(double x1, double y1, Color c1, double x2, double y2, Color c2,
340
336
  double x3, double y3, Color c3, ZPos z, BlendMode mode)
341
337
  {
342
338
  DrawOp op;
@@ -354,7 +350,7 @@ void Gosu::Graphics::draw_triangle(double x1, double y1, Color c1, double x2, do
354
350
  current_queue().schedule_draw_op(op);
355
351
  }
356
352
 
357
- void Gosu::Graphics::draw_quad(double x1, double y1, Color c1, double x2, double y2, Color c2,
353
+ void Gosu::draw_quad(double x1, double y1, Color c1, double x2, double y2, Color c2,
358
354
  double x3, double y3, Color c3, double x4, double y4, Color c4,
359
355
  ZPos z, BlendMode mode)
360
356
  {
@@ -378,25 +374,26 @@ void Gosu::Graphics::draw_quad(double x1, double y1, Color c1, double x2, double
378
374
  current_queue().schedule_draw_op(op);
379
375
  }
380
376
 
381
- void Gosu::Graphics::draw_rect(double x, double y, double width, double height, Color c, ZPos z,
377
+ void Gosu::draw_rect(double x, double y, double width, double height, Color c, ZPos z,
382
378
  Gosu::BlendMode mode)
383
379
  {
384
380
  draw_quad(x, y, c, x + width, y, c, x, y + height, c, x + width, y + height, c, z, mode);
385
381
  }
386
382
 
387
- void Gosu::Graphics::schedule_draw_op(const Gosu::DrawOp& op)
383
+ void Gosu::schedule_draw_op(const Gosu::DrawOp& op)
388
384
  {
389
385
  current_queue().schedule_draw_op(op);
390
386
  }
391
387
 
392
- void Gosu::Graphics::set_physical_resolution(unsigned phys_width, unsigned phys_height)
388
+ void Gosu::Viewport::set_physical_resolution(int phys_width, int phys_height)
393
389
  {
394
390
  m_impl->phys_width = phys_width;
395
391
  m_impl->phys_height = phys_height;
396
392
  // TODO: Should be merged into RenderState and removed from Graphics.
393
+ const OpenGLContext current_context;
397
394
  glMatrixMode(GL_PROJECTION);
398
395
  glLoadIdentity();
399
- glViewport(0, 0, phys_width, phys_height);
396
+ glViewport(0, 0, static_cast<GLsizei>(phys_width), static_cast<GLsizei>(phys_height));
400
397
  #ifdef GOSU_IS_OPENGLES
401
398
  glOrthof(0, phys_width, phys_height, 0, -1, 1);
402
399
  #else
@@ -405,66 +402,3 @@ void Gosu::Graphics::set_physical_resolution(unsigned phys_width, unsigned phys_
405
402
 
406
403
  m_impl->update_base_transform();
407
404
  }
408
-
409
- std::unique_ptr<Gosu::ImageData> Gosu::Graphics::create_image(const Bitmap& src, unsigned src_x,
410
- unsigned src_y, unsigned src_width,
411
- unsigned src_height, unsigned flags)
412
- {
413
- static const unsigned max_size = MAX_TEXTURE_SIZE;
414
-
415
- // Backward compatibility: This used to be 'bool tileable'.
416
- if (flags == 1) flags = IF_TILEABLE;
417
-
418
- bool wants_retro = (flags & IF_RETRO);
419
-
420
- // Special case: If the texture is supposed to have hard borders, is
421
- // quadratic, has a size that is at least 64 pixels but no more than max_size
422
- // pixels and a power of two, create a single texture just for this image.
423
- if ((flags & IF_TILEABLE) == IF_TILEABLE && src_width == src_height &&
424
- (src_width & (src_width - 1)) == 0 && src_width >= 64 && src_width <= max_size) {
425
- std::shared_ptr<Texture> texture{new Texture(src_width, src_height, wants_retro)};
426
- std::unique_ptr<ImageData> data;
427
-
428
- // Use the source bitmap directly if the source area completely covers
429
- // it.
430
- if (src_x == 0 && src_width == src.width() && src_y == 0 && src_height == src.height()) {
431
- data = texture->try_alloc(src, 0);
432
- }
433
- else {
434
- Bitmap bmp(src_width, src_height);
435
- bmp.insert(0, 0, src, src_x, src_y, src_width, src_height);
436
- data = texture->try_alloc(bmp, 0);
437
- }
438
-
439
- if (!data) throw std::logic_error{"Internal texture block allocation error"};
440
- return data;
441
- }
442
-
443
- // Too large to fit on a single texture.
444
- if (src_width > max_size - 2 || src_height > max_size - 2) {
445
- Bitmap bmp(src_width, src_height);
446
- bmp.insert(0, 0, src, src_x, src_y, src_width, src_height);
447
- return std::unique_ptr<ImageData>{
448
- new LargeImageData(bmp, max_size - 2, max_size - 2, flags)};
449
- }
450
-
451
- Bitmap bmp = apply_border_flags(flags, src, src_x, src_y, src_width, src_height);
452
-
453
- // Try to put the bitmap into one of the already allocated textures.
454
- for (const auto& texture : textures) {
455
- if (texture->retro() != wants_retro) continue;
456
-
457
- std::unique_ptr<ImageData> data = texture->try_alloc(bmp, 1);
458
- if (data) return data;
459
- }
460
-
461
- // All textures are full: Create a new one.
462
-
463
- std::shared_ptr<Texture> texture{new Texture(max_size, max_size, wants_retro)};
464
- textures.push_back(texture);
465
-
466
- std::unique_ptr<ImageData> data{texture->try_alloc(bmp, 1)};
467
- if (!data.get()) throw std::logic_error("Internal texture block allocation error");
468
-
469
- return data;
470
- }
data/src/GraphicsImpl.hpp CHANGED
@@ -3,16 +3,8 @@
3
3
  #include <Gosu/Bitmap.hpp>
4
4
  #include <Gosu/Graphics.hpp>
5
5
  #include <Gosu/Platform.hpp>
6
-
7
- #if defined(GOSU_IS_IPHONE) || defined(GOSU_IS_OPENGLES)
8
- #include <OpenGLES/ES1/gl.h>
9
- #include <OpenGLES/ES1/glext.h>
10
- #else
11
- #include <SDL.h>
12
- #include <SDL_opengl.h>
13
- #endif
14
-
15
6
  #include <algorithm>
7
+ #include <cstdint>
16
8
  #include <list>
17
9
  #include <vector>
18
10
 
@@ -21,15 +13,14 @@ namespace Gosu
21
13
  struct RenderState;
22
14
  class RenderStateManager;
23
15
 
24
- const GLuint NO_TEXTURE = static_cast<GLuint>(-1);
25
16
  const unsigned NO_CLIPPING = 0xffffffff;
26
-
17
+
27
18
  // In various places in Gosu, width==NO_CLIPPING by convention means
28
19
  // that no clipping should happen.
29
20
  struct ClipRect
30
21
  {
31
22
  double x, y, width, height;
32
-
23
+
33
24
  bool operator==(const ClipRect& other) const
34
25
  {
35
26
  // No clipping
@@ -38,10 +29,7 @@ namespace Gosu
38
29
  (x == other.x && y == other.y && width == other.width && height == other.height);
39
30
  }
40
31
  };
41
- }
42
32
 
43
- namespace Gosu
44
- {
45
33
  enum QueueMode
46
34
  {
47
35
  QM_RENDER_TO_SCREEN,
@@ -56,59 +44,41 @@ namespace Gosu
56
44
  class DrawOpQueue;
57
45
  typedef std::list<Transform> Transforms;
58
46
  typedef std::list<DrawOpQueue> DrawOpQueueStack;
59
- class LargeImageData;
60
47
  class Macro;
61
48
  struct ArrayVertex
62
49
  {
63
- GLfloat tex_coords[2];
64
- GLuint color;
65
- GLfloat vertices[3];
50
+ float tex_coords[2];
51
+ std::uint32_t color;
52
+ float vertices[3];
66
53
  };
67
-
54
+
68
55
  template<typename T>
69
56
  bool is_p_to_the_left_of_ab(T xa, T ya, T xb, T yb, T xp, T yp)
70
57
  {
71
58
  return (xb - xa) * (yp - ya) - (xp - xa) * (yb - ya) > 0;
72
59
  }
73
-
60
+
74
61
  template<typename T, typename C>
75
62
  void normalize_coordinates(T& x1, T& y1, T& x2, T& y2, T& x3, T& y3, C& c3, T& x4, T& y4, C& c4)
76
63
  {
77
64
  if (is_p_to_the_left_of_ab(x1, y1, x2, y2, x3, y3) ==
78
65
  is_p_to_the_left_of_ab(x2, y2, x3, y3, x4, y4)) {
79
- using std::swap;
80
- swap(x3, x4);
81
- swap(y3, y4);
82
- swap(c3, c4);
83
- }
84
- }
85
-
86
- template<typename Float>
87
- void apply_transform(const Transform& transform, Float& x, Float& y)
88
- {
89
- Float in[4] = { x, y, 0, 1 };
90
- Float out[4] = { 0, 0, 0, 0 };
91
- for (int i = 0; i < 4; ++i) {
92
- for (int j = 0; j < 4; ++j) {
93
- out[i] += in[j] * transform[j * 4 + i];
94
- }
66
+ std::swap(x3, x4);
67
+ std::swap(y3, y4);
68
+ std::swap(c3, c4);
95
69
  }
96
- x = out[0] / out[3];
97
- y = out[1] / out[3];
98
70
  }
99
-
71
+
72
+ void schedule_draw_op(const DrawOp& op);
73
+
100
74
  #ifdef GOSU_IS_IPHONE
101
75
  int clip_rect_base_factor();
102
76
  #else
103
77
  inline int clip_rect_base_factor() { return 1; }
104
78
  #endif
105
-
106
- #ifndef GOSU_IS_IPHONE
107
- SDL_Window* shared_window();
108
- #endif
109
-
110
- void ensure_current_context();
111
-
79
+
80
+ void register_frame();
81
+
112
82
  inline std::string escape_markup(const std::string& text) {
113
83
  auto markup = text;
114
84
  for (std::string::size_type pos = 0; pos < markup.length(); ++pos) {
data/src/Image.cpp CHANGED
@@ -1,53 +1,54 @@
1
1
  #include <Gosu/Bitmap.hpp>
2
+ #include <Gosu/Buffer.hpp>
3
+ #include <Gosu/Drawable.hpp>
2
4
  #include <Gosu/Graphics.hpp>
3
- #include <Gosu/IO.hpp>
4
5
  #include <Gosu/Image.hpp>
5
- #include <Gosu/ImageData.hpp>
6
6
  #include <Gosu/Math.hpp>
7
- #include "EmptyImageData.hpp"
7
+ #include "EmptyDrawable.hpp"
8
8
  #include <stdexcept>
9
9
 
10
10
  Gosu::Image::Image()
11
- : m_data{EmptyImageData::instance_ptr()}
12
11
  {
12
+ static const auto default_data_ptr = std::make_shared<EmptyDrawable>(0, 0);
13
+ m_drawable = default_data_ptr;
13
14
  }
14
15
 
15
16
  Gosu::Image::Image(const std::string& filename, unsigned image_flags)
16
- : Image(load_image_file(filename), image_flags)
17
+ : Image(load_image_file(filename), image_flags)
17
18
  {
18
19
  }
19
20
 
20
- Gosu::Image::Image(const std::string& filename, int src_x, int src_y, int src_width, int src_height,
21
- unsigned image_flags)
22
- : Image(load_image_file(filename), src_x, src_y, src_width, src_height, image_flags)
21
+ Gosu::Image::Image(const std::string& filename, const Rect& source_rect, unsigned image_flags)
22
+ : Image(load_image_file(filename), source_rect, image_flags)
23
23
  {
24
24
  }
25
25
 
26
26
  Gosu::Image::Image(const Bitmap& source, unsigned image_flags)
27
- : Image(source, 0, 0, source.width(), source.height(), image_flags)
27
+ : Image(source, Rect::covering(source), image_flags)
28
28
  {
29
29
  }
30
30
 
31
- Gosu::Image::Image(const Bitmap& source, int src_x, int src_y, int src_width, int src_height,
32
- unsigned image_flags)
33
- : m_data{Graphics::create_image(source, src_x, src_y, src_width, src_height, image_flags)}
31
+ Gosu::Image::Image(const Bitmap& source, const Rect& source_rect, unsigned image_flags)
32
+ : m_drawable(create_drawable(source, source_rect, image_flags))
34
33
  {
35
34
  }
36
35
 
37
- Gosu::Image::Image(std::unique_ptr<ImageData>&& data)
38
- : m_data{data.release()}
36
+ Gosu::Image::Image(std::unique_ptr<Drawable> data)
37
+ : m_drawable(std::move(data))
39
38
  {
40
- if (!m_data) throw std::invalid_argument("Gosu::Image cannot be initialized with nullptr");
39
+ if (!m_drawable) {
40
+ throw std::invalid_argument("Gosu::Image cannot be initialized with nullptr");
41
+ }
41
42
  }
42
43
 
43
44
  unsigned Gosu::Image::width() const
44
45
  {
45
- return m_data->width();
46
+ return m_drawable->width();
46
47
  }
47
48
 
48
49
  unsigned Gosu::Image::height() const
49
50
  {
50
- return m_data->height();
51
+ return m_drawable->height();
51
52
  }
52
53
 
53
54
  void Gosu::Image::draw(double x, double y, ZPos z, double scale_x, double scale_y, Color c,
@@ -56,7 +57,7 @@ void Gosu::Image::draw(double x, double y, ZPos z, double scale_x, double scale_
56
57
  double x2 = x + width() * scale_x;
57
58
  double y2 = y + height() * scale_y;
58
59
 
59
- m_data->draw(x, y, c, x2, y, c, x, y2, c, x2, y2, c, z, mode);
60
+ m_drawable->draw(x, y, c, x2, y, c, x, y2, c, x2, y2, c, z, mode);
60
61
  }
61
62
 
62
63
  void Gosu::Image::draw_mod(double x, double y, ZPos z, double scale_x, double scale_y, Color c1,
@@ -65,7 +66,7 @@ void Gosu::Image::draw_mod(double x, double y, ZPos z, double scale_x, double sc
65
66
  double x2 = x + width() * scale_x;
66
67
  double y2 = y + height() * scale_y;
67
68
 
68
- m_data->draw(x, y, c1, x2, y, c2, x, y2, c3, x2, y2, c4, z, mode);
69
+ m_drawable->draw(x, y, c1, x2, y, c2, x, y2, c3, x2, y2, c4, z, mode);
69
70
  }
70
71
 
71
72
  void Gosu::Image::draw_rot(double x, double y, ZPos z, double angle, double center_x,
@@ -87,44 +88,49 @@ void Gosu::Image::draw_rot(double x, double y, ZPos z, double angle, double cent
87
88
  double dist_to_bottom_x = -offs_x * size_y * (1 - center_y);
88
89
  double dist_to_bottom_y = -offs_y * size_y * (1 - center_y);
89
90
 
90
- m_data->draw(x + dist_to_left_x + dist_to_top_x, y + dist_to_left_y + dist_to_top_y, c,
91
- x + dist_to_right_x + dist_to_top_x, y + dist_to_right_y + dist_to_top_y, c,
92
- x + dist_to_left_x + dist_to_bottom_x, y + dist_to_left_y + dist_to_bottom_y, c,
93
- x + dist_to_right_x + dist_to_bottom_x, y + dist_to_right_y + dist_to_bottom_y, c,
94
- z, mode);
91
+ m_drawable->draw( //
92
+ x + dist_to_left_x + dist_to_top_x, y + dist_to_left_y + dist_to_top_y, c,
93
+ x + dist_to_right_x + dist_to_top_x, y + dist_to_right_y + dist_to_top_y, c,
94
+ x + dist_to_left_x + dist_to_bottom_x, y + dist_to_left_y + dist_to_bottom_y, c,
95
+ x + dist_to_right_x + dist_to_bottom_x, y + dist_to_right_y + dist_to_bottom_y, c, //
96
+ z, mode);
95
97
  }
96
98
 
97
- Gosu::ImageData& Gosu::Image::data() const
99
+ Gosu::Drawable& Gosu::Image::drawable() const
98
100
  {
99
- return *m_data;
101
+ return *m_drawable;
100
102
  }
101
103
 
102
- std::vector<Gosu::Image> Gosu::load_tiles(const Bitmap& bmp, //
104
+ std::vector<Gosu::Image> Gosu::load_tiles(const Bitmap& bitmap, //
103
105
  int tile_width, int tile_height, unsigned flags)
104
106
  {
107
+ if (tile_width == 0 || tile_height == 0) {
108
+ throw std::invalid_argument("Gosu::load_tiles does not support empty tiles");
109
+ }
110
+
105
111
  int tiles_x, tiles_y;
106
112
  std::vector<Image> images;
107
113
 
108
114
  if (tile_width > 0) {
109
- tiles_x = bmp.width() / tile_width;
115
+ tiles_x = bitmap.width() / tile_width;
110
116
  }
111
117
  else {
112
118
  tiles_x = -tile_width;
113
- tile_width = bmp.width() / tiles_x;
119
+ tile_width = bitmap.width() / tiles_x;
114
120
  }
115
121
 
116
122
  if (tile_height > 0) {
117
- tiles_y = bmp.height() / tile_height;
123
+ tiles_y = bitmap.height() / tile_height;
118
124
  }
119
125
  else {
120
126
  tiles_y = -tile_height;
121
- tile_height = bmp.height() / tiles_y;
127
+ tile_height = bitmap.height() / tiles_y;
122
128
  }
123
129
 
124
130
  for (int y = 0; y < tiles_y; ++y) {
125
131
  for (int x = 0; x < tiles_x; ++x) {
126
- images.emplace_back(bmp, x * tile_width, y * tile_height, tile_width, tile_height,
127
- flags);
132
+ images.emplace_back(
133
+ bitmap, Rect { x * tile_width, y * tile_height, tile_width, tile_height }, flags);
128
134
  }
129
135
  }
130
136
 
@@ -134,6 +140,6 @@ std::vector<Gosu::Image> Gosu::load_tiles(const Bitmap& bmp, //
134
140
  std::vector<Gosu::Image> Gosu::load_tiles(const std::string& filename, //
135
141
  int tile_width, int tile_height, unsigned flags)
136
142
  {
137
- Bitmap bmp = load_image_file(filename);
138
- return load_tiles(bmp, tile_width, tile_height, flags);
143
+ const Bitmap bitmap = load_image_file(filename);
144
+ return load_tiles(bitmap, tile_width, tile_height, flags);
139
145
  }