项目架构重构,代码整理
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
#include "stdafx.h"
|
||||
#include "TetrisRenderInternal.h"
|
||||
#include "TetrisAssets.h"
|
||||
#include <objidl.h>
|
||||
#include <string>
|
||||
|
||||
#pragma comment(lib, "gdiplus.lib")
|
||||
|
||||
using namespace Gdiplus;
|
||||
|
||||
/**
|
||||
* @brief 尝试从指定路径加载 GDI+ 位图。
|
||||
*/
|
||||
static Bitmap* TryLoadBitmap(const std::wstring& path)
|
||||
{
|
||||
if (path.empty() || !FileExists(path))
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Bitmap* loadedImage = Bitmap::FromFile(path.c_str(), FALSE);
|
||||
if (loadedImage != nullptr && loadedImage->GetLastStatus() == Ok)
|
||||
{
|
||||
return loadedImage;
|
||||
}
|
||||
|
||||
delete loadedImage;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 确保 GDI+ 已初始化,返回初始化是否成功。
|
||||
*/
|
||||
static bool EnsureGdiplusStarted()
|
||||
{
|
||||
static ULONG_PTR gdiplusToken = 0;
|
||||
static bool attempted = false;
|
||||
static bool started = false;
|
||||
|
||||
if (!attempted)
|
||||
{
|
||||
attempted = true;
|
||||
GdiplusStartupInput startupInput;
|
||||
started = GdiplusStartup(&gdiplusToken, &startupInput, nullptr) == Ok;
|
||||
}
|
||||
|
||||
return started;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 加载并缓存主背景图片。
|
||||
*/
|
||||
Bitmap* LoadBackgroundImage()
|
||||
{
|
||||
static Bitmap* backgroundImage = nullptr;
|
||||
static bool attempted = false;
|
||||
|
||||
if (!attempted)
|
||||
{
|
||||
attempted = true;
|
||||
|
||||
if (EnsureGdiplusStarted())
|
||||
{
|
||||
const std::wstring candidates[] =
|
||||
{
|
||||
BuildAssetPath(L"assets\\images\\background.png"),
|
||||
BuildWorkingDirAssetPath(L"assets\\images\\background.png"),
|
||||
BuildAssetPath(L"assets\\images\\background.bmp"),
|
||||
BuildWorkingDirAssetPath(L"assets\\images\\background.bmp")
|
||||
};
|
||||
|
||||
for (const std::wstring& candidate : candidates)
|
||||
{
|
||||
backgroundImage = TryLoadBitmap(candidate);
|
||||
if (backgroundImage != nullptr)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return backgroundImage;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 按序号加载并缓存致谢页图片。
|
||||
*/
|
||||
Bitmap* LoadCreditImage(int index)
|
||||
{
|
||||
constexpr int creditPageCount = 4;
|
||||
static Bitmap* creditImages[creditPageCount] = {};
|
||||
static bool attempted[creditPageCount] = {};
|
||||
|
||||
if (index < 0 || index >= creditPageCount)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (!attempted[index])
|
||||
{
|
||||
attempted[index] = true;
|
||||
|
||||
if (EnsureGdiplusStarted())
|
||||
{
|
||||
const wchar_t* imageNames[creditPageCount] =
|
||||
{
|
||||
L"assets\\images\\qls.jpg",
|
||||
L"assets\\images\\wyk.jpg",
|
||||
L"assets\\images\\swj.jpg",
|
||||
L"assets\\images\\qhy.jpg"
|
||||
};
|
||||
const std::wstring creditExtraCandidates[] =
|
||||
{
|
||||
BuildAssetPath(imageNames[index]),
|
||||
BuildWorkingDirAssetPath(imageNames[index]),
|
||||
BuildAssetPath(L"assets\\images\\qhy.png"),
|
||||
BuildWorkingDirAssetPath(L"assets\\images\\qhy.png"),
|
||||
BuildAssetPath(L"assets\\images\\qhy.jpeg"),
|
||||
BuildWorkingDirAssetPath(L"assets\\images\\qhy.jpeg"),
|
||||
BuildAssetPath(L"assets\\images\\qhy.bmp"),
|
||||
BuildWorkingDirAssetPath(L"assets\\images\\qhy.bmp")
|
||||
};
|
||||
int candidateCount = (index == 3) ? 8 : 2;
|
||||
|
||||
for (int i = 0; i < candidateCount; i++)
|
||||
{
|
||||
creditImages[index] = TryLoadBitmap(creditExtraCandidates[i]);
|
||||
if (creditImages[index] != nullptr)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return creditImages[index];
|
||||
}
|
||||
Reference in New Issue
Block a user