diff --git a/LEGO1/lego/legoomni/src/common/legotextureinfo.cpp b/LEGO1/lego/legoomni/src/common/legotextureinfo.cpp index edf78e8c..b587cccd 100644 --- a/LEGO1/lego/legoomni/src/common/legotextureinfo.cpp +++ b/LEGO1/lego/legoomni/src/common/legotextureinfo.cpp @@ -107,9 +107,9 @@ LegoTextureInfo* LegoTextureInfo::Create(const char* p_name, LegoTexture* p_text for (i = 0; i < sizeOfArray(entries); i++) { if (i < image->GetCount()) { entries[i].peFlags = 0; - entries[i].peRed = image->GetPaletteEntry(i).GetRed(); - entries[i].peGreen = image->GetPaletteEntry(i).GetGreen(); - entries[i].peBlue = image->GetPaletteEntry(i).GetBlue(); + entries[i].peRed = image->GetPalette()->colors[i].r; + entries[i].peGreen = image->GetPalette()->colors[i].g; + entries[i].peBlue = image->GetPalette()->colors[i].b; } else { entries[i].peFlags = 0x80; diff --git a/LEGO1/lego/legoomni/src/common/legoutils.cpp b/LEGO1/lego/legoomni/src/common/legoutils.cpp index 918c10c4..3628f8fc 100644 --- a/LEGO1/lego/legoomni/src/common/legoutils.cpp +++ b/LEGO1/lego/legoomni/src/common/legoutils.cpp @@ -753,12 +753,14 @@ void WriteDefaultTexture(LegoFile* p_file, const char* p_name) paletteEntries[i].SetBlue(entries[i].peBlue); } - image->SetCount(i); + SDL_Palette* newPalette = SDL_CreatePalette(i); if (i > 0) { - // Note: this appears to be a bug. size should be i * sizeof(LegoPaletteEntry) - memcpy(image->GetPalette(), paletteEntries, i); + for (int j = 0; j < i; j++) { + image->SetPaletteEntry(j, paletteEntries[j]); + } } + image->SetPalette(newPalette); LegoTexture texture; texture.SetImage(image); diff --git a/LEGO1/lego/sources/misc/legoimage.cpp b/LEGO1/lego/sources/misc/legoimage.cpp index da3f04a0..a9337593 100644 --- a/LEGO1/lego/sources/misc/legoimage.cpp +++ b/LEGO1/lego/sources/misc/legoimage.cpp @@ -10,38 +10,39 @@ DECOMP_SIZE_ASSERT(LegoImage, 0x310); // FUNCTION: LEGO1 0x100994c0 LegoPaletteEntry::LegoPaletteEntry() { - m_red = 0; - m_green = 0; - m_blue = 0; + m_color.r = 0; + m_color.g = 0; + m_color.b = 0; + m_color.a = SDL_ALPHA_OPAQUE; } // FUNCTION: LEGO1 0x100994d0 LegoResult LegoPaletteEntry::Read(LegoStorage* p_storage) { LegoResult result; - if ((result = p_storage->Read(&m_red, sizeof(m_red))) != SUCCESS) { + if ((result = p_storage->Read(&m_color.r, sizeof(m_color.r))) != SUCCESS) { return result; } - if ((result = p_storage->Read(&m_green, sizeof(m_green))) != SUCCESS) { + if ((result = p_storage->Read(&m_color.g, sizeof(m_color.g))) != SUCCESS) { return result; } - if ((result = p_storage->Read(&m_blue, sizeof(m_blue))) != SUCCESS) { + if ((result = p_storage->Read(&m_color.b, sizeof(m_color.b))) != SUCCESS) { return result; } return SUCCESS; } // FUNCTION: LEGO1 0x10099520 -LegoResult LegoPaletteEntry::Write(LegoStorage* p_storage) +LegoResult LegoPaletteEntry::Write(LegoStorage* p_storage) const { LegoResult result; - if ((result = p_storage->Write(&m_red, sizeof(m_red))) != SUCCESS) { + if ((result = p_storage->Write(&m_color.r, sizeof(m_color.r))) != SUCCESS) { return result; } - if ((result = p_storage->Write(&m_green, sizeof(m_green))) != SUCCESS) { + if ((result = p_storage->Write(&m_color.g, sizeof(m_color.g))) != SUCCESS) { return result; } - if ((result = p_storage->Write(&m_blue, sizeof(m_blue))) != SUCCESS) { + if ((result = p_storage->Write(&m_color.b, sizeof(m_color.b))) != SUCCESS) { return result; } return SUCCESS; @@ -50,84 +51,86 @@ LegoResult LegoPaletteEntry::Write(LegoStorage* p_storage) // FUNCTION: LEGO1 0x10099570 LegoImage::LegoImage() { - m_width = 0; - m_height = 0; - m_count = 0; - m_bits = NULL; + m_surface = NULL; + m_palette = NULL; } // FUNCTION: LEGO1 0x100995a0 LegoImage::LegoImage(LegoU32 p_width, LegoU32 p_height) { - m_width = p_width; - m_height = p_height; - m_count = 0; - m_bits = new LegoU8[m_width * m_height]; + m_surface = SDL_CreateSurface(p_width, p_height, SDL_PIXELFORMAT_INDEX8); + m_palette = NULL; } // FUNCTION: LEGO1 0x100995f0 LegoImage::~LegoImage() { - if (m_bits) { - delete[] m_bits; - } + SDL_DestroySurface(m_surface); + SDL_DestroyPalette(m_palette); } // FUNCTION: LEGO1 0x10099610 LegoResult LegoImage::Read(LegoStorage* p_storage, LegoU32 p_square) { LegoResult result; - if ((result = p_storage->Read(&m_width, sizeof(m_width))) != SUCCESS) { + LegoU32 width, height, count; + if ((result = p_storage->Read(&width, sizeof(width))) != SUCCESS) { return result; } - if ((result = p_storage->Read(&m_height, sizeof(m_height))) != SUCCESS) { + if ((result = p_storage->Read(&height, sizeof(height))) != SUCCESS) { return result; } - if ((result = p_storage->Read(&m_count, sizeof(m_height))) != SUCCESS) { + if ((result = p_storage->Read(&count, sizeof(height))) != SUCCESS) { return result; } - for (LegoU32 i = 0; i < m_count; i++) { - if ((result = m_palette[i].Read(p_storage)) != SUCCESS) { + if (m_palette) { + SDL_DestroyPalette(m_palette); + } + m_palette = SDL_CreatePalette(count); + for (LegoU32 i = 0; i < count; i++) { + LegoPaletteEntry palette_entry; + if ((result = palette_entry.Read(p_storage)) != SUCCESS) { return result; } + m_palette->colors[i] = palette_entry.getColor(); } - if (m_bits) { - delete[] m_bits; + if (m_surface) { + SDL_DestroySurface(m_surface); } - m_bits = new LegoU8[m_width * m_height]; - if ((result = p_storage->Read(m_bits, m_width * m_height)) != SUCCESS) { + m_surface = SDL_CreateSurface(width, height, SDL_PIXELFORMAT_INDEX8); + if ((result = p_storage->Read(m_surface->pixels, width * height)) != SUCCESS) { return result; } - if (p_square && m_width != m_height) { - LegoU8* newBits; + if (p_square && width != height) { + SDL_Surface* newSurface; - if (m_height < m_width) { - LegoU32 aspect = m_width / m_height; - newBits = new LegoU8[m_width * m_width]; - LegoU8* src = m_bits; - LegoU8* dst = newBits; + if (height < width) { + LegoU32 aspect = width / height; + newSurface = SDL_CreateSurface(width, width, SDL_PIXELFORMAT_INDEX8); + LegoU8* src = (LegoU8*) m_surface->pixels; + LegoU8* dst = (LegoU8*) newSurface->pixels; - for (LegoU32 row = 0; row < m_height; row++) { + for (LegoU32 row = 0; row < height; row++) { if (aspect) { for (LegoU32 dup = aspect; dup; dup--) { - memcpy(dst, src, m_width); - dst += m_width; + memcpy(dst, src, width); + dst += width; } } - src += m_width; + src += width; } - m_height = m_width; + height = width; } else { - LegoU32 aspect = m_height / m_width; - newBits = new LegoU8[m_height * m_height]; - LegoU8* src = m_bits; - LegoU8* dst = newBits; + LegoU32 aspect = height / width; + newSurface = SDL_CreateSurface(height, height, SDL_PIXELFORMAT_INDEX8); + LegoU8* src = (LegoU8*) m_surface->pixels; + LegoU8* dst = (LegoU8*) newSurface->pixels; - for (LegoU32 row = 0; row < m_height; row++) { - for (LegoU32 col = 0; col < m_width; col++) { + for (LegoU32 row = 0; row < height; row++) { + for (LegoU32 col = 0; col < width; col++) { if (aspect) { for (LegoU32 dup = aspect; dup; dup--) { *dst = *src; @@ -139,11 +142,11 @@ LegoResult LegoImage::Read(LegoStorage* p_storage, LegoU32 p_square) } } - m_width = m_height; + width = height; } - delete[] m_bits; - m_bits = newBits; + SDL_DestroySurface(m_surface); + m_surface = newSurface; } return SUCCESS; @@ -153,22 +156,24 @@ LegoResult LegoImage::Read(LegoStorage* p_storage, LegoU32 p_square) LegoResult LegoImage::Write(LegoStorage* p_storage) { LegoResult result; - if ((result = p_storage->Write(&m_width, sizeof(m_width))) != SUCCESS) { + if ((result = p_storage->Write(&m_surface->w, sizeof(m_surface->w))) != SUCCESS) { return result; } - if ((result = p_storage->Write(&m_height, sizeof(m_height))) != SUCCESS) { + if ((result = p_storage->Write(&m_surface->h, sizeof(m_surface->h))) != SUCCESS) { return result; } - if ((result = p_storage->Write(&m_count, sizeof(m_height))) != SUCCESS) { + if ((result = p_storage->Write(&m_surface->h, sizeof(m_surface->h))) != SUCCESS) { return result; } - for (LegoU32 i = 0; i < m_count; i++) { - if ((result = m_palette[i].Write(p_storage)) != SUCCESS) { - return result; + if (m_palette) { + LegoPaletteEntry paletteEntry; + for (LegoU32 i = 0; i < m_palette->ncolors; i++) { + paletteEntry.setColor(m_palette->colors[i]); + if ((result = paletteEntry.Write(p_storage)) != SUCCESS) { + return result; + } } - } - if (m_bits) { - if ((result = p_storage->Write(m_bits, m_width * m_height)) != SUCCESS) { + if ((result = p_storage->Write(m_surface->pixels, m_surface->w * m_surface->h)) != SUCCESS) { return result; } } diff --git a/LEGO1/lego/sources/misc/legoimage.h b/LEGO1/lego/sources/misc/legoimage.h index c62701be..0b54ea5c 100644 --- a/LEGO1/lego/sources/misc/legoimage.h +++ b/LEGO1/lego/sources/misc/legoimage.h @@ -3,6 +3,8 @@ #include "legotypes.h" +#include + class LegoStorage; // SIZE 0x03 @@ -10,19 +12,19 @@ class LegoPaletteEntry { public: LegoPaletteEntry(); // LegoPaletteEntry(LegoU8 p_red, LegoU8 p_green, LegoU8 p_blue); - LegoU8 GetRed() { return m_red; } - void SetRed(LegoU8 p_red) { m_red = p_red; } - LegoU8 GetGreen() { return m_green; } - void SetGreen(LegoU8 p_green) { m_green = p_green; } - LegoU8 GetBlue() { return m_blue; } - void SetBlue(LegoU8 p_blue) { m_blue = p_blue; } + LegoU8 GetRed() const { return m_color.r; } + void SetRed(LegoU8 p_red) { m_color.r = p_red; } + LegoU8 GetGreen() const { return m_color.g; } + void SetGreen(LegoU8 p_green) { m_color.g = p_green; } + LegoU8 GetBlue() const { return m_color.b; } + void SetBlue(LegoU8 p_blue) { m_color.b = p_blue; } + SDL_Color getColor() const { return m_color; } + void setColor(SDL_Color p_color) { m_color = p_color; } LegoResult Read(LegoStorage* p_storage); - LegoResult Write(LegoStorage* p_storage); + LegoResult Write(LegoStorage* p_storage) const; protected: - LegoU8 m_red; // 0x00 - LegoU8 m_green; // 0x01 - LegoU8 m_blue; // 0x02 + SDL_Color m_color; }; // 0x310 @@ -31,26 +33,29 @@ class LegoImage { LegoImage(); LegoImage(LegoU32 p_width, LegoU32 p_height); ~LegoImage(); - LegoU32 GetWidth() { return m_width; } - void SetWidth(LegoU32 p_width) { m_width = p_width; } - LegoU32 GetHeight() { return m_height; } - void SetHeight(LegoU32 p_height) { m_height = p_height; } - LegoU32 GetCount() { return m_count; } - void SetCount(LegoU32 p_count) { m_count = p_count; } - LegoPaletteEntry* GetPalette() { return m_palette; } - LegoPaletteEntry& GetPaletteEntry(LegoU32 p_i) { return m_palette[p_i]; } - void SetPaletteEntry(LegoU32 p_i, LegoPaletteEntry& p_paletteEntry) { m_palette[p_i] = p_paletteEntry; } - LegoU8* GetBits() { return m_bits; } - void SetBits(LegoU8* p_bits) { m_bits = p_bits; } + LegoU32 GetWidth() const { return m_surface->w; } + LegoU32 GetHeight() const { return m_surface->h; } + LegoU32 GetCount() const { return m_palette ? m_palette->ncolors : 0; } + SDL_Palette* GetPalette() const { return m_palette; } + void SetPalette(SDL_Palette* p_palette) + { + SDL_DestroyPalette(m_palette); + m_palette = p_palette; + } + void SetPaletteEntry(LegoU32 p_i, LegoPaletteEntry& p_paletteEntry) + { + m_palette->colors[p_i] = p_paletteEntry.getColor(); + } + const LegoU8* GetBits() const { return (LegoU8*) m_surface->pixels; } LegoResult Read(LegoStorage* p_storage, LegoU32 p_square); LegoResult Write(LegoStorage* p_storage); protected: - LegoU32 m_width; // 0x00 - LegoU32 m_height; // 0x04 - LegoU32 m_count; // 0x08 - LegoPaletteEntry m_palette[256]; // 0x0c - LegoU8* m_bits; // 0x30c + SDL_Surface* m_surface; + SDL_Palette* m_palette; + // LegoU32 m_count; // 0x08 + // LegoPaletteEntry m_palette[256]; // 0x0c + // LegoU8* m_bits; // 0x30c }; #endif // __LEGOIMAGE_H