Skip to content

Commit

Permalink
2500% performance increase to saving CCLCC thumbnails
Browse files Browse the repository at this point in the history
  • Loading branch information
Dextinfire committed Sep 15, 2024
1 parent b379d95 commit aa45e9d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
37 changes: 19 additions & 18 deletions src/games/cclcc/savesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ SaveError SaveSystem::MountSaveFile() {
entryArray[i] = new SaveFileEntry();

entryArray[i]->Status = Io::ReadLE<uint16_t>(stream);
if(entryArray[i]->Status == 1 && entryArray == QuickSaveEntries) {
if (entryArray[i]->Status == 1 && entryArray == QuickSaveEntries) {
QuickSaveCount++;
}
// Not sure about these two
Expand Down Expand Up @@ -279,7 +279,7 @@ void SaveSystem::WriteSaveFile() {
Io::WriteArrayLE<uint8_t>(GameExtraData, stream, 1024);

stream->Seek(0x387c, SEEK_SET); // TODO: Actually save system data

std::vector<uint8_t> thumbnailData(SaveThumbnailWidth * SaveThumbnailHeight * 4);
for (auto* entryArray : {FullSaveEntries, QuickSaveEntries}) {
int64_t saveDataPos = stream->Position;
for (int i = 0; i < MaxSaveEntries; i++) {
Expand Down Expand Up @@ -343,23 +343,24 @@ void SaveSystem::WriteSaveFile() {
assert(stream->Position - startPos == 0x3ec0);
Io::WriteArrayLE<uint8_t>(entry->MapLoadData, stream, 0x6ac8);
Io::WriteArrayLE<uint8_t>(entry->YesNoData, stream, 0x54);

// CCLCC PS4 Save thumbnails are 240x135 RGB16
std::vector<uint8_t> thumbnailData = Renderer->GetImageFromTexture(
entry->SaveThumbnail.Sheet.Texture, entry->SaveThumbnail.Bounds);



int thumbnailPadding = 0xA14;
stream->Seek(thumbnailPadding, SEEK_CUR);

for (int i = 0; i < thumbnailData.size(); i+=4) {
uint8_t r = thumbnailData[i];
uint8_t g = thumbnailData[i + 1];
uint8_t b = thumbnailData[i + 2];
uint16_t pixel = ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
Io::WriteLE<uint16_t>(stream, pixel);
}
Renderer->GetImageFromTexture(
entry->SaveThumbnail.Sheet.Texture, entry->SaveThumbnail.Bounds, thumbnailData);

int thumbnailPadding = 0xA14;
stream->Seek(thumbnailPadding, SEEK_CUR);

for (int i = 0; i < thumbnailData.size(); i += 4) {
uint8_t r = thumbnailData[i];
uint8_t g = thumbnailData[i + 1];
uint8_t b = thumbnailData[i + 2];
uint16_t pixel = ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
pixel = SDL_SwapBE16(pixel);
thumbnailData[i/2] = pixel >> 8;
thumbnailData[i/2 + 1] = pixel & 0xFF;
}
Io::WriteArrayLE<uint8_t>(thumbnailData.data(), stream, thumbnailData.size() / 2);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/io/physicalfilestream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ IoError PhysicalFileStream::Duplicate(Stream** outStream) {
}

int64_t PhysicalFileStream::Write(void* buffer, int64_t sz, int cnt) {
// Todo: buffered write (SDL_RWwrite doesn't buffer system calls)
int64_t written = SDL_RWwrite(RW, buffer, sz, cnt);
Seek(sz * cnt, SEEK_CUR);
return written;
Expand Down
1 change: 1 addition & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <SDL_stdinc.h>
#include <string>
#include <cctype>
#include <chrono>
#include "../vendor/span/span.hpp"

// TODO own _malloca for gcc
Expand Down

0 comments on commit aa45e9d

Please sign in to comment.