CCGLView.cpp
13.8 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "platform/CCGLView.h"
#include "base/CCTouch.h"
#include "base/CCDirector.h"
#include "base/CCEventDispatcher.h"
#include "2d/CCCamera.h"
#include "2d/CCScene.h"
#include "renderer/CCRenderer.h"
NS_CC_BEGIN
namespace {
static Touch* g_touches[EventTouch::MAX_TOUCHES] = { nullptr };
static unsigned int g_indexBitsUsed = 0;
// System touch pointer ID (It may not be ascending order number) <-> Ascending order number from 0
static std::map<intptr_t, int> g_touchIdReorderMap;
static int getUnUsedIndex()
{
int i;
int temp = g_indexBitsUsed;
for (i = 0; i < EventTouch::MAX_TOUCHES; i++) {
if (! (temp & 0x00000001)) {
g_indexBitsUsed |= (1 << i);
return i;
}
temp >>= 1;
}
// all bits are used
return -1;
}
static std::vector<Touch*> getAllTouchesVector()
{
std::vector<Touch*> ret;
int i;
int temp = g_indexBitsUsed;
for (i = 0; i < EventTouch::MAX_TOUCHES; i++) {
if ( temp & 0x00000001) {
ret.push_back(g_touches[i]);
}
temp >>= 1;
}
return ret;
}
static void removeUsedIndexBit(int index)
{
if (index < 0 || index >= EventTouch::MAX_TOUCHES)
{
return;
}
unsigned int temp = 1 << index;
temp = ~temp;
g_indexBitsUsed &= temp;
}
}
//default context attributions are set as follows
GLContextAttrs GLView::_glContextAttrs = {8, 8, 8, 8, 24, 8, 0};
void GLView::setGLContextAttrs(GLContextAttrs& glContextAttrs)
{
_glContextAttrs = glContextAttrs;
}
GLContextAttrs GLView::getGLContextAttrs()
{
return _glContextAttrs;
}
GLView::GLView()
: _screenSize(0,0)
, _designResolutionSize(0,0)
, _scaleX(1.0f)
, _scaleY(1.0f)
, _resolutionPolicy(ResolutionPolicy::UNKNOWN)
{
}
GLView::~GLView()
{
}
void GLView::pollEvents()
{
}
void GLView::updateDesignResolutionSize()
{
if (_screenSize.width > 0 && _screenSize.height > 0
&& _designResolutionSize.width > 0 && _designResolutionSize.height > 0)
{
_scaleX = (float)_screenSize.width / _designResolutionSize.width;
_scaleY = (float)_screenSize.height / _designResolutionSize.height;
if (_resolutionPolicy == ResolutionPolicy::NO_BORDER)
{
_scaleX = _scaleY = MAX(_scaleX, _scaleY);
}
else if (_resolutionPolicy == ResolutionPolicy::SHOW_ALL)
{
_scaleX = _scaleY = MIN(_scaleX, _scaleY);
}
else if ( _resolutionPolicy == ResolutionPolicy::FIXED_HEIGHT) {
_scaleX = _scaleY;
_designResolutionSize.width = ceilf(_screenSize.width/_scaleX);
}
else if ( _resolutionPolicy == ResolutionPolicy::FIXED_WIDTH) {
_scaleY = _scaleX;
_designResolutionSize.height = ceilf(_screenSize.height/_scaleY);
}
// calculate the rect of viewport
float viewPortW = _designResolutionSize.width * _scaleX;
float viewPortH = _designResolutionSize.height * _scaleY;
_viewPortRect.setRect((_screenSize.width - viewPortW) / 2, (_screenSize.height - viewPortH) / 2, viewPortW, viewPortH);
// reset director's member variables to fit visible rect
auto director = Director::getInstance();
director->_winSizeInPoints = getDesignResolutionSize();
director->_isStatusLabelUpdated = true;
director->setProjection(director->getProjection());
// Github issue #16139
// A default viewport is needed in order to display the FPS,
// since the FPS are rendered in the Director, and there is no viewport there.
// Everything, including the FPS should renderer in the Scene.
//TODO: minggo
// glViewport(0, 0, _screenSize.width, _screenSize.height);
}
}
void GLView::setDesignResolutionSize(float width, float height, ResolutionPolicy resolutionPolicy)
{
CCASSERT(resolutionPolicy != ResolutionPolicy::UNKNOWN, "should set resolutionPolicy");
if (width == 0.0f || height == 0.0f)
{
return;
}
_designResolutionSize.setSize(width, height);
_resolutionPolicy = resolutionPolicy;
updateDesignResolutionSize();
}
const Size& GLView::getDesignResolutionSize() const
{
return _designResolutionSize;
}
Size GLView::getFrameSize() const
{
return _screenSize;
}
void GLView::setFrameSize(float width, float height)
{
_screenSize = Size(width, height);
// Github issue #16003 and #16485
// only update the designResolution if it wasn't previously set
if (_designResolutionSize.equals(Size::ZERO))
_designResolutionSize = _screenSize;
}
Rect GLView::getVisibleRect() const
{
Rect ret;
ret.size = getVisibleSize();
ret.origin = getVisibleOrigin();
return ret;
}
Rect GLView::getSafeAreaRect() const
{
return getVisibleRect();
}
Size GLView::getVisibleSize() const
{
if (_resolutionPolicy == ResolutionPolicy::NO_BORDER)
{
return Size(_screenSize.width/_scaleX, _screenSize.height/_scaleY);
}
else
{
return _designResolutionSize;
}
}
Vec2 GLView::getVisibleOrigin() const
{
if (_resolutionPolicy == ResolutionPolicy::NO_BORDER)
{
return Vec2((_designResolutionSize.width - _screenSize.width/_scaleX)/2,
(_designResolutionSize.height - _screenSize.height/_scaleY)/2);
}
else
{
return Vec2::ZERO;
}
}
void GLView::setViewPortInPoints(float x , float y , float w , float h)
{
Viewport vp;
vp.x = (int)(x * _scaleX + _viewPortRect.origin.x);
vp.y = (int)(y * _scaleY + _viewPortRect.origin.y);
vp.w = (unsigned int)(w * _scaleX);
vp.h = (unsigned int)(h * _scaleY);
Camera::setDefaultViewport(vp);
}
void GLView::setScissorInPoints(float x , float y , float w , float h)
{
auto renderer = Director::getInstance()->getRenderer();
renderer->setScissorRect((int)(x * _scaleX + _viewPortRect.origin.x),
(int)(y * _scaleY + _viewPortRect.origin.y),
(unsigned int)(w * _scaleX),
(unsigned int)(h * _scaleY));
}
bool GLView::isScissorEnabled()
{
auto renderer = Director::getInstance()->getRenderer();
return renderer->getScissorTest();
}
Rect GLView::getScissorRect() const
{
auto renderer = Director::getInstance()->getRenderer();
auto& rect = renderer->getScissorRect();
float x = (rect.x - _viewPortRect.origin.x) / _scaleX;
float y = (rect.y- _viewPortRect.origin.y) / _scaleY;
float w = rect.width/ _scaleX;
float h = rect.height / _scaleY;
return Rect(x, y, w, h);
}
void GLView::setViewName(const std::string& viewname )
{
_viewName = viewname;
}
const std::string& GLView::getViewName() const
{
return _viewName;
}
void GLView::handleTouchesBegin(int num, intptr_t ids[], float xs[], float ys[])
{
intptr_t id = 0;
float x = 0.0f;
float y = 0.0f;
int unusedIndex = 0;
EventTouch touchEvent;
for (int i = 0; i < num; ++i)
{
id = ids[i];
x = xs[i];
y = ys[i];
auto iter = g_touchIdReorderMap.find(id);
// it is a new touch
if (iter == g_touchIdReorderMap.end())
{
unusedIndex = getUnUsedIndex();
// The touches is more than MAX_TOUCHES ?
if (unusedIndex == -1) {
CCLOG("The touches is more than MAX_TOUCHES, unusedIndex = %d", unusedIndex);
continue;
}
Touch* touch = g_touches[unusedIndex] = new (std::nothrow) Touch();
touch->setTouchInfo(unusedIndex, (x - _viewPortRect.origin.x) / _scaleX,
(y - _viewPortRect.origin.y) / _scaleY);
CCLOGINFO("x = %f y = %f", touch->getLocationInView().x, touch->getLocationInView().y);
g_touchIdReorderMap.emplace(id, unusedIndex);
touchEvent._touches.push_back(touch);
}
}
if (touchEvent._touches.size() == 0)
{
CCLOG("touchesBegan: size = 0");
return;
}
touchEvent._eventCode = EventTouch::EventCode::BEGAN;
auto dispatcher = Director::getInstance()->getEventDispatcher();
dispatcher->dispatchEvent(&touchEvent);
}
void GLView::handleTouchesMove(int num, intptr_t ids[], float xs[], float ys[])
{
handleTouchesMove(num, ids, xs, ys, nullptr, nullptr);
}
void GLView::handleTouchesMove(int num, intptr_t ids[], float xs[], float ys[], float fs[], float ms[])
{
intptr_t id = 0;
float x = 0.0f;
float y = 0.0f;
float force = 0.0f;
float maxForce = 0.0f;
EventTouch touchEvent;
for (int i = 0; i < num; ++i)
{
id = ids[i];
x = xs[i];
y = ys[i];
force = fs ? fs[i] : 0.0f;
maxForce = ms ? ms[i] : 0.0f;
auto iter = g_touchIdReorderMap.find(id);
if (iter == g_touchIdReorderMap.end())
{
CCLOG("if the index doesn't exist, it is an error");
continue;
}
CCLOGINFO("Moving touches with id: %d, x=%f, y=%f, force=%f, maxFource=%f", (int)id, x, y, force, maxForce);
Touch* touch = g_touches[iter->second];
if (touch)
{
touch->setTouchInfo(iter->second, (x - _viewPortRect.origin.x) / _scaleX,
(y - _viewPortRect.origin.y) / _scaleY, force, maxForce);
touchEvent._touches.push_back(touch);
}
else
{
// It is error, should return.
CCLOG("Moving touches with id: %ld error", (long int)id);
return;
}
}
if (touchEvent._touches.size() == 0)
{
CCLOG("touchesMoved: size = 0");
return;
}
touchEvent._eventCode = EventTouch::EventCode::MOVED;
auto dispatcher = Director::getInstance()->getEventDispatcher();
dispatcher->dispatchEvent(&touchEvent);
}
void GLView::handleTouchesOfEndOrCancel(EventTouch::EventCode eventCode, int num, intptr_t ids[], float xs[], float ys[])
{
intptr_t id = 0;
float x = 0.0f;
float y = 0.0f;
EventTouch touchEvent;
for (int i = 0; i < num; ++i)
{
id = ids[i];
x = xs[i];
y = ys[i];
auto iter = g_touchIdReorderMap.find(id);
if (iter == g_touchIdReorderMap.end())
{
CCLOG("if the index doesn't exist, it is an error");
continue;
}
/* Add to the set to send to the director */
Touch* touch = g_touches[iter->second];
if (touch)
{
CCLOGINFO("Ending touches with id: %d, x=%f, y=%f", (int)id, x, y);
touch->setTouchInfo(iter->second, (x - _viewPortRect.origin.x) / _scaleX,
(y - _viewPortRect.origin.y) / _scaleY);
touchEvent._touches.push_back(touch);
g_touches[iter->second] = nullptr;
removeUsedIndexBit(iter->second);
g_touchIdReorderMap.erase(id);
}
else
{
CCLOG("Ending touches with id: %ld error", static_cast<long>(id));
return;
}
}
if (touchEvent._touches.size() == 0)
{
CCLOG("touchesEnded or touchesCancel: size = 0");
return;
}
touchEvent._eventCode = eventCode;
auto dispatcher = Director::getInstance()->getEventDispatcher();
dispatcher->dispatchEvent(&touchEvent);
for (auto& touch : touchEvent._touches)
{
// release the touch object.
touch->release();
}
}
void GLView::handleTouchesEnd(int num, intptr_t ids[], float xs[], float ys[])
{
handleTouchesOfEndOrCancel(EventTouch::EventCode::ENDED, num, ids, xs, ys);
}
void GLView::handleTouchesCancel(int num, intptr_t ids[], float xs[], float ys[])
{
handleTouchesOfEndOrCancel(EventTouch::EventCode::CANCELLED, num, ids, xs, ys);
}
const Rect& GLView::getViewPortRect() const
{
return _viewPortRect;
}
std::vector<Touch*> GLView::getAllTouches() const
{
return getAllTouchesVector();
}
float GLView::getScaleX() const
{
return _scaleX;
}
float GLView::getScaleY() const
{
return _scaleY;
}
void GLView::renderScene(Scene* scene, Renderer* renderer)
{
CCASSERT(scene, "Invalid Scene");
CCASSERT(renderer, "Invalid Renderer");
scene->render(renderer, Mat4::IDENTITY, nullptr);
}
NS_CC_END