Skip to content

Commit 3f3b353

Browse files
authored
Merge pull request hrydgard#21092 from white-axe/libretro-vfs
Use the libretro VFS interface in libretro builds
2 parents b648243 + a950879 commit 3f3b353

45 files changed

Lines changed: 675 additions & 4766 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ if(LIBRETRO)
256256
if(NOT MSVC)
257257
add_compile_options(-fPIC)
258258
endif()
259+
add_compile_definitions(HAVE_LIBRETRO_VFS)
259260
endif()
260261

261262
if(ANDROID)
@@ -944,6 +945,10 @@ add_library(Common STATIC
944945
Common/TimeUtil.h
945946
)
946947

948+
if(LIBRETRO)
949+
target_include_directories(Common PRIVATE libretro/libretro-common/include)
950+
endif()
951+
947952
include_directories(Common)
948953
setup_target_project(Common Common)
949954

@@ -1685,6 +1690,10 @@ add_library(native STATIC
16851690
ext/jpge/jpge.h
16861691
)
16871692

1693+
if(LIBRETRO)
1694+
target_include_directories(native PRIVATE libretro/libretro-common/include)
1695+
endif()
1696+
16881697
if(LINUX AND NOT ANDROID)
16891698
set(RT_LIB rt)
16901699
endif()
@@ -2511,6 +2520,10 @@ add_library(${CoreLibName} ${CoreLinkType}
25112520
${CMAKE_CURRENT_BINARY_DIR}/git-version.cpp
25122521
)
25132522

2523+
if(LIBRETRO)
2524+
target_include_directories(${CoreLibName} PRIVATE libretro/libretro-common/include)
2525+
endif()
2526+
25142527
if(ANDROID)
25152528
set(CoreExtraLibs ${CoreExtraLibs} android)
25162529
if(X86_64)

Common/Data/Format/IniFile.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#endif
1515

1616
#include <iostream>
17-
#include <fstream>
1817
#include <sstream>
1918
#include <string>
2019
#include <vector>

Common/Data/Format/PNGLoad.cpp

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -134,35 +134,63 @@ bool PNGHeaderPeek::IsValidPNGHeader() const {
134134
}
135135

136136
bool pngSave(const Path &filename, const void *buffer, int w, int h, int bytesPerPixel) {
137-
png_image png{};
138-
png.version = PNG_IMAGE_VERSION;
139-
png.format = bytesPerPixel == 3 ? PNG_FORMAT_RGB : PNG_FORMAT_RGBA;
140-
png.width = w;
141-
png.height = h;
142-
const int row_stride = w * bytesPerPixel;
137+
png_bytepp row_ptrs = nullptr;
143138

144139
FILE *fp = File::OpenCFile(filename, "wb");
145140
if (!fp) {
146141
ERROR_LOG(Log::IO, "Unable to open png file for writing: %s", filename.c_str());
147142
return false;
148143
}
149144

150-
int result = png_image_write_to_stdio(&png, fp, 0, buffer, row_stride, nullptr);
145+
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, pngErrorHandler, pngWarningHandler);
146+
if (png_ptr == nullptr) {
147+
fclose(fp);
148+
ERROR_LOG(Log::IO, "PNG encode failed.");
149+
return false;
150+
}
151151

152-
if (png.warning_or_error >= 2) {
153-
ERROR_LOG(Log::IO, "Saving image to PNG produced errors.");
152+
png_infop info_ptr = png_create_info_struct(png_ptr);
153+
if (info_ptr == nullptr) {
154+
png_destroy_write_struct(&png_ptr, nullptr);
155+
fclose(fp);
156+
ERROR_LOG(Log::IO, "PNG encode failed.");
157+
return false;
154158
}
155159

156-
png_image_free(&png);
157-
fclose(fp);
160+
if (setjmp(png_jmpbuf(png_ptr))) {
161+
if (row_ptrs != nullptr) {
162+
png_free(png_ptr, row_ptrs);
163+
}
164+
png_destroy_write_struct(&png_ptr, &info_ptr);
165+
fclose(fp);
158166

159-
if (!result) {
160167
// Should we even do this?
161168
File::Delete(filename);
162169

163170
ERROR_LOG(Log::IO, "PNG encode failed.");
164171
return false;
165172
}
166173

174+
png_set_write_fn(png_ptr, fp, [](png_structp png_ptr, png_bytep data, png_size_t size) {
175+
if (fwrite(data, 1, size, (FILE *)png_get_io_ptr(png_ptr)) < size) {
176+
png_error(png_ptr, "Failed to write to file.");
177+
}
178+
}, [](png_structp png_ptr) {
179+
fflush((FILE *)png_get_io_ptr(png_ptr));
180+
});
181+
182+
png_set_IHDR(png_ptr, info_ptr, w, h, 8, bytesPerPixel == 3 ? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_RGBA, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
183+
184+
row_ptrs = (png_bytepp)png_malloc(png_ptr, (png_alloc_size_t)h * (png_alloc_size_t)sizeof(png_bytep));
185+
for (png_alloc_size_t i = 0; i < h; ++i) {
186+
row_ptrs[i] = (png_bytep)buffer + (png_alloc_size_t)w * (png_alloc_size_t)bytesPerPixel * i;
187+
}
188+
png_set_rows(png_ptr, info_ptr, row_ptrs);
189+
190+
png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, nullptr);
191+
192+
png_free(png_ptr, row_ptrs);
193+
png_destroy_write_struct(&png_ptr, &info_ptr);
194+
fclose(fp);
167195
return true;
168196
}

0 commit comments

Comments
 (0)