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", "WindowName":"Beagle Rescue",
"WindowSize":{"w":320,"h":240}, "WindowSize":{"w":320,"h":240},
"WindowFullScreen": 0, "WindowFullScreen": 0,
"GlobalScale": 4 "GlobalScale": 3
} }

View File

@ -12,3 +12,8 @@ void Entity::addGroup(Group mGroup)
groupBitset[mGroup] = true; groupBitset[mGroup] = true;
manager.AddToGroup(this, mGroup); 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 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 ComponentBitSet = std::bitset<maxComponents>;
using GroupBitset = std::bitset<maxGroups>; using GroupBitset = std::bitset<maxGroups>;
@ -91,6 +92,14 @@ public:
groupBitset[mGroup] = false; groupBitset[mGroup] = false;
} }
std::string tag;
void setTag(std::string t);
Entity* getEntity()
{
return this;
}
template <typename T> bool hasComponent() const template <typename T> bool hasComponent() const
{ {
return componentBitSet[getComponentTypeID<T>()]; return componentBitSet[getComponentTypeID<T>()];
@ -124,6 +133,7 @@ class Manager
private: private:
std::vector<std::unique_ptr<Entity>> entities; std::vector<std::unique_ptr<Entity>> entities;
std::array<std::vector<Entity*>, maxGroups> groupedEntities; std::array<std::vector<Entity*>, maxGroups> groupedEntities;
std::vector<std::vector<Entity*>> taggedEntities;
public: public:
void update() void update()
{ {
@ -160,6 +170,19 @@ public:
groupedEntities[mGroup].emplace_back(mEntity); 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) std::vector<Entity*>& getGroup(Group mGroup)
{ {
return groupedEntities[mGroup]; return groupedEntities[mGroup];

View File

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

View File

@ -70,6 +70,21 @@ public:
spriteType = sType; spriteType = sType;
if(sType == spriteAnimation) 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::ifstream jsonText(json);
std::ostringstream tmp; std::ostringstream tmp;
tmp << jsonText.rdbuf(); tmp << jsonText.rdbuf();

View File

@ -59,6 +59,7 @@ int last_time;
int current_time; int current_time;
int diff_time; int diff_time;
Game::Game() { Game::Game() {
// TODO Auto-generated constructor stub // TODO Auto-generated constructor stub
@ -94,6 +95,7 @@ void Game::init(const char *title, int width, int height, bool fullscreen, int g
else else
{ {
SDL_SetRenderDrawColor(renderer, 255,255,255,255); SDL_SetRenderDrawColor(renderer, 255,255,255,255);
SDL_SetHint(SDL_HINT_RENDER_VSYNC, "1");
isRunning = true; isRunning = true;
} }
//Initialize SDL_mixer //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); 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 = 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(); SDL_Rect myDestRect = SDL_Rect();
myDestRect.x = 12; myDestRect.x = 12;
myDestRect.y = 8; 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.addComponent<SpriteComponent>("puppy", SpriteComponent::spriteObject);
puppy.addGroup(groupObjects); 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<TransformComponent>(180*globalScale,180*globalScale,32,32,globalScale);
// enemy.addComponent<SpriteComponent>("robber", SpriteComponent::spriteAnimation, "assets/textures/actors/robberrodent.json"); // enemy.addComponent<SpriteComponent>("robber", SpriteComponent::spriteAnimation, "assets/textures/actors/robberrodent.json");
// enemy.addGroup(groupEnemies); // enemy.addGroup(groupEnemies);
@ -186,12 +199,24 @@ void Game::update()
SDL_Rect playerCol = player.getComponent<ColliderComponent>().collider; SDL_Rect playerCol = player.getComponent<ColliderComponent>().collider;
Vector2D playerPos = player.getComponent<TransformComponent>().position; 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; // 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) // if (Mix_PlayChannel(-1, Game::assets->GetSoundClip("bark1"),0) == 0)
// { // {
// Mix_PlayChannel(-1, Game::assets->GetSoundClip("bark1"),0); // Mix_PlayChannel(-1, Game::assets->GetSoundClip("bark1"),0);
@ -274,7 +299,7 @@ void Game::render()
{ {
p->draw(); 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) for (auto& guiElement : gui)
{ {
guiElement->draw(); guiElement->draw();

View File

@ -25,20 +25,21 @@ int main(int argc, const char * argv[])
// ============================= // =============================
// Load cJSON config.json file // 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::ifstream jsonText("src/config/config.json");
std::ostringstream tmp; std::ostringstream tmp;
tmp << jsonText.rdbuf(); tmp << jsonText.rdbuf();
std::string json = tmp.str(); std::string json = tmp.str();
cJSON * myJSON = cJSON_Parse(json.c_str()); cJSON * myJSON = cJSON_Parse(json.c_str());
cJSON * windowName = cJSON_GetObjectItemCaseSensitive(myJSON, "WindowName"); 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"); cJSON * windowSize = cJSON_GetObjectItem(myJSON, "WindowSize");
int windowWidth = cJSON_GetObjectItem(windowSize, "w")->valueint; int windowWidth = cJSON_GetObjectItem(windowSize, "w")->valueint;
int windowHeight = cJSON_GetObjectItem(windowSize, "h")->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 windowFS = cJSON_GetObjectItem(myJSON, "WindowFullScreen")->valueint;
int globalScale = cJSON_GetObjectItem(myJSON, "GlobalScale")->valueint; int globalScale = cJSON_GetObjectItem(myJSON, "GlobalScale")->valueint;
bool isWindowFS; bool isWindowFS;
@ -73,5 +74,15 @@ int main(int argc, const char * argv[])
} }
game->clean(); 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; return 0;
} }

View File

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

View File

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