gosu 0.7.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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,186 @@
|
|
1
|
+
#include <Gosu/Text.hpp>
|
2
|
+
#include <Gosu/Bitmap.hpp>
|
3
|
+
#include <Gosu/Utility.hpp>
|
4
|
+
|
5
|
+
#include <pango/pango.h>
|
6
|
+
#include <pango/pangoft2.h>
|
7
|
+
|
8
|
+
#include <glib.h>
|
9
|
+
|
10
|
+
#include <string>
|
11
|
+
|
12
|
+
std::wstring Gosu::defaultFontName()
|
13
|
+
{
|
14
|
+
return L"sans";
|
15
|
+
}
|
16
|
+
|
17
|
+
namespace Gosu
|
18
|
+
{
|
19
|
+
class pango
|
20
|
+
{
|
21
|
+
int width, height;
|
22
|
+
|
23
|
+
PangoContext* context;
|
24
|
+
PangoLayout* layout;
|
25
|
+
/*static*/ PangoFontDescription *font_description;
|
26
|
+
PangoAttribute* attr;
|
27
|
+
PangoAttrList* attrList;
|
28
|
+
|
29
|
+
public:
|
30
|
+
pango();
|
31
|
+
~pango();
|
32
|
+
unsigned textWidth(const std::wstring& text,
|
33
|
+
const std::wstring& fontFace, unsigned fontHeight,
|
34
|
+
unsigned fontFlags);
|
35
|
+
void drawText(Bitmap& bitmap, const std::wstring& text, int x, int y,
|
36
|
+
Color c, const std::wstring& fontFace, unsigned fontHeight,
|
37
|
+
unsigned fontFlags);
|
38
|
+
};
|
39
|
+
}
|
40
|
+
|
41
|
+
Gosu::pango::pango()
|
42
|
+
{
|
43
|
+
font_description = NULL;
|
44
|
+
attr = NULL;
|
45
|
+
attrList = NULL;
|
46
|
+
}
|
47
|
+
|
48
|
+
Gosu::pango::~pango()
|
49
|
+
{
|
50
|
+
g_object_unref(context);
|
51
|
+
g_object_unref(layout);
|
52
|
+
|
53
|
+
if(font_description)
|
54
|
+
pango_font_description_free(font_description);
|
55
|
+
if(attr)
|
56
|
+
pango_attribute_destroy(attr);
|
57
|
+
}
|
58
|
+
|
59
|
+
unsigned Gosu::pango::textWidth(const std::wstring& text,
|
60
|
+
const std::wstring& fontFace, unsigned fontHeight,
|
61
|
+
unsigned fontFlags)
|
62
|
+
{
|
63
|
+
g_type_init();
|
64
|
+
|
65
|
+
int dpi_x = 100, dpi_y = 100;
|
66
|
+
|
67
|
+
context = pango_ft2_get_context(dpi_x, dpi_y);
|
68
|
+
|
69
|
+
pango_context_set_language(context, pango_language_from_string ("en_US"));
|
70
|
+
PangoDirection init_dir = PANGO_DIRECTION_LTR;
|
71
|
+
pango_context_set_base_dir(context, init_dir);
|
72
|
+
|
73
|
+
// static PangoFontDescription *font_description;
|
74
|
+
font_description = pango_font_description_new();
|
75
|
+
|
76
|
+
pango_font_description_set_family(font_description,
|
77
|
+
g_strdup(narrow(fontFace).c_str()));
|
78
|
+
pango_font_description_set_style(font_description,
|
79
|
+
(fontFlags & ffItalic) ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL);
|
80
|
+
pango_font_description_set_variant(font_description, PANGO_VARIANT_NORMAL);
|
81
|
+
pango_font_description_set_weight(font_description,
|
82
|
+
(fontFlags & ffBold) ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL);
|
83
|
+
pango_font_description_set_stretch(font_description, PANGO_STRETCH_NORMAL);
|
84
|
+
int init_scale = int(fontHeight/2.0 + 0.5);
|
85
|
+
pango_font_description_set_size(font_description, init_scale * PANGO_SCALE);
|
86
|
+
|
87
|
+
pango_context_set_font_description(context, font_description);
|
88
|
+
|
89
|
+
|
90
|
+
layout = pango_layout_new(context);
|
91
|
+
|
92
|
+
|
93
|
+
if(fontFlags & ffUnderline)
|
94
|
+
{
|
95
|
+
// PangoAttribute *attr;
|
96
|
+
attr = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
|
97
|
+
attr->start_index = 0;
|
98
|
+
attr->end_index = text.length();
|
99
|
+
// PangoAttrList* attrList;
|
100
|
+
attrList = pango_attr_list_new();
|
101
|
+
pango_attr_list_insert(attrList, attr);
|
102
|
+
pango_layout_set_attributes(layout, attrList);
|
103
|
+
pango_attr_list_unref(attrList);
|
104
|
+
}
|
105
|
+
|
106
|
+
|
107
|
+
// IMPR: Catch errors? (Last NULL-Pointer)
|
108
|
+
gchar* utf8Str = g_ucs4_to_utf8((gunichar*)text.c_str(), text.length(), NULL, NULL, NULL);
|
109
|
+
pango_layout_set_text(layout, utf8Str, -1);
|
110
|
+
g_free(utf8Str);
|
111
|
+
|
112
|
+
PangoDirection base_dir = pango_context_get_base_dir(context);
|
113
|
+
pango_layout_set_alignment(layout,
|
114
|
+
base_dir == PANGO_DIRECTION_LTR ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT);
|
115
|
+
|
116
|
+
pango_layout_set_width(layout, -1);
|
117
|
+
|
118
|
+
PangoRectangle logical_rect;
|
119
|
+
|
120
|
+
pango_layout_get_pixel_extents(layout, NULL, &logical_rect);
|
121
|
+
height = logical_rect.height;
|
122
|
+
width = logical_rect.width;
|
123
|
+
|
124
|
+
return width;
|
125
|
+
}
|
126
|
+
|
127
|
+
void Gosu::pango::drawText(Bitmap& bitmap, const std::wstring& text, int x, int y,
|
128
|
+
Color c, const std::wstring& fontFace, unsigned fontHeight,
|
129
|
+
unsigned fontFlags)
|
130
|
+
{
|
131
|
+
textWidth(text, fontFace, fontHeight, fontFlags);
|
132
|
+
|
133
|
+
FT_Bitmap ft_bitmap;
|
134
|
+
|
135
|
+
guchar* buf = new guchar[width * height];
|
136
|
+
std::fill(buf, buf + width * height, 0x00);
|
137
|
+
|
138
|
+
ft_bitmap.rows = height;
|
139
|
+
ft_bitmap.width = width;
|
140
|
+
ft_bitmap.pitch = ft_bitmap.width;
|
141
|
+
ft_bitmap.buffer = buf;
|
142
|
+
ft_bitmap.num_grays = 256;
|
143
|
+
ft_bitmap.pixel_mode = ft_pixel_mode_grays;
|
144
|
+
|
145
|
+
int x_start = 0;
|
146
|
+
pango_ft2_render_layout(&ft_bitmap, layout, x_start, 0);
|
147
|
+
|
148
|
+
|
149
|
+
int min_height = height;
|
150
|
+
if((unsigned)height > fontHeight) min_height = fontHeight;
|
151
|
+
|
152
|
+
for(int y2 = 0; y2 < min_height; y2++)
|
153
|
+
{
|
154
|
+
if (y + y2 < 0 || y + y2 >= bitmap.height())
|
155
|
+
break;
|
156
|
+
|
157
|
+
for(int x2 = 0; x2 < width; x2++)
|
158
|
+
{
|
159
|
+
if (x + x2 < 0 || x + x2 >= bitmap.width())
|
160
|
+
break;
|
161
|
+
unsigned val = ft_bitmap.buffer[y2*width+x2];
|
162
|
+
Color color = c;
|
163
|
+
color.setAlpha(val);
|
164
|
+
bitmap.setPixel(x2 + x, y2 + y, color);
|
165
|
+
}
|
166
|
+
}
|
167
|
+
|
168
|
+
delete[] buf;
|
169
|
+
}
|
170
|
+
|
171
|
+
|
172
|
+
unsigned Gosu::textWidth(const std::wstring& text,
|
173
|
+
const std::wstring& fontFace, unsigned fontHeight,
|
174
|
+
unsigned fontFlags)
|
175
|
+
{
|
176
|
+
pango helper;
|
177
|
+
return helper.textWidth(text, fontFace, fontHeight, fontFlags);
|
178
|
+
}
|
179
|
+
|
180
|
+
void Gosu::drawText(Bitmap& bitmap, const std::wstring& text, int x, int y,
|
181
|
+
Color c, const std::wstring& fontFace, unsigned fontHeight,
|
182
|
+
unsigned fontFlags)
|
183
|
+
{
|
184
|
+
pango helper;
|
185
|
+
helper.drawText(bitmap, text, x, y, c, fontFace, fontHeight, fontFlags);
|
186
|
+
}
|
@@ -0,0 +1,172 @@
|
|
1
|
+
#define _WIN32_WINNT 0x0500
|
2
|
+
#include <windows.h>
|
3
|
+
|
4
|
+
#include <Gosu/Bitmap.hpp>
|
5
|
+
#include <Gosu/Text.hpp>
|
6
|
+
#include <Gosu/Utility.hpp>
|
7
|
+
#include <Gosu/WinUtility.hpp>
|
8
|
+
#include <boost/utility.hpp>
|
9
|
+
#include <cstdlib>
|
10
|
+
#include <cwchar>
|
11
|
+
#include <algorithm>
|
12
|
+
#include <stdexcept>
|
13
|
+
#include <map>
|
14
|
+
#include <set>
|
15
|
+
|
16
|
+
std::wstring Gosu::defaultFontName()
|
17
|
+
{
|
18
|
+
return L"Arial";
|
19
|
+
}
|
20
|
+
|
21
|
+
// IMPR: throwLastError/check is used a lot here, Win9x doesn't provide GDI
|
22
|
+
// error codes though. Wait until 9x is dead or fix this!
|
23
|
+
|
24
|
+
namespace Gosu
|
25
|
+
{
|
26
|
+
std::wstring getNameFromTTFFile(const std::wstring& filename);
|
27
|
+
|
28
|
+
namespace
|
29
|
+
{
|
30
|
+
class WinBitmap : boost::noncopyable
|
31
|
+
{
|
32
|
+
HDC dc;
|
33
|
+
HBITMAP bitmap;
|
34
|
+
char* pixels;
|
35
|
+
|
36
|
+
public:
|
37
|
+
WinBitmap(unsigned width, unsigned height)
|
38
|
+
{
|
39
|
+
dc = Win::check(::CreateCompatibleDC(0),
|
40
|
+
"creating a device context");
|
41
|
+
|
42
|
+
BITMAPCOREHEADER coreHeader;
|
43
|
+
coreHeader.bcSize = sizeof coreHeader;
|
44
|
+
coreHeader.bcWidth = width;
|
45
|
+
coreHeader.bcHeight = height;
|
46
|
+
coreHeader.bcPlanes = 1;
|
47
|
+
coreHeader.bcBitCount = 24;
|
48
|
+
|
49
|
+
bitmap = ::CreateDIBSection(dc,
|
50
|
+
reinterpret_cast<BITMAPINFO*>(&coreHeader), DIB_RGB_COLORS,
|
51
|
+
reinterpret_cast<VOID**>(&pixels), 0, 0);
|
52
|
+
if (bitmap == 0)
|
53
|
+
{
|
54
|
+
::DeleteDC(dc);
|
55
|
+
Win::throwLastError("creating a bitmap");
|
56
|
+
}
|
57
|
+
|
58
|
+
::SelectObject(dc, bitmap);
|
59
|
+
|
60
|
+
HBRUSH brush = ::CreateSolidBrush(0x000000);
|
61
|
+
RECT rc = { 0, 0, width, height };
|
62
|
+
::FillRect(dc, &rc, brush);
|
63
|
+
::DeleteObject(brush);
|
64
|
+
}
|
65
|
+
|
66
|
+
~WinBitmap()
|
67
|
+
{
|
68
|
+
::DeleteObject(bitmap);
|
69
|
+
::DeleteObject(::SelectObject(dc, ::GetStockObject(SYSTEM_FONT)));
|
70
|
+
::DeleteDC(dc);
|
71
|
+
}
|
72
|
+
|
73
|
+
HDC context() const
|
74
|
+
{
|
75
|
+
return dc;
|
76
|
+
}
|
77
|
+
|
78
|
+
HBITMAP handle() const
|
79
|
+
{
|
80
|
+
return bitmap;
|
81
|
+
}
|
82
|
+
|
83
|
+
/*char* data() const
|
84
|
+
{
|
85
|
+
return pixels;
|
86
|
+
}*/
|
87
|
+
|
88
|
+
void selectFont(std::wstring fontName, unsigned fontHeight,
|
89
|
+
unsigned fontFlags) const
|
90
|
+
{
|
91
|
+
static std::map<std::wstring, std::wstring> customFonts;
|
92
|
+
|
93
|
+
if (fontName.find(L"/") != std::wstring::npos)
|
94
|
+
{
|
95
|
+
if (customFonts.count(fontName) == 0)
|
96
|
+
{
|
97
|
+
AddFontResourceEx(fontName.c_str(), FR_PRIVATE, 0);
|
98
|
+
fontName = customFonts[fontName] = getNameFromTTFFile(fontName);
|
99
|
+
}
|
100
|
+
else
|
101
|
+
fontName = customFonts[fontName];
|
102
|
+
}
|
103
|
+
|
104
|
+
// IMPR: Maybe remember the last font we had and don't
|
105
|
+
// recreate it every time?
|
106
|
+
|
107
|
+
LOGFONT logfont = { fontHeight, 0, 0, 0,
|
108
|
+
fontFlags & ffBold ? FW_BOLD : FW_NORMAL,
|
109
|
+
fontFlags & ffItalic ? TRUE : FALSE,
|
110
|
+
fontFlags & ffUnderline ? TRUE : FALSE,
|
111
|
+
FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
|
112
|
+
CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY,
|
113
|
+
DEFAULT_PITCH | FF_DONTCARE };
|
114
|
+
|
115
|
+
// IMPR: This truncates. Is this reasonable? Should we rather
|
116
|
+
// throw something?
|
117
|
+
/*std::*/wcsncpy(logfont.lfFaceName, fontName.c_str(), LF_FACESIZE);
|
118
|
+
logfont.lfFaceName[LF_FACESIZE - 1] = 0;
|
119
|
+
|
120
|
+
HFONT newFont = Win::check(::CreateFontIndirect(&logfont),
|
121
|
+
"creating font object for " + narrow(fontName));
|
122
|
+
|
123
|
+
HFONT oldFont = reinterpret_cast<HFONT>(::SelectObject(dc, newFont));
|
124
|
+
Win::check(oldFont, "selecting the font object for " + narrow(fontName));
|
125
|
+
::DeleteObject(oldFont);
|
126
|
+
}
|
127
|
+
};
|
128
|
+
}
|
129
|
+
};
|
130
|
+
|
131
|
+
unsigned Gosu::textWidth(const std::wstring& text,
|
132
|
+
const std::wstring& fontName, unsigned fontHeight, unsigned fontFlags)
|
133
|
+
{
|
134
|
+
WinBitmap helper(1, 1);
|
135
|
+
helper.selectFont(fontName, fontHeight, fontFlags);
|
136
|
+
|
137
|
+
SIZE size;
|
138
|
+
if (!::GetTextExtentPoint32(helper.context(), text.c_str(), text.length(), &size))
|
139
|
+
Win::throwLastError("calculating the width of a text");
|
140
|
+
|
141
|
+
return size.cx;
|
142
|
+
}
|
143
|
+
|
144
|
+
void Gosu::drawText(Bitmap& bitmap, const std::wstring& text, int x, int y,
|
145
|
+
Color c, const std::wstring& fontName, unsigned fontHeight,
|
146
|
+
unsigned fontFlags)
|
147
|
+
{
|
148
|
+
unsigned width = textWidth(text, fontName, fontHeight, fontFlags);
|
149
|
+
|
150
|
+
WinBitmap helper(width, fontHeight);
|
151
|
+
helper.selectFont(fontName, fontHeight, fontFlags);
|
152
|
+
|
153
|
+
if (::SetTextColor(helper.context(), 0xffffff) == CLR_INVALID)
|
154
|
+
Win::throwLastError("setting the text color");
|
155
|
+
|
156
|
+
Win::check(::SetBkMode(helper.context(), TRANSPARENT),
|
157
|
+
"setting a bitmap's background mode to TRANSPARENT");
|
158
|
+
|
159
|
+
::ExtTextOut(helper.context(), 0, 0, 0, 0, text.c_str(), text.length(), 0);
|
160
|
+
|
161
|
+
for (unsigned relY = 0; relY < fontHeight; ++relY)
|
162
|
+
for (unsigned relX = 0; relX < width; ++relX)
|
163
|
+
{
|
164
|
+
Color pixel = c;
|
165
|
+
pixel.setAlpha(GetPixel(helper.context(), relX, relY) & 0xff);
|
166
|
+
// IMPR: Make better use of c.alpha()?
|
167
|
+
// Maybe even add an AlphaMode parameter?
|
168
|
+
if (pixel != 0 && x + relX >= 0 && x + relX < bitmap.width() &&
|
169
|
+
y + relY >= 0 && y + relY < bitmap.height())
|
170
|
+
bitmap.setPixel(x + relX, y + relY, pixel);
|
171
|
+
}
|
172
|
+
}
|
@@ -0,0 +1,104 @@
|
|
1
|
+
#include <GosuImpl/Graphics/Texture.hpp>
|
2
|
+
#include <GosuImpl/Graphics/TexChunk.hpp>
|
3
|
+
#include <Gosu/Bitmap.hpp>
|
4
|
+
#include <Gosu/Platform.hpp>
|
5
|
+
#include <stdexcept>
|
6
|
+
|
7
|
+
// TODO: Not threadsafe.
|
8
|
+
unsigned Gosu::Texture::maxTextureSize()
|
9
|
+
{
|
10
|
+
const static unsigned MIN_SIZE = 256, MAX_SIZE = 1024;
|
11
|
+
|
12
|
+
static unsigned size = 0;
|
13
|
+
if (size == 0)
|
14
|
+
{
|
15
|
+
size = MIN_SIZE / 2;
|
16
|
+
GLint width = 1;
|
17
|
+
do
|
18
|
+
{
|
19
|
+
size *= 2;
|
20
|
+
glTexImage2D(GL_PROXY_TEXTURE_2D, 0, 4, size * 2, size * 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
|
21
|
+
glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);
|
22
|
+
} while (width != 0 && size < MAX_SIZE);
|
23
|
+
}
|
24
|
+
|
25
|
+
return size;
|
26
|
+
}
|
27
|
+
|
28
|
+
Gosu::Texture::Texture(unsigned size)
|
29
|
+
: allocator(size, size), num(0)
|
30
|
+
{
|
31
|
+
// Create texture name.
|
32
|
+
glGenTextures(1, &name);
|
33
|
+
if (name == static_cast<GLuint>(-1))
|
34
|
+
throw std::runtime_error("Couldn't create OpenGL texture");
|
35
|
+
|
36
|
+
// Create empty texture.
|
37
|
+
glBindTexture(GL_TEXTURE_2D, name);
|
38
|
+
glTexImage2D(GL_TEXTURE_2D, 0, 4, allocator.width(), allocator.height(), 0,
|
39
|
+
GL_RGBA, GL_UNSIGNED_BYTE, 0);
|
40
|
+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
41
|
+
}
|
42
|
+
|
43
|
+
Gosu::Texture::~Texture()
|
44
|
+
{
|
45
|
+
glDeleteTextures(1, &name);
|
46
|
+
}
|
47
|
+
|
48
|
+
unsigned Gosu::Texture::size() const
|
49
|
+
{
|
50
|
+
return allocator.width(); // == height
|
51
|
+
}
|
52
|
+
|
53
|
+
GLuint Gosu::Texture::texName() const
|
54
|
+
{
|
55
|
+
return name;
|
56
|
+
}
|
57
|
+
|
58
|
+
std::auto_ptr<Gosu::TexChunk>
|
59
|
+
Gosu::Texture::tryAlloc(Graphics& graphics,
|
60
|
+
DrawOpQueue& queue, boost::shared_ptr<Texture> ptr,
|
61
|
+
const Bitmap& bmp, unsigned srcX,
|
62
|
+
unsigned srcY, unsigned srcWidth,
|
63
|
+
unsigned srcHeight, unsigned padding)
|
64
|
+
{
|
65
|
+
std::auto_ptr<Gosu::TexChunk> result;
|
66
|
+
|
67
|
+
boost::optional<BlockAllocator::Block> block = allocator.alloc(srcWidth, srcHeight);
|
68
|
+
if (!block)
|
69
|
+
return result;
|
70
|
+
|
71
|
+
result.reset(new TexChunk(graphics, queue, ptr, block->left + padding, block->top + padding,
|
72
|
+
block->width - 2 * padding, block->height - 2 * padding, padding));
|
73
|
+
|
74
|
+
#ifdef __BIG_ENDIAN__
|
75
|
+
std::vector<unsigned> pixelData(srcWidth * srcHeight);
|
76
|
+
for (unsigned y = 0; y < srcHeight; ++y)
|
77
|
+
for (unsigned x = 0; x < srcWidth; ++x)
|
78
|
+
{
|
79
|
+
boost::uint32_t pixVal = (bmp.getPixel(x, y).argb() & 0x00ffffff) << 8 | bmp.getPixel(x, y).alpha();
|
80
|
+
pixVal = bigToNative(pixVal);
|
81
|
+
pixelData[y * srcWidth + x] = pixVal;
|
82
|
+
}
|
83
|
+
const unsigned* texData = &pixelData[0];
|
84
|
+
unsigned format = GL_RGBA;
|
85
|
+
#else
|
86
|
+
#ifndef GL_BGRA
|
87
|
+
#define GL_BGRA 0x80E1
|
88
|
+
#endif
|
89
|
+
const unsigned* texData = bmp.glCompatibleData();
|
90
|
+
unsigned format = GL_BGRA;
|
91
|
+
#endif
|
92
|
+
|
93
|
+
glBindTexture(GL_TEXTURE_2D, name);
|
94
|
+
glTexSubImage2D(GL_TEXTURE_2D, 0, block->left, block->top, block->width, block->height,
|
95
|
+
format, GL_UNSIGNED_BYTE, texData);
|
96
|
+
num += 1;
|
97
|
+
return result;
|
98
|
+
}
|
99
|
+
|
100
|
+
void Gosu::Texture::free(unsigned x, unsigned y)
|
101
|
+
{
|
102
|
+
allocator.free(x, y);
|
103
|
+
num -= 1;
|
104
|
+
}
|