World of Might and Magic  0.2.0
Open reimplementation of Might and Magic 6 7 8 game engine
Image.cpp
См. документацию.
2 
3 #include <algorithm>
4 
5 #include "Engine/Engine.h"
6 #include "Engine/LOD.h"
7 #include "Engine/OurMath.h"
8 #include "Engine/ZlibWrapper.h"
9 
13 
15 
17 
18 #include "PaletteManager.h"
19 
21 
22 stru355 stru_4E82A4 = {0x20, 0x41, 0, 0x20, 0xFF0000, 0xFF00, 0xFF, static_cast<int>(0xFF000000)};
23 stru355 stru_4EFCBC = {0x20, 0x41, 0, 0x10, 0x7C00, 0x3E0, 0x1F, 0x8000};
24 
25 // Texture_MM7 pTex_F7CE30;
26 
28  switch (format) {
30  return L"IMAGE_FORMAT_R5G6B5";
32  return L"IMAGE_FORMAT_A1R5G5B5";
34  return L"IMAGE_FORMAT_A8R8G8B8";
36  return L"IMAGE_FORMAT_R8G8B8";
38  return L"IMAGE_FORMAT_R8G8B8A8";
39 
40  default:
41  Error("Invalid format: %d", format);
42  return L"Invalid format";
43  }
44 }
45 
47  switch (format) {
49  return 2;
51  return 2;
53  return 4;
55  return 3;
57  return 4;
58 
59  default:
60  Error("Invalid format: %d", format);
61  return 0;
62  }
63 }
64 
66  if (!this->tex) {
67  this->tex = assets->GetBitmap(this->name);
68  }
69  return this->tex;
70 }
71 
73  TextureFrameTable *v1 = this;
74  FILE *file = fopen("data\\dtft.bin", "wb");
75  if (file == nullptr) {
76  Error("Unable to save dtft.bin!", 0);
77  }
78  fwrite(v1, 4u, 1u, file);
79  fwrite(v1->pTextures, 0x14u, v1->sNumTextures, file);
80  fclose(file);
81 }
82 
83 void TextureFrameTable::FromFile(void *data_mm6, void *data_mm7, void *data_mm8) {
84  uint num_mm6_frames = data_mm6 ? *(int *)data_mm6 : 0,
85  num_mm7_frames = data_mm7 ? *(int *)data_mm7 : 0,
86  num_mm8_frames = data_mm8 ? *(int *)data_mm8 : 0;
87 
88  this->sNumTextures =
89  /*num_mm6_frames + */ num_mm7_frames /*+ num_mm8_frames*/;
90  Assert(sNumTextures, 0);
91  // Assert(!num_mm8_frames);
92 
93  TextureFrame_MM7 *frame_data =
94  (TextureFrame_MM7 *)((unsigned char *)data_mm7 + 4);
95  auto frames = new TextureFrame[this->sNumTextures];
96  for (unsigned int i = 0; i < this->sNumTextures; ++i) {
97  frames[i].name = frame_data->pTextureName;
98  std::transform(frames[i].name.begin(), frames[i].name.end(),
99  frames[i].name.begin(), ::tolower);
100 
101  frames[i].uAnimLength = frame_data->uAnimLength;
102  frames[i].uAnimTime = frame_data->uAnimTime;
103  frames[i].uFlags = frame_data->uFlags;
104 
105  ++frame_data;
106  }
107 
108  // pTextures = (TextureFrame *)malloc(sNumTextures * sizeof(TextureFrame));
109  // memcpy(pTextures, (char *)data_mm7 + 4,
110  // num_mm7_frames * sizeof(TextureFrame)); memcpy(pTextures + num_mm7_frames,
111  // (char *)data_mm6 + 4, num_mm6_frames * sizeof(TextureFrame));
112  // memcpy(pTextures + num_mm6_frames + num_mm7_frames, (char *)data_mm8 + 4,
113  // num_mm8_frames * sizeof(TextureFrame));
114 
115  this->pTextures = frames;
116 }
117 
119  if (uFrameID <= this->sNumTextures && uFrameID >= 0) {
120  for (unsigned int i = uFrameID;; ++i) {
121  // this->pTextures[i].uTextureID =
122  // pBitmaps_LOD->LoadTexture(this->pTextures[i].pTextureName,
123  // TEXTURE_DEFAULT);
124 
125  // if (this->pTextures[i].uTextureID != -1)
126  // pBitmaps_LOD->pTextures[this->pTextures[i].uTextureID].palette_id2
127  // =
128  // pPaletteManager->LoadPalette(pBitmaps_LOD->pTextures[this->pTextures[i].uTextureID].palette_id1);
129 
130  if (this->pTextures[i].uFlags & 1) break;
131  }
132  }
133  return;
134 }
135 
137  String name = Str2;
138  std::transform(name.begin(), name.end(), name.begin(), ::tolower);
139 
140  for (unsigned int i = 0; i < this->sNumTextures; ++i) {
141  if (this->pTextures[i].name == name) return i;
142  }
143  return -1;
144 }
145 
146 Texture *TextureFrameTable::GetFrameTexture(int uFrameID, signed int a3) {
147  int v3 = uFrameID;
148  TextureFrame *v4 = this->pTextures;
149  TextureFrame *v5 = &v4[uFrameID];
150  int v6 = v5->uAnimLength;
151  if (v5->uFlags & 1 && (v6 != 0)) {
152  int v7 = (a3 >> 3) % v6;
153  for (char *i = (char *)&v5->uAnimTime;; i += 20) {
154  int v9 = *(short *)i;
155  if (v7 <= v9) break;
156  v7 -= v9;
157  ++v3;
158  }
159  return v4[v3].GetTexture();
160  } else {
161  return v5->GetTexture();
162  }
163 }
164 
166  header.pName[0] = 0;
167 
168  if (header.pBits & 0x0400) {
169  __debugbreak();
170  }
171 
172  if (paletted_pixels != nullptr) {
173  free(paletted_pixels);
174  paletted_pixels = nullptr;
175  }
176 
177  if (pPalette24 != nullptr) {
178  free(pPalette24);
179  pPalette24 = nullptr;
180  }
181 
182  pLevelOfDetail1 = nullptr;
183 
184  memset(&header, 0, sizeof(header));
185 }
186 
188  memset(&header, 0, sizeof(header));
189  paletted_pixels = nullptr;
190  pLevelOfDetail1 = nullptr;
191  pPalette24 = nullptr;
192 }
193 
195  Image *img = new Image();
196  if (img) {
197  img->loader = loader;
198  }
199 
200  return img;
201 }
202 
204  if (!initialized) {
205  void *data = nullptr;
209  }
210  }
211 
212  if ((width == 0 || height == 0) && initialized) __debugbreak();
213 
214  return initialized;
215 }
216 
217 unsigned int Image::GetWidth() {
218  if (!initialized) {
219  LoadImageData();
220  }
221 
222  if (initialized) {
223  if (width == 0) __debugbreak();
224  return width;
225  }
226 
227  return 0;
228 }
229 
230 unsigned int Image::GetHeight() {
231  if (!initialized) {
232  LoadImageData();
233  }
234 
235  if (initialized) {
236  if (height == 0) __debugbreak();
237  return height;
238  }
239 
240  return 0;
241 }
242 
243 Image *Image::Create(unsigned int width, unsigned int height,
244  IMAGE_FORMAT format, const void *pixels) {
245  if (width == 0 || height == 0) __debugbreak();
246 
247  Image *img = new Image(false);
248  if (img) {
249  img->initialized = true;
250  img->width = width;
251  img->height = height;
252  img->native_format = format;
253  unsigned int num_pixels = img->GetWidth() * img->GetHeight();
254  unsigned int num_pixels_bytes =
255  num_pixels * IMAGE_FORMAT_BytesPerPixel(format);
256  img->pixels[format] = new unsigned char[num_pixels_bytes];
257  if (pixels) {
258  memcpy(img->pixels[format], pixels, num_pixels_bytes);
259  } else {
260  memset(img->pixels[format], 0, num_pixels_bytes);
261  }
262 
263  return img;
264 
265  } else {
266  return nullptr;
267  }
268 }
269 
271  if (!initialized) {
272  LoadImageData();
273  }
274 
275  if (initialized) {
276  if (this->pixels[format]) {
277  return this->pixels[format];
278  }
279 
280  auto native_pixels = this->pixels[this->native_format];
281  if (native_pixels) {
282  static ImageFormatConverter
283  converters[IMAGE_NUM_FORMATS][IMAGE_NUM_FORMATS] = {
284  // IMAGE_FORMAT_R5G6B5 ->
285  {
286  nullptr, // IMAGE_FORMAT_R5G6B5
287  nullptr, // IMAGE_FORMAT_A1R5G5B5
288  Image_R5G6B5_to_A8R8G8B8, // IMAGE_FORMAT_A8R8G8B8
289  Image_R5G6B5_to_R8G8B8, // IMAGE_FORMAT_R8G8B8
290  Image_R5G6B5_to_R8G8B8A8, // IMAGE_FORMAT_R8G8B8A8
291  },
292 
293  // IMAGE_FORMAT_A1R5G5B5 ->
294  {
295  nullptr, // IMAGE_FORMAT_R5G6B5
296  nullptr, // IMAGE_FORMAT_A1R5G5B5
297  nullptr, // IMAGE_FORMAT_A8R8G8B8
298  nullptr, // IMAGE_FORMAT_R8G8B8
299  Image_A1R5G5B5_to_R8G8B8A8, // IMAGE_FORMAT_R8G8B8A8
300  },
301 
302  // IMAGE_FORMAT_A8R8G8B8 ->
303  {
304  Image_A8R8G8B8_to_R5G6B5, // IMAGE_FORMAT_R5G6B5
305  Image_A8R8G8B8_to_A1R5G5B5, // IMAGE_FORMAT_A1R5G5B5
306  nullptr, // IMAGE_FORMAT_A8R8G8B8
307  nullptr, // IMAGE_FORMAT_R8G8B8
308  Image_A8R8G8B8_to_R8G8B8A8, // IMAGE_FORMAT_R8G8B8A8
309  },
310  };
311 
312  ImageFormatConverter cvt = converters[this->native_format][format];
313  if (cvt) {
314  unsigned int num_pixels = this->GetWidth() * this->GetHeight();
315 
316  void *cvt_pixels =
317  new unsigned char[num_pixels *
319  if (cvt(width * height, native_pixels, cvt_pixels)) {
320  return this->pixels[format] = cvt_pixels;
321  } else {
322  delete[] cvt_pixels;
323  cvt_pixels = nullptr;
324  }
325  } else {
326  logger->Warning(L"No ImageConverter defined from %s to %s",
328  IMAGE_FORMAT_ToString(format));
329  }
330  }
331  }
332  return nullptr;
333 }
334 
336  if (loader) {
339  }
340 
341  if (initialized) {
342  if (loader) {
343  delete loader;
344  loader = nullptr;
345  }
346 
347  for (unsigned int i = 0; i < IMAGE_NUM_FORMATS; ++i) {
348  if (pixels[i]) {
349  delete[] pixels[i];
350  pixels[i] = nullptr;
351  }
352  }
353 
355  width = 0;
356  height = 0;
357  }
358 
359  delete this;
360  return true;
361 }
Image_A1R5G5B5_to_R8G8B8A8
bool Image_A1R5G5B5_to_R8G8B8A8(unsigned int num_pixels, const void *src_pixels, void *dst_pixels)
Definition: ImageFormatConverter.h:143
TextureFrame::uFlags
int16_t uFlags
Definition: Image.h:121
LOD.h
Image::GetHeight
unsigned int GetHeight()
Definition: Image.cpp:230
Image::native_format
IMAGE_FORMAT native_format
Definition: Image.h:51
TextureFrameTable::FindTextureByName
int FindTextureByName(const char *Str2)
Definition: Image.cpp:136
stru_4EFCBC
stru355 stru_4EFCBC
Definition: Image.cpp:23
TextureFrameTable::ToFile
void ToFile()
Definition: Image.cpp:72
pTextureFrameTable
struct TextureFrameTable * pTextureFrameTable
Definition: Image.cpp:20
Image::width
unsigned int width
Definition: Image.h:49
Texture_MM7::Texture_MM7
Texture_MM7()
Definition: Image.cpp:187
LegacyImages.h
IMAGE_FORMAT_R8G8B8A8
@ IMAGE_FORMAT_R8G8B8A8
Definition: Image.h:9
Image::loader
ImageLoader * loader
Definition: Image.h:47
height
EGLSurface EGLint EGLint EGLint EGLint height
Definition: SDL_egl.h:1596
TextureFrameTable::pTextures
TextureFrame * pTextures
Definition: Image.h:143
stru_4E82A4
stru355 stru_4E82A4
Definition: Image.cpp:22
Texture
Definition: Texture.h:4
ImageFormatConverter
bool(* ImageFormatConverter)(unsigned int num_pixels, const void *src, void *dst)
Definition: ImageFormatConverter.h:3
IMAGE_FORMAT_BytesPerPixel
unsigned int IMAGE_FORMAT_BytesPerPixel(IMAGE_FORMAT format)
Definition: Image.cpp:46
PaletteManager.h
TextureFrame::uAnimLength
int16_t uAnimLength
Definition: Image.h:120
TextureFrameTable::FromFile
void FromFile(void *data_mm6, void *data_mm7, void *data_mm8)
Definition: Image.cpp:83
TextureFrame_MM7::uAnimTime
int16_t uAnimTime
Definition: LegacyImages.h:80
ImageFormatConverter.h
ImageLoader.h
IMAGE_INVALID_FORMAT
@ IMAGE_INVALID_FORMAT
Definition: Image.h:12
TextureHeader::pBits
uint32_t pBits
Definition: Image.h:91
Texture_MM7::paletted_pixels
uint8_t * paletted_pixels
Definition: Image.h:102
Engine.h
Image.h
TextureFrame::GetTexture
Texture * GetTexture()
Definition: Image.cpp:65
Image_R5G6B5_to_A8R8G8B8
bool Image_R5G6B5_to_A8R8G8B8(unsigned int num_pixels, const void *src_pixels, void *dst_pixels)
Definition: ImageFormatConverter.h:82
IMAGE_FORMAT_A8R8G8B8
@ IMAGE_FORMAT_A8R8G8B8
Definition: Image.h:7
TextureFrameTable
Definition: Image.h:131
Image_R5G6B5_to_R8G8B8A8
bool Image_R5G6B5_to_R8G8B8A8(unsigned int num_pixels, const void *src_pixels, void *dst_pixels)
Definition: ImageFormatConverter.h:94
format
SDL_AudioFormat format
Definition: SDL_audio.h:194
Texture_MM7::header
TextureHeader header
Definition: Image.h:101
Image::GetWidth
unsigned int GetWidth()
Definition: Image.cpp:217
IMAGE_FORMAT_R8G8B8
@ IMAGE_FORMAT_R8G8B8
Definition: Image.h:8
TextureFrameTable::sNumTextures
uint32_t sNumTextures
Definition: Image.h:142
TextureFrame::name
String name
Definition: Image.h:118
Image_A8R8G8B8_to_R8G8B8A8
bool Image_A8R8G8B8_to_R8G8B8A8(unsigned int num_pixels, const void *src_pixels, void *dst_pixels)
Definition: ImageFormatConverter.h:46
Image
Definition: Image.h:19
IMAGE_FORMAT_ToString
const wchar_t * IMAGE_FORMAT_ToString(IMAGE_FORMAT format)
Definition: Image.cpp:27
Image_A8R8G8B8_to_A1R5G5B5
bool Image_A8R8G8B8_to_A1R5G5B5(unsigned int num_pixels, const void *src_pixels, void *dst_pixels)
Definition: ImageFormatConverter.h:69
TextureHeader::pName
char pName[16]
Definition: Image.h:79
ZlibWrapper.h
TextureFrame_MM7::pTextureName
char pTextureName[12]
Definition: LegacyImages.h:78
TextureFrame_MM7
Definition: LegacyImages.h:77
IMAGE_FORMAT_R5G6B5
@ IMAGE_FORMAT_R5G6B5
Definition: Image.h:5
v1
GLfloat GLfloat v1
Definition: SDL_opengl_glext.h:694
Image::LoadImageData
virtual bool LoadImageData()
Definition: Image.cpp:203
width
EGLSurface EGLint EGLint EGLint width
Definition: SDL_egl.h:1596
Log::Warning
void Warning(const wchar_t *pFormat,...)
Definition: Log.cpp:28
Image::height
unsigned int height
Definition: Image.h:50
ImageLoader::GetResourceName
virtual String GetResourceName() const
Definition: ImageLoader.h:15
TextureFrameTable::LoadAnimationSequenceAndPalettes
void LoadAnimationSequenceAndPalettes(int uIconID)
Definition: Image.cpp:118
AssetsManager::GetBitmap
Texture * GetBitmap(const String &name)
Definition: AssetsManager.cpp:126
ImageLoader
Definition: ImageLoader.h:10
IMAGE_FORMAT
IMAGE_FORMAT
Definition: Image.h:4
Image_R5G6B5_to_R8G8B8
bool Image_R5G6B5_to_R8G8B8(unsigned int num_pixels, const void *src_pixels, void *dst_pixels)
Definition: ImageFormatConverter.h:116
transform
GLuint GLenum GLenum transform
Definition: SDL_opengl_glext.h:9999
Image::Image
Image(bool lazy_initialization=true)
Definition: Image.h:21
FrameTableInc.h
Image::initialized
bool initialized
Definition: Image.h:46
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
AssetsManager::ReleaseImage
bool ReleaseImage(const String &name)
Definition: AssetsManager.cpp:16
name
EGLImageKHR EGLint * name
Definition: SDL_egl.h:1497
Image_A8R8G8B8_to_R5G6B5
bool Image_A8R8G8B8_to_R5G6B5(unsigned int num_pixels, const void *src_pixels, void *dst_pixels)
Definition: ImageFormatConverter.h:57
ImageLoader::Load
virtual bool Load(unsigned int *width, unsigned int *height, void **pixels, IMAGE_FORMAT *format)=0
Texture.h
Image::Create
static Image * Create(unsigned int width, unsigned int height, IMAGE_FORMAT format, const void *pixels=nullptr)
Definition: Image.cpp:243
assets
AssetsManager * assets
Definition: AssetsManager.cpp:12
uint
unsigned int uint
Definition: MM7.h:4
v3
GLfloat GLfloat GLfloat GLfloat v3
Definition: SDL_opengl_glext.h:696
__debugbreak
void __cdecl __debugbreak(void)
IMAGE_FORMAT_A1R5G5B5
@ IMAGE_FORMAT_A1R5G5B5
Definition: Image.h:6
TextureFrame_MM7::uFlags
int16_t uFlags
Definition: LegacyImages.h:82
Image::GetPixels
const void * GetPixels(IMAGE_FORMAT format)
Definition: Image.cpp:270
stru355
Definition: Image.h:173
OurMath.h
IMAGE_NUM_FORMATS
@ IMAGE_NUM_FORMATS
Definition: Image.h:11
Texture_MM7::pLevelOfDetail1
uint8_t * pLevelOfDetail1
Definition: Image.h:103
logger
Log * logger
Definition: IocContainer.cpp:47
TextureFrame_MM7::uAnimLength
int16_t uAnimLength
Definition: LegacyImages.h:81
Texture_MM7::pPalette24
uint8_t * pPalette24
Definition: Image.h:104
TextureFrame
Definition: Image.h:114
TextureFrame::uAnimTime
int16_t uAnimTime
Definition: Image.h:119
pixels
GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid * pixels
Definition: SDL_opengl.h:1572
String
std::string String
Definition: Strings.h:10
Image::Release
bool Release()
Definition: Image.cpp:335
Texture_MM7::Release
void Release()
Definition: Image.cpp:165
img
GLint GLvoid * img
Definition: SDL_opengl.h:1980
TextureFrame::tex
Texture * tex
Definition: Image.h:126
TextureFrameTable::GetFrameTexture
Texture * GetFrameTexture(int uFrameID, int time)
Definition: Image.cpp:146