SDL_RWops * SDL_RWFromFile(const char *file, const char *mode) { SDL_RWops *rwops = NULL; if (!file || !*file || !mode || !*mode) { SDL_SetError("SDL_RWFromFile(): No file or no mode specified"); return NULL; } #if defined(ANDROID) rwops = SDL_AllocRW(); if (!rwops) return NULL; /* SDL_SetError already setup by SDL_AllocRW() */ if (Android_JNI_FileOpen(rwops, file, mode) < 0) { SDL_FreeRW(rwops); return NULL; } rwops->seek = Android_JNI_FileSeek; rwops->read = Android_JNI_FileRead; rwops->write = Android_JNI_FileWrite; rwops->close = Android_JNI_FileClose; #elif defined(__WIN32__) rwops = SDL_AllocRW(); if (!rwops) return NULL; /* SDL_SetError already setup by SDL_AllocRW() */ if (windows_file_open(rwops, file, mode) < 0) { SDL_FreeRW(rwops); return NULL; } rwops->seek = windows_file_seek; rwops->read = windows_file_read; rwops->write = windows_file_write; rwops->close = windows_file_close; #elif HAVE_STDIO_H { #ifdef __APPLE__ FILE *fp = SDL_OpenFPFromBundleOrFallback(file, mode); #else FILE *fp = fopen(file, mode); #endif if (fp == NULL) { SDL_SetError("Couldn't open %s", file); } else { rwops = SDL_RWFromFP(fp, 1); } } #else SDL_SetError("SDL not compiled with stdio support"); #endif /* !HAVE_STDIO_H */ return (rwops); }
SDL_RWops *getImage(void) { FILE* file; SDL_RWops *rw; if((file = popen("adb shell \"screencap -p | busybox uuencode -\" | uudecode","r"))==NULL) { fprintf(stderr, "can't open dump.png\n"); return 0; } rw = SDL_RWFromFP(file, 0); close(file); return rw; }
SDL_RWops *SDL_RWFromFile(const char *file, const char *mode) { SDL_RWops *rwops = NULL; #ifdef HAVE_STDIO_H FILE *fp = NULL; #endif if ( !file || !*file || !mode || !*mode ) { SDL_SetError("SDL_RWFromFile(): No file or no mode specified"); return NULL; } #if defined(__WIN32__) && !defined(__SYMBIAN32__) rwops = SDL_AllocRW(); if (!rwops) return NULL; /* SDL_SetError already setup by SDL_AllocRW() */ if (win32_file_open(rwops,file,mode) < 0) { SDL_FreeRW(rwops); return NULL; } rwops->seek = win32_file_seek; rwops->read = win32_file_read; rwops->write = win32_file_write; rwops->close = win32_file_close; #elif HAVE_STDIO_H #ifdef __MACOS__ { char *mpath = unix_to_mac(file); fp = fopen(mpath, mode); SDL_free(mpath); } #else fp = fopen(file, mode); #endif if ( fp == NULL ) { SDL_SetError("Couldn't open %s", file); } else { rwops = SDL_RWFromFP(fp, 1); } #else SDL_SetError("SDL not compiled with stdio support"); #endif /* !HAVE_STDIO_H */ return(rwops); }
void cargar_sonido ( char *fichero_wav, int identificador ){ /* Variables locales */ FILE *fichero; SDL_RWops *file; char filename[LON_BUFF]; SDL_AudioSpec wav_spec; Uint32 size; Uint8 *data; /* Generamos buffer, le asignamos un identificador y comprobamos errores */ alGenBuffers( 1, &buffer[identificador] ); if ( !alIsBuffer ( buffer[identificador] )){ log_msj ( "[KO] error al crear los buffers\n"); } /* Establecemos donde estara situado el directorio para los sonidos */ strcpy(filename, "sonidos"); strcat(filename, "/"); strcat(filename, fichero_wav); log_msj("[efectos.c] Cargando sonido %s\n", filename); /* Cargamos ficheros Wave */ fichero = abre_fichero(filename, "rb"); file = SDL_RWFromFP(fichero,0); if( SDL_LoadWAV_RW(file,1, &wav_spec,&data,&size)== NULL ){ log_msj("[KO]Error al cargar %s\n",filename); return; } alBufferData ( buffer[identificador], AL_FORMAT_STEREO16, data, size, wav_spec.freq ); /* Almacenamos en buffer */ SDL_FreeWAV(data);/*Liberamos*/ fclose(fichero); /* Generamos las fuentes de sonido y comprobamos errores */ alGenSources( 1, &source[identificador] ); if ( !alIsSource ( source[identificador])){ log_msj ("[KO] No se pueden crear las fuentes de sonido\n"); } /* Pasamos el archivo wav del buffer a la fuente */ alSourcei ( source[identificador], AL_BUFFER, buffer[identificador]); }
SDL_RWops* FastFileLock(struct FF_Handle *i) { if (!i) return NULL; #if defined HAVE_MMAP || defined _WIN32 char *buffer = NULL; if(!g_MemMap) return NULL; #endif #if defined HAVE_MMAP || defined _WIN32 buffer = (char*)g_MemMap; buffer += i->off; return SDL_RWFromMem(buffer, i->len); #else fseek(g_File, i->off, SEEK_SET); return SDL_RWFromFP(g_File, /*autoclose=*/0); #endif }
/** * @brief Tests writing to file handle * * \sa * http://wiki.libsdl.org/moin.cgi/SDL_RWFromFP * http://wiki.libsdl.org/moin.cgi/SDL_RWClose * */ int rwops_testFPWrite(void) { FILE *fp; SDL_RWops *rw; int result; /* Run write tests. */ fp = fopen(RWopsWriteTestFilename, "w+"); SDLTest_AssertCheck(fp != NULL, "Verify handle from opening file '%s' in write mode is not NULL", RWopsWriteTestFilename); /* Bail out if NULL */ if (fp == NULL) return TEST_ABORTED; /* Open */ rw = SDL_RWFromFP( fp, SDL_TRUE ); SDLTest_AssertPass("Call to SDL_RWFromFP() succeeded"); SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_RWFromFP in write mode does not return NULL"); /* Bail out if NULL */ if (rw == NULL) { fclose(fp); return TEST_ABORTED; } /* Check type */ SDLTest_AssertCheck( rw->type == SDL_RWOPS_STDFILE, "Verify RWops type is SDL_RWOPS_STDFILE; expected: %d, got: %d", SDL_RWOPS_STDFILE, rw->type); /* Run generic tests */ _testGenericRWopsValidations( rw, 1 ); /* Close handle - does fclose() */ result = SDL_RWclose(rw); SDLTest_AssertPass("Call to SDL_RWclose() succeeded"); SDLTest_AssertCheck(result == 0, "Verify result value is 0; got: %d", result); return TEST_COMPLETED; }
MPEG::MPEG(int Mpeg_FD, bool SDLaudio) : MPEGerror() { SDL_RWops *source; mpeg_mem = 0; // *** FIXME we're leaking a bit of memory for the FILE * // best solution would be to have SDL_RWFromFD FILE *file = fdopen(Mpeg_FD, "rb"); if (!file) { InitErrorState(); SetError(strerror(errno)); return; } source = SDL_RWFromFP(file,false); if (!source) { InitErrorState(); SetError(SDL_GetError()); return; } Init(source, SDLaudio); }
int main(int argc, char* argv[]) { SDL_Surface *screen = NULL; SDL_Surface *image = NULL; SDL_Surface *rot = NULL; SDL_RWops *rw = NULL; const SDL_VideoInfo *videoInfo = NULL; FILE* file; FILE* newFile; double scale; char buffer[256]; int value; int prevValue; /*-----------------------------------------------------------------*/ if (SDL_Init(SDL_INIT_EVERYTHING) < 0) { fprintf(stderr, "SDL_Init failed - %s\n", SDL_GetError()); return 1; } /*-----------------------------------------------------------------*/ videoInfo = SDL_GetVideoInfo(); if (videoInfo == 0) { fprintf(stderr, "SDL_GetVideoInfo failed - %s\n", SDL_GetError()); SDL_Quit(); return 1; } /*-----------------------------------------------------------------*/ if((file = popen("adb shell \"screencap -p | busybox uuencode -\" | uudecode","r"))==NULL) { fprintf(stderr, "can't open dump.png\n"); return 0; } rw = SDL_RWFromFP(file, 0); close(file); image = IMG_LoadPNG_RW(rw); SDL_FreeRW(rw); //scale = (image->h)/(videoInfo->current_h); scale = 0.5; /*-----------------------------------------------------------------*/ screen = SDL_SetVideoMode((image->w)*scale, (image->h)*scale, videoInfo->vfmt->BitsPerPixel, SDL_SWSURFACE|SDL_DOUBLEBUF); if (!screen) { fprintf(stderr, "SetVideoMode failed - %s\n", SDL_GetError()); SDL_FreeSurface(image); SDL_Quit(); return 1; } rot = rotozoomSurface( image, 0, scale, SMOOTHING_ON ); if (!image) { fprintf(stderr, "IMG_LoadBMP_RW failed - %s\n", IMG_GetError()); SDL_Quit(); return 1; } SDL_BlitSurface(rot, 0, screen, 0); SDL_Flip(screen); SDL_FreeSurface(rot); SDL_FreeSurface(image); /*-----------------------------------------------------------------*/ while(1){ if((file = popen("adb shell dumpsys window | grep cur= | awk -F cur= '{print $NF}' | awk -F ' ' '{print $1}' | awk -F 'x' '{print $NF}'", "r"))==NULL) { fprintf(stderr, "can't get size\n"); return 0; } while(fgets(buffer, sizeof(buffer), file) != 0) { value = atoi(buffer); } pclose(file); if((file = popen("adb shell \"screencap -p | busybox uuencode -\" | uudecode","r"))==NULL) { fprintf(stderr, "can't open dump.png\n"); return 0; } rw = SDL_RWFromFP(file, 0); close(file); image = IMG_LoadPNG_RW(rw); SDL_FreeRW(rw); if (value != image->h) { if (prevValue != image->h) { screen = SDL_SetVideoMode((image->h)*scale, (image->w)*scale, videoInfo->vfmt->BitsPerPixel, SDL_SWSURFACE|SDL_DOUBLEBUF); } rot = rotozoomSurface( image, 270, scale, SMOOTHING_ON ); } if (value == image->h) { if (prevValue != image->h) { screen = SDL_SetVideoMode((image->w)*scale, (image->h)*scale, videoInfo->vfmt->BitsPerPixel, SDL_SWSURFACE|SDL_DOUBLEBUF); } rot = rotozoomSurface( image, 0, scale, SMOOTHING_ON ); } if (!image) { fprintf(stderr, "IMG_LoadBMP_RW failed - %s\n", IMG_GetError()); SDL_Quit(); return 1; } SDL_FreeSurface(image); SDL_BlitSurface(rot, NULL, screen, 0); SDL_Flip(screen); SDL_FreeSurface(rot); prevValue = value; } //SDL_Delay(5000); SDL_Quit(); return 0; }
FILE_HANDLE *fopen (const char *filename, const char *mode) { #ifndef HX_WINDOWS 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 = SDL_RWFromFP (::fopen (buffer, "rb"), SDL_TRUE); free (buffer); } CFRelease (str); CFRelease (path); } } #else result = SDL_RWFromFile (filename, mode); #endif if (result) { return new FILE_HANDLE (result); } return NULL; #else FILE* result = ::fopen (filename, mode); if (result) { return new FILE_HANDLE (result); } return NULL; #endif }
SDL_RWops * SDL_RWFromFile(const char *file, const char *mode) { SDL_RWops *rwops = NULL; if (!file || !*file || !mode || !*mode) { SDL_SetError("SDL_RWFromFile(): No file or no mode specified"); return NULL; } #if defined(ANDROID) #ifdef HAVE_STDIO_H /* Try to open the file on the filesystem first */ if (*file == '/') { FILE *fp = fopen(file, mode); if (fp) { return SDL_RWFromFP(fp, 1); } } else { /* Try opening it from internal storage if it's a relative path */ char *path; FILE *fp; path = SDL_stack_alloc(char, PATH_MAX); if (path) { SDL_snprintf(path, PATH_MAX, "%s/%s", SDL_AndroidGetInternalStoragePath(), file); fp = fopen(path, mode); SDL_stack_free(path); if (fp) { return SDL_RWFromFP(fp, 1); } } } #endif /* HAVE_STDIO_H */ /* Try to open the file from the asset system */ rwops = SDL_AllocRW(); if (!rwops) return NULL; /* SDL_SetError already setup by SDL_AllocRW() */ if (Android_JNI_FileOpen(rwops, file, mode) < 0) { SDL_FreeRW(rwops); return NULL; } rwops->size = Android_JNI_FileSize; rwops->seek = Android_JNI_FileSeek; rwops->read = Android_JNI_FileRead; rwops->write = Android_JNI_FileWrite; rwops->close = Android_JNI_FileClose; #elif defined(__WIN32__) rwops = SDL_AllocRW(); if (!rwops) return NULL; /* SDL_SetError already setup by SDL_AllocRW() */ if (windows_file_open(rwops, file, mode) < 0) { SDL_FreeRW(rwops); return NULL; } rwops->size = windows_file_size; rwops->seek = windows_file_seek; rwops->read = windows_file_read; rwops->write = windows_file_write; rwops->close = windows_file_close; #elif HAVE_STDIO_H { #ifdef __APPLE__ FILE *fp = SDL_OpenFPFromBundleOrFallback(file, mode); #else FILE *fp = fopen(file, mode); #endif if (fp == NULL) { SDL_SetError("Couldn't open %s", file); } else { rwops = SDL_RWFromFP(fp, 1); } } #else SDL_SetError("SDL not compiled with stdio support"); #endif /* !HAVE_STDIO_H */ return (rwops); }
bool font_init(void) { bool error = false; // we'll be bypassing the NXSurface automatic scaling features // and drawing at the real resolution so we can get better-looking fonts. // sdl_screen = screen->GetSDLSurface(); // at 320x240 switch to bitmap fonts for better appearance #ifdef CONFIG_ENABLE_TTF if (SCALE == 1) #endif { stat("fonts: using bitmapped from %s", bmpfontfile); SDL_RWops* rwops = fileopen_SDL_RWops_RO(bmpfontfile); if (!rwops) { staterr("Couldn't open bitmap font file %s: SDL_RWFromFP: %s", bmpfontfile, SDL_GetError()); return 1; } SDL_Surface *sheet = SDL_LoadBMP_RW(rwops, 1); if (!sheet) { staterr("Couldn't open bitmap font file: SDL_LoadBMP_RW: %s", SDL_GetError()); return 1; } uint32_t fgindex = SDL_MapRGB(sheet->format, 255, 255, 255); error = error || whitefont.InitBitmapChars(sheet, fgindex, 0xffffff); error = error || greenfont.InitBitmapChars(sheet, fgindex, 0x00ff80); error = error || bluefont.InitBitmapChars(sheet, fgindex, 0xa0b5de); error = error || shadowfont.InitBitmapCharsShadowed(sheet, fgindex, 0xffffff, 0x000000); } #ifdef CONFIG_ENABLE_TTF else { // It will get size 8, 17, 26, 35, ... // Hope ot will look nice on higher resolutions int pointsize = 8 + 9 * (SCALE - 1); stat("fonts: using truetype at %dpt", pointsize); // initilize normal TTF fonts if (TTF_Init() < 0) { staterr("Couldn't initialize SDL_ttf: %s", TTF_GetError()); return 1; } TTF_Font *font = TTF_OpenFontRW(SDL_RWFromFP(fileopenRO(ttffontfile), SDL_TRUE), 1, pointsize); if (!font) { staterr("Couldn't open font: '%s'", ttffontfile); return 1; } error = error || whitefont.InitChars(font, 0xffffff); error = error || greenfont.InitChars(font, 0x00ff80); error = error || bluefont.InitChars(font, 0xa0b5de); error = error || shadowfont.InitCharsShadowed(font, 0xffffff, 0x000000); TTF_CloseFont(font); } #endif error = error || whitefont.InitTextures(); error = error || greenfont.InitTextures(); error = error || bluefont.InitTextures(); error = error || shadowfont.InitTextures(); error = error || create_shade_sfc(); if (error) return 1; fontheight = (whitefont.letters['M']->h / SCALE); initilized = true; return 0; }
bool SDLRWops::fromFP(void * fp, bool autoclose) #endif { rwops_ = SDL_RWFromFP(fp, (autoclose ? SDL_TRUE : SDL_FALSE)); return (rwops_ != NULL); }
// load the surface from a .pbm or bitmap file bool NXSurface::LoadImage(const char *pbm_name, bool use_colorkey, int use_display_format) { stat("NXSurface::LoadImage name = %s, this = %p", pbm_name, this); SDL_Surface *image; Free(); // if (use_display_format == -1) // { // use value specified in settings // use_display_format = settings->displayformat; // } image = SDL_LoadBMP_RW(SDL_RWFromFP(fileopenRO(pbm_name), SDL_TRUE), 1); if (!image) { staterr("NXSurface::LoadImage: load failed of '%s'!", pbm_name); return 1; } if (use_colorkey) { SDL_SetColorKey(image, SDL_TRUE, SDL_MapRGB(image->format, 0, 0, 0)); } SDL_Texture * tmptex = SDL_CreateTextureFromSurface(renderer, image); if (!tmptex) { staterr("NXSurface::LoadImage: SDL_CreateTextureFromSurface failed: %s", SDL_GetError()); SDL_FreeSurface(image); return 1; } SDL_FreeSurface(image); { int wd, ht, access; Uint32 format; NXFormat nxformat; if (SDL_QueryTexture(tmptex, &format, &access, &wd, &ht)) goto error; nxformat.format = format; if (AllocNew(wd, ht, &nxformat)) goto error; if (SDL_SetTextureBlendMode(tmptex, SDL_BLENDMODE_MOD)) goto error; if (SDL_SetRenderTarget(renderer, fTexture)) goto error; if (SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255)) goto error; if (SDL_RenderClear(renderer)) goto error; if (SDL_RenderCopy(renderer, tmptex, NULL, NULL)) goto error; if (SDL_SetRenderTarget(renderer, NULL)) goto error; if (SDL_SetTextureBlendMode(fTexture, SDL_BLENDMODE_BLEND)) goto error; SDL_DestroyTexture(tmptex); goto done; error: { staterr("NXSurface::LoadImage failed: %s", SDL_GetError()); if (tmptex) { SDL_DestroyTexture(tmptex); tmptex = NULL; } if (fTexture){ SDL_DestroyTexture(fTexture); fTexture = NULL; } SDL_SetRenderTarget(renderer, NULL); } done: ; } stat("NXSurface::LoadImage name = %s, this = %p done", pbm_name, this); return (fTexture == NULL); }
SDL_RWops *SDL_RWFromFile(const char *file, const char *mode) { SDL_RWops *rwops = NULL; #if defined(__DREAMCAST__) file_t fd; int dc_mode = 0; #elif defined(_HAVE_STDIO_H) //#ifdef HAVE_STDIO_H FILE *fp = NULL; #endif if ( !file || !*file || !mode || !*mode ) { SDL_SetError("SDL_RWFromFile(): No file or no mode specified"); return NULL; } #if defined(__WIN32__) && !defined(__SYMBIAN32__) rwops = SDL_AllocRW(); if (!rwops) return NULL; /* SDL_SetError already setup by SDL_AllocRW() */ if (win32_file_open(rwops,file,mode) < 0) { SDL_FreeRW(rwops); return NULL; } rwops->seek = win32_file_seek; rwops->read = win32_file_read; rwops->write = win32_file_write; rwops->close = win32_file_close; #elif defined(HAVE_STDIO_H) #ifdef __DREAMCAST__ if(SDL_strchr(mode,'r') != NULL) { dc_mode = O_RDONLY; } if(SDL_strchr(mode,'w') != NULL) { if(dc_mode & O_RDONLY) { dc_mode &= ~O_RDONLY; dc_mode = (O_RDWR | O_CREAT | O_TRUNC); } else { dc_mode = (O_WRONLY | O_CREAT | O_TRUNC); } } if(SDL_strchr(mode,'a') != NULL) { if(!(dc_mode & O_RDONLY)) dc_mode |= (O_WRONLY | O_APPEND); } if(SDL_strchr(mode,'+') != NULL) { if((dc_mode & O_WRONLY) || (dc_mode & O_RDWR)) { dc_mode &= ~(O_CREAT | O_TRUNC); } else { dc_mode |= O_RDONLY; } } fd = fs_open(file, dc_mode); if ( fd == FILEHND_INVALID ) { SDL_SetError("Couldn't open %s", file); } else { rwops = SDL_RWFromFD(fd, 1); } #else #ifdef __MACOS__ { char *mpath = unix_to_mac(file); fp = fopen(mpath, mode); SDL_free(mpath); } #else fp = fopen(file, mode); #endif if ( fp == NULL ) { SDL_SetError("Couldn't open %s", file); } else { rwops = SDL_RWFromFP(fp, 1); } #endif /* __DREAMCAST__ */ #else SDL_SetError("SDL not compiled with stdio support"); #endif /* !HAVE_STDIO_H */ return(rwops); }
int main(int argc, char* argv[]) { SDL_Surface *screen = NULL; SDL_Surface *image = NULL; const SDL_VideoInfo *videoInfo = NULL; FILE* file; /*-----------------------------------------------------------------*/ if (SDL_Init(SDL_INIT_EVERYTHING) < 0) { fprintf(stderr, "SDL_Init failed - %s\n", SDL_GetError()); return 1; } /*-----------------------------------------------------------------*/ videoInfo = SDL_GetVideoInfo(); if (videoInfo == 0) { fprintf(stderr, "SDL_GetVideoInfo failed - %s\n", SDL_GetError()); SDL_Quit(); return 1; } /*-----------------------------------------------------------------*/ screen = SDL_SetVideoMode(image->w, image->h, videoInfo->vfmt->BitsPerPixel, SDL_SWSURFACE); if (!screen) { fprintf(stderr, "SetVideoMode failed - %s\n", SDL_GetError()); SDL_FreeSurface(image); SDL_Quit(); return 1; } /*-----------------------------------------------------------------*/ while(1) { if((file = popen("adb shell \"screencap -p | busybox uuencode -\" |uudecode","r"))==NULL) { fprintf(stderr, "can't open dump.png\n"); return 0; } SDL_RWops *rw = SDL_RWFromFP(file, 0); image = IMG_LoadPNG_RW(rw); SDL_FreeRW(rw); close(file); if (!image) { fprintf(stderr, "IMG_LoadBMP_RW failed - %s\n", IMG_GetError()); SDL_Quit(); return 1; } /*-----------------------------------------------------------------*/ SDL_BlitSurface(image, 0, screen, 0); SDL_Flip(screen); //SDL_Delay(5000); SDL_FreeSurface(image); } SDL_Quit(); return 0; }
File::File( FILE *fp ) { this->fp = fp; this->rwops = SDL_RWFromFP( fp, 1 ); }