Added error checking for config.json

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

Binary file not shown.

View File

@ -3,5 +3,5 @@
"WindowName":"Beagle Rescue",
"WindowSize":{"w":320,"h":240},
"WindowFullScreen": 0,
"GlobalScale": 4
"GlobalScale": 3
}

View File

@ -12,3 +12,8 @@ void Entity::addGroup(Group mGroup)
groupBitset[mGroup] = true;
manager.AddToGroup(this, mGroup);
}
void Entity::setTag(std::string t)
{
this->tag = t;
}

View File

@ -37,7 +37,8 @@ template <typename T> inline ComponentID getComponentTypeID() noexcept
}
constexpr std::size_t maxComponents = 32;
constexpr std::size_t maxGroups =32;
constexpr std::size_t maxGroups = 32;
constexpr std::size_t maxEntities = 32;
using ComponentBitSet = std::bitset<maxComponents>;
using GroupBitset = std::bitset<maxGroups>;
@ -91,6 +92,14 @@ public:
groupBitset[mGroup] = false;
}
std::string tag;
void setTag(std::string t);
Entity* getEntity()
{
return this;
}
template <typename T> bool hasComponent() const
{
return componentBitSet[getComponentTypeID<T>()];
@ -124,6 +133,7 @@ class Manager
private:
std::vector<std::unique_ptr<Entity>> entities;
std::array<std::vector<Entity*>, maxGroups> groupedEntities;
std::vector<std::vector<Entity*>> taggedEntities;
public:
void update()
{
@ -160,6 +170,19 @@ public:
groupedEntities[mGroup].emplace_back(mEntity);
}
std::vector<Entity*> getEntitiesByTag(std::string t)
{
std::vector<Entity*> taggedEntities;
for (int e = 0; e<=entities.size(); e++)
{
if (entities[e]->tag == t)
{
taggedEntities.emplace_back(entities[e]->getEntity());
}
}
return taggedEntities;
}
std::vector<Entity*>& getGroup(Group mGroup)
{
return groupedEntities[mGroup];

View File

@ -39,18 +39,22 @@ public:
case SDLK_DOWN:
break;
case SDLK_LEFT:
if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
transform->velocity.x = -1;
if(Game::playerIsGrounded){
sprite->Play("Walk");
}
sprite->spriteFlip = SDL_FLIP_NONE;
}
break;
case SDLK_RIGHT:
if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
transform->velocity.x = 1;
if(Game::playerIsGrounded){
sprite->Play("Walk");
}
sprite->spriteFlip = SDL_FLIP_HORIZONTAL;
}
break;
case SDLK_k:
if (!Game::debugCollisionBoxes)
@ -59,6 +63,7 @@ public:
{Game::debugCollisionBoxes = false; }
break;
case SDLK_j:
if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
if (Mix_PlayChannel(-1, Game::assets->GetSoundClip("bwoop"),0) == 0)
{
Mix_PlayChannel(-1, Game::assets->GetSoundClip("bwoop"),0);
@ -66,6 +71,7 @@ public:
Game::gravityOnPlayer = true;
sprite->Play("Jump");
transform->velocity.y = -2;
}
break;
case SDLK_u:
Game::gsm->AdvanceState();
@ -82,36 +88,42 @@ public:
switch (Game::event.key.keysym.sym)
{
case SDLK_UP:
transform->velocity.y = 0;
// transform->velocity.y = 0;
// sprite->Play("idle");
// sprite->spriteFlip = SDL_FLIP_NONE;
break;
case SDLK_DOWN:
transform->velocity.y = 0;
// transform->velocity.y = 0;
// sprite->Play("idle");
// sprite->spriteFlip = SDL_FLIP_NONE;
break;
case SDLK_LEFT:
if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
transform->velocity.x = 0;
sprite->Play("Idle");
if (!Game::gravityOnPlayer){
Game::gravityOnPlayer = true;
// sprite->Play("Fall");
// sprite->Play("Fall");
}
}
break;
case SDLK_RIGHT:
if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
transform->velocity.x = 0;
sprite->Play("Idle");
if (!Game::gravityOnPlayer){
Game::gravityOnPlayer = true;
}
}
break;
case SDLK_k:
break;
case SDLK_j:
if(Game::gsm->currentState == GameStateManager::ST_COREGAME){
transform->velocity.y = 0;
sprite->Play("Fall");
Game::gravityOnPlayer = true;
}
break;
case SDLK_u:
break;

View File

@ -70,6 +70,21 @@ public:
spriteType = sType;
if(sType == spriteAnimation)
{
std::string bogusPath = "src/config/credits.json";
std::ifstream fin(bogusPath);
if(fin.is_open()){
std::cout<<"file is open"<<std::endl;
} else {
std::cout<<"file is NOT open"<<std::endl;
}
if(fin.fail()){
std::cout<<"file open fail"<<std::endl;
} else{
std::cout<<"file open success"<<std::endl;
}
std::ifstream jsonText(json);
std::ostringstream tmp;
tmp << jsonText.rdbuf();

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;
}

View File

@ -12,10 +12,11 @@
#include "string.h"
#include <iostream>
#include <stdio.h>
#include <vector>
extern Manager manager;
UIText::UIText(const char* text, std::string texId, int x, int y, int letterW, int letterH, int lScale)
UIText::UIText(std::string text, std::string texId, int x, int y, int letterW, int letterH, int lScale)
{
inputText = text;
textureID = texId;
@ -30,7 +31,7 @@ UIText::~UIText()
{
}
void UIText::ParseString(const char* inputText, int x, int y, int scale)
void UIText::ParseString(std::string inputText, int x, int y, int scale, std::string tag)
{
//Parse input text into an array of char
int posX = x;
@ -38,30 +39,47 @@ void UIText::ParseString(const char* inputText, int x, int y, int scale)
int i = 0;
// printf(inputText);
char current = inputText[i];
// const char* newLineChar{10};
// std::vector<char> writableStr(inputText.begin(), inputText.end());
// writableStr.push_back('\0');
do
{
// for (int i = 0; i < writableStr.size(); i++)
// {
// if (writableStr.at(i) == '\n')
// {
// printf("found new line");
// }
// }
++i;
if (strcmp(&current,"\n"))
if (strcmp(&current,"\n")!=0)
{
posX += letterWidth;
}
else
{
// printf("new line detected");
// printf("new line detected/n");
posX = x;
posY += letterHeight;
}
UIText::AddLetter(posX, posY, current);
UIText::AddLetter(posX, posY, current, tag);
current = inputText[i];
} while ((strcmp(&current,"\0"))!=0);
}
void UIText::AddLetter(int xpos, int ypos, char crnt)
void UIText::AddLetter(int xpos, int ypos, char crnt, std::string tag)
{
auto& letter(manager.addEntity());
letter.addComponent<TransformComponent>(xpos*scale, ypos*scale, letterWidth, letterHeight, 1);
// printf("Scale: %d\n",scale);
letter.addComponent<SpriteComponent>("font", SpriteComponent::spriteText, crnt, letterWidth, letterHeight, scale);
letter.setTag(tag);
letter.addGroup(Game::groupUI_Layer1);
}
// void UIText::RemoveLetter()
// {
// }

View File

@ -14,18 +14,18 @@
class UIText
{
public:
const char* inputText;
std::string inputText;
int letterHeight;
int letterWidth;
int posX;
int posY;
std::string textureID;
UIText(const char* inputText, std::string texID, int x, int y, int letterW, int letterH, int lScale);
UIText(std::string inputText, std::string texID, int x, int y, int letterW, int letterH, int lScale);
~UIText();
// void SetCharClips(SDL_Texture* fontTex, int x, int y, int letterW, int letterH);
void AddLetter(int xpos, int ypos, char crnt);
void ParseString(const char* inputText, int x, int y, int scale);
void AddLetter(int xpos, int ypos, char crnt, std::string tag);
void ParseString(std::string inputText, int x, int y, int scale, std::string tag);
int scale;
};