-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathL2DFramework.cpp
86 lines (77 loc) · 2.31 KB
/
L2DFramework.cpp
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
#include "L2DFramework.h"
#include "LAppDefine.hpp"
#include "LAppPal.hpp"
#include "LAppAllocator.hpp"
#include "CubismRenderer_CC.h"
#include "L2DModel.h"
#include "cocos2d.h"
#if CSM_RENDERER_EXT
#include "renderer/backend/Device.h"
#endif
using namespace l2d;
using namespace cocos2d;
using namespace Csm;
using namespace Live2D::Cubism::Core;
static bool L2DFrameworkInited = false;
static bool L2DFrameworkInitTried = false;
static EventListenerCustom* L2DRecreatedListener = nullptr;
static LAppAllocator L2DAllocator;
static CubismFramework::Option L2DOption;
bool Framework::lazyInit()
{
if (L2DFrameworkInited) return true;
if (L2DFrameworkInitTried)return false;
return _init();
}
bool Framework::_init()
{
L2DFrameworkInitTried = true;
// prepare for Cubism Framework API.
L2DOption.LogFunction = LAppPal::PrintMessage;
L2DOption.LoggingLevel = LAppDefine::CubismLoggingLevel;
if (!CubismFramework::StartUp(&L2DAllocator, &L2DOption))
{
cocos2d::log("[L2D] can't start framework");
return false;
}
CubismFramework::Initialize();
#if CSM_RENDERER_EXT
auto exts = (char*)backend::Device::getInstance()->getDeviceInfo()->getExtension();
if (strstr(exts, "GL_NV_shader_framebuffer_fetch "))
Rendering::CubismRenderer_CC::SetExtShaderMode(true, true);
#endif
L2DRecreatedListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, [](EventCustom*)
{
Rendering::CubismRenderer::StaticRelease();
});
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(
L2DRecreatedListener, -1);
L2DFrameworkInited = true;
return true;
}
bool Framework::end()
{
if (!L2DFrameworkInited) return true;
Director::getInstance()->getEventDispatcher()->removeEventListener(L2DRecreatedListener);
L2DRecreatedListener = nullptr;
// make a copy to avoid erase in iter
const auto instances = Model::instances;
for (auto& m : instances)
m->removeFromParentAndCleanup(true);
CC_ASSERT(Model::instances.empty());
Model::instances.clear();
// note: all models should be destructed before this
CubismFramework::Dispose();
cocos2d::log("[L2D] framework end successfully");
L2DFrameworkInited = false;
L2DFrameworkInitTried = false;
return true;
}
unsigned int Framework::getVersion()
{
return csmGetVersion();
}
unsigned int Framework::getLatestMocVersion()
{
return csmGetLatestMocVersion();
}