Skip to content

Instantly share code, notes, and snippets.

@matthewlehew
Last active February 16, 2016 15:29
Show Gist options
  • Save matthewlehew/755fae28780bb99edbf0 to your computer and use it in GitHub Desktop.
Save matthewlehew/755fae28780bb99edbf0 to your computer and use it in GitHub Desktop.
RapidJSON class definitions
//
// GameData.cpp
// RueRunner
//
// Created by Matthew LeHew on 2/15/16.
//
//
#include "GameData.hpp"
#include "external/json/document.h"
#include "external/json/writer.h"
#include "external/json/stringbuffer.h"
#include "external/json/filereadstream.h"
#include "external/json/filewritestream.h"
#include "external/json/rapidjson.h"
#include <cstdio>
#include <iostream>
using namespace rapidjson;
int highScoreValue;
void GameData::startup()
{
std::string scoresDir = cocos2d::FileUtils::getInstance()->getWritablePath();
std::string scoresFilePathString = scoresDir + "scores.json";
const char* scoresFilePath = scoresFilePathString.c_str();
FILE* scoresFile = fopen(scoresFilePath, "r");
if (scoresFile)
{
readScores();
}
else if (!scoresFile)
{
fclose(scoresFile);
createScores();
writeScores();
}
}
void GameData::readScores()
{
std::string scoresDir = cocos2d::FileUtils::getInstance()->getWritablePath();
std::string scoresFilePathString = scoresDir + "scores.json";
const char* scoresFilePath = scoresFilePathString.c_str();
FILE* scoresFile = fopen(scoresFilePath, "r");
std::cout << scoresFilePath;
char readBuffer[65536];
FileReadStream is(scoresFile, readBuffer, sizeof(readBuffer));
scores.ParseStream(is);
fclose(scoresFile);
highScoreValue = scores["High Score"].GetInt();
}
void GameData::writeScores()
{
std::string scoresDir = cocos2d::FileUtils::getInstance()->getWritablePath();
std::string scoresFilePathString = scoresDir + "scores.json";
const char* scoresFilePath = scoresFilePathString.c_str();
FILE* scoresFile = fopen(scoresFilePath, "w");
char writeBuffer[65536];
FileWriteStream os(scoresFile, writeBuffer, sizeof(writeBuffer));
Writer<FileWriteStream> writer(os);
scores.Accept(writer);
fclose(scoresFile);
}
/*void GameData::createScores()
{
scores.SetObject();
Document::AllocatorType& allocator = scores.GetAllocator();
scores.AddMember("High Score", 0, allocator);
}*/
void GameData::createScores()
{
CrtAllocator crtAllocator;
GenericDocument<rapidjson::UTF8<>, rapidjson::CrtAllocator> scores(&crtAllocator);
scores.SetObject();
CrtAllocator& allocator = scores.GetAllocator();
scores.AddMember("High Score", 0, allocator);
std::cout << &scores;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment