gosu 0.7.10.1
Sign up to get free protection for your applications and to get access to all the features.
- data/COPYING.txt +29 -0
- data/Gosu/Async.hpp +48 -0
- data/Gosu/Audio.hpp +145 -0
- data/Gosu/AutoLink.hpp +16 -0
- data/Gosu/Bitmap.hpp +85 -0
- data/Gosu/ButtonsMac.hpp +114 -0
- data/Gosu/ButtonsWin.hpp +111 -0
- data/Gosu/ButtonsX.hpp +115 -0
- data/Gosu/Color.hpp +172 -0
- data/Gosu/Directories.hpp +36 -0
- data/Gosu/Font.hpp +59 -0
- data/Gosu/Fwd.hpp +31 -0
- data/Gosu/Gosu.hpp +26 -0
- data/Gosu/Graphics.hpp +86 -0
- data/Gosu/GraphicsBase.hpp +45 -0
- data/Gosu/IO.hpp +255 -0
- data/Gosu/Image.hpp +148 -0
- data/Gosu/ImageData.hpp +45 -0
- data/Gosu/Input.hpp +116 -0
- data/Gosu/Math.hpp +95 -0
- data/Gosu/Platform.hpp +61 -0
- data/Gosu/RotFlip.hpp +116 -0
- data/Gosu/Sockets.hpp +129 -0
- data/Gosu/Text.hpp +47 -0
- data/Gosu/TextInput.hpp +57 -0
- data/Gosu/Timing.hpp +16 -0
- data/Gosu/Utility.hpp +24 -0
- data/Gosu/WinUtility.hpp +76 -0
- data/Gosu/Window.hpp +84 -0
- data/GosuImpl/Async.cpp +37 -0
- data/GosuImpl/AudioFmod.cpp +417 -0
- data/GosuImpl/AudioSDL.cpp +255 -0
- data/GosuImpl/DirectoriesMac.mm +38 -0
- data/GosuImpl/DirectoriesUnix.cpp +48 -0
- data/GosuImpl/DirectoriesWin.cpp +42 -0
- data/GosuImpl/FileUnix.cpp +100 -0
- data/GosuImpl/FileWin.cpp +83 -0
- data/GosuImpl/Graphics/Bitmap.cpp +116 -0
- data/GosuImpl/Graphics/BitmapBMP.cpp +232 -0
- data/GosuImpl/Graphics/BitmapColorKey.cpp +39 -0
- data/GosuImpl/Graphics/BitmapPNG.cpp +276 -0
- data/GosuImpl/Graphics/BitmapUtils.cpp +67 -0
- data/GosuImpl/Graphics/BlockAllocator.cpp +127 -0
- data/GosuImpl/Graphics/BlockAllocator.hpp +34 -0
- data/GosuImpl/Graphics/Color.cpp +126 -0
- data/GosuImpl/Graphics/Common.hpp +21 -0
- data/GosuImpl/Graphics/DrawOp.hpp +154 -0
- data/GosuImpl/Graphics/Font.cpp +110 -0
- data/GosuImpl/Graphics/Graphics.cpp +295 -0
- data/GosuImpl/Graphics/Image.cpp +159 -0
- data/GosuImpl/Graphics/LargeImageData.cpp +115 -0
- data/GosuImpl/Graphics/LargeImageData.hpp +37 -0
- data/GosuImpl/Graphics/RotFlip.cpp +184 -0
- data/GosuImpl/Graphics/TexChunk.cpp +77 -0
- data/GosuImpl/Graphics/TexChunk.hpp +40 -0
- data/GosuImpl/Graphics/Text.cpp +223 -0
- data/GosuImpl/Graphics/TextMac.cpp +242 -0
- data/GosuImpl/Graphics/TextPangoFT.cpp +186 -0
- data/GosuImpl/Graphics/TextWin.cpp +172 -0
- data/GosuImpl/Graphics/Texture.cpp +104 -0
- data/GosuImpl/Graphics/Texture.hpp +34 -0
- data/GosuImpl/IO.cpp +48 -0
- data/GosuImpl/InputMac.mm +677 -0
- data/GosuImpl/InputWin.cpp +444 -0
- data/GosuImpl/InputX.cpp +158 -0
- data/GosuImpl/MacUtility.hpp +48 -0
- data/GosuImpl/Math.cpp +49 -0
- data/GosuImpl/RubyGosu.swg +474 -0
- data/GosuImpl/RubyGosuStub.mm +17 -0
- data/GosuImpl/RubyGosu_DllMain.cxx +30 -0
- data/GosuImpl/RubyGosu_wrap.cxx +8521 -0
- data/GosuImpl/RubyGosu_wrap.h +31 -0
- data/GosuImpl/Sockets/CommSocket.cpp +304 -0
- data/GosuImpl/Sockets/ListenerSocket.cpp +60 -0
- data/GosuImpl/Sockets/MessageSocket.cpp +136 -0
- data/GosuImpl/Sockets/Socket.cpp +145 -0
- data/GosuImpl/Sockets/Sockets.hpp +66 -0
- data/GosuImpl/TextInputMac.mm +207 -0
- data/GosuImpl/TextInputWin.cpp +197 -0
- data/GosuImpl/TextInputX.cpp +201 -0
- data/GosuImpl/TextTTFWin.cpp +247 -0
- data/GosuImpl/TimingUnix.cpp +17 -0
- data/GosuImpl/TimingWin.cpp +28 -0
- data/GosuImpl/Utility.cpp +140 -0
- data/GosuImpl/WinMain.cpp +69 -0
- data/GosuImpl/WinUtility.cpp +137 -0
- data/GosuImpl/WindowMac.mm +466 -0
- data/GosuImpl/WindowWin.cpp +447 -0
- data/GosuImpl/WindowX.cpp +392 -0
- data/GosuImpl/X11vroot.h +118 -0
- data/README.txt +13 -0
- data/Rakefile +178 -0
- data/examples/ChipmunkIntegration.rb +275 -0
- data/examples/CptnRuby.rb +231 -0
- data/examples/MoreChipmunkAndRMagick.rb +155 -0
- data/examples/OpenGLIntegration.rb +232 -0
- data/examples/RMagickIntegration.rb +449 -0
- data/examples/TextInput.cpp +170 -0
- data/examples/TextInput.rb +139 -0
- data/examples/Tutorial.cpp +215 -0
- data/examples/Tutorial.rb +137 -0
- data/examples/media/Beep.wav +0 -0
- data/examples/media/CptnRuby Gem.png +0 -0
- data/examples/media/CptnRuby Map.txt +25 -0
- data/examples/media/CptnRuby Tileset.png +0 -0
- data/examples/media/CptnRuby.png +0 -0
- data/examples/media/Cursor.png +0 -0
- data/examples/media/Earth.png +0 -0
- data/examples/media/Explosion.wav +0 -0
- data/examples/media/LargeStar.png +0 -0
- data/examples/media/Sky.jpg +0 -0
- data/examples/media/Smoke.png +0 -0
- data/examples/media/Soldier.png +0 -0
- data/examples/media/Space.png +0 -0
- data/examples/media/Star.png +0 -0
- data/examples/media/Starfighter.bmp +0 -0
- data/linux/Makefile.in +98 -0
- data/linux/configure +5658 -0
- data/linux/configure.ac +126 -0
- data/linux/extconf.rb +11 -0
- data/mac/English.lproj/InfoPlist.strings +0 -0
- data/mac/Gosu-Info.plist +26 -0
- data/mac/Gosu.xcodeproj/project.pbxproj +1194 -0
- data/mac/RubyGosu Template-Info.plist +26 -0
- data/mac/libboost_thread_1_34_1_universal.a +0 -0
- data/mac/libboost_thread_d_1_34_1_universal.a +0 -0
- data/mac/libfmod_universal.a +0 -0
- data/mac/libpng_universal.a +0 -0
- data/mac/libz_universal.a +0 -0
- data/reference/Async_8hpp-source.html +70 -0
- data/reference/Audio_8hpp-source.html +114 -0
- data/reference/Audio_8hpp.html +50 -0
- data/reference/AutoLink_8hpp-source.html +38 -0
- data/reference/AutoLink_8hpp.html +34 -0
- data/reference/Bitmap_8hpp-source.html +85 -0
- data/reference/Bitmap_8hpp.html +58 -0
- data/reference/ButtonsMac_8hpp-source.html +133 -0
- data/reference/ButtonsWin_8hpp-source.html +133 -0
- data/reference/ButtonsX_8hpp-source.html +134 -0
- data/reference/Color_8hpp-source.html +169 -0
- data/reference/Color_8hpp.html +85 -0
- data/reference/Directories_8hpp-source.html +42 -0
- data/reference/Directories_8hpp.html +46 -0
- data/reference/Font_8hpp-source.html +65 -0
- data/reference/Font_8hpp.html +41 -0
- data/reference/Fwd_8hpp-source.html +52 -0
- data/reference/Fwd_8hpp.html +37 -0
- data/reference/Gosu_8hpp-source.html +48 -0
- data/reference/Gosu_8hpp.html +34 -0
- data/reference/GraphicsBase_8hpp-source.html +57 -0
- data/reference/GraphicsBase_8hpp.html +56 -0
- data/reference/Graphics_8hpp-source.html +96 -0
- data/reference/Graphics_8hpp.html +53 -0
- data/reference/IO_8hpp-source.html +255 -0
- data/reference/IO_8hpp.html +74 -0
- data/reference/ImageData_8hpp-source.html +62 -0
- data/reference/ImageData_8hpp.html +43 -0
- data/reference/Image_8hpp-source.html +126 -0
- data/reference/Image_8hpp.html +48 -0
- data/reference/Input_8hpp-source.html +118 -0
- data/reference/Input_8hpp.html +50 -0
- data/reference/Math_8hpp-source.html +92 -0
- data/reference/Math_8hpp.html +74 -0
- data/reference/Platform_8hpp-source.html +83 -0
- data/reference/Platform_8hpp.html +73 -0
- data/reference/RotFlip_8hpp-source.html +138 -0
- data/reference/RotFlip_8hpp.html +77 -0
- data/reference/Sockets_8hpp-source.html +130 -0
- data/reference/Sockets_8hpp.html +66 -0
- data/reference/TextInput_8hpp-source.html +64 -0
- data/reference/TextInput_8hpp.html +41 -0
- data/reference/Text_8hpp-source.html +51 -0
- data/reference/Text_8hpp.html +46 -0
- data/reference/Timing_8hpp-source.html +36 -0
- data/reference/Timing_8hpp.html +42 -0
- data/reference/Utility_8hpp-source.html +44 -0
- data/reference/Utility_8hpp.html +48 -0
- data/reference/WinUtility_8hpp-source.html +79 -0
- data/reference/WinUtility_8hpp.html +64 -0
- data/reference/Window_8hpp-source.html +91 -0
- data/reference/Window_8hpp.html +41 -0
- data/reference/annotated.html +51 -0
- data/reference/classGosu_1_1Audio-members.html +34 -0
- data/reference/classGosu_1_1Audio.html +46 -0
- data/reference/classGosu_1_1Bitmap-members.html +44 -0
- data/reference/classGosu_1_1Bitmap.html +263 -0
- data/reference/classGosu_1_1Buffer-members.html +44 -0
- data/reference/classGosu_1_1Buffer.html +78 -0
- data/reference/classGosu_1_1Buffer.png +0 -0
- data/reference/classGosu_1_1Button-members.html +36 -0
- data/reference/classGosu_1_1Button.html +143 -0
- data/reference/classGosu_1_1Color-members.html +56 -0
- data/reference/classGosu_1_1Color.html +387 -0
- data/reference/classGosu_1_1File-members.html +41 -0
- data/reference/classGosu_1_1File.html +69 -0
- data/reference/classGosu_1_1File.png +0 -0
- data/reference/classGosu_1_1Font-members.html +39 -0
- data/reference/classGosu_1_1Font.html +309 -0
- data/reference/classGosu_1_1Graphics-members.html +50 -0
- data/reference/classGosu_1_1Graphics.html +234 -0
- data/reference/classGosu_1_1Image-members.html +45 -0
- data/reference/classGosu_1_1Image.html +518 -0
- data/reference/classGosu_1_1ImageData-members.html +37 -0
- data/reference/classGosu_1_1ImageData.html +60 -0
- data/reference/classGosu_1_1Input-members.html +44 -0
- data/reference/classGosu_1_1Input.html +223 -0
- data/reference/classGosu_1_1MessageSocket-members.html +40 -0
- data/reference/classGosu_1_1MessageSocket.html +233 -0
- data/reference/classGosu_1_1Resource-members.html +39 -0
- data/reference/classGosu_1_1Resource.html +116 -0
- data/reference/classGosu_1_1Resource.png +0 -0
- data/reference/classGosu_1_1Sample-members.html +37 -0
- data/reference/classGosu_1_1Sample.html +200 -0
- data/reference/classGosu_1_1SampleInstance-members.html +38 -0
- data/reference/classGosu_1_1SampleInstance.html +169 -0
- data/reference/classGosu_1_1Song-members.html +43 -0
- data/reference/classGosu_1_1Song.html +260 -0
- data/reference/classGosu_1_1TextInput-members.html +38 -0
- data/reference/classGosu_1_1TextInput.html +121 -0
- data/reference/classGosu_1_1Window-members.html +50 -0
- data/reference/classGosu_1_1Window.html +271 -0
- data/reference/doxyfile +233 -0
- data/reference/doxygen.css +433 -0
- data/reference/doxygen.png +0 -0
- data/reference/files.html +54 -0
- data/reference/functions.html +236 -0
- data/reference/functions_enum.html +45 -0
- data/reference/functions_func.html +227 -0
- data/reference/functions_vars.html +47 -0
- data/reference/hierarchy.html +53 -0
- data/reference/index.html +26 -0
- data/reference/namespaceGosu.html +2890 -0
- data/reference/namespaceGosu_1_1Colors.html +70 -0
- data/reference/namespaceGosu_1_1Win.html +275 -0
- data/reference/namespacemembers.html +216 -0
- data/reference/namespacemembers_enum.html +52 -0
- data/reference/namespacemembers_eval.html +54 -0
- data/reference/namespacemembers_func.html +185 -0
- data/reference/namespacemembers_type.html +46 -0
- data/reference/namespacemembers_vars.html +46 -0
- data/reference/namespaces.html +35 -0
- data/reference/tab_b.gif +0 -0
- data/reference/tab_l.gif +0 -0
- data/reference/tab_r.gif +0 -0
- data/reference/tabs.css +102 -0
- data/windows/Gosu.sln +29 -0
- data/windows/Gosu.vcproj +553 -0
- data/windows/RubyGosu.vcproj +138 -0
- metadata +305 -0
@@ -0,0 +1,83 @@
|
|
1
|
+
#include <Gosu/IO.hpp>
|
2
|
+
#include <Gosu/Utility.hpp>
|
3
|
+
#include <Gosu/WinUtility.hpp>
|
4
|
+
#include <windows.h>
|
5
|
+
|
6
|
+
// IMPR: Error checking
|
7
|
+
|
8
|
+
struct Gosu::File::Impl
|
9
|
+
{
|
10
|
+
HANDLE handle;
|
11
|
+
|
12
|
+
Impl()
|
13
|
+
: handle(INVALID_HANDLE_VALUE)
|
14
|
+
{
|
15
|
+
}
|
16
|
+
|
17
|
+
~Impl()
|
18
|
+
{
|
19
|
+
if (handle != INVALID_HANDLE_VALUE)
|
20
|
+
::CloseHandle(handle);
|
21
|
+
}
|
22
|
+
};
|
23
|
+
|
24
|
+
Gosu::File::File(const std::wstring& filename, FileMode mode)
|
25
|
+
: pimpl(new Impl)
|
26
|
+
{
|
27
|
+
DWORD access;
|
28
|
+
switch (mode)
|
29
|
+
{
|
30
|
+
case fmRead:
|
31
|
+
access = GENERIC_READ;
|
32
|
+
break;
|
33
|
+
case fmReplace:
|
34
|
+
access = GENERIC_WRITE;
|
35
|
+
break;
|
36
|
+
case fmAlter:
|
37
|
+
access = GENERIC_READ | GENERIC_WRITE;
|
38
|
+
break;
|
39
|
+
}
|
40
|
+
DWORD shareMode = FILE_SHARE_READ;
|
41
|
+
DWORD creationDisp = (mode == fmRead) ? OPEN_EXISTING : OPEN_ALWAYS;
|
42
|
+
|
43
|
+
pimpl->handle = ::CreateFile(filename.c_str(), access, shareMode, 0,
|
44
|
+
creationDisp, FILE_ATTRIBUTE_NORMAL, 0);
|
45
|
+
if (pimpl->handle == INVALID_HANDLE_VALUE)
|
46
|
+
Win::throwLastError("opening " + Gosu::narrow(filename));
|
47
|
+
if (mode == fmReplace)
|
48
|
+
resize(0);
|
49
|
+
}
|
50
|
+
|
51
|
+
Gosu::File::~File()
|
52
|
+
{
|
53
|
+
}
|
54
|
+
|
55
|
+
std::size_t Gosu::File::size() const
|
56
|
+
{
|
57
|
+
return ::GetFileSize(pimpl->handle, 0);
|
58
|
+
}
|
59
|
+
|
60
|
+
void Gosu::File::resize(std::size_t newSize)
|
61
|
+
{
|
62
|
+
if (::SetFilePointer(pimpl->handle, newSize, 0, FILE_BEGIN) == INVALID_SET_FILE_POINTER)
|
63
|
+
Win::throwLastError("setting the file pointer");
|
64
|
+
Win::check(::SetEndOfFile(pimpl->handle), "resizing a file");
|
65
|
+
}
|
66
|
+
|
67
|
+
void Gosu::File::read(std::size_t offset, std::size_t length,
|
68
|
+
void* destBuffer) const
|
69
|
+
{
|
70
|
+
if (::SetFilePointer(pimpl->handle, offset, 0, FILE_BEGIN) == INVALID_SET_FILE_POINTER)
|
71
|
+
Win::throwLastError("setting the file pointer");
|
72
|
+
DWORD dummy;
|
73
|
+
Win::check(::ReadFile(pimpl->handle, destBuffer, length, &dummy, 0));
|
74
|
+
}
|
75
|
+
|
76
|
+
void Gosu::File::write(std::size_t offset, std::size_t length,
|
77
|
+
const void* sourceBuffer)
|
78
|
+
{
|
79
|
+
if (::SetFilePointer(pimpl->handle, offset, 0, FILE_BEGIN) == INVALID_SET_FILE_POINTER)
|
80
|
+
Win::throwLastError("setting the file pointer");
|
81
|
+
DWORD dummy;
|
82
|
+
Win::check(::WriteFile(pimpl->handle, sourceBuffer, length, &dummy, 0));
|
83
|
+
}
|
@@ -0,0 +1,116 @@
|
|
1
|
+
#include <Gosu/Bitmap.hpp>
|
2
|
+
#include <cassert>
|
3
|
+
#include <algorithm>
|
4
|
+
#include <vector>
|
5
|
+
|
6
|
+
Gosu::Bitmap::Bitmap()
|
7
|
+
: w(0), h(0)
|
8
|
+
{
|
9
|
+
}
|
10
|
+
|
11
|
+
/*unsigned Gosu::Bitmap::width() const
|
12
|
+
{
|
13
|
+
return w;
|
14
|
+
}
|
15
|
+
|
16
|
+
unsigned Gosu::Bitmap::height() const
|
17
|
+
{
|
18
|
+
return h;
|
19
|
+
}*/
|
20
|
+
|
21
|
+
void Gosu::Bitmap::swap(Bitmap& other)
|
22
|
+
{
|
23
|
+
std::swap(pixels, other.pixels);
|
24
|
+
std::swap(w, other.w);
|
25
|
+
std::swap(h, other.h);
|
26
|
+
}
|
27
|
+
|
28
|
+
void Gosu::Bitmap::resize(unsigned width, unsigned height, Color c)
|
29
|
+
{
|
30
|
+
Bitmap temp;
|
31
|
+
temp.pixels.resize(width * height, c);
|
32
|
+
temp.w = width;
|
33
|
+
temp.h = height;
|
34
|
+
temp.insert(*this, 0, 0);
|
35
|
+
swap(temp);
|
36
|
+
}
|
37
|
+
|
38
|
+
void Gosu::Bitmap::fill(Color c)
|
39
|
+
{
|
40
|
+
std::fill(pixels.begin(), pixels.end(), c);
|
41
|
+
}
|
42
|
+
|
43
|
+
void Gosu::Bitmap::replace(Color what, Color with)
|
44
|
+
{
|
45
|
+
std::replace(pixels.begin(), pixels.end(), what, with);
|
46
|
+
}
|
47
|
+
|
48
|
+
/*Gosu::Color Gosu::Bitmap::getPixel(unsigned x, unsigned y) const
|
49
|
+
{
|
50
|
+
assert(x <= w);
|
51
|
+
assert(y <= h);
|
52
|
+
assert(w*h > 0);
|
53
|
+
return pixels[y * w + x];
|
54
|
+
}
|
55
|
+
|
56
|
+
void Gosu::Bitmap::setPixel(unsigned x, unsigned y, Color c)
|
57
|
+
{
|
58
|
+
assert(x <= w);
|
59
|
+
assert(y <= h);
|
60
|
+
assert(w*h > 0);
|
61
|
+
pixels[y * w + x] = c;
|
62
|
+
}*/
|
63
|
+
|
64
|
+
void Gosu::Bitmap::insert(const Bitmap& source, int x, int y)
|
65
|
+
{
|
66
|
+
insert(source, x, y, 0, 0, source.width(), source.height());
|
67
|
+
}
|
68
|
+
|
69
|
+
void Gosu::Bitmap::insert(const Bitmap& source, int x, int y, unsigned srcX,
|
70
|
+
unsigned srcY, unsigned srcWidth, unsigned srcHeight)
|
71
|
+
{
|
72
|
+
if (x < 0)
|
73
|
+
{
|
74
|
+
unsigned clipLeft = -x;
|
75
|
+
|
76
|
+
if (clipLeft >= srcWidth)
|
77
|
+
return;
|
78
|
+
|
79
|
+
srcX += clipLeft;
|
80
|
+
srcWidth -= clipLeft;
|
81
|
+
x = 0;
|
82
|
+
}
|
83
|
+
|
84
|
+
if (y < 0)
|
85
|
+
{
|
86
|
+
unsigned clipTop = -y;
|
87
|
+
|
88
|
+
if (clipTop >= srcHeight)
|
89
|
+
return;
|
90
|
+
|
91
|
+
srcY += clipTop;
|
92
|
+
srcHeight -= clipTop;
|
93
|
+
y = 0;
|
94
|
+
}
|
95
|
+
|
96
|
+
if (x + srcWidth > w)
|
97
|
+
{
|
98
|
+
if (static_cast<unsigned>(x) >= w)
|
99
|
+
return;
|
100
|
+
|
101
|
+
srcWidth = w - x;
|
102
|
+
}
|
103
|
+
|
104
|
+
if (y + srcHeight > h)
|
105
|
+
{
|
106
|
+
if (static_cast<unsigned>(y) >= h)
|
107
|
+
return;
|
108
|
+
|
109
|
+
srcHeight = h - y;
|
110
|
+
}
|
111
|
+
|
112
|
+
for (unsigned relY = 0; relY < srcHeight; ++relY)
|
113
|
+
for (unsigned relX = 0; relX < srcWidth; ++relX)
|
114
|
+
setPixel(x + relX, y + relY,
|
115
|
+
source.getPixel(srcX + relX, srcY + relY));
|
116
|
+
}
|
@@ -0,0 +1,232 @@
|
|
1
|
+
#include <Gosu/Bitmap.hpp>
|
2
|
+
#include <Gosu/IO.hpp>
|
3
|
+
#include <boost/cstdint.hpp>
|
4
|
+
#include <boost/integer.hpp>
|
5
|
+
#include <stdexcept>
|
6
|
+
|
7
|
+
namespace
|
8
|
+
{
|
9
|
+
typedef std::vector<Gosu::Color> ColorTable;
|
10
|
+
|
11
|
+
void decodePart(unsigned bitCount, Gosu::Bitmap& bmp, Gosu::Reader& reader,
|
12
|
+
boost::uint32_t x, boost::uint32_t y, const ColorTable& colorTable)
|
13
|
+
{
|
14
|
+
if (bitCount == 24)
|
15
|
+
{
|
16
|
+
Gosu::Color color;
|
17
|
+
color.setBlue(reader.getPod<boost::uint8_t>());
|
18
|
+
color.setGreen(reader.getPod<boost::uint8_t>());
|
19
|
+
color.setRed(reader.getPod<boost::uint8_t>());
|
20
|
+
color.setAlpha(255);
|
21
|
+
bmp.setPixel(x, y, color);
|
22
|
+
}
|
23
|
+
else if (bitCount == 8)
|
24
|
+
{
|
25
|
+
boost::uint8_t index;
|
26
|
+
reader.readPod(index);
|
27
|
+
if (index >= colorTable.size())
|
28
|
+
throw std::runtime_error("Bitmap palette index out of range");
|
29
|
+
bmp.setPixel(x, y, colorTable[index]);
|
30
|
+
}
|
31
|
+
else if (bitCount == 4)
|
32
|
+
{
|
33
|
+
boost::uint8_t doubleIndex, index[2];
|
34
|
+
reader.readPod(doubleIndex);
|
35
|
+
index[0] = (doubleIndex & 0xf0) >> 4;
|
36
|
+
index[1] = (doubleIndex & 0x0f);
|
37
|
+
|
38
|
+
for (int i = 0; i < 2; ++i)
|
39
|
+
{
|
40
|
+
if (index[i] > colorTable.size())
|
41
|
+
throw std::runtime_error("Bitmap palette index out of range");
|
42
|
+
bmp.setPixel(x + i, y, colorTable[index[i]]);
|
43
|
+
}
|
44
|
+
}
|
45
|
+
}
|
46
|
+
|
47
|
+
template<unsigned bits>
|
48
|
+
typename boost::uint_t<bits>::fast readVal(Gosu::Reader& reader)
|
49
|
+
{
|
50
|
+
typename boost::uint_t<bits>::least val;
|
51
|
+
reader.readPod(val, Gosu::boLittle);
|
52
|
+
return val;
|
53
|
+
}
|
54
|
+
}
|
55
|
+
|
56
|
+
Gosu::Reader Gosu::loadFromBMP(Bitmap& bmp, Reader reader)
|
57
|
+
{
|
58
|
+
if (readVal<8>(reader) != 'B' || readVal<8>(reader) != 'M')
|
59
|
+
throw std::runtime_error("File is not a bitmap!");
|
60
|
+
|
61
|
+
// File header
|
62
|
+
// Size
|
63
|
+
readVal<32>(reader);
|
64
|
+
// Reserved
|
65
|
+
readVal<16>(reader);
|
66
|
+
readVal<16>(reader);
|
67
|
+
// Offset to bitmap data
|
68
|
+
readVal<32>(reader);
|
69
|
+
|
70
|
+
// Info header
|
71
|
+
unsigned infoHeaderSize = readVal<32>(reader);
|
72
|
+
if (infoHeaderSize != 12 && infoHeaderSize != 40)
|
73
|
+
throw std::runtime_error("Unsupported BMP header");
|
74
|
+
bool isOs2Bitmap = infoHeaderSize == 12;
|
75
|
+
|
76
|
+
unsigned width, height, bitCount, clrUsed;
|
77
|
+
if (isOs2Bitmap)
|
78
|
+
{
|
79
|
+
width = readVal<16>(reader);
|
80
|
+
height = readVal<16>(reader);
|
81
|
+
readVal<16>(reader); // planes
|
82
|
+
bitCount = readVal<16>(reader);
|
83
|
+
clrUsed = 1 << bitCount;
|
84
|
+
}
|
85
|
+
else
|
86
|
+
{
|
87
|
+
width = readVal<32>(reader);
|
88
|
+
height = readVal<32>(reader);
|
89
|
+
readVal<16>(reader); // planes
|
90
|
+
bitCount = readVal<16>(reader);
|
91
|
+
readVal<32>(reader); // compression
|
92
|
+
readVal<32>(reader); // image size
|
93
|
+
readVal<32>(reader); // xPelsPerMeter
|
94
|
+
readVal<32>(reader); // yPelsPerMeter
|
95
|
+
clrUsed = readVal<32>(reader);
|
96
|
+
readVal<32>(reader); // important colors
|
97
|
+
}
|
98
|
+
|
99
|
+
if (bitCount != 4 && bitCount != 8 && bitCount != 24)
|
100
|
+
throw std::runtime_error("Unsupported BMP file bit depth");
|
101
|
+
|
102
|
+
bmp.resize(width, height);
|
103
|
+
|
104
|
+
ColorTable colors;
|
105
|
+
if (bitCount != 24)
|
106
|
+
{
|
107
|
+
if (clrUsed == 0)
|
108
|
+
clrUsed = 1 << bitCount;
|
109
|
+
for (unsigned i = 0; i < clrUsed; ++i)
|
110
|
+
{
|
111
|
+
Gosu::Color color;
|
112
|
+
color.setAlpha(255);
|
113
|
+
color.setBlue(readVal<8>(reader));
|
114
|
+
color.setGreen(readVal<8>(reader));
|
115
|
+
color.setRed(readVal<8>(reader));
|
116
|
+
if (!isOs2Bitmap)
|
117
|
+
readVal<8>(reader); // reserved
|
118
|
+
colors.push_back(color);
|
119
|
+
}
|
120
|
+
}
|
121
|
+
|
122
|
+
int xOffset = 1;
|
123
|
+
if (bitCount == 4)
|
124
|
+
xOffset = 2;
|
125
|
+
|
126
|
+
unsigned pixelBytesPerRow = 0;
|
127
|
+
switch (bitCount)
|
128
|
+
{
|
129
|
+
case 4: pixelBytesPerRow = (width * 2 + 1) / 2; break;
|
130
|
+
case 8: pixelBytesPerRow = width;
|
131
|
+
case 24: pixelBytesPerRow = width * 3;
|
132
|
+
}
|
133
|
+
unsigned fillBytes = (4 - (pixelBytesPerRow % 4)) % 4;
|
134
|
+
|
135
|
+
if (bitCount == 24)
|
136
|
+
{
|
137
|
+
// Look-ahead-experiment.
|
138
|
+
// Reducing Reader::read calls => performance on *nix?
|
139
|
+
std::vector<boost::uint8_t> buf(height * (pixelBytesPerRow + fillBytes));
|
140
|
+
boost::uint8_t* ptr = &buf[0];
|
141
|
+
reader.read(ptr, buf.size());
|
142
|
+
for (int y = height - 1; y >= 0; --y)
|
143
|
+
{
|
144
|
+
for (unsigned x = 0; x < width; ++x)
|
145
|
+
{
|
146
|
+
Gosu::Color color;
|
147
|
+
color.setBlue(*ptr++);
|
148
|
+
color.setGreen(*ptr++);
|
149
|
+
color.setRed(*ptr++);
|
150
|
+
color.setAlpha(255);
|
151
|
+
bmp.setPixel(x, y, color);
|
152
|
+
}
|
153
|
+
ptr += fillBytes;
|
154
|
+
}
|
155
|
+
}
|
156
|
+
else
|
157
|
+
for (int y = height - 1; y >= 0; --y)
|
158
|
+
{
|
159
|
+
for (unsigned x = 0; x < width; x += xOffset)
|
160
|
+
decodePart(bitCount, bmp, reader, x, y, colors);
|
161
|
+
|
162
|
+
reader.seek(fillBytes);
|
163
|
+
}
|
164
|
+
|
165
|
+
return reader;
|
166
|
+
}
|
167
|
+
|
168
|
+
namespace
|
169
|
+
{
|
170
|
+
template<unsigned bits, typename T>
|
171
|
+
void writeVal(Gosu::Writer& writer, T value)
|
172
|
+
{
|
173
|
+
typename boost::uint_t<bits>::least val = value;
|
174
|
+
writer.writePod(val, Gosu::boLittle);
|
175
|
+
}
|
176
|
+
}
|
177
|
+
|
178
|
+
Gosu::Writer Gosu::saveToBMP(const Bitmap& bmp, Writer writer)
|
179
|
+
{
|
180
|
+
// rowSize is the width ceiled to the next multiple of four.
|
181
|
+
unsigned rowSize = bmp.width() * 3;
|
182
|
+
rowSize = int((float(rowSize) - 1.0)/4.0 + 1.0) * 4;
|
183
|
+
|
184
|
+
// File header
|
185
|
+
// Type
|
186
|
+
writeVal<8>(writer, 'B');
|
187
|
+
writeVal<8>(writer, 'M');
|
188
|
+
// Size
|
189
|
+
writeVal<32>(writer, 14 + 40 + rowSize * bmp.height());
|
190
|
+
// Reserved
|
191
|
+
writeVal<16>(writer, 0);
|
192
|
+
writeVal<16>(writer, 0);
|
193
|
+
// Offset to data
|
194
|
+
writeVal<32>(writer, 14 + 40);
|
195
|
+
|
196
|
+
// Info header
|
197
|
+
// Size
|
198
|
+
writeVal<32>(writer, 40);
|
199
|
+
// Width, height
|
200
|
+
writeVal<32>(writer, bmp.width());
|
201
|
+
writeVal<32>(writer, bmp.height());
|
202
|
+
// Planes
|
203
|
+
writeVal<16>(writer, 1);
|
204
|
+
// Bit count
|
205
|
+
writeVal<16>(writer, 24);
|
206
|
+
// Compression
|
207
|
+
writeVal<32>(writer, 0);
|
208
|
+
// Image size
|
209
|
+
writeVal<32>(writer, rowSize * bmp.height());
|
210
|
+
// xPelsPerMeter/yPelsPerMeter
|
211
|
+
writeVal<32>(writer, 0);
|
212
|
+
writeVal<32>(writer, 0);
|
213
|
+
// Used and important colors
|
214
|
+
writeVal<32>(writer, 0);
|
215
|
+
writeVal<32>(writer, 0);
|
216
|
+
|
217
|
+
for (int y = bmp.height() - 1; y >= 0; y--)
|
218
|
+
{
|
219
|
+
for (unsigned x = 0; x < bmp.width(); x++)
|
220
|
+
{
|
221
|
+
// BGR order
|
222
|
+
writeVal<8>(writer, bmp.getPixel(x, y).blue());
|
223
|
+
writeVal<8>(writer, bmp.getPixel(x, y).green());
|
224
|
+
writeVal<8>(writer, bmp.getPixel(x, y).red());
|
225
|
+
}
|
226
|
+
int offset = rowSize - bmp.width() * 3;
|
227
|
+
for(int i = 0; i < offset; ++i)
|
228
|
+
writeVal<8>(writer, 0);
|
229
|
+
}
|
230
|
+
return writer;
|
231
|
+
}
|
232
|
+
|
@@ -0,0 +1,39 @@
|
|
1
|
+
#include <Gosu/Bitmap.hpp>
|
2
|
+
#include <vector>
|
3
|
+
|
4
|
+
void Gosu::applyColorKey(Bitmap& bitmap, Color key)
|
5
|
+
{
|
6
|
+
std::vector<Color> surroundingColors;
|
7
|
+
surroundingColors.reserve(4);
|
8
|
+
|
9
|
+
for (unsigned y = 0; y < bitmap.height(); ++y)
|
10
|
+
for (unsigned x = 0; x < bitmap.width(); ++x)
|
11
|
+
if (bitmap.getPixel(x, y) == key)
|
12
|
+
{
|
13
|
+
surroundingColors.clear();
|
14
|
+
if (x > 0 && bitmap.getPixel(x - 1, y) != key)
|
15
|
+
surroundingColors.push_back(bitmap.getPixel(x - 1, y));
|
16
|
+
if (x < bitmap.width() - 1 && bitmap.getPixel(x + 1, y) != key)
|
17
|
+
surroundingColors.push_back(bitmap.getPixel(x + 1, y));
|
18
|
+
if (y > 0 && bitmap.getPixel(x, y - 1) != key)
|
19
|
+
surroundingColors.push_back(bitmap.getPixel(x, y - 1));
|
20
|
+
if (y < bitmap.height() - 1 && bitmap.getPixel(x, y + 1) != key)
|
21
|
+
surroundingColors.push_back(bitmap.getPixel(x, y + 1));
|
22
|
+
|
23
|
+
if (surroundingColors.empty())
|
24
|
+
{
|
25
|
+
bitmap.setPixel(x, y, Colors::none);
|
26
|
+
continue;
|
27
|
+
}
|
28
|
+
|
29
|
+
unsigned red = 0, green = 0, blue = 0;
|
30
|
+
for (unsigned i = 0; i < surroundingColors.size(); ++i)
|
31
|
+
{
|
32
|
+
red += surroundingColors[i].red();
|
33
|
+
green += surroundingColors[i].green();
|
34
|
+
blue += surroundingColors[i].blue();
|
35
|
+
}
|
36
|
+
bitmap.setPixel(x, y, Color(0, red / surroundingColors.size(),
|
37
|
+
green / surroundingColors.size(), blue / surroundingColors.size()));
|
38
|
+
}
|
39
|
+
}
|