Cocos2d-x Win32 keyboard simulated touch event

Source: Internet
Author: User

The Cocos2d-x allows you to simulate a response to a touch event by clicking a Win32 mouse, that is, simulating only a single touch. In the previous article "How To Make A horizontal Combat Pass game Cocos2d-x 2.0.4" with direction keys and attack keys, the mouse single point of touch can not meet the requirements of Win32 testing, the extension here allows the keyboard to simulate a response to the touch event at the same time.

Cocos2d-x version: 2.1.3
ModifyMain. cppThe file content is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# Include "Main. H"
# Include "appdelegate. H"
# Include "cceglview. H"

Using_ns_cc;

Lresult mywndprochook (uint message, wparam, lparam, bool * pprocessed );

// Uncomment below line, open debug Console
// # Define use_win32_console

Int apientry _ twinmain (hinstance,
Hinstance hprevinstance,
Lptstr lpcmdline,
Int ncmdshow)
{
Unreferenced_parameter (hprevinstance );
Unreferenced_parameter (lpcmdline );

# Ifdef use_win32_console
Allocconsole ();
Freopen ("conin $", "r", stdin );
Freopen ("conout $", "W", stdout );
Freopen ("conout $", "W", stderr );
# Endif

// Create the application instance
Appdelegate app;
Cceglview * eglview = cceglview: Export dopenglview ();
Eglview-> setframesize (480,320 );
// Eglview-> setframesize (960,640 );
// Eglview-> setframesize (2048,153 6 );
// Eglview-> setframezoomfactor (0.4f );
Eglview-> setwndproc (mywndprochook); // Add this sentence

Int ret = ccapplication: sharedapplication ()-> Run ();

# Ifdef use_win32_console
Freeconsole ();
# Endif

Return ret;
}

Lresult mywndprochook (uint message, wparam, lparam, bool * pprocessed)
{
Switch (Message)
{
Case wm_keydown:
Case wm_keyup:
{
// Set with the coordinates when framesize = designresolutionsize
Ccpoint Pt = ccpointzero;
Switch (wparam)
{
Case 'A': // left
{
PT = CPP (25,255 );
}
Break;
Case 'D': // right
{
PT = CPP (95,256 );
}
Break;
Case 'W': // up
{
PT = CPP (63,214 );
}
Break;
Case's ': // down
{
PT = CPP (62,290 );
}
Break;
Case 'J': // attack
{
PT = CPP (367,277 );
}
Break;
Case 'K': // skip
{
PT = CPP (445,247 );
}
Break;
Default:
Return 0;
}
If (getkeystate ('D') & 0x8000)
{
If (getkeystate ('W') & 0x8000) // upper right corner
{
PT = CPP (91,227 );
}
Else if (getkeystate ('s ') & 0x8000) // bottom right corner
{
PT = CPP (91,284 );
}
}
Else if (getkeystate ('A') & 0x8000)
{
If (getkeystate ('W') & 0x8000) // upper left corner
{
PT = CPP (36,227 );
}
Else if (getkeystate ('s ') & 0x8000) // lower left corner
{
PT = CPP (36,284 );
}
}

Cceglview * eglview = cceglview: Export dopenglview ();
Ccsize originaldesignresolutionsize = ccsizemake (480,320); // Original Design resolution size
Resolutionpolicy eresolutionpolicy = kresolutionfixedwidth; // Resolution Policy
Ccsize obdesignresolutionsize = eglview-> getdesignresolutionsize ();
Int offsetwidth = obdesignresolutionsize. Width-originaldesignresolutionsize. width;
Int offsetheight = obdesignresolutionsize. Height-originaldesignresolutionsize. height;
Ccsize obscreensize = eglview-> getframesize ();
Int offsetwid2= obscreensize. Width-originaldesignresolutionsize. width;
Int offsetheight2 = obscreensize. Height-originaldesignresolutionsize. height;
Switch (eresolutionpolicy)
{
Case kresolutionexactfit:
{
//...
}
Break;
Case kresolutionnoborder:
{
//...
}
Break;
Case kresolutionshowall:
{
PT. x + = offsetwid2/ 2;
PT. Y + = offsetheight2/2;
}
Break;
Case kresolutionfixedheight:
{
//...
}
Break;
Case kresolutionfixedwidth:
{
PT. Y + = offsetheight;
}
Break;
}

PT. x * = eglview-> getscalex ();
PT. y * = eglview-> getscaley ();

Int id = wparam;
If (Message = wm_keydown)
{
Eglview-> handletouchesbegin (1, & ID, & pt. X, & pt. y );
}
Else
{
Eglview-> handletouchesend (1, & ID, & pt. X, & pt. y );
}

* Pprocessed = true;
}
Break;
}

Return 0;
}

From the code above, we can see that instead of directly calling logical functions, we call touch events. Coordinates are calculated when the window is designed for resolution. For example, if the resolution is 480x320, the changed coordinates are automatically calculated after the resolution is changed.

Shows the touch events corresponding to the keyboard:

In addition, to the right + up = the upper right corner, similar to the role can perform a diagonal walk.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.