Skip to content

Commit eef5e49

Browse files
authored
Add controller actions sample and refactor win32 sample to demo "EnterVR" scenario. (#70)
1 parent 59dbc6b commit eef5e49

21 files changed

Lines changed: 873 additions & 60 deletions

Samples.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "XrUtility", "XrUtility", "{
4040
shared\XrUtility\XrInstanceContext.h = shared\XrUtility\XrInstanceContext.h
4141
shared\XrUtility\XrMath.h = shared\XrUtility\XrMath.h
4242
shared\XrUtility\XrSessionContext.h = shared\XrUtility\XrSessionContext.h
43+
shared\XrUtility\XrSide.h = shared\XrUtility\XrSide.h
4344
shared\XrUtility\XrStereoView.h = shared\XrUtility\XrStereoView.h
4445
shared\XrUtility\XrString.h = shared\XrUtility\XrString.h
4546
shared\XrUtility\XrStruct.h = shared\XrUtility\XrStruct.h

samples/SampleSceneWin32/Main.cpp

Lines changed: 89 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,109 @@
1414
//
1515
//*********************************************************
1616
#include "pch.h"
17+
#include <windowsx.h>
18+
#include <shellapi.h>
1719
#include <XrSceneLib/XrApp.h>
20+
#include "Resource.h"
1821

1922
std::unique_ptr<engine::Scene> TryCreateTitleScene(engine::Context& context);
2023
std::unique_ptr<engine::Scene> TryCreateControllerModelScene(engine::Context& context);
24+
std::unique_ptr<engine::Scene> TryCreateControllerActionsScene(engine::Context& context);
2125

22-
int APIENTRY wWinMain(_In_ HINSTANCE, _In_opt_ HINSTANCE, _In_ LPWSTR, _In_ int) {
23-
try {
26+
// Global Variables:
27+
std::thread sceneThread;
28+
std::unique_ptr<engine::XrApp> app;
29+
30+
void EnterVR();
31+
INT_PTR CALLBACK DialogWinProc(HWND, UINT, WPARAM, LPARAM);
32+
33+
int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE, _In_ LPWSTR, _In_ int) {
34+
int nArgs;
35+
LPWSTR* szArglist = ::CommandLineToArgvW(GetCommandLineW(), &nArgs);
36+
if (nArgs >= 2 && ::_wcsicmp(szArglist[1], L"-openxr") == 0) {
37+
EnterVR();
38+
}
39+
40+
DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOGBOX), NULL, DialogWinProc);
41+
return 0;
42+
}
43+
44+
void EnterVR() {
45+
sceneThread = std::thread([] {
2446
CHECK_HRCMD(::CoInitializeEx(nullptr, COINIT_MULTITHREADED));
2547
auto on_exit = MakeScopeGuard([] { ::CoUninitialize(); });
2648

2749
engine::XrAppConfiguration appConfig({"SampleSceneWin32", 1});
2850
appConfig.RequestedExtensions.push_back(XR_MSFT_CONTROLLER_MODEL_EXTENSION_NAME);
51+
appConfig.RequestedExtensions.push_back(XR_MSFT_HAND_INTERACTION_EXTENSION_NAME);
52+
appConfig.RequestedExtensions.push_back(XR_EXT_HP_MIXED_REALITY_CONTROLLER_EXTENSION_NAME);
2953

30-
auto app = engine::CreateXrApp(appConfig);
54+
app = engine::CreateXrApp(appConfig);
3155
app->AddScene(TryCreateTitleScene(app->Context()));
3256
app->AddScene(TryCreateControllerModelScene(app->Context()));
57+
app->AddScene(TryCreateControllerActionsScene(app->Context()));
3358
app->Run();
34-
} catch (const std::exception& ex) {
35-
sample::Trace("Unhandled Exception: {}", ex.what());
36-
return 1;
37-
} catch (...) {
38-
sample::Trace(L"Unhandled Exception");
39-
return 1;
59+
app = nullptr;
60+
});
61+
}
62+
63+
void ExitVR() {
64+
if (sceneThread.joinable()) {
65+
if (app) {
66+
app->Stop();
67+
}
68+
sceneThread.join();
4069
}
70+
}
4171

42-
return 0;
72+
void TrapCursorIfMouseLeave(HWND hwnd) {
73+
RECT rc;
74+
::GetWindowRect(hwnd, &rc);
75+
76+
POINT pt;
77+
::GetCursorPos(&pt);
78+
79+
if (!::PtInRect(&rc, pt)) {
80+
::SetCursorPos((rc.left + rc.right) / 2, (rc.top + rc.bottom) / 2);
81+
}
82+
}
83+
84+
INT_PTR CALLBACK DialogWinProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
85+
UNREFERENCED_PARAMETER(lParam);
86+
constexpr UINT_PTR IDT_MOUSETRAP = 1;
87+
88+
switch (message) {
89+
case WM_INITDIALOG:
90+
// Setup a recurring timer to move mouse back when it moves out of this window.
91+
// It's a simple way to keep WM_MOUSEMOVE event to remain sending to this window.
92+
::SetTimer(hwnd, IDT_MOUSETRAP, 100, NULL);
93+
return (INT_PTR)TRUE;
94+
95+
case WM_COMMAND:
96+
if (LOWORD(wParam) == IDOK) {
97+
ExitVR();
98+
EnterVR();
99+
} else if (LOWORD(wParam) == IDCANCEL) {
100+
::EndDialog(hwnd, 0);
101+
}
102+
break;
103+
104+
case WM_TIMER:
105+
if (wParam == IDT_MOUSETRAP && hwnd == ::GetForegroundWindow()) {
106+
TrapCursorIfMouseLeave(hwnd);
107+
}
108+
break;
109+
110+
case WM_MOUSEMOVE:
111+
::SetWindowText(hwnd, fmt::format(L"Mouse pos: {}, {}", GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)).c_str());
112+
break;
113+
114+
case WM_DESTROY:
115+
ExitVR();
116+
::KillTimer(hwnd, IDT_MOUSETRAP);
117+
::PostQuitMessage(0);
118+
break;
119+
}
120+
121+
return (INT_PTR)FALSE;
43122
}

samples/SampleSceneWin32/Main.ico

45.1 KB
Binary file not shown.

samples/SampleSceneWin32/Main.rc

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
//Microsoft Visual C++ generated resource script.
2+
//
3+
#include "resource.h"
4+
5+
#define APSTUDIO_READONLY_SYMBOLS
6+
/////////////////////////////////////////////////////////////////////////////
7+
//
8+
// Generated from the TEXTINCLUDE resource.
9+
//
10+
#ifndef APSTUDIO_INVOKED
11+
#include <SDKDDKVer.h>
12+
#endif
13+
#define APSTUDIO_HIDDEN_SYMBOLS
14+
#include "windows.h"
15+
#undef APSTUDIO_HIDDEN_SYMBOLS
16+
/////////////////////////////////////////////////////////////////////////////
17+
#undef APSTUDIO_READONLY_SYMBOLS
18+
19+
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
20+
LANGUAGE 9, 1
21+
22+
/////////////////////////////////////////////////////////////////////////////
23+
//
24+
// Icon
25+
//
26+
27+
// Icon with lowest ID value placed first to ensure application icon
28+
// remains consistent on all systems.
29+
30+
IDI_DIALOGBOX ICON "Main.ico"
31+
IDI_SMALL ICON "small.ico"
32+
33+
34+
/////////////////////////////////////////////////////////////////////////////
35+
//
36+
// Dialog
37+
//
38+
39+
IDD_DIALOGBOX DIALOGEX 0, 0, 320, 200
40+
STYLE DS_SETFONT | DS_MODALFRAME | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU
41+
CAPTION "SampleSceneWin32"
42+
FONT 8, "MS Shell Dlg"
43+
BEGIN
44+
ICON IDR_MAINFRAME,IDC_STATIC,14,14,21,20
45+
LTEXT "OpenXR Win32 sample, Version 1.0",IDC_STATIC,42,14,114,8,SS_NOPREFIX
46+
LTEXT "Copyright (c) 2020",IDC_STATIC,42,26,114,8
47+
DEFPUSHBUTTON "Enter &VR",IDOK,113,41,50,14,WS_GROUP
48+
DEFPUSHBUTTON "E&xit",IDCANCEL,168,41,50,14,WS_GROUP
49+
END
50+
51+
/////////////////////////////////////////////////////////////////////////////
52+
//
53+
// DESIGNINFO
54+
//
55+
56+
#ifdef APSTUDIO_INVOKED
57+
GUIDELINES DESIGNINFO
58+
BEGIN
59+
IDD_DIALOGBOX, DIALOG
60+
BEGIN
61+
LEFTMARGIN, 7
62+
RIGHTMARGIN, 163
63+
TOPMARGIN, 7
64+
BOTTOMMARGIN, 55
65+
END
66+
END
67+
#endif // APSTUDIO_INVOKED
68+
69+
#ifdef APSTUDIO_INVOKED
70+
/////////////////////////////////////////////////////////////////////////////
71+
//
72+
// TEXTINCLUDE
73+
//
74+
1 TEXTINCLUDE
75+
BEGIN
76+
"resource.h\0"
77+
END
78+
79+
2 TEXTINCLUDE
80+
BEGIN
81+
"#ifndef APSTUDIO_INVOKED\r\n"
82+
"#include ""targetver.h""\r\n"
83+
"#endif\r\n"
84+
"#define APSTUDIO_HIDDEN_SYMBOLS\r\n"
85+
"#include ""windows.h""\r\n"
86+
"#undef APSTUDIO_HIDDEN_SYMBOLS\r\n"
87+
"\0"
88+
END
89+
90+
3 TEXTINCLUDE
91+
BEGIN
92+
"\r\n"
93+
"\0"
94+
END
95+
96+
#endif // APSTUDIO_INVOKED
97+
98+
/////////////////////////////////////////////////////////////////////////////
99+
//
100+
// String Table
101+
//
102+
103+
STRINGTABLE
104+
BEGIN
105+
IDC_DIALOGBOX "SampleSceneWin32"
106+
IDS_APP_TITLE "SampleSceneWin32"
107+
END
108+
109+
#endif
110+
/////////////////////////////////////////////////////////////////////////////
111+
112+
113+
114+
#ifndef APSTUDIO_INVOKED
115+
/////////////////////////////////////////////////////////////////////////////
116+
//
117+
// Generated from the TEXTINCLUDE resource.
118+
//
119+
120+
/////////////////////////////////////////////////////////////////////////////
121+
#endif // not APSTUDIO_INVOKED
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//{{NO_DEPENDENCIES}}
2+
// Microsoft Visual C++ generated include file.
3+
// Used by Main.rc
4+
5+
#define IDS_APP_TITLE 103
6+
7+
#define IDR_MAINFRAME 128
8+
#define IDD_DIALOGBOX 103
9+
#define IDI_DIALOGBOX 107
10+
#define IDI_SMALL 108
11+
#define IDC_DIALOGBOX 109
12+
#define IDC_MYICON 2
13+
#ifndef IDC_STATIC
14+
#define IDC_STATIC -1
15+
#endif
16+
// Next default values for new objects
17+
//
18+
#ifdef APSTUDIO_INVOKED
19+
#ifndef APSTUDIO_READONLY_SYMBOLS
20+
21+
#define _APS_NO_MFC 130
22+
#define _APS_NEXT_RESOURCE_VALUE 129
23+
#define _APS_NEXT_COMMAND_VALUE 32771
24+
#define _APS_NEXT_CONTROL_VALUE 1000
25+
#define _APS_NEXT_SYMED_VALUE 110
26+
#endif
27+
#endif

samples/SampleSceneWin32/SampleSceneWin32.vcxproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,14 @@
9797
</ItemDefinitionGroup>
9898
<ItemGroup>
9999
<ClInclude Include="pch.h" />
100+
<ClInclude Include="Resource.h" />
100101
</ItemGroup>
101102
<ItemGroup>
102103
<ClCompile Include="pch.cpp">
103104
<PrecompiledHeader>Create</PrecompiledHeader>
104105
</ClCompile>
105106
<ClCompile Include="Main.cpp" />
107+
<ClCompile Include="Scene_ControllerActions.cpp" />
106108
<ClCompile Include="Scene_ControllerModel.cpp" />
107109
</ItemGroup>
108110
<ItemGroup>
@@ -113,6 +115,13 @@
113115
<ItemGroup>
114116
<None Include="packages.config" />
115117
</ItemGroup>
118+
<ItemGroup>
119+
<Image Include="Main.ico" />
120+
<Image Include="small.ico" />
121+
</ItemGroup>
122+
<ItemGroup>
123+
<ResourceCompile Include="Main.rc" />
124+
</ItemGroup>
116125
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
117126
<Target Name="CopyContent" AfterTargets="Build">
118127
<Copy DestinationFolder="$(OutDir)" SkipUnchangedFiles="True" SourceFiles="@(ContentFiles)" UseHardlinksIfPossible="True" />
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup>
4+
<ClCompile Include="pch.cpp" />
5+
<ClCompile Include="Main.cpp" />
6+
<ClCompile Include="Scene_ControllerActions.cpp" />
7+
<ClCompile Include="Scene_ControllerModel.cpp" />
8+
</ItemGroup>
9+
<ItemGroup>
10+
<ClInclude Include="pch.h" />
11+
<ClInclude Include="Resource.h">
12+
<Filter>Resources</Filter>
13+
</ClInclude>
14+
</ItemGroup>
15+
<ItemGroup>
16+
<Image Include="Main.ico">
17+
<Filter>Resources</Filter>
18+
</Image>
19+
<Image Include="small.ico">
20+
<Filter>Resources</Filter>
21+
</Image>
22+
</ItemGroup>
23+
<ItemGroup>
24+
<None Include="packages.config" />
25+
<None Include="$(OpenXRLoaderBinaryRoot)\bin\openxr_loader.dll" />
26+
</ItemGroup>
27+
<ItemGroup>
28+
<Filter Include="Resources">
29+
<UniqueIdentifier>{d833b40e-3939-4c3b-8788-8d0a14dc2bd8}</UniqueIdentifier>
30+
</Filter>
31+
</ItemGroup>
32+
<ItemGroup>
33+
<ResourceCompile Include="Main.rc">
34+
<Filter>Resources</Filter>
35+
</ResourceCompile>
36+
</ItemGroup>
37+
</Project>

0 commit comments

Comments
 (0)