Mix_Chunk *CSoundHandler::GetSoundChunk(std::string &sound) { if (sound.empty()) return nullptr; // Load and insert try { auto data = CResourceHandler::get()->load(ResourceID(std::string("SOUNDS/") + sound, EResType::SOUND))->readAll(); SDL_RWops *ops = SDL_RWFromMem(data.first.release(), data.second); Mix_Chunk *chunk; chunk = Mix_LoadWAV_RW(ops, 1); // will free ops return chunk; } catch(std::exception &e) { logGlobal->warnStream() << "Cannot get sound " << sound << " chunk: " << e.what(); return nullptr; } }
void SpriteFile::readFile(void) { SpriteFileHeader header; SpriteImageMarker marker; std::ifstream is(path.c_str(), std::ifstream::binary); //first we read SpriteFileHeader is.read((char*) &header, sizeof(SpriteFileHeader)); nbFrames = header.nbFrames; name = header.spriteName; width = header.width; height = header.height; for(int i=0; i<nbFrames; i++) { //reading marker for length is.read( (char*) &marker, sizeof(SpriteImageMarker)); int frame_length = marker.length; char * buffer = new char[frame_length]; is.read(buffer, frame_length); //creating surface from char array SDL_RWops *rw = SDL_RWFromMem(buffer, frame_length); SDL_Surface *surface = IMG_Load_RW(rw, 1); //1 to release/free RWops std::cout << IMG_GetError() << std::endl; //exceptio here, ... todo delete buffer; surfaces.push_back(surface); } is.close(); }
/** * \brief Function called by the Lua data file to add a font. * * - Argument 1 (table): properties of the font. * * \param l the Lua context that is calling this function * \return number of values to return to Lua */ int TextSurface::l_font(lua_State* l) { luaL_checktype(l, 1, LUA_TTABLE); const std::string& font_id = LuaContext::check_string_field(l, 1, "id"); const std::string& file_name = LuaContext::check_string_field(l, 1, "file"); int font_size = LuaContext::opt_int_field(l, 1, "size", 11); bool is_default = LuaContext::opt_boolean_field(l, 1, "default", false); fonts[font_id].file_name = file_name; fonts[font_id].font_size = font_size; if (is_default || default_font_id.empty()) { default_font_id = font_id; } // Load the font. size_t index = file_name.rfind('.'); std::string extension; if (index != std::string::npos) { extension = file_name.substr(index); } if (extension == ".png" || extension == ".PNG") { // It's a bitmap font. fonts[font_id].bitmap = new Surface(file_name, Surface::DIR_DATA); } else { // It's a normal font. size_t size; FileTools::data_file_open_buffer(file_name, &fonts[font_id].buffer, &size); fonts[font_id].bitmap = NULL; fonts[font_id].rw = SDL_RWFromMem(fonts[font_id].buffer, int(size)); fonts[font_id].internal_font = TTF_OpenFontRW(fonts[font_id].rw, 0, font_size); Debug::check_assertion(fonts[font_id].internal_font != NULL, StringConcat() << "Cannot load font from file '" << file_name << "': " << TTF_GetError()); } return 0; }
void broov_gui_init_audio_image(int audio_image_idx) { broov_gui_clean_audio_image(); g_audio_image_idx = audio_image_idx; switch (audio_image_idx) { case BROOV_LOADING_IMAGE: default: rw4 = SDL_RWFromMem(bg_loading, bg_loading_len); break; } if (rw4) { char image[] = "JPG"; SDL_Surface *backgroundImage = IMG_LoadTyped_RW(rw4, 1, image); if (backgroundImage) { backgroundTexture = SDL_CreateTextureFromSurface(0, backgroundImage); } } }
SDL_Surface * Lux_OGL_LoadSpriteImage(std::string file) { uint8_t * data = NULL; uint32_t size; SDL_Surface * temp_surface = NULL; if ( lux::game ) { size = lux::game->GetFile(file, &data, false); if ( size ) { SDL_RWops * src = SDL_RWFromMem(data, size); if ( IMG_isPNG(src) ) { temp_surface = IMG_Load_RW(src, 1); SDL_SetAlpha(temp_surface, 0, 255); } else lux::core->SystemMessage(SYSTEM_MESSAGE_LOG) << "not a png image" << std::endl; } } return temp_surface; }
Mix_Chunk *loadSoundFromPak(char *name) { unsigned long size; unsigned char *buffer; SDL_RWops *rw; Mix_Chunk *chunk; if (existsInPak(name) == FALSE) { return NULL; } buffer = uncompressFileRW(name, &size); rw = SDL_RWFromMem(buffer, size); chunk = Mix_LoadWAV_RW(rw, TRUE); free(buffer); return chunk; }
void WindowManager::setWindowIcon(const unsigned char *data, const int size) const { // prepare data buffer structure SDL_RWops *buffer = SDL_RWFromMem((void*) data, size); if(buffer != NULL) { // load BMP from prepared data buffer SDL_Surface *surface = SDL_LoadBMP_RW(buffer, 1); if(surface != NULL) { // set window icon SDL_WM_SetIcon(surface, NULL); SDL_FreeSurface(surface); } else { cerr << "Could not create window icon surface: " << SDL_GetError() << endl; } } else { cerr << "Could not prepare window icon data: " << SDL_GetError() << endl; } }
int load_font(char *fname, int size) { char *p; free_font(); font = TTF_OpenFont(fname, size); if (!font) { //Try to open the in memory font SDL_RWops *s; s = SDL_RWFromMem(dejavu_sans_ttf, dejavu_sans_ttf_len); font = TTF_OpenFontRW(s, 0, size); if (font) { __android_log_print(ANDROID_LOG_INFO, "BroovPlayer", "Open font from memory successful"); } else { __android_log_print(ANDROID_LOG_INFO, "BroovPlayer", "Open font from memory failed:%s", TTF_GetError()); return 3; } } /* print some metrics and attributes */ //printf("size : %d\n",size); //printf("TTF_FontHeight : %d\n",TTF_FontHeight(font)); //printf("TTF_FontAscent : %d\n",TTF_FontAscent(font)); //printf("TTF_FontDescent : %d\n",TTF_FontDescent(font)); //printf("TTF_FontLineSkip : %d\n",TTF_FontLineSkip(font)); //printf("TTF_FontFaceIsFixedWidth: %d\n",TTF_FontFaceIsFixedWidth(font)); /* cache new glyphs */ cache_glyphs(); return 0; }
struct s_object *f_bitmap_new(struct s_object *self, struct s_object *stream, struct s_object *environment) { struct s_bitmap_attributes *attributes = p_bitmap_alloc(self); struct s_drawable_attributes *drawable_attributes = d_cast(self, drawable); struct s_stream_attributes *stream_attributes = d_cast(stream, stream); struct s_environment_attributes *environment_attributes = d_cast(environment, environment); char *memblock, buffer[d_string_buffer_size]; struct stat file_stats; int width, height; SDL_RWops *surfaced_block; SDL_Surface *unoptimized_surface; fstat(stream_attributes->descriptor, &file_stats); if ((memblock = mmap(NULL, file_stats.st_size, PROT_READ, MAP_SHARED, stream_attributes->descriptor, 0)) != MAP_FAILED) { surfaced_block = SDL_RWFromMem(memblock, file_stats.st_size); if ((unoptimized_surface = IMG_Load_RW(surfaced_block, d_true))) { attributes->image = SDL_CreateTextureFromSurface(environment_attributes->renderer, unoptimized_surface); if (SDL_QueryTexture(attributes->image, NULL, NULL, &width, &height) == 0) { d_call(&(drawable_attributes->point_dimension), m_point_set_x, (double)width); d_call(&(drawable_attributes->point_dimension), m_point_set_y, (double)height); d_call(&(drawable_attributes->point_center), m_point_set_x, (double)(width/2.0)); d_call(&(drawable_attributes->point_center), m_point_set_y, (double)(height/2.0)); } else { snprintf(buffer, d_string_buffer_size, "unable to retrieve informations for bitmap %s exception", d_string_cstring(stream_attributes->string_name)); d_throw(v_exception_texture, buffer); } } else { snprintf(buffer, d_string_buffer_size, "ungenerable texture for bitmap %s exception", d_string_cstring(stream_attributes->string_name)); d_throw(v_exception_texture, buffer); } SDL_FreeSurface(unoptimized_surface); munmap(memblock, file_stats.st_size); } else { snprintf(buffer, d_string_buffer_size, "wrong type for file %s exception", d_string_cstring(stream_attributes->string_name)); d_throw(v_exception_wrong_type, buffer); } return self; }
void Audio::playVoice(std::string file) { if (chunk_voice != nullptr) { Mix_FreeChunk(chunk_voice); delete[] buffer_voice; chunk_voice=nullptr; } //Read voice uint32_t size=0; buffer_voice = sr->getResourceManager()->getFile("sound",file,size); if (buffer_voice == nullptr) { LOG("Missing file '" + file + "'"); return; } SDL_RWops* rw = SDL_RWFromMem(buffer_voice,size); if (rw==0) ERROR("SDL_RWFromMem failed: "+std::string(SDL_GetError())); chunk_voice = Mix_LoadWAV_RW(rw,1); if (chunk_voice==0) ERROR("SDL_LoadWAV_RW failed: "+std::string(SDL_GetError())); //Play voice Mix_PlayChannel(0,chunk_voice,0); }
void Audio::playBGM(std::string file) { if (music != nullptr) { Mix_FreeMusic(music); delete[] buffer_music; music=nullptr; } //Read music uint32_t size=0; buffer_music= sr->getResourceManager()->getFile("music",file,size); if (buffer_music == nullptr) { LOG("Missing file '" + file + "'"); return; } SDL_RWops* rw = SDL_RWFromMem(buffer_music,size); if (rw==0) ERROR("SDL_RWFromMem failed: "+std::string(SDL_GetError())); music = Mix_LoadMUS_RW(rw,1); if (music==0) ERROR("SDL_LoadMUS_RW failed: "+std::string(SDL_GetError())); //Play music Mix_PlayMusic(music,-1); }
//Graphics void Operation::image_set(ScenarioRunner* sr) { uint32_t id = sr->getDWORD(),size=0; char* buffer = sr->getResourceManager()->getFile("graphics",sr->getString(),size); SDL_RWops* rw = SDL_RWFromMem(buffer,size); if (rw==0) ERROR("SDL_RWFromMem failed: "+string(SDL_GetError())); Layer layer; layer.surface = IMG_LoadPNG_RW(rw); if (layer.surface==0) ERROR("IMG_LoadPNG_RW failed: "+string(SDL_GetError())); layer.rect.x = sr->getDWORD(); layer.rect.y = sr->getDWORD(); layer.rect.w = layer.surface->w; layer.rect.h = layer.surface->h; if (sr->getDWORD()) sr->getWindow()->setLayer(id,layer); sr->getDWORD(); sr->getDWORD(); }
Mix_Chunk *Resource::GetSound ( char const *_filename ) { char buffer[2048]; MemMappedFile *memmap = NULL; Mix_Chunk *chunk = NULL; if (!chunk) { const Data::LList<char *> *resourcePaths = g_app->GetResourcePaths(); for (size_t i = 0; i < resourcePaths->size() && !chunk; i++) { sprintf(buffer, "%s%s", resourcePaths->get(i), _filename); chunk = Mix_LoadWAV(buffer); } } memmap = GetUncompressedFile ( _filename ); if ( !chunk && memmap ) { SDL_RWops *data = SDL_RWFromMem ( memmap->m_data, memmap->m_size ); chunk = Mix_LoadWAV_RW ( data, 0 ); } return chunk; }
/** * @brief Tests opening from memory. */ static void rwops_testMem (void) { char mem[sizeof(hello_world)]; SDL_RWops *rw; /* Begin testcase. */ SDL_ATbegin( "SDL_RWFromMem" ); /* Open. */ rw = SDL_RWFromMem( mem, sizeof(hello_world)-1 ); if (SDL_ATassert( "Opening memory with SDL_RWFromMem", rw != NULL )) return; /* Run generic tests. */ if (rwops_testGeneric( rw, 1 )) return; /* Close. */ SDL_FreeRW( rw ); /* End testcase. */ SDL_ATend(); }
/** * \brief Creates a surface from the specified image file name. * * This function acts like a constructor excepts that it returns NULL if the * file does not exist or is not a valid image. * * \param file_name Name of the image file to load, relative to the base directory specified. * \param base_directory The base directory to use. * \return The surface created, or NULL if the file could not be loaded. */ Surface* Surface::create_from_file(const std::string& file_name, ImageDirectory base_directory) { std::string prefix; bool language_specific = false; if (base_directory == DIR_SPRITES) { prefix = "sprites/"; } else if (base_directory == DIR_LANGUAGE) { language_specific = true; prefix = "images/"; } std::string prefixed_file_name = prefix + file_name; if (!FileTools::data_file_exists(prefixed_file_name, language_specific)) { // File not found. return NULL; } size_t size; char* buffer; FileTools::data_file_open_buffer(prefixed_file_name, &buffer, &size, language_specific); SDL_RWops* rw = SDL_RWFromMem(buffer, int(size)); SDL_Surface* internal_surface = IMG_Load_RW(rw, 0); FileTools::data_file_close_buffer(buffer); SDL_RWclose(rw); if (internal_surface == NULL) { // Not a valid image. return NULL; } Surface* surface = new Surface(internal_surface); surface->owns_internal_surface = true; return surface; }
void Operation::layer_set(ScenarioRunner* sr) { uint32_t id = sr->getDWORD(),size; std::string file = sr->getString(); char* buffer = sr->getResourceManager()->getFile("graphics",file,size); if (buffer == nullptr) { LOG("Missing file '" + file + "'"); for (int i = 0; i < 5; i++) sr->getDWORD(); return; } SDL_RWops* rw = SDL_RWFromMem(buffer,size); if (rw==0) ERROR("SDL_RWFromMem failed: "+std::string(SDL_GetError())); Layer layer; layer.surface = IMG_LoadPNG_RW(rw); SDL_RWclose(rw); delete[] buffer; if (layer.surface==0) ERROR("IMG_LoadPNG_RW failed: "+std::string(SDL_GetError())); //Layer coordinates layer.rect.x = sr->getDWORD(); layer.rect.y = sr->getDWORD(); layer.rect.w = layer.surface->w; layer.rect.h = layer.surface->h; //Layer visibility layer.visible = (sr->getDWORD() ? true : false); sr->getWindow()->setLayer(id,layer); sr->getDWORD(); //Unknown parameter sr->getDWORD(); //Unknown parameter }
void Engine::Texture2D::loadFromAsset(const GLchar *asset) { Tools::AssetRessource assetRessource = Tools::openAsset(asset); SDL_RWops *rw = SDL_RWFromMem(assetRessource.buffer, assetRessource.size); Tools::closeAsset(assetRessource); SDL_Surface *image = IMG_Load_RW(rw, 1); if (image == nullptr) { ALOGE("Error while loading image: %s", asset); throw std::exception(); } if (glIsTexture(_idTexture)) glDeleteTextures(1, &_idTexture); glGenTextures(1, &_idTexture); glBindTexture(GL_TEXTURE_2D, _idTexture); switch (image->format->format) { case 386930691: glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image->w, image->h, 0, GL_RGB, GL_UNSIGNED_BYTE, image->pixels); break; case 376840196: glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image->w, image->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels); break; default: ALOGE("%s: Pixel format unsupported(%d)", asset, image->format->format); throw std::exception(); } glGenerateMipmap(GL_TEXTURE_2D); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); SDL_FreeSurface(image); }
// Allocate an SDL chunk and cache it. Mix_Chunk *CSoundHandler::GetSoundChunk(soundBase::soundID soundID) { // Find its name auto fname = sounds[soundID]; if (fname.empty()) return nullptr; // Load and insert try { auto data = CResourceHandler::get()->load(ResourceID(std::string("SOUNDS/") + fname, EResType::SOUND))->readAll(); SDL_RWops *ops = SDL_RWFromMem(data.first.release(), data.second); Mix_Chunk *chunk; chunk = Mix_LoadWAV_RW(ops, 1); // will free ops soundChunks.insert(std::pair<soundBase::soundID, Mix_Chunk *>(soundID, chunk)); return chunk; } catch(std::exception &e) { logGlobal->warnStream() << "Cannot get sound " << soundID << " chunk: " << e.what(); return nullptr; } }
/** * \brief Returns the SDL_Surface corresponding to the requested file. * * The returned SDL_Surface has to be manually deleted. * * \param file_name Name of the image file to load, relative to the base directory specified. * \param base_directory The base directory to use. * \return The SDL_Surface. */ SDL_Surface* Surface::get_surface_from_file( const std::string& file_name, ImageDirectory base_directory) { std::string prefix; bool language_specific = false; if (base_directory == DIR_SPRITES) { prefix = "sprites/"; } else if (base_directory == DIR_LANGUAGE) { language_specific = true; prefix = "images/"; } std::string prefixed_file_name = prefix + file_name; if (!FileTools::data_file_exists(prefixed_file_name, language_specific)) { // File not found. return NULL; } size_t size; char* buffer; FileTools::data_file_open_buffer(prefixed_file_name, &buffer, &size, language_specific); SDL_RWops* rw = SDL_RWFromMem(buffer, int(size)); SDL_Surface* software_surface = IMG_Load_RW(rw, 0); SDL_RWclose(rw); FileTools::data_file_close_buffer(buffer); Debug::check_assertion(software_surface != NULL, std::string("Cannot load image '") + prefixed_file_name + "'"); return software_surface; }
TTF_Font *read_and_alloc_font(const char *path, int pt_size) { TTF_Font *out; SDL_RWops *rw; Uint8 *data; FILE *fp = fopen(path, "r"); size_t r; if (!fp) { fprintf(stderr, "Could not open font %s\n", path); return NULL; } data = (Uint8*)xmalloc(1 * 1024*1024); r = fread(data, 1, 1 * 1024 * 1024, fp); if (r == 0 || ferror(fp)) { free(data); return NULL; } rw = SDL_RWFromMem(data, 1 * 1024 * 1024); if (!rw) { fprintf(stderr, "Could not create RW: %s\n", SDL_GetError()); free(data); return NULL; } out = TTF_OpenFontRW(rw, 1, pt_size); if (!out) { fprintf(stderr, "TTF: Unable to create font %s (%s)\n", path, TTF_GetError()); } fclose(fp); return out; }
bool Image::load_memory( const char* buffer, int buffer_size, const std::string& ext, uint32 pixel_format ) { SDL_SURFACE::SharedPtr surface; if( buffer == nullptr ) { NOM_LOG_ERR( NOM, "Cannot load image source from memory: buffer == nullptr" ); return false; } if( buffer_size == 0 ) { NOM_LOG_WARN( NOM, "Cannot load image source from memory: buffer_size == 0" ); return false; } surface.reset( IMG_LoadTyped_RW( SDL_RWFromMem( (char*) buffer, buffer_size), 1, ext.c_str() ), priv::FreeSurface ); if( surface != nullptr ) { this->image_.reset( SDL_ConvertSurfaceFormat( surface.get(), pixel_format, 0), priv::FreeSurface ); return true; } else { NOM_LOG_ERR( NOM, IMG_GetError() ); return false; } }
SDL_Surface *Resource::GetImage ( char const *_filename ) { char buffer[2048]; MemMappedFile *memmap = NULL; SDL_Surface *surf = NULL; if (!surf) { const Data::LList<char *> *resourcePaths = g_app->GetResourcePaths(); for (size_t i = 0; i < resourcePaths->size() && !surf; i++) { sprintf(buffer, "%s%s", resourcePaths->get(i), _filename); surf = IMG_Load(buffer); } } memmap = GetUncompressedFile ( _filename ); if ( !surf && memmap ) { SDL_RWops *data = SDL_RWFromMem ( memmap->m_data, memmap->m_size ); surf = IMG_Load_RW ( data, 0 ); ARCReleaseAssert ( surf ); } return surf; }
bool CSound::LoadSample (ESample Sample, int ResourceID) { SDL_RWops *rwSample; // Check if the sample slot is free ASSERT (m_Samples[Sample] == NULL); LPVOID pData; DWORD DataSize; // If we could not get the sound resource information if (!GetSoundResource (ResourceID, pData, DataSize)) { // Get out return false; } // Convert pData to SDL_RWops rwSample = SDL_RWFromMem(pData, DataSize); // Open Sample m_Samples[Sample] = Mix_LoadWAV_RW(rwSample, 0); SDL_FreeRW(rwSample); if (!m_Samples[Sample]) { // Log failure theLog.WriteLine ("Sound => !!! Could not open sample %d because %s", ResourceID, Mix_GetError()); // Get out return false; } // Everything went right return true; }
int AUDs_LoadWaveFile( int num ) { if (SoundDisabled) return AUD_RETURN_SUCCESS; ASSERT( ( num >= 0 ) && ( num < MAX_SAMPLES ) ); if ( !SoundAvailable ) return AUD_RETURN_SUCCESS; // read the sample and assign the list entry sample_s* pSample = SND_ReadSample( &SampleInfo[ num ] ); SND_ConvertRate( pSample, aud_sample_quality ); // because SDL_RWFromFP doesn't work cross platform, we need // to get a little bit kinky // set up a buffer for the file char *tmp_sample_buffer = NULL; size_t tmp_sample_size = 0; // get the size of this sample tmp_sample_size = SYS_GetFileLength(SampleInfo[num].file); if (tmp_sample_size <= 0) { return FALSE; // return on error } // alloc space for the buffer tmp_sample_buffer = (char *)ALLOCMEM(tmp_sample_size + 1); // open the sample file FILE *wave = SYS_fopen(SampleInfo[num].file, "rb"); if (wave == NULL) return FALSE; // read the file into the temp_sample_buffer int read_rc = SYS_fread((void *)tmp_sample_buffer, 1, tmp_sample_size, wave); if (read_rc <= 0) { MSGOUT("ERROR: Error reading sample %s.", SampleInfo[num].file); return FALSE; } SDL_RWops *waverwops = SDL_RWFromMem((void *)tmp_sample_buffer, tmp_sample_size); snd_chunks[num] = Mix_LoadWAV_RW(waverwops, 0); SYS_fclose(wave); FREEMEM( tmp_sample_buffer ); FREEMEM( pSample->samplebuffer ); pSample->samplebuffer = NULL; #ifdef SHOW_SAMPLE_LOADING_INFO MSGOUT("loaded %-12s to slot %03d: rate: %05d, numch: %02d, type: %s, bytes: %07d", SampleInfo[ num ].file, num, pSample->samplerate, pSample->numchannels, ( pSample->samplesize == 8 ) ? "8" : "16" , pSample->samplebytes ); #endif // SHOW_SAMPLE_LOADING_INFO // store the slot submitted to the play functions SampleInfo[ num ].samplepointer = (char *)num; //CAVEAT: for now we store the number of refframes in size SampleInfo[ num ].size = FLOAT2INT( (float) FRAME_MEASURE_TIMEBASE * (float) pSample->samplebytes / (float) ( pSample->samplerate * pSample->alignment ) + 0.5f ); FREEMEM( pSample ); return AUD_RETURN_SUCCESS; }
// play specified stream file ------------------------------------------------- // int AUDs_PlayAudioStream( char *fname ) { if (SoundDisabled) return 1; ASSERT(fname!=NULL); if (Mix_PlayingMusic()) Mix_HaltMusic(); if (music != NULL) { Mix_FreeMusic(music); FREEMEM(tmp_music_buffer); tmp_music_buffer = NULL; music = NULL; } // because SDL_RWFromFP doesn't work cross platform, we need // to get a little bit kinky // set up a buffer for the file size_t tmp_music_size = 0; // get the size of this sample tmp_music_size = SYS_GetFileLength(fname); if (tmp_music_size <= 0) { return 1; // return on error } // alloc space for the buffer if (tmp_music_buffer != NULL) { printf("ERROR: Shouldn't be this far without being cleaned up previously\n"); FREEMEM(tmp_music_buffer); tmp_music_buffer = NULL; } tmp_music_buffer = (char *)ALLOCMEM(tmp_music_size + 1); // open the sample file FILE *musicfp = SYS_fopen(fname, "rb"); if (musicfp == NULL) return 1; // read the file into the temp_sample_buffer int read_rc = SYS_fread((void *)tmp_music_buffer, 1, tmp_music_size, musicfp); if(read_rc <= 0) { MSGOUT("ERROR: Error reading music %s.", fname); return FALSE; } SDL_RWops *musicrwops = SDL_RWFromMem((void *)tmp_music_buffer, tmp_music_size); music = Mix_LoadMUS_RW(musicrwops, 1); //MSGOUT(" PlayAudioStream() with %s ", fname); if (music == NULL) { printf("Mix_LoadMUS(\"%s\"): %s\n", fname, Mix_GetError()); // this might be a critical error... FREEMEM(tmp_music_buffer); tmp_music_buffer = NULL; return 1; } Mix_VolumeMusic(128); if (music != NULL) { Mix_PlayMusic(music, 0); } //Pick up your your toys when you finish SYS_fclose(musicfp); return 1; }
/** * @sa M_Stop */ static void M_Start (const char* file) { if (Q_strnull(file)) return; if (!s_env.initialized) { Com_Printf("M_Start: No sound started!\n"); return; } if (music.playingStream || !music.playing) return; char name[MAX_QPATH]; Com_StripExtension(file, name, sizeof(name)); const size_t len = strlen(name); if (len + 4 >= MAX_QPATH) { Com_Printf("M_Start: MAX_QPATH exceeded: " UFO_SIZE_T "\n", len + 4); return; } /* we are already playing that track */ if (Q_streq(name, music.currentTrack) && music.data && Mix_PlayingMusic()) return; /* we are still playing some background track - fade it out */ if (music.data && Mix_PlayingMusic()) { if (!Mix_FadeOutMusic(1500)) M_Stop(); Q_strncpyz(music.nextTrack, name, sizeof(music.nextTrack)); return; } /* make really sure the last track is closed and freed */ M_Stop(); /* load it in */ byte* musicBuf; const int size = FS_LoadFile(va("music/%s.ogg", name), &musicBuf); if (size == -1) { Com_Printf("M_Start: Could not load '%s' background track!\n", name); return; } SDL_RWops* rw = SDL_RWFromMem(musicBuf, size); if (!rw) { Com_Printf("M_Start: Could not load music: 'music/%s'!\n", name); FS_FreeFile(musicBuf); return; } #if SDL_VERSION_ATLEAST(2,0,0) music.data = Mix_LoadMUS_RW(rw, 1); #else music.data = Mix_LoadMUS_RW(rw); #endif if (!music.data) { Com_Printf("M_Start: Could not load music: 'music/%s' (%s)!\n", name, Mix_GetError()); SDL_FreeRW(rw); FS_FreeFile(musicBuf); return; } Q_strncpyz(music.currentTrack, name, sizeof(music.currentTrack)); music.buffer = musicBuf; if (Mix_FadeInMusic(music.data, 1, 1500) == -1) Com_Printf("M_Start: Could not play music: 'music/%s' (%s)!\n", name, Mix_GetError()); }
int ONScripter::playMPEG(const char *filename, bool click_flag, bool loop_flag) { unsigned long length = script_h.cBR->getFileLength( filename ); if (length == 0){ fprintf( stderr, " *** can't find file [%s] ***\n", filename ); return 0; } #ifdef ANDROID playVideoAndroid(filename); return 0; #endif int ret = 0; #if !defined(MP3_MAD) && !defined(USE_SDL_RENDERER) unsigned char *mpeg_buffer = new unsigned char[length]; script_h.cBR->getFile( filename, mpeg_buffer ); SMPEG *mpeg_sample = SMPEG_new_rwops( SDL_RWFromMem( mpeg_buffer, length ), NULL, 0 ); if ( !SMPEG_error( mpeg_sample ) ){ SMPEG_enableaudio( mpeg_sample, 0 ); if ( audio_open_flag ){ SMPEG_actualSpec( mpeg_sample, &audio_format ); SMPEG_enableaudio( mpeg_sample, 1 ); } SMPEG_enablevideo( mpeg_sample, 1 ); SMPEG_setdisplay( mpeg_sample, screen_surface, NULL, NULL ); SMPEG_setvolume( mpeg_sample, music_volume ); SMPEG_loop(mpeg_sample, loop_flag); Mix_HookMusic( mp3callback, mpeg_sample ); SMPEG_play( mpeg_sample ); bool done_flag = false; while( !(done_flag & click_flag) && SMPEG_status(mpeg_sample) == SMPEG_PLAYING ){ SDL_Event event; while( SDL_PollEvent( &event ) ){ switch (event.type){ case SDL_KEYUP: if ( ((SDL_KeyboardEvent *)&event)->keysym.sym == SDLK_RETURN || ((SDL_KeyboardEvent *)&event)->keysym.sym == SDLK_SPACE || ((SDL_KeyboardEvent *)&event)->keysym.sym == SDLK_ESCAPE ) done_flag = true; break; case SDL_QUIT: ret = 1; case SDL_MOUSEBUTTONUP: done_flag = true; break; default: break; } } SDL_Delay( 10 ); } SMPEG_stop( mpeg_sample ); Mix_HookMusic( NULL, NULL ); SMPEG_delete( mpeg_sample ); } delete[] mpeg_buffer; #else fprintf( stderr, "mpegplay command is disabled.\n" ); #endif return ret; }
__declspec(dllexport) SDL_RWops * __stdcall SDL_RWFromMemVB6(void *mem, int size) { return SDL_RWFromMem(mem, size); }
bool GraphicsHelps::setWindowIcon(SDL_Window *window, FIBITMAP *img, int iconSize) { #ifdef _WIN32 struct SDL_SysWMinfo wmInfo; SDL_VERSION(&wmInfo.version); if(-1 == SDL_GetWindowWMInfo(window, &wmInfo)) return false; if(wmInfo.subsystem != SDL_SYSWM_WINDOWS) return false; HWND windowH = wmInfo.info.win.window; HICON hicon = NULL; BYTE *icon_bmp; unsigned int icon_len, y; SDL_RWops *dst; unsigned int w = FreeImage_GetWidth(img); unsigned int h = FreeImage_GetWidth(img); Uint8 *bits = (Uint8 *)FreeImage_GetBits(img); unsigned int pitch = FreeImage_GetPitch(img); /* Create temporary bitmap buffer */ icon_len = 40 + h * w * 4; icon_bmp = SDL_stack_alloc(BYTE, icon_len); dst = SDL_RWFromMem(icon_bmp, icon_len); if(!dst) { SDL_stack_free(icon_bmp); return false; } /* Write the BITMAPINFO header */ SDL_WriteLE32(dst, 40); SDL_WriteLE32(dst, w); SDL_WriteLE32(dst, h * 2); SDL_WriteLE16(dst, 1); SDL_WriteLE16(dst, 32); SDL_WriteLE32(dst, BI_RGB); SDL_WriteLE32(dst, h * w * 4); SDL_WriteLE32(dst, 0); SDL_WriteLE32(dst, 0); SDL_WriteLE32(dst, 0); SDL_WriteLE32(dst, 0); y = 0; do { Uint8 *src = bits + y * pitch; SDL_RWwrite(dst, src, pitch, 1); } while(++y < h); hicon = CreateIconFromResource(icon_bmp, icon_len, TRUE, 0x00030000); SDL_RWclose(dst); SDL_stack_free(icon_bmp); /* Set the icon for the window */ SendMessage(windowH, WM_SETICON, (iconSize < 32) ? ICON_SMALL : ICON_BIG, (LPARAM) hicon); #else (void)iconSize; SDL_Surface *sicon = GraphicsHelps::fi2sdl(img); SDL_SetWindowIcon(window, sicon); SDL_FreeSurface(sicon); const char *error = SDL_GetError(); if(*error != '\0') return false; #endif return true; }
// Loads the next image in the set, deletes the "last image" and // moves the currently decoded one to last image. int ISSImages::loadNextImage() { if ( filenames.size() <= 0 ) { printf("ERROR, no files to load\n"); return -1; } // Iterate our image safely if ( current_image + 1 == filenames.end() ) current_image = filenames.begin(); else current_image++; // Rotate decoded images if ( previous_image_surface ) SDL_FreeSurface(previous_image_surface); if ( current_image_surface ) previous_image_surface = current_image_surface; // Begin our load std::cout << "Loading image " << *current_image << "..."; FILE *fp = fopen((*current_image).c_str(), "rb"); if ( !fp ) { printf("Can't open file.\n"); return -1; } // Determine the size of the file for our buffer fseek(fp, 0, SEEK_END); unsigned long fsize = ftell(fp); rewind(fp); // Make a memory buffer the size of the image on our heap unsigned char *buf = new unsigned char[fsize]; // Read the file into our buffer if ( fread(buf, 1, fsize, fp) != fsize) { printf("Can't read file.\n"); return -1; } fclose(fp); // Parse the exif data to get the rotation EXIFInfo result; ParseEXIF(buf, fsize, result); // Create an SDL_RWops since we already have the file loaded, we don't // need another buffer with it, we'll load it and create a surface // from the buffer already containing the JPEG SDL_RWops *file; file = SDL_RWFromMem( buf, fsize ); current_image_surface = IMG_Load_RW( file, 0 ); SDL_FreeRW(file); if ( ! current_image_surface ) { printf("ERROR Loading image!\n"); return -1; } delete[] buf; printf("done\n"); // rotate the image if necessary // we want to do this before scaling because it will change whether it's // scaled to the W or the H to fit our display // ----------------------------------------------------------------------------- int rotation = 0; bool mirrored = false; switch (result.orientation) { case 7: mirrored = true; case 8: rotation = 270; break; case 5: mirrored = true; case 6: rotation = 90; break; case 4: mirrored = true; case 3: rotation = 180; break; case 2: mirrored = true; case 1: rotation = 0; break; } if ( mirrored || rotation ) { printf("Rotating image..."); if ( rotateImage(rotation, mirrored) ) printf("ERROR Rotating image!\n"); else printf("done\n"); } // scale the image if necessary // ----------------------------------------------------------------------------- int w = current_image_surface->w; int h = current_image_surface->h; if (w != 800) { h = (h * 800) / w; w = 800; } if (h > 600) { w = (w * 600) / h; h = 600; } if ( w != current_image_surface->w || h != current_image_surface->h) { printf("Scaling image..."); if ( scaleImage(w, h) ) printf("ERROR Scaling image!\n"); else printf("done\n"); } // frame the scaled image, if necessary // ----------------------------------------------------------------------------- if ( 800 != current_image_surface->w || 600 != current_image_surface->h) { printf("Framing image..."); if ( frameImage(800, 600) ) printf("ERROR Framing image!\n"); else printf("done\n"); } // return success // ----------------------------------------------------------------------------- return 0; }