Added error checking for config.json

This commit is contained in:
2021-03-08 20:51:58 -05:00
parent fe13bac080
commit de99b127b1
10 changed files with 161 additions and 52 deletions

View File

@ -59,6 +59,7 @@ int last_time;
int current_time;
int diff_time;
Game::Game() {
// TODO Auto-generated constructor stub
@ -94,6 +95,7 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g
else
{
SDL_SetRenderDrawColor(renderer, 255,255,255,255);
SDL_SetHint(SDL_HINT_RENDER_VSYNC, "1");
isRunning = true;
}
//Initialize SDL_mixer
@ -119,9 +121,10 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g
map = new Map("terrain",globalScale,16);
const char* myText = "Find lost puppies!";
std::string myText = "Find lost puppies!\nThey need your help!";
text = new UIText(myText, "font", 0, 0, 8, 12, globalScale);
text->ParseString(myText, 12, 22, globalScale);
text->ParseString(myText, 12, 22, globalScale, "text");
SDL_Rect myDestRect = SDL_Rect();
myDestRect.x = 12;
myDestRect.y = 8;
@ -145,6 +148,16 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g
puppy.addComponent<SpriteComponent>("puppy", SpriteComponent::spriteObject);
puppy.addGroup(groupObjects);
// const char* strA = "r";
// const char* strB = "r";
//
// if (strcmp(strA,strB)==0)
// {
// printf("strcomp evaluated to true");
// } else {
// printf("strcomp false or not evaluated");
// }
// enemy.addComponent<TransformComponent>(180*globalScale,180*globalScale,32,32,globalScale);
// enemy.addComponent<SpriteComponent>("robber", SpriteComponent::spriteAnimation, "assets/textures/actors/robberrodent.json");
// enemy.addGroup(groupEnemies);
@ -186,12 +199,24 @@ void Game::update()
SDL_Rect playerCol = player.getComponent<ColliderComponent>().collider;
Vector2D playerPos = player.getComponent<TransformComponent>().position;
if (Mix_PlayingMusic() == 0)
// if (gsm->currentState == GameStateManager::ST_INIT)
// {
// const char* initText = "Loading...";
// text->ParseString(initText,12,22,gScale,"init");
// const char* titleText = "Beagle Rescue";
// const char* gameOverText = "Game Over";
// }
if (Mix_PlayingMusic() == 0 && gsm->currentState == GameStateManager::ST_COREGAME)
{
// std::cout << "Play Music Now" << std::endl;
// Mix_PlayMusic(assets->GetMusicTrack("simonZ"), -1);
Mix_PlayMusic(assets->GetMusicTrack("simonZ"), -1);
}
if (Mix_PlayingMusic() != 0 && gsm->currentState != GameStateManager::ST_COREGAME)
{
Mix_HaltMusic();
}
// if (Mix_PlayChannel(-1, Game::assets->GetSoundClip("bark1"),0) == 0)
// {
// Mix_PlayChannel(-1, Game::assets->GetSoundClip("bark1"),0);
@ -274,7 +299,7 @@ void Game::render()
{
p->draw();
}
if (gsm->currentState==GameStateManager::ST_TITLESCREEN){
if (gsm->currentState==GameStateManager::ST_TITLESCREEN||gsm->currentState==GameStateManager::ST_INIT||gsm->currentState==GameStateManager::ST_GAMEOVER){
for (auto& guiElement : gui)
{
guiElement->draw();

View File

@ -25,20 +25,21 @@ int main(int argc, const char * argv[])
// =============================
// Load cJSON config.json file
// =============================
// Starting with Error Checking
std::string configPath = "src/config/config.json";
std::ifstream fin(configPath);
if(fin.is_open()){
// std::cout<<"config.json is opened successfully"<<std::endl;
std::ifstream jsonText("src/config/config.json");
std::ostringstream tmp;
tmp << jsonText.rdbuf();
std::string json = tmp.str();
cJSON * myJSON = cJSON_Parse(json.c_str());
cJSON * windowName = cJSON_GetObjectItemCaseSensitive(myJSON, "WindowName");
// if (cJSON_IsString(windowName) && (windowName->valuestring != NULL))
// {
// printf("Window Name is: %s\n", windowName->valuestring);
// }
cJSON * windowSize = cJSON_GetObjectItem(myJSON, "WindowSize");
int windowWidth = cJSON_GetObjectItem(windowSize, "w")->valueint;
int windowHeight = cJSON_GetObjectItem(windowSize, "h")->valueint;
// printf("Window:\nwidth:%d\nheight:%d\n",windowWidth,windowHeight);
int windowFS = cJSON_GetObjectItem(myJSON, "WindowFullScreen")->valueint;
int globalScale = cJSON_GetObjectItem(myJSON, "GlobalScale")->valueint;
bool isWindowFS;
@ -73,5 +74,15 @@ int main(int argc, const char * argv[])
}
game->clean();
} else {
std::cout<<"config.json not found or opened"<<std::endl;
}
if(fin.fail()){
std::cout<<"config.json load failed"<<std::endl;
} else{
// std::cout<<"config.json loaded"<<std::endl;
}
return 0;
}