Use SDL_Surface/SDL_Palette in LegoImage

This commit is contained in:
Anonymous Maarten 2024-12-27 23:53:33 +01:00
parent 97fc786178
commit 0806733e69
4 changed files with 104 additions and 92 deletions

View File

@ -107,9 +107,9 @@ LegoTextureInfo* LegoTextureInfo::Create(const char* p_name, LegoTexture* p_text
for (i = 0; i < sizeOfArray(entries); i++) { for (i = 0; i < sizeOfArray(entries); i++) {
if (i < image->GetCount()) { if (i < image->GetCount()) {
entries[i].peFlags = 0; entries[i].peFlags = 0;
entries[i].peRed = image->GetPaletteEntry(i).GetRed(); entries[i].peRed = image->GetPalette()->colors[i].r;
entries[i].peGreen = image->GetPaletteEntry(i).GetGreen(); entries[i].peGreen = image->GetPalette()->colors[i].g;
entries[i].peBlue = image->GetPaletteEntry(i).GetBlue(); entries[i].peBlue = image->GetPalette()->colors[i].b;
} }
else { else {
entries[i].peFlags = 0x80; entries[i].peFlags = 0x80;

View File

@ -753,12 +753,14 @@ void WriteDefaultTexture(LegoFile* p_file, const char* p_name)
paletteEntries[i].SetBlue(entries[i].peBlue); paletteEntries[i].SetBlue(entries[i].peBlue);
} }
image->SetCount(i); SDL_Palette* newPalette = SDL_CreatePalette(i);
if (i > 0) { if (i > 0) {
// Note: this appears to be a bug. size should be i * sizeof(LegoPaletteEntry) for (int j = 0; j < i; j++) {
memcpy(image->GetPalette(), paletteEntries, i); image->SetPaletteEntry(j, paletteEntries[j]);
}
} }
image->SetPalette(newPalette);
LegoTexture texture; LegoTexture texture;
texture.SetImage(image); texture.SetImage(image);

View File

@ -10,38 +10,39 @@ DECOMP_SIZE_ASSERT(LegoImage, 0x310);
// FUNCTION: LEGO1 0x100994c0 // FUNCTION: LEGO1 0x100994c0
LegoPaletteEntry::LegoPaletteEntry() LegoPaletteEntry::LegoPaletteEntry()
{ {
m_red = 0; m_color.r = 0;
m_green = 0; m_color.g = 0;
m_blue = 0; m_color.b = 0;
m_color.a = SDL_ALPHA_OPAQUE;
} }
// FUNCTION: LEGO1 0x100994d0 // FUNCTION: LEGO1 0x100994d0
LegoResult LegoPaletteEntry::Read(LegoStorage* p_storage) LegoResult LegoPaletteEntry::Read(LegoStorage* p_storage)
{ {
LegoResult result; 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; 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; 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 result;
} }
return SUCCESS; return SUCCESS;
} }
// FUNCTION: LEGO1 0x10099520 // FUNCTION: LEGO1 0x10099520
LegoResult LegoPaletteEntry::Write(LegoStorage* p_storage) LegoResult LegoPaletteEntry::Write(LegoStorage* p_storage) const
{ {
LegoResult result; 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; 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; 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 result;
} }
return SUCCESS; return SUCCESS;
@ -50,84 +51,86 @@ LegoResult LegoPaletteEntry::Write(LegoStorage* p_storage)
// FUNCTION: LEGO1 0x10099570 // FUNCTION: LEGO1 0x10099570
LegoImage::LegoImage() LegoImage::LegoImage()
{ {
m_width = 0; m_surface = NULL;
m_height = 0; m_palette = NULL;
m_count = 0;
m_bits = NULL;
} }
// FUNCTION: LEGO1 0x100995a0 // FUNCTION: LEGO1 0x100995a0
LegoImage::LegoImage(LegoU32 p_width, LegoU32 p_height) LegoImage::LegoImage(LegoU32 p_width, LegoU32 p_height)
{ {
m_width = p_width; m_surface = SDL_CreateSurface(p_width, p_height, SDL_PIXELFORMAT_INDEX8);
m_height = p_height; m_palette = NULL;
m_count = 0;
m_bits = new LegoU8[m_width * m_height];
} }
// FUNCTION: LEGO1 0x100995f0 // FUNCTION: LEGO1 0x100995f0
LegoImage::~LegoImage() LegoImage::~LegoImage()
{ {
if (m_bits) { SDL_DestroySurface(m_surface);
delete[] m_bits; SDL_DestroyPalette(m_palette);
}
} }
// FUNCTION: LEGO1 0x10099610 // FUNCTION: LEGO1 0x10099610
LegoResult LegoImage::Read(LegoStorage* p_storage, LegoU32 p_square) LegoResult LegoImage::Read(LegoStorage* p_storage, LegoU32 p_square)
{ {
LegoResult result; 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; return result;
} }
if ((result = p_storage->Read(&m_height, sizeof(m_height))) != SUCCESS) { if ((result = p_storage->Read(&height, sizeof(height))) != SUCCESS) {
return result; return result;
} }
if ((result = p_storage->Read(&m_count, sizeof(m_height))) != SUCCESS) { if ((result = p_storage->Read(&count, sizeof(height))) != SUCCESS) {
return result; return result;
} }
for (LegoU32 i = 0; i < m_count; i++) { if (m_palette) {
if ((result = m_palette[i].Read(p_storage)) != SUCCESS) { 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; return result;
} }
m_palette->colors[i] = palette_entry.getColor();
} }
if (m_bits) { if (m_surface) {
delete[] m_bits; SDL_DestroySurface(m_surface);
} }
m_bits = new LegoU8[m_width * m_height]; m_surface = SDL_CreateSurface(width, height, SDL_PIXELFORMAT_INDEX8);
if ((result = p_storage->Read(m_bits, m_width * m_height)) != SUCCESS) { if ((result = p_storage->Read(m_surface->pixels, width * height)) != SUCCESS) {
return result; return result;
} }
if (p_square && m_width != m_height) { if (p_square && width != height) {
LegoU8* newBits; SDL_Surface* newSurface;
if (m_height < m_width) { if (height < width) {
LegoU32 aspect = m_width / m_height; LegoU32 aspect = width / height;
newBits = new LegoU8[m_width * m_width]; newSurface = SDL_CreateSurface(width, width, SDL_PIXELFORMAT_INDEX8);
LegoU8* src = m_bits; LegoU8* src = (LegoU8*) m_surface->pixels;
LegoU8* dst = newBits; LegoU8* dst = (LegoU8*) newSurface->pixels;
for (LegoU32 row = 0; row < m_height; row++) { for (LegoU32 row = 0; row < height; row++) {
if (aspect) { if (aspect) {
for (LegoU32 dup = aspect; dup; dup--) { for (LegoU32 dup = aspect; dup; dup--) {
memcpy(dst, src, m_width); memcpy(dst, src, width);
dst += m_width; dst += width;
} }
} }
src += m_width; src += width;
} }
m_height = m_width; height = width;
} }
else { else {
LegoU32 aspect = m_height / m_width; LegoU32 aspect = height / width;
newBits = new LegoU8[m_height * m_height]; newSurface = SDL_CreateSurface(height, height, SDL_PIXELFORMAT_INDEX8);
LegoU8* src = m_bits; LegoU8* src = (LegoU8*) m_surface->pixels;
LegoU8* dst = newBits; LegoU8* dst = (LegoU8*) newSurface->pixels;
for (LegoU32 row = 0; row < m_height; row++) { for (LegoU32 row = 0; row < height; row++) {
for (LegoU32 col = 0; col < m_width; col++) { for (LegoU32 col = 0; col < width; col++) {
if (aspect) { if (aspect) {
for (LegoU32 dup = aspect; dup; dup--) { for (LegoU32 dup = aspect; dup; dup--) {
*dst = *src; *dst = *src;
@ -139,11 +142,11 @@ LegoResult LegoImage::Read(LegoStorage* p_storage, LegoU32 p_square)
} }
} }
m_width = m_height; width = height;
} }
delete[] m_bits; SDL_DestroySurface(m_surface);
m_bits = newBits; m_surface = newSurface;
} }
return SUCCESS; return SUCCESS;
@ -153,22 +156,24 @@ LegoResult LegoImage::Read(LegoStorage* p_storage, LegoU32 p_square)
LegoResult LegoImage::Write(LegoStorage* p_storage) LegoResult LegoImage::Write(LegoStorage* p_storage)
{ {
LegoResult result; 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; 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; 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; return result;
} }
for (LegoU32 i = 0; i < m_count; i++) { if (m_palette) {
if ((result = m_palette[i].Write(p_storage)) != SUCCESS) { LegoPaletteEntry paletteEntry;
return result; 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 ((result = p_storage->Write(m_surface->pixels, m_surface->w * m_surface->h)) != SUCCESS) {
if (m_bits) {
if ((result = p_storage->Write(m_bits, m_width * m_height)) != SUCCESS) {
return result; return result;
} }
} }

View File

@ -3,6 +3,8 @@
#include "legotypes.h" #include "legotypes.h"
#include <SDL3/SDL_surface.h>
class LegoStorage; class LegoStorage;
// SIZE 0x03 // SIZE 0x03
@ -10,19 +12,19 @@ class LegoPaletteEntry {
public: public:
LegoPaletteEntry(); LegoPaletteEntry();
// LegoPaletteEntry(LegoU8 p_red, LegoU8 p_green, LegoU8 p_blue); // LegoPaletteEntry(LegoU8 p_red, LegoU8 p_green, LegoU8 p_blue);
LegoU8 GetRed() { return m_red; } LegoU8 GetRed() const { return m_color.r; }
void SetRed(LegoU8 p_red) { m_red = p_red; } void SetRed(LegoU8 p_red) { m_color.r = p_red; }
LegoU8 GetGreen() { return m_green; } LegoU8 GetGreen() const { return m_color.g; }
void SetGreen(LegoU8 p_green) { m_green = p_green; } void SetGreen(LegoU8 p_green) { m_color.g = p_green; }
LegoU8 GetBlue() { return m_blue; } LegoU8 GetBlue() const { return m_color.b; }
void SetBlue(LegoU8 p_blue) { m_blue = p_blue; } 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 Read(LegoStorage* p_storage);
LegoResult Write(LegoStorage* p_storage); LegoResult Write(LegoStorage* p_storage) const;
protected: protected:
LegoU8 m_red; // 0x00 SDL_Color m_color;
LegoU8 m_green; // 0x01
LegoU8 m_blue; // 0x02
}; };
// 0x310 // 0x310
@ -31,26 +33,29 @@ class LegoImage {
LegoImage(); LegoImage();
LegoImage(LegoU32 p_width, LegoU32 p_height); LegoImage(LegoU32 p_width, LegoU32 p_height);
~LegoImage(); ~LegoImage();
LegoU32 GetWidth() { return m_width; } LegoU32 GetWidth() const { return m_surface->w; }
void SetWidth(LegoU32 p_width) { m_width = p_width; } LegoU32 GetHeight() const { return m_surface->h; }
LegoU32 GetHeight() { return m_height; } LegoU32 GetCount() const { return m_palette ? m_palette->ncolors : 0; }
void SetHeight(LegoU32 p_height) { m_height = p_height; } SDL_Palette* GetPalette() const { return m_palette; }
LegoU32 GetCount() { return m_count; } void SetPalette(SDL_Palette* p_palette)
void SetCount(LegoU32 p_count) { m_count = p_count; } {
LegoPaletteEntry* GetPalette() { return m_palette; } SDL_DestroyPalette(m_palette);
LegoPaletteEntry& GetPaletteEntry(LegoU32 p_i) { return m_palette[p_i]; } m_palette = p_palette;
void SetPaletteEntry(LegoU32 p_i, LegoPaletteEntry& p_paletteEntry) { m_palette[p_i] = p_paletteEntry; } }
LegoU8* GetBits() { return m_bits; } void SetPaletteEntry(LegoU32 p_i, LegoPaletteEntry& p_paletteEntry)
void SetBits(LegoU8* p_bits) { m_bits = p_bits; } {
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 Read(LegoStorage* p_storage, LegoU32 p_square);
LegoResult Write(LegoStorage* p_storage); LegoResult Write(LegoStorage* p_storage);
protected: protected:
LegoU32 m_width; // 0x00 SDL_Surface* m_surface;
LegoU32 m_height; // 0x04 SDL_Palette* m_palette;
LegoU32 m_count; // 0x08 // LegoU32 m_count; // 0x08
LegoPaletteEntry m_palette[256]; // 0x0c // LegoPaletteEntry m_palette[256]; // 0x0c
LegoU8* m_bits; // 0x30c // LegoU8* m_bits; // 0x30c
}; };
#endif // __LEGOIMAGE_H #endif // __LEGOIMAGE_H