Skip to content

Commit

Permalink
Revert "OpenGLのテクスチャ読み込み処理を修正"
Browse files Browse the repository at this point in the history
  • Loading branch information
MihailJP committed Jun 19, 2024
1 parent beb4e01 commit 6cdca0e
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 84 deletions.
8 changes: 4 additions & 4 deletions common/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

#define MIHAJONG_MAJOR_VER 2
#define MIHAJONG_MINOR_VER 4
#define MIHAJONG_PATCH_VER 4
#define MIHAJONG_VER "2.4.4"
#define MIHAJONG_PATCH_VER 3
#define MIHAJONG_VER "2.4.3"

#define MJCORE_MAJOR_VER 2
#define MJCORE_MINOR_VER 4
Expand Down Expand Up @@ -32,8 +32,8 @@

#define GRAPHIC_MAJOR_VER 2
#define GRAPHIC_MINOR_VER 4
#define GRAPHIC_PATCH_VER 4
#define GRAPHIC_VER "2.4.4"
#define GRAPHIC_PATCH_VER 2
#define GRAPHIC_VER "2.4.2"

#define CONFIG_MAJOR_VER 2
#define CONFIG_MINOR_VER 4
Expand Down
101 changes: 33 additions & 68 deletions graphic/loadtex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
namespace mihajong_graphic {

//namespace {
std::map<intptr_t, std::map<intptr_t, TexturePtr> > Textures;
std::map<intptr_t, TexturePtr> Textures;
#if !defined(_WIN32) || !defined(WITH_DIRECTX)
std::map<TexturePtr, unsigned> TextureWidth;
std::map<TexturePtr, unsigned> TextureHeight;
Expand All @@ -29,11 +29,11 @@ void LoadTexture(DevicePtr device, TexturePtr* texture, LPCTSTR resource) {
#endif
auto resourceID = reinterpret_cast<intptr_t>(resource);
assert((resourceID & 0xffff0000) == 0); // 上位ワードが0なら文字列ではなくリソース番号とみなされる(Win32APIの仕様)
if (Textures[reinterpret_cast<intptr_t>(device)].find(resourceID) != Textures[reinterpret_cast<intptr_t>(device)].end()) { // 既にロード済みのテクスチャ
if (Textures.find(resourceID) != Textures.end()) { // 既にロード済みのテクスチャ
#if defined(_WIN32) && defined(WITH_DIRECTX)
Textures[resourceID]->AddRef();
#endif
*texture = Textures[reinterpret_cast<intptr_t>(device)][resourceID];
*texture = Textures[resourceID];
return;
} else { // ロードされていない場合
#ifdef _WIN32
Expand All @@ -46,16 +46,16 @@ void LoadTexture(DevicePtr device, TexturePtr* texture, LPCTSTR resource) {
DWORD pngSize = SizeofResource(GraphicDLL, Resource);
void* pngData = LockResource(ResourceMem);
#if defined(_WIN32) && defined(WITH_DIRECTX)
Textures[reinterpret_cast<intptr_t>(device)][resourceID] = nullptr;
Textures[resourceID] = nullptr;
HRESULT result =
D3DXCreateTextureFromFileInMemoryEx(device, pngData, pngSize, D3DX_DEFAULT, D3DX_DEFAULT, 0, 0,
D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_FILTER_TRIANGLE | D3DX_FILTER_DITHER, D3DX_DEFAULT,
0x00000000, nullptr, nullptr, &(Textures[reinterpret_cast<intptr_t>(device)][resourceID]));
0x00000000, nullptr, nullptr, &(Textures[resourceID]));
UnlockResource(ResourceMem);
switch (result) {
case D3D_OK:
Textures[reinterpret_cast<intptr_t>(device)][resourceID]->AddRef();
*texture = Textures[reinterpret_cast<intptr_t>(device)][resourceID];
Textures[resourceID]->AddRef();
*texture = Textures[resourceID];
return; // Congratulations, your texture has been loaded.
case D3DERR_NOTAVAILABLE:
throw TextureCreationError("", resourceID);
Expand All @@ -71,12 +71,12 @@ void LoadTexture(DevicePtr device, TexturePtr* texture, LPCTSTR resource) {
throw TextureCreationError("原因不明のエラーです。", resourceID);
}
#else
Textures[reinterpret_cast<intptr_t>(device)][resourceID] = 0;
Textures[resourceID] = 0;
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &Textures[reinterpret_cast<intptr_t>(device)][resourceID]);
TextureWidth[Textures[reinterpret_cast<intptr_t>(device)][resourceID]] =
TextureHeight[Textures[reinterpret_cast<intptr_t>(device)][resourceID]] = 0;
glBindTexture(GL_TEXTURE_2D, Textures[reinterpret_cast<intptr_t>(device)][resourceID]);
glGenTextures(1, &Textures[resourceID]);
TextureWidth[Textures[resourceID]] =
TextureHeight[Textures[resourceID]] = 0;
glBindTexture(GL_TEXTURE_2D, Textures[resourceID]);
HGLOBAL resBuf = GlobalAlloc(GMEM_MOVEABLE, pngSize);
void* pResBuf = GlobalLock(resBuf);
Bitmap* bitmap = nullptr;
Expand All @@ -95,8 +95,8 @@ void LoadTexture(DevicePtr device, TexturePtr* texture, LPCTSTR resource) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, data.Width, data.Height, 0,
GL_BGRA_EXT, GL_UNSIGNED_BYTE, data.Scan0);
bitmap->UnlockBits(&data);
TextureWidth[Textures[reinterpret_cast<intptr_t>(device)][resourceID]] = data.Width;
TextureHeight[Textures[reinterpret_cast<intptr_t>(device)][resourceID]] = data.Height;
TextureWidth[Textures[resourceID]] = data.Width;
TextureHeight[Textures[resourceID]] = data.Height;
}
}
glBindTexture(GL_TEXTURE_2D, 0);
Expand All @@ -105,17 +105,17 @@ void LoadTexture(DevicePtr device, TexturePtr* texture, LPCTSTR resource) {
UnlockResource(ResourceMem);
if (stream) stream->Release();
delete bitmap;
*texture = Textures[reinterpret_cast<intptr_t>(device)][resourceID];
*texture = Textures[resourceID];
return;
#endif
#else /* _WIN32 */
/* テクスチャの仮初期化 */
Textures[reinterpret_cast<intptr_t>(device)][resourceID] = 0;
Textures[resourceID] = 0;
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &Textures[reinterpret_cast<intptr_t>(device)][resourceID]);
TextureWidth[Textures[reinterpret_cast<intptr_t>(device)][resourceID]] =
TextureHeight[Textures[reinterpret_cast<intptr_t>(device)][resourceID]] = 0;
glBindTexture(GL_TEXTURE_2D, Textures[reinterpret_cast<intptr_t>(device)][resourceID]);
glGenTextures(1, &Textures[resourceID]);
TextureWidth[Textures[resourceID]] =
TextureHeight[Textures[resourceID]] = 0;
glBindTexture(GL_TEXTURE_2D, Textures[resourceID]);
/* ファイルをオープン */
const std::string fileName = dataFileName(resourceID);
FILE* pngFile = fopen(fileName.c_str(), "rb");
Expand Down Expand Up @@ -179,14 +179,14 @@ void LoadTexture(DevicePtr device, TexturePtr* texture, LPCTSTR resource) {
throw TextureCreationError(fileName + std::string(" にアルファチャンネルがありません"));
}
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, pngWidth, pngHeight, 0,
GL_RGBA, GL_UNSIGNED_BYTE, imageDat);
TextureWidth[Textures[reinterpret_cast<intptr_t>(device)][resourceID]] = pngWidth;
TextureHeight[Textures[reinterpret_cast<intptr_t>(device)][resourceID]] = pngHeight;
TextureWidth[Textures[resourceID]] = pngWidth;
TextureHeight[Textures[resourceID]] = pngHeight;
/* 解放 */
delete[] imageDat;
png_destroy_read_struct(&pngPtr, &infoPtr, nullptr);
Expand All @@ -196,54 +196,19 @@ void LoadTexture(DevicePtr device, TexturePtr* texture, LPCTSTR resource) {
}
}

void PreloadTextures(DevicePtr device) { // テクスチャの先行読み込み
constexpr intptr_t textureList[] = {
IDB_PNG_TBLBAIZE,
IDB_PNG_TBLBORDER,
IDB_PNG_SDBAR,
IDB_PNG_TILE,
IDB_PNG_FONT,
IDB_PNG_TITLE,
IDB_PNG_BUTTON,
IDB_PNG_FONT_HUGE,
IDB_PNG_DICE,
IDB_PNG_FONT_SMALL,
IDB_PNG_TENBOU,
IDB_PNG_CHICHAMARK,
IDB_PNG_SCORE_INDICATOR,
IDB_PNG_CALL_TEXT,
IDB_PNG_CALL_DIGITS,
IDB_PNG_AGARI_WINDOW,
IDB_PNG_SCORE_DIGITS,
IDB_PNG_CHECKBOX,
IDB_PNG_TILE_BLACK,
IDB_PNG_MOON_CLOCK,
IDB_PNG_SPLASH_SCREEN,
IDB_PNG_TITLE_BACKGROUND,
0, // sentinel
};
TexturePtr dummyTexture;
for (const intptr_t* i = textureList; *i != 0; ++i)
LoadTexture(device, &dummyTexture, MAKEINTRESOURCE(*i));
}

void UnloadAllTextures() {
#if defined(_WIN32) && defined(WITH_DIRECTX)
for (const auto& d : Textures) {
for (const auto& k : d.second) {
ULONG refs = k.second->Release();
CodeConv::tostringstream o;
o << _T("UnloadAllTextures(): Texture resource #") << k.first << _T(": remaining number of refs is ") << refs << std::endl;
OutputDebugString(o.str().c_str());
}
for (const auto& k : Textures) {
ULONG refs = k.second->Release();
CodeConv::tostringstream o;
o << _T("UnloadAllTextures(): Texture resource #") << k.first << _T(": remaining number of refs is ") << refs << std::endl;
OutputDebugString(o.str().c_str());
}
#else
for (const auto& d : Textures) {
for (const auto& k : d.second) {
const GLuint tx[1] = {k.second};
glDeleteTextures(1, tx);
}
}
for (const auto& k : Textures) {
const GLuint tx[1] = {k.second};
glDeleteTextures(1, tx);
}
#endif
Textures.clear();
#if !defined(_WIN32) || !defined(WITH_DIRECTX)
Expand Down
1 change: 0 additions & 1 deletion graphic/loadtex.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

namespace mihajong_graphic {
void LoadTexture(DevicePtr device, TexturePtr* texture, LPCTSTR resource); // テクスチャ読み込み
void PreloadTextures(DevicePtr device); // テクスチャの先行読み込み
void UnloadAllTextures(); // テクスチャの解放(アプリケーション終了時にだけ呼ぶこと!)
#if !defined(_WIN32) || !defined(WITH_DIRECTX)
int getTextureWidth(DevicePtr device, TexturePtr texture);
Expand Down
36 changes: 29 additions & 7 deletions graphic/scrmanip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,34 @@ void ScreenManipulator::InitDevice(bool fullscreen) { // Direct3D オブジェ
}

void ScreenManipulator::preloadTextures() { // テクスチャの先行読み込み
PreloadTextures(pDevice);
constexpr intptr_t textureList[] = {
IDB_PNG_TBLBAIZE,
IDB_PNG_TBLBORDER,
IDB_PNG_SDBAR,
IDB_PNG_TILE,
IDB_PNG_FONT,
IDB_PNG_TITLE,
IDB_PNG_BUTTON,
IDB_PNG_FONT_HUGE,
IDB_PNG_DICE,
IDB_PNG_FONT_SMALL,
IDB_PNG_TENBOU,
IDB_PNG_CHICHAMARK,
IDB_PNG_SCORE_INDICATOR,
IDB_PNG_CALL_TEXT,
IDB_PNG_CALL_DIGITS,
IDB_PNG_AGARI_WINDOW,
IDB_PNG_SCORE_DIGITS,
IDB_PNG_CHECKBOX,
IDB_PNG_TILE_BLACK,
IDB_PNG_MOON_CLOCK,
IDB_PNG_SPLASH_SCREEN,
IDB_PNG_TITLE_BACKGROUND,
0, // sentinel
};
TexturePtr dummyTexture;
for (const intptr_t* i = textureList; *i != 0; ++i)
LoadTexture(pDevice, &dummyTexture, MAKEINTRESOURCE(*i));
}

void ScreenManipulator::disposeTextures() {
Expand All @@ -130,11 +157,7 @@ ScreenManipulator::ScreenManipulator(HWND windowHandle, bool fullscreen) {
InitDevice(fullscreen);
SplashScreen::LoadTexture(pDevice);
myScene = new SplashScreen(this);
#ifdef WITH_DIRECTX
myFPSIndicator = new FPSIndicator(this);
#else /* WITH_DIRECTX */
myFPSIndicator = nullptr;
#endif /* WITH_DIRECTX */
lastRedrawTime = 0;
redrawFlag = true;
}
Expand All @@ -146,8 +169,7 @@ ScreenManipulator::ScreenManipulator(Display* displayPtr, Window windowHandle, b
pDevice = nullptr; disp = displayPtr; hWnd = windowHandle;
InitDevice(fullscreen);
myScene = new SplashScreen(this);
//myFPSIndicator = new FPSIndicator(this);
myFPSIndicator = nullptr;
myFPSIndicator = new FPSIndicator(this);
lastRedrawTime = 0;
redrawFlag = true;
}
Expand Down
3 changes: 0 additions & 3 deletions graphic/sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ SpriteRenderer* SpriteRenderer::instantiate(DevicePtr device) {
return renderer[reinterpret_cast<intptr_t>(device)];
} else { // デバイスに対応するスプライトは初回の使用(初期化が必要)
renderer[reinterpret_cast<intptr_t>(device)] = new SpriteRenderer(device);
#ifndef WITH_DIRECTX
PreloadTextures(device);
#endif /* WITH_DIRECTX */
return renderer[reinterpret_cast<intptr_t>(device)];
}
}
Expand Down
1 change: 0 additions & 1 deletion mjcore/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ void GameThread::ThreadMain(GameThread* lpParam) {
#endif /*_WIN32*/
#ifndef WITH_DIRECTX
mihajong_graphic::WaitForWindowInit();
mihajong_graphic::Transit(mihajong_graphic::SceneID::splash);
#endif /*WITH_DIRECTX*/
initapp(gameType, hwnd);
startgame(gameType);
Expand Down

0 comments on commit 6cdca0e

Please sign in to comment.