Skip to content

Commit eb7f507

Browse files
committed
add mouse event.
1 parent 248d45b commit eb7f507

File tree

3 files changed

+188
-60
lines changed

3 files changed

+188
-60
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ SET(LOCAL_SOURCES_FILES
99
src/project.cpp
1010
src/launcher.h
1111
src/defines.c
12+
src/clock.h
1213

1314
res/resource.h
1415
res/resource.rc

src/clock.h

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#pragma once
2+
3+
struct clock{
4+
public:
5+
virtual ~clock(){};
6+
virtual double get_dt(bool peek = false) = 0;
7+
virtual bool is_valid() = 0;
8+
};
9+
10+
struct hr_clock : clock{
11+
hr_clock(){
12+
LARGE_INTEGER freq;
13+
valid = QueryPerformanceFrequency(&freq) && QueryPerformanceCounter(&stamp);
14+
frequency = (double)freq.QuadPart;
15+
}
16+
virtual ~hr_clock(){
17+
18+
}
19+
virtual double get_dt(bool peek = false){
20+
LARGE_INTEGER newT;
21+
QueryPerformanceCounter(&newT);
22+
23+
double ret = (newT.QuadPart - stamp.QuadPart) / frequency;
24+
if (!peek){
25+
stamp = newT;
26+
}
27+
return ret;
28+
}
29+
virtual bool is_valid(){
30+
return valid;
31+
}
32+
private:
33+
bool valid;
34+
double frequency;
35+
LARGE_INTEGER stamp;
36+
};
37+
38+
struct lr_clock : clock{
39+
lr_clock(){
40+
stamp = GetTickCount();
41+
}
42+
virtual ~lr_clock(){
43+
44+
}
45+
virtual double get_dt(bool peek = false){
46+
DWORD newT = GetTickCount();
47+
double ret = (newT - stamp) / 1000.0;
48+
if (!peek){
49+
stamp = newT;
50+
}
51+
return ret;
52+
}
53+
virtual bool is_valid(){
54+
return true;
55+
}
56+
DWORD stamp;
57+
};

src/main.cpp

Lines changed: 130 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <ssengine/macros.h>
88
#include "launcher.h"
9+
#include "clock.h"
910

1011
#include <ssengine/uri.h>
1112
#include <ssengine/render/resources.h>
@@ -37,11 +38,133 @@ static ss_logger logger = {
3738
};
3839

3940
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+
}
4060

4161
static void WINAPI wndproc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
4262
{
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+
}
4372
switch (msg)
4473
{
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;
45168
case WM_CLOSE:
46169
//TODO: Send global event.
47170
PostQuitMessage(0);
@@ -94,7 +217,7 @@ static BOOL create_window(ss_core_context* C)
94217

95218
hwnd = CreateWindowExW(WS_EX_APPWINDOW | WS_EX_WINDOWEDGE,
96219
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);
98221

99222
free(wsTitle);
100223
ShowWindow(hwnd, SW_SHOW);
@@ -169,65 +292,12 @@ static void destroy_render_device(ss_core_context* C){
169292
device = NULL;
170293
}
171294

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-
225295
static void main_loop(ss_core_context* C)
226296
{
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();
231301
}
232302
lua_State *L = ss_get_script_context(C);
233303

@@ -269,15 +339,15 @@ static void main_loop(ss_core_context* C)
269339

270340
static int tagOnFrame = 0;
271341
ss_cache_script_from_macro(L, "SCRIPTS(onFrame)", &tagOnFrame);
272-
lua_pushnumber(L, t->get_dt());
342+
lua_pushnumber(L, s_clock->get_dt());
273343
ss_lua_safe_call(L, 1, 0);
274344

275345
ss_db_flush(C);
276346
device->present();
277347
}
278348
}
279349

280-
delete t;
350+
delete s_clock;
281351
ss_run_script_from_macro(C, "SCRIPTS(onStop)");
282352
}
283353

0 commit comments

Comments
 (0)