|
6 | 6 |
|
7 | 7 | #include <ssengine/macros.h>
|
8 | 8 | #include "launcher.h"
|
| 9 | +#include "clock.h" |
9 | 10 |
|
10 | 11 | #include <ssengine/uri.h>
|
11 | 12 | #include <ssengine/render/resources.h>
|
@@ -37,11 +38,133 @@ static ss_logger logger = {
|
37 | 38 | };
|
38 | 39 |
|
39 | 40 | static HWND hwnd;
|
| 41 | +static clock* s_clock; |
| 42 | +static int captureCount = 0; |
| 43 | + |
| 44 | +inline bool ResetCaptureCount(){ |
| 45 | + bool ret = captureCount != 0; |
| 46 | + captureCount = 0; |
| 47 | + return ret; |
| 48 | +} |
| 49 | + |
| 50 | +inline void AddCaptureCount(HWND hwnd){ |
| 51 | + if (captureCount++ == 0){ |
| 52 | + SetCapture(hwnd); |
| 53 | + } |
| 54 | +} |
| 55 | +inline void DecCaptureCount(){ |
| 56 | + if (--captureCount == 0){ |
| 57 | + ReleaseCapture(); |
| 58 | + } |
| 59 | +} |
40 | 60 |
|
41 | 61 | static void WINAPI wndproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
|
42 | 62 | {
|
| 63 | + ss_core_context* C; |
| 64 | + if (msg == WM_CREATE){ |
| 65 | + LPCREATESTRUCT cs = reinterpret_cast<LPCREATESTRUCT>(lparam); |
| 66 | + SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)cs->lpCreateParams); |
| 67 | + C = (ss_core_context*)(cs->lpCreateParams); |
| 68 | + } |
| 69 | + else { |
| 70 | + C = (ss_core_context*)(::GetWindowLongPtr(hwnd, GWLP_USERDATA)); |
| 71 | + } |
43 | 72 | switch (msg)
|
44 | 73 | {
|
| 74 | + case WM_KEYDOWN: |
| 75 | + { |
| 76 | + int keyCode = wparam & 0xff; |
| 77 | + } |
| 78 | + break; |
| 79 | + case WM_KEYUP: |
| 80 | + { |
| 81 | + int keyCode = wparam & 0xff; |
| 82 | + } |
| 83 | + break; |
| 84 | + case WM_SYSCOMMAND: |
| 85 | + { |
| 86 | + if (wparam == SC_MINIMIZE) |
| 87 | + { |
| 88 | + } |
| 89 | + else if (wparam == SC_RESTORE){ |
| 90 | + } |
| 91 | + DefWindowProcW(hwnd, msg, wparam, lparam); |
| 92 | + } |
| 93 | + break; |
| 94 | + case WM_MOUSEMOVE: |
| 95 | + { |
| 96 | + lua_State* L = ss_get_script_context(C); |
| 97 | + static int tagEvent = 0; |
| 98 | + ss_cache_script_from_macro(L, "SCRIPTS(onMouseMove)", &tagEvent); |
| 99 | + lua_pushinteger(L, (short)LOWORD(lparam)); |
| 100 | + lua_pushinteger(L, (short)HIWORD(lparam)); |
| 101 | + lua_pushnumber(L, s_clock->get_dt(true)); |
| 102 | + ss_lua_safe_call(L, 3, 0); |
| 103 | + } |
| 104 | + break; |
| 105 | + case WM_CAPTURECHANGED: |
| 106 | + { |
| 107 | + if (ResetCaptureCount()){ |
| 108 | + lua_State* L = ss_get_script_context(C); |
| 109 | + static int tagEvent = 0; |
| 110 | + ss_cache_script_from_macro(L, "SCRIPTS(onLostFocus)", &tagEvent); |
| 111 | + lua_pushnumber(L, s_clock->get_dt(true)); |
| 112 | + ss_lua_safe_call(L, 1, 0); |
| 113 | + } |
| 114 | + } |
| 115 | + break; |
| 116 | + case WM_LBUTTONDOWN: |
| 117 | + { |
| 118 | + AddCaptureCount(hwnd); |
| 119 | + lua_State* L = ss_get_script_context(C); |
| 120 | + static int tagEvent = 0; |
| 121 | + ss_cache_script_from_macro(L, "SCRIPTS(onMouseDown)", &tagEvent); |
| 122 | + lua_pushinteger(L, 1); |
| 123 | + lua_pushinteger(L, (short)LOWORD(lparam)); |
| 124 | + lua_pushinteger(L, (short)HIWORD(lparam)); |
| 125 | + lua_pushnumber(L, s_clock->get_dt(true)); |
| 126 | + ss_lua_safe_call(L, 4, 0); |
| 127 | + } |
| 128 | + break; |
| 129 | + case WM_LBUTTONUP: |
| 130 | + { |
| 131 | + DecCaptureCount(); |
| 132 | + lua_State* L = ss_get_script_context(C); |
| 133 | + static int tagEvent = 0; |
| 134 | + ss_cache_script_from_macro(L, "SCRIPTS(onMouseUp)", &tagEvent); |
| 135 | + lua_pushinteger(L, 1); |
| 136 | + lua_pushinteger(L, (short)LOWORD(lparam)); |
| 137 | + lua_pushinteger(L, (short)HIWORD(lparam)); |
| 138 | + lua_pushnumber(L, s_clock->get_dt(true)); |
| 139 | + ss_lua_safe_call(L, 4, 0); |
| 140 | + } |
| 141 | + break; |
| 142 | + case WM_RBUTTONDOWN: |
| 143 | + { |
| 144 | + AddCaptureCount(hwnd); |
| 145 | + lua_State* L = ss_get_script_context(C); |
| 146 | + static int tagEvent = 0; |
| 147 | + ss_cache_script_from_macro(L, "SCRIPTS(onMouseDown)", &tagEvent); |
| 148 | + lua_pushinteger(L, 2); |
| 149 | + lua_pushinteger(L, (short)LOWORD(lparam)); |
| 150 | + lua_pushinteger(L, (short)HIWORD(lparam)); |
| 151 | + lua_pushnumber(L, s_clock->get_dt(true)); |
| 152 | + ss_lua_safe_call(L, 4, 0); |
| 153 | + } |
| 154 | + break; |
| 155 | + case WM_RBUTTONUP: |
| 156 | + { |
| 157 | + DecCaptureCount(); |
| 158 | + lua_State* L = ss_get_script_context(C); |
| 159 | + static int tagEvent = 0; |
| 160 | + ss_cache_script_from_macro(L, "SCRIPTS(onMouseUp)", &tagEvent); |
| 161 | + lua_pushinteger(L, 2); |
| 162 | + lua_pushinteger(L, (short)LOWORD(lparam)); |
| 163 | + lua_pushinteger(L, (short)HIWORD(lparam)); |
| 164 | + lua_pushnumber(L, s_clock->get_dt(true)); |
| 165 | + ss_lua_safe_call(L, 4, 0); |
| 166 | + } |
| 167 | + break; |
45 | 168 | case WM_CLOSE:
|
46 | 169 | //TODO: Send global event.
|
47 | 170 | PostQuitMessage(0);
|
@@ -94,7 +217,7 @@ static BOOL create_window(ss_core_context* C)
|
94 | 217 |
|
95 | 218 | hwnd = CreateWindowExW(WS_EX_APPWINDOW | WS_EX_WINDOWEDGE,
|
96 | 219 | class_name, wsTitle,
|
97 |
| - (WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN) &(~WS_SIZEBOX) &(~WS_MAXIMIZEBOX), 0, 0, uiWidth, uiHeight, NULL, NULL, hInstance, NULL); |
| 220 | + (WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN) &(~WS_SIZEBOX) &(~WS_MAXIMIZEBOX), 0, 0, uiWidth, uiHeight, NULL, NULL, hInstance, C); |
98 | 221 |
|
99 | 222 | free(wsTitle);
|
100 | 223 | ShowWindow(hwnd, SW_SHOW);
|
@@ -169,65 +292,12 @@ static void destroy_render_device(ss_core_context* C){
|
169 | 292 | device = NULL;
|
170 | 293 | }
|
171 | 294 |
|
172 |
| -struct clock{ |
173 |
| -public: |
174 |
| - virtual ~clock(){}; |
175 |
| - virtual double get_dt() = 0; |
176 |
| - virtual bool is_valid() = 0; |
177 |
| -}; |
178 |
| - |
179 |
| -struct hr_clock : clock{ |
180 |
| - hr_clock(){ |
181 |
| - LARGE_INTEGER freq; |
182 |
| - valid = QueryPerformanceFrequency(&freq) && QueryPerformanceCounter(&stamp); |
183 |
| - frequency = (double)freq.QuadPart; |
184 |
| - } |
185 |
| - virtual ~hr_clock(){ |
186 |
| - |
187 |
| - } |
188 |
| - virtual double get_dt(){ |
189 |
| - LARGE_INTEGER newT; |
190 |
| - QueryPerformanceCounter(&newT); |
191 |
| - |
192 |
| - double ret = (newT.QuadPart - stamp.QuadPart) / frequency; |
193 |
| - stamp = newT; |
194 |
| - return ret; |
195 |
| - } |
196 |
| - virtual bool is_valid(){ |
197 |
| - return valid; |
198 |
| - } |
199 |
| -private: |
200 |
| - bool valid; |
201 |
| - double frequency; |
202 |
| - LARGE_INTEGER stamp; |
203 |
| -}; |
204 |
| - |
205 |
| -struct lr_clock : clock{ |
206 |
| - lr_clock(){ |
207 |
| - stamp = GetTickCount(); |
208 |
| - } |
209 |
| - virtual ~lr_clock(){ |
210 |
| - |
211 |
| - } |
212 |
| - virtual double get_dt(){ |
213 |
| - DWORD newT = GetTickCount(); |
214 |
| - double ret = (newT - stamp) / 1000.0; |
215 |
| - stamp = newT; |
216 |
| - return ret; |
217 |
| - } |
218 |
| - virtual bool is_valid(){ |
219 |
| - return true; |
220 |
| - } |
221 |
| - DWORD stamp; |
222 |
| -}; |
223 |
| - |
224 |
| - |
225 | 295 | static void main_loop(ss_core_context* C)
|
226 | 296 | {
|
227 |
| - clock* t = new hr_clock(); |
228 |
| - if (!t->is_valid()){ |
229 |
| - delete t; |
230 |
| - t = new lr_clock(); |
| 297 | + s_clock = new hr_clock(); |
| 298 | + if (!s_clock->is_valid()){ |
| 299 | + delete s_clock; |
| 300 | + s_clock = new lr_clock(); |
231 | 301 | }
|
232 | 302 | lua_State *L = ss_get_script_context(C);
|
233 | 303 |
|
@@ -269,15 +339,15 @@ static void main_loop(ss_core_context* C)
|
269 | 339 |
|
270 | 340 | static int tagOnFrame = 0;
|
271 | 341 | ss_cache_script_from_macro(L, "SCRIPTS(onFrame)", &tagOnFrame);
|
272 |
| - lua_pushnumber(L, t->get_dt()); |
| 342 | + lua_pushnumber(L, s_clock->get_dt()); |
273 | 343 | ss_lua_safe_call(L, 1, 0);
|
274 | 344 |
|
275 | 345 | ss_db_flush(C);
|
276 | 346 | device->present();
|
277 | 347 | }
|
278 | 348 | }
|
279 | 349 |
|
280 |
| - delete t; |
| 350 | + delete s_clock; |
281 | 351 | ss_run_script_from_macro(C, "SCRIPTS(onStop)");
|
282 | 352 | }
|
283 | 353 |
|
|
0 commit comments