/
Window.cpp
261 lines (211 loc) · 6.58 KB
/
Window.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include <Gosu/Gosu.hpp>
#include "Graphics/Common.hpp"
#include <SDL.h>
#include <cstdlib>
#include <memory>
#include <stdexcept>
using namespace std::tr1::placeholders;
namespace Gosu
{
namespace FPS
{
void registerFrame();
}
void throwSDLError(const std::string& operation)
{
const char *error = SDL_GetError();
throw std::runtime_error(operation + ": " + (error ? error : "(unknown error)"));
}
void cleanup();
SDL_Window* sharedWindow()
{
static SDL_Window *window = 0;
if (window == 0)
{
if (SDL_Init(SDL_INIT_VIDEO) < 0)
throwSDLError("Could not initialize SDL Video");
std::atexit(cleanup);
Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_HIDDEN;
#if SDL_VERSION_ATLEAST(2, 0, 1)
flags |= SDL_WINDOW_ALLOW_HIGHDPI;
#endif
window = SDL_CreateWindow("", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 64, 64, flags);
if (window == 0)
throwSDLError("Could not create window");
}
return window;
}
SDL_GLContext sharedGLContext()
{
static SDL_GLContext context = 0;
if (context == 0)
{
#ifdef GOSU_IS_OPENGLES
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
#endif
context = SDL_GL_CreateContext(sharedWindow());
if (context == 0)
throwSDLError("Could not create OpenGL context");
}
return context;
}
void ensureCurrentContext()
{
SDL_GL_MakeCurrent(sharedWindow(), sharedGLContext());
}
void cleanup()
{
SDL_GL_DeleteContext(sharedGLContext());
SDL_DestroyWindow(sharedWindow());
SDL_QuitSubSystem(SDL_INIT_VIDEO);
}
}
struct Gosu::Window::Impl
{
double updateInterval;
std::auto_ptr<Graphics> graphics;
std::auto_ptr<Input> input;
};
Gosu::Window::Window(unsigned width, unsigned height, bool fullscreen, double updateInterval)
: pimpl(new Impl)
{
int actualWidth = width;
int actualHeight = height;
double scaleFactor = 1.0;
double blackBarWidth = 0;
double blackBarHeight = 0;
if (fullscreen) {
actualWidth = Gosu::screenWidth();
actualHeight = Gosu::screenHeight();
double scaleX = 1.0 * actualWidth / width;
double scaleY = 1.0 * actualHeight / height;
scaleFactor = std::min(scaleX, scaleY);
if (scaleX < scaleY) {
blackBarHeight = (actualHeight / scaleX - height) / 2;
}
else if (scaleY < scaleX) {
blackBarWidth = (actualWidth / scaleY - width) / 2;
}
}
else {
double maxWidth = Gosu::availableWidth();
double maxHeight = Gosu::availableHeight();
if (width > maxWidth || height > maxHeight) {
scaleFactor = std::min(maxWidth / width, maxHeight / height);
actualWidth = width * scaleFactor;
actualHeight = height * scaleFactor;
}
}
// TODO - it would be better/enough to only do this in show()
SDL_SetWindowTitle(sharedWindow(), "");
SDL_SetWindowSize(sharedWindow(), actualWidth, actualHeight);
SDL_SetWindowPosition(sharedWindow(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
if (fullscreen)
{
SDL_SetWindowFullscreen(sharedWindow(), fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
}
SDL_ShowWindow(sharedWindow());
#if SDL_VERSION_ATLEAST(2, 0, 1)
SDL_GL_GetDrawableSize(sharedWindow(), &actualWidth, &actualHeight);
#endif
ensureCurrentContext();
SDL_GL_SetSwapInterval(1);
pimpl->graphics.reset(new Graphics(actualWidth, actualHeight, fullscreen));
pimpl->graphics->setResolution(width, height, blackBarWidth, blackBarHeight);
pimpl->input.reset(new Input(sharedWindow()));
pimpl->input->setMouseFactors(1 / scaleFactor, 1 / scaleFactor, blackBarWidth, blackBarHeight);
input().onButtonDown = std::tr1::bind(&Window::buttonDown, this, _1);
input().onButtonUp = std::tr1::bind(&Window::buttonUp, this, _1);
pimpl->updateInterval = updateInterval;
}
Gosu::Window::~Window()
{
SDL_HideWindow(sharedWindow());
}
std::wstring Gosu::Window::caption() const
{
return utf8ToWstring(SDL_GetWindowTitle(sharedWindow()));
}
void Gosu::Window::setCaption(const std::wstring& caption)
{
std::string utf8 = wstringToUTF8(caption);
SDL_SetWindowTitle(sharedWindow(), utf8.c_str());
}
double Gosu::Window::updateInterval() const
{
return pimpl->updateInterval;
}
namespace GosusDarkSide
{
// TODO: Find a way for this to fit into Gosu's design.
// This can point to a function that wants to be called every
// frame, e.g. rb_thread_schedule.
typedef void (*HookOfHorror)();
HookOfHorror oncePerTick = 0;
}
void Gosu::Window::show()
{
while (true) {
unsigned long startTime = milliseconds();
SDL_Event e;
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT)
return;
else
input().feedSDLEvent(&e);
}
Song::update();
input().update();
update();
SDL_ShowCursor(needsCursor());
if (needsRedraw()) {
ensureCurrentContext();
if (graphics().begin()) {
draw();
graphics().end();
FPS::registerFrame();
}
SDL_GL_SwapWindow(sharedWindow());
if (GosusDarkSide::oncePerTick) GosusDarkSide::oncePerTick();
}
// Sleep to keep this loop from eating 100% CPU.
unsigned long frameTime = milliseconds() - startTime;
if (frameTime < pimpl->updateInterval) {
sleep(pimpl->updateInterval - frameTime);
}
}
}
void Gosu::Window::close()
{
SDL_Event e;
e.type = SDL_QUIT;
SDL_PushEvent(&e);
}
const Gosu::Graphics& Gosu::Window::graphics() const
{
return *pimpl->graphics;
}
Gosu::Graphics& Gosu::Window::graphics()
{
return *pimpl->graphics;
}
const Gosu::Input& Gosu::Window::input() const
{
return *pimpl->input;
}
Gosu::Input& Gosu::Window::input()
{
return *pimpl->input;
}
// Deprecated.
class Gosu::Audio {};
namespace { Gosu::Audio dummyAudio; }
const Gosu::Audio& Gosu::Window::audio() const
{
return dummyAudio;
}
Gosu::Audio& Gosu::Window::audio()
{
return dummyAudio;
}