Mercurial > hg > Game > Games
comparison Orchestland/Assets/OVR/Scripts/OVRPluginEvent.cs @ 1:f7675884f2a1
Add Orchestland project
author | Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp> |
---|---|
date | Fri, 17 Jul 2015 23:09:20 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:347d21cdfc22 | 1:f7675884f2a1 |
---|---|
1 /************************************************************************************ | |
2 | |
3 Copyright : Copyright 2014 Oculus VR, LLC. All Rights reserved. | |
4 | |
5 Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License"); | |
6 you may not use the Oculus VR Rift SDK except in compliance with the License, | |
7 which is provided at the time of installation or download, or which | |
8 otherwise accompanies this software in either electronic or hard copy form. | |
9 | |
10 You may obtain a copy of the License at | |
11 | |
12 http://www.oculusvr.com/licenses/LICENSE-3.2 | |
13 | |
14 Unless required by applicable law or agreed to in writing, the Oculus VR SDK | |
15 distributed under the License is distributed on an "AS IS" BASIS, | |
16 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
17 See the License for the specific language governing permissions and | |
18 limitations under the License. | |
19 | |
20 ************************************************************************************/ | |
21 | |
22 using UnityEngine; | |
23 using System; | |
24 | |
25 /// <summary> | |
26 /// Matches the events in the native plugin. | |
27 /// </summary> | |
28 public enum RenderEventType | |
29 { | |
30 // PC | |
31 BeginFrame = 0, | |
32 EndFrame = 1, | |
33 Initialize = 2, | |
34 Destroy = 3, | |
35 | |
36 // Android | |
37 InitRenderThread = 0, | |
38 Pause = 1, | |
39 Resume = 2, | |
40 LeftEyeEndFrame = 3, | |
41 RightEyeEndFrame = 4, | |
42 TimeWarp = 5, | |
43 PlatformUI = 6, | |
44 PlatformUIConfirmQuit = 7, | |
45 } | |
46 | |
47 /// <summary> | |
48 /// Communicates with native plugin functions that run on the rendering thread. | |
49 /// </summary> | |
50 public static class OVRPluginEvent | |
51 { | |
52 /// <summary> | |
53 /// Immediately issues the given event. | |
54 /// </summary> | |
55 public static void Issue(RenderEventType eventType) | |
56 { | |
57 GL.IssuePluginEvent(EncodeType((int)eventType)); | |
58 } | |
59 | |
60 /// <summary> | |
61 /// Create a data channel through the single 32-bit integer we are given to | |
62 /// communicate with the plugin. | |
63 /// Split the 32-bit integer event data into two separate "send-two-bytes" | |
64 /// plugin events. Then issue the explicit event that makes use of the data. | |
65 /// </summary> | |
66 public static void IssueWithData(RenderEventType eventType, int eventData) | |
67 { | |
68 // Encode and send-two-bytes of data | |
69 GL.IssuePluginEvent(EncodeData((int)eventType, eventData, 0)); | |
70 | |
71 // Encode and send remaining two-bytes of data | |
72 GL.IssuePluginEvent(EncodeData((int)eventType, eventData, 1)); | |
73 | |
74 // Explicit event that uses the data | |
75 GL.IssuePluginEvent(EncodeType((int)eventType)); | |
76 } | |
77 | |
78 // PRIVATE MEMBERS | |
79 //------------------------------ | |
80 // Pack event data into a Uint32: | |
81 // - isDataFlag | |
82 // - bytePos | |
83 // - eventId data is associated with | |
84 // - 2-bytes of event data | |
85 // | |
86 // 31 30 29...25 24...16 15...0 | |
87 // [data][pos][eventid][unused][payload] | |
88 //------------------------------ | |
89 private const UInt32 IS_DATA_FLAG = 0x80000000; | |
90 private const UInt32 DATA_POS_MASK = 0x40000000; | |
91 private const int DATA_POS_SHIFT = 30; | |
92 private const UInt32 EVENT_TYPE_MASK = 0x3E000000; | |
93 private const int EVENT_TYPE_SHIFT = 25; | |
94 private const UInt32 PAYLOAD_MASK = 0x0000FFFF; | |
95 private const int PAYLOAD_SHIFT = 16; | |
96 | |
97 private static int EncodeType(int eventType) | |
98 { | |
99 return (int)((UInt32)eventType & ~IS_DATA_FLAG); // make sure upper bit is not set | |
100 } | |
101 | |
102 private static int EncodeData(int eventId, int eventData, int pos) | |
103 { | |
104 UInt32 data = 0; | |
105 data |= IS_DATA_FLAG; | |
106 data |= (((UInt32)pos << DATA_POS_SHIFT) & DATA_POS_MASK); | |
107 data |= (((UInt32)eventId << EVENT_TYPE_SHIFT) & EVENT_TYPE_MASK); | |
108 data |= (((UInt32)eventData >> (pos * PAYLOAD_SHIFT)) & PAYLOAD_MASK); | |
109 | |
110 return (int)data; | |
111 } | |
112 | |
113 private static int DecodeData(int eventData) | |
114 { | |
115 // bool hasData = (((UInt32)eventData & IS_DATA_FLAG) != 0); | |
116 UInt32 pos = (((UInt32)eventData & DATA_POS_MASK) >> DATA_POS_SHIFT); | |
117 // UInt32 eventId = (((UInt32)eventData & EVENT_TYPE_MASK) >> EVENT_TYPE_SHIFT); | |
118 UInt32 payload = (((UInt32)eventData & PAYLOAD_MASK) << (PAYLOAD_SHIFT * (int)pos)); | |
119 | |
120 return (int)payload; | |
121 } | |
122 } |