Use SDL_RWops
This commit is contained in:
@@ -133,6 +133,7 @@
|
||||
<file name="src/backend/sdl/SDLWindow.cpp" />
|
||||
<file name="src/backend/sdl/SDLRenderer.cpp" />
|
||||
<file name="src/backend/sdl/SDLMouse.cpp" />
|
||||
<file name="src/backend/sdl/SDLFileIO.cpp" />
|
||||
|
||||
</section>
|
||||
|
||||
@@ -149,7 +150,7 @@
|
||||
<file name="src/ui/TouchEvent.cpp" />
|
||||
<file name="src/ui/WindowEvent.cpp" />
|
||||
<file name="src/utils/ByteArray.cpp" />
|
||||
<file name="src/utils/FileIO.cpp" />
|
||||
<file name="src/utils/FileIO.cpp" unless="LIME_SDL" />
|
||||
|
||||
</files>
|
||||
|
||||
|
||||
@@ -1,25 +1,30 @@
|
||||
#ifndef LIME_UTILS_FILEIO_H
|
||||
#define LIME_UTILS_FILEIO_H
|
||||
|
||||
|
||||
extern "C" {
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
}
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
namespace lime {
|
||||
|
||||
|
||||
extern int fclose (FILE *stream);
|
||||
extern FILE *fdopen (int fd, const char *mode);
|
||||
extern FILE* fopen (const char *filename, const char *mode);
|
||||
struct FILE_HANDLE {
|
||||
|
||||
void *handle;
|
||||
|
||||
FILE_HANDLE (void* handle) : handle (handle) {}
|
||||
|
||||
FILE* getFile ();
|
||||
|
||||
};
|
||||
|
||||
extern int fclose (FILE_HANDLE *stream);
|
||||
extern FILE_HANDLE *fdopen (int fd, const char *mode);
|
||||
extern FILE_HANDLE *fopen (const char *filename, const char *mode);
|
||||
//extern FILE* freopen (const char *filename, const char *mode, FILE *stream);
|
||||
extern size_t fread (void *ptr, size_t size, size_t count, FILE *stream);
|
||||
extern int fseek (FILE *stream, long int offset, int origin);
|
||||
extern long int ftell (FILE *stream);
|
||||
extern size_t fwrite (const void *ptr, size_t size, size_t count, FILE *stream);
|
||||
extern size_t fread (void *ptr, size_t size, size_t count, FILE_HANDLE *stream);
|
||||
extern int fseek (FILE_HANDLE *stream, long int offset, int origin);
|
||||
extern long int ftell (FILE_HANDLE *stream);
|
||||
extern size_t fwrite (const void *ptr, size_t size, size_t count, FILE_HANDLE *stream);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -110,7 +110,7 @@ namespace lime {
|
||||
|
||||
if (resource->path) {
|
||||
|
||||
FILE *file;
|
||||
FILE_HANDLE *file;
|
||||
|
||||
#ifdef ANDROID
|
||||
FileInfo info = AndroidGetAssetFD (resource->path);
|
||||
@@ -130,7 +130,7 @@ namespace lime {
|
||||
#ifdef ANDROID
|
||||
ov_open (file, &oggFile, NULL, info.length);
|
||||
#else
|
||||
ov_open (file, &oggFile, NULL, 0);
|
||||
ov_open (file->getFile (), &oggFile, NULL, 0);
|
||||
#endif
|
||||
|
||||
} else {
|
||||
|
||||
@@ -49,7 +49,7 @@ namespace lime {
|
||||
WAVE_Data wave_data;
|
||||
unsigned char* data;
|
||||
|
||||
FILE *file = NULL;
|
||||
FILE_HANDLE *file = NULL;
|
||||
|
||||
if (resource->path) {
|
||||
|
||||
|
||||
111
project/src/backend/sdl/SDLFileIO.cpp
Normal file
111
project/src/backend/sdl/SDLFileIO.cpp
Normal file
@@ -0,0 +1,111 @@
|
||||
#include <utils/FileIO.h>
|
||||
|
||||
#ifdef HX_MACOS
|
||||
#include <CoreFoundation/CoreFoundation.h>
|
||||
#endif
|
||||
|
||||
#include <SDL_rwops.h>
|
||||
|
||||
|
||||
namespace lime {
|
||||
|
||||
|
||||
FILE* FILE_HANDLE::getFile () {
|
||||
|
||||
if (((SDL_RWops*)handle)->type == SDL_RWOPS_STDFILE) {
|
||||
|
||||
return ((SDL_RWops*)handle)->hidden.stdio.fp;
|
||||
|
||||
}
|
||||
|
||||
return NULL;
|
||||
|
||||
}
|
||||
|
||||
|
||||
int fclose (FILE_HANDLE *stream) {
|
||||
|
||||
if (stream) {
|
||||
|
||||
return SDL_RWclose ((SDL_RWops*)stream->handle);
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
|
||||
FILE_HANDLE *fdopen (int fd, const char *mode) {
|
||||
|
||||
return 0;
|
||||
//return SDL_RWFromFP (fd, mode);
|
||||
|
||||
}
|
||||
|
||||
|
||||
FILE_HANDLE *fopen (const char *filename, const char *mode) {
|
||||
|
||||
SDL_RWops *result;
|
||||
#ifdef HX_MACOS
|
||||
result = SDL_RWFromFile (filename, "rb");
|
||||
if (!result) {
|
||||
CFStringRef str = CFStringCreateWithCString (NULL, filename, kCFStringEncodingUTF8);
|
||||
CFURLRef path = CFBundleCopyResourceURL (CFBundleGetMainBundle (), str, NULL, NULL);
|
||||
CFRelease (str);
|
||||
if (path) {
|
||||
str = CFURLCopyPath (path);
|
||||
CFIndex maxSize = CFStringGetMaximumSizeForEncoding (CFStringGetLength (str), kCFStringEncodingUTF8);
|
||||
char *buffer = (char *)malloc (maxSize);
|
||||
if (CFStringGetCString (str, buffer, maxSize, kCFStringEncodingUTF8)) {
|
||||
result = ::fopen (buffer,"rb");
|
||||
free (buffer);
|
||||
}
|
||||
CFRelease (str);
|
||||
CFRelease (path);
|
||||
}
|
||||
}
|
||||
#else
|
||||
result = SDL_RWFromFile (filename, mode);
|
||||
#endif
|
||||
|
||||
if (result) {
|
||||
|
||||
return new FILE_HANDLE (result);
|
||||
|
||||
}
|
||||
|
||||
return NULL;
|
||||
|
||||
}
|
||||
|
||||
|
||||
size_t fread (void *ptr, size_t size, size_t count, FILE_HANDLE *stream) {
|
||||
|
||||
return SDL_RWread (stream ? (SDL_RWops*)stream->handle : NULL, ptr, size, count);
|
||||
|
||||
}
|
||||
|
||||
|
||||
int fseek (FILE_HANDLE *stream, long int offset, int origin) {
|
||||
|
||||
return SDL_RWseek (stream ? (SDL_RWops*)stream->handle : NULL, offset, origin);
|
||||
|
||||
}
|
||||
|
||||
|
||||
long int ftell (FILE_HANDLE *stream) {
|
||||
|
||||
return SDL_RWtell (stream ? (SDL_RWops*)stream->handle : NULL);
|
||||
|
||||
}
|
||||
|
||||
|
||||
size_t fwrite (const void *ptr, size_t size, size_t count, FILE_HANDLE *stream) {
|
||||
|
||||
return SDL_RWwrite (stream ? (SDL_RWops*)stream->handle : NULL, ptr, size, count);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -190,7 +190,7 @@ namespace lime {
|
||||
jpegError.base.error_exit = OnError;
|
||||
jpegError.base.output_message = OnOutput;
|
||||
|
||||
FILE *file = NULL;
|
||||
FILE_HANDLE *file = NULL;
|
||||
|
||||
if (setjmp (jpegError.on_error)) {
|
||||
|
||||
@@ -210,7 +210,7 @@ namespace lime {
|
||||
if (resource->path) {
|
||||
|
||||
file = lime::fopen (resource->path, "rb");
|
||||
jpeg_stdio_src (&cinfo, file);
|
||||
jpeg_stdio_src (&cinfo, file->getFile ());
|
||||
|
||||
} else {
|
||||
|
||||
|
||||
@@ -83,7 +83,7 @@ namespace lime {
|
||||
png_uint_32 width, height;
|
||||
int bit_depth, color_type, interlace_type;
|
||||
|
||||
FILE *file = NULL;
|
||||
FILE_HANDLE *file = NULL;
|
||||
|
||||
if (resource->path) {
|
||||
|
||||
@@ -136,7 +136,7 @@ namespace lime {
|
||||
|
||||
if (file) {
|
||||
|
||||
png_init_io (png_ptr, file);
|
||||
png_init_io (png_ptr, file->getFile ());
|
||||
png_set_sig_bytes (png_ptr, PNG_SIG_SIZE);
|
||||
|
||||
} else {
|
||||
|
||||
@@ -145,7 +145,7 @@ namespace lime {
|
||||
|
||||
ByteArray ByteArray::FromFile(const OSChar *inFilename)
|
||||
{
|
||||
FILE *file = lime::fopen (inFilename, "rb");
|
||||
FILE_HANDLE *file = lime::fopen (inFilename, "rb");
|
||||
if (!file)
|
||||
{
|
||||
#ifdef ANDROID
|
||||
@@ -170,7 +170,7 @@ value lime_byte_array_overwrite_file(value inFilename, value inBytes) {
|
||||
|
||||
// file is created if it doesn't exist,
|
||||
// if it exists, it is truncated to zero
|
||||
FILE *file = lime::fopen (val_os_string(inFilename), "wb");
|
||||
FILE_HANDLE *file = lime::fopen (val_os_string(inFilename), "wb");
|
||||
if (!file)
|
||||
{
|
||||
#ifdef ANDROID
|
||||
|
||||
@@ -8,24 +8,48 @@
|
||||
namespace lime {
|
||||
|
||||
|
||||
int fclose (FILE *stream) {
|
||||
FILE* FILE_HANDLE::getFile () {
|
||||
|
||||
return ::fclose (stream);
|
||||
return (FILE*)handle;
|
||||
|
||||
}
|
||||
|
||||
|
||||
FILE *fdopen (int fd, const char *mode) {
|
||||
int fclose (FILE_HANDLE *stream) {
|
||||
|
||||
return ::fdopen (fd, mode);
|
||||
if (stream) {
|
||||
|
||||
int value = ::fclose ((FILE*)stream->handle);
|
||||
if (stream) delete stream;
|
||||
return value;
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
|
||||
FILE* fopen (const char *filename, const char *mode) {
|
||||
FILE_HANDLE *fdopen (int fd, const char *mode) {
|
||||
|
||||
FILE* result = ::fdopen (fd, mode);
|
||||
|
||||
if (result) {
|
||||
|
||||
return new FILE_HANDLE (result);
|
||||
|
||||
}
|
||||
|
||||
return NULL;
|
||||
|
||||
}
|
||||
|
||||
|
||||
FILE_HANDLE *fopen (const char *filename, const char *mode) {
|
||||
|
||||
FILE* result;
|
||||
#ifdef HX_MACOS
|
||||
FILE *result = ::fopen (filename, "rb");
|
||||
result = ::fopen (filename, "rb");
|
||||
if (!result) {
|
||||
CFStringRef str = CFStringCreateWithCString (NULL, filename, kCFStringEncodingUTF8);
|
||||
CFURLRef path = CFBundleCopyResourceURL (CFBundleGetMainBundle (), str, NULL, NULL);
|
||||
@@ -42,38 +66,45 @@ namespace lime {
|
||||
CFRelease (path);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
#else
|
||||
return ::fopen (filename, mode);
|
||||
result = ::fopen (filename, mode);
|
||||
#endif
|
||||
|
||||
}
|
||||
if (result) {
|
||||
|
||||
return new FILE_HANDLE (result);
|
||||
|
||||
size_t fread (void *ptr, size_t size, size_t count, FILE *stream) {
|
||||
}
|
||||
|
||||
return ::fread (ptr, size, count, stream);
|
||||
return NULL;
|
||||
|
||||
}
|
||||
|
||||
|
||||
int fseek (FILE *stream, long int offset, int origin) {
|
||||
size_t fread (void *ptr, size_t size, size_t count, FILE_HANDLE *stream) {
|
||||
|
||||
return ::fseek (stream, offset, origin);
|
||||
return ::fread (ptr, size, count, stream ? (FILE*)stream->handle : NULL);
|
||||
|
||||
}
|
||||
|
||||
|
||||
long int ftell (FILE *stream) {
|
||||
int fseek (FILE_HANDLE *stream, long int offset, int origin) {
|
||||
|
||||
return ::ftell (stream);
|
||||
return ::fseek (stream ? (FILE*)stream->handle : NULL, offset, origin);
|
||||
|
||||
}
|
||||
|
||||
|
||||
size_t fwrite (const void *ptr, size_t size, size_t count, FILE *stream) {
|
||||
long int ftell (FILE_HANDLE *stream) {
|
||||
|
||||
return ::fwrite (ptr, size, count, stream);
|
||||
return ::ftell (stream ? (FILE*)stream->handle : NULL);
|
||||
|
||||
}
|
||||
|
||||
|
||||
size_t fwrite (const void *ptr, size_t size, size_t count, FILE_HANDLE *stream) {
|
||||
|
||||
return ::fwrite (ptr, size, count, (FILE*)stream->handle);
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user