/
gamewidget.cpp
307 lines (259 loc) · 8.55 KB
/
gamewidget.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/** @file gamewidget.cpp
*
* @authors Copyright © 2013-2017 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2014-2015 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#include "de_platform.h" // must be included first
#include "api_render.h"
#include "ui/widgets/gamewidget.h"
#include "clientapp.h"
#include "ui/ddevent.h"
#include "ui/ui_main.h"
#include "ui/sys_input.h"
#include "ui/busyvisual.h"
#include "ui/clientwindowsystem.h"
#include "ui/viewcompositor.h"
#include "ui/widgets/taskbarwidget.h"
#include "ui/widgets/busywidget.h"
#include "dd_def.h"
#include "dd_main.h"
#include "dd_loop.h"
#include "sys_system.h"
//#include "ui/editors/edit_bias.h"
#include "world/map.h"
#include "world/p_players.h"
#include "network/net_main.h"
#include "client/cl_def.h" // clientPaused
#include "render/r_main.h"
#include "render/rend_main.h"
#include "render/cameralensfx.h"
//#include "render/lightgrid.h"
#include "gl/gl_main.h"
#include "gl/sys_opengl.h"
#include "gl/gl_defer.h"
#include <doomsday/console/exec.h>
#include <de/GLState>
#include <de/GLTextureFramebuffer>
#include <de/LogBuffer>
#include <de/VRConfig>
/**
* Maximum number of milliseconds spent uploading textures at the beginning
* of a frame. Note that non-uploaded textures will appear as pure white
* until their content gets uploaded (you should precache them).
*/
#define FRAME_DEFERRED_UPLOAD_TIMEOUT 20
using namespace de;
DENG2_PIMPL(GameWidget)
{
bool needFrames = true; // Rendering a new frame is necessary.
VRConfig::Eye lastFrameEye = VRConfig::NeitherEye;
Impl(Public *i) : Base(i) {}
/**
* Render the 3D game view of each local player. The results are stored in each
* players' ViewCompositor as a texture. This is a slow operation and should only
* be done once after each time game tics have been run.
*/
void renderGameViews()
{
lastFrameEye = ClientApp::vr().currentEye();
ClientApp::forLocalPlayers([] (ClientPlayer &player)
{
player.viewCompositor().renderGameView([] (int playerNum) {
R_RenderViewPort(playerNum);
});
return LoopContinue;
});
}
/**
* Draw the game widget's contents by compositing the various layers: game view,
* border, HUD, finale, intermission, and engine/debug overlays. This is generally
* a quick operation and can be done multiple times per window paint.
*/
void drawComposited()
{
int numLocal = 0;
ClientApp::forLocalPlayers([this, &numLocal] (ClientPlayer &player)
{
player.viewCompositor().drawCompositedLayers();
++numLocal;
return LoopContinue;
});
// Nobody is playing right now?
if (numLocal == 0)
{
ClientApp::player(0).viewCompositor().drawCompositedLayers();
}
}
void draw()
{
// If the eye changes, we will need to redraw the view.
if (ClientApp::vr().currentEye() != lastFrameEye)
{
needFrames = true;
}
if (needFrames)
{
// Notify the world that a new render frame has begun.
App_World().beginFrame(CPP_BOOL(R_NextViewer()));
// Each players' view is rendered into an FBO first. What is seen on screen
// is then composited using the player view as a texture with additional layers
// and effects.
renderGameViews();
// End any open DGL sequence.
DGL_End();
// Notify the world that we've finished rendering the frame.
App_World().endFrame();
needFrames = false;
}
drawComposited();
}
void updateSize()
{
LOG_AS("GameWidget");
LOG_GL_XVERBOSE("View resized to ", self().rule().recti().size().asText());
// Update viewports.
R_SetViewGrid(0, 0);
/*if (!App_GameLoaded())
{
// Update for busy mode.
R_UseViewPort(0);
}*/
//UI_LoadFonts();
}
};
GameWidget::GameWidget(String const &name)
: GuiWidget(name), d(new Impl(this))
{
requestGeometry(false);
}
/*
void GameWidget::glApplyViewport(Rectanglei const &rect)
{
GLState::current()
.setNormalizedViewport(normalizedRect(rect))
.apply();
}
*/
void GameWidget::pause()
{
if (App_GameLoaded() && !clientPaused)
{
Con_Execute(CMDS_DDAY, "pause", true, false);
}
}
void GameWidget::drawComposited()
{
d->drawComposited();
}
void GameWidget::viewResized()
{
GuiWidget::viewResized();
if (!BusyMode_Active() && !isDisabled() && !Sys_IsShuttingDown() &&
ClientWindow::mainExists())
{
d->updateSize();
}
}
void GameWidget::update()
{
GuiWidget::update();
if (isDisabled() || BusyMode_Active()) return;
// We may be performing GL operations.
ClientWindow::main().glActivate();
// Run at least one (fractional) tic.
Loop_RunTics();
// Time has progressed, so game views need rendering.
d->needFrames = true;
// We may have received a Quit message from the windowing system
// during events/tics processing.
if (Sys_IsShuttingDown()) return;
GL_ProcessDeferredTasks(FRAME_DEFERRED_UPLOAD_TIMEOUT);
// Release the busy transition frame now when we can be sure that busy mode
// is over / didn't start at all.
if (!Con_TransitionInProgress())
{
ClientWindow::main().busy().releaseTransitionFrame();
}
}
void GameWidget::drawContent()
{
if (isDisabled() || !GL_IsFullyInited() || !App_GameLoaded())
return;
root().painter().flush();
GLState::push();
Rectanglei pos;
if (hasChangedPlace(pos))
{
// Automatically update if the widget is resized.
d->updateSize();
}
d->draw();
GLState::considerNativeStateUndefined();
GLState::pop();
}
bool GameWidget::handleEvent(Event const &event)
{
/**
* @todo Event processing should occur here, not during Loop_RunTics().
* However, care must be taken to reproduce the vanilla behavior of
* controls with regard to response times.
*
* @todo Input drivers need to support Unicode text; for now we have to
* submit as Latin1.
*/
ClientWindow &window = root().window().as<ClientWindow>();
if (event.type() == Event::MouseButton && !root().window().eventHandler().isMouseTrapped() &&
rule().recti().contains(event.as<MouseEvent>().pos()))
{
if (!window.hasSidebar() && !window.isGameMinimized())
{
// If the mouse is not trapped, we will just eat button clicks which
// will prevent them from reaching the legacy input system.
return true;
}
// If the sidebar is open, we must explicitly click on the GameWidget to
// cause input to be trapped.
switch (handleMouseClick(event))
{
case MouseClickFinished:
// Click completed on the widget, trap the mouse.
window.eventHandler().trapMouse();
window.taskBar().close();
root().setFocus(0); // Allow input to reach here.
root().clearFocusStack(); // Ensure no popups steal focus away.
break;
default:
// Just ignore the event.
return true;
}
}
if (event.type() == Event::KeyPress ||
event.type() == Event::KeyRepeat ||
event.type() == Event::KeyRelease)
{
KeyEvent const &ev = event.as<KeyEvent>();
Keyboard_Submit(ev.state() == KeyEvent::Pressed? IKE_DOWN :
ev.state() == KeyEvent::Repeat? IKE_REPEAT :
IKE_UP,
ev.ddKey(), ev.nativeCode(), ev.text().toLatin1());
}
return false;
}
void GameWidget::glDeinit()
{
GuiWidget::glDeinit();
//d->glDeinit();
}