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.
- checksums.yaml +4 -4
- data/Gosu/Audio.hpp +35 -66
- data/Gosu/AutoLink.hpp +14 -16
- data/Gosu/Bitmap.hpp +50 -37
- data/Gosu/Buttons.hpp +246 -265
- data/Gosu/Color.hpp +32 -76
- data/Gosu/Directories.hpp +14 -17
- data/Gosu/Font.hpp +28 -34
- data/Gosu/Fwd.hpp +27 -31
- data/Gosu/Gosu.hpp +2 -5
- data/Gosu/Graphics.hpp +31 -48
- data/Gosu/GraphicsBase.hpp +27 -58
- data/Gosu/IO.hpp +44 -56
- data/Gosu/Image.hpp +29 -73
- data/Gosu/ImageData.hpp +13 -17
- data/Gosu/Input.hpp +42 -57
- data/Gosu/Inspection.hpp +2 -6
- data/Gosu/Math.hpp +32 -38
- data/Gosu/Platform.hpp +10 -29
- data/Gosu/Text.hpp +30 -39
- data/Gosu/TextInput.hpp +29 -36
- data/Gosu/Timing.hpp +14 -16
- data/Gosu/Utility.hpp +10 -15
- data/Gosu/Version.hpp +13 -14
- data/Gosu/Window.hpp +53 -68
- data/README.md +23 -11
- data/ext/gosu/extconf.rb +31 -81
- data/lib/gosu/patches.rb +35 -19
- data/lib/gosu/run.rb +13 -4
- data/rdoc/gosu.rb +24 -20
- data/src/ALChannelManagement.hpp +119 -0
- data/src/{Audio/Audio.cpp → Audio.cpp} +177 -211
- data/src/AudioFile.hpp +57 -0
- data/src/AudioToolboxFile.hpp +214 -0
- data/src/Bitmap.cpp +159 -0
- data/src/BitmapIO.cpp +141 -0
- data/src/BlockAllocator.cpp +133 -0
- data/src/{Graphics/BlockAllocator.hpp → BlockAllocator.hpp} +34 -35
- data/src/ClipRectStack.hpp +87 -0
- data/src/{Graphics/Color.cpp → Color.cpp} +30 -28
- data/src/DirectoriesApple.cpp +68 -0
- data/src/DirectoriesUnix.cpp +20 -18
- data/src/DirectoriesWin.cpp +40 -41
- data/src/DrawOp.hpp +168 -0
- data/src/DrawOpQueue.hpp +190 -0
- data/src/FileUnix.cpp +40 -46
- data/src/FileWin.cpp +42 -38
- data/src/Font.cpp +165 -0
- data/src/{Text/FormattedString.hpp → FormattedString.hpp} +114 -114
- data/src/GosuAppDelegate.cpp +30 -0
- data/src/{UIKit/GosuAppDelegate.h → GosuAppDelegate.h} +0 -0
- data/src/{UIKit/GosuGLView.mm → GosuGLView.cpp} +22 -17
- data/src/{UIKit/GosuGLView.h → GosuGLView.h} +0 -0
- data/src/GosuViewController.cpp +231 -0
- data/src/{UIKit/GosuViewController.h → GosuViewController.h} +0 -0
- data/src/Graphics.cpp +464 -0
- data/src/{Graphics/Common.hpp → GraphicsImpl.hpp} +29 -32
- data/src/IO.cpp +17 -16
- data/src/Iconv.hpp +13 -22
- data/src/Image.cpp +142 -0
- data/src/Input.cpp +459 -0
- data/src/InputUIKit.cpp +197 -0
- data/src/Inspection.cpp +4 -5
- data/src/LargeImageData.cpp +151 -0
- data/src/LargeImageData.hpp +43 -0
- data/src/{Graphics/Macro.cpp → Macro.cpp} +77 -78
- data/src/Macro.hpp +30 -0
- data/src/Math.cpp +17 -29
- data/src/{Audio/OggFile.hpp → OggFile.hpp} +19 -24
- data/src/RenderState.hpp +205 -0
- data/src/Resolution.cpp +86 -0
- data/src/ResolutionApple.cpp +25 -0
- data/{ext/gosu/gosu_wrap.cxx → src/RubyGosu.cxx} +2256 -1707
- data/{ext/gosu/gosu_wrap.h → src/RubyGosu.h} +9 -9
- data/src/{Audio/SndFile.hpp → SndFile.hpp} +54 -43
- data/src/TexChunk.cpp +117 -0
- data/src/{Graphics/TexChunk.hpp → TexChunk.hpp} +13 -18
- data/src/Text.cpp +371 -0
- data/src/TextApple.cpp +209 -0
- data/src/TextInput.cpp +278 -0
- data/src/TextTTFWin.cpp +251 -0
- data/src/{Text/TextUnix.cpp → TextUnix.cpp} +96 -92
- data/src/TextWin.cpp +194 -0
- data/src/{Graphics/Texture.cpp → Texture.cpp} +35 -38
- data/src/{Graphics/Texture.hpp → Texture.hpp} +9 -13
- data/src/TimingApple.cpp +11 -7
- data/src/TimingUnix.cpp +13 -7
- data/src/TimingWin.cpp +6 -1
- data/src/{Graphics/Transform.cpp → Transform.cpp} +17 -12
- data/src/{Graphics/TransformStack.hpp → TransformStack.hpp} +24 -25
- data/src/Utility.cpp +29 -70
- data/src/UtilityApple.cpp +52 -0
- data/src/UtilityWin.cpp +7 -4
- data/src/Version.cpp +22 -0
- data/src/WinMain.cpp +30 -33
- data/src/WinUtility.cpp +24 -22
- data/src/WinUtility.hpp +11 -20
- data/src/Window.cpp +142 -112
- data/src/WindowUIKit.cpp +155 -0
- data/src/stb_image.h +384 -173
- data/src/stb_vorbis.c +20 -18
- metadata +60 -62
- data/Gosu/TR1.hpp +0 -56
- data/src/AppleUtility.hpp +0 -66
- data/src/Audio/ALChannelManagement.hpp +0 -114
- data/src/Audio/Audio.mm +0 -1
- data/src/Audio/AudioFile.hpp +0 -53
- data/src/Audio/AudioToolboxFile.hpp +0 -207
- data/src/Bitmap/Bitmap.cpp +0 -183
- data/src/Bitmap/BitmapIO.cpp +0 -176
- data/src/DirectoriesApple.mm +0 -71
- data/src/Graphics/BlockAllocator.cpp +0 -142
- data/src/Graphics/ClipRectStack.hpp +0 -93
- data/src/Graphics/DrawOp.hpp +0 -175
- data/src/Graphics/DrawOpQueue.hpp +0 -188
- data/src/Graphics/Graphics.cpp +0 -478
- data/src/Graphics/Image.cpp +0 -193
- data/src/Graphics/LargeImageData.cpp +0 -133
- data/src/Graphics/LargeImageData.hpp +0 -46
- data/src/Graphics/Macro.hpp +0 -36
- data/src/Graphics/RenderState.hpp +0 -211
- data/src/Graphics/Resolution.cpp +0 -91
- data/src/Graphics/ResolutionApple.mm +0 -19
- data/src/Graphics/TexChunk.cpp +0 -112
- data/src/Input/Input.cpp +0 -463
- data/src/Input/InputUIKit.mm +0 -190
- data/src/Input/TextInput.cpp +0 -261
- data/src/Text/Font.cpp +0 -175
- data/src/Text/Text.cpp +0 -391
- data/src/Text/TextApple.mm +0 -227
- data/src/Text/TextTTFWin.cpp +0 -249
- data/src/Text/TextWin.cpp +0 -186
- data/src/UIKit/GosuAppDelegate.mm +0 -24
- data/src/UIKit/GosuViewController.mm +0 -211
- data/src/UtilityApple.mm +0 -63
- data/src/WindowUIKit.mm +0 -139
@@ -1,6 +1,6 @@
|
|
1
1
|
/* ----------------------------------------------------------------------------
|
2
2
|
* This file was automatically generated by SWIG (http://www.swig.org).
|
3
|
-
* Version 3.0.
|
3
|
+
* Version 3.0.11
|
4
4
|
*
|
5
5
|
* This file is not intended to be easily readable and contains a number of
|
6
6
|
* coding conventions designed to improve portability and efficiency. Do not make
|
@@ -21,25 +21,25 @@ class SwigDirector_TextInput : public Gosu::TextInput, public Swig::Director {
|
|
21
21
|
public:
|
22
22
|
SwigDirector_TextInput(VALUE self);
|
23
23
|
virtual ~SwigDirector_TextInput();
|
24
|
-
virtual std::
|
24
|
+
virtual std::string filter(std::string text) const;
|
25
25
|
};
|
26
26
|
|
27
27
|
|
28
28
|
class SwigDirector_Window : public Gosu::Window, public Swig::Director {
|
29
29
|
|
30
30
|
public:
|
31
|
-
SwigDirector_Window(VALUE self, unsigned int width, unsigned int height, bool fullscreen = false, double
|
31
|
+
SwigDirector_Window(VALUE self, unsigned int width, unsigned int height, bool fullscreen = false, double update_interval = 16.666666);
|
32
32
|
virtual ~SwigDirector_Window();
|
33
33
|
virtual void show();
|
34
34
|
virtual bool tick();
|
35
35
|
virtual void update();
|
36
36
|
virtual void draw();
|
37
|
-
virtual bool
|
38
|
-
virtual bool
|
39
|
-
virtual void
|
40
|
-
virtual void
|
41
|
-
virtual void
|
42
|
-
virtual void
|
37
|
+
virtual bool needs_redraw() const;
|
38
|
+
virtual bool needs_cursor() const;
|
39
|
+
virtual void lose_focus();
|
40
|
+
virtual void release_memory();
|
41
|
+
virtual void button_down(Gosu::Button arg0);
|
42
|
+
virtual void button_up(Gosu::Button arg0);
|
43
43
|
};
|
44
44
|
|
45
45
|
|
@@ -1,11 +1,15 @@
|
|
1
|
-
#
|
2
|
-
#define GOSU_SRC_AUDIO_SNDFILE_HPP
|
1
|
+
#pragma once
|
3
2
|
|
4
3
|
#include <Gosu/Audio.hpp>
|
5
4
|
#include <Gosu/Platform.hpp>
|
6
5
|
#include <Gosu/Utility.hpp>
|
7
6
|
#include <sndfile.h>
|
8
7
|
|
8
|
+
#ifdef GOSU_IS_WIN
|
9
|
+
#define NOMINMAX
|
10
|
+
#include <windows.h>
|
11
|
+
#endif
|
12
|
+
|
9
13
|
namespace Gosu
|
10
14
|
{
|
11
15
|
class SndFile : public AudioFile
|
@@ -21,60 +25,65 @@ namespace Gosu
|
|
21
25
|
static HMODULE dll()
|
22
26
|
{
|
23
27
|
static HMODULE dll = LoadLibrary(L"libsndfile.dll");
|
24
|
-
if (!dll)
|
25
|
-
throw std::runtime_error("Cannot find libsndfile.dll");
|
26
|
-
|
28
|
+
if (!dll) throw std::runtime_error("Cannot find libsndfile.dll");
|
27
29
|
return dll;
|
28
30
|
}
|
29
31
|
|
30
32
|
#define CREATE_STUB(NAME, RETURN, PARAMS, NAMES) \
|
31
33
|
static RETURN NAME PARAMS \
|
32
34
|
{ \
|
33
|
-
typedef RETURN (__cdecl *NAME##_ptr) PARAMS;
|
35
|
+
typedef RETURN (__cdecl *NAME##_ptr) PARAMS; \
|
34
36
|
static NAME##_ptr f = (NAME##_ptr)GetProcAddress(dll(), #NAME); \
|
35
|
-
if (!f)
|
37
|
+
if (!f) { \
|
36
38
|
throw std::runtime_error("Cannot find " ## #NAME); \
|
39
|
+
} \
|
37
40
|
return f NAMES; \
|
38
41
|
}
|
39
42
|
CREATE_STUB(sf_open_virtual, SNDFILE*,
|
40
43
|
(SF_VIRTUAL_IO* sfvirtual, int mode, SF_INFO* sfinfo, void* user_data),
|
41
44
|
(sfvirtual, mode, sfinfo, user_data))
|
42
45
|
CREATE_STUB(sf_close, int,
|
43
|
-
(SNDFILE
|
46
|
+
(SNDFILE* sndfile),
|
44
47
|
(sndfile))
|
45
48
|
CREATE_STUB(sf_readf_short, sf_count_t,
|
46
|
-
(SNDFILE
|
49
|
+
(SNDFILE* sndfile, short* ptr, sf_count_t items),
|
47
50
|
(sndfile, ptr, items))
|
48
51
|
CREATE_STUB(sf_seek, sf_count_t,
|
49
|
-
(SNDFILE
|
52
|
+
(SNDFILE* sndfile, sf_count_t frames, int whence),
|
50
53
|
(sndfile, frames, whence))
|
51
54
|
CREATE_STUB(sf_strerror, const char*,
|
52
55
|
(SNDFILE* sndfile),
|
53
56
|
(sndfile))
|
57
|
+
#undef CREATE_STUB
|
54
58
|
#endif
|
55
59
|
|
56
|
-
static sf_count_t get_filelen(SndFile
|
60
|
+
static sf_count_t get_filelen(SndFile* self)
|
57
61
|
{
|
58
62
|
return self->buffer.size();
|
59
63
|
}
|
60
64
|
|
61
|
-
static sf_count_t seek(sf_count_t offset, int whence, SndFile
|
65
|
+
static sf_count_t seek(sf_count_t offset, int whence, SndFile* self)
|
62
66
|
{
|
63
|
-
switch (whence)
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
case
|
67
|
+
switch (whence) {
|
68
|
+
case SEEK_SET:
|
69
|
+
self->reader.set_position(offset);
|
70
|
+
break;
|
71
|
+
case SEEK_CUR:
|
72
|
+
self->reader.seek(offset);
|
73
|
+
break;
|
74
|
+
case SEEK_END:
|
75
|
+
self->reader.set_position(self->buffer.size() - offset);
|
76
|
+
break;
|
68
77
|
}
|
69
78
|
|
70
79
|
if (self->reader.position() > self->buffer.size()) {
|
71
|
-
self->reader.
|
80
|
+
self->reader.set_position(self->buffer.size());
|
72
81
|
}
|
73
|
-
|
82
|
+
|
74
83
|
return 0;
|
75
84
|
}
|
76
85
|
|
77
|
-
static sf_count_t read(void
|
86
|
+
static sf_count_t read(void* ptr, sf_count_t count, SndFile* self)
|
78
87
|
{
|
79
88
|
sf_count_t avail = self->buffer.size() - self->reader.position();
|
80
89
|
count = std::min(avail, count);
|
@@ -82,58 +91,62 @@ namespace Gosu
|
|
82
91
|
return count;
|
83
92
|
}
|
84
93
|
|
85
|
-
static sf_count_t tell(SndFile
|
94
|
+
static sf_count_t tell(SndFile* self)
|
86
95
|
{
|
87
96
|
return self->reader.position();
|
88
97
|
}
|
89
98
|
|
90
|
-
static SF_VIRTUAL_IO*
|
99
|
+
static SF_VIRTUAL_IO* io_interface()
|
91
100
|
{
|
92
101
|
static SF_VIRTUAL_IO io;
|
93
102
|
io.get_filelen = (sf_vio_get_filelen)&get_filelen;
|
94
103
|
io.seek = (sf_vio_seek)&seek;
|
95
104
|
io.read = (sf_vio_read)&read;
|
96
105
|
io.tell = (sf_vio_tell)&tell;
|
97
|
-
io.write =
|
106
|
+
io.write = nullptr;
|
98
107
|
return &io;
|
99
108
|
}
|
100
109
|
|
101
110
|
public:
|
102
111
|
SndFile(Reader reader)
|
103
|
-
:
|
112
|
+
: file(nullptr), reader(buffer.front_reader())
|
104
113
|
{
|
105
114
|
info.format = 0;
|
106
115
|
buffer.resize(reader.resource().size() - reader.position());
|
107
116
|
reader.read(buffer.data(), buffer.size());
|
108
|
-
file = sf_open_virtual(
|
109
|
-
if (!file)
|
110
|
-
throw std::runtime_error(
|
117
|
+
file = sf_open_virtual(io_interface(), SFM_READ, &info, this);
|
118
|
+
if (!file) {
|
119
|
+
throw std::runtime_error(sf_strerror(nullptr));
|
120
|
+
}
|
111
121
|
}
|
112
122
|
|
113
|
-
SndFile(const std::
|
114
|
-
:
|
123
|
+
SndFile(const std::string& filename)
|
124
|
+
: file(nullptr), reader(buffer.front_reader())
|
115
125
|
{
|
116
126
|
info.format = 0;
|
127
|
+
// TODO: Not sure if this is still necessary.
|
128
|
+
// Can libsndfile open UTF-8 filenames on Windows?
|
117
129
|
#ifdef GOSU_IS_WIN
|
118
|
-
|
119
|
-
file = sf_open_virtual(
|
130
|
+
load_file(buffer, filename);
|
131
|
+
file = sf_open_virtual(io_interface(), SFM_READ, &info, this);
|
120
132
|
#else
|
121
|
-
file = sf_open(
|
133
|
+
file = sf_open(filename.c_str(), SFM_READ, &info);
|
122
134
|
#endif
|
123
|
-
if (!file)
|
124
|
-
throw std::runtime_error(
|
135
|
+
if (!file) {
|
136
|
+
throw std::runtime_error(sf_strerror(nullptr));
|
137
|
+
}
|
125
138
|
}
|
126
139
|
|
127
140
|
~SndFile()
|
128
141
|
{
|
129
|
-
if (file)
|
142
|
+
if (file) {
|
130
143
|
sf_close(file);
|
144
|
+
}
|
131
145
|
}
|
132
146
|
|
133
147
|
ALenum format() const
|
134
148
|
{
|
135
|
-
switch (info.channels)
|
136
|
-
{
|
149
|
+
switch (info.channels) {
|
137
150
|
case 1:
|
138
151
|
return AL_FORMAT_MONO16;
|
139
152
|
case 2:
|
@@ -143,15 +156,15 @@ namespace Gosu
|
|
143
156
|
};
|
144
157
|
}
|
145
158
|
|
146
|
-
ALuint
|
159
|
+
ALuint sample_rate() const
|
147
160
|
{
|
148
161
|
return info.samplerate;
|
149
162
|
}
|
150
163
|
|
151
|
-
std::size_t
|
164
|
+
std::size_t read_data(void* dest, std::size_t length)
|
152
165
|
{
|
153
|
-
int
|
154
|
-
return sf_readf_short(file, (short*)dest, length /
|
166
|
+
int frame_size = sizeof(short) * info.channels;
|
167
|
+
return sf_readf_short(file, (short*)dest, length / frame_size) * frame_size;
|
155
168
|
}
|
156
169
|
|
157
170
|
void rewind()
|
@@ -160,5 +173,3 @@ namespace Gosu
|
|
160
173
|
}
|
161
174
|
};
|
162
175
|
}
|
163
|
-
|
164
|
-
#endif
|
data/src/TexChunk.cpp
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
#include "TexChunk.hpp"
|
2
|
+
#include "DrawOpQueue.hpp"
|
3
|
+
#include "Texture.hpp"
|
4
|
+
#include <Gosu/Bitmap.hpp>
|
5
|
+
#include <Gosu/Graphics.hpp>
|
6
|
+
|
7
|
+
void Gosu::TexChunk::set_tex_info()
|
8
|
+
{
|
9
|
+
info.tex_name = texture->tex_name();
|
10
|
+
float texture_size = texture->size();
|
11
|
+
info.left = x / texture_size;
|
12
|
+
info.top = y / texture_size;
|
13
|
+
info.right = (x + w) / texture_size;
|
14
|
+
info.bottom = (y + h) / texture_size;
|
15
|
+
}
|
16
|
+
|
17
|
+
Gosu::TexChunk::TexChunk(std::shared_ptr<Texture> texture, int x, int y, int w, int h, int padding)
|
18
|
+
: texture(texture), x(x), y(y), w(w), h(h), padding(padding)
|
19
|
+
{
|
20
|
+
set_tex_info();
|
21
|
+
}
|
22
|
+
|
23
|
+
Gosu::TexChunk::TexChunk(const TexChunk& parent_chunk, int x, int y, int w, int h)
|
24
|
+
: texture(parent_chunk.texture), x(parent_chunk.x + x), y(parent_chunk.y + y), w(w), h(h),
|
25
|
+
padding(0)
|
26
|
+
{
|
27
|
+
set_tex_info();
|
28
|
+
texture->block(this->x, this->y, this->w, this->h);
|
29
|
+
}
|
30
|
+
|
31
|
+
Gosu::TexChunk::~TexChunk()
|
32
|
+
{
|
33
|
+
texture->free(x - padding, y - padding, w + 2 * padding, h + 2 * padding);
|
34
|
+
}
|
35
|
+
|
36
|
+
void Gosu::TexChunk::draw(double x1, double y1, Color c1, double x2, double y2, Color c2,
|
37
|
+
double x3, double y3, Color c3, double x4, double y4, Color c4, ZPos z, AlphaMode mode) const
|
38
|
+
{
|
39
|
+
DrawOp op;
|
40
|
+
op.render_state.texture = texture;
|
41
|
+
op.render_state.mode = mode;
|
42
|
+
|
43
|
+
normalize_coordinates(x1, y1, x2, y2, x3, y3, c3, x4, y4, c4);
|
44
|
+
|
45
|
+
op.vertices_or_block_index = 4;
|
46
|
+
op.vertices[0] = DrawOp::Vertex(x1, y1, c1);
|
47
|
+
op.vertices[1] = DrawOp::Vertex(x2, y2, c2);
|
48
|
+
// TODO: Should be harmonized
|
49
|
+
#ifdef GOSU_IS_OPENGLES
|
50
|
+
op.vertices[2] = DrawOp::Vertex(x3, y3, c3);
|
51
|
+
op.vertices[3] = DrawOp::Vertex(x4, y4, c4);
|
52
|
+
#else
|
53
|
+
op.vertices[3] = DrawOp::Vertex(x3, y3, c3);
|
54
|
+
op.vertices[2] = DrawOp::Vertex(x4, y4, c4);
|
55
|
+
#endif
|
56
|
+
op.left = info.left;
|
57
|
+
op.top = info.top;
|
58
|
+
op.right = info.right;
|
59
|
+
op.bottom = info.bottom;
|
60
|
+
|
61
|
+
op.z = z;
|
62
|
+
Graphics::schedule_draw_op(op);
|
63
|
+
}
|
64
|
+
|
65
|
+
const Gosu::GLTexInfo* Gosu::TexChunk::gl_tex_info() const
|
66
|
+
{
|
67
|
+
return &info;
|
68
|
+
}
|
69
|
+
|
70
|
+
Gosu::Bitmap Gosu::TexChunk::to_bitmap() const
|
71
|
+
{
|
72
|
+
return texture->to_bitmap(x, y, w, h);
|
73
|
+
}
|
74
|
+
|
75
|
+
std::unique_ptr<Gosu::ImageData> Gosu::TexChunk::subimage(int x, int y, int width, int height) const
|
76
|
+
{
|
77
|
+
return std::unique_ptr<Gosu::ImageData>(new TexChunk(*this, x, y, width, height));
|
78
|
+
}
|
79
|
+
|
80
|
+
void Gosu::TexChunk::insert(const Bitmap& original, int x, int y)
|
81
|
+
{
|
82
|
+
// TODO: Should respect border_flags.
|
83
|
+
|
84
|
+
Bitmap alternate;
|
85
|
+
const Bitmap* bitmap = &original;
|
86
|
+
|
87
|
+
if (x < 0 || y < 0 || x + original.width() > w || y + original.height() > h) {
|
88
|
+
int offset_x = 0, offset_y = 0;
|
89
|
+
int trimmed_width = original.width(), trimmed_height = original.height();
|
90
|
+
if (x < 0) {
|
91
|
+
offset_x = x;
|
92
|
+
trimmed_width += x;
|
93
|
+
x = 0;
|
94
|
+
}
|
95
|
+
if (y < 0) {
|
96
|
+
offset_y = y;
|
97
|
+
trimmed_height += y;
|
98
|
+
y = 0;
|
99
|
+
}
|
100
|
+
if (x + trimmed_width > w) {
|
101
|
+
trimmed_width -= (w - x - trimmed_width);
|
102
|
+
}
|
103
|
+
if (y + trimmed_height > h) {
|
104
|
+
trimmed_height -= (h - y - trimmed_height);
|
105
|
+
}
|
106
|
+
|
107
|
+
if (trimmed_width <= 0 || trimmed_height <= 0) return;
|
108
|
+
|
109
|
+
alternate.resize(trimmed_width, trimmed_height);
|
110
|
+
alternate.insert(original, offset_x, offset_y);
|
111
|
+
bitmap = &alternate;
|
112
|
+
}
|
113
|
+
|
114
|
+
glBindTexture(GL_TEXTURE_2D, tex_name());
|
115
|
+
glTexSubImage2D(GL_TEXTURE_2D, 0, this->x + x, this->y + y, bitmap->width(), bitmap->height(),
|
116
|
+
Color::GL_FORMAT, GL_UNSIGNED_BYTE, bitmap->data());
|
117
|
+
}
|
@@ -1,27 +1,25 @@
|
|
1
|
-
#
|
2
|
-
#define GOSU_SRC_GRAPHICS_TEXCHUNK_HPP
|
1
|
+
#pragma once
|
3
2
|
|
3
|
+
#include "GraphicsImpl.hpp"
|
4
4
|
#include <Gosu/Fwd.hpp>
|
5
5
|
#include <Gosu/ImageData.hpp>
|
6
|
-
#include <Gosu/TR1.hpp>
|
7
|
-
#include "Common.hpp"
|
8
6
|
#include <memory>
|
9
|
-
#include <vector>
|
10
7
|
#include <stdexcept>
|
8
|
+
#include <vector>
|
11
9
|
|
12
10
|
class Gosu::TexChunk : public Gosu::ImageData
|
13
11
|
{
|
14
|
-
std::
|
12
|
+
std::shared_ptr<Texture> texture;
|
15
13
|
int x, y, w, h, padding;
|
16
14
|
|
17
15
|
// Cached for faster access.
|
18
16
|
GLTexInfo info;
|
19
17
|
|
20
|
-
void
|
18
|
+
void set_tex_info();
|
21
19
|
|
22
20
|
public:
|
23
|
-
TexChunk(std::
|
24
|
-
TexChunk(const TexChunk&
|
21
|
+
TexChunk(std::shared_ptr<Texture> texture, int x, int y, int w, int h, int padding);
|
22
|
+
TexChunk(const TexChunk& parent_chunk, int x, int y, int w, int h);
|
25
23
|
~TexChunk();
|
26
24
|
|
27
25
|
int width() const
|
@@ -34,9 +32,9 @@ public:
|
|
34
32
|
return h;
|
35
33
|
}
|
36
34
|
|
37
|
-
GLuint
|
35
|
+
GLuint tex_name() const
|
38
36
|
{
|
39
|
-
return info.
|
37
|
+
return info.tex_name;
|
40
38
|
}
|
41
39
|
|
42
40
|
void draw(double x1, double y1, Color c1,
|
@@ -44,12 +42,9 @@ public:
|
|
44
42
|
double x3, double y3, Color c3,
|
45
43
|
double x4, double y4, Color c4,
|
46
44
|
ZPos z, AlphaMode mode) const;
|
47
|
-
|
48
|
-
const GLTexInfo*
|
49
|
-
Gosu::Bitmap
|
50
|
-
|
45
|
+
|
46
|
+
const GLTexInfo* gl_tex_info() const;
|
47
|
+
Gosu::Bitmap to_bitmap() const;
|
48
|
+
std::unique_ptr<ImageData> subimage(int x, int y, int width, int height) const;
|
51
49
|
void insert(const Bitmap& bitmap, int x, int y);
|
52
50
|
};
|
53
|
-
|
54
|
-
#endif
|
55
|
-
|
data/src/Text.cpp
ADDED
@@ -0,0 +1,371 @@
|
|
1
|
+
#include "FormattedString.hpp"
|
2
|
+
#include "GraphicsImpl.hpp"
|
3
|
+
#include <Gosu/Bitmap.hpp>
|
4
|
+
#include <Gosu/Graphics.hpp>
|
5
|
+
#include <Gosu/Image.hpp>
|
6
|
+
#include <Gosu/Math.hpp>
|
7
|
+
#include <Gosu/Text.hpp>
|
8
|
+
#include <Gosu/Utility.hpp>
|
9
|
+
#include <cassert>
|
10
|
+
#include <cmath>
|
11
|
+
#include <algorithm>
|
12
|
+
#include <map>
|
13
|
+
#include <vector>
|
14
|
+
using namespace std;
|
15
|
+
|
16
|
+
namespace Gosu
|
17
|
+
{
|
18
|
+
namespace
|
19
|
+
{
|
20
|
+
// Just a very simple heuristic that should make Chinese and Japanese text working in most
|
21
|
+
// of the cases.
|
22
|
+
bool is_breaking_asian_glyph(wchar_t ch)
|
23
|
+
{
|
24
|
+
return (ch >= 0x3040 && ch <= 0x3096) || // Hiragana
|
25
|
+
(ch >= 0x30a0 && ch <= 0x30fa) || // Katakana
|
26
|
+
(ch >= 0x4e00 && ch <= 0x9fff) || // CJK Unfied Ideographs
|
27
|
+
(ch >= 0x3400 && ch <= 0x4db5); // CJK Unified Ideographs Extension A
|
28
|
+
}
|
29
|
+
|
30
|
+
struct WordInfo
|
31
|
+
{
|
32
|
+
FormattedString text;
|
33
|
+
unsigned width;
|
34
|
+
unsigned space_width;
|
35
|
+
};
|
36
|
+
typedef vector<WordInfo> Words;
|
37
|
+
|
38
|
+
// Local helper class which manages building the bitmap from the
|
39
|
+
// collected words.
|
40
|
+
class TextBlockBuilder
|
41
|
+
{
|
42
|
+
Bitmap bmp;
|
43
|
+
unsigned used_lines, allocated_lines;
|
44
|
+
|
45
|
+
string font_name;
|
46
|
+
unsigned font_height;
|
47
|
+
int line_spacing;
|
48
|
+
Alignment align;
|
49
|
+
|
50
|
+
unsigned space_width_;
|
51
|
+
|
52
|
+
void alloc_next_line()
|
53
|
+
{
|
54
|
+
++used_lines;
|
55
|
+
if (used_lines == allocated_lines) {
|
56
|
+
allocated_lines += 10;
|
57
|
+
bmp.resize(bmp.width(),
|
58
|
+
font_height * allocated_lines + line_spacing * (allocated_lines - 1),
|
59
|
+
0x00ffffff);
|
60
|
+
}
|
61
|
+
}
|
62
|
+
|
63
|
+
public:
|
64
|
+
TextBlockBuilder(const string& font_name, unsigned font_height, int line_spacing,
|
65
|
+
unsigned width, Alignment align)
|
66
|
+
{
|
67
|
+
used_lines = 0;
|
68
|
+
allocated_lines = 10;
|
69
|
+
|
70
|
+
bmp.resize(width, (line_spacing + font_height) * allocated_lines, 0x00ffffff);
|
71
|
+
|
72
|
+
this->font_name = font_name;
|
73
|
+
this->font_height = font_height;
|
74
|
+
this->line_spacing = line_spacing;
|
75
|
+
this->align = align;
|
76
|
+
|
77
|
+
space_width_ = text_width(FormattedString(L" ", 0));
|
78
|
+
}
|
79
|
+
|
80
|
+
unsigned width() const
|
81
|
+
{
|
82
|
+
return bmp.width();
|
83
|
+
}
|
84
|
+
|
85
|
+
unsigned text_width(const FormattedString& text) const
|
86
|
+
{
|
87
|
+
if (text.length() == 0) {
|
88
|
+
return 0;
|
89
|
+
}
|
90
|
+
|
91
|
+
if (text.entity_at(0)) {
|
92
|
+
return entity_bitmap(text.entity_at(0)).width();
|
93
|
+
}
|
94
|
+
|
95
|
+
vector<FormattedString> parts = text.split_parts();
|
96
|
+
unsigned result = 0;
|
97
|
+
for (auto& part : parts) {
|
98
|
+
string text = wstring_to_utf8(part.unformat());
|
99
|
+
result += Gosu::text_width(text, font_name, font_height, part.flags_at(0));
|
100
|
+
}
|
101
|
+
return result;
|
102
|
+
}
|
103
|
+
|
104
|
+
void add_line(Words::const_iterator begin, Words::const_iterator end,
|
105
|
+
unsigned words_width, bool override_align)
|
106
|
+
{
|
107
|
+
alloc_next_line();
|
108
|
+
|
109
|
+
unsigned words = end - begin;
|
110
|
+
|
111
|
+
unsigned total_spacing = 0;
|
112
|
+
if (begin < end) {
|
113
|
+
for (auto i = begin; i != end - 1; ++i) {
|
114
|
+
total_spacing += i->space_width;
|
115
|
+
}
|
116
|
+
}
|
117
|
+
|
118
|
+
// Where does the line start? (y)
|
119
|
+
unsigned top = (used_lines - 1) * (font_height + line_spacing);
|
120
|
+
|
121
|
+
// Where does the line start? (x)
|
122
|
+
double pos;
|
123
|
+
switch (align) {
|
124
|
+
// Start so that the text touches the right border.
|
125
|
+
case AL_RIGHT:
|
126
|
+
pos = bmp.width() - words_width - total_spacing;
|
127
|
+
break;
|
128
|
+
|
129
|
+
// Start so that the text is centered.
|
130
|
+
case AL_CENTER:
|
131
|
+
pos = bmp.width() - words_width - total_spacing;
|
132
|
+
pos /= 2;
|
133
|
+
break;
|
134
|
+
|
135
|
+
// Just start at the left border.
|
136
|
+
default:
|
137
|
+
pos = 0;
|
138
|
+
}
|
139
|
+
|
140
|
+
for (auto cur = begin; cur != end; ++cur) {
|
141
|
+
vector<FormattedString> parts = cur->text.split_parts();
|
142
|
+
int x = 0;
|
143
|
+
for (auto& part : parts) {
|
144
|
+
if (part.entity_at(0)) {
|
145
|
+
Gosu::Bitmap entity = entity_bitmap(part.entity_at(0));
|
146
|
+
multiply_bitmap_alpha(entity, part.color_at(0).alpha());
|
147
|
+
bmp.insert(entity, trunc(pos) + x, trunc(top));
|
148
|
+
x += entity.width();
|
149
|
+
continue;
|
150
|
+
}
|
151
|
+
|
152
|
+
string unformatted_part = wstring_to_utf8(part.unformat());
|
153
|
+
draw_text(bmp, unformatted_part, trunc(pos) + x, trunc(top),
|
154
|
+
part.color_at(0), font_name, font_height, part.flags_at(0));
|
155
|
+
|
156
|
+
x += Gosu::text_width(unformatted_part, font_name, font_height,
|
157
|
+
part.flags_at(0));
|
158
|
+
}
|
159
|
+
|
160
|
+
if (align == AL_JUSTIFY && !override_align) {
|
161
|
+
pos += cur->width + 1.0 * (width() - words_width) / (words - 1);
|
162
|
+
}
|
163
|
+
else {
|
164
|
+
pos += cur->width + cur->space_width;
|
165
|
+
}
|
166
|
+
}
|
167
|
+
}
|
168
|
+
|
169
|
+
void add_empty_line()
|
170
|
+
{
|
171
|
+
alloc_next_line();
|
172
|
+
}
|
173
|
+
|
174
|
+
Bitmap result() const
|
175
|
+
{
|
176
|
+
Bitmap result = bmp;
|
177
|
+
result.resize(result.width(),
|
178
|
+
font_height * used_lines + line_spacing * (used_lines - 1),
|
179
|
+
0x00ffffff);
|
180
|
+
return result;
|
181
|
+
}
|
182
|
+
|
183
|
+
unsigned space_width() const
|
184
|
+
{
|
185
|
+
return space_width_;
|
186
|
+
}
|
187
|
+
};
|
188
|
+
|
189
|
+
void process_words(TextBlockBuilder& builder, const Words& words)
|
190
|
+
{
|
191
|
+
if (words.empty()) return builder.add_empty_line();
|
192
|
+
|
193
|
+
// Index into words to the first word in the current line.
|
194
|
+
auto line_begin = words.begin();
|
195
|
+
|
196
|
+
// Used width, in pixels, of the words [line_begin..w[.
|
197
|
+
unsigned words_width = 0;
|
198
|
+
|
199
|
+
// Used width of the spaces between (w-line_begin) words.
|
200
|
+
unsigned spaces_width = 0;
|
201
|
+
|
202
|
+
for (auto w = words.begin(); w != words.end(); ++w) {
|
203
|
+
unsigned new_words_width = words_width + w->width;
|
204
|
+
|
205
|
+
if (new_words_width + spaces_width <= builder.width()) {
|
206
|
+
// There's enough space for the words [line_begin..w] plus
|
207
|
+
// the spaces between them: Proceed with the next word.
|
208
|
+
words_width = new_words_width;
|
209
|
+
spaces_width += w->space_width;
|
210
|
+
}
|
211
|
+
else {
|
212
|
+
// No, this word wouldn't fit into the current line: Draw
|
213
|
+
// the current line, then start a new line with the current
|
214
|
+
// word.
|
215
|
+
builder.add_line(line_begin, w, words_width, false);
|
216
|
+
|
217
|
+
line_begin = w;
|
218
|
+
words_width = w->width;
|
219
|
+
spaces_width = w->space_width;
|
220
|
+
}
|
221
|
+
}
|
222
|
+
|
223
|
+
// Draw the last line as well.
|
224
|
+
if (words.empty() || line_begin != words.end()) {
|
225
|
+
builder.add_line(line_begin, words.end(), words_width, true);
|
226
|
+
}
|
227
|
+
}
|
228
|
+
|
229
|
+
void process_paragraph(TextBlockBuilder& builder, const FormattedString& paragraph)
|
230
|
+
{
|
231
|
+
Words collected_words;
|
232
|
+
|
233
|
+
unsigned begin_of_word = 0;
|
234
|
+
|
235
|
+
for (unsigned cur = 0; cur < paragraph.length(); ++cur) {
|
236
|
+
WordInfo new_word;
|
237
|
+
|
238
|
+
if (paragraph.char_at(cur) == L' ') {
|
239
|
+
// Whitespace:
|
240
|
+
// Add last word to list if existent
|
241
|
+
if (begin_of_word != cur) {
|
242
|
+
new_word.text = paragraph.range(begin_of_word, cur);
|
243
|
+
new_word.width = builder.text_width(new_word.text);
|
244
|
+
new_word.space_width = builder.space_width();
|
245
|
+
collected_words.push_back(new_word);
|
246
|
+
}
|
247
|
+
begin_of_word = cur + 1;
|
248
|
+
}
|
249
|
+
else if (is_breaking_asian_glyph(paragraph.char_at(cur))) {
|
250
|
+
// Asian glyph (treat as single word):
|
251
|
+
// Add last word to list if existent
|
252
|
+
if (begin_of_word != cur) {
|
253
|
+
new_word.text = paragraph.range(begin_of_word, cur);
|
254
|
+
new_word.width = builder.text_width(new_word.text);
|
255
|
+
new_word.space_width = 0;
|
256
|
+
collected_words.push_back(new_word);
|
257
|
+
}
|
258
|
+
// Add glyph as a single "word"
|
259
|
+
new_word.text = paragraph.range(cur, cur + 1);
|
260
|
+
new_word.width = builder.text_width(new_word.text);
|
261
|
+
new_word.space_width = 0;
|
262
|
+
collected_words.push_back(new_word);
|
263
|
+
begin_of_word = cur + 1;
|
264
|
+
}
|
265
|
+
}
|
266
|
+
if (begin_of_word < paragraph.length()) {
|
267
|
+
WordInfo last_word;
|
268
|
+
last_word.text = paragraph.range(begin_of_word, paragraph.length());
|
269
|
+
last_word.width = builder.text_width(last_word.text);
|
270
|
+
last_word.space_width = 0;
|
271
|
+
collected_words.push_back(last_word);
|
272
|
+
}
|
273
|
+
|
274
|
+
process_words(builder, collected_words);
|
275
|
+
}
|
276
|
+
|
277
|
+
void process_text(TextBlockBuilder& builder, const FormattedString& text)
|
278
|
+
{
|
279
|
+
vector<FormattedString> paragraphs = text.split_lines();
|
280
|
+
for (auto& paragraph : paragraphs) {
|
281
|
+
process_paragraph(builder, paragraph);
|
282
|
+
}
|
283
|
+
}
|
284
|
+
}
|
285
|
+
}
|
286
|
+
|
287
|
+
Gosu::Bitmap Gosu::create_text(const string& text, const string& font_name, unsigned font_height,
|
288
|
+
int line_spacing, unsigned width, Alignment align, unsigned font_flags)
|
289
|
+
{
|
290
|
+
if (line_spacing <= -static_cast<int>(font_height)) {
|
291
|
+
throw logic_error("negative line spacing of more than line height impossible");
|
292
|
+
}
|
293
|
+
|
294
|
+
wstring wtext = utf8_to_wstring(text);
|
295
|
+
FormattedString fs(wtext.c_str(), font_flags);
|
296
|
+
if (fs.length() == 0) {
|
297
|
+
return Bitmap(width, font_height);
|
298
|
+
}
|
299
|
+
|
300
|
+
// Set up the builder object which will manage all the drawing and
|
301
|
+
// conversions for us.
|
302
|
+
TextBlockBuilder builder(font_name, font_height, line_spacing, width, align);
|
303
|
+
|
304
|
+
// Let the process* functions draw everything.
|
305
|
+
process_text(builder, fs);
|
306
|
+
|
307
|
+
// Done!
|
308
|
+
return builder.result();
|
309
|
+
}
|
310
|
+
|
311
|
+
// Very easy special case.
|
312
|
+
Gosu::Bitmap Gosu::create_text(const string& text, const string& font_name, unsigned font_height,
|
313
|
+
unsigned font_flags)
|
314
|
+
{
|
315
|
+
wstring wtext = utf8_to_wstring(text);
|
316
|
+
FormattedString fs(wtext.c_str(), font_flags);
|
317
|
+
if (fs.length() == 0) {
|
318
|
+
return Bitmap(1, font_height);
|
319
|
+
}
|
320
|
+
|
321
|
+
vector<FormattedString> lines = fs.split_lines();
|
322
|
+
|
323
|
+
Bitmap bmp(1, lines.size() * font_height);
|
324
|
+
|
325
|
+
for (int i = 0; i < lines.size(); ++i) {
|
326
|
+
if (lines[i].length() == 0) continue;
|
327
|
+
|
328
|
+
unsigned x = 0;
|
329
|
+
vector<FormattedString> parts = lines[i].split_parts();
|
330
|
+
for (auto& part : parts) {
|
331
|
+
if (part.length() == 1 && part.entity_at(0)) {
|
332
|
+
Gosu::Bitmap entity = entity_bitmap(part.entity_at(0));
|
333
|
+
multiply_bitmap_alpha(entity, part.color_at(0).alpha());
|
334
|
+
bmp.resize(max(bmp.width(), x + entity.width()), bmp.height(), 0x00ffffff);
|
335
|
+
bmp.insert(entity, x, i * font_height);
|
336
|
+
x += entity.width();
|
337
|
+
continue;
|
338
|
+
}
|
339
|
+
|
340
|
+
assert (part.length() > 0);
|
341
|
+
string unformatted_text = wstring_to_utf8(part.unformat());
|
342
|
+
unsigned part_width = text_width(unformatted_text, font_name, font_height,
|
343
|
+
part.flags_at(0));
|
344
|
+
bmp.resize(max(bmp.width(), x + part_width), bmp.height(), 0x00ffffff);
|
345
|
+
draw_text(bmp, unformatted_text, x, i * font_height, part.color_at(0), font_name,
|
346
|
+
font_height, part.flags_at(0));
|
347
|
+
x += part_width;
|
348
|
+
}
|
349
|
+
}
|
350
|
+
|
351
|
+
return bmp;
|
352
|
+
}
|
353
|
+
|
354
|
+
static map<string, shared_ptr<Gosu::Bitmap>> entities;
|
355
|
+
|
356
|
+
void Gosu::register_entity(const string& name, const Gosu::Bitmap& replacement)
|
357
|
+
{
|
358
|
+
entities[name].reset(new Bitmap(replacement));
|
359
|
+
}
|
360
|
+
|
361
|
+
bool Gosu::is_entity(const string& name)
|
362
|
+
{
|
363
|
+
return entities[name].get();
|
364
|
+
}
|
365
|
+
|
366
|
+
const Gosu::Bitmap& Gosu::entity_bitmap(const string& name)
|
367
|
+
{
|
368
|
+
shared_ptr<Gosu::Bitmap>& ptr = entities[name];
|
369
|
+
if (!ptr) throw runtime_error("Unknown entity: " + name);
|
370
|
+
return *ptr;
|
371
|
+
}
|