-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTexture.cpp
More file actions
231 lines (187 loc) · 6.68 KB
/
Texture.cpp
File metadata and controls
231 lines (187 loc) · 6.68 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
Copyright (c) 2024-2025 Toksisitee. All rights reserved.
This work is licensed under the terms of the MIT license.
For a copy, refer to license.md or https://opensource.org/licenses/MIT
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
*/
#pragma once
#include <string>
#include <map>
#include <memory>
#include <D3dx9tex.h>
#include <DxErr.h>
#include "spdlog\spdlog.h"
#include "Utils.h"
#include "Texture.h"
CTextureManager g_TextureManager;
CTexture2D::CTexture2D( LPDIRECT3DDEVICE9 pd3dDevice, const std::string& sDirectory, std::string sTexName, int nWidth, int nHeight ) :
m_pd3dDevice( pd3dDevice ), m_nWidth( nWidth ), m_nHeight( nHeight )
{
HRESULT hr;
std::string sPath = Util::FileSystem::GetAssetsDirectory() + sDirectory + "\\" + sTexName;
std::wstring wStr = std::wstring( sPath.begin(), sPath.end() );
if ( FAILED( (hr = D3DXCreateTextureFromFileEx( pd3dDevice, wStr.c_str(), nWidth, nHeight, D3DX_DEFAULT, 0,
D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, &m_pD3DTexture )) ) ) {
spdlog::error( "Failed to create Texture2D\nName: {}\nWidth: {}, Height: {}\nError: {} :: {}",
sPath.c_str(), nWidth, nHeight, DXGetErrorStringA( hr ), DXGetErrorDescriptionA( hr ) );
return;
}
}
CTexture2D::CTexture2D( LPDIRECT3DDEVICE9 pd3dDevice, const std::string& sDirectory, std::string sTexName ) :
m_pd3dDevice( pd3dDevice ), m_nWidth( 0 ), m_nHeight( 0 )
{
HRESULT hr;
std::string sPath = Util::FileSystem::GetAssetsDirectory() + sDirectory + "\\" + sTexName;
std::wstring wStr = std::wstring( sPath.begin(), sPath.end() );
if ( FAILED( (hr = D3DXCreateTextureFromFile( pd3dDevice, wStr.c_str(), &m_pD3DTexture )) ) ) {
spdlog::error( "Failed to create Texture2D\nName: {}\nError: {} :: {}",
sPath.c_str(), DXGetErrorStringA( hr ), DXGetErrorDescriptionA( hr ) );
return;
}
D3DSURFACE_DESC desc;
m_pD3DTexture->GetLevelDesc( 0, &desc );
m_nHeight = desc.Height;
m_nWidth = desc.Width;
}
CTexture2D::CTexture2D( LPDIRECT3DDEVICE9 pd3dDevice, int nWidth, int nHeight, Assets::CPalette* pPalette ) :
m_pd3dDevice( pd3dDevice ), m_nWidth( nWidth ), m_nHeight( nHeight )
{
D3DLOCKED_RECT rc;
pd3dDevice->CreateTexture( nWidth, nHeight, 1, 0, D3DFMT_X8R8G8B8, D3DPOOL_MANAGED, &m_pD3DTexture, NULL );
m_pD3DTexture->LockRect( 0, &rc, NULL, D3DLOCK_DISCARD );
BYTE* pTexels = static_cast<BYTE*>(rc.pBits);
for ( int y = 0; y < nHeight; y++ ) {
for ( int x = 0; x < nWidth; x++ ) {
const uint8_t uIndex = y * nWidth + x;
Color* clr = pPalette->GetColor( uIndex );
WriteRGBTexel( pTexels, x, y, rc.Pitch, clr );
}
}
m_pD3DTexture->UnlockRect( 0 );
}
CTexture2D::CTexture2D( LPDIRECT3DDEVICE9 pd3dDevice, int nWidth, int nHeight, uint8_t* pData, Assets::CPalette* pPalette ) :
m_pd3dDevice( pd3dDevice ), m_nWidth( nWidth ), m_nHeight( nHeight )
{
D3DLOCKED_RECT rc;
pd3dDevice->CreateTexture( nWidth, nHeight, 1, 0, D3DFMT_X8R8G8B8, D3DPOOL_MANAGED, &m_pD3DTexture, NULL );
m_pD3DTexture->LockRect( 0, &rc, NULL, D3DLOCK_DISCARD );
BYTE* pTexels = static_cast<BYTE*>(rc.pBits);
for ( int y = 0; y < nHeight; y++ ) {
for ( int x = 0; x < nWidth; x++ ) {
const size_t uIndex = y * nWidth + x;
Color* clr = pPalette->GetColor( pData[uIndex] );
WriteRGBTexel( pTexels, x, y, rc.Pitch, clr );
}
}
m_pD3DTexture->UnlockRect( 0 );
}
CTexture2D::CTexture2D( LPDIRECT3DDEVICE9 pd3dDevice, int nWidth, int nHeight ) :
m_pd3dDevice( pd3dDevice ), m_nWidth( nWidth ), m_nHeight( nHeight )
{
pd3dDevice->CreateTexture( nWidth, nHeight, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &m_pD3DTexture, NULL );
}
void CTexture2D::Clear()
{
if ( m_pD3DTexture ) {
m_pD3DTexture->Release();
m_pD3DTexture = nullptr;
}
}
CTexture2D::~CTexture2D()
{
Clear();
}
/*
-------------------
| CTextureManager |
-------------------
*/
CTextureManager::~CTextureManager()
{
DeleteAll();
}
void CTextureManager::AddTexture( const std::string& sName, CTexture2D* pTexture )
{
m_TextureMap[sName] = pTexture;
}
void CTextureManager::ClearAll()
{
for ( auto it = m_TextureMap.begin(); it != m_TextureMap.end(); ++it ) {
it->second->Clear();
}
}
CTexture2D* CTextureManager::LoadTexture( LPDIRECT3DDEVICE9 pd3dDevice, const std::string& sDirectory, const std::string& sName )
{
if ( auto it = m_TextureMap.find( sName ); it != m_TextureMap.end() ) {
return it->second;
}
CTexture2D* pTex = new CTexture2D( pd3dDevice, sDirectory, sName );
m_TextureMap[sName] = pTex;
return pTex;
}
CTexture2D* CTextureManager::LoadTexture( LPDIRECT3DDEVICE9 pd3dDevice, const std::string& sDirectory, const std::string& sName, int nHeight, int nWidth )
{
if ( auto it = m_TextureMap.find( sName ); it != m_TextureMap.end() ) {
return it->second;
}
CTexture2D* pTex = new CTexture2D( pd3dDevice, sDirectory, sName, nHeight, nWidth );
m_TextureMap[sName] = pTex;
return pTex;
}
CTexture2D* CTextureManager::GetTexture2D( const std::string& sName )
{
if ( auto iter = m_TextureMap.find( sName ); iter != m_TextureMap.end() ) {
return iter->second;
}
return nullptr;
}
void CTextureManager::DeleteAll()
{
for ( auto it = m_TextureMap.begin(); it != m_TextureMap.end(); ++it ) {
delete it->second;
}
m_TextureMap.clear();
}
/*
-------------------
| Utils |
-------------------
*/
CTexture2D* CopyTexture( LPDIRECT3DDEVICE9 pd3dDevice, CTexture2D* pSrcTex2D, const std::string& sName, int nDownscale )
{
if ( !pSrcTex2D || !pSrcTex2D->GetTexture() ) {
spdlog::error( "CopyTexture: Source texture is nullptr." );
return nullptr;
}
IDirect3DTexture9* pSrcD3DTex = pSrcTex2D->GetTexture();
int nWidth = nDownscale;
int nHeight = nDownscale;
DWORD dwFilter = D3DX_FILTER_BOX;
if ( nDownscale <= 0 ) {
D3DSURFACE_DESC desc;
pSrcD3DTex->GetLevelDesc( 0, &desc );
nWidth = desc.Width;
nHeight = desc.Height;
dwFilter = D3DX_FILTER_NONE;
}
CTexture2D* pTexture2D = new CTexture2D( pd3dDevice, nWidth, nHeight );
IDirect3DTexture9* pDstD3DTex = pTexture2D->GetTexture();
if ( !pDstD3DTex ) {
spdlog::error( "CopyTexture: Failed to create destination texture." );
delete pTexture2D;
return nullptr;
}
IDirect3DSurface9* pSrcSurface = nullptr;
IDirect3DSurface9* pDstSurface = nullptr;
if ( SUCCEEDED( pSrcD3DTex->GetSurfaceLevel( 0, &pSrcSurface ) ) &&
SUCCEEDED( pDstD3DTex->GetSurfaceLevel( 0, &pDstSurface ) ) ) {
if ( FAILED( D3DXLoadSurfaceFromSurface( pDstSurface, nullptr, nullptr, pSrcSurface, nullptr, nullptr, dwFilter, 0 ) ) ) {
spdlog::error( "CopyTexture: Failed to copy texture data." );
}
}
if ( pSrcSurface ) pSrcSurface->Release();
if ( pDstSurface ) pDstSurface->Release();
return pTexture2D;
}