Files
Yosuga/3rdparty/Live2D/Src/LAppLive2D/Src/LAppPal.cpp
T
Misaki f7be85fbf6 1. 完全解耦了Live2D的LApp部分,使其可以接入任何GUI库
2. 初步尝试合并嵌入式Linux平台支持的相关代码,预计在下一次提交完整支持。
2026-06-24 01:05:26 +08:00

155 lines
3.6 KiB
C++

/**
* Copyright(c) Live2D Inc. All rights reserved.
*
* Use of this source code is governed by the Live2D Open Software license
* that can be found at https://www.live2d.com/eula/live2d-open-software-license-agreement_en.html.
*/
#include "LAppPal.hpp"
#include "LAppDefine.hpp"
#include <chrono>
#include <cstdio>
#include <stdarg.h>
#include <sys/stat.h>
#include <iostream>
#include <fstream>
#include <Model/CubismMoc.hpp>
#include "LAppDefine.hpp"
#ifdef _WIN32
# include <io.h>
# define VSNPRINTF(buf, sz, fmt, args) \
vsnprintf_s(buf, sz, _TRUNCATE, fmt, args)
#else
# include <cstdio> // vsnprintf
# define VSNPRINTF(buf, sz, fmt, args) \
vsnprintf(buf, sz, fmt, args)
#endif
using std::endl;
using namespace Csm;
using namespace std;
using namespace LAppDefine;
double LAppPal::s_currentFrame = 0.0;
double LAppPal::s_lastFrame = 0.0;
double LAppPal::s_deltaTime = 0.0;
csmByte* LAppPal::LoadFileAsBytes(const string filePath, csmSizeInt* outSize)
{
//filePath;//
const char* path = filePath.c_str();
int size = 0;
struct stat statBuf;
if (stat(path, &statBuf) == 0)
{
size = statBuf.st_size;
if (size == 0)
{
if (DebugLogEnable)
{
PrintLogLn("Stat succeeded but file size is zero. path:%s", path);
}
return NULL;
}
}
else
{
if (DebugLogEnable)
{
PrintLogLn("Stat failed. errno:%d path:%s", errno, path);
}
return NULL;
}
std::fstream file;
file.open(path, std::ios::in | std::ios::binary);
if (!file.is_open())
{
if (DebugLogEnable)
{
PrintLogLn("File open failed. path:%s", path);
}
return NULL;
}
char* buf = new char[size];
file.read(buf, size);
file.close();
*outSize = size;
return reinterpret_cast<csmByte*>(buf);
}
void LAppPal::ReleaseBytes(csmByte* byteData)
{
delete[] byteData;
}
csmFloat32 LAppPal::GetDeltaTime()
{
return static_cast<csmFloat32>(s_deltaTime);
}
static std::chrono::steady_clock::time_point s_lastTime;
static double s_deltaTime = 0.0;
void LAppPal::UpdateTime()
{
// s_currentFrame = glfwGetTime();
// s_deltaTime = s_currentFrame - s_lastFrame;
// s_lastFrame = s_currentFrame;
static bool initialized = false;
if (!initialized)
{
s_lastTime = std::chrono::steady_clock::now();
initialized = true;
return;
}
auto now = std::chrono::steady_clock::now();
std::chrono::duration<double> diff = now - s_lastTime;
s_deltaTime = diff.count();
s_lastTime = now;
}
void LAppPal::PrintLog(const csmChar* format, ...)
{
va_list args;
csmChar buf[256];
va_start(args, format);
VSNPRINTF(buf, sizeof(buf), format, args); // 標準出力でレンダリング
#ifdef CSM_DEBUG_MEMORY_LEAKING
// メモリリークチェック時は大量の標準出力がはしり重いのでprintfを利用する
std::printf(buf);
#else
std::cout << buf;
#endif
va_end(args);
}
void LAppPal::PrintLogLn(const Csm::csmChar* format, ...)
{
va_list args;
csmChar buf[256];
va_start(args, format);
VSNPRINTF(buf, sizeof(buf), format, args); // 標準出力でレンダリング
#ifdef CSM_DEBUG_MEMORY_LEAKING
// メモリリークチェック時は大量の標準出力がはしり重いのでprintfを利用する
std::printf("%s\n", buf);
#else
std::cout << buf << std::endl;
#endif
va_end(args);
}
void LAppPal::PrintMessage(const csmChar* message)
{
PrintLog("%s", message);
}
void LAppPal::PrintMessageLn(const csmChar* message)
{
PrintLogLn("%s", message);
}