gosu 0.10.9.pre1 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (136) hide show
  1. checksums.yaml +4 -4
  2. data/Gosu/Audio.hpp +35 -66
  3. data/Gosu/AutoLink.hpp +14 -16
  4. data/Gosu/Bitmap.hpp +50 -37
  5. data/Gosu/Buttons.hpp +246 -265
  6. data/Gosu/Color.hpp +32 -76
  7. data/Gosu/Directories.hpp +14 -17
  8. data/Gosu/Font.hpp +28 -34
  9. data/Gosu/Fwd.hpp +27 -31
  10. data/Gosu/Gosu.hpp +2 -5
  11. data/Gosu/Graphics.hpp +31 -48
  12. data/Gosu/GraphicsBase.hpp +27 -58
  13. data/Gosu/IO.hpp +44 -56
  14. data/Gosu/Image.hpp +29 -73
  15. data/Gosu/ImageData.hpp +13 -17
  16. data/Gosu/Input.hpp +42 -57
  17. data/Gosu/Inspection.hpp +2 -6
  18. data/Gosu/Math.hpp +32 -38
  19. data/Gosu/Platform.hpp +10 -29
  20. data/Gosu/Text.hpp +30 -39
  21. data/Gosu/TextInput.hpp +29 -36
  22. data/Gosu/Timing.hpp +14 -16
  23. data/Gosu/Utility.hpp +10 -15
  24. data/Gosu/Version.hpp +13 -14
  25. data/Gosu/Window.hpp +53 -68
  26. data/README.md +23 -11
  27. data/ext/gosu/extconf.rb +31 -81
  28. data/lib/gosu/patches.rb +35 -19
  29. data/lib/gosu/run.rb +13 -4
  30. data/rdoc/gosu.rb +24 -20
  31. data/src/ALChannelManagement.hpp +119 -0
  32. data/src/{Audio/Audio.cpp → Audio.cpp} +177 -211
  33. data/src/AudioFile.hpp +57 -0
  34. data/src/AudioToolboxFile.hpp +214 -0
  35. data/src/Bitmap.cpp +159 -0
  36. data/src/BitmapIO.cpp +141 -0
  37. data/src/BlockAllocator.cpp +133 -0
  38. data/src/{Graphics/BlockAllocator.hpp → BlockAllocator.hpp} +34 -35
  39. data/src/ClipRectStack.hpp +87 -0
  40. data/src/{Graphics/Color.cpp → Color.cpp} +30 -28
  41. data/src/DirectoriesApple.cpp +68 -0
  42. data/src/DirectoriesUnix.cpp +20 -18
  43. data/src/DirectoriesWin.cpp +40 -41
  44. data/src/DrawOp.hpp +168 -0
  45. data/src/DrawOpQueue.hpp +190 -0
  46. data/src/FileUnix.cpp +40 -46
  47. data/src/FileWin.cpp +42 -38
  48. data/src/Font.cpp +165 -0
  49. data/src/{Text/FormattedString.hpp → FormattedString.hpp} +114 -114
  50. data/src/GosuAppDelegate.cpp +30 -0
  51. data/src/{UIKit/GosuAppDelegate.h → GosuAppDelegate.h} +0 -0
  52. data/src/{UIKit/GosuGLView.mm → GosuGLView.cpp} +22 -17
  53. data/src/{UIKit/GosuGLView.h → GosuGLView.h} +0 -0
  54. data/src/GosuViewController.cpp +231 -0
  55. data/src/{UIKit/GosuViewController.h → GosuViewController.h} +0 -0
  56. data/src/Graphics.cpp +464 -0
  57. data/src/{Graphics/Common.hpp → GraphicsImpl.hpp} +29 -32
  58. data/src/IO.cpp +17 -16
  59. data/src/Iconv.hpp +13 -22
  60. data/src/Image.cpp +142 -0
  61. data/src/Input.cpp +459 -0
  62. data/src/InputUIKit.cpp +197 -0
  63. data/src/Inspection.cpp +4 -5
  64. data/src/LargeImageData.cpp +151 -0
  65. data/src/LargeImageData.hpp +43 -0
  66. data/src/{Graphics/Macro.cpp → Macro.cpp} +77 -78
  67. data/src/Macro.hpp +30 -0
  68. data/src/Math.cpp +17 -29
  69. data/src/{Audio/OggFile.hpp → OggFile.hpp} +19 -24
  70. data/src/RenderState.hpp +205 -0
  71. data/src/Resolution.cpp +86 -0
  72. data/src/ResolutionApple.cpp +25 -0
  73. data/{ext/gosu/gosu_wrap.cxx → src/RubyGosu.cxx} +2256 -1707
  74. data/{ext/gosu/gosu_wrap.h → src/RubyGosu.h} +9 -9
  75. data/src/{Audio/SndFile.hpp → SndFile.hpp} +54 -43
  76. data/src/TexChunk.cpp +117 -0
  77. data/src/{Graphics/TexChunk.hpp → TexChunk.hpp} +13 -18
  78. data/src/Text.cpp +371 -0
  79. data/src/TextApple.cpp +209 -0
  80. data/src/TextInput.cpp +278 -0
  81. data/src/TextTTFWin.cpp +251 -0
  82. data/src/{Text/TextUnix.cpp → TextUnix.cpp} +96 -92
  83. data/src/TextWin.cpp +194 -0
  84. data/src/{Graphics/Texture.cpp → Texture.cpp} +35 -38
  85. data/src/{Graphics/Texture.hpp → Texture.hpp} +9 -13
  86. data/src/TimingApple.cpp +11 -7
  87. data/src/TimingUnix.cpp +13 -7
  88. data/src/TimingWin.cpp +6 -1
  89. data/src/{Graphics/Transform.cpp → Transform.cpp} +17 -12
  90. data/src/{Graphics/TransformStack.hpp → TransformStack.hpp} +24 -25
  91. data/src/Utility.cpp +29 -70
  92. data/src/UtilityApple.cpp +52 -0
  93. data/src/UtilityWin.cpp +7 -4
  94. data/src/Version.cpp +22 -0
  95. data/src/WinMain.cpp +30 -33
  96. data/src/WinUtility.cpp +24 -22
  97. data/src/WinUtility.hpp +11 -20
  98. data/src/Window.cpp +142 -112
  99. data/src/WindowUIKit.cpp +155 -0
  100. data/src/stb_image.h +384 -173
  101. data/src/stb_vorbis.c +20 -18
  102. metadata +60 -62
  103. data/Gosu/TR1.hpp +0 -56
  104. data/src/AppleUtility.hpp +0 -66
  105. data/src/Audio/ALChannelManagement.hpp +0 -114
  106. data/src/Audio/Audio.mm +0 -1
  107. data/src/Audio/AudioFile.hpp +0 -53
  108. data/src/Audio/AudioToolboxFile.hpp +0 -207
  109. data/src/Bitmap/Bitmap.cpp +0 -183
  110. data/src/Bitmap/BitmapIO.cpp +0 -176
  111. data/src/DirectoriesApple.mm +0 -71
  112. data/src/Graphics/BlockAllocator.cpp +0 -142
  113. data/src/Graphics/ClipRectStack.hpp +0 -93
  114. data/src/Graphics/DrawOp.hpp +0 -175
  115. data/src/Graphics/DrawOpQueue.hpp +0 -188
  116. data/src/Graphics/Graphics.cpp +0 -478
  117. data/src/Graphics/Image.cpp +0 -193
  118. data/src/Graphics/LargeImageData.cpp +0 -133
  119. data/src/Graphics/LargeImageData.hpp +0 -46
  120. data/src/Graphics/Macro.hpp +0 -36
  121. data/src/Graphics/RenderState.hpp +0 -211
  122. data/src/Graphics/Resolution.cpp +0 -91
  123. data/src/Graphics/ResolutionApple.mm +0 -19
  124. data/src/Graphics/TexChunk.cpp +0 -112
  125. data/src/Input/Input.cpp +0 -463
  126. data/src/Input/InputUIKit.mm +0 -190
  127. data/src/Input/TextInput.cpp +0 -261
  128. data/src/Text/Font.cpp +0 -175
  129. data/src/Text/Text.cpp +0 -391
  130. data/src/Text/TextApple.mm +0 -227
  131. data/src/Text/TextTTFWin.cpp +0 -249
  132. data/src/Text/TextWin.cpp +0 -186
  133. data/src/UIKit/GosuAppDelegate.mm +0 -24
  134. data/src/UIKit/GosuViewController.mm +0 -211
  135. data/src/UtilityApple.mm +0 -63
  136. data/src/WindowUIKit.mm +0 -139
@@ -1,5 +1,4 @@
1
- #ifndef GOSU_SRC_GRAPHICS_COMMON_HPP
2
- #define GOSU_SRC_GRAPHICS_COMMON_HPP
1
+ #pragma once
3
2
 
4
3
  #include <Gosu/Bitmap.hpp>
5
4
  #include <Gosu/Graphics.hpp>
@@ -44,7 +43,7 @@ namespace Gosu
44
43
  {
45
44
  // No clipping
46
45
  return (width == NO_CLIPPING && other.width == NO_CLIPPING) ||
47
- // Clipping, but same
46
+ // Clipping, but same
48
47
  (x == other.x && y == other.y && width == other.width && height == other.height);
49
48
  }
50
49
  };
@@ -62,64 +61,62 @@ namespace Gosu
62
61
  class Macro;
63
62
  struct ArrayVertex
64
63
  {
65
- GLfloat texCoords[2];
64
+ GLfloat tex_coords[2];
66
65
  GLuint color;
67
66
  GLfloat vertices[3];
68
67
  };
69
68
 
70
69
  template<typename T>
71
- bool isPToTheLeftOfAB(T xa, T ya,
72
- T xb, T yb, T xp, T yp)
70
+ bool is_p_to_the_left_of_ab(T xa, T ya, T xb, T yb, T xp, T yp)
73
71
  {
74
72
  return (xb - xa) * (yp - ya) - (xp - xa) * (yb - ya) > 0;
75
73
  }
76
74
 
77
75
  template<typename T, typename C>
78
- void reorderCoordinatesIfNecessary(T& x1, T& y1,
79
- T& x2, T& y2, T& x3, T& y3, C& c3, T& x4, T& y4, C& c4)
76
+ void normalize_coordinates(T& x1, T& y1, T& x2, T& y2, T& x3, T& y3, C& c3, T& x4, T& y4, C& c4)
80
77
  {
81
- if (isPToTheLeftOfAB(x1, y1, x2, y2, x3, y3) ==
82
- isPToTheLeftOfAB(x2, y2, x3, y3, x4, y4))
83
- {
84
- std::swap(x3, x4);
85
- std::swap(y3, y4);
86
- std::swap(c3, c4);
78
+ if (is_p_to_the_left_of_ab(x1, y1, x2, y2, x3, y3) ==
79
+ is_p_to_the_left_of_ab(x2, y2, x3, y3, x4, y4)) {
80
+ using std::swap;
81
+ swap(x3, x4);
82
+ swap(y3, y4);
83
+ swap(c3, c4);
87
84
  }
88
85
  }
89
86
 
90
87
  template<typename Float>
91
- void applyTransform(const Transform& transform, Float& x, Float& y)
88
+ void apply_transform(const Transform& transform, Float& x, Float& y)
92
89
  {
93
- Float in[4] = { x, y, 0, 1 };
90
+ Float in[4] = { x, y, 0, 1 };
94
91
  Float out[4] = { 0, 0, 0, 0 };
95
- for (int i = 0; i < 4; ++i)
96
- for (int j = 0; j < 4; ++j)
92
+ for (int i = 0; i < 4; ++i) {
93
+ for (int j = 0; j < 4; ++j) {
97
94
  out[i] += in[j] * transform[j * 4 + i];
95
+ }
96
+ }
98
97
  x = out[0] / out[3];
99
98
  y = out[1] / out[3];
100
99
  }
101
100
 
102
- inline void multiplyBitmapAlpha(Bitmap& bmp, Color::Channel alpha)
101
+ inline void multiply_bitmap_alpha(Bitmap& bmp, Color::Channel alpha)
103
102
  {
104
- for (int y = 0; y < bmp.height(); ++y)
105
- for (int x = 0; x < bmp.width(); ++x)
106
- {
107
- Color c = bmp.getPixel(x, y);
108
- c.setAlpha(c.alpha() * alpha / 255);
109
- bmp.setPixel(x, y, c);
103
+ for (int y = 0; y < bmp.height(); ++y) {
104
+ for (int x = 0; x < bmp.width(); ++x) {
105
+ Color c = bmp.get_pixel(x, y);
106
+ c.set_alpha(c.alpha() * alpha / 255);
107
+ bmp.set_pixel(x, y, c);
110
108
  }
109
+ }
111
110
  }
112
111
 
113
112
  #ifdef GOSU_IS_IPHONE
114
- int clipRectBaseFactor();
113
+ int clip_rect_base_factor();
115
114
  #else
116
- inline int clipRectBaseFactor() { return 1; }
115
+ inline int clip_rect_base_factor() { return 1; }
117
116
  #endif
118
117
 
119
- bool isEntity(const std::wstring& name);
120
- const Bitmap& entityBitmap(const std::wstring& name);
118
+ bool is_entity(const std::string& name);
119
+ const Bitmap& entity_bitmap(const std::string& name);
121
120
 
122
- void ensureCurrentContext();
121
+ void ensure_current_context();
123
122
  }
124
-
125
- #endif
data/src/IO.cpp CHANGED
@@ -11,8 +11,9 @@ void Gosu::Reader::read(void* dest, std::size_t length)
11
11
  void Gosu::Writer::write(const void* source, std::size_t length)
12
12
  {
13
13
  // Try to resize the source if necessary.
14
- if (pos + length > res->size())
14
+ if (pos + length > res->size()) {
15
15
  res->resize(pos + length);
16
+ }
16
17
 
17
18
  res->write(pos, length, source);
18
19
  seek(length);
@@ -23,36 +24,36 @@ std::size_t Gosu::Buffer::size() const
23
24
  return buf.size();
24
25
  }
25
26
 
26
- void Gosu::Buffer::resize(std::size_t newSize)
27
+ void Gosu::Buffer::resize(std::size_t new_size)
27
28
  {
28
- buf.resize(newSize);
29
+ buf.resize(new_size);
29
30
  }
30
31
 
31
- void Gosu::Buffer::read(std::size_t offset, std::size_t length,
32
- void* destBuffer) const
32
+ void Gosu::Buffer::read(std::size_t offset, std::size_t length, void* dest_buffer) const
33
33
  {
34
- assert(offset + length <= size());
35
- if (length)
36
- std::memcpy(destBuffer, &buf[offset], length);
34
+ assert (offset + length <= size());
35
+ if (length) {
36
+ std::memcpy(dest_buffer, &buf[offset], length);
37
+ }
37
38
  }
38
39
 
39
- void Gosu::Buffer::write(std::size_t offset, std::size_t length,
40
- const void* sourceBuffer)
40
+ void Gosu::Buffer::write(std::size_t offset, std::size_t length, const void* source_buffer)
41
41
  {
42
- assert(offset + length <= size());
43
- if (length)
44
- std::memcpy(&buf[offset], sourceBuffer, length);
42
+ assert (offset + length <= size());
43
+ if (length) {
44
+ std::memcpy(&buf[offset], source_buffer, length);
45
+ }
45
46
  }
46
47
 
47
- void Gosu::loadFile(Buffer& buffer, const std::wstring& filename)
48
+ void Gosu::load_file(Buffer& buffer, const std::string& filename)
48
49
  {
49
50
  File file(filename);
50
51
  buffer.resize(file.size());
51
52
  file.read(0, buffer.size(), buffer.data());
52
53
  }
53
54
 
54
- void Gosu::saveFile(const Buffer& buffer, const std::wstring& filename)
55
+ void Gosu::save_file(const Buffer& buffer, const std::string& filename)
55
56
  {
56
- File file(filename, fmReplace);
57
+ File file(filename, FM_REPLACE);
57
58
  file.write(0, buffer.size(), buffer.data());
58
59
  }
@@ -1,5 +1,4 @@
1
- #ifndef GOSU_SRC_ICONV_HPP
2
- #define GOSU_SRC_ICONV_HPP
1
+ #pragma once
3
2
 
4
3
  #include <Gosu/Platform.hpp>
5
4
 
@@ -11,50 +10,42 @@ namespace Gosu
11
10
  template<typename Out, const char* to, const char* from, typename In>
12
11
  Out iconvert(const In& in)
13
12
  {
14
- if (in.empty())
15
- return Out();
16
-
17
- const size_t bufferLen = 128;
13
+ if (in.empty()) return Out();
14
+
15
+ const size_t buffer_len = 128;
18
16
  typedef typename In::value_type InElem;
19
17
  typedef typename Out::value_type OutElem;
20
18
 
21
19
  static iconv_t cd = iconv_open(to, from);
22
20
 
23
21
  Out result;
24
- OutElem buffer[bufferLen];
22
+ OutElem buffer[buffer_len];
25
23
 
26
24
  char* inbuf = const_cast<char*>(reinterpret_cast<const char*>(&in[0]));
27
25
  size_t inbytesleft = in.size() * sizeof(InElem);
28
26
  char* outbuf = reinterpret_cast<char*>(buffer);
29
27
  size_t outbytesleft = sizeof buffer;
30
28
 
31
- for (;;)
32
- {
29
+ for (;;) {
33
30
  size_t ret = ::iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
34
- if (ret == static_cast<size_t>(-1) && errno == EILSEQ)
35
- {
31
+ if (ret == static_cast<size_t>(-1) && errno == EILSEQ) {
36
32
  // Skip illegal sequence part, repeat loop.
37
33
  // TODO: Or retry w/ different encoding?
38
34
  ++inbuf;
39
35
  --inbytesleft;
40
36
  }
41
- else if (ret == static_cast<size_t>(-1) && errno == E2BIG)
42
- {
37
+ else if (ret == static_cast<size_t>(-1) && errno == E2BIG) {
43
38
  // Append new characters, reset out buffer, then repeat loop.
44
- result.insert(result.end(), buffer, buffer + bufferLen);
39
+ result.insert(result.end(), buffer, buffer + buffer_len);
45
40
  outbuf = reinterpret_cast<char*>(buffer);
46
41
  outbytesleft = sizeof buffer;
47
42
  }
48
- else
49
- {
43
+ else {
50
44
  // Append what's new in the buffer, then LEAVE loop.
51
- result.insert(result.end(), buffer, buffer + bufferLen - outbytesleft / sizeof(OutElem));
45
+ result.insert(result.end(), buffer,
46
+ buffer + buffer_len - outbytesleft / sizeof(OutElem));
52
47
  return result;
53
48
  }
54
- }
49
+ }
55
50
  }
56
51
  }
57
-
58
-
59
- #endif
60
-
@@ -0,0 +1,142 @@
1
+ #include <Gosu/Image.hpp>
2
+ #include <Gosu/Bitmap.hpp>
3
+ #include <Gosu/Graphics.hpp>
4
+ #include <Gosu/IO.hpp>
5
+ #include <Gosu/ImageData.hpp>
6
+ #include <Gosu/Math.hpp>
7
+ #include <stdexcept>
8
+
9
+ Gosu::Image::Image(const std::string& filename, unsigned flags)
10
+ {
11
+ // Forward.
12
+ Bitmap bmp;
13
+ load_image_file(bmp, filename);
14
+ Image(bmp, flags).data_.swap(data_);
15
+ }
16
+
17
+ Gosu::Image::Image(const std::string& filename, unsigned src_x, unsigned src_y, unsigned src_width,
18
+ unsigned src_height, unsigned flags)
19
+ {
20
+ // Forward.
21
+ Bitmap bmp;
22
+ load_image_file(bmp, filename);
23
+ Image(bmp, src_x, src_y, src_width, src_height, flags).data_.swap(data_);
24
+ }
25
+
26
+ Gosu::Image::Image(const Bitmap& source, unsigned flags)
27
+ {
28
+ // Forward.
29
+ Image(source, 0, 0, source.width(), source.height(), flags).data_.swap(data_);
30
+ }
31
+
32
+ Gosu::Image::Image(const Bitmap& source, unsigned src_x, unsigned src_y, unsigned src_width,
33
+ unsigned src_height, unsigned flags)
34
+ : data_(Graphics::create_image(source, src_x, src_y, src_width, src_height, flags))
35
+ {
36
+ }
37
+
38
+ Gosu::Image::Image(std::unique_ptr<ImageData>&& data)
39
+ : data_(data.release())
40
+ {
41
+ if (this->data_.get() == nullptr) {
42
+ throw std::invalid_argument("Gosu::Image cannot be initialized with nullptr");
43
+ }
44
+ }
45
+
46
+ unsigned Gosu::Image::width() const
47
+ {
48
+ return data_->width();
49
+ }
50
+
51
+ unsigned Gosu::Image::height() const
52
+ {
53
+ return data_->height();
54
+ }
55
+
56
+ void Gosu::Image::draw(double x, double y, ZPos z, double scale_x, double scale_y, Color c,
57
+ AlphaMode mode) const
58
+ {
59
+ double x2 = x + width() * scale_x;
60
+ double y2 = y + height() * scale_y;
61
+
62
+ data_->draw(x, y, c, x2, y, c, x, y2, c, x2, y2, c, z, mode);
63
+ }
64
+
65
+ void Gosu::Image::draw_mod(double x, double y, ZPos z, double scale_x, double scale_y, Color c1,
66
+ Color c2, Color c3, Color c4, AlphaMode mode) const
67
+ {
68
+ double x2 = x + width() * scale_x;
69
+ double y2 = y + height() * scale_y;
70
+
71
+ data_->draw(x, y, c1, x2, y, c2, x, y2, c3, x2, y2, c4, z, mode);
72
+ }
73
+
74
+ void Gosu::Image::draw_rot(double x, double y, ZPos z, double angle,
75
+ double center_x, double center_y, double scale_x, double scale_y, Color c, AlphaMode mode) const
76
+ {
77
+ double size_x = width() * scale_x;
78
+ double size_y = height() * scale_y;
79
+ double offs_x = offset_x(angle, 1);
80
+ double offs_y = offset_y(angle, 1);
81
+
82
+ // Offset to the centers of the original Image's edges after rotation.
83
+ double dist_to_left_x = +offs_y * size_x * center_x;
84
+ double dist_to_left_y = -offs_x * size_x * center_x;
85
+ double dist_to_right_x = -offs_y * size_x * (1 - center_x);
86
+ double dist_to_right_y = +offs_x * size_x * (1 - center_x);
87
+ double dist_to_top_x = +offs_x * size_y * center_y;
88
+ double dist_to_top_y = +offs_y * size_y * center_y;
89
+ double dist_to_bottom_x = -offs_x * size_y * (1 - center_y);
90
+ double dist_to_bottom_y = -offs_y * size_y * (1 - center_y);
91
+
92
+ data_->draw(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);
97
+ }
98
+
99
+ Gosu::ImageData& Gosu::Image::data() const
100
+ {
101
+ return *data_;
102
+ }
103
+
104
+ std::vector<Gosu::Image> Gosu::load_tiles(const Bitmap& bmp, int tile_width, int tile_height,
105
+ unsigned flags)
106
+ {
107
+ int tiles_x, tiles_y;
108
+ std::vector<Image> images;
109
+
110
+ if (tile_width > 0) {
111
+ tiles_x = bmp.width() / tile_width;
112
+ }
113
+ else {
114
+ tiles_x = -tile_width;
115
+ tile_width = bmp.width() / tiles_x;
116
+ }
117
+
118
+ if (tile_height > 0) {
119
+ tiles_y = bmp.height() / tile_height;
120
+ }
121
+ else {
122
+ tiles_y = -tile_height;
123
+ tile_height = bmp.height() / tiles_y;
124
+ }
125
+
126
+ for (int y = 0; y < tiles_y; ++y) {
127
+ for (int x = 0; x < tiles_x; ++x) {
128
+ images.emplace_back(bmp, x * tile_width, y * tile_height, tile_width, tile_height,
129
+ flags);
130
+ }
131
+ }
132
+
133
+ return images;
134
+ }
135
+
136
+ std::vector<Gosu::Image> Gosu::load_tiles(const std::string& filename, int tile_width,
137
+ int tile_height, unsigned flags)
138
+ {
139
+ Bitmap bmp;
140
+ load_image_file(bmp, filename);
141
+ return load_tiles(bmp, tile_width, tile_height, flags);
142
+ }
@@ -0,0 +1,459 @@
1
+ #include <Gosu/Platform.hpp>
2
+ #if !defined(GOSU_IS_IPHONE)
3
+
4
+ #include <Gosu/Input.hpp>
5
+ #include <Gosu/TextInput.hpp>
6
+ #include <Gosu/Utility.hpp>
7
+ #include <SDL.h>
8
+ #include <cwctype>
9
+ #include <cstdlib>
10
+ #include <algorithm>
11
+ #include <array>
12
+
13
+ namespace
14
+ {
15
+ void cleanup();
16
+
17
+ void require_sdl_video()
18
+ {
19
+ static bool initialized = false;
20
+ if (!initialized) {
21
+ SDL_InitSubSystem(SDL_INIT_VIDEO);
22
+ initialized = true;
23
+ std::atexit(cleanup);
24
+ }
25
+ }
26
+
27
+ void cleanup()
28
+ {
29
+ SDL_QuitSubSystem(SDL_INIT_VIDEO);
30
+ }
31
+
32
+ std::array<bool, Gosu::NUM_BUTTONS> button_states = { { false } };
33
+ }
34
+
35
+ struct Gosu::Input::Impl
36
+ {
37
+ Input& input;
38
+ SDL_Window* window;
39
+ TextInput* text_input;
40
+ double mouse_x, mouse_y;
41
+ double mouse_scale_x, mouse_scale_y;
42
+ double mouse_offset_x, mouse_offset_y;
43
+
44
+ Impl(Input& input, SDL_Window* window)
45
+ : input(input), window(window), text_input(nullptr)
46
+ {
47
+ mouse_scale_x = mouse_scale_y = 1;
48
+ mouse_offset_x = mouse_offset_y = 0;
49
+ }
50
+
51
+ void update_mouse_position()
52
+ {
53
+ // Do not use GetGlobalMouseState on Linux for now to prevent this bug:
54
+ // https://github.com/gosu/gosu/issues/326
55
+ // Once SDL 2.0.5 has been released, we can use this function as a workaround:
56
+ // https://wiki.libsdl.org/SDL_GetWindowBordersSize
57
+ #if SDL_VERSION_ATLEAST(2, 0, 4) && !defined(GOSU_IS_X)
58
+ int x, y, window_x, window_y;
59
+ SDL_GetWindowPosition(window, &window_x, &window_y);
60
+ SDL_GetGlobalMouseState(&x, &y);
61
+ mouse_x = x - window_x, mouse_y = y - window_y;
62
+ #else
63
+ int x, y;
64
+ SDL_GetMouseState(&x, &y);
65
+ mouse_x = x, mouse_y = y;
66
+ #endif
67
+ }
68
+
69
+ void set_mouse_position(double x, double y)
70
+ {
71
+ SDL_WarpMouseInWindow(window,
72
+ (x - mouse_offset_x) / mouse_scale_x,
73
+ (y - mouse_offset_y) / mouse_scale_y);
74
+
75
+ #if SDL_VERSION_ATLEAST(2, 0, 4) && !defined(GOSU_IS_X)
76
+ // On systems where we have a working GetGlobalMouseState, we can warp the mouse and
77
+ // retrieve its position directly afterwards.
78
+ update_mouse_position();
79
+ #else
80
+ // Otherwise, we have to assume that setting the position worked, because if we update the
81
+ // mouse position now, we'll get the previous position.
82
+ mouse_x = x, mouse_y = y;
83
+ #endif
84
+ }
85
+
86
+ void enqueue_event(int id, bool down)
87
+ {
88
+ event_queue.push_back(down ? id : ~id);
89
+ }
90
+
91
+ void dispatch_enqueued_events()
92
+ {
93
+ for (int event : event_queue) {
94
+ bool down = (event >= 0);
95
+ Button button(down ? event : ~event);
96
+
97
+ button_states[button.id()] = down;
98
+ if (down && input.on_button_down) {
99
+ input.on_button_down(button);
100
+ }
101
+ else if (!down && input.on_button_up) {
102
+ input.on_button_up(button);
103
+ }
104
+ }
105
+ event_queue.clear();
106
+ }
107
+
108
+ void initialize_gamepads()
109
+ {
110
+ SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER);
111
+
112
+ int num_gamepads = std::min<int>(Gosu::NUM_GAMEPADS, SDL_NumJoysticks());
113
+
114
+ for (int i = 0; i < num_gamepads; ++i) {
115
+ // Prefer the SDL_GameController API...
116
+ if (SDL_IsGameController(i)) {
117
+ SDL_GameController* game_controller = SDL_GameControllerOpen(i);
118
+ if (game_controller) {
119
+ game_controllers.push_back(game_controller);
120
+ continue;
121
+ }
122
+ }
123
+ // ...but fall back on the good, old SDL_Joystick API :)
124
+ SDL_Joystick* joystick = SDL_JoystickOpen(i);
125
+ if (joystick) {
126
+ joysticks.push_back(joystick);
127
+ }
128
+ }
129
+ }
130
+
131
+ void release_gamepads()
132
+ {
133
+ std::for_each(joysticks.begin(), joysticks.end(), &SDL_JoystickClose);
134
+ joysticks.clear();
135
+ std::for_each(game_controllers.begin(), game_controllers.end(), &SDL_GameControllerClose);
136
+ game_controllers.clear();
137
+
138
+ SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER);
139
+ }
140
+
141
+ typedef std::array<bool, GP_NUM_PER_GAMEPAD> GamepadBuffer;
142
+
143
+ void poll_gamepads()
144
+ {
145
+ // This gamepad is an OR-ed version of all the other gamepads. If button
146
+ // 3 is pressed on any attached gamepad, down(GP_BUTTON_3) will return
147
+ // true. This is handy for singleplayer games.
148
+ GamepadBuffer any_gamepad = { false };
149
+
150
+ std::size_t available_gamepads = game_controllers.size() + joysticks.size();
151
+
152
+ for (int i = 0; i < available_gamepads; ++i) {
153
+ GamepadBuffer current_gamepad = { false };
154
+
155
+ // Poll data from SDL, using either of two API interfaces.
156
+ if (i < game_controllers.size()) {
157
+ SDL_GameController* game_controller = game_controllers[i];
158
+ poll_game_controller(game_controller, current_gamepad);
159
+ }
160
+ else {
161
+ SDL_Joystick* joystick = joysticks[i - game_controllers.size()];
162
+ poll_joystick(joystick, current_gamepad);
163
+ }
164
+
165
+ // Now at the same time, enqueue all events for this particular
166
+ // gamepad, and OR the keyboard state into any_gamepad.
167
+ int offset = GP_RANGE_BEGIN + GP_NUM_PER_GAMEPAD * (i + 1);
168
+ for (int j = 0; j < current_gamepad.size(); ++j) {
169
+ any_gamepad[j] = any_gamepad[j] || current_gamepad[j];
170
+
171
+ if (current_gamepad[j] && !button_states[j + offset]) {
172
+ button_states[j + offset] = true;
173
+ enqueue_event(j + offset, true);
174
+ }
175
+ else if (!current_gamepad[j] && button_states[j + offset]) {
176
+ button_states[j + offset] = false;
177
+ enqueue_event(j + offset, false);
178
+ }
179
+ }
180
+ }
181
+
182
+ // And lastly, enqueue events for the virtual "any" gamepad.
183
+ for (int j = 0; j < any_gamepad.size(); ++j) {
184
+ if (any_gamepad[j] && !button_states[j + GP_RANGE_BEGIN]) {
185
+ button_states[j + GP_RANGE_BEGIN] = true;
186
+ enqueue_event(j + GP_RANGE_BEGIN, true);
187
+ }
188
+ else if (!any_gamepad[j] && button_states[j + GP_RANGE_BEGIN]) {
189
+ button_states[j + GP_RANGE_BEGIN] = false;
190
+ enqueue_event(j + GP_RANGE_BEGIN, false);
191
+ }
192
+ }
193
+ }
194
+
195
+ private:
196
+ // For button down event: Button name value (>= 0)
197
+ // For button up event: ~Button name value (< 0)
198
+ std::vector<int> event_queue;
199
+
200
+ std::vector<SDL_Joystick*> joysticks;
201
+ std::vector<SDL_GameController*> game_controllers;
202
+
203
+ // SDL returns axis values in the range -2^15 through 2^15-1, so we consider
204
+ // -2^14 through 2^14 the dead zone.
205
+
206
+ enum { DEAD_ZONE = (1 << 14) };
207
+
208
+ void poll_game_controller(SDL_GameController* controller, GamepadBuffer& gamepad)
209
+ {
210
+ gamepad[GP_LEFT - GP_RANGE_BEGIN] =
211
+ SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_DPAD_LEFT) ||
212
+ SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_LEFTX) < -DEAD_ZONE ||
213
+ SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_RIGHTX) < -DEAD_ZONE;
214
+
215
+ gamepad[GP_RIGHT - GP_RANGE_BEGIN] =
216
+ SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_DPAD_RIGHT) ||
217
+ SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_LEFTX) > +DEAD_ZONE ||
218
+ SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_RIGHTX) > +DEAD_ZONE;
219
+
220
+ gamepad[GP_UP - GP_RANGE_BEGIN] =
221
+ SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_DPAD_UP) ||
222
+ SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_LEFTY) < -DEAD_ZONE ||
223
+ SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_RIGHTY) < -DEAD_ZONE;
224
+
225
+ gamepad[GP_DOWN - GP_RANGE_BEGIN] =
226
+ SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_DPAD_DOWN) ||
227
+ SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_LEFTY) > +DEAD_ZONE ||
228
+ SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_RIGHTY) > +DEAD_ZONE;
229
+
230
+ int button = 0;
231
+ for (button; button < SDL_CONTROLLER_BUTTON_DPAD_UP; ++button) {
232
+ gamepad[GP_BUTTON_0 + button - GP_RANGE_BEGIN] =
233
+ SDL_GameControllerGetButton(controller, (SDL_GameControllerButton) button);
234
+ }
235
+ gamepad[GP_BUTTON_0 + button++ - GP_RANGE_BEGIN] =
236
+ SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_TRIGGERLEFT) > +DEAD_ZONE;
237
+ gamepad[GP_BUTTON_0 + button++ - GP_RANGE_BEGIN] =
238
+ SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_TRIGGERRIGHT) > +DEAD_ZONE;
239
+ }
240
+
241
+ void poll_joystick(SDL_Joystick* joystick, GamepadBuffer& gamepad)
242
+ {
243
+ int axes = SDL_JoystickNumAxes(joystick);
244
+ int hats = SDL_JoystickNumHats(joystick);
245
+ int buttons = std::min<int>(GP_NUM_PER_GAMEPAD - 4, SDL_JoystickNumButtons(joystick));
246
+
247
+ for (int axis = 0; axis < axes; ++axis) {
248
+ Sint16 value = SDL_JoystickGetAxis(joystick, axis);
249
+
250
+ if (value < -DEAD_ZONE) {
251
+ if (axis % 2 == 0) {
252
+ gamepad[GP_LEFT - GP_RANGE_BEGIN] = true;
253
+ }
254
+ else {
255
+ gamepad[GP_UP - GP_RANGE_BEGIN] = true;
256
+ }
257
+ }
258
+ else if (value > +DEAD_ZONE) {
259
+ if (axis % 2 == 0) {
260
+ gamepad[GP_RIGHT - GP_RANGE_BEGIN] = true;
261
+ }
262
+ else {
263
+ gamepad[GP_DOWN - GP_RANGE_BEGIN] = true;
264
+ }
265
+ }
266
+ }
267
+
268
+ for (int hat = 0; hat < hats; ++hat) {
269
+ Uint8 value = SDL_JoystickGetHat(joystick, hat);
270
+
271
+ if (value & SDL_HAT_LEFT) {
272
+ gamepad[GP_LEFT - GP_RANGE_BEGIN] = true;
273
+ }
274
+ if (value & SDL_HAT_RIGHT) {
275
+ gamepad[GP_RIGHT - GP_RANGE_BEGIN] = true;
276
+ }
277
+ if (value & SDL_HAT_UP) {
278
+ gamepad[GP_UP - GP_RANGE_BEGIN] = true;
279
+ }
280
+ if (value & SDL_HAT_DOWN) {
281
+ gamepad[GP_DOWN - GP_RANGE_BEGIN] = true;
282
+ }
283
+ }
284
+
285
+ for (int button = 0; button < buttons; ++button) {
286
+ if (SDL_JoystickGetButton(joystick, button)) {
287
+ gamepad[GP_BUTTON_0 + button - GP_RANGE_BEGIN] = true;
288
+ }
289
+ }
290
+ }
291
+ };
292
+
293
+ Gosu::Input::Input(void* window)
294
+ : pimpl(new Impl(*this, (SDL_Window*)window))
295
+ {
296
+ require_sdl_video();
297
+
298
+ pimpl->initialize_gamepads();
299
+ }
300
+
301
+ Gosu::Input::~Input()
302
+ {
303
+ pimpl->release_gamepads();
304
+ }
305
+
306
+ bool Gosu::Input::feed_sdl_event(void* event)
307
+ {
308
+ const SDL_Event* e = static_cast<SDL_Event*>(event);
309
+
310
+ if (pimpl->text_input && pimpl->text_input->feed_sdl_event(event)) {
311
+ return true;
312
+ }
313
+
314
+ switch (e->type) {
315
+ case SDL_KEYDOWN:
316
+ case SDL_KEYUP: {
317
+ if (e->key.repeat == 0 && e->key.keysym.scancode <= KB_RANGE_END) {
318
+ pimpl->enqueue_event(e->key.keysym.scancode, e->type == SDL_KEYDOWN);
319
+ return true;
320
+ }
321
+ break;
322
+ }
323
+ case SDL_MOUSEBUTTONDOWN:
324
+ case SDL_MOUSEBUTTONUP: {
325
+ if (e->button.button >= 1 && e->button.button <= 3) {
326
+ pimpl->enqueue_event(MS_LEFT + e->button.button - 1, e->type == SDL_MOUSEBUTTONDOWN);
327
+ return true;
328
+ }
329
+ break;
330
+ }
331
+ case SDL_MOUSEWHEEL: {
332
+ if (e->wheel.y > 0) {
333
+ pimpl->enqueue_event(MS_WHEEL_UP, true);
334
+ pimpl->enqueue_event(MS_WHEEL_UP, false);
335
+ return true;
336
+ }
337
+ else if (e->wheel.y < 0) {
338
+ pimpl->enqueue_event(MS_WHEEL_DOWN, true);
339
+ pimpl->enqueue_event(MS_WHEEL_DOWN, false);
340
+ return true;
341
+ }
342
+ break;
343
+ }
344
+ }
345
+ return false;
346
+ }
347
+
348
+ std::string Gosu::Input::id_to_char(Button btn)
349
+ {
350
+ require_sdl_video();
351
+
352
+ if (btn.id() > KB_RANGE_END) return "";
353
+
354
+ // SDL_GetKeyName returns "Space" for this value, but we want the character value.
355
+ if (btn.id() == KB_SPACE) return " ";
356
+
357
+ SDL_Keycode keycode = SDL_GetKeyFromScancode(static_cast<SDL_Scancode>(btn.id()));
358
+ if (keycode == SDLK_UNKNOWN) return "";
359
+
360
+ const char* name = SDL_GetKeyName(keycode);
361
+ if (name == nullptr) return "";
362
+
363
+ std::wstring wname = utf8_to_wstring(name);
364
+ if (wname.length() != 1) return "";
365
+
366
+ // Convert to lower case to be consistent with previous versions of Gosu.
367
+ // German umlauts are already reported in lower-case by SDL, anyway.
368
+ // (This should handle Turkish i/I just fine because it uses the current locale.)
369
+ wname[0] = (wchar_t)std::towlower((int)wname[0]);
370
+ return wstring_to_utf8(wname);
371
+ }
372
+
373
+ Gosu::Button Gosu::Input::char_to_id(std::string ch)
374
+ {
375
+ require_sdl_video();
376
+
377
+ SDL_Keycode keycode = SDL_GetKeyFromName(ch.c_str());
378
+ return keycode == SDLK_UNKNOWN ? NO_BUTTON : Button(SDL_GetScancodeFromKey(keycode));
379
+ }
380
+
381
+ bool Gosu::Input::down(Gosu::Button btn)
382
+ {
383
+ if (btn == NO_BUTTON || btn.id() >= NUM_BUTTONS) return false;
384
+
385
+ return button_states[btn.id()];
386
+ }
387
+
388
+ double Gosu::Input::mouse_x() const
389
+ {
390
+ return pimpl->mouse_x * pimpl->mouse_scale_x + pimpl->mouse_offset_x;
391
+ }
392
+
393
+ double Gosu::Input::mouse_y() const
394
+ {
395
+ return pimpl->mouse_y * pimpl->mouse_scale_y + pimpl->mouse_offset_y;
396
+ }
397
+
398
+ void Gosu::Input::set_mouse_position(double x, double y)
399
+ {
400
+ pimpl->set_mouse_position(x, y);
401
+ }
402
+
403
+ void Gosu::Input::set_mouse_factors(double scale_x, double scale_y,
404
+ double black_bar_width, double black_bar_height)
405
+ {
406
+ pimpl->mouse_scale_x = scale_x;
407
+ pimpl->mouse_scale_y = scale_y;
408
+ pimpl->mouse_offset_x = -black_bar_width;
409
+ pimpl->mouse_offset_y = -black_bar_height;
410
+ }
411
+
412
+ const Gosu::Touches& Gosu::Input::current_touches() const
413
+ {
414
+ // Note: We can actually use the SDL's touch API to implement this, even on macOS! Neat.
415
+
416
+ static Gosu::Touches none;
417
+ return none;
418
+ }
419
+
420
+ double Gosu::Input::accelerometer_x() const
421
+ {
422
+ return 0.0;
423
+ }
424
+
425
+ double Gosu::Input::accelerometer_y() const
426
+ {
427
+ return 0.0;
428
+ }
429
+
430
+ double Gosu::Input::accelerometer_z() const
431
+ {
432
+ return 0.0;
433
+ }
434
+
435
+ void Gosu::Input::update()
436
+ {
437
+ pimpl->update_mouse_position();
438
+ pimpl->dispatch_enqueued_events();
439
+ pimpl->poll_gamepads();
440
+ }
441
+
442
+ Gosu::TextInput* Gosu::Input::text_input() const
443
+ {
444
+ return pimpl->text_input;
445
+ }
446
+
447
+ void Gosu::Input::set_text_input(TextInput* text_input)
448
+ {
449
+ if (pimpl->text_input && text_input == nullptr) {
450
+ SDL_StopTextInput();
451
+ }
452
+ else if (pimpl->text_input == nullptr && text_input) {
453
+ SDL_StartTextInput();
454
+ }
455
+
456
+ pimpl->text_input = text_input;
457
+ }
458
+
459
+ #endif