summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/CMakeLists.txt2
-rw-r--r--src/Controllers/SFMLController/SFMLController.cpp1
-rw-r--r--src/Controllers/SFMLController/SFMLController.hpp1
-rw-r--r--src/Helpers/CMakeLists.txt3
-rw-r--r--src/Helpers/Skin.hpp17
-rw-r--r--src/Helpers/Skin/CMakeLists.txt1
-rw-r--r--src/Helpers/Skin/Skin.cpp118
-rw-r--r--src/View/CMakeLists.txt2
-rw-r--r--src/View/MainWindow.cpp23
-rw-r--r--src/View/MainWindow.hpp5
-rw-r--r--src/main.cpp10
-rw-r--r--src/skin/original/Pragmatica-Medium.ttfbin87416 -> 0 bytes
12 files changed, 166 insertions, 17 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index e7b7f73..6b4b21f 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -17,3 +17,5 @@ target_link_libraries(2P11 ${SFML_LIBRARIES} Model ConsoleController View SFMLCo
add_subdirectory(./Model)
add_subdirectory(./Controllers/)
add_subdirectory(./View/)
+add_subdirectory(./Helpers/)
+
diff --git a/src/Controllers/SFMLController/SFMLController.cpp b/src/Controllers/SFMLController/SFMLController.cpp
index 5a5960f..e2016b4 100644
--- a/src/Controllers/SFMLController/SFMLController.cpp
+++ b/src/Controllers/SFMLController/SFMLController.cpp
@@ -24,7 +24,6 @@ void SFMLController::run(){
//First pop
m_game.popRandomNumber();
-
//Start game
while(m_MainWindow.isOpen()){
diff --git a/src/Controllers/SFMLController/SFMLController.hpp b/src/Controllers/SFMLController/SFMLController.hpp
index 0847268..a695131 100644
--- a/src/Controllers/SFMLController/SFMLController.hpp
+++ b/src/Controllers/SFMLController/SFMLController.hpp
@@ -11,6 +11,7 @@
#include "../../View/MainWindow.hpp"
#include "../../Model/Game.hpp"
#include "../../Helpers/Keyboard.hpp"
+#include "../../Helpers/Skin.hpp"
diff --git a/src/Helpers/CMakeLists.txt b/src/Helpers/CMakeLists.txt
new file mode 100644
index 0000000..bde671e
--- /dev/null
+++ b/src/Helpers/CMakeLists.txt
@@ -0,0 +1,3 @@
+#Make Model lib
+
+add_subdirectory(./Skin/)
diff --git a/src/Helpers/Skin.hpp b/src/Helpers/Skin.hpp
new file mode 100644
index 0000000..6f01f18
--- /dev/null
+++ b/src/Helpers/Skin.hpp
@@ -0,0 +1,17 @@
+#include <SFML/Graphics/Color.hpp>
+#include <string>
+#include <vector>
+#include <fstream>
+#include <iostream>
+
+
+namespace skin{
+
+ std::vector<sf::Color> loadSkin(std::string skinName);
+
+ std::vector<std::string> removeComments(std::vector<std::string> linesVector);
+
+ bool containOnlySpace(std::string line);
+
+ std::vector<int> extractRGBA(std::string line);
+}
diff --git a/src/Helpers/Skin/CMakeLists.txt b/src/Helpers/Skin/CMakeLists.txt
new file mode 100644
index 0000000..8ad7f17
--- /dev/null
+++ b/src/Helpers/Skin/CMakeLists.txt
@@ -0,0 +1 @@
+add_library(Skin Skin.cpp)
diff --git a/src/Helpers/Skin/Skin.cpp b/src/Helpers/Skin/Skin.cpp
new file mode 100644
index 0000000..b421dae
--- /dev/null
+++ b/src/Helpers/Skin/Skin.cpp
@@ -0,0 +1,118 @@
+#include "../Skin.hpp"
+
+
+
+std::vector<sf::Color> skin::loadSkin(std::string skinName){
+
+ std::vector<sf::Color> skinLoaded;
+
+ std::ifstream skinFile("./bin/skin/"+skinName+"/skin.txt");
+
+
+ try{
+ if(!skinFile.is_open())
+ throw 1;
+
+ }
+ catch(int code){
+
+ std::cout << "Failed to open skin " + skinName + "check the location and the permissions !";
+ exit(code);
+ }
+
+
+ std::string line;
+ std::vector<std::string> linesVector;
+
+ while (std::getline(skinFile, line))
+ {
+ if(!line.empty() && !skin::containOnlySpace(line))
+ linesVector.push_back(line);
+ }
+
+ linesVector=skin::removeComments(linesVector);
+
+ for(int i=0;i<linesVector.size();i++){
+
+ std::vector<int> rgba;
+ rgba=skin::extractRGBA(linesVector.at(i));
+ if(rgba.size()==3)
+ skinLoaded.push_back(sf::Color(rgba.at(0), rgba.at(1), rgba.at(2)));
+ else
+ skinLoaded.push_back(sf::Color(rgba.at(0), rgba.at(1), rgba.at(2),rgba.at(3)));
+ }
+
+ return skinLoaded;
+}
+
+std::vector<std::string> skin::removeComments(std::vector<std::string> linesVector){
+
+ std::vector<std::string> linesWitouthCom;
+
+ for(int i=0;i<linesVector.size();i++){
+ std::string currentLine(linesVector.at(i));
+
+ if(currentLine[0]!='#'){
+ size_t posCom=currentLine.find("#");
+
+ if(posCom != std::string::npos)
+ linesWitouthCom.push_back(currentLine.erase(posCom,currentLine.size()));
+ else
+ linesWitouthCom.push_back(currentLine);
+
+ }
+ }
+
+ return linesWitouthCom;
+}
+
+
+
+bool skin::containOnlySpace(std::string line){
+ for(int i=0;i<line.size();i++){
+
+ if(line[i] != ' ')
+ return false;
+
+ }
+ return true;
+}
+
+
+std::vector<int> skin::extractRGBA(std::string line){
+ std::vector<int> rgba;
+
+ std::string numberFound;
+
+ int start=0;
+ try {
+ for(int j=0;j<3;j++){
+ for(int i=start;i<line.size();i++){
+ char ch=line.at(i);
+
+ if(ch>='0' && ch <='9'){
+ numberFound.push_back(ch);
+ }
+ else if(ch==' ' && numberFound.size() > 0){
+ start=i;
+ break;
+ }
+ }
+ rgba.push_back(std::stoi(numberFound));
+ numberFound.clear();
+ }
+
+
+ if(rgba.size()<3 || rgba.size()>4){
+ throw "Invalid thème format";
+ exit(1);
+ }
+ }
+ catch(std::string error){
+ std::cout << error;
+ exit(1);
+ }
+
+ return rgba;
+
+}
diff --git a/src/View/CMakeLists.txt b/src/View/CMakeLists.txt
index 9a13671..81d8b52 100644
--- a/src/View/CMakeLists.txt
+++ b/src/View/CMakeLists.txt
@@ -1,2 +1,4 @@
#Make Model lib
add_library(View ./MainWindow.cpp)
+
+target_link_libraries(View Skin)
diff --git a/src/View/MainWindow.cpp b/src/View/MainWindow.cpp
index ea4346d..3a61068 100644
--- a/src/View/MainWindow.cpp
+++ b/src/View/MainWindow.cpp
@@ -13,17 +13,14 @@ MainWindow::MainWindow(int width, int height, std::string title):
m_gridSize(0,0),
m_gridPosition(),
m_spaceBetweenCell(15),
+ m_skinName("original"),
m_font()
{
//Set windows size
m_windowSize=RenderWindow::getSize();
-
- m_gridPosition=sf::Vector2u(0,200);
-
-
- //Load font
- m_font.loadFromFile("./src/skin/original/Pragmatica-Medium.ttf");
+ //Set default grid position
+ m_gridPosition=sf::Vector2u(0,200);
//Define original skin:
m_skin.push_back(sf::Color(250,248,239)); //Background MainWindow
@@ -54,6 +51,9 @@ MainWindow::MainWindow(int width, int height, std::string title):
//Skin for other number
m_skin.push_back(sf::Color(60,58,50)); //More than 2048
+ //Load font
+ m_font.loadFromFile("./bin/fonts/Pragmatica-Medium.ttf");
+ m_skin=skin::loadSkin(m_skinName);
}
@@ -140,7 +140,7 @@ void MainWindow::drawCell(int x, int y, int value){
text.setFont(m_font);
text.setStyle(sf::Text::Bold);
text.setCharacterSize(fontSize);
- text.setString(valueString);
+ text.setString(valueString);
if(value==2 || value==4)
text.setColor(m_skin.at(3));
else
@@ -173,7 +173,7 @@ sf::Color MainWindow::getCellColor(int value){
}
void MainWindow::drawGameOver(int gridX, int gridY){
-
+
sf::RectangleShape gridShape(sf::Vector2f(m_gridSize.x,m_gridSize.y));
gridShape.setFillColor(m_skin.at(6));
gridShape.setPosition(gridX,gridY);
@@ -187,14 +187,14 @@ void MainWindow::drawATH(Stats stats){
int titleY=m_gridPosition.y-190;
- //==================== Draw title ====================
+ //==================== Draw title ====================
sf::Text text;
text.setFont(m_font);
text.setStyle(sf::Text::Bold);
text.setCharacterSize(80);
text.setColor(m_skin.at(3));
text.setPosition(titleX,titleY);
- text.setString("2048");
+ text.setString("2048");
RenderWindow::draw(text);
@@ -221,7 +221,7 @@ void MainWindow::drawATH(Stats stats){
RenderWindow::draw(text);
//==================== Draw score ====================
-
+
int scoreX=bestScoreX-bestScoreSizeX-5;
int scoreY=bestScoreY;
int scoreSizeX=bestScoreSizeX;
@@ -253,3 +253,4 @@ void MainWindow::drawGame(std::vector<std::vector<int> > grid, bool gameIsOver,
this->drawGrid(grid,gameIsOver);
this->drawATH(stats);
}
+
diff --git a/src/View/MainWindow.hpp b/src/View/MainWindow.hpp
index 072a7ec..3eacd54 100644
--- a/src/View/MainWindow.hpp
+++ b/src/View/MainWindow.hpp
@@ -6,10 +6,10 @@
#include <string>
#include <sstream>
#include "../Model/Stats.hpp"
+#include "../Helpers/Skin.hpp"
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
-
class MainWindow : public sf::RenderWindow{
private:
@@ -26,6 +26,8 @@ class MainWindow : public sf::RenderWindow{
sf::Vector2u m_gridPosition;
int m_spaceBetweenCell;
+ std::string m_skinName;
+
public:
MainWindow(int width, int height, std::string title);
~MainWindow();
@@ -39,5 +41,6 @@ class MainWindow : public sf::RenderWindow{
void drawATH(Stats stats);
+ void MoveCell();
void drawGame(std::vector<std::vector<int> > grid, bool gameIsOver, Stats stats);
};
diff --git a/src/main.cpp b/src/main.cpp
index 165c067..92cee05 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -13,7 +13,6 @@
-
//----- Start -----
int main()
{
@@ -21,10 +20,13 @@ int main()
srand(time(NULL));
//Init controller
+#if defined(CONSOLECONTROLLER)
+ ConsoleController controller;
+#elif defined(SFMLCONTROLLER)
SFMLController controller;
-
- //ConsoleController controller;
-
+#else
+#error Please defined which controller to use in CMakeList.txt
+#endif
//Run the game
controller.run();
diff --git a/src/skin/original/Pragmatica-Medium.ttf b/src/skin/original/Pragmatica-Medium.ttf
deleted file mode 100644
index f5fee8d..0000000
--- a/src/skin/original/Pragmatica-Medium.ttf
+++ /dev/null
Binary files differ