From 164d94eec1b88f0e9a466585b512fc8dca4c80ed Mon Sep 17 00:00:00 2001 From: manzerbredes Date: Mon, 4 May 2015 14:00:54 +0200 Subject: [PATCH] Add stats class, make a functionnal SFML game --- .../SFMLController/SFMLController.cpp | 2 +- src/Model/CMakeLists.txt | 2 +- src/Model/Game.cpp | 14 ++-- src/Model/Game.hpp | 8 +-- src/Model/Stats.cpp | 29 ++++++++ src/Model/Stats.hpp | 30 ++++++++ src/View/MainWindow.cpp | 68 +++++++++++++++---- src/View/MainWindow.hpp | 5 +- 8 files changed, 127 insertions(+), 31 deletions(-) create mode 100644 src/Model/Stats.cpp create mode 100644 src/Model/Stats.hpp diff --git a/src/Controllers/SFMLController/SFMLController.cpp b/src/Controllers/SFMLController/SFMLController.cpp index 3d5d4a1..bf4a069 100644 --- a/src/Controllers/SFMLController/SFMLController.cpp +++ b/src/Controllers/SFMLController/SFMLController.cpp @@ -57,7 +57,7 @@ void SFMLController::run(){ m_MainWindow.clearBG(); //m_game.swipe(kbdh::Direction::Left); std::vector > aaa=m_game.getGrid(); - m_MainWindow.drawGrid(aaa,m_game.isOver()); + m_MainWindow.drawGame(aaa,m_game.isOver(), m_game.getStats()); m_MainWindow.display(); //keyPress=this->waitArrowKeyPress(); diff --git a/src/Model/CMakeLists.txt b/src/Model/CMakeLists.txt index 888589e..4ebe544 100644 --- a/src/Model/CMakeLists.txt +++ b/src/Model/CMakeLists.txt @@ -1,2 +1,2 @@ #Make Model lib -add_library(Model Grid.cpp Game.cpp) +add_library(Model Grid.cpp Game.cpp Stats.cpp) diff --git a/src/Model/Game.cpp b/src/Model/Game.cpp index 9c67184..5781f54 100644 --- a/src/Model/Game.cpp +++ b/src/Model/Game.cpp @@ -4,7 +4,7 @@ //==================== Constructor and Destructor ==================== //Constructor -Game::Game() : Grid(), m_nbMove(0), m_score(0){ +Game::Game() : Grid(), m_stats(){ } //Destructor @@ -35,8 +35,8 @@ bool Game::swipe(kbdh::Direction direction){ } if(moveDone){ - m_score+=m_lastMoveScore; - m_nbMove++; + m_stats.incScore(m_lastMoveScore); + m_stats.incnbMove(); this->popRandomNumber(); } @@ -75,14 +75,10 @@ void Game::popRandomNumber(){ //==================== Getters and Setter ==================== //Retrieve the Score -int Game::getScore(){ - return m_score; +Stats Game::getStats(){ + return m_stats; } -//Retrieve the number of moves -int Game::getNbMove(){ - return m_nbMove; -} //std::vector > Game::getGrid(){ //return m_grid.getGrid(); diff --git a/src/Model/Game.hpp b/src/Model/Game.hpp index 77be7d7..e468176 100644 --- a/src/Model/Game.hpp +++ b/src/Model/Game.hpp @@ -11,6 +11,7 @@ #include #include "../Helpers/Keyboard.hpp" #include "Grid.hpp" +#include "Stats.hpp" #include class Game : public Grid @@ -18,9 +19,7 @@ class Game : public Grid private: //Members //Grid m_grid; - int m_score; - int m_nbMove; - + Stats m_stats; public: //Constructor and Destructor Game(); @@ -33,8 +32,7 @@ class Game : public Grid //bool isOver(); //Getters and Setters - int getScore(); - int getNbMove(); + Stats getStats(); //int maxStrLenInGrid(); //std::vector > getGrid(); }; diff --git a/src/Model/Stats.cpp b/src/Model/Stats.cpp new file mode 100644 index 0000000..8bada78 --- /dev/null +++ b/src/Model/Stats.cpp @@ -0,0 +1,29 @@ +#include "Stats.hpp" + + +Stats::Stats() : + m_score(0), + m_nbMove(0) +{ + +} + +Stats::~Stats(){ +} + + + +void Stats::incScore(int value){ + m_score+=value; +} +void Stats::incnbMove(){ + m_nbMove++; +} + + +int Stats::getScore(){ + return m_score; +} +int Stats::getNbMove(){ + return m_nbMove; +} diff --git a/src/Model/Stats.hpp b/src/Model/Stats.hpp new file mode 100644 index 0000000..49c7356 --- /dev/null +++ b/src/Model/Stats.hpp @@ -0,0 +1,30 @@ +#ifndef __STATS__ +#define __STATS__ + +#include + + + + + +class Stats{ + + private: + int m_score; + int m_nbMove; + public: + + Stats(); + ~Stats(); + void incScore(int value); + void incnbMove(); + + + int getScore(); + int getNbMove(); + + + +}; + +#endif diff --git a/src/View/MainWindow.cpp b/src/View/MainWindow.cpp index 659450f..23b8677 100644 --- a/src/View/MainWindow.cpp +++ b/src/View/MainWindow.cpp @@ -18,7 +18,7 @@ MainWindow::MainWindow(int width, int height, std::string title): //Set windows size m_windowSize=RenderWindow::getSize(); - m_gridPosition=sf::Vector2u(50,200); + m_gridPosition=sf::Vector2u(0,200); @@ -75,6 +75,11 @@ void MainWindow::drawGrid(std::vector > grid, bool gameIsOver){ m_gridSize.x=(grid.at(0).size()*m_cellSize.x)+(grid.at(0).size()*m_spaceBetweenCell)+m_spaceBetweenCell; m_gridSize.y=(grid.size()*m_cellSize.y)+(grid.size()*m_spaceBetweenCell)+m_spaceBetweenCell; + //Center: + m_gridPosition.x=m_windowSize.x/2-m_gridSize.x/2; + m_gridPosition.y=220; + + //Draw the grid sf::RectangleShape gridShape(sf::Vector2f(m_gridSize.x,m_gridSize.y)); gridShape.setFillColor(m_skin.at(2)); @@ -97,7 +102,6 @@ void MainWindow::drawGrid(std::vector > grid, bool gameIsOver){ if(gameIsOver) this->drawGameOver(gridX,gridY); - this->drawATH(); } @@ -177,7 +181,7 @@ void MainWindow::drawGameOver(int gridX, int gridY){ } -void MainWindow::drawATH(){ +void MainWindow::drawATH(Stats stats){ int titleX=m_gridPosition.x; int titleY=m_gridPosition.y-190; @@ -195,21 +199,57 @@ void MainWindow::drawATH(){ RenderWindow::draw(text); - //==================== Draw score ==================== + //==================== Draw best score ==================== - int scoreSizeX=110; - int scoreX=m_gridPosition.x+m_gridSize.x-scoreSizeX; + int scoreAndBestScoreFontSize(20); - sf::RectangleShape scoreShape(sf::Vector2f(scoreSizeX,60)); + int bestScoreSizeX=110; + int bestScoreSizeY=60; + int bestScoreX=m_gridPosition.x+m_gridSize.x-bestScoreSizeX; + int bestScoreY=titleY+25; + + sf::RectangleShape bestScoreShape(sf::Vector2f(bestScoreSizeX,bestScoreSizeY)); + bestScoreShape.setFillColor(m_skin.at(2)); + bestScoreShape.setPosition(bestScoreX,bestScoreY); + RenderWindow::draw(bestScoreShape); + + text.setString("BEST"); + text.setPosition(bestScoreX+bestScoreSizeY-scoreAndBestScoreFontSize-5,bestScoreY+12); + text.setCharacterSize(scoreAndBestScoreFontSize-5); + text.setColor(m_skin.at(7)); + + RenderWindow::draw(text); + + //==================== Draw score ==================== + + int scoreX=bestScoreX-bestScoreSizeX-5; + int scoreY=bestScoreY; + int scoreSizeX=bestScoreSizeX; + int scoreSizeY=bestScoreSizeY; + int scoreLength=std::to_string(stats.getScore()).size(); + + sf::RectangleShape scoreShape(sf::Vector2f(scoreSizeX,scoreSizeY)); scoreShape.setFillColor(m_skin.at(2)); - scoreShape.setPosition(scoreX,titleY+25); + scoreShape.setPosition(scoreX,scoreY); RenderWindow::draw(scoreShape); - //==================== Draw best score ==================== - - sf::RectangleShape bestScoreShape(sf::Vector2f(scoreSizeX,60)); - bestScoreShape.setFillColor(m_skin.at(2)); - bestScoreShape.setPosition(scoreX-scoreSizeX-5,titleY+25); - RenderWindow::draw(bestScoreShape); + text.setString("SCORE"); + text.setPosition(scoreX+scoreSizeY-scoreAndBestScoreFontSize-10,scoreY+12); + text.setCharacterSize(scoreAndBestScoreFontSize-5); + text.setColor(m_skin.at(7)); + + RenderWindow::draw(text); + + text.setString(std::to_string(stats.getScore())); + text.setPosition(scoreX+scoreSizeY-((scoreLength+20)/2)-scoreAndBestScoreFontSize+10,scoreY+scoreSizeY - scoreAndBestScoreFontSize-10); + text.setCharacterSize(scoreAndBestScoreFontSize); + text.setColor(sf::Color::White); + + RenderWindow::draw(text); +} + +void MainWindow::drawGame(std::vector > grid, bool gameIsOver, Stats stats){ + this->drawGrid(grid,gameIsOver); + this->drawATH(stats); } diff --git a/src/View/MainWindow.hpp b/src/View/MainWindow.hpp index 729b961..072a7ec 100644 --- a/src/View/MainWindow.hpp +++ b/src/View/MainWindow.hpp @@ -5,6 +5,7 @@ #include #include #include +#include "../Model/Stats.hpp" #include #include @@ -36,5 +37,7 @@ class MainWindow : public sf::RenderWindow{ void drawGameOver(int gridX, int griY); - void drawATH(); + void drawATH(Stats stats); + + void drawGame(std::vector > grid, bool gameIsOver, Stats stats); };