项目架构重构,代码整理

This commit is contained in:
2026-04-28 22:44:31 +08:00
parent 2f435f5ca6
commit 0840a807b5
18 changed files with 2332 additions and 1893 deletions
+3 -165
View File
@@ -1,8 +1,8 @@
#include "stdafx.h"
#include "stdafx.h"
#include "Tetris.h"
#include "TetrisRenderInternal.h"
#include <objidl.h>
#include <gdiplus.h>
#include <string>
#pragma comment(lib, "gdiplus.lib")
@@ -34,169 +34,6 @@ static HBRUSH GetCachedParticleBrush(COLORREF color)
return CreateSolidBrush(color);
}
static std::wstring BuildAssetPath(const wchar_t* relativePath)
{
wchar_t modulePath[MAX_PATH] = {};
GetModuleFileNameW(nullptr, modulePath, MAX_PATH);
std::wstring basePath(modulePath);
size_t lastSlash = basePath.find_last_of(L"\\/");
if (lastSlash != std::wstring::npos)
{
basePath.resize(lastSlash);
}
std::wstring projectRelative = basePath + L"\\..\\..\\" + relativePath;
wchar_t fullPath[MAX_PATH] = {};
DWORD result = GetFullPathNameW(projectRelative.c_str(), MAX_PATH, fullPath, nullptr);
if (result > 0 && result < MAX_PATH)
{
return fullPath;
}
return projectRelative;
}
static std::wstring BuildWorkingDirAssetPath(const wchar_t* relativePath)
{
wchar_t currentDirectory[MAX_PATH] = {};
DWORD length = GetCurrentDirectoryW(MAX_PATH, currentDirectory);
if (length == 0 || length >= MAX_PATH)
{
return L"";
}
std::wstring candidate = std::wstring(currentDirectory) + L"\\" + relativePath;
wchar_t fullPath[MAX_PATH] = {};
DWORD result = GetFullPathNameW(candidate.c_str(), MAX_PATH, fullPath, nullptr);
if (result > 0 && result < MAX_PATH)
{
return fullPath;
}
return candidate;
}
static Bitmap* LoadBackgroundImage()
{
static ULONG_PTR gdiplusToken = 0;
static Bitmap* backgroundImage = nullptr;
static bool attempted = false;
if (!attempted)
{
attempted = true;
GdiplusStartupInput startupInput;
if (GdiplusStartup(&gdiplusToken, &startupInput, nullptr) == Ok)
{
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)
{
if (candidate.empty())
{
continue;
}
DWORD attributes = GetFileAttributesW(candidate.c_str());
if (attributes == INVALID_FILE_ATTRIBUTES || (attributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
{
continue;
}
Bitmap* loadedImage = Bitmap::FromFile(candidate.c_str(), FALSE);
if (loadedImage != nullptr && loadedImage->GetLastStatus() == Ok)
{
backgroundImage = loadedImage;
break;
}
delete loadedImage;
}
}
}
return backgroundImage;
}
/**
* @brief 按序号加载致谢页图片资源。
*/
static Bitmap* LoadCreditImage(int index)
{
constexpr int creditPageCount = 4;
static ULONG_PTR gdiplusToken = 0;
static Bitmap* creditImages[creditPageCount] = {};
static bool attempted[creditPageCount] = {};
if (index < 0 || index >= creditPageCount)
{
return nullptr;
}
if (!attempted[index])
{
attempted[index] = true;
GdiplusStartupInput startupInput;
if (GdiplusStartup(&gdiplusToken, &startupInput, nullptr) == Ok)
{
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++)
{
const std::wstring& candidate = creditExtraCandidates[i];
if (candidate.empty())
{
continue;
}
DWORD attributes = GetFileAttributesW(candidate.c_str());
if (attributes == INVALID_FILE_ATTRIBUTES || (attributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
{
continue;
}
Bitmap* loadedImage = Bitmap::FromFile(candidate.c_str(), FALSE);
if (loadedImage != nullptr && loadedImage->GetLastStatus() == Ok)
{
creditImages[index] = loadedImage;
break;
}
delete loadedImage;
}
}
}
return creditImages[index];
}
void TDrawScreen(HDC hdc, HWND hWnd)
{
RECT clientRect;
@@ -2855,3 +2692,4 @@ void TDrawScreen(HDC hdc, HWND hWnd)
DeleteObject(bodyFont);
DeleteObject(smallFont);
}