gosu 0.9.2 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (84) hide show
  1. checksums.yaml +4 -4
  2. data/Gosu/Bitmap.hpp +3 -3
  3. data/Gosu/Directories.hpp +6 -3
  4. data/Gosu/Gosu.hpp +0 -1
  5. data/Gosu/GraphicsBase.hpp +12 -8
  6. data/Gosu/Input.hpp +5 -16
  7. data/Gosu/Platform.hpp +1 -0
  8. data/Gosu/Version.hpp +3 -5
  9. data/Gosu/Window.hpp +7 -8
  10. data/README.txt +3 -3
  11. data/ext/gosu/extconf.rb +17 -16
  12. data/ext/gosu/gosu_wrap.cxx +59 -58
  13. data/ext/gosu/gosu_wrap.h +1 -1
  14. data/rdoc/gosu.rb +285 -283
  15. data/src/{MacUtility.hpp → AppleUtility.hpp} +24 -42
  16. data/src/Audio/ALChannelManagement.hpp +1 -1
  17. data/src/Audio/{AudioOpenAL.cpp → Audio.cpp} +6 -7
  18. data/src/Audio/Audio.mm +1 -0
  19. data/src/Audio/AudioFile.hpp +2 -2
  20. data/src/Audio/AudioToolboxFile.hpp +5 -20
  21. data/src/Audio/OggFile.hpp +44 -56
  22. data/src/Audio/SndFile.hpp +2 -2
  23. data/src/Bitmap/Bitmap.cpp +98 -2
  24. data/src/Bitmap/BitmapIO.cpp +156 -0
  25. data/src/DirectoriesApple.mm +76 -0
  26. data/src/DirectoriesUnix.cpp +5 -12
  27. data/src/DirectoriesWin.cpp +5 -0
  28. data/src/Graphics/BlockAllocator.hpp +2 -2
  29. data/src/Graphics/ClipRectStack.hpp +2 -2
  30. data/src/Graphics/Common.hpp +2 -2
  31. data/src/Graphics/DrawOp.hpp +2 -2
  32. data/src/Graphics/DrawOpQueue.hpp +2 -2
  33. data/src/Graphics/Graphics.cpp +7 -2
  34. data/src/Graphics/LargeImageData.cpp +6 -6
  35. data/src/Graphics/LargeImageData.hpp +3 -3
  36. data/src/Graphics/Macro.hpp +2 -2
  37. data/src/Graphics/RenderState.hpp +2 -2
  38. data/src/Graphics/Resolution.cpp +1 -1
  39. data/src/Graphics/TexChunk.hpp +2 -2
  40. data/src/Graphics/Texture.cpp +21 -16
  41. data/src/Graphics/Texture.hpp +7 -5
  42. data/src/Graphics/TransformStack.hpp +2 -2
  43. data/src/Iconv.hpp +2 -2
  44. data/src/Input/Input.cpp +3 -1
  45. data/src/Input/{InputTouch.mm → InputUIKit.mm} +32 -26
  46. data/src/Input/TextInput.cpp +1 -1
  47. data/src/Text/FormattedString.hpp +2 -2
  48. data/src/Text/TextApple.mm +8 -8
  49. data/src/Text/TextMac.cpp +1 -1
  50. data/src/Text/TextUnix.cpp +1 -1
  51. data/src/UIKit/GosuAppDelegate.h +8 -0
  52. data/src/UIKit/GosuAppDelegate.mm +24 -0
  53. data/src/UIKit/GosuGLView.h +8 -0
  54. data/src/UIKit/GosuGLView.mm +130 -0
  55. data/src/UIKit/GosuViewController.h +13 -0
  56. data/src/UIKit/GosuViewController.mm +214 -0
  57. data/src/UtilityApple.mm +5 -18
  58. data/src/Window.cpp +1 -3
  59. data/src/WindowUIKit.mm +124 -0
  60. data/src/stb_image.h +6437 -0
  61. data/src/stb_image_write.h +730 -0
  62. data/src/stb_vorbis.c +5459 -0
  63. metadata +18 -26
  64. data/Gosu/Sockets.hpp +0 -156
  65. data/src/Audio/AudioOpenAL.mm +0 -1
  66. data/src/Bitmap/BitmapApple.mm +0 -226
  67. data/src/Bitmap/BitmapBMP.cpp +0 -79
  68. data/src/Bitmap/BitmapColorKey.cpp +0 -50
  69. data/src/Bitmap/BitmapFreeImage.cpp +0 -174
  70. data/src/Bitmap/BitmapGDIplus.cpp +0 -212
  71. data/src/Bitmap/BitmapUtils.cpp +0 -76
  72. data/src/DirectoriesMac.mm +0 -38
  73. data/src/DirectoriesTouch.mm +0 -38
  74. data/src/GosuView.hpp +0 -15
  75. data/src/GosuView.mm +0 -208
  76. data/src/Input/AccelerometerReader.hpp +0 -10
  77. data/src/Input/AccelerometerReader.mm +0 -31
  78. data/src/Sockets/CommSocket.cpp +0 -305
  79. data/src/Sockets/ListenerSocket.cpp +0 -59
  80. data/src/Sockets/MessageSocket.cpp +0 -128
  81. data/src/Sockets/Socket.cpp +0 -145
  82. data/src/Sockets/Socket.hpp +0 -66
  83. data/src/WindowTouch.mm +0 -243
  84. data/src/X11vroot.h +0 -118
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gosu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.2
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julian Raschke
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-24 00:00:00.000000000 Z
11
+ date: 2015-08-20 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |2
14
14
  2D game development library.
@@ -36,13 +36,17 @@ files:
36
36
  - lib/gosu.rb
37
37
  - ext/gosu/gosu_wrap.h
38
38
  - ext/gosu/gosu_wrap.cxx
39
- - src/X11vroot.h
39
+ - src/stb_image.h
40
+ - src/stb_image_write.h
41
+ - src/UIKit/GosuAppDelegate.h
42
+ - src/UIKit/GosuGLView.h
43
+ - src/UIKit/GosuViewController.h
44
+ - src/AppleUtility.hpp
40
45
  - src/Audio/ALChannelManagement.hpp
41
46
  - src/Audio/AudioFile.hpp
42
47
  - src/Audio/AudioToolboxFile.hpp
43
48
  - src/Audio/OggFile.hpp
44
49
  - src/Audio/SndFile.hpp
45
- - src/GosuView.hpp
46
50
  - src/Graphics/BlockAllocator.hpp
47
51
  - src/Graphics/ClipRectStack.hpp
48
52
  - src/Graphics/Common.hpp
@@ -55,17 +59,11 @@ files:
55
59
  - src/Graphics/Texture.hpp
56
60
  - src/Graphics/TransformStack.hpp
57
61
  - src/Iconv.hpp
58
- - src/Input/AccelerometerReader.hpp
59
- - src/MacUtility.hpp
60
- - src/Sockets/Socket.hpp
61
62
  - src/Text/FormattedString.hpp
62
- - src/Audio/AudioOpenAL.cpp
63
+ - src/stb_vorbis.c
64
+ - src/Audio/Audio.cpp
63
65
  - src/Bitmap/Bitmap.cpp
64
- - src/Bitmap/BitmapBMP.cpp
65
- - src/Bitmap/BitmapColorKey.cpp
66
- - src/Bitmap/BitmapFreeImage.cpp
67
- - src/Bitmap/BitmapGDIplus.cpp
68
- - src/Bitmap/BitmapUtils.cpp
66
+ - src/Bitmap/BitmapIO.cpp
69
67
  - src/DirectoriesUnix.cpp
70
68
  - src/DirectoriesWin.cpp
71
69
  - src/FileUnix.cpp
@@ -85,10 +83,6 @@ files:
85
83
  - src/Inspection.cpp
86
84
  - src/IO.cpp
87
85
  - src/Math.cpp
88
- - src/Sockets/CommSocket.cpp
89
- - src/Sockets/ListenerSocket.cpp
90
- - src/Sockets/MessageSocket.cpp
91
- - src/Sockets/Socket.cpp
92
86
  - src/Text/Font.cpp
93
87
  - src/Text/Text.cpp
94
88
  - src/Text/TextMac.cpp
@@ -103,18 +97,17 @@ files:
103
97
  - src/Window.cpp
104
98
  - src/WinMain.cpp
105
99
  - src/WinUtility.cpp
106
- - src/Audio/AudioOpenAL.mm
107
- - src/Bitmap/BitmapApple.mm
108
- - src/DirectoriesMac.mm
109
- - src/DirectoriesTouch.mm
110
- - src/GosuView.mm
100
+ - src/Audio/Audio.mm
101
+ - src/DirectoriesApple.mm
111
102
  - src/Graphics/ResolutionApple.mm
112
- - src/Input/AccelerometerReader.mm
113
- - src/Input/InputTouch.mm
103
+ - src/Input/InputUIKit.mm
114
104
  - src/Input/TextInputMac.mm
115
105
  - src/Text/TextApple.mm
106
+ - src/UIKit/GosuAppDelegate.mm
107
+ - src/UIKit/GosuGLView.mm
108
+ - src/UIKit/GosuViewController.mm
116
109
  - src/UtilityApple.mm
117
- - src/WindowTouch.mm
110
+ - src/WindowUIKit.mm
118
111
  - Gosu/Audio.hpp
119
112
  - Gosu/AutoLink.hpp
120
113
  - Gosu/Bitmap.hpp
@@ -133,7 +126,6 @@ files:
133
126
  - Gosu/IO.hpp
134
127
  - Gosu/Math.hpp
135
128
  - Gosu/Platform.hpp
136
- - Gosu/Sockets.hpp
137
129
  - Gosu/Text.hpp
138
130
  - Gosu/TextInput.hpp
139
131
  - Gosu/Timing.hpp
@@ -1,156 +0,0 @@
1
- //! \file Sockets.hpp
2
- //! Interface of the three socket classes, MessageSocket, CommSocket and ListenerSocket.
3
-
4
- #ifndef GOSU_SOCKETS_HPP
5
- #define GOSU_SOCKETS_HPP
6
-
7
- #include <Gosu/TR1.hpp>
8
- #include <cstddef>
9
- #include <string>
10
- #include <Gosu/Platform.hpp>
11
-
12
- namespace Gosu
13
- {
14
- //! Addresses are returned from and given to Gosu functions in host byte order.
15
- typedef std::tr1::uint32_t SocketAddress;
16
-
17
- //! Ports are returned from and given to Gosu functions in host byte order.
18
- typedef std::tr1::uint16_t SocketPort;
19
-
20
- //! Constant that can be used as a placeholder for an arbitrary port, e.g. when
21
- //! starting to listen.
22
- const SocketPort anyPort = 0;
23
-
24
- //! Tries to convert a dotted IP4 string into an address suitable for
25
- //! socket functions. If the string supplied is not such a string, it
26
- //! tries to look up the host via DNS. If both methods fail, zero is
27
- //! returned.
28
- SocketAddress stringToAddress(const std::string& s);
29
- //! Converts an address into a dotted IP4 string.
30
- std::string addressToString(SocketAddress address);
31
-
32
- //! Wraps an UDP socket. Message sockets can send data to and receive
33
- //! data from arbitrary addresses. Also, message sockets send messages
34
- //! (packets) which are limited in size and can arrive in any order, or
35
- //! not at all.
36
- class MessageSocket
37
- {
38
- struct Impl;
39
- const GOSU_UNIQUE_PTR<Impl> pimpl;
40
- #if defined(GOSU_CPP11_ENABLED)
41
- MessageSocket(const MessageSocket&) = delete;
42
- MessageSocket& operator=(const MessageSocket&) = delete;
43
- MessageSocket(MessageSocket&&) = delete;
44
- MessageSocket& operator=(MessageSocket&&) = delete;
45
- #endif
46
-
47
- public:
48
- //! Opens a message socket for listening at the specified port.
49
- //! Gosu::anyPort may be passed to have the message socket use
50
- //! a random free port.
51
- explicit MessageSocket(SocketPort port);
52
- ~MessageSocket();
53
-
54
- //! Returns the local address of the socket.
55
- SocketAddress address() const;
56
- //! Returns the local port of the socket.
57
- SocketPort port() const;
58
- //! Returns the maximum size, in bytes, of a packet that can be sent
59
- //! from this socket.
60
- std::size_t maxMessageSize() const;
61
-
62
- //! Collects all the packets that were sent to this socket and
63
- //! calls onReceive for each of them.
64
- void update();
65
-
66
- //! Sends something to the given port of the computer identified
67
- //! by the address.
68
- void send(SocketAddress address, SocketPort port,
69
- const void* buffer, std::size_t size);
70
- /*void broadcast(SocketPort port, const void* buffer,
71
- std::size_t size);*/
72
-
73
- //! If assigned, will be called by update for every packet received.
74
- std::tr1::function<void (SocketAddress, SocketPort, const void*,
75
- std::size_t)> onReceive;
76
- };
77
-
78
- //! Defines the way in which data is collected until the onReceive event
79
- //! is called for CommSockets.
80
- enum CommMode
81
- {
82
- cmRaw,
83
- //cmLines,
84
- cmManaged
85
- };
86
-
87
- class Socket;
88
-
89
- //! Wraps a TCP socket that is used for one part of bi-directional
90
- //! communication.
91
- class CommSocket
92
- {
93
- struct Impl;
94
- const GOSU_UNIQUE_PTR<Impl> pimpl;
95
- #if defined(GOSU_CPP11_ENABLED)
96
- CommSocket(const CommSocket&) = delete;
97
- CommSocket& operator=(const CommSocket&) = delete;
98
- CommSocket(CommSocket&&) = delete;
99
- CommSocket& operator=(CommSocket&&) = delete;
100
- #endif
101
-
102
- public:
103
- CommSocket(CommMode mode, SocketAddress targetAddress,
104
- SocketPort targetPort);
105
- CommSocket(CommMode mode, Socket& socket);
106
- ~CommSocket();
107
-
108
- SocketAddress address() const;
109
- SocketPort port() const;
110
- SocketAddress remoteAddress() const;
111
- SocketPort remotePort() const;
112
- CommMode mode() const;
113
-
114
- bool connected() const;
115
- void disconnect();
116
- bool keepAlive() const;
117
- void setKeepAlive(bool value);
118
-
119
- void update();
120
- void send(const void* buffer, std::size_t size);
121
- void sendPendingData();
122
- std::size_t pendingBytes() const;
123
-
124
- std::tr1::function<void (const void*, std::size_t)> onReceive;
125
- std::tr1::function<void ()> onDisconnection;
126
- };
127
-
128
- //! Wraps a TCP socket that waits on a specific port and can create
129
- //! CommSocket instances via its onConnection event.
130
- class ListenerSocket
131
- {
132
- struct Impl;
133
- const GOSU_UNIQUE_PTR<Impl> pimpl;
134
- #if defined(GOSU_CPP11_ENABLED)
135
- ListenerSocket(const ListenerSocket&) = delete;
136
- ListenerSocket& operator=(const ListenerSocket&) = delete;
137
- ListenerSocket(ListenerSocket&&) = delete;
138
- ListenerSocket& operator=(ListenerSocket&&) = delete;
139
- #endif
140
-
141
- public:
142
- ListenerSocket(SocketPort port);
143
- ~ListenerSocket();
144
-
145
- SocketAddress address() const;
146
- SocketPort port() const;
147
-
148
- void update();
149
-
150
- //! This signal is fired by update() whenever someone connects
151
- //! to the port which is currently listened on.
152
- std::tr1::function<void (Socket&)> onConnection;
153
- };
154
- }
155
-
156
- #endif
@@ -1 +0,0 @@
1
- #include "AudioOpenAL.cpp"
@@ -1,226 +0,0 @@
1
- #include <Gosu/Graphics.hpp>
2
- #include <Gosu/Bitmap.hpp>
3
- #include <Gosu/IO.hpp>
4
- #include <Gosu/Platform.hpp>
5
- #include <Gosu/Utility.hpp>
6
-
7
- #include "../MacUtility.hpp"
8
- #include <stdexcept>
9
-
10
- #ifdef GOSU_IS_IPHONE
11
- #import <UIKit/UIKit.h>
12
- #else
13
- #import <AppKit/AppKit.h>
14
- #import <AppKit/NSStringDrawing.h>
15
- #endif
16
-
17
- namespace
18
- {
19
- void CGImageToBitmap(CGImageRef imageRef, Gosu::Bitmap& bitmap)
20
- {
21
- static Gosu::CFRef<CGColorSpaceRef> colorSpace(CGColorSpaceCreateDeviceRGB());
22
-
23
- // Use a temporary context to draw the CGImage into the bitmap.
24
- bitmap.resize(CGImageGetWidth(imageRef), CGImageGetHeight(imageRef));
25
- Gosu::CFRef<CGContextRef> context(CGBitmapContextCreate(bitmap.data(), bitmap.width(), bitmap.height(),
26
- 8, bitmap.width() * 4, colorSpace.obj(), kCGImageAlphaPremultipliedLast));
27
- CGContextDrawImage(context.obj(), CGRectMake(0.0, 0.0, bitmap.width(), bitmap.height()), imageRef);
28
- }
29
-
30
- void unmultiplyAlpha(Gosu::Bitmap& bitmap)
31
- {
32
- int pixels = bitmap.width() * bitmap.height();
33
- for (unsigned char* ptr = reinterpret_cast<unsigned char*>(bitmap.data()); pixels--; ptr += 4)
34
- {
35
- // Avoid division-by-zero weirdness
36
- if (!ptr[3])
37
- continue;
38
- ptr[0] = ptr[0] * 255 / ptr[3];
39
- ptr[1] = ptr[1] * 255 / ptr[3];
40
- ptr[2] = ptr[2] * 255 / ptr[3];
41
- }
42
- }
43
-
44
- #ifdef GOSU_IS_IPHONE
45
- #define APPLE_IMAGE UIImage
46
- void appleImageToBitmap(UIImage* image, Gosu::Bitmap& bitmap)
47
- {
48
- CGImageToBitmap([image CGImage], bitmap);
49
- }
50
- #else
51
- #define APPLE_IMAGE NSImage
52
- void appleImageToBitmap(NSImage* image, Gosu::Bitmap& bitmap)
53
- {
54
- // If we have CGImageForProposedRect (10.6+), use it. This is important because the code below
55
- // started to break in 10.6:
56
- http://stackoverflow.com/questions/2239785/nsimage-color-shift-on-snow-leopard
57
-
58
- if ([image respondsToSelector:@selector(CGImageForProposedRect:context:hints:)])
59
- {
60
- CGImageToBitmap((CGImageRef)[image CGImageForProposedRect:NULL context:nil hints:nil], bitmap);
61
- unmultiplyAlpha(bitmap);
62
- }
63
-
64
- // Otherwise, take the slow route using -drawInRect:fromRect:operation:fraction:
65
- // TODO: Support vector graphics (we rely on NSBitmapImageRep right now for pixelsWide/pixelsHigh)
66
-
67
- id anyRep = [image bestRepresentationForDevice:nil];
68
- if (![anyRep isKindOfClass:[NSBitmapImageRep class]])
69
- throw std::logic_error("Cannot read vector graphics files");
70
- NSBitmapImageRep* rep = (NSBitmapImageRep*)anyRep;
71
-
72
- bitmap.resize([rep pixelsWide], [rep pixelsHigh]);
73
-
74
- // Use a temporary context to draw the NSImage to the buffer. For that, construct a color
75
- // space that does not alter the colors while drawing from the NSImage to the CGBitmapContext.
76
- static Gosu::CFRef<CGColorSpaceRef> colorSpace(CGColorSpaceCreateDeviceRGB());
77
-
78
- // We do NOT want premultiplied alpha (yet?) in Gosu. However, with kCGImageAlphaLast, this
79
- // call mysteriously fails.
80
- Gosu::CFRef<CGContextRef> context(CGBitmapContextCreate(bitmap.data(), bitmap.width(), bitmap.height(),
81
- 8, bitmap.width() * 4,
82
- colorSpace.obj(), kCGImageAlphaPremultipliedLast));
83
-
84
- [NSGraphicsContext saveGraphicsState];
85
- [NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithGraphicsPort:context.obj() flipped:NO]];
86
- [image drawInRect:NSMakeRect(0, 0, bitmap.width(), bitmap.height()) fromRect:NSZeroRect
87
- operation:NSCompositeCopy fraction:1.0];
88
- [NSGraphicsContext restoreGraphicsState];
89
- unmultiplyAlpha(bitmap);
90
- }
91
- #endif
92
- }
93
-
94
- // TODO: Move into proper internal header
95
- namespace Gosu { bool isExtension(const wchar_t* str, const wchar_t* ext); }
96
-
97
- void Gosu::loadImageFile(Bitmap& bitmap, const std::wstring& filename)
98
- {
99
- ObjRef<NSAutoreleasePool> pool([NSAutoreleasePool new]);
100
- ObjRef<APPLE_IMAGE> image;
101
- ObjRef<NSString> filenameRef([[NSString alloc] initWithUTF8String: wstringToUTF8(filename).c_str()]);
102
-
103
- #ifdef GOSU_IS_IPHONE
104
- if ([UIScreen mainScreen].scale > 1) {
105
- #endif
106
- // Prevent app from automatically appending @2x to full filenames by loading into an NSData object first
107
- NSData *data = [NSData dataWithContentsOfFile:filenameRef.obj()];
108
- image.reset([[APPLE_IMAGE alloc] initWithData:data]);
109
- #ifdef GOSU_IS_IPHONE
110
- }
111
- else {
112
- image.reset([[APPLE_IMAGE alloc] initWithContentsOfFile:filenameRef.obj()]);
113
- }
114
- #endif
115
-
116
- if (! image.get())
117
- throw std::runtime_error("Cannot load image file " + wstringToUTF8(filename));
118
-
119
- appleImageToBitmap(image.obj(), bitmap);
120
- if (isExtension(filename.c_str(), L".bmp"))
121
- applyColorKey(bitmap, Color::FUCHSIA);
122
- }
123
-
124
- void Gosu::loadImageFile(Bitmap& bitmap, Reader reader)
125
- {
126
- char signature[2];
127
- reader.read(signature, 2);
128
- reader.seek(-2);
129
-
130
- ObjRef<NSAutoreleasePool> pool([NSAutoreleasePool new]);
131
-
132
- std::size_t length = reader.resource().size() - reader.position();
133
- ObjRef<NSMutableData> buffer([[NSMutableData alloc] initWithLength: length]);
134
- reader.read([buffer.get() mutableBytes], length);
135
-
136
- ObjRef<APPLE_IMAGE> image([[APPLE_IMAGE alloc] initWithData: buffer.get()]);
137
- if (!image.get())
138
- throw std::runtime_error("Cannot load image file from stream");
139
-
140
- appleImageToBitmap(image.obj(), bitmap);
141
- if (signature[0] == 'B' && signature[1] == 'M')
142
- applyColorKey(bitmap, Color::FUCHSIA);
143
- }
144
-
145
- void Gosu::saveImageFile(const Bitmap& bitmap, const std::wstring& filename)
146
- {
147
- Buffer buffer;
148
- saveImageFile(bitmap, buffer.backWriter(), filename);
149
- saveFile(buffer, filename);
150
- }
151
-
152
- #ifndef GOSU_IS_IPHONE
153
- void Gosu::saveImageFile(const Bitmap& originalBitmap, Writer writer, const std::wstring& formatHint)
154
- {
155
- NSBitmapImageFileType fileType;
156
- if (isExtension(formatHint.c_str(), L"png"))
157
- fileType = NSPNGFileType;
158
- else if (isExtension(formatHint.c_str(), L"bmp"))
159
- fileType = NSBMPFileType;
160
- else if (isExtension(formatHint.c_str(), L"gif"))
161
- fileType = NSGIFFileType;
162
- else if (isExtension(formatHint.c_str(), L"jpg") || isExtension(formatHint.c_str(), L"jpeg"))
163
- fileType = NSJPEGFileType;
164
- else if (isExtension(formatHint.c_str(), L"tif") || isExtension(formatHint.c_str(), L"tiff"))
165
- fileType = NSTIFFFileType;
166
- else
167
- throw std::runtime_error("Unsupported image format for writing: " + wstringToUTF8(formatHint));
168
-
169
- Bitmap copyOfBitmap;
170
- const Bitmap& bitmap = (fileType == NSBMPFileType) ? copyOfBitmap : originalBitmap;
171
- if (fileType == NSBMPFileType)
172
- {
173
- copyOfBitmap = originalBitmap;
174
- unapplyColorKey(copyOfBitmap, Color::FUCHSIA);
175
- }
176
-
177
- unsigned char* plane = (unsigned char*)bitmap.data();
178
- ObjRef<NSBitmapImageRep> rep([[NSBitmapImageRep alloc]
179
- initWithBitmapDataPlanes:&plane pixelsWide:bitmap.width() pixelsHigh:bitmap.height()
180
- bitsPerSample:8 samplesPerPixel:4 hasAlpha:YES isPlanar:NO
181
- colorSpaceName:NSDeviceRGBColorSpace // Nobody really seems to know which one to use
182
- bitmapFormat:NSAlphaNonpremultipliedBitmapFormat bytesPerRow:bitmap.width() * 4 bitsPerPixel:32]);
183
-
184
- ObjRef<NSAutoreleasePool> pool([NSAutoreleasePool new]);
185
- NSData* data = [rep.obj() representationUsingType:fileType properties:nil];
186
- writer.write([data bytes], [data length]);
187
- }
188
- #else
189
- void Gosu::saveImageFile(const Bitmap& bmp, Writer writer, const std::wstring& formatHint)
190
- {
191
- if (isExtension(formatHint.c_str(), L"bmp"))
192
- {
193
- Bitmap bitmap = bmp;
194
- unapplyColorKey(bitmap, Color::FUCHSIA);
195
- Gosu::saveToBMP(bitmap, writer);
196
- return;
197
- }
198
-
199
- ObjRef<NSAutoreleasePool> pool([NSAutoreleasePool new]);
200
-
201
- CGDataProviderRef dataProvider =
202
- CGDataProviderCreateWithData(0, bmp.data(), bmp.width() * bmp.height() * 4, 0);
203
-
204
- static CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
205
-
206
- CGImageRef imageRef =
207
- CGImageCreate(bmp.width(), bmp.height(), 8, 32, bmp.width() * 4, colorspace,
208
- kCGImageAlphaLast, dataProvider, 0, false, kCGRenderingIntentDefault);
209
-
210
- ObjRef<UIImage> image([[UIImage alloc] initWithCGImage: imageRef]);
211
-
212
- NSData* data;
213
- if (isExtension(formatHint.c_str(), L"jpeg") || isExtension(formatHint.c_str(), L"jpg"))
214
- data = UIImageJPEGRepresentation(image.get(), 0.0);
215
- else if (isExtension(formatHint.c_str(), L"png"))
216
- data = UIImagePNGRepresentation(image.get());
217
- else
218
- throw std::runtime_error("Unsupported image format for writing: " + wstringToUTF8(formatHint));
219
- writer.write([data bytes], [data length]);
220
- image.reset();
221
-
222
- CGImageRelease(imageRef);
223
-
224
- CGDataProviderRelease(dataProvider);
225
- }
226
- #endif