Skip to content

Commit 31cba02

Browse files
committed
xrGame,xrEngine: fix exit from game, fix GUI cursor move
1 parent d379a0a commit 31cba02

File tree

9 files changed

+127
-157
lines changed

9 files changed

+127
-157
lines changed

src/Common/PlatformLinux.inl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ inline char *_strupr_l(char *str, locale_t loc)
6565
#define VOID void
6666
#define HKL void *
6767
#define ActivateKeyboardLayout(x, y) {}
68-
#define GetCursorPos(point) {}
6968
#define ScreenToClient(hwnd, p) {}
7069

7170
#define __except(X) catch(X)

src/xrEngine/Device_Initialize.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ void CRenderDevice::Initialize()
3939
if (strstr(Core.Params, "-weather"))
4040
initialize_weather_editor();
4141

42-
//SetEnvironmentVariable("SDL_VIDEODRIVER", "windows");
43-
SetEnvironmentVariable("SDL_AUDIODRIVER", "directsound");
44-
4542
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
4643
{
4744
Log("Unable to initialize SDL: %s", SDL_GetError());
@@ -62,14 +59,16 @@ void CRenderDevice::Initialize()
6259
R_ASSERT3(m_sdlWnd, "Unable to create SDL window", SDL_GetError());
6360
}
6461
// Save window properties
65-
6662
m_dwWindowStyle = SDL_GetWindowFlags(m_sdlWnd);
6763
if (SDL_GetDisplayBounds(0, &m_rcWindowBounds) != 0)
6864
{
69-
Log("SDL_GetDisplayBounds failed: %s", SDL_GetError());
65+
Log("SDL_GetDisplayBounds m_rcWindowBounds failed: %s", SDL_GetError());
7066
}
7167

72-
//GetClientRect(m_hWnd, &m_rcWindowClient);
68+
if (SDL_GetDisplayBounds(0, &m_rcWindowClient) != 0)
69+
{
70+
Log("SDL_GetDisplayBounds m_rcWindowClient failed: %s", SDL_GetError());
71+
}
7372
}
7473

7574
void CRenderDevice::DumpStatistics(IGameFont& font, IPerformanceAlert* alert)

src/xrEngine/IInputReceiver.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "xr_input.h"
55
#include "IInputReceiver.h"
6+
#include "SDL_syswm.h"
67

78
void IInputReceiver::IR_Capture(void)
89
{
@@ -49,14 +50,16 @@ BOOL IInputReceiver::IR_GetBtnState(int btn)
4950
return pInput->iGetAsyncBtnState(btn);
5051
}
5152

52-
void IInputReceiver::IR_GetMousePosScreen(Ivector2& p) { GetCursorPos((LPPOINT)&p); }
53-
void IInputReceiver::IR_GetMousePosReal(HWND hwnd, Ivector2& p)
53+
void IInputReceiver::IR_GetMousePosScreen(Ivector2& p)
54+
{
55+
SDL_GetGlobalMouseState(&p.x, &p.y);
56+
}
57+
58+
void IInputReceiver::IR_GetMousePosReal(SDL_Window *m_sdlWnd, Ivector2& p)
5459
{
5560
IR_GetMousePosScreen(p);
56-
if (hwnd)
57-
ScreenToClient(hwnd, (LPPOINT)&p);
5861
}
59-
void IInputReceiver::IR_GetMousePosReal(Ivector2& p) { /*IR_GetMousePosReal(RDEVICE.m_sdlWnd, p)*/; }
62+
void IInputReceiver::IR_GetMousePosReal(Ivector2& p) { IR_GetMousePosReal(RDEVICE.m_sdlWnd, p); }
6063
void IInputReceiver::IR_GetMousePosIndependent(Fvector2& f)
6164
{
6265
Ivector2 p;

src/xrEngine/IInputReceiver.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class ENGINE_API IInputReceiver
1919
public:
2020
static void IR_GetLastMouseDelta(Ivector2& p);
2121
static void IR_GetMousePosScreen(Ivector2& p);
22-
static void IR_GetMousePosReal(HWND hwnd, Ivector2& p);
22+
static void IR_GetMousePosReal(SDL_Window *m_sdlWnd, Ivector2& p);
2323
static void IR_GetMousePosReal(Ivector2& p);
2424
static void IR_GetMousePosIndependent(Fvector2& f);
2525
static void IR_GetMousePosIndependentCrop(Fvector2& f);

src/xrEngine/device.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,7 @@ void CRenderDevice::message_loop()
353353
continue;
354354
case SDL_WINDOWEVENT_CLOSE:
355355
event.type = SDL_QUIT;
356-
SDL_PeepEvents(&event, 1, SDL_ADDEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT);
357-
continue;
356+
break;
358357
#if SDL_VERSION_ATLEAST(2, 0, 5)
359358
case SDL_WINDOWEVENT_SHOWN:
360359
case SDL_WINDOWEVENT_FOCUS_GAINED:

src/xrEngine/xr_input.cpp

Lines changed: 91 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ CInput::CInput(BOOL bExclusive, int deviceForInit)
6565

6666
CInput::~CInput(void)
6767
{
68+
SDL_SetRelativeMouseMode(SDL_FALSE);
6869
#ifdef ENGINE_BUILD
6970
Device.seqFrame.Remove(this);
7071
Device.seqAppDeactivate.Remove(this);
@@ -79,89 +80,67 @@ void CInput::DumpStatistics(IGameFont& font, IPerformanceAlert* alert)
7980
font.OutNext("*** INPUT: %2.2fms", pInput->GetStats().FrameTime.result);
8081
}
8182

82-
void CInput::SetAllAcquire(BOOL bAcquire)
83-
{
84-
}
83+
void CInput::SetAllAcquire(BOOL bAcquire) {}
8584

86-
void CInput::SetMouseAcquire(BOOL bAcquire)
87-
{
88-
}
85+
void CInput::SetMouseAcquire(BOOL bAcquire) {}
8986
void CInput::SetKBDAcquire(BOOL bAcquire) {}
9087
//-----------------------------------------------------------------------
91-
void CInput::KeyUpdate(SDL_Event* event)
88+
BOOL b_altF4 = FALSE;
89+
void CInput::KeyUpdate()
9290
{
91+
if (b_altF4)
92+
return;
93+
9394
bool b_dik_pause_was_pressed = false;
9495

95-
if (SDL_SCANCODE_PAUSE == event->key.keysym.scancode)
96+
const Uint8* state = SDL_GetKeyboardState(NULL);
97+
#ifndef _EDITOR
98+
bool b_alt_tab = false;
99+
100+
if (!b_altF4 && state[SDL_SCANCODE_F4] && (state[SDL_SCANCODE_RALT] || state[SDL_SCANCODE_LALT]))
96101
{
97-
if (SDL_KEYDOWN == event->key.type)
98-
b_dik_pause_was_pressed = true;
102+
b_altF4 = TRUE;
103+
Engine.Event.Defer("KERNEL:disconnect");
104+
Engine.Event.Defer("KERNEL:quit");
105+
SDL_Event ev;
106+
ev.type = SDL_QUIT;
107+
SDL_PushEvent(&ev);
99108
}
109+
#endif
110+
if (b_altF4)
111+
return;
100112

101113
#ifndef _EDITOR
102-
bool b_alt_tab = false;
103-
104114
if (Device.dwPrecacheFrame == 0)
105115
#endif
106116
{
107-
const Uint8* state = SDL_GetKeyboardState(NULL);
108-
109-
if (SDL_KEYDOWN == event->key.type)
110-
cbStack.back()->IR_OnKeyboardPress(event->key.keysym.scancode);
111-
else if (SDL_KEYUP == event->key.type)
117+
for (u32 i = 0; i < COUNT_KB_BUTTONS; i++)
112118
{
113-
cbStack.back()->IR_OnKeyboardRelease(event->key.keysym.scancode);
119+
if (state[i])
120+
cbStack.back()->IR_OnKeyboardPress(i);
121+
else
122+
{
123+
cbStack.back()->IR_OnKeyboardRelease(i);
114124
#ifndef _EDITOR
115-
if (SDL_SCANCODE_TAB == event->key.keysym.scancode && KMOD_ALT == SDL_GetModState())
116-
b_alt_tab = true;
125+
if (SDL_SCANCODE_TAB == state[i] &&
126+
(iGetAsyncKeyState(SDL_SCANCODE_LALT) || iGetAsyncKeyState(SDL_SCANCODE_RALT)))
127+
b_alt_tab = true;
117128
#endif
129+
}
118130
}
119131

120132
for (u32 i = 0; i < COUNT_KB_BUTTONS; i++)
121-
if (state[i])
133+
if (KBState[i] && state[i])
122134
cbStack.back()->IR_OnKeyboardHold(i);
135+
136+
for (u32 idx = 0; idx < COUNT_KB_BUTTONS; idx++)
137+
KBState[idx] = state[idx];
123138
}
124139

125140
#ifndef _EDITOR
126141
if (b_alt_tab)
127142
SDL_MinimizeWindow(Device.m_sdlWnd);
128143
#endif
129-
/*
130-
#ifndef _EDITOR
131-
//update xinput if exist
132-
for( DWORD iUserIndex=0; iUserIndex<DXUT_MAX_CONTROLLERS; iUserIndex++ )
133-
{
134-
DXUTGetGamepadState( iUserIndex, &g_GamePads[iUserIndex], true, false );
135-
136-
if( !g_GamePads[iUserIndex].bConnected )
137-
continue; // unplugged?
138-
139-
bool new_b, old_b;
140-
new_b = !!(g_GamePads[iUserIndex].wPressedButtons & XINPUT_GAMEPAD_A);
141-
old_b = !!(g_GamePads[iUserIndex].wLastButtons & XINPUT_GAMEPAD_A);
142-
143-
if(new_b != old_b)
144-
{
145-
if(old_b)
146-
cbStack.back()->IR_OnMousePress(0);
147-
else
148-
cbStack.back()->IR_OnMouseRelease(0);
149-
}
150-
int dx,dy;
151-
dx = iFloor(g_GamePads[iUserIndex].fThumbRX*6);
152-
dy = iFloor(g_GamePads[iUserIndex].fThumbRY*6);
153-
if(dx || dy)
154-
cbStack.back()->IR_OnMouseMove ( dx, dy );
155-
}
156-
157-
if(Device.fTimeGlobal > stop_vibration_time)
158-
{
159-
stop_vibration_time = flt_max;
160-
set_vibration (0, 0);
161-
}
162-
//xinput
163-
#endif
164-
*/
165144
}
166145

167146
bool CInput::get_key_name(int dik, LPSTR dest_str, int dest_sz)
@@ -209,46 +188,71 @@ void CInput::ClipCursor(bool clip)
209188
}
210189
}
211190

212-
void CInput::MouseUpdate(SDL_Event *event)
191+
void CInput::MouseUpdate(SDL_Event* event)
213192
{
214-
DWORD dwElements = MOUSEBUFFERSIZE;
215-
216193
#ifndef _EDITOR
217194
if (Device.dwPrecacheFrame)
218195
return;
219196
#endif
220197
BOOL mouse_prev[COUNT_MOUSE_BUTTONS];
221198

222-
mouse_prev[0] = mouseState[0];
223-
mouse_prev[1] = mouseState[1];
224-
mouse_prev[2] = mouseState[2];
225-
mouse_prev[3] = mouseState[3];
226-
mouse_prev[4] = mouseState[4];
227-
mouse_prev[5] = mouseState[5];
228-
mouse_prev[6] = mouseState[6];
229-
mouse_prev[7] = mouseState[7];
230-
231199
offs[0] = offs[1] = offs[2] = 0;
232200

233201
switch (event->type)
234202
{
235203
case SDL_MOUSEMOTION:
204+
{
205+
offs[0] += event->motion.xrel;
206+
offs[1] += event->motion.yrel;
236207
timeStamp[0] = event->motion.timestamp;
237-
cbStack.back()->IR_OnMouseMove(event->motion.xrel, event->motion.yrel);
238-
break;
239-
case SDL_MOUSEBUTTONUP:
240-
cbStack.back()->IR_OnMouseRelease(event->button.button);
208+
timeStamp[1] = event->motion.timestamp;
209+
if (offs[0] || offs[1])
210+
cbStack.back()->IR_OnMouseMove(offs[0], offs[1]);
211+
}
212+
break;
213+
case SDL_MOUSEBUTTONUP:
214+
mouseState[event->button.button] = FALSE;
215+
cbStack.back()->IR_OnKeyboardRelease(SDL_NUM_SCANCODES + event->button.button);
241216
break;
242217
case SDL_MOUSEBUTTONDOWN:
243-
cbStack.back()->IR_OnMousePress(event->button.button);
218+
mouseState[event->button.button] = TRUE;
219+
cbStack.back()->IR_OnKeyboardPress(SDL_NUM_SCANCODES + event->button.button);
244220
break;
245221
case SDL_MOUSEWHEEL:
222+
offs[2] += event->wheel.direction;
246223
timeStamp[2] = event->wheel.timestamp;
247-
cbStack.back()->IR_OnMouseWheel(event->wheel.direction);
224+
if (offs[2])
225+
cbStack.back()->IR_OnMouseWheel(offs[2]);
226+
break;
227+
default:
228+
if (timeStamp[1] && ((dwCurTime - timeStamp[1]) >= 25))
229+
cbStack.back()->IR_OnMouseStop(1, timeStamp[1] = 0);
230+
if (timeStamp[0] && ((dwCurTime - timeStamp[0]) >= 25))
231+
cbStack.back()->IR_OnMouseStop(0, timeStamp[0] = 0);
248232
break;
249-
default: break;
250233
}
251234

235+
auto isButtonOnHold = [&](int i) {
236+
if (mouseState[i] && mouse_prev[i])
237+
cbStack.back()->IR_OnMouseHold(i);
238+
};
239+
240+
isButtonOnHold(0);
241+
isButtonOnHold(1);
242+
isButtonOnHold(2);
243+
isButtonOnHold(3);
244+
isButtonOnHold(4);
245+
isButtonOnHold(5);
246+
isButtonOnHold(6);
247+
248+
mouse_prev[0] = mouseState[0];
249+
mouse_prev[1] = mouseState[1];
250+
mouse_prev[2] = mouseState[2];
251+
mouse_prev[3] = mouseState[3];
252+
mouse_prev[4] = mouseState[4];
253+
mouse_prev[5] = mouseState[5];
254+
mouse_prev[6] = mouseState[6];
255+
mouse_prev[7] = mouseState[7];
252256
}
253257

254258
//-------------------------------------------------------
@@ -327,41 +331,29 @@ void CInput::OnFrame(void)
327331

328332
while (SDL_PollEvent(&event))
329333
{
334+
BOOL b_break_cycle = false;
330335
switch (event.type)
331336
{
332337
case SDL_KEYDOWN:
333-
case SDL_KEYUP: KeyUpdate(&event); continue;
338+
case SDL_KEYUP: KeyUpdate(); continue;
334339

335340
case SDL_MOUSEMOTION:
336341
case SDL_MOUSEBUTTONUP:
337342
case SDL_MOUSEBUTTONDOWN:
338343
case SDL_MOUSEWHEEL:
339344
MouseUpdate(&event);
340-
//MouseUpdate();
341345
continue;
342-
// case SDL_WINDOWEVENT:
343-
// switch (event.window.event)
344-
// {
345-
// case SDL_WINDOWEVENT_CLOSE:
346-
// event.type = SDL_QUIT;
347-
// SDL_PushEvent(&event);
348-
// continue;
349-
// case SDL_WINDOWEVENT_ENTER:
350-
//#if SDL_VERSION_ATLEAST(2, 0, 5)
351-
// case SDL_WINDOWEVENT_TAKE_FOCUS:
352-
// RDEVICE.OnWM_Activate(event.window.data1, event.window.data2);
353-
// continue;
354-
//#endif
355-
// default: SDL_Log("Window %d got unknown event %d", event.window.windowID, event.window.event); continue;
356-
// }
357-
// continue;
358-
case SDL_QUIT:
359-
Engine.Event.Defer("KERNEL:disconnect");
360-
Engine.Event.Defer("KERNEL:quit");
346+
case SDL_QUIT: // go to outside event loop
347+
event.type = SDL_QUIT;
348+
SDL_PushEvent(&event);
349+
b_break_cycle = TRUE;
361350
break;
362351

363352
default: continue;
364353
}
354+
355+
if (b_break_cycle)
356+
break;
365357
}
366358

367359
stats.FrameTime.End();
@@ -380,9 +372,9 @@ void CInput::unacquire() {}
380372

381373
void CInput::acquire(const bool& exclusive)
382374
{
383-
//pMouse->SetCooperativeLevel(Device.editor() ? Device.editor()->main_handle() : RDEVICE.m_sdlWnd,
375+
// pMouse->SetCooperativeLevel(Device.editor() ? Device.editor()->main_handle() : RDEVICE.m_sdlWnd,
384376
// (exclusive ? DISCL_EXCLUSIVE : DISCL_NONEXCLUSIVE) | DISCL_FOREGROUND | DISCL_NOWINKEY);
385-
//pMouse->Acquire();
377+
// pMouse->Acquire();
386378
}
387379

388380
void CInput::exclusive_mode(const bool& exclusive)

src/xrEngine/xr_input.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class ENGINE_API CInput
2525
{
2626
COUNT_MOUSE_BUTTONS = 8,
2727
COUNT_MOUSE_AXIS = 3,
28-
COUNT_KB_BUTTONS = SDL_NUM_SCANCODES
28+
COUNT_KB_BUTTONS = SDL_SCANCODE_MODE
2929
};
3030

3131
struct InputStatistics
@@ -50,7 +50,7 @@ class ENGINE_API CInput
5050
xr_vector<IInputReceiver*> cbStack;
5151

5252
void MouseUpdate(SDL_Event *event);
53-
void KeyUpdate(SDL_Event *event);
53+
void KeyUpdate();
5454

5555
InputStatistics stats;
5656

0 commit comments

Comments
 (0)