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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533 | #include "fsCDisplay.h"
#include <base/EventDispatcher.h>
#include <base/Director.h>
#include <renderer/Renderer.h>
#ifdef AXMOL_V3
#include <scene/Camera.h>
#else
#include <2d/Camera.h>
#endif
#ifdef AXMOL_V3
#include <platform/RenderView.h>
#else
#if defined(AX_PLATFORM_PC) || (AX_TARGET_PLATFORM == AX_PLATFORM_WASM)
#include <platform/RenderViewImpl.h>
#elif AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID
#include <platform/android/RenderViewImpl-android.h>
#elif AX_TARGET_PLATFORM == AX_PLATFORM_IOS
#include <platform/ios/RenderViewImpl-ios.h>
#elif AX_TARGET_PLATFORM == AX_PLATFORM_WINRT
#include <platform/winrt/RenderViewImpl-winrt.h>
#endif
#endif
#include <fsCore/src/debug/fsAssert.h>
#include <fsCore/fsIHardware.h>
#include <fsCore/src/fsCAppSettings.h>
#include <fsAppCore/src/fsCAppCoreMain.h>
#include <fsAudio/fsIAudioManager.h>
#include <fsRenderer/impl/axmol/fsNAxmolRenderView.h>
namespace
{
struct fsSScissorRect
{
fsS32 x = 0;
fsS32 y = 0;
fsF32 w = 0.0f;
fsF32 h = 0.0f;
};
fsSScissorRect logicalScissorToFramebuffer(
const fsF32 pLogicalX,
const fsF32 pLogicalY,
const fsF32 pLogicalW,
const fsF32 pLogicalH,
const fsF32 pPixelScaleX,
const fsF32 pPixelScaleY)
{
fsSScissorRect rect;
rect.x = static_cast<fsS32>(pLogicalX * pPixelScaleX);
rect.y = static_cast<fsS32>(pLogicalY * pPixelScaleY);
rect.w = pLogicalW * pPixelScaleX;
rect.h = pLogicalH * pPixelScaleY;
return rect;
}
// Collapses the AXMOL_V3/non-V3 enum qualification into one place.
#ifdef AXMOL_V3
void designResolutionApply(ax::RenderViewCore* const pRenderView, const fsF32 pW, const fsF32 pH, const fsBool pShowAll = false)
#else
void designResolutionApply(ax::RenderView* const pRenderView, const fsF32 pW, const fsF32 pH, const fsBool pShowAll = false)
#endif
{
#ifdef AXMOL_V3
pRenderView->setDesignResolutionSize(pW, pH, pShowAll ? ax::ResolutionPolicy::SHOW_ALL : ax::ResolutionPolicy::NO_BORDER);
#else
pRenderView->setDesignResolutionSize(pW, pH, pShowAll ? ResolutionPolicy::SHOW_ALL : ResolutionPolicy::NO_BORDER);
#endif
}
#ifdef AXMOL_V3
void mobileDesignResolutionApply(ax::RenderViewCore* const pRenderView, const fsCAppSettings* const pAppSett)<--- Parameter 'pRenderView' can be declared as pointer to const
#else
void mobileDesignResolutionApply(ax::RenderView* const pRenderView, const fsCAppSettings* const pAppSett)<--- Parameter 'pRenderView' can be declared as pointer to const
#endif
{
#if !defined(AX_PLATFORM_PC) && (AX_TARGET_PLATFORM != AX_PLATFORM_WASM)
#if (AX_TARGET_PLATFORM == AX_PLATFORM_WINRT)
const fsBool showAll = true;
#elif AX_TARGET_PLATFORM == AX_PLATFORM_IOS
const fsBool showAll = fsIDisplay::fixedAspectGet();
#else
const fsBool showAll = false;
#endif
designResolutionApply(
pRenderView,
static_cast<fsF32>(pAppSett->s32Get("appCanvasW")),
static_cast<fsF32>(pAppSett->s32Get("appCanvasH")),
showAll);
#endif
}
}
void fsCDisplay::defaultCameraFlagSetDo(fsU32 pFlag)
{
mImpl->getDefaultCamera()->setCameraFlag(static_cast<ax::CameraFlag>(pFlag));
}
fsBool fsCDisplay::displayTypeIsDesktopGet() const
{
#if defined(AX_PLATFORM_PC)
return true;
#else
return false;
#endif
}
fsPoint fsCDisplay::fixedDisplaySizeGet() const
{
auto* const renderView = ax::Director::getInstance()->getRenderView();
ax::Vec2 renderViewDimensions;
if (renderView)
{
renderViewDimensions = renderView->getDesignResolutionSize();
}
if (renderViewDimensions.width <= 0.0f || renderViewDimensions.height <= 0.0f)
{
const fsCAppSettings* appSett = fsCAppCoreMain::instanceGet()->settingsGet();
renderViewDimensions.set(
static_cast<float>(appSett->s32Get("appCanvasW")),
static_cast<float>(appSett->s32Get("appCanvasH")));
}
const fsS32 width = (renderViewDimensions.width > renderViewDimensions.height
? static_cast<fsS32>(renderViewDimensions.width)
: static_cast<fsS32>(renderViewDimensions.height));
const fsS32 height = (renderViewDimensions.width > renderViewDimensions.height
? static_cast<fsS32>(renderViewDimensions.height)
: static_cast<fsS32>(renderViewDimensions.width));
fsLogMsg("Setting fixed display size:%d,%d\n", width, height);
return fsPoint(width, height);
}
void fsCDisplay::desktopScreenModesEnumerateDo()
{
#if defined(AX_PLATFORM_PC)
const fsCAppSettings* appSett = fsCAppCoreMain::instanceGet()->settingsGet();<--- Variable 'appSett' is assigned a value that is never used.
// fsCDisplayMode initialWindowSoWeGetProperQuitAndAboutFromGLFWAutoCreate(appSett->s32Get("appCanvasW"), appSett->s32Get("appCanvasH"), 32);
fsCDisplayMode initialWindowSoWeGetProperQuitAndAboutFromGLFWAutoCreate(640, 480, 32);
renderViewCreate(&initialWindowSoWeGetProperQuitAndAboutFromGLFWAutoCreate, false);
// glfwInit();
// fsS32 modeCount;
// fsS32 currentMode = 0;
// const GLFWvidmode* modes = glfwGetVideoModes(glfwGetPrimaryMonitor(), &modeCount);
// while (currentMode++ < modeCount)
// {
// screenModeAdd(modes[currentMode].width, modes[currentMode].height, 32);
// }
//const GLFWvidmode* mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
//screenModeAdd(mode->width, mode->height, 32);
// glfwTerminate();
#endif
}
void fsCDisplay::buffersSwap()
{
#if defined(AX_PLATFORM_PC) || (AX_TARGET_PLATFORM == AX_PLATFORM_WINRT)
ax::Director::getInstance()->getRenderView()->swapBuffers();
#endif
}
void fsCDisplay::clipWindowSetDo(const fsCDisplayMode* const pDisplayMode)
{
const auto displayW = static_cast<fsF32>(pDisplayMode->sizeGet().x);
const auto displayH = static_cast<fsF32>(pDisplayMode->sizeGet().y);
const auto desW = static_cast<fsF32>(mDesignedDisplayMode.sizeGet().x);
const auto desH = static_cast<fsF32>(mDesignedDisplayMode.sizeGet().y);
const auto scaleX = static_cast<fsF32>(scaleGet().x);
const auto scaleY = static_cast<fsF32>(scaleGet().y);
const auto director = axmol::Director::getInstance();
auto* const renderView = director->getRenderView();
fsF32 pixelScaleX = 1.0f;
fsF32 pixelScaleY = 1.0f;
fsNAxmolRenderView::pixelScaleGet(renderView, pixelScaleX, pixelScaleY);
fsSScissorRect scissorRect;
#if AX_TARGET_PLATFORM == AX_PLATFORM_IOS
if (fsIDisplay::fixedAspectGet())
{
// On iOS, Axmol's logical visible area and framebuffer size do not map
// cleanly through getRetinaFactor() under fixedAspect. Apply the clip in
// visible logical coordinates and convert it to framebuffer pixels here.
const auto visibleSize = renderView->getVisibleSize();
const auto fit = fsNAxmolRenderView::aspectFitRectGet(desW / desH, visibleSize.width, visibleSize.height);
scissorRect = logicalScissorToFramebuffer(fit.x, fit.y, fit.w, fit.h, pixelScaleX, pixelScaleY);
director->getRenderer()->setScissorRect(scissorRect.x, scissorRect.y, scissorRect.w, scissorRect.h);
director->getRenderer()->setScissorTest(true);
return;
}
#endif
#ifdef AXMOL_V3
scissorRect.x = static_cast<fsS32>((displayW * (1.0f - scaleX) - (desW - displayW) * scaleX) / 2.0f);
scissorRect.y = static_cast<fsS32>((displayH * (1.0f - scaleY) - (desH - displayH) * scaleY) / 2.0f);
scissorRect.w = desW * scaleX * renderView->getRenderScale();
scissorRect.h = desH * scaleY * renderView->getRenderScale();
#else
#if defined(AX_PLATFORM_PC) || (AX_TARGET_PLATFORM == AX_PLATFORM_WASM)
// Desktop/WASM: getFrameSize()/getVisibleSize() are both reported in points
// (their ratio is always ~1). The actual point->pixel factor is tracked
// separately by RenderViewImpl via getRetinaFactor().
const auto clipScaleX = static_cast<fsF32>(renderView->getRetinaFactor());
const auto clipScaleY = clipScaleX;
#else
// Android's RenderView never overrides getRetinaFactor() (always 1); the
// real device pixel ratio only shows up as the frame/visible size ratio.
const auto clipScaleX = pixelScaleX;
const auto clipScaleY = pixelScaleY;
#endif
scissorRect = logicalScissorToFramebuffer(
(displayW * (1.0f - scaleX) - (desW - displayW) * scaleX) / 2.0f,
(displayH * (1.0f - scaleY) - (desH - displayH) * scaleY) / 2.0f,
desW * scaleX,
desH * scaleY,
clipScaleX,
clipScaleY);
#endif
director->getRenderer()->setScissorRect(scissorRect.x, scissorRect.y, scissorRect.w, scissorRect.h);
director->getRenderer()->setScissorTest(true);
}
fsStr fsCDisplay::nameGet()
{
const fsCAppSettings* const settings = fsCAppCoreMain::instanceGet()->settingsGet();
fsStr name = settings->strGetWithDefault("installedName", "testApp");
if (settings->buildFlavourGet() == "CE")
{
name += " Collectors Edition";
}
return name;
}
fsBool fsCDisplay::desktopScreenModeSetDo(const fsCDisplayMode* const pDisplayMode, fsBool pFullScreen)
{
#if defined(AX_PLATFORM_PC)
auto director = ax::Director::getInstance();
auto renderView = director->getRenderView();
if (!renderView)
{
renderViewCreate(pDisplayMode, pFullScreen);
}
else
{
#ifdef AXMOL_V3
auto renderViewDesktop = static_cast<ax::RenderView*>(renderView);
#else
auto renderViewDesktop = static_cast<ax::RenderViewImpl*>(renderView);
#endif
auto glWindow = renderViewDesktop->getWindow();
if (pFullScreen)
{
fsLogMsg("Setting full screen resolution to %dx%d.\n", pDisplayMode->widthGet(), pDisplayMode->heightGet());
if (!renderViewDesktop->isFullscreen())
{
glfwSetWindowPos(glWindow, 0, 0);
renderViewDesktop->setFullscreen();
}
renderView->setFrameSize(pDisplayMode->widthGet(), pDisplayMode->heightGet());
designResolutionApply(renderView, pDisplayMode->widthGet(), pDisplayMode->heightGet());
#ifdef AXMOL_V3
mImpl->getDefaultCamera()->initClassic();
#endif
}
else
{
fsLogMsg("Setting windowed screen resolution to %dx%d.",
pDisplayMode->widthGet(),
pDisplayMode->heightGet());
renderViewDesktop->setWindowed(pDisplayMode->widthGet(), pDisplayMode->heightGet());
renderView->setFrameSize(pDisplayMode->widthGet(), pDisplayMode->heightGet());
designResolutionApply(renderView, pDisplayMode->widthGet(), pDisplayMode->heightGet());
#ifdef AXMOL_V3
mImpl->getDefaultCamera()->initClassic();
#endif
GLFWvidmode const* const videoMode = glfwGetVideoMode(glfwGetPrimaryMonitor());
glfwSetWindowPos(glWindow,
(videoMode->width - pDisplayMode->widthGet()) / 2,
(videoMode->height - pDisplayMode->heightGet()) / 2);
clipWindowSet(pDisplayMode);
}
}
#endif
buffersSwap();
return true;
}
void fsCDisplay::renderViewCreate(const fsCDisplayMode* const pDisplayMode, fsBool pFullScreen)
{
auto director = axmol::Director::getInstance();
auto renderView = director->getRenderView();
auto installedName = nameGet();
if (renderView)<--- first condition
{
const fsCAppSettings* appSett = fsCAppCoreMain::instanceGet()->settingsGet();
mobileDesignResolutionApply(renderView, appSett);
}
else if (!renderView)<--- else if condition is opposite to first condition
{
const fsCAppSettings* appSett = fsCAppCoreMain::instanceGet()->settingsGet();
#if defined(AX_PLATFORM_PC)
#ifdef AXMOL_V3
renderView = axmol::RenderView::createWithFullscreen(installedName.utf8Get());
#else
renderView = axmol::RenderViewImpl::createWithFullScreen(installedName.utf8Get());
#endif
const GLFWvidmode* currentVideoMode = glfwGetVideoMode(glfwGetPrimaryMonitor());
screenModeAdd(currentVideoMode->width, currentVideoMode->height, 32);
if (!pFullScreen)
{
#ifdef AXMOL_V3
auto renderViewDesktopView = static_cast<ax::RenderView*>(renderView);
#else
auto renderViewDesktopView = static_cast<ax::RenderViewImpl*>(renderView);
#endif
auto glWindow = renderViewDesktopView->getWindow();
renderViewDesktopView->setWindowed(pDisplayMode->widthGet(), pDisplayMode->heightGet());
glfwSetWindowPos(glWindow, (currentVideoMode->width - pDisplayMode->widthGet()) / 2,
(currentVideoMode->height - pDisplayMode->heightGet()) / 2);
designResolutionApply(renderView, pDisplayMode->widthGet(), pDisplayMode->heightGet());
renderView->setFrameSize(pDisplayMode->widthGet(), pDisplayMode->heightGet());
}
else
{
designResolutionApply(renderView, currentVideoMode->width, currentVideoMode->height);
}
#elif (AX_TARGET_PLATFORM == AX_PLATFORM_WASM)
#ifdef AXMOL_V3
renderView = axmol::RenderView::createWithRect(installedName.utf8Get(),
axmol::Rect(0, 0, appSett->s32Get("appCanvasW"), appSett->s32Get("appCanvasH")), 1.0f, false);
#else
renderView = axmol::RenderViewImpl::createWithRect(installedName.utf8Get(),
axmol::Rect(0, 0, appSett->s32Get("appCanvasW"), appSett->s32Get("appCanvasH")), 1.0f, false);
#endif
#else
#ifdef AXMOL_V3
renderView = axmol::RenderView::create(installedName.utf8Get());
#else
renderView = axmol::RenderViewImpl::create(installedName.utf8Get());
#endif
#endif
director->setRenderView(renderView);
mobileDesignResolutionApply(renderView, appSett);
buffersSwap();
if (!pFullScreen)
{
clipWindowSet(pDisplayMode);
}
}
#if !defined fsRELEASE
// director->setDisplayStats(true);
#endif
director->setAnimationInterval(1.0f / 60);
director->setContentScaleFactor(1.0f);
appImplSceneCreate();
focusStatusHandle();
#if defined fsDesktop
fsCAppCoreMain::instanceGet()->hardwareGet()->appHideEnable();
#endif
}
void fsCDisplay::focusStatusHandle()
{
#if defined AX_PLATFORM_PC
auto director = axmol::Director::getInstance();
director->getEventDispatcher()->addCustomEventListener(
#ifdef AXMOL_V3
axmol::RenderView::EVENT_WINDOW_UNFOCUSED,
[](axmol::CustomEvent* e)
#else
axmol::RenderViewImpl::EVENT_WINDOW_UNFOCUSED,
[](axmol::EventCustom* e)
#endif
{
auto director = axmol::Director::getInstance();
director->stopAnimation();
fsIAudioManager::instanceGet()->pause();
// fsCAppCoreMain::instanceGet()->hardwareGet()->appMinimize();
});
director->getEventDispatcher()->addCustomEventListener(
#ifdef AXMOL_V3
axmol::RenderView::EVENT_WINDOW_FOCUSED,
[](axmol::CustomEvent* e)
#else
axmol::RenderViewImpl::EVENT_WINDOW_FOCUSED,
[](axmol::EventCustom* e)
#endif
{
auto director = axmol::Director::getInstance();
director->startAnimation();
fsIAudioManager::instanceGet()->resume();
// fsCAppCoreMain::instanceGet()->hardwareGet()->appMaximize();
});
// auto renderView = static_cast<axmol::RenderVewImpl*>(axmol::Director::getInstance()->getOpenRenderView());
// glfwSetWindowAttrib(renderView->getWindow(), GLFW_AUTO_ICONIFY, GLFW_TRUE);
// glfwSetWindowFocusCallback(renderView->getWindow(), &focus);
#endif
}
void fsCDisplay::focus(GLFWwindow* pWindow, int pGotFocus)
{
#if defined AX_PLATFORM_PC
auto director = axmol::Director::getInstance();
if (pGotFocus == GLFW_TRUE)
{
director->startAnimation();
fsIAudioManager::instanceGet()->resume();
// fsCAppCoreMain::instanceGet()->hardwareGet()->appMaximize();
}
else
{
director->stopAnimation();
fsIAudioManager::instanceGet()->pause();
// fsCAppCoreMain::instanceGet()->hardwareGet()->appMinimize();
}
#endif
}
void fsCDisplay::appImplSceneCreate()
{
mImpl = axmol::Scene::create();
mImpl->retain();
auto appScene = fsCAppLayer::create();
appScene->shutdownCallbackRegister();
appScene->scheduleUpdate();
mImpl->addChild(appScene);
axmol::Director::getInstance()->runWithScene(mImpl);
}
fsCDisplay::fsCDisplay():
mImpl(nullptr)
{
#if defined(AX_PLATFORM_PC)
#else
renderViewCreate(nullptr, false);
#endif
}
fsCDisplay::~fsCDisplay()
{
mImpl->release();
}
void fsCDisplay::fsCAppLayer::draw(axmol::Renderer* renderer, const axmol::Mat4& transform, uint32_t flags)
{
fsCAppCoreMain::instanceGet()->render();
// Layer::draw(renderer, transform, flags);
}
void fsCDisplay::fsCAppLayer::update(float delta)
{
if (fsCAppCoreMain::instanceGet()->update(static_cast<fsS32>(delta * 1000.0f)))
{
shutdown();
Layer::update(delta);
}
}
void fsCDisplay::fsCAppLayer::shutdownCallbackRegister()
{
auto disp = axmol::Director::getInstance()->getEventDispatcher();
disp->addCustomEventListener(
axmol::Director::EVENT_RESET,
#ifdef AXMOL_V3
[this](axmol::CustomEvent* e)
#else
[this](axmol::EventCustom* e)
#endif
{
fsCAppCoreMain::destroy();
});
}
// Update CCRef and uncommented swap in alternative shutdownCallbackRegister()
// The magic 219 was empirically found - engine hasn't completely shutdown so
// has some stuff lying around
//
// #define AX_REF_LEAK_DETECTION 1
// int Ref::leakCount()
// {
// return __refAllocationList.size();
// }
// #include <base/Ref.h>
// void fsCDisplay::fsCAppLayer::shutdownCallbackRegister()
// {
// auto disp = axmol::Director::getInstance()->getEventDispatcher();
// disp->addCustomEventListener(
// axmol::Director::EVENT_RESET,
// [this](axmol::EventCustom* e)
// {
// fsCAppCoreMain::destroy();
// if (axmol::Ref::leakCount() > 219)
// {
// axmol::Ref::printLeaks();
// }
// });
// }
void fsCDisplay::fsCAppLayer::shutdown()
{
// End cocos2d Director which purges next frame and triggers registered callback above
axmol::Director::getInstance()->end();
// To navigate back to native iOS screen(if present) without quitting the application, do not use Director::getInstance()->end() and exit(0) as given above,
// instead trigger a custom event created in RootViewController.mm as below*/
// EventCustom customEndEvent("game_scene_close_event");
// _eventDispatcher->dispatchEvent(&customEndEvent);
}
|