unsigned char* CCFileUtils::getFileData(const char* pszFileName, const char* pszMode, unsigned long * pSize) { IW_CALLSTACK("CCFileUtils::getFileData"); s3eFile* pFile = s3eFileOpen(pszFileName, pszMode); if (! pFile && getIsPopupNotify()) { IwAssertMsg(GAME, pFile, ("Open file %s Failed. s3eFileError Code : %i", pszFileName, s3eFileGetError())); } if (! pFile) { *pSize = 0; return 0; } int32 fileSize = s3eFileGetSize(pFile); *pSize=fileSize; static int32* pDataToBeReadBinary; pDataToBeReadBinary = (int32*)s3eMallocBase(fileSize); memset(pDataToBeReadBinary, 0, fileSize); s3eFileRead(pDataToBeReadBinary, fileSize, 1, pFile); s3eFileClose(pFile); return (unsigned char*)pDataToBeReadBinary; }
bool CLevel::OpenXmlFile( const char * path, TiXmlDocument * doc ) { if ( doc == NULL ) { return false; } s3eFile * file = s3eFileOpen(path, "r"); if ( file != NULL ) { s3eFileSeek(file, 0, S3E_FILESEEK_END); long len = s3eFileTell( file ); s3eFileSeek(file, 0, S3E_FILESEEK_SET); char * buff = new char[len]; s3eFileRead(buff, sizeof(char), len, file); doc->Parse(buff); SAFE_DELETE_ARRAY( buff ); } else { IwAssertMsg(GAME, false, ("can't open file '%s'", path)); return false; } s3eFileClose(file); return true; }
void SimpleAudioEngine::preloadBackgroundMusic(const char* pszFilePath) { s3eFile *fileHandle = s3eFileOpen(pszFilePath, "rb"); IwAssertMsg(GAME, fileHandle, ("Open file %s Failed. s3eFileError Code : %i", pszFilePath, s3eFileGetError())); g_AudioFileSize = s3eFileGetSize(fileHandle); g_AudioBuffer = (int16*)malloc(g_AudioFileSize); memset(g_AudioBuffer, 0, g_AudioFileSize); s3eFileRead(g_AudioBuffer, g_AudioFileSize, 1, fileHandle); s3eFileClose(fileHandle); }
//------------------------------------------------------------------------- bool WavReader::readChunkFact(const RIFFChunkHeader& header, SoundData*& data, s3eFile& file) { uint32 sampleCount; if (s3eFileRead(&sampleCount, sizeof(uint32), 1, &file) != 1) { IwAssertMsg(SOUND, false, ("Error reading WAVE file info from %s", filename)); return false; } data->sample_count = sampleCount; return true; }
//------------------------------------------------------------------------- bool CIwSoundWAV::ReadChunkFact(const CIwStringL& pathname, const IwRIFFChunkHeader& header, CIwSoundData*& pData, s3eFile& file) { uint32 sampleCount; if (s3eFileRead(&sampleCount, sizeof(uint32), 1, &file) != 1) { IwAssertMsg(SOUND, false, ("Error reading WAVE file info from %s", pathname.c_str())); return false; } pData->SetSampleCount(sampleCount); return true; }
AKRESULT s3eIOHook::Read(AkFileDesc &in_fileDesc, const AkIoHeuristics &, void *out_pBuffer, AkIOTransferInfo &io_transferInfo) { int32 position = (int32)io_transferInfo.uFilePosition; if( s3eFileSeek((s3eFile *)in_fileDesc.hFile, position, S3E_FILESEEK_SET) == S3E_RESULT_SUCCESS ) { uint32 itemsRead = s3eFileRead(out_pBuffer, 1, io_transferInfo.uRequestedSize, (s3eFile *)in_fileDesc.hFile); if( itemsRead > 0 ) return AK_Success; } return AK_Fail; }
void Map::ReadJsonFile(char * filename) { cJSON *root; char * jsonContent; s3eFile* fileHandler; fileHandler=s3eFileOpen(filename, "rb"); if (fileHandler != NULL) { // Allocate buffer to be filled with the files contents int32 fileSize = s3eFileGetSize(fileHandler); jsonContent = (char*)s3eMallocBase(fileSize+1); // Read data from file if (s3eFileRead(&jsonContent[0], fileSize+1, 1, fileHandler) != 1) { // An kError has occurred, retrieve information for display std::cout<<s3eFileGetErrorString()<<std::endl; } else { // Data reading has been successful jsonContent[fileSize] = '\0'; } } else { // Something went wrong during opening of the file retrieve error for display std::cout<<s3eFileGetErrorString()<<std::endl; } if (fileHandler) s3eFileClose(fileHandler); root = cJSON_Parse(jsonContent); s3eFileFlush(fileHandler); _height=cJSON_GetObjectItem(root,"height")->valueint; cJSON *layers = cJSON_GetObjectItem(root,"layers"); _tileHeight=cJSON_GetObjectItem(root,"tileheight")->valueint; _tileWidth=cJSON_GetObjectItem(root,"tilewidth")->valueint; _width=cJSON_GetObjectItem(root,"width")->valueint; _layer_base->Init(cJSON_GetArrayItem(layers,0)); _layer_middle->Init(cJSON_GetArrayItem(layers,1)); _layer_maze->Init(cJSON_GetArrayItem(layers,2)); cJSON *tilesets = cJSON_GetObjectItem(root,"tilesets"); _tileset_map->Init(cJSON_GetArrayItem(tilesets,0)); _tileset_maze->Init(cJSON_GetArrayItem(tilesets,1)); _total=_height*_width; _size=CIwSVec2(_width*_tileWidth,_height*_tileHeight); }
//------------------------------------------------------------------------- bool CIwSoundWAV::ReadChunkFormat(const CIwStringL& pathname, const IwRIFFChunkHeader& header, CIwSoundData*& pData, s3eFile& file) { IwWAVEFormatChunkADPCM format; // Read data from file if ((header.length < sizeof(IwWAVEFormatChunk)) || (s3eFileRead(&format, sizeof(IwWAVEFormatChunkADPCM), 1, &file) != 1)) { IwAssertMsg(SOUND, false, ("Invalid format chunk in WAV file")); return false; } IwAssertMsg(SOUND, format.channels == 1, ("%s has more than 1 channel. IwSound is mono only", pathname.c_str())); // Create object based on header info switch (format.formatTag) { default: // Unsupported format - Exit without creating data IwAssertMsg(SOUND, false, ("Unsupported WAVE file format (%d)", format.formatTag)); return false; break; case WAVE_FORMAT_PCM: // Raw PCM data. Bits per sample can be 8 or 16 // Bits per sample will have been read into pad bytes of format info switch (format.bitsPerSample) { case 8: // 8 bps pData = new CIwSoundData(PCM_8BIT_MONO, format.samplesPerSec); break; case 16: // 16 bps pData = new CIwSoundData(PCM_16BIT_MONO, format.samplesPerSec); break; default: // Unhandled IwAssertMsg(SOUND, false, ("Unsupported bits-per-sample (%d)", format.bitsPerSample)); return false; break; } break; case 17: // ADPCM compressed data pData = new CIwSoundDataADPCM(ADPCM_MONO, format.samplesPerSec, ((IwWAVEFormatChunkADPCM*) &format)->samplesPerBlock, ((IwWAVEFormatChunkADPCM*) &format)->blockAlign); break; } return true; }
void SimpleAudioEngine::preloadBackgroundMusic(const char* pszFilePath) { // Changing file path to full path std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath); s3eFile *fileHandle = s3eFileOpen(fullPath.c_str(), "rb"); IwAssertMsg(GAME, fileHandle, ("Open file %s Failed. s3eFileError Code : %i", fullPath.c_str(), s3eFileGetError())); g_AudioFileSize = s3eFileGetSize(fileHandle); g_AudioBuffer = (int16*)malloc(g_AudioFileSize); memset(g_AudioBuffer, 0, g_AudioFileSize); s3eFileRead(g_AudioBuffer, g_AudioFileSize, 1, fileHandle); s3eFileClose(fileHandle); }
Sounds::Sound* Sounds::loadSound(const char* filename) { Sounds::Sound* sound = new Sounds::Sound(); s3eFile *fileHandle = s3eFileOpen(filename, "rb"); if (fileHandle) { strcpy(sound->fileName, filename); sound->fileSize = s3eFileGetSize(fileHandle); sound->buffer = (int16*)s3eMallocBase(sound->fileSize); memset(sound->buffer, 0, sound->fileSize); s3eFileRead(sound->buffer, sound->fileSize, 1, fileHandle); s3eFileClose(fileHandle); } else fprintf(stderr, "Error loading sound file: %s.\n", filename); return sound; }
Buffer loadFile(const char * fname) { s3eFile * file = s3eFileOpen(fname, "rb"); if(file) { size_t size = s3eFileGetSize(file); Buffer result(size); s3eFileRead(result.data(), 1, size, file); s3eFileClose(file); return result; } else { IwAssertMsg(AUDIO_UTILS, false, ("failed to open: %s", fname)); return Buffer(); } }
bool CIwGameFile::Read(void* buffer, int len) { if (File == NULL) return false; if (s3eFileRead(buffer, len, 1, File) != 1) { #if defined(_DEBUG) s3eFileGetError(); CIwGameError::LogError("Error: CIwGameFile::Read(): ", s3eFileGetErrorString()); #endif // _DEBUG Close(); return false; } return true; }
void SimpleAudioEngine::preloadEffect(const char* pszFilePath) { SoundFxMap::iterator it = g_pSoundFxMap->find(pszFilePath) ; if( it==g_pSoundFxMap->end() ) { s3eFile *fileHandle = s3eFileOpen(pszFilePath, "rb"); IwAssertMsg(GAME, fileHandle, ("Open file %s Failed. s3eFileError Code : %i", pszFilePath, s3eFileGetError())); int32 fileSize = s3eFileGetSize(fileHandle); int16* buff = (int16*)malloc(fileSize); (*g_pSoundFxMap)[pszFilePath] = SoundFx(buff,fileSize) ; memset(buff, 0, fileSize); s3eFileRead(buff, fileSize, 1, fileHandle); s3eFileClose(fileHandle); } }
bool CCSAXParser::parse(const char *pszFile) { bool bRet = false; char* buf = NULL; s3eFile* file = NULL; do { file = s3eFileOpen(pszFile, "r"); if (!file) { IwAssertMsg(GAME, file, ("Open file %s Failed. s3eFileError Code : %i", pszFile, s3eFileGetError())); break; } s3eFileSeek(file, 0, S3E_FILESEEK_END); int size = s3eFileTell(file); s3eFileSeek(file, 0, S3E_FILESEEK_SET); buf = new char[size]; int done =0; int len = (int)s3eFileRead(buf, 1, size, file); if (XML_Parse(s_pParser, buf, len, 1) == XML_STATUS_ERROR) { CCLog("GAME: cocos2d: plist err: %s at line %d", XML_ErrorString(XML_GetErrorCode(s_pParser)), XML_GetCurrentLineNumber(s_pParser)); break; } bRet = true; } while(0); // cleanup if (file) { s3eFileClose(file); } if (buf) { delete []buf; } return bRet; }
void SimpleAudioEngine::preloadEffect(const char* pszFilePath) { // Changing file path to full path std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename(pszFilePath); SoundFxMap::iterator it = g_pSoundFxMap->find(fullPath) ; if( it==g_pSoundFxMap->end() ) { s3eFile *fileHandle = s3eFileOpen(fullPath.c_str(), "rb"); IwAssertMsg(GAME, fileHandle, ("Open file %s Failed. s3eFileError Code : %i", fullPath.c_str(), s3eFileGetError())); int32 fileSize = s3eFileGetSize(fileHandle); int16* buff = (int16*)malloc(fileSize); (*g_pSoundFxMap)[fullPath] = SoundFx(buff,fileSize) ; memset(buff, 0, fileSize); s3eFileRead(buff, fileSize, 1, fileHandle); s3eFileClose(fileHandle); } }
void SimpleAudioEngine::preloadEffect(const char* pszFilePath) { SoundFxMap::iterator it = g_pSoundFxMap->find(pszFilePath) ; if( it==g_pSoundFxMap->end() ) { #if 0 CIwResHandlerWAV * wav_handler = new CIwResHandlerWAV(); class OpenSoundData: public CIwSoundData { public: int16 * GetBuffer() { return (int16*)m_Samples; } }; OpenSoundData * res = (OpenSoundData*)dynamic_cast<CIwSoundData*>(wav_handler->Build(pszFilePath)); int32 data_size = res->GetBufferSize(); int16* buff = (int16*)malloc(data_size); memcpy(buff, res->GetBuffer(), data_size); (*g_pSoundFxMap)[pszFilePath] = SoundFx(buff, data_size); delete res; delete wav_handler; #else s3eFile *fileHandle = s3eFileOpen(pszFilePath, "rb"); IwAssertMsg(GAME, fileHandle, ("Open file %s Failed. s3eFileError Code : %i", pszFilePath, s3eFileGetError())); int32 fileSize = s3eFileGetSize(fileHandle); int16* buff = (int16*)malloc(fileSize); (*g_pSoundFxMap)[pszFilePath] = SoundFx(buff,fileSize) ; memset(buff, 0, fileSize); s3eFileRead(buff, fileSize, 1, fileHandle); s3eFileClose(fileHandle); #endif } }
void MapBackground::CreateMapTileImage2(MapTile* pMapTile, char* szPath, bool isJpg) { if (!IsTileVisible(pMapTile)) { return; } CIwImage image; if (!isJpg) { image.LoadFromFile(szPath); if (image.GetWidth()) { pMapTile->pTexture = new CIwTexture; pMapTile->pTexture->CopyFromImage(&image); pMapTile->pTexture->Upload(); } } else { s3eFile* pFile = s3eFileOpen(szPath, "r"); if (pFile) { uint32 gResultLen = s3eFileGetSize(pFile); void* gResult = (void*)s3eMalloc(gResultLen + 1); uint32 test = s3eFileRead(gResult, sizeof(char), gResultLen, pFile); gResultLen = test; s3eFileClose(pFile); JPEGImage(gResult, gResultLen, image); pMapTile->pTexture = new CIwTexture; pMapTile->pTexture->CopyFromImage(&image); pMapTile->pTexture->Upload(); delete gResult; } } }
//------------------------------------------------------------------------- bool CIwSoundWAV::ReadChunkData(const CIwStringL& pathname, const IwRIFFChunkHeader& header, CIwSoundData*& pData, s3eFile& file) { // Check that we have already read a format chunk and the object has been created if (!pData) { IwAssertMsg(SOUND, false, ("Data chunk encountered before format chunk in %s", pathname.c_str())); return false; } // If the number of samples has already been set then check we're not changing it #ifdef IW_USE_ASSERTIONS uint32 currentBufSiz = pData->GetBufferSize(); IwAssertMsg(SOUND, (currentBufSiz == 0) || (header.length == currentBufSiz), ("Unexpected data size in %s", pathname.c_str())); #endif // Create the data array pData->SetBufferSize(header.length); // Read in the actual data. This can be read straight into the array independent of BPS if (s3eFileRead(&pData->m_Samples[0], 1, header.length, &file) != header.length) { IwAssertMsg(SOUND, false, ("Error reading WAVE file data from %s", pathname.c_str())); return false; } // DJB - Our 8-bit samples seem to be saved as unsigned data. Don't why this is or a way // to distinguish signed from unsigned. // Transform all 8-bit samples for now // DP - wav files are 8-bit unsigned and 16-bit signed, thats just the (random) way they are if (pData->m_Format == PCM_8BIT_MONO) pData->SwitchDataSign(); return true; }
uint32 ResourceManager::RegisterAudio(std::string audioName) { s3eFile *fileHandle = s3eFileOpen(audioName.c_str(), "rb"); if(fileHandle == null) { std::string errMsg = "Unknown audio resource requested: '"; errMsg += audioName + "'"; s3eDebugAssertShow(S3E_MESSAGE_CONTINUE, errMsg.c_str()); return 0; } int soundSize = s3eFileGetSize(fileHandle); int16 *soundData = (int16*)s3eMallocBase(soundSize); memset(soundData, 0, soundSize); s3eFileRead(soundData, soundSize, 1, fileHandle); s3eFileClose(fileHandle); int handle = audioData->size(); audioData->push_back(soundData); audioSizes->push_back(soundSize); return handle; }
void userReadData(png_structp pngPtr, png_bytep data, png_size_t length) { png_voidp png_pointer = png_get_io_ptr(pngPtr); s3eFileRead((char*)data, length, 1, (s3eFile*)png_pointer); }
//------------------------------------------------------------------------- CIwSoundData* CIwSoundWAV::Create(const CIwStringL& pathname, void* buffer, u_int file_size) { IW_CALLSTACK("CIwSoundWAV::Create") CIwSoundData* pData = NULL; // Object to return // Open file s3eFile* pFile = NULL; if (buffer != NULL) pFile = s3eFileOpenFromMemory(buffer, file_size); else pFile = IwFileOpenPrefixed(pathname.c_str(), "rb"); IwAssertMsg(SOUND, pFile, ("Could not load file %s", pathname.c_str())); if (!pFile) return NULL; // Read RIFF header - Gives the file size and checks that this is a WAVE // file as expected IwRIFFHeader riffHeader; if ((s3eFileRead(&riffHeader, sizeof(IwRIFFHeader), 1, pFile) != 1) || (strncmp(riffHeader.typeID, "RIFF", 4) != 0) || (strncmp(riffHeader.subTypeID, "WAVE", 4) != 0)) { IwAssertMsg(SOUND, false, ("Invalid header in %s (RIFF Header)", pathname.c_str())); s3eFileClose(pFile); return NULL; } // Read in RIFF chunks until we reach the end of the file // Read the RIFF chunk header. This tells us what type of chunk follows. IwRIFFChunkHeader chunkHeader; bool readData = false; uint32 fileSize = s3eFileGetSize(pFile); while (ReadChunkHeader(chunkHeader, *(s3eFile*)pFile)) { uint32 chunkStartPos = s3eFileTell(pFile); // Next action depends on chunk type. The order of this is important and we may fail // if an unexpected chunk type is found if (!strncmp(chunkHeader.typeID, "fmt ", 4)) { // Read WAVE info chunk if (!ReadChunkFormat(pathname, chunkHeader, pData, *(s3eFile*)pFile)) { s3eFileClose(pFile); return NULL; } } else if (!strncmp(chunkHeader.typeID, "data", 4)) { if (!ReadChunkData(pathname, chunkHeader, pData, *(s3eFile*)pFile)) { s3eFileClose(pFile); return NULL; } readData = true; } else if (!strncmp(chunkHeader.typeID, "fact", 4)) { if (!ReadChunkFact(pathname, chunkHeader, pData, *(s3eFile*)pFile)) { s3eFileClose(pFile); return NULL; } } else { // Unknown chunk type // Make a proper string from the chunk type info char typeID[5]; strncpy(typeID, chunkHeader.typeID, 4); typeID[4] = 0; // Terminate const char* g_IgnoreTypes = "LIST" //LIST is just copyright info etc. "DISP"; //DISP seems to be info about what package exported it IwAssertMsg(SOUND, strstr(g_IgnoreTypes, typeID), ("Unhandled chunk type '%s' in %s. Ignoring this data.", typeID, pathname.c_str())); } // Exit if at end of file if (chunkStartPos + chunkHeader.length >= fileSize) break; // Move to next chunk s3eFileSeek(pFile, chunkStartPos + chunkHeader.length, S3E_FILESEEK_SET); } // Check that we have read the sample data IwAssertMsg(SOUND, readData, ("No data chunk read in %s", pathname.c_str())); s3eFileClose(pFile); return pData; }
//------------------------------------------------------------------------- bool WavReader::readChunkHeader(RIFFChunkHeader& header, s3eFile& file) { return (s3eFileRead(&header, sizeof(RIFFChunkHeader), 1, &file) == 1); }
//------------------------------------------------------------------------- bool CIwSoundWAV::ReadChunkHeader(IwRIFFChunkHeader& header, s3eFile& file) { return (s3eFileRead(&header, sizeof(IwRIFFChunkHeader), 1, &file) == 1); }
bool GAFAsset::initWithImageData(const std::string& jsonPath) { if (!s3eFileCheckExists(jsonPath.c_str())) { GAFLOGERROR("CAN NOT create GAFAsset : %s does not exists", jsonPath.c_str()); return false; } GAFData aConfigData; //std::string fp = CCFileUtils::sharedFileUtils()->fullPathForFilename(jsonPath.c_str()); std::string fp = jsonPath; s3eFile * file = s3eFileOpen (fp.c_str(), "rb"); if (!file) { GAFLOGERROR("CAN NOT create GAFAsset : can not open %s.", fp.c_str()); return false; } aConfigData.setSize(s3eFileGetSize(file)); aConfigData.setBytes(new unsigned char[aConfigData.size()]); s3eFileRead(aConfigData.bytes(), aConfigData.size(), 1, file); s3eFileClose(file); aConfigData.setDeleteData(true); if (!aConfigData.bytes()) { GAFLOGERROR("Can not get data from json file : %s", jsonPath.c_str()); return NULL; } if (!aConfigData.bytes()) { GAFLOGWARN("can not init GAFAsset - invalid anImageData"); return false; } CCDictionary* configDictionary = CCJSONConverter::sharedConverter()->dictionaryFrom( (const char *)aConfigData.bytes()); CCString *versionNode = (CCString*)configDictionary->objectForKey(kVersionKey); if (!isAssetVersionPlayable(versionNode->getCString())) { return false; } CCArray *animationConfigFrames = (CCArray *)configDictionary->objectForKey(kAnimationConfigFramesKey); CCArray *interactionObjectNodes = (CCArray *)configDictionary->objectForKey(kInteractionObjectsKey); CCArray *standObjectsNodes = (CCArray *)configDictionary->objectForKey(kStandObjectsKey); CCArray *textureAtlasNode = (CCArray *)configDictionary->objectForKey(kTextureAtlasKey); CCArray *animationSequences = (CCArray *)configDictionary->objectForKey(kAnimationSequencesKey); CCDictionary *objectNodes = (CCDictionary *)configDictionary->objectForKey(kAnimationObjectsKey); CCDictionary *masksNodes = (CCDictionary *)configDictionary->objectForKey(kAnimationMasksKey); CCDictionary *namedPartsNodes = (CCDictionary *)configDictionary->objectForKey(kAnimationNamedPartsKey); if (!animationConfigFrames || !textureAtlasNode|| !objectNodes) { GAFLOGERROR("Error while creating GAFAsset. Required subnodes in dictionary are missing."); return false; } CC_SAFE_RELEASE(_textureAtlas); if (!textureAtlasNode->count()) { return false; } CCDictionary * atlasDictionary = (CCDictionary *)textureAtlasNode->objectAtIndex(0); float atlasScale = atlasScaleFromAtlasConfig(atlasDictionary); for (int i = 1; i < textureAtlasNode->count(); ++i) { CCDictionary * a = (CCDictionary *)textureAtlasNode->objectAtIndex(i); float as = atlasScaleFromAtlasConfig(a); if ( fabs(atlasScale - _currentDeviceScale) > fabs(as - _currentDeviceScale)) { atlasDictionary = a; atlasScale = as; } } _usedAtlasContentScaleFactor = atlasScale; CCArray * atlasesInfo = (CCArray *)atlasDictionary->objectForKey(kAtlasInfoKey); if (!atlasesInfo) { GAFLOGERROR("Error while creating GAFAsset.atlasesInfo subnode is missing in atlasDictionary."); return false; } _textureAtlas = GAFTextureAtlas::create(fp.c_str(), atlasDictionary); if (!_textureAtlas) { GAFLOGERROR("Failed to initialize GAFAsset. GAFTextureAtlas could not be created."); return false; } CC_SAFE_RETAIN(_textureAtlas); if (_objects != objectNodes) { CC_SAFE_RELEASE(_objects); _objects = objectNodes; CC_SAFE_RETAIN(_objects); } if (_masks != masksNodes) { CC_SAFE_RELEASE(_masks); _masks = masksNodes; CC_SAFE_RETAIN(_masks); } if (_namedParts != namedPartsNodes) { CC_SAFE_RELEASE(_namedParts); _namedParts = namedPartsNodes; CC_SAFE_RETAIN(_namedParts); } if (interactionObjectNodes) { CC_SAFE_RELEASE(_interactionObjects); _interactionObjects = CCArray::create(); CC_SAFE_RETAIN(_interactionObjects); for (unsigned int i = 0; i < interactionObjectNodes->count(); ++i) { CCDictionary * dict = (CCDictionary*)interactionObjectNodes->objectAtIndex(i); GAFInteractionObject * interObject = GAFInteractionObject::create(dict); if (interObject) { _interactionObjects->addObject(interObject); } } } if (standObjectsNodes) { CC_SAFE_RELEASE(_standObjects); _standObjects = CCArray::create(); CC_SAFE_RETAIN(_standObjects); for (unsigned int i = 0; i < standObjectsNodes->count(); ++i) { CCDictionary * dict = (CCDictionary*)standObjectsNodes->objectAtIndex(i); GAFActionObject * interObject = GAFActionObject::create(dict); if (interObject) { _standObjects->addObject(interObject); } } } loadFramesFromConfigDictionary(configDictionary); if (animationSequences) { loadAnimationSequences(animationSequences); } configDictionary->removeAllObjects(); return true; }
int main() { // Initialise the 2D graphics system Iw2DInit(); // Create an image from a PNG file CIw2DImage* image = Iw2DCreateImage("textures/Tiles.png"); //Avatar bits AnimationData testData; testData.animationName = "Up"; testData.animationPosition = 0; testData.frameHeight = 64; testData.frameWidth = 32; testData.numberOfFrames = 4; AnimationData testData2; testData2.animationName = "Right"; testData2.animationPosition = 64; testData2.frameHeight = 64; testData2.frameWidth = 32; testData2.numberOfFrames = 4; Animation* animation = new Animation("textures/AvatarTest.png",testData); Animation* animation2 = new Animation("textures/AvatarTest.png",testData2); s3eFile* file = s3eFileOpen("tilemaps/tilemapdemo.json", "rb"); int len = s3eFileGetSize(file); char* rawTileJSON = new char[len]; if (file != NULL) { if (s3eFileRead(rawTileJSON, len, 1, file) != 1) { s3eFileGetError(); s3eDebugOutputString(s3eFileGetErrorString()); } s3eFileClose(file); } else { s3eFileGetError(); s3eDebugOutputString(s3eFileGetErrorString()); } cJSON *root = cJSON_Parse(rawTileJSON); int gridHeight = cJSON_GetObjectItem(root,"height")->valueint; int gridWidth = cJSON_GetObjectItem(root,"width")->valueint; int tileWidth = cJSON_GetObjectItem(root,"tileheight")->valueint; int tileHeight = cJSON_GetObjectItem(root,"tilewidth")->valueint; cJSON *layers = cJSON_GetObjectItem(root,"layers"); cJSON *tileData; int i; for (i = 0; i<cJSON_GetArraySize(layers); i++) { cJSON *layer = cJSON_GetArrayItem(layers,i); tileData = cJSON_GetObjectItem(layer,"data"); } // Loop forever, until the user or the OS performs some action to quit the app while (!s3eDeviceCheckQuitRequest()) { // Clear the drawing surface Iw2DSurfaceClear(0xff000000); int x,y,tileIndex,tileType; for( y = 0; y < gridHeight; y++) { for(x = 0; x < gridWidth; x++) { tileIndex = (y * gridWidth) + x; tileType = cJSON_GetArrayItem(tileData,tileIndex)->valueint -1; // Draw an image Iw2DDrawImageRegion(image, CIwFVec2(x*tileWidth,y*tileHeight),CIwFVec2(tileWidth,tileHeight),CIwFVec2(tileType*tileWidth,0),CIwFVec2(tileWidth,tileHeight)); } } animation->render(50,50); animation2->render(150,200); // Show the drawing surface Iw2DSurfaceShow(); // Yield to the OS s3eDeviceYield(0); } cJSON_Delete(root); // Clean-up delete rawTileJSON; delete image; delete animation; delete animation2; Iw2DTerminate(); return 0; }
bool CCImage::_initWithPngData(void * pData, int nDatalen) { IW_CALLSTACK("CCImage::_initWithPngData"); bool bRet = false; s3eFile* pFile = s3eFileOpenFromMemory(pData, nDatalen); IwAssert(GAME, pFile); png_byte pngsig[PNGSIGSIZE]; bool is_png = false; s3eFileRead((char*)pngsig, PNGSIGSIZE, 1, pFile); is_png = png_sig_cmp(pngsig, 0, PNGSIGSIZE) == 0 ? true : false; if (!is_png) return false; png_structp pngPtr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (!pngPtr) return false; png_infop infoPtr = png_create_info_struct(pngPtr); if (!infoPtr) return false; png_bytep* rowPtrs = NULL; m_pData = NULL; if (setjmp(png_jmpbuf(pngPtr))) { png_destroy_read_struct(&pngPtr, &infoPtr,(png_infopp)0); if (rowPtrs != NULL) delete [] rowPtrs; if (m_pData != NULL) delete [] m_pData; CCLog("ERROR: An error occured while reading the PNG file"); return false; } png_set_read_fn(pngPtr, pFile, userReadData); png_set_sig_bytes(pngPtr, PNGSIGSIZE); png_read_info(pngPtr, infoPtr); png_uint_32 bitdepth = png_get_bit_depth(pngPtr, infoPtr); png_uint_32 channels = png_get_channels(pngPtr, infoPtr); png_uint_32 color_type = png_get_color_type(pngPtr, infoPtr); // Convert palette color to true color if (color_type ==PNG_COLOR_TYPE_PALETTE) png_set_palette_to_rgb(pngPtr); // Convert low bit colors to 8 bit colors if (png_get_bit_depth(pngPtr, infoPtr) < 8) { if (color_type==PNG_COLOR_TYPE_GRAY || color_type==PNG_COLOR_TYPE_GRAY_ALPHA) png_set_gray_1_2_4_to_8(pngPtr); else png_set_packing(pngPtr); } if (png_get_valid(pngPtr, infoPtr, PNG_INFO_tRNS)) png_set_tRNS_to_alpha(pngPtr); // Convert high bit colors to 8 bit colors if (bitdepth == 16) png_set_strip_16(pngPtr); // Convert gray color to true color if (color_type==PNG_COLOR_TYPE_GRAY || color_type==PNG_COLOR_TYPE_GRAY_ALPHA) png_set_gray_to_rgb(pngPtr); // Update the changes png_read_update_info(pngPtr, infoPtr); // init image info m_bPreMulti = true; unsigned int bytesPerComponent = png_get_channels(pngPtr, infoPtr); m_bHasAlpha = (bytesPerComponent == 4 ? true : false); m_nHeight = (unsigned int)png_get_image_height(pngPtr, infoPtr); m_nWidth = (unsigned int) png_get_image_width(pngPtr, infoPtr); m_nBitsPerComponent = (unsigned int)png_get_bit_depth(pngPtr, infoPtr); m_pData = new unsigned char[m_nHeight * m_nWidth * bytesPerComponent]; unsigned int bytesPerRow = m_nWidth * bytesPerComponent; { unsigned char *ptr = m_pData; rowPtrs = new png_bytep[m_nHeight]; for (int i = 0; i < m_nHeight; i++) { int q = (i) * bytesPerRow; rowPtrs[i] = (png_bytep)m_pData + q; } png_read_image(pngPtr, rowPtrs); delete[] (png_bytep)rowPtrs; png_destroy_read_struct(&pngPtr, &infoPtr,(png_infopp)0); s3eFileClose(pFile); pFile = 0; } // premultiplay if alpha if(m_bHasAlpha) for(unsigned int i = 0; i < m_nHeight*bytesPerRow; i += bytesPerComponent){ *(m_pData + i + 0) = (*(m_pData + i + 0) * *(m_pData + i + 3) + 1) >> 8; *(m_pData + i + 1) = (*(m_pData + i + 1) * *(m_pData + i + 3) + 1) >> 8; *(m_pData + i + 2) = (*(m_pData + i + 2) * *(m_pData + i + 3) + 1) >> 8; *(m_pData + i + 3) = *(m_pData + i + 3); } bRet = true; return bRet; }
uint CIwGamePlatformFileMarm::Read(CxFile file, void* buffer, uint num_bytes) { return s3eFileRead(buffer, num_bytes, 1, (s3eFile*)file); }
size_t COggVorbisFileHelper::read_func(void *ptr, size_t size, size_t nmemb, void *datasource) { return s3eFileRead(ptr, size, nmemb, (s3eFile*)datasource); }
void Map::ReadJsonFile(char * filename) { m_filename=filename; cJSON *root; char * jsonContent; s3eFile* fileHandler; fileHandler=s3eFileOpen(filename, "rb"); if (fileHandler != NULL) { // Allocate buffer to be filled with the files contents int32 fileSize = s3eFileGetSize(fileHandler); jsonContent = (char*)s3eMallocBase(fileSize+1); // Read data from file if (s3eFileRead(&jsonContent[0], fileSize+1, 1, fileHandler) != 1) { // An kError has occurred, retrieve information for display std::cout<<s3eFileGetErrorString()<<std::endl; } else { // Data reading has been successful jsonContent[fileSize] = '\0'; } } else { // Something went wrong during opening of the file retrieve error for display std::cout<<s3eFileGetErrorString()<<std::endl; } if (fileHandler) s3eFileClose(fileHandler); root = cJSON_Parse(jsonContent); s3eFileFlush(fileHandler); _height=cJSON_GetObjectItem(root,"height")->valueint; cJSON *layers = cJSON_GetObjectItem(root,"layers"); _tileHeight=cJSON_GetObjectItem(root,"tileheight")->valueint; _tileWidth=cJSON_GetObjectItem(root,"tilewidth")->valueint; _width=cJSON_GetObjectItem(root,"width")->valueint; _layer_base->Init(cJSON_GetArrayItem(layers,0)); _layer_middle->Init(cJSON_GetArrayItem(layers,1)); _layer_maze->Init(cJSON_GetArrayItem(layers,2)); cJSON *tilesets = cJSON_GetObjectItem(root,"tilesets"); _tileset_map->Init(cJSON_GetArrayItem(tilesets,0)); _tileset_maze->Init(cJSON_GetArrayItem(tilesets,1)); cJSON * properties=cJSON_GetObjectItem(root,"properties"); int propSize=cJSON_GetArraySize(properties); for(int i=propSize-1;i!=-1;i--) { cJSON * prop=cJSON_GetArrayItem(properties,i); char* propString=prop->valuestring; char* name=prop->string; char* chars_array = strtok(propString, ","); if(CharCMP(name,"Block",sizeof("Block"))) { while(chars_array) { _EventBlock.append(atoi(chars_array)); chars_array = strtok(NULL, ","); } } else if(CharCMP(name,"BG",sizeof("BG"))) { _BGImage=Iw2DCreateImageResource(propString); } else if(CharCMP(name,"Door",sizeof("Door"))) { while(chars_array) { m_doors.append(atoi(chars_array)); chars_array = strtok(NULL, ","); } } else if(CharCMP(name,"EndPoint",sizeof("EndPoint"))) { _EndPos[0]=atoi(chars_array); chars_array = strtok(NULL, ","); _EndPos[1]=atoi(chars_array); } else if(CharCMP(name,"StartPoint",sizeof("StartPoint"))) { _StartPos[0]=atoi(chars_array); chars_array = strtok(NULL, ","); _StartPos[1]=atoi(chars_array); } else if(CharCMP(name,"emaze",sizeof("emaze"))) { while(chars_array) { mapEndIndex.append(atoi(chars_array)); chars_array = strtok(NULL, ","); } } else if(CharCMP(name,"etmaze",sizeof("etmaze"))) { while(chars_array) { mazeEndIndex.append(atoi(chars_array)); chars_array = strtok(NULL, ","); } } else if(CharCMP(name,"smaze",sizeof("smaze"))) { while(chars_array) { mapStartIndex.append(atoi(chars_array)); chars_array = strtok(NULL, ","); } } else if(CharCMP(name,"stmaze",sizeof("stmaze"))) { while(chars_array) { mazeStartIndex.append(atoi(chars_array)); chars_array = strtok(NULL, ","); } } } _total=_height*_width; _size=CIwSVec2(_width*_tileWidth,_height*_tileHeight); }