diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..f9446f0 Binary files /dev/null and b/.DS_Store differ diff --git a/Enemy.cpp b/Enemy.cpp new file mode 100644 index 0000000..eb3afb7 --- /dev/null +++ b/Enemy.cpp @@ -0,0 +1,153 @@ +#include "Enemy.h" +#include + +//SDL_Rect cameraRect; + +SpriteManager *enemySpriteManager = NULL; + +bool isAlert = false; +bool isHit = false; +bool isDead = false; + +int last_rand; // ensure same number doesn't happen twice in a row + + +// strength is the % chance to attempt to hit the player +Enemy::Enemy(int health, int block_chance, int strength){ + this->health = health; + this->block_chance = block_chance; + this->strength = strength; + + punching = false; +} + +Enemy::~Enemy(){ + +} + +void Enemy::initEnemy() +{ + enemySpriteManager = new SpriteManager("assets/KK_ENEMY1_HIT2.png",500,0); +} + +// Detects if a given game object is within the given range +bool objectDetected(int playerX, int range) { + bool detected = false; + int xDist = enemySpriteManager->getCharacterXpos() - playerX; + if (abs(xDist) <= range) detected = true; + return detected; +} + +// checks if playerX is within hit range +bool withinHitRange(int playerX) { + int xDist = enemySpriteManager->getCharacterXpos() - playerX; + return (abs(xDist) <= 100); // temp +} + +// determine if enemy will hit player +bool Enemy::hitPlayer(int &flag) { + srand(time(0)); + int rand_num = rand() % 100 + 1; + if (rand_num != last_rand && rand_num < strength) { + srand(time(0)); + flag = 5; + std::cout << rand_num << std::endl; + last_rand = rand_num; + return true; + } + return false; +} + +bool Enemy::blocked(int &flag) { + // if player is punching, % chance to block + srand(time(0)); + int rand_num = rand() % 100 + 1; // value btwn 1 and 100 + //std::cout << rand_num << std::endl; + if (rand_num < block_chance) std::cout << "BLOCK" << std::endl; + return (rand_num < block_chance); +} + +// isHit is given to determine if enemy should react to being hit +// flag table: +// 0 - calm +// 1 - moving +// 2 - injured +// 3 - dead +// 4 - block +// 5 - punch +void Enemy::updateEnemy(SDL_Rect& cameraRect, int playerX){ + int flag = 0; + int step = 0; + if (!isDead) { + // if isHit == true, react to hit + if (isHit) { + if (!blocked(flag)) { + //std::cout << "NOT BLOCKED" << std::endl; + flag = 2; + step = 0; + health -= 5; + if (health <= 0) isDead = true; + //std::cout << "HEALTH: " << health << std::endl; + } + isHit = false; + } else { + // if MC within range, follow keeping 50 sprites away + isAlert = objectDetected(playerX, 250); + if (isAlert) { + //std::cout << "ALERT" << std::endl; + flag = 1; + // check if within hitting range + // if not, move to player + if (withinHitRange(playerX)) { + // enter fight loop + if (hitPlayer(flag)) { + std::cout << "THROW PUNCH" << std::endl; + punching = true; + } else punching = false; + + } else { + // move to player + if (getEnemyXpos() - playerX < 0) step = 5; + else step = -5; + } + } + } + } else flag = 3; + enemySpriteManager-> updateEnemySprite(flag, step); + +} + + + +void Enemy::renderEnemy(SDL_Rect& cameraRect){ + //std::cout << "ENEMY: "; + enemySpriteManager -> renderSprite(cameraRect); +} + +int Enemy::getEnemyXpos() { + return enemySpriteManager->getCharacterXpos(); +} + +int Enemy::getEnemyYpos() { + return enemySpriteManager->getCharacterYpos(); +} + +void Enemy::setAlertFlag(bool flag) { + isAlert = flag; +} + +void Enemy::setHitFlag(bool flag) { + isHit = flag; +} + +bool Enemy::enemyIsDead() { + return isDead; +} + +bool Enemy::enemyThrewPunch() { + return punching; +} + + + + diff --git a/Enemy.h b/Enemy.h new file mode 100644 index 0000000..6efd43b --- /dev/null +++ b/Enemy.h @@ -0,0 +1,41 @@ +#ifndef _ENEMY_H_ +#define _ENEMY_H_ + + + +#include "GameEngine.h" + +extern SDL_Rect cameraRect; + +class Enemy{ + public: + Enemy(int health, int block_chance, int strength); + ~Enemy(); + + void initEnemy(); + void updateEnemy(SDL_Rect& cameraRect, int playerX); + void renderEnemy(SDL_Rect& cameraRect); + + // ADDED FOR ENEMY COLLISION + int getEnemyXpos(); + int getEnemyYpos(); + + void setAlertFlag(bool flag); + void setHitFlag(bool flag); + + bool enemyIsDead(); + bool blocked(int &flag); + bool enemyThrewPunch(); // tell if enemy has thrown a punch + + private: + int health; + int block_chance; + int strength; + + bool punching; + + bool hitPlayer(int &flag); // try to hit player + +}; + +#endif diff --git a/GameEngine.cpp b/GameEngine.cpp index 8094888..6441419 100644 --- a/GameEngine.cpp +++ b/GameEngine.cpp @@ -13,16 +13,24 @@ const int LEVEL_HEIGHT = 960; const int STEPX = 10; + SDL_Event input; + bool flag_left = false; bool flag_right = false; +bool flag_punch = false; // ADDED BY KALEB + +bool paused = false; + int leftcount = 0; int rightcount = 0; +int esccount = 0; int start = 0; //object definition TileMap *tileMap = NULL; GameObject *karateKid = NULL; +Enemy *enemy1 = NULL; SDL_Renderer* GameEngine::renderer = NULL; ScreenManager *startScreen = NULL; @@ -41,7 +49,7 @@ int GameEngine::initGameEngine(const char* title, int xpos, int ypos, int width, flags = SDL_WINDOW_FULLSCREEN; } - + SDL_Init(SDL_INIT_TIMER); if (SDL_Init(SDL_INIT_EVERYTHING) != 0) { std::cout << "Error initializing SDL: " << SDL_GetError() << std::endl; return 0; @@ -52,11 +60,10 @@ int GameEngine::initGameEngine(const char* title, int xpos, int ypos, int width, std::cout << "Windown created successfully" << std::endl; } - renderer = SDL_CreateRenderer(window,-1,0); + renderer = SDL_CreateRenderer(window,-1,SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED); if(renderer){ std::cout << "Renderer created successfully" << std::endl; } - startScreen = new ScreenManager(); isRunning =true; @@ -64,16 +71,31 @@ int GameEngine::initGameEngine(const char* title, int xpos, int ypos, int width, SDL_SetRenderDrawColor(renderer, 192, 238, 254, 1); //object initialization karateKid = new GameObject(); + enemy1 = new Enemy(20, 50, 50); // enemy w/ health = 20, block_chance = 50%, strength = 50% tileMap = new TileMap(); karateKid->initGameObject(); + enemy1->initEnemy(); return 0; } +// ADDED BY KALEB +// Tell if player is within given range to an enemy +// returns false if enemy is dead +bool playerAtEnemy(Enemy *enem, int range) { + if (!enem->enemyIsDead()) { + int playerX = karateKid->getObjectXpos(); + int enemyX = enem->getEnemyXpos(); + int xDist = playerX - enemyX; + if (abs(xDist) <= range) return true; + } + return false; +} + void GameEngine::handleGameEngineEvents(){ SDL_Event input; @@ -90,60 +112,107 @@ void GameEngine::handleGameEngineEvents(){ { case SDLK_LEFT: if(startScreen->getState() == 0){ - flag_left = true; - if (leftcount == 2){ - leftcount = 0; - } - else{ - leftcount++; - } - } - break; + flag_left = true; + if (leftcount == 2){ + leftcount = 0; + } + else{ + leftcount++; + } + } + break; case SDLK_RIGHT: - if(startScreen->getState() == 0){ - flag_right = true; - if (rightcount == 2){ - rightcount = 0; + if(startScreen->getState() == 0){ + if (playerAtEnemy(enemy1, 50)) flag_right = false; // added + else flag_right = true; // modified + if (rightcount == 2){ + rightcount = 0; + } + else{ + rightcount++; + } } - else{ - rightcount++; - } - } break; - case SDLK_RETURN: + case SDLK_RETURN: if(startScreen->getState() == 1){ startScreen->chanageState(0); } break; - - //TODO: temp until we have a way to end the game - case SDLK_SPACE: - startScreen->chanageState(2); - break; - } + // ADDED BY KALEB + case SDLK_SPACE: + // player has punched + flag_punch = true; + // if the player is within hit range, updateEnemy to react to hit + if (playerAtEnemy(enemy1, 70)) + enemy1->setHitFlag(true); + break; + + case SDLK_ESCAPE: + if(esccount == 0) + { + esccount = 1; + } + if(esccount == 1) + { + startScreen->chanageState(3); + paused = true; + esccount = 2; + } + else{ + startScreen->chanageState(0); + esccount = 0; + paused = false; + + } + + break; + - } + + } + + } } } +void doBattle(GameObject *karateKid, Enemy *enem) { + // if enemy has punched, decrement health + if (enem->enemyThrewPunch()) { + karateKid->alterHealth(-20); + } +} + void GameEngine::updateGameEngine(SDL_Rect& cameraRect){ - karateKid->updateGameObject(cameraRect); + if (!paused) { + karateKid->updateGameObject(cameraRect); + enemy1->updateEnemy(cameraRect, karateKid->getObjectXpos()); + // temp(?) function for handling battle between player and enemy + doBattle(karateKid, enemy1); + if (karateKid->getObjectHealth() <= 0) {}//std::cout << "GAME OVER" << std::endl; + } } void GameEngine::renderGameEngine(SDL_Rect& cameraRect){ - SDL_RenderClear(renderer); tileMap->drawTileMap(cameraRect); karateKid->renderGameObject(cameraRect); + enemy1->renderEnemy(cameraRect); + + /* screen manager actions */ if(startScreen->getState() == 1){ startScreen->initScreen(); } else if(startScreen->getState() == 2){ startScreen->endScreen(); } + else if(startScreen->getState() == 3){ + startScreen->pauseScreen(); + } + /* end screen manager actions */ + SDL_RenderPresent(renderer); } diff --git a/GameEngine.h b/GameEngine.h index 7fd8278..38a0f5d 100644 --- a/GameEngine.h +++ b/GameEngine.h @@ -17,11 +17,12 @@ #include "TextureManager.h" #include "GameObject.h" +#include "Enemy.h" #include "TileMap.h" #include "SpriteManager.h" #include "ScreenManager.h" - +extern Uint32 frameStart; extern const int LEVEL_WIDTH; extern const int LEVEL_HEIGHT; @@ -37,11 +38,17 @@ extern const int MC_IMG_DEST; extern bool flag_left; extern int leftcount; //for left movement extern int rightcount; //for right movement +extern int esccount; extern bool flag_right; + extern int start; // to place the player in starting position +extern bool flag_punch; // for player punching +extern bool paused; //character step on xaxis extern const int STEPX; +extern SDL_Event input; + class GameEngine @@ -61,7 +68,6 @@ class GameEngine bool running(){ return isRunning;} static SDL_Renderer* renderer; - static SDL_Event input; private: diff --git a/GameObject.cpp b/GameObject.cpp index a0ae3aa..93a1c74 100644 --- a/GameObject.cpp +++ b/GameObject.cpp @@ -7,7 +7,7 @@ SpriteManager *spriteManager = NULL; GameObject::GameObject(){ - + health = 100; // ADDED BY KALEB } GameObject::~GameObject(){ @@ -28,7 +28,7 @@ void GameObject::updateGameObject(SDL_Rect& cameraRect){ void GameObject::renderGameObject(SDL_Rect& cameraRect){ - + //std::cout << "MC: "; spriteManager -> renderSprite(cameraRect); } @@ -58,3 +58,24 @@ void GameObject::setCamera( SDL_Rect& cameraRect ) } } + +int GameObject::getObjectXpos() { + return spriteManager->getCharacterXpos(); +} + +int GameObject::getObjectYpos() { + return spriteManager->getCharacterYpos(); +} + +int GameObject::getObjectHealth() { + return health; +} + +// increment/decrement health by given value +void GameObject::alterHealth(int val) { + health += val; + std::cout << "HEALTH NOW: " << health << std::endl; +} + + + diff --git a/GameObject.h b/GameObject.h index 17269d6..ec3135a 100644 --- a/GameObject.h +++ b/GameObject.h @@ -17,7 +17,18 @@ class GameObject{ void renderGameObject(SDL_Rect& cameraRect); void setCamera( SDL_Rect& cameraRect ); - + + // ADDED FOR ENEMY COLLISION + int getObjectXpos(); + int getObjectYpos(); + int getObjectHealth(); + + // ADDED BY KALEB + void alterHealth(int val); // increment/decrement health by given value + + private: + int health; // ADDED BY KALEB + }; diff --git a/ScreenManager.cpp b/ScreenManager.cpp index ea9d179..a0642cf 100644 --- a/ScreenManager.cpp +++ b/ScreenManager.cpp @@ -1,5 +1,6 @@ #include "ScreenManager.h" + ScreenManager::ScreenManager(){ state = 1; } @@ -40,6 +41,7 @@ void ScreenManager::initScreen(){ SDL_Rect title_dest = {200, 150, 500, 100}; SDL_RenderCopy(GameEngine::renderer, my_texture, NULL, &title_dest); SDL_FreeSurface(temp); + TTF_CloseFont(font); font = TTF_OpenFont("assets/arcadeclassic.regular.ttf", 15); SDL_Color gold = {245, 200, 66}; @@ -48,8 +50,9 @@ void ScreenManager::initScreen(){ SDL_Rect text_dest = {100, 400, 600, 50}; SDL_RenderCopy(GameEngine::renderer, my_texture, NULL, &text_dest); SDL_FreeSurface(temp); + TTF_CloseFont(font); } - + } void ScreenManager::endScreen(){ @@ -69,5 +72,36 @@ void ScreenManager::endScreen(){ SDL_SetRenderDrawColor(GameEngine::renderer, 192, 238, 254, 1); SDL_FreeSurface(temp); + TTF_CloseFont(font); } -} \ No newline at end of file +} + +void ScreenManager::pauseScreen(){ + if(state == 3){ + TTF_Init(); + SDL_Rect end = {50, 50, SCREEN_WIDTH-100, SCREEN_HEIGHT-100}; + SDL_SetRenderDrawColor(GameEngine::renderer, 0, 0, 0, 255); + SDL_RenderFillRect(GameEngine::renderer, &end); + + SDL_Texture* my_texture = NULL; + TTF_Font* font = TTF_OpenFont("assets/arcadeclassic.regular.ttf", 30); + SDL_Color gold = {245, 200, 66}; + SDL_Surface* temp = TTF_RenderText_Solid(font, "Game Paused!", gold); + my_texture = SDL_CreateTextureFromSurface(GameEngine::renderer, temp); + SDL_Rect title_dest = {175, 250, 500, 100}; + SDL_RenderCopy(GameEngine::renderer, my_texture, NULL, &title_dest); + + SDL_SetRenderDrawColor(GameEngine::renderer, 192, 238, 254, 1); + SDL_FreeSurface(temp); + TTF_CloseFont(font); + + font = TTF_OpenFont("assets/arcadeclassic.regular.ttf", 15); + SDL_Color white = {255, 255, 255}; + temp = TTF_RenderText_Solid(font, "Press Esc to Resume", white); + my_texture = SDL_CreateTextureFromSurface(GameEngine::renderer, temp); + SDL_Rect text_dest = {100, 400, 600, 50}; + SDL_RenderCopy(GameEngine::renderer, my_texture, NULL, &text_dest); + SDL_FreeSurface(temp); + TTF_CloseFont(font); + } +} diff --git a/ScreenManager.h b/ScreenManager.h index 45d5cc2..395cf6b 100644 --- a/ScreenManager.h +++ b/ScreenManager.h @@ -6,15 +6,16 @@ class ScreenManager{ private: int state; - + public: ScreenManager(); ~ScreenManager(); void initScreen(); void endScreen(); + void pauseScreen(); void chanageState(int); int getState(); }; -#endif \ No newline at end of file +#endif diff --git a/SpriteManager.h b/SpriteManager.h index 3e55fd8..0065ab6 100644 --- a/SpriteManager.h +++ b/SpriteManager.h @@ -10,7 +10,9 @@ class SpriteManager{ void updateSprite(); + void updateEnemySprite(int flag, int step); // ADDED FOR ENEMY void renderSprite(SDL_Rect& cameraRect); + //Position accessors int getCharacterXpos(); diff --git a/Spritemanager.cpp b/Spritemanager.cpp index ebf2e49..128cf7c 100644 --- a/Spritemanager.cpp +++ b/Spritemanager.cpp @@ -1,6 +1,14 @@ #include "SpriteManager.h" +//#include "SDL2/SDL_timer.h" int check = 0; +int lastPosition; // ADDED BY KALEB - last position of the srcRect +int lastSecond = 0; // ADDED BY KALEB - for timing animations + + + +bool punchAction_flag = 0 ; + SpriteManager::SpriteManager(const char* texture,int x, int y) { @@ -20,13 +28,13 @@ void SpriteManager::updateSprite(){ srcRect.y = 0; srcRect.w = MC_IMG_SRC; srcRect.h = MC_IMG_SRC; - + if(start == 0){ srcRect.x = 1600; xpos = SpriteManager::getCharacterXpos() + STEPX; destRect.x = xpos; start = 1 ; - + lastPosition = srcRect.x; // save lastPosition (don't stay on fight anim) } else{ if(flag_right == true){ @@ -40,6 +48,7 @@ void SpriteManager::updateSprite(){ xpos = SpriteManager::getCharacterXpos() + STEPX; destRect.x = xpos; flag_right =false; + lastPosition = srcRect.x; // save lastPosition (don't stay on fight anim) } else if(flag_left == true){ @@ -52,6 +61,39 @@ void SpriteManager::updateSprite(){ xpos = SpriteManager::getCharacterXpos() - STEPX; destRect.x = xpos; flag_left =false; + lastPosition = srcRect.x; // save lastPosition (don't stay on fight anim) + } + + // ADDED BY KALEB + else if (flag_punch == true) { + // lastPosition = srcRect.x; // save lastPosition (don't stay on fight anim) + // + // lastSecond = SDL_GetTicks() / 500; + // gSpriteClips[ 0 ].x = 2800; + // gSpriteClips[ 0 ].y = 0; + // gSpriteClips[ 0 ].w = MC_IMG_SRC; + // gSpriteClips[ 0 ].h = MC_IMG_SRC; + // + // gSpriteClips[ 1 ].x = 2800; + // gSpriteClips[ 1 ].y = 0; + // gSpriteClips[ 1 ].w = MC_IMG_SRC; + // gSpriteClips[ 1 ].h = MC_IMG_SRC; + // + // gSpriteClips[ 2 ].x = 2800; + // gSpriteClips[ 2 ].y = 0; + // gSpriteClips[ 2 ].w = MC_IMG_SRC; + // gSpriteClips[ 2 ].h = MC_IMG_SRC; + // + // gSpriteClips[ 3 ].x = 2800; + // gSpriteClips[ 3 ].y = 0; + // gSpriteClips[ 3 ].w = MC_IMG_SRC; + // gSpriteClips[ 3 ].h = MC_IMG_SRC; + + punchAction_flag = 1; + + srcRect.x = 2800; + flag_punch = false; + //std::cout << "Flag punch set false" << std::endl; } @@ -67,6 +109,67 @@ void SpriteManager::updateSprite(){ } +// ADDED BY KALEB +// Updates enemy sprite based on flag given and step +// step is the x-direction movement +// flag table: +// 0 - calm +// 1 - moving +// 2 - injured +// 3 - dead +// 4 - block +// 5 - punch +void SpriteManager::updateEnemySprite(int flag, int step) { + srcRect.y = 0; + srcRect.w = MC_IMG_SRC; + srcRect.h = MC_IMG_SRC; + + // if calm + if (flag == 0) { + srcRect.y = 0; + srcRect.x = 1600; + xpos = SpriteManager::getCharacterXpos(); + } + // if moving + if (flag == 1) { + srcRect.y = 0; + // if step < 0, move left + if (step <= 0) { + srcRect.x = 1200; + } + // else moving right + else { + srcRect.x = 1600; + } + xpos = SpriteManager::getCharacterXpos() + step; + } + // if hit + // show red + if (flag == 2) { + //std::cout << "HIT!" << std::endl; + //lastPosition = srcRect.x; + srcRect.y = srcRect.h*2; + srcRect.x = srcRect.w*3; + } + // if dead + if (flag == 3) { + srcRect.y = srcRect.h*2; + srcRect.x = 0; + } + // if block + if (flag == 4) { + srcRect.y = 0; + srcRect.x = srcRect.w*2; + } + // if punch + if (flag == 5) { + srcRect.y = 0; + srcRect.x = 0; + } + destRect.x = xpos; + destRect.y = 320; +} + int SpriteManager::getCharacterXpos() { return xpos; @@ -83,6 +186,29 @@ void SpriteManager::renderSprite(SDL_Rect& camera ) if(destRect.x>LEVEL_WIDTH-150){ destRect.x = LEVEL_WIDTH-150; } - TextureManager::renderTexture(999, destRect.x-camera.x, destRect.y ,objTexture, &srcRect); + //when the punch action flag is activated the below code captures the current time and renders the punch texture + //for 2 seconds + /*if(punchAction_flag ==1){ + int max = SDL_GetTicks()+2000; + + for(int i = SDL_GetTicks(); i < max; i++){ + std::cout << max << " " << i << std::endl; + TextureManager::renderTexture(999, destRect.x-camera.x, destRect.y ,objTexture, &srcRect); + } + punchAction_flag=0; + } + else{ + TextureManager::renderTexture(999, destRect.x-camera.x, destRect.y ,objTexture, &srcRect); + }*/ + TextureManager::renderTexture(999, destRect.x-camera.x, destRect.y ,objTexture, &srcRect); + //std::cout << destRect.x-camera.x << ", " << destRect.y << std::endl; + + // ADDED BY KALEB + // a poor workaround for timing of animations + int ticks = SDL_GetTicks() / 500; + if ((ticks) != lastSecond) { + srcRect.x = lastPosition; + lastSecond = ticks; + } } diff --git a/TileMap.cpp b/TileMap.cpp index d536c39..721bd9b 100644 --- a/TileMap.cpp +++ b/TileMap.cpp @@ -12,7 +12,7 @@ int lvl1[4][25]={ TileMap::TileMap(){ tileMapTexture = TextureManager::loadTexture("EnvTiles.png"); loadTileMap(lvl1); - std::cout << lvl1 << '\n'; + //std::cout << lvl1 << '\n'; srcRect.w = 400; srcRect.h = 400; } diff --git a/assets/KK_ENEMY1.png b/assets/KK_ENEMY1.png new file mode 100644 index 0000000..381e7fa Binary files /dev/null and b/assets/KK_ENEMY1.png differ diff --git a/assets/KK_ENEMY1_HIT.png b/assets/KK_ENEMY1_HIT.png new file mode 100644 index 0000000..3e42d2c Binary files /dev/null and b/assets/KK_ENEMY1_HIT.png differ diff --git a/assets/KK_ENEMY1_HIT1.png b/assets/KK_ENEMY1_HIT1.png new file mode 100644 index 0000000..dce2812 Binary files /dev/null and b/assets/KK_ENEMY1_HIT1.png differ diff --git a/assets/KK_ENEMY1_HIT2.png b/assets/KK_ENEMY1_HIT2.png new file mode 100644 index 0000000..880aeb5 Binary files /dev/null and b/assets/KK_ENEMY1_HIT2.png differ diff --git a/assets/KK_ENEMY1_HIT2.xcf b/assets/KK_ENEMY1_HIT2.xcf new file mode 100644 index 0000000..8007dfd Binary files /dev/null and b/assets/KK_ENEMY1_HIT2.xcf differ diff --git a/main.cpp b/main.cpp index b67211f..15b8a00 100644 --- a/main.cpp +++ b/main.cpp @@ -1,3 +1,4 @@ + #include "GameEngine.h" @@ -10,7 +11,8 @@ int main(){ //maxtime between frames Uint32 frameStart; - int frameTime; + int frameTime, lastTime, deltaTime; + int currentTime = 0; SDL_Rect camera = { 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT }; gameEngine = new GameEngine(); @@ -18,6 +20,14 @@ int main(){ gameEngine->initGameEngine("MY GAME",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,SCREEN_WIDTH,SCREEN_HEIGHT,false); while(gameEngine->running()){ + Uint64 start = SDL_GetPerformanceCounter(); + + //currentTime holds the game clock time and is only incremented when the game is not paused + //the game is paused and resumed using the esc key + //a variable "esccount" declared in the GameEngine counts the number of times the esc key is pressed. + //initallly it is set to 0 and on the 1st click it will be set to 1 which will cause the pause screen to appear and the gameClock(currentTime) to stop from further incrementing(since it will cause paused=true) + //when the second esc key press in detected the esccount is set to 0 which caused the pause screen to disappear and the gameClock to resume + frameStart = SDL_GetTicks(); // time since we first initialized SDL @@ -29,6 +39,11 @@ int main(){ if(frameDelay > frameTime){ SDL_Delay(frameDelay - frameTime); } + + Uint64 end = SDL_GetPerformanceCounter(); + + float elapsed = (end - start) / (float)SDL_GetPerformanceFrequency(); + //std::cout << "Current FPS: " << (1.0f / elapsed) << std::endl; } gameEngine->cleanGameEngine(); diff --git a/makefile b/makefile index 06dd4cd..ed38527 100644 --- a/makefile +++ b/makefile @@ -1,2 +1,2 @@ all: - g++ main.cpp GameEngine.cpp TextureManager.cpp GameObject.cpp TileMap.cpp SpriteManager.cpp ScreenManager.cpp `sdl2-config --cflags --libs` -lSDL2_image -lSDL2_ttf -std=c++11 + g++ main.cpp GameEngine.cpp TextureManager.cpp GameObject.cpp Enemy.cpp TileMap.cpp Spritemanager.cpp ScreenManager.cpp `sdl2-config --cflags --libs` -lSDL2_image -lSDL2_ttf -std=c++11 -o KarateKid