World of Might and Magic  0.2.0
Open reimplementation of Might and Magic 6 7 8 game engine
HWLContainer.cpp
См. документацию.
2 
3 #include <cstring>
4 #include <vector>
5 
6 #include "Engine/IocContainer.h"
7 #include "Engine/Log.h"
8 #include "Engine/ZlibWrapper.h"
9 
10 #pragma pack(push, 1)
11 struct HWLHeader {
14 };
15 #pragma pack(pop)
16 
18  pFile = nullptr;
20 }
21 
23  if (pFile != nullptr) {
24  fclose(this->pFile);
25  }
26 }
27 
28 bool HWLContainer::Open(const String &pFilename) {
29  pFile = fopen(pFilename.c_str(), "rb");
30  if (!pFile) {
31  log->Warning(L"Failed to open file: %S", pFilename.c_str());
32  return false;
33  }
34 
35  HWLHeader header;
36  fread(&header, sizeof(HWLHeader), 1, pFile);
37  if (header.uSignature != 'TD3D') {
38  log->Warning(L"Invalid format: %S", pFilename.c_str());
39  return false;
40  }
41  fseek(pFile, header.uDataOffset, SEEK_SET);
42 
43  typedef struct HWLNode {
44  String sName;
45  size_t uOffset;
46  } HWLNode;
47  std::vector<HWLNode> vNodes;
48 
49  uint32_t uNumItems = 0;
50  fread(&uNumItems, 4, 1, pFile);
51  char tmpName[21];
52  for (unsigned int i = 0; i < uNumItems; ++i) {
53  fread(tmpName, 20, 1, pFile);
54  tmpName[20] = 0;
55  HWLNode node;
56  node.sName = MakeLower(String(tmpName));
57  node.uOffset = 0;
58  vNodes.push_back(node);
59  }
60 
61  for (unsigned int i = 0; i < uNumItems; ++i) {
62  uint32_t uOffset = 0;
63  fread(&uOffset, 4, 1, pFile);
64  vNodes[i].uOffset = uOffset;
65  }
66 
67  for (HWLNode &node : vNodes) {
68  mNodes[node.sName] = node.uOffset;
69  }
70 
71  return true;
72 }
73 
74 #pragma pack(push, 1)
85 };
86 #pragma pack(pop)
87 
89  if (mNodes.size() == 0) {
90  return nullptr;
91  }
92 
93  std::map<String, size_t>::iterator it = mNodes.find(MakeLower(pName));
94  if (it == mNodes.end()) {
95  return nullptr;
96  }
97  size_t uOffset = it->second;
98 
99  fseek(pFile, uOffset, SEEK_SET);
100 
101  HWLTextureHeader textureHeader;
102  fread(&textureHeader, sizeof(HWLTextureHeader), 1, pFile);
103 
104  HWLTexture *pTex = new HWLTexture;
105  pTex->uBufferWidth = textureHeader.uBufferWidth;
106  pTex->uBufferHeight = textureHeader.uBufferHeight;
107  pTex->uAreaWidth = textureHeader.uAreaWidth;
108  pTex->uAreaHeigth = textureHeader.uAreaHeigth;
109  pTex->uWidth = textureHeader.uWidth;
110  pTex->uHeight = textureHeader.uHeight;
111  pTex->uAreaX = textureHeader.uAreaX;
112  pTex->uAreaY = textureHeader.uAreaY;
113 
114  pTex->pPixels = new uint16_t[pTex->uWidth * pTex->uHeight];
115  if (textureHeader.uCompressedSize) {
117  fread((void*)buffer->GetData(), textureHeader.uCompressedSize, 1, pFile);
119  memcpy(pTex->pPixels, buffer->GetData(), buffer->GetSize());
120  } else {
121  fread(pTex->pPixels, 2, pTex->uWidth * pTex->uHeight, pFile);
122  }
123 
124  return pTex;
125 }
uint16_t
unsigned __int16 uint16_t
Definition: SDL_config.h:37
HWLTextureHeader::uHeight
uint32_t uHeight
Definition: HWLContainer.cpp:82
IocContainer.h
HWLTextureHeader::uBufferWidth
uint32_t uBufferWidth
Definition: HWLContainer.cpp:77
Engine_::IocContainer::ResolveLogger
static Log * ResolveLogger()
Definition: IocContainer.cpp:51
HWLTextureHeader::uWidth
uint32_t uWidth
Definition: HWLContainer.cpp:81
HWLTextureHeader::uAreaY
uint32_t uAreaY
Definition: HWLContainer.cpp:84
HWLTexture::uAreaWidth
int uAreaWidth
Definition: HWLContainer.h:15
HWLTexture
Definition: HWLContainer.h:9
HWLContainer::LoadTexture
HWLTexture * LoadTexture(const String &pName)
Definition: HWLContainer.cpp:88
HWLTexture::uBufferWidth
int uBufferWidth
Definition: HWLContainer.h:13
buffer
EGLContext EGLenum EGLClientBuffer buffer
Definition: SDL_egl.h:952
AllocMemBuffer
PMemBuffer AllocMemBuffer(size_t size)
Definition: MemBuffer.cpp:20
HWLContainer::~HWLContainer
virtual ~HWLContainer()
Definition: HWLContainer.cpp:22
ZlibWrapper.h
HWLContainer::HWLContainer
HWLContainer()
Definition: HWLContainer.cpp:17
HWLHeader::uSignature
uint32_t uSignature
Definition: HWLContainer.cpp:12
HWLHeader::uDataOffset
uint32_t uDataOffset
Definition: HWLContainer.cpp:13
Log::Warning
void Warning(const wchar_t *pFormat,...)
Definition: Log.cpp:28
HWLTexture::uHeight
unsigned int uHeight
Definition: HWLContainer.h:18
HWLTextureHeader
Definition: HWLContainer.cpp:75
HWLTexture::uBufferHeight
int uBufferHeight
Definition: HWLContainer.h:14
HWLContainer::pFile
FILE * pFile
Definition: HWLContainer.h:36
HWLTexture::uWidth
unsigned int uWidth
Definition: HWLContainer.h:17
HWLHeader
Definition: HWLContainer.cpp:11
HWLContainer::Open
bool Open(const String &pFilename)
Definition: HWLContainer.cpp:28
HWLTextureHeader::uAreaWidth
uint32_t uAreaWidth
Definition: HWLContainer.cpp:79
HWLTexture::pPixels
uint16_t * pPixels
Definition: HWLContainer.h:21
zlib::Uncompress
int Uncompress(void *dest, unsigned int *destLen, const void *source, unsigned int sourceLen)
Definition: ZlibWrapper.cpp:7
HWLContainer::log
Log * log
Definition: HWLContainer.h:37
HWLTextureHeader::uAreaHeigth
uint32_t uAreaHeigth
Definition: HWLContainer.cpp:80
HWLTextureHeader::uAreaX
uint32_t uAreaX
Definition: HWLContainer.cpp:83
HWLTextureHeader::uCompressedSize
uint32_t uCompressedSize
Definition: HWLContainer.cpp:76
HWLContainer::mNodes
std::map< String, size_t > mNodes
Definition: HWLContainer.h:38
HWLTextureHeader::uBufferHeight
uint32_t uBufferHeight
Definition: HWLContainer.cpp:78
MakeLower
String MakeLower(const String &text)
Definition: Strings.cpp:43
HWLTexture::uAreaX
int uAreaX
Definition: HWLContainer.h:19
HWLTexture::uAreaY
int uAreaY
Definition: HWLContainer.h:20
HWLContainer.h
PMemBuffer
std::shared_ptr< IMemBuffer > PMemBuffer
Definition: MemBuffer.h:13
uint32_t
unsigned __int32 uint32_t
Definition: SDL_config.h:39
String
std::string String
Definition: Strings.h:10
Log.h
HWLTexture::uAreaHeigth
int uAreaHeigth
Definition: HWLContainer.h:16