Skip to content

Commit

Permalink
pixel studio PSP export!
Browse files Browse the repository at this point in the history
  • Loading branch information
counter185 committed Oct 19, 2024
1 parent 3470745 commit 26243c1
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 9 deletions.
82 changes: 82 additions & 0 deletions freesprite/FileIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3140,6 +3140,88 @@ bool writeOpenRaster(PlatformNativePathString path, MainEditor* editor)
return false;
}

bool writePixelStudioPSP(PlatformNativePathString path, MainEditor* data)
{
std::ofstream outfile(path);

if (outfile.is_open()) {
json o = json::object();
o["Version"] = 2;
o["Id"] = randomUUID();
o["Name"] = "voidsprite Image";
o["Width"] = data->canvas.dimensions.x;
o["Height"] = data->canvas.dimensions.y;
o["Type"] = 0;
o["Background"] = true;
o["BackgroundColor"] = { {"r", 0}, {"g", 0}, {"b", 0}, {"a", 0}};
o["TileMode"] = false;
o["TileFade"] = data->tileGridAlpha;
o["ActiveClipIndex"] = 0;
o["Clips"] = json::array();

json clip = json::object();
clip["Name"] = "Untitled";
clip["LayerTypes"] = json::array();
clip["ActiveFrameIndex"] = 0;
clip["Frames"] = json::array();

json frame = json::object();
frame["Id"] = randomUUID();
frame["Delay"] = 0.3;
frame["ActiveLayerIndex"] = data->selLayer;
frame["Layers"] = json::array();

for (Layer*& l : data->layers) {
json layer = json::object();
layer["Id"] = randomUUID();
layer["Transparency"] = l->layerAlpha / 255.0;
layer["Hidden"] = l->hidden;
layer["Linked"] = false;
layer["Outline"] = 0;
layer["Lock"] = 0;
layer["Sx"] = 0;
layer["Sy"] = 0;
layer["Version"] = 1;

json historyJson;
historyJson["Actions"] = json::array();
historyJson["Index"] = 0;
std::string pixelDataPNGAsBase64 = "";
if (writePNG(convertStringOnWin32("temp.bin"), l)) {
FILE* infile = platformOpenFile(convertStringOnWin32("temp.bin"), PlatformFileModeRB);
fseek(infile, 0, SEEK_END);
uint64_t fileLength = ftell(infile);
fseek(infile, 0, SEEK_SET);
//char* fileBuffer = (char*)malloc(fileLength);
std::string fileBuffer;
fileBuffer.resize(fileLength);
fread(fileBuffer.data(), fileLength, 1, infile);
//fread(fileBuffer, fileLength, 1, infile);
fclose(infile);

pixelDataPNGAsBase64 = base64::to_base64(fileBuffer);
}
else {
printf("WRITEPNG FAILED\n");
}
historyJson["_source"] = pixelDataPNGAsBase64;

layer["_historyJson"] = historyJson.dump();

frame["Layers"].push_back(layer);
}

clip["Frames"].push_back(frame);

o["Clips"].push_back(clip);

outfile << o.dump();
outfile.close();
return true;
}
return false;
}

bool writeXYZ(PlatformNativePathString path, Layer* data)
{
std::vector<uint32_t> uniqueColors;
Expand Down
5 changes: 4 additions & 1 deletion freesprite/FileIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ bool writeVOIDSNv3(PlatformNativePathString path, MainEditor* editor);
bool writeVOIDSNv4(PlatformNativePathString path, MainEditor* editor);
bool writeVOIDSNv5(PlatformNativePathString path, MainEditor* editor);
bool writeOpenRaster(PlatformNativePathString path, MainEditor* data);
bool writePixelStudioPSP(PlatformNativePathString path, MainEditor* data);
bool writeXYZ(PlatformNativePathString path, Layer* data);
bool writeBMP(PlatformNativePathString path, Layer* data);
bool writeTGA(PlatformNativePathString path, Layer* data);
Expand Down Expand Up @@ -230,6 +231,7 @@ inline void g_setupIO() {
*exVOIDSNv4,
*exVOIDSNv3,
*exVOIDSNv2,
*exPixelStudioPSP,
*exORA,
*exPNG,
*exBMP,
Expand All @@ -247,6 +249,7 @@ inline void g_setupIO() {
g_fileExporters.push_back( exVOIDSNv3 = FileExporter::sessionExporter("voidsprite Session version 3", ".voidsnv3", &writeVOIDSNv3) );
g_fileExporters.push_back( exVOIDSNv2 = FileExporter::sessionExporter("voidsprite Session version 2", ".voidsnv2", &writeVOIDSNv2) );
g_fileExporters.push_back( exORA = FileExporter::sessionExporter("OpenRaster", ".ora", &writeOpenRaster) );
g_fileExporters.push_back( exPixelStudioPSP = FileExporter::sessionExporter("Pixel Studio PSP", ".psp", &writePixelStudioPSP) );

g_fileExporters.push_back( exPNG = FileExporter::flatExporter("PNG (libpng)", ".png", &writePNG, FORMAT_RGB | FORMAT_PALETTIZED) );
g_fileExporters.push_back( exXYZ = FileExporter::flatExporter("RPG2000/2003 XYZ", ".xyz", &writeXYZ, FORMAT_RGB | FORMAT_PALETTIZED) );
Expand All @@ -271,7 +274,7 @@ inline void g_setupIO() {
g_fileImporters.push_back(FileImporter::sessionImporter("voidsprite Session v2", ".voidsnv2", &readVOIDSN, exVOIDSNv2));
g_fileImporters.push_back(FileImporter::sessionImporter("voidsprite Session v1", ".voidsnv1", &readVOIDSN, exVOIDSNv3));
g_fileImporters.push_back(FileImporter::sessionImporter("OpenRaster", ".ora", &readOpenRaster, exORA));
g_fileImporters.push_back(FileImporter::sessionImporter("Pixel Studio", ".psp", &readPixelStudioPSP));
g_fileImporters.push_back(FileImporter::sessionImporter("Pixel Studio", ".psp", &readPixelStudioPSP, exPixelStudioPSP));
g_fileImporters.push_back(FileImporter::sessionImporter("RPG Maker 2000/2003 map (load chipset + preview map)", ".lmu", &readLMU));

g_fileImporters.push_back(FileImporter::flatImporter("voidsprite 9-segment pattern", ".void9sp", &readVOID9SP, NULL));
Expand Down
21 changes: 13 additions & 8 deletions freesprite/ToolText.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,21 @@ void ToolText::renderText()
//todo
}

TTF_SetFontSize(font, textSize);
textSurface = TTF_RenderUTF8_Solid(font, text.c_str(), { 255, 255, 255, 255 });
if (textSurface != NULL) {
SDL_Surface* converted = SDL_ConvertSurfaceFormat(textSurface, SDL_PIXELFORMAT_ARGB8888, 0);
SDL_FreeSurface(textSurface);
textSurface = converted;
cachedTextTexture = SDL_CreateTextureFromSurface(g_rd, textSurface);
if (font != NULL) {
TTF_SetFontSize(font, textSize);
textSurface = TTF_RenderUTF8_Solid(font, text.c_str(), { 255, 255, 255, 255 });
if (textSurface != NULL) {
SDL_Surface* converted = SDL_ConvertSurfaceFormat(textSurface, SDL_PIXELFORMAT_ARGB8888, 0);
SDL_FreeSurface(textSurface);
textSurface = converted;
cachedTextTexture = SDL_CreateTextureFromSurface(g_rd, textSurface);
}
else {
g_addNotification(ErrorNotification("Error", "Failed to render text"));
}
}
else {
g_addNotification(ErrorNotification("Error", "Failed to render text"));
g_addNotification(ErrorNotification("TTF Error", "Error loading font"));
}

}
14 changes: 14 additions & 0 deletions freesprite/mathops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -610,3 +610,17 @@ std::vector<std::string> split(std::string a, char b)

return ret;
}

std::string randomUUID()
{
std::string chars = "0123456789abcdef";
std::string ret = "";
for (int i = 0; i < 32; i++) {
ret += chars[rand() % 16];
}
ret.insert(8, "-");
ret.insert(13, "-");
ret.insert(18, "-");
ret.insert(23, "-");
return ret;
}
2 changes: 2 additions & 0 deletions freesprite/mathops.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ uint32_t PackRGBAtoARGB(uint8_t r, uint8_t g, uint8_t b, uint8_t a);

std::vector<std::string> split(std::string a, char b);

std::string randomUUID();

template<typename T>
inline std::vector<T> joinVectors(std::initializer_list<std::vector<T>> vecs)
{
Expand Down

0 comments on commit 26243c1

Please sign in to comment.