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
@@ -0,0 +1,57 @@
1
+ #pragma once
2
+
3
+ #include <Gosu/Platform.hpp>
4
+ #include <vector>
5
+ #ifdef GOSU_IS_MAC
6
+ #include <OpenAL/al.h>
7
+ #else
8
+ #include <AL/al.h>
9
+ #endif
10
+
11
+ namespace Gosu
12
+ {
13
+ class AudioFile
14
+ {
15
+ AudioFile(const AudioFile&) = delete;
16
+ AudioFile& operator=(const AudioFile&) = delete;
17
+ AudioFile(AudioFile&&) = delete;
18
+ AudioFile& operator=(AudioFile&&) = delete;
19
+
20
+ std::vector<char> data_;
21
+
22
+ public:
23
+ AudioFile() {}
24
+
25
+ virtual ~AudioFile() {}
26
+
27
+ virtual ALenum format() const = 0;
28
+
29
+ virtual ALuint sample_rate() const = 0;
30
+
31
+ virtual std::size_t read_data(void* dest, std::size_t length) = 0;
32
+
33
+ virtual void rewind() = 0;
34
+
35
+ const std::vector<char>& decoded_data()
36
+ {
37
+ static const unsigned INCREMENT = 512 * 1024;
38
+
39
+ if (!data_.empty()) {
40
+ return data_;
41
+ }
42
+
43
+ for (;;) {
44
+ data_.resize(data_.size() + INCREMENT);
45
+
46
+ int read_bytes = read_data(&data_[data_.size() - INCREMENT], INCREMENT);
47
+
48
+ if (read_bytes < INCREMENT) {
49
+ data_.resize(data_.size() - INCREMENT + read_bytes);
50
+ break;
51
+ }
52
+ }
53
+
54
+ return data_;
55
+ }
56
+ };
57
+ }
@@ -0,0 +1,214 @@
1
+ #pragma once
2
+
3
+ #import "AudioFile.hpp"
4
+ #import <AudioToolbox/AudioToolbox.h>
5
+ #import <AudioToolbox/AudioConverter.h>
6
+ #import <AudioToolbox/ExtendedAudioFile.h>
7
+ #import <CoreFoundation/CoreFoundation.h>
8
+ #import <Foundation/Foundation.h>
9
+ #import <Gosu/IO.hpp>
10
+ #import <Gosu/Platform.hpp>
11
+ #import <Gosu/Utility.hpp>
12
+ #import <OpenAL/al.h>
13
+ #import <algorithm>
14
+ #import <arpa/inet.h>
15
+ #import <stdexcept>
16
+ #import <vector>
17
+
18
+ inline static void throw_os_error(OSStatus status, unsigned line)
19
+ {
20
+ std::string what;
21
+ @autoreleasepool {
22
+ NSError* error = [NSError errorWithDomain:NSOSStatusErrorDomain code:status userInfo:nil];
23
+ NSString* message = [NSString stringWithFormat:@"Error 0x%x on line %u: %@", line, status,
24
+ error.localizedDescription];
25
+ what = message.UTF8String;
26
+ }
27
+ throw std::runtime_error(what);
28
+ }
29
+
30
+ #define CHECK_OS(status) do { if (status) throw_os_error(status, __LINE__); } while (0)
31
+
32
+ namespace Gosu
33
+ {
34
+ class AudioToolboxFile : public AudioFile
35
+ {
36
+ Gosu::Buffer buffer_;
37
+ AudioFileID file_id_;
38
+ ExtAudioFileRef file_;
39
+ SInt64 position_;
40
+ SInt64 seek_offset_;
41
+
42
+ ALenum format_;
43
+ ALuint sample_rate_;
44
+ UInt32 bytes_per_frame_;
45
+ bool big_endian_;
46
+
47
+ static OSStatus AudioFile_ReadProc(void* in_client_data, SInt64 in_position,
48
+ UInt32 request_count, void* buffer, UInt32* actual_count)
49
+ {
50
+ const Resource& res = *static_cast<Resource*>(in_client_data);
51
+ *actual_count = std::min<UInt32>(request_count, res.size() - in_position);
52
+ res.read(in_position, *actual_count, buffer);
53
+ return noErr;
54
+ }
55
+
56
+ static SInt64 AudioFile_GetSizeProc(void* in_client_data)
57
+ {
58
+ const Resource& res = *static_cast<Resource*>(in_client_data);
59
+ return res.size();
60
+ }
61
+
62
+ void init_seek_offset()
63
+ {
64
+ AudioConverterRef ac_ref;
65
+ UInt32 acr_size = sizeof ac_ref;
66
+ CHECK_OS(ExtAudioFileGetProperty(file_, kExtAudioFileProperty_AudioConverter,
67
+ &acr_size, &ac_ref));
68
+
69
+ AudioConverterPrimeInfo prime_info;
70
+ UInt32 pi_size = sizeof prime_info;
71
+ OSStatus result = AudioConverterGetProperty(ac_ref, kAudioConverterPrimeInfo,
72
+ &pi_size, &prime_info);
73
+ if (result != kAudioConverterErr_PropertyNotSupported) {
74
+ CHECK_OS(result);
75
+ seek_offset_ = prime_info.leadingFrames;
76
+ }
77
+ }
78
+
79
+ void init_client_format_based_on(const AudioStreamBasicDescription& base)
80
+ {
81
+ AudioStreamBasicDescription client_data = { 0 };
82
+ sample_rate_ = client_data.mSampleRate = 22050;
83
+ client_data.mFormatID = kAudioFormatLinearPCM;
84
+ client_data.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsPacked;
85
+ client_data.mBitsPerChannel = 16;
86
+ client_data.mChannelsPerFrame = base.mChannelsPerFrame;
87
+ client_data.mFramesPerPacket = 1;
88
+ client_data.mBytesPerPacket =
89
+ client_data.mBytesPerFrame =
90
+ client_data.mChannelsPerFrame * client_data.mBitsPerChannel / 8;
91
+ CHECK_OS(ExtAudioFileSetProperty(file_, kExtAudioFileProperty_ClientDataFormat,
92
+ sizeof client_data, &client_data));
93
+
94
+ init_seek_offset();
95
+
96
+ format_ = client_data.mChannelsPerFrame == 1 ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
97
+ }
98
+
99
+ void init()
100
+ {
101
+ // Streaming starts at beginning
102
+ position_ = 0;
103
+
104
+ // Unless overridden later, assume that the index into seek() is 0-based
105
+ seek_offset_ = 0;
106
+
107
+ AudioStreamBasicDescription desc;
108
+ UInt32 size_of_property = sizeof desc;
109
+ CHECK_OS(ExtAudioFileGetProperty(file_, kExtAudioFileProperty_FileDataFormat,
110
+ &size_of_property, &desc));
111
+
112
+ // Sample rate for OpenAL
113
+ sample_rate_ = desc.mSampleRate;
114
+
115
+ // Sanity checks
116
+ if (desc.mFormatFlags & kAudioFormatFlagIsNonInterleaved) {
117
+ throw std::runtime_error("Non-interleaved formats are unsupported");
118
+ }
119
+
120
+ // Easy formats
121
+ format_ = 0;
122
+ if (desc.mChannelsPerFrame == 1) {
123
+ /*if (desc.mBitsPerChannel == 8)
124
+ format_ = AL_FORMAT_MONO8;
125
+ else*/ if (desc.mBitsPerChannel == 16)
126
+ format_ = AL_FORMAT_MONO16;
127
+ }
128
+ else if (desc.mChannelsPerFrame == 2) {
129
+ /*if (desc.mBitsPerChannel == 8)
130
+ format_ = AL_FORMAT_STEREO8;
131
+ else */if (desc.mBitsPerChannel == 16)
132
+ format_ = AL_FORMAT_STEREO16;
133
+ }
134
+
135
+ if (format_ == 0 ||
136
+ // If format not native for OpenAL, set client data format
137
+ // to enable conversion
138
+ desc.mFormatFlags & kAudioFormatFlagIsBigEndian ||
139
+ desc.mFormatFlags & kAudioFormatFlagIsFloat ||
140
+ !(desc.mFormatFlags & kAudioFormatFlagIsSignedInteger)) {
141
+ init_client_format_based_on(desc);
142
+ }
143
+ else {
144
+ // Just set the old format as the client format so
145
+ // ExtAudioFileSeek will work for us.
146
+ CHECK_OS(ExtAudioFileSetProperty(file_, kExtAudioFileProperty_ClientDataFormat,
147
+ sizeof desc, &desc));
148
+ }
149
+ }
150
+
151
+ public:
152
+ AudioToolboxFile(const std::string& filename)
153
+ {
154
+ NSURL* URL = [NSURL fileURLWithPath:[NSString stringWithUTF8String:filename.c_str()]];
155
+ CHECK_OS(ExtAudioFileOpenURL((__bridge CFURLRef) URL, &file_));
156
+
157
+ file_id_ = 0;
158
+
159
+ init();
160
+ }
161
+
162
+ AudioToolboxFile(Gosu::Reader reader)
163
+ {
164
+ buffer_.resize(reader.resource().size() - reader.position());
165
+ reader.read(buffer_.data(), buffer_.size());
166
+
167
+ // TODO: This fails on iOS with MP3 files.
168
+ // TODO: ^ Is the comment above still true on non-ancient iOS versions?
169
+
170
+ void* client_data = &buffer_;
171
+ CHECK_OS(AudioFileOpenWithCallbacks(client_data, AudioFile_ReadProc, 0,
172
+ AudioFile_GetSizeProc, 0, 0, &file_id_));
173
+ CHECK_OS(ExtAudioFileWrapAudioFileID(file_id_, false, &file_));
174
+
175
+ init();
176
+ }
177
+
178
+ ~AudioToolboxFile()
179
+ {
180
+ ExtAudioFileDispose(file_);
181
+
182
+ if (file_id_) {
183
+ AudioFileClose(file_id_);
184
+ }
185
+ }
186
+
187
+ ALenum format() const
188
+ {
189
+ return format_;
190
+ }
191
+
192
+ ALuint sample_rate() const
193
+ {
194
+ return sample_rate_;
195
+ }
196
+
197
+ void rewind()
198
+ {
199
+ CHECK_OS(ExtAudioFileSeek(file_, 0 + seek_offset_));
200
+ }
201
+
202
+ std::size_t read_data(void* dest, size_t length)
203
+ {
204
+ AudioBufferList abl;
205
+ abl.mNumberBuffers = 1;
206
+ abl.mBuffers[0].mNumberChannels = 1;
207
+ abl.mBuffers[0].mDataByteSize = length;
208
+ abl.mBuffers[0].mData = dest;
209
+ UInt32 numFrames = 0xffffffff; // give us as many frames as possible given our buffer
210
+ CHECK_OS(ExtAudioFileRead(file_, &numFrames, &abl));
211
+ return abl.mBuffers[0].mDataByteSize;
212
+ }
213
+ };
214
+ }
@@ -0,0 +1,159 @@
1
+ #include <Gosu/Bitmap.hpp>
2
+ #include <cassert>
3
+ #include <algorithm>
4
+ #include <vector>
5
+
6
+ void Gosu::Bitmap::swap(Bitmap& other)
7
+ {
8
+ std::swap(pixels, other.pixels);
9
+ std::swap(w, other.w);
10
+ std::swap(h, other.h);
11
+ }
12
+
13
+ void Gosu::Bitmap::resize(unsigned width, unsigned height, Color c)
14
+ {
15
+ if (width == w && height == h)
16
+ return;
17
+
18
+ Bitmap temp(width, height, c);
19
+ temp.insert(*this, 0, 0);
20
+ swap(temp);
21
+ }
22
+
23
+ void Gosu::Bitmap::insert(const Bitmap& source, int x, int y)
24
+ {
25
+ insert(source, x, y, 0, 0, source.width(), source.height());
26
+ }
27
+
28
+ void Gosu::Bitmap::insert(const Bitmap& source, int x, int y, unsigned src_x, unsigned src_y,
29
+ unsigned src_width, unsigned src_height)
30
+ {
31
+ // TODO: This should use memcpy if possible (x == 0 && src_width == this->width())
32
+
33
+ if (x < 0) {
34
+ unsigned clip_left = -x;
35
+
36
+ if (clip_left >= src_width)
37
+ return;
38
+
39
+ src_x += clip_left;
40
+ src_width -= clip_left;
41
+ x = 0;
42
+ }
43
+
44
+ if (y < 0) {
45
+ unsigned clip_top = -y;
46
+
47
+ if (clip_top >= src_height)
48
+ return;
49
+
50
+ src_y += clip_top;
51
+ src_height -= clip_top;
52
+ y = 0;
53
+ }
54
+
55
+ if (x + src_width > w) {
56
+ if (static_cast<unsigned>(x) >= w)
57
+ return;
58
+
59
+ src_width = w - x;
60
+ }
61
+
62
+ if (y + src_height > h) {
63
+ if (static_cast<unsigned>(y) >= h)
64
+ return;
65
+
66
+ src_height = h - y;
67
+ }
68
+
69
+ for (unsigned rel_y = 0; rel_y < src_height; ++rel_y)
70
+ for (unsigned rel_x = 0; rel_x < src_width; ++rel_x)
71
+ set_pixel(x + rel_x, y + rel_y, source.get_pixel(src_x + rel_x, src_y + rel_y));
72
+ }
73
+
74
+ void Gosu::apply_color_key(Bitmap& bitmap, Color key)
75
+ {
76
+ std::vector<Color> surrounding_colors;
77
+ surrounding_colors.reserve(4);
78
+
79
+ for (unsigned y = 0; y < bitmap.height(); ++y)
80
+ for (unsigned x = 0; x < bitmap.width(); ++x)
81
+ if (bitmap.get_pixel(x, y) == key) {
82
+ surrounding_colors.clear();
83
+ if (x > 0 && bitmap.get_pixel(x - 1, y) != key)
84
+ surrounding_colors.push_back(bitmap.get_pixel(x - 1, y));
85
+ if (x < bitmap.width() - 1 && bitmap.get_pixel(x + 1, y) != key)
86
+ surrounding_colors.push_back(bitmap.get_pixel(x + 1, y));
87
+ if (y > 0 && bitmap.get_pixel(x, y - 1) != key)
88
+ surrounding_colors.push_back(bitmap.get_pixel(x, y - 1));
89
+ if (y < bitmap.height() - 1 && bitmap.get_pixel(x, y + 1) != key)
90
+ surrounding_colors.push_back(bitmap.get_pixel(x, y + 1));
91
+
92
+ if (surrounding_colors.empty()) {
93
+ bitmap.set_pixel(x, y, Color::NONE);
94
+ continue;
95
+ }
96
+
97
+ unsigned red = 0, green = 0, blue = 0;
98
+ for (auto& color : surrounding_colors) {
99
+ red += color.red();
100
+ green += color.green();
101
+ blue += color.blue();
102
+ }
103
+ bitmap.set_pixel(x, y, Color(0, red / surrounding_colors.size(),
104
+ green / surrounding_colors.size(), blue / surrounding_colors.size()));
105
+ }
106
+ }
107
+
108
+ void Gosu::unapply_color_key(Bitmap& bitmap, Color color)
109
+ {
110
+ Color* p = bitmap.data();
111
+ for (int i = bitmap.width() * bitmap.height(); i > 0; --i, ++p)
112
+ if (p->alpha() == 0)
113
+ *p = color;
114
+ else
115
+ p->set_alpha(255);
116
+ }
117
+
118
+ void Gosu::apply_border_flags(Bitmap& dest, const Bitmap& source, unsigned src_x, unsigned src_y,
119
+ unsigned src_width, unsigned src_height, unsigned image_flags)
120
+ {
121
+ // Backwards compatibility: This used to be 'bool tileable'.
122
+ if (image_flags == 1)
123
+ image_flags = IF_TILEABLE;
124
+
125
+ dest.resize(src_width + 2, src_height + 2);
126
+
127
+ // The borders are made "harder" by duplicating the original bitmap's
128
+ // borders.
129
+
130
+ // Top.
131
+ if (image_flags & IF_TILEABLE_TOP)
132
+ dest.insert(source, 1, 0, src_x, src_y, src_width, 1);
133
+ // Bottom.
134
+ if (image_flags & IF_TILEABLE_BOTTOM)
135
+ dest.insert(source, 1, dest.height() - 1, src_x, src_y + src_height - 1, src_width, 1);
136
+ // Left.
137
+ if (image_flags & IF_TILEABLE_LEFT)
138
+ dest.insert(source, 0, 1, src_x, src_y, 1, src_height);
139
+ // Right.
140
+ if (image_flags & IF_TILEABLE_RIGHT)
141
+ dest.insert(source, dest.width() - 1, 1, src_x + src_width - 1, src_y, 1, src_height);
142
+
143
+ // Top left.
144
+ if ((image_flags & IF_TILEABLE_TOP) && (image_flags & IF_TILEABLE_LEFT))
145
+ dest.set_pixel(0, 0, source.get_pixel(src_x, src_y));
146
+ // Top right.
147
+ if ((image_flags & IF_TILEABLE_TOP) && (image_flags & IF_TILEABLE_RIGHT))
148
+ dest.set_pixel(dest.width() - 1, 0, source.get_pixel(src_x + src_width - 1, src_y));
149
+ // Bottom left.
150
+ if ((image_flags & IF_TILEABLE_BOTTOM) && (image_flags & IF_TILEABLE_LEFT))
151
+ dest.set_pixel(0, dest.height() - 1, source.get_pixel(src_x, src_y + src_height - 1));
152
+ // Bottom right.
153
+ if ((image_flags & IF_TILEABLE_BOTTOM) && (image_flags & IF_TILEABLE_RIGHT))
154
+ dest.set_pixel(dest.width() - 1, dest.height() - 1,
155
+ source.get_pixel(src_x + src_width - 1, src_y + src_height - 1));
156
+
157
+ // Now put the final image into the prepared borders.
158
+ dest.insert(source, 1, 1, src_x, src_y, src_width, src_height);
159
+ }
@@ -0,0 +1,141 @@
1
+ #include <Gosu/Bitmap.hpp>
2
+ #include <Gosu/IO.hpp>
3
+ #include <Gosu/Platform.hpp>
4
+ #include <Gosu/Utility.hpp>
5
+ #include <cstring>
6
+ #include <stdexcept>
7
+
8
+ #define STB_IMAGE_IMPLEMENTATION
9
+ #define STBI_NO_STDIO
10
+ #define STBI_NO_LINEAR
11
+
12
+ // Work around this bug:
13
+ // https://bugs.launchpad.net/ubuntu/+source/gcc-5/+bug/1568899
14
+ #ifdef GOSU_IS_X
15
+ #define STBI_NO_SIMD
16
+ #endif
17
+
18
+ #include "stb_image.h"
19
+
20
+ namespace
21
+ {
22
+ int read_callback(void* user, char* data, int size)
23
+ {
24
+ Gosu::Reader* reader = static_cast<Gosu::Reader*>(user);
25
+ std::size_t remaining = reader->resource().size() - reader->position();
26
+ std::size_t actual_size = (size < remaining ? size : remaining);
27
+ reader->read(data, actual_size);
28
+ return actual_size;
29
+ }
30
+
31
+ void skip_callback(void* user, int n)
32
+ {
33
+ Gosu::Reader* reader = static_cast<Gosu::Reader*>(user);
34
+ reader->set_position(reader->position() + n);
35
+ }
36
+
37
+ int eof_callback(void* user)
38
+ {
39
+ Gosu::Reader* reader = static_cast<Gosu::Reader*>(user);
40
+ return reader->position() == reader->resource().size();
41
+ }
42
+
43
+ bool is_bmp(Gosu::Reader reader)
44
+ {
45
+ std::size_t remaining = reader.resource().size() - reader.position();
46
+ if (remaining < 2) {
47
+ return false;
48
+ }
49
+ char magic_bytes[2];
50
+ reader.read(magic_bytes, sizeof magic_bytes);
51
+ reader.seek(sizeof magic_bytes);
52
+ return magic_bytes[0] == 'B' && magic_bytes[1] == 'M';
53
+ }
54
+ }
55
+
56
+ void Gosu::load_image_file(Gosu::Bitmap& bitmap, const std::string& filename)
57
+ {
58
+ Buffer buffer;
59
+ load_file(buffer, filename);
60
+ load_image_file(bitmap, buffer.front_reader());
61
+ }
62
+
63
+ void Gosu::load_image_file(Gosu::Bitmap& bitmap, Reader input)
64
+ {
65
+ bool needs_color_key = is_bmp(input);
66
+
67
+ stbi_io_callbacks callbacks;
68
+ callbacks.read = read_callback;
69
+ callbacks.skip = skip_callback;
70
+ callbacks.eof = eof_callback;
71
+ int x, y, n;
72
+ stbi_uc* bytes = stbi_load_from_callbacks(&callbacks, &input, &x, &y, &n, STBI_rgb_alpha);
73
+
74
+ if (bytes == nullptr) {
75
+ // TODO - stbi_failure_reason is not thread safe. Everything here should be wrapped in a
76
+ // mutex.
77
+ throw std::runtime_error("Cannot load image: " + std::string(stbi_failure_reason()));
78
+ }
79
+
80
+ bitmap.resize(x, y);
81
+ std::memcpy(bitmap.data(), bytes, x * y * sizeof(Gosu::Color));
82
+
83
+ stbi_image_free(bytes);
84
+
85
+ if (needs_color_key) {
86
+ apply_color_key(bitmap, Gosu::Color::FUCHSIA);
87
+ }
88
+ }
89
+
90
+ #define STB_IMAGE_WRITE_IMPLEMENTATION
91
+ #include "stb_image_write.h"
92
+
93
+ void Gosu::save_image_file(const Gosu::Bitmap& bitmap, const std::string& filename)
94
+ {
95
+ int ok;
96
+
97
+ if (has_extension(filename, "bmp")) {
98
+ ok = stbi_write_bmp(filename.c_str(), bitmap.width(), bitmap.height(), 4, bitmap.data());
99
+ }
100
+ else if (has_extension(filename, "tga")) {
101
+ ok = stbi_write_tga(filename.c_str(), bitmap.width(), bitmap.height(), 4, bitmap.data());
102
+ }
103
+ else {
104
+ ok = stbi_write_png(filename.c_str(), bitmap.width(), bitmap.height(), 4, bitmap.data(), 0);
105
+ }
106
+
107
+ if (ok == 0) {
108
+ throw std::runtime_error("Could not save image data to file: " + filename);
109
+ }
110
+ }
111
+
112
+ static void stbi_write_to_writer(void* context, void* data, int size)
113
+ {
114
+ Gosu::Writer* writer = reinterpret_cast<Gosu::Writer*>(context);
115
+ writer->write(data, size);
116
+ }
117
+
118
+ void Gosu::save_image_file(const Gosu::Bitmap& bitmap, Gosu::Writer writer,
119
+ const std::string& format_hint)
120
+ {
121
+ int ok;
122
+
123
+ if (has_extension(format_hint, "bmp")) {
124
+ ok = stbi_write_bmp_to_func(stbi_write_to_writer, &writer, bitmap.width(), bitmap.height(),
125
+ 4, bitmap.data());
126
+ }
127
+ else if (has_extension(format_hint, "tga")) {
128
+ stbi_write_tga_with_rle = 0;
129
+ ok = stbi_write_tga_to_func(stbi_write_to_writer, &writer, bitmap.width(), bitmap.height(),
130
+ 4, bitmap.data());
131
+ }
132
+ else {
133
+ ok = stbi_write_png_to_func(stbi_write_to_writer, &writer, bitmap.width(), bitmap.height(),
134
+ 4, bitmap.data(), 0);
135
+ }
136
+
137
+ if (ok == 0) {
138
+ throw std::runtime_error("Could not save image data to memory (format hint = '" +
139
+ format_hint + "'");
140
+ }
141
+ }