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 CIwGameFile::Write(void* buffer, int len) { if (File == NULL) return false; if (s3eFileWrite(buffer, len, 1, File) != 1) { #if defined(_DEBUG) s3eFileGetError(); CIwGameError::LogError("Error: CIwGameFile::Write(): ", s3eFileGetErrorString()); Close(); #endif // _DEBUG return false; } return true; }
bool CIwGameFile::Open(void* memory_buffer, int memory_buffer_len) { FileAvailable = false; InMemory = true; File = s3eFileOpenFromMemory(memory_buffer, memory_buffer_len); if (File == NULL) { #if defined(_DEBUG) s3eFileGetError(); CIwGameError::LogError("Error: CIwGameFile::Open(memory): ", s3eFileGetErrorString()); #endif // _DEBUG Error = ErrorOpenFailed; return false; } FileAvailable = true; return true; }
const char* CIwGamePlatformFileMarm::getErrorString() { s3eFileGetError(); return s3eFileGetErrorString(); }
bool CIwGameFile::Open(const char* path, const char* mode, bool blocking) { FileAvailable = false; InMemory = false; // make sure user cannot open the file twice if (File != NULL) { Error = ErrorAlreadyOpen; return false; } // Get the path if (path != NULL) Filename.setString(path); else path = Filename.c_str(); // Check we actually have a path if (path == NULL) { Error = ErrorInvalidPath; return false; } // Check to see if the file is located on the web if (isHttp(Filename.c_str(), Filename.GetLength())) { Download(); if (blocking) { while (!FileAvailable) { IW_GAME_HTTP_MANAGER->Update(); s3eDeviceYield(0); } if (Error != ErrorNone) return false; } return true; } else { if (mode == NULL) { Error = ErrorEmptyMode; return false; } } // Open the file File = s3eFileOpen(Filename.c_str(), mode); if (File == NULL) { #if defined(_DEBUG) s3eFileGetError(); CIwGameError::LogError("Error: CIwGameFile::Open(): ", s3eFileGetErrorString()); #endif // _DEBUG Error = ErrorOpenFailed; return false; } FileAvailable = true; 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; }
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); }