Compare commits
5 commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
1570bc6210 | ||
![]() |
8488827025 | ||
![]() |
9759972f35 | ||
![]() |
710cc4001f | ||
![]() |
fec126f0d2 |
30 changed files with 142 additions and 377 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1,4 +1,5 @@
|
||||||
CMakeFiles
|
CMakeFiles
|
||||||
|
*.sh
|
||||||
*.a
|
*.a
|
||||||
*.o
|
*.o
|
||||||
cmake_install.cmake
|
cmake_install.cmake
|
||||||
|
@ -6,6 +7,4 @@ Makefile
|
||||||
CMakeCache.txt
|
CMakeCache.txt
|
||||||
*.app
|
*.app
|
||||||
2P11
|
2P11
|
||||||
clear.sh
|
|
||||||
*.vim
|
*.vim
|
||||||
game_backup.xcf
|
|
||||||
|
|
|
@ -1,31 +1,14 @@
|
||||||
#==================== PREPROCESSOR DEFINITION ====================
|
|
||||||
|
|
||||||
#Define which controller use.
|
|
||||||
#Avalaible controllers are:
|
|
||||||
# - CONSOLECONTROLLER
|
|
||||||
# - SFMLCONTROLLER
|
|
||||||
add_definitions(-DSFMLCONTROLLER)
|
|
||||||
add_definitions(-DLIVESKINNING)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#==================== START CMAKE ====================
|
|
||||||
|
|
||||||
|
|
||||||
#Defined project name
|
#Defined project name
|
||||||
project(2P11)
|
project(2P11)
|
||||||
|
|
||||||
#Assign Modules path
|
#Assign Modules path
|
||||||
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
|
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FGS} -std=c++11 -lpthread")
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FGS} -std=c++11")
|
||||||
|
|
||||||
#Defined project VERSION
|
#Defined project VERSION
|
||||||
set(VERSION_MAJOR 0)
|
set(VERSION_MAJOR 0)
|
||||||
set(VERSION_MINOR 1)
|
set(VERSION_MINOR 1)
|
||||||
set(VERSION_REV 0)
|
set(VERSION_REV 0)
|
||||||
set(VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_REV}")
|
set(VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_REV}")
|
||||||
set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin )
|
|
||||||
|
|
||||||
#Minimum cmake VERSION
|
#Minimum cmake VERSION
|
||||||
cmake_minimum_required(VERSION 2.6)
|
cmake_minimum_required(VERSION 2.6)
|
||||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 35 KiB |
|
@ -1,31 +0,0 @@
|
||||||
|
|
||||||
050 050 050 #Background MainWindow
|
|
||||||
100 100 100 #Background cells
|
|
||||||
123 0 9 #Background grid color
|
|
||||||
19 110 101 #2 and 4 font color
|
|
||||||
143 122 102 #Button color
|
|
||||||
4 15 58 #other number font Color
|
|
||||||
238 228 218 186 #Game over color bg
|
|
||||||
|
|
||||||
#Skin 2 et le 4
|
|
||||||
238 228 218 #2
|
|
||||||
237 224 200 #4
|
|
||||||
|
|
||||||
#Skin 8 à 64
|
|
||||||
242 177 121 #8
|
|
||||||
245 149 99 #16
|
|
||||||
246 124 95 #32
|
|
||||||
246 94 59 #64
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#Skin 128 à 2048
|
|
||||||
237 207 114 #128
|
|
||||||
237 204 97 #256
|
|
||||||
237 200 80 #512
|
|
||||||
237 197 63 #1024
|
|
||||||
238 194 46 #2048
|
|
||||||
|
|
||||||
#Skin for other number
|
|
||||||
60 58 50 #More than 2048
|
|
||||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 36 KiB |
|
@ -1,31 +0,0 @@
|
||||||
|
|
||||||
250 248 239 #Background MainWindow
|
|
||||||
205 192 180 #Background cells
|
|
||||||
187 173 160 #Background grid color
|
|
||||||
119 110 101 #2 and 4 font color
|
|
||||||
143 122 102 #Button color
|
|
||||||
249 246 242 #other number font Color
|
|
||||||
238 228 218 186 #Game over color bg
|
|
||||||
|
|
||||||
#Skin 2 et le 4
|
|
||||||
238 228 218 #2
|
|
||||||
237 224 200 #4
|
|
||||||
|
|
||||||
#Skin 8 à 64
|
|
||||||
242 177 121 #8
|
|
||||||
245 149 99 #16
|
|
||||||
246 124 95 #32
|
|
||||||
246 94 59 #64
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#Skin 128 à 2048
|
|
||||||
237 207 114 #128
|
|
||||||
237 204 97 #256
|
|
||||||
237 200 80 #512
|
|
||||||
237 197 63 #1024
|
|
||||||
238 194 46 #2048
|
|
||||||
|
|
||||||
#Skin for other number
|
|
||||||
60 58 50 #More than 2048
|
|
||||||
|
|
Binary file not shown.
9
clear.sh
Executable file
9
clear.sh
Executable file
|
@ -0,0 +1,9 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
path="$(dirname "$(readlink -f $0)")"
|
||||||
|
|
||||||
|
find "$path" -name "CMakeFiles" -exec rm -rf {} \;
|
||||||
|
find "$path" -name "CMakeCache.txt" -delete
|
||||||
|
find "$path" -name "cmake_install.cmake" -delete
|
||||||
|
find "$path" -name "Makefile" -delete
|
||||||
|
|
BIN
src/2P11
Executable file
BIN
src/2P11
Executable file
Binary file not shown.
|
@ -17,5 +17,3 @@ target_link_libraries(2P11 ${SFML_LIBRARIES} Model ConsoleController View SFMLCo
|
||||||
add_subdirectory(./Model)
|
add_subdirectory(./Model)
|
||||||
add_subdirectory(./Controllers/)
|
add_subdirectory(./Controllers/)
|
||||||
add_subdirectory(./View/)
|
add_subdirectory(./View/)
|
||||||
add_subdirectory(./Helpers/)
|
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#include "./ConsoleController.hpp"
|
#include "./ConsoleController.hpp"
|
||||||
#include <SFML/Window/Keyboard.hpp>
|
#include <SFML/Window/Keyboard.hpp>
|
||||||
#include "../../Helpers/Keyboard.hpp"
|
#include "../../Helpers/Keyboard.hpp"
|
||||||
#include "../../Model/Stats.hpp"
|
|
||||||
|
|
||||||
//==================== Constructor and Destructor ====================
|
//==================== Constructor and Destructor ====================
|
||||||
|
|
||||||
|
@ -31,7 +31,7 @@ void ConsoleController::run()
|
||||||
m_game.popRandomNumber();
|
m_game.popRandomNumber();
|
||||||
|
|
||||||
//First cout stats
|
//First cout stats
|
||||||
this->coutStats(m_game.getStats());
|
this->coutStats();
|
||||||
|
|
||||||
//First cout grid
|
//First cout grid
|
||||||
m_game.coutGrid();
|
m_game.coutGrid();
|
||||||
|
@ -47,7 +47,7 @@ void ConsoleController::run()
|
||||||
bool moveDone=m_game.swipe(keyPress);
|
bool moveDone=m_game.swipe(keyPress);
|
||||||
|
|
||||||
//Cout stats
|
//Cout stats
|
||||||
this->coutStats(m_game.getStats());
|
this->coutStats();
|
||||||
|
|
||||||
//Cout grid
|
//Cout grid
|
||||||
m_game.coutGrid();
|
m_game.coutGrid();
|
||||||
|
@ -55,7 +55,7 @@ void ConsoleController::run()
|
||||||
}
|
}
|
||||||
|
|
||||||
//Last cout stats
|
//Last cout stats
|
||||||
this->coutStats(m_game.getStats());
|
this->coutStats();
|
||||||
|
|
||||||
//Last cout grid
|
//Last cout grid
|
||||||
m_game.coutGrid();
|
m_game.coutGrid();
|
||||||
|
@ -122,8 +122,8 @@ kbdh::Direction ConsoleController::waitArrowKeyPress()
|
||||||
|
|
||||||
|
|
||||||
//Cout the stats of the game
|
//Cout the stats of the game
|
||||||
void ConsoleController::coutStats(Stats stats){
|
void ConsoleController::coutStats(){
|
||||||
|
|
||||||
std::cout << std::endl << "Score : " << stats.getScore() << std::endl;
|
std::cout << std::endl << "Score : " << m_game.getScore() << std::endl;
|
||||||
std::cout << "Nombre de coups : " << stats.getNbMove() << std::endl;
|
std::cout << "Nombre de coups : " << m_game.getNbMove() << std::endl;
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@ class ConsoleController
|
||||||
|
|
||||||
//Helpers
|
//Helpers
|
||||||
void run();
|
void run();
|
||||||
void coutStats(Stats stats);
|
void coutStats();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
BIN
src/Controllers/ConsoleController/libConsoleController.a
Normal file
BIN
src/Controllers/ConsoleController/libConsoleController.a
Normal file
Binary file not shown.
|
@ -3,31 +3,27 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//==================== Constructor and Destructor ====================
|
|
||||||
|
|
||||||
//Constructor
|
|
||||||
SFMLController::SFMLController() : m_game(), m_MainWindow(600,800, "2P11"){
|
SFMLController::SFMLController() : m_game(), m_MainWindow(600,800, "2P11"){
|
||||||
}
|
}
|
||||||
|
|
||||||
//Destructor
|
|
||||||
SFMLController::~SFMLController(){
|
SFMLController::~SFMLController(){
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//Run controller
|
|
||||||
|
|
||||||
void SFMLController::run(){
|
void SFMLController::run(){
|
||||||
|
|
||||||
//Init keypress
|
|
||||||
kbdh::Direction keyPress;
|
kbdh::Direction keyPress;
|
||||||
|
|
||||||
//First pop
|
|
||||||
m_game.popRandomNumber();
|
m_game.popRandomNumber();
|
||||||
|
|
||||||
//Start game
|
|
||||||
while(m_MainWindow.isOpen()){
|
while(m_MainWindow.isOpen()){
|
||||||
|
|
||||||
//Check event
|
|
||||||
sf::Event event;
|
sf::Event event;
|
||||||
while (m_MainWindow.pollEvent(event))
|
while (m_MainWindow.pollEvent(event))
|
||||||
{
|
{
|
||||||
|
@ -58,16 +54,71 @@ void SFMLController::run(){
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//Clear window
|
|
||||||
m_MainWindow.clearBG();
|
m_MainWindow.clearBG();
|
||||||
|
//m_game.swipe(kbdh::Direction::Left);
|
||||||
//Draw the game
|
std::vector<std::vector<int> > aaa=m_game.getGrid();
|
||||||
m_MainWindow.drawGame(m_game.getGrid(),m_game.isOver(), m_game.getStats());
|
m_MainWindow.drawGame(aaa,m_game.isOver(), m_game.getStats());
|
||||||
|
|
||||||
//Display the changements
|
|
||||||
m_MainWindow.display();
|
m_MainWindow.display();
|
||||||
|
|
||||||
|
//keyPress=this->waitArrowKeyPress();
|
||||||
|
m_game.swipe(keyPress);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Wait for keypress and return the keyPress.
|
||||||
|
kbdh::Direction SFMLController::waitArrowKeyPress()
|
||||||
|
{
|
||||||
|
//Initialise keyPress
|
||||||
|
kbdh::Direction keyPress;
|
||||||
|
|
||||||
|
//White space to remove arrows print by the terminal
|
||||||
|
std::string spaces=" ";
|
||||||
|
|
||||||
|
//Wait for keypress
|
||||||
|
while(1){
|
||||||
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
|
||||||
|
{
|
||||||
|
keyPress=kbdh::Left;
|
||||||
|
while(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
|
||||||
|
{
|
||||||
|
//Wait for release and try to remove arrow printed characters
|
||||||
|
std::cout << "\r" << spaces;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
|
||||||
|
{
|
||||||
|
keyPress=kbdh::Right;
|
||||||
|
while(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
|
||||||
|
{
|
||||||
|
//Wait for release and try to remove arrow printed characters
|
||||||
|
std::cout << "\r" << spaces;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
|
||||||
|
{
|
||||||
|
keyPress=kbdh::Up;
|
||||||
|
while(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
|
||||||
|
{
|
||||||
|
//Wait for release and try to remove arrow printed characters
|
||||||
|
std::cout << "\r" << spaces;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
|
||||||
|
{
|
||||||
|
keyPress=kbdh::Down;
|
||||||
|
while(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
|
||||||
|
{
|
||||||
|
//Wait for release and try to remove arrow printed characters
|
||||||
|
std::cout << "\r" << spaces;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return keyPress;
|
||||||
|
}
|
||||||
|
|
|
@ -1,31 +1,26 @@
|
||||||
#ifndef __SFMLCONTROLLER__
|
|
||||||
#define __SFMLCONTROLLER__
|
|
||||||
|
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include <SFML/Window.hpp>
|
#include <SFML/Window.hpp>
|
||||||
#include <SFML/Window/Keyboard.hpp>
|
|
||||||
|
|
||||||
#include "../../View/MainWindow.hpp"
|
#include "../../View/MainWindow.hpp"
|
||||||
#include "../../Model/Game.hpp"
|
#include "../../Model/Game.hpp"
|
||||||
#include "../../Helpers/Keyboard.hpp"
|
#include "../../Helpers/Keyboard.hpp"
|
||||||
#include "../../Helpers/Skin.hpp"
|
#include <SFML/Window/Keyboard.hpp>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class SFMLController{
|
class SFMLController{
|
||||||
|
|
||||||
private:
|
private:
|
||||||
MainWindow m_MainWindow;
|
MainWindow m_MainWindow;
|
||||||
Game m_game;
|
Game m_game;
|
||||||
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
SFMLController();
|
SFMLController();
|
||||||
~SFMLController();
|
~SFMLController();
|
||||||
|
|
||||||
|
kbdh::Direction waitArrowKeyPress();
|
||||||
void run();
|
void run();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
|
@ -1,3 +0,0 @@
|
||||||
#Make Model lib
|
|
||||||
|
|
||||||
add_subdirectory(./Skin/)
|
|
|
@ -1,19 +0,0 @@
|
||||||
#include <SFML/Graphics/Color.hpp>
|
|
||||||
#include <SFML/Graphics/Image.hpp>
|
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
|
||||||
#include <fstream>
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
|
|
||||||
namespace skin{
|
|
||||||
|
|
||||||
std::vector<sf::Color> loadSkin(std::string skinName);
|
|
||||||
std::vector<sf::Color> loadShader(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);
|
|
||||||
}
|
|
|
@ -1 +0,0 @@
|
||||||
add_library(Skin Skin.cpp)
|
|
|
@ -1,167 +0,0 @@
|
||||||
#include "../Skin.hpp"
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
std::vector<sf::Color> skin::loadShader(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;
|
|
||||||
|
|
||||||
}
|
|
||||||
std::vector<sf::Color> skin::loadSkin(std::string skinName){
|
|
||||||
sf::Image game;
|
|
||||||
game.loadFromFile("bin/skin/"+skinName+"/game.png");
|
|
||||||
|
|
||||||
std::vector<sf::Color> skin;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//Background
|
|
||||||
skin.push_back(game.getPixel(0,0)); //Background MainWindow
|
|
||||||
skin.push_back(game.getPixel(76,243)); //Background cells
|
|
||||||
skin.push_back(game.getPixel(61,227)); //Background grid color
|
|
||||||
skin.push_back(game.getPixel(326,58)); //Score bg
|
|
||||||
skin.push_back(game.getPixel(441,58)); //Best score bg
|
|
||||||
skin.push_back(sf::Color(143,122,102)); //Button bg
|
|
||||||
skin.push_back(sf::Color(238,228,218,186)); //Game over color bg
|
|
||||||
|
|
||||||
//Font
|
|
||||||
skin.push_back(game.getPixel(65,105)); //Title font color
|
|
||||||
skin.push_back(game.getPixel(348,78)); //Score title fontcolor
|
|
||||||
skin.push_back(game.getPixel(468,77)); //Best score title font color
|
|
||||||
skin.push_back(game.getPixel(400,96)); //Score font color
|
|
||||||
skin.push_back(game.getPixel(484,97)); //Best score font color
|
|
||||||
skin.push_back(game.getPixel(128,660)); //2 and 4 font color
|
|
||||||
skin.push_back(game.getPixel(359,661)); //other number font Color
|
|
||||||
skin.push_back(sf::Color(143,122,102)); //game over font
|
|
||||||
|
|
||||||
//Skin 2 et le 4
|
|
||||||
skin.push_back(game.getPixel(70,597)); //2
|
|
||||||
skin.push_back(game.getPixel(190,597)); //4
|
|
||||||
|
|
||||||
//Skin 8 à 64
|
|
||||||
skin.push_back(game.getPixel(311,597)); //8
|
|
||||||
skin.push_back(game.getPixel(431,597)); //16
|
|
||||||
skin.push_back(game.getPixel(71,477)); //32
|
|
||||||
skin.push_back(game.getPixel(191,477)); //64
|
|
||||||
|
|
||||||
//Skin 128 à 2048
|
|
||||||
skin.push_back(game.getPixel(312,477)); //128
|
|
||||||
skin.push_back(game.getPixel(431,477)); //256
|
|
||||||
skin.push_back(game.getPixel(71,358)); //512
|
|
||||||
skin.push_back(game.getPixel(191,358)); //1024
|
|
||||||
skin.push_back(game.getPixel(311,358)); //2048
|
|
||||||
|
|
||||||
//Skin for other number
|
|
||||||
skin.push_back(game.getPixel(432,358)); //More than 2048
|
|
||||||
|
|
||||||
return skin;
|
|
||||||
}
|
|
|
@ -49,6 +49,11 @@ void Game::coutGrid(){
|
||||||
std::cout << this->description();
|
std::cout << this->description();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Return true if the game is lost. False else.
|
||||||
|
//bool Game::isOver(){
|
||||||
|
//return m_grid.isOver();
|
||||||
|
//}
|
||||||
|
|
||||||
//Pop a random number on the grid
|
//Pop a random number on the grid
|
||||||
void Game::popRandomNumber(){
|
void Game::popRandomNumber(){
|
||||||
std::tuple<int, int> coord(Grid::getRandomEmptyCellCoord());
|
std::tuple<int, int> coord(Grid::getRandomEmptyCellCoord());
|
||||||
|
@ -64,13 +69,22 @@ void Game::popRandomNumber(){
|
||||||
number=2;
|
number=2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Grid::setCell(coord, number);
|
Grid::setCell(coord, number);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//==================== Getters and Setter ====================
|
//==================== Getters and Setter ====================
|
||||||
|
|
||||||
//Retrieve the Score
|
//Retrieve the Score
|
||||||
Stats Game::getStats(){
|
Stats Game::getStats(){
|
||||||
return m_stats;
|
return m_stats;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//std::vector<std::vector<int> > Game::getGrid(){
|
||||||
|
//return m_grid.getGrid();
|
||||||
|
//}
|
||||||
|
|
||||||
|
|
||||||
|
//int Game::maxStrLenInGrid(){
|
||||||
|
//return m_grid.maxStrLenInGrid();
|
||||||
|
//}
|
||||||
|
|
|
@ -9,16 +9,16 @@
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <tuple>
|
|
||||||
|
|
||||||
#include "../Helpers/Keyboard.hpp"
|
#include "../Helpers/Keyboard.hpp"
|
||||||
#include "Grid.hpp"
|
#include "Grid.hpp"
|
||||||
#include "Stats.hpp"
|
#include "Stats.hpp"
|
||||||
|
#include <tuple>
|
||||||
|
|
||||||
class Game : public Grid
|
class Game : public Grid
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
//Members
|
//Members
|
||||||
|
//Grid m_grid;
|
||||||
Stats m_stats;
|
Stats m_stats;
|
||||||
public:
|
public:
|
||||||
//Constructor and Destructor
|
//Constructor and Destructor
|
||||||
|
@ -29,9 +29,12 @@ class Game : public Grid
|
||||||
bool swipe(kbdh::Direction direction);
|
bool swipe(kbdh::Direction direction);
|
||||||
void coutGrid();
|
void coutGrid();
|
||||||
void popRandomNumber();
|
void popRandomNumber();
|
||||||
|
//bool isOver();
|
||||||
|
|
||||||
//Getters and Setters
|
//Getters and Setters
|
||||||
Stats getStats();
|
Stats getStats();
|
||||||
|
//int maxStrLenInGrid();
|
||||||
|
//std::vector<std::vector<int> > getGrid();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -12,22 +12,6 @@ Grid::Grid(): m_size(4), m_grid(4){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//m_grid.at(3).at(0)=2048;
|
//m_grid.at(3).at(0)=2048;
|
||||||
|
|
||||||
//m_grid.at(3).at(0)=2;
|
|
||||||
//m_grid.at(3).at(1)=4;
|
|
||||||
//m_grid.at(3).at(2)=8;
|
|
||||||
//m_grid.at(3).at(3)=16;
|
|
||||||
//m_grid.at(2).at(0)=32;
|
|
||||||
//m_grid.at(2).at(1)=64;
|
|
||||||
//m_grid.at(2).at(2)=128;
|
|
||||||
//m_grid.at(2).at(3)=256;
|
|
||||||
//m_grid.at(1).at(0)=512;
|
|
||||||
//m_grid.at(1).at(1)=1024;
|
|
||||||
//m_grid.at(1).at(2)=2048;
|
|
||||||
//m_grid.at(1).at(3)=4096;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Destructor
|
//Destructor
|
||||||
|
@ -363,7 +347,7 @@ std::vector<int> Grid::getCol(int col){
|
||||||
return colVect;
|
return colVect;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Retrieve the grid for the view
|
|
||||||
std::vector<std::vector<int> > Grid::getGrid(){
|
std::vector<std::vector<int> > Grid::getGrid(){
|
||||||
return m_grid;
|
return m_grid;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,6 @@
|
||||||
#include "Stats.hpp"
|
#include "Stats.hpp"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//==================== Constructor and destructor ====================
|
|
||||||
|
|
||||||
//Constructor
|
|
||||||
Stats::Stats() :
|
Stats::Stats() :
|
||||||
m_score(0),
|
m_score(0),
|
||||||
m_nbMove(0)
|
m_nbMove(0)
|
||||||
|
@ -12,12 +8,11 @@ Stats::Stats() :
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Destructor
|
|
||||||
Stats::~Stats(){
|
Stats::~Stats(){
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//==================== Helpers ====================
|
|
||||||
void Stats::incScore(int value){
|
void Stats::incScore(int value){
|
||||||
m_score+=value;
|
m_score+=value;
|
||||||
}
|
}
|
||||||
|
@ -25,7 +20,7 @@ void Stats::incnbMove(){
|
||||||
m_nbMove++;
|
m_nbMove++;
|
||||||
}
|
}
|
||||||
|
|
||||||
//==================== Getters ====================
|
|
||||||
int Stats::getScore(){
|
int Stats::getScore(){
|
||||||
return m_score;
|
return m_score;
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,21 +4,22 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Stats{
|
class Stats{
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int m_score;
|
int m_score;
|
||||||
int m_nbMove;
|
int m_nbMove;
|
||||||
public:
|
public:
|
||||||
//Constructor and destructor
|
|
||||||
Stats();
|
Stats();
|
||||||
~Stats();
|
~Stats();
|
||||||
|
|
||||||
//Helpers
|
|
||||||
void incScore(int value);
|
void incScore(int value);
|
||||||
void incnbMove();
|
void incnbMove();
|
||||||
|
|
||||||
//Getters
|
|
||||||
int getScore();
|
int getScore();
|
||||||
int getNbMove();
|
int getNbMove();
|
||||||
|
|
||||||
|
|
BIN
src/Model/libModel.a
Normal file
BIN
src/Model/libModel.a
Normal file
Binary file not shown.
|
@ -1,4 +1,2 @@
|
||||||
#Make Model lib
|
#Make Model lib
|
||||||
add_library(View ./MainWindow.cpp)
|
add_library(View ./MainWindow.cpp)
|
||||||
|
|
||||||
target_link_libraries(View Skin)
|
|
||||||
|
|
|
@ -13,15 +13,18 @@ MainWindow::MainWindow(int width, int height, std::string title):
|
||||||
m_gridSize(0,0),
|
m_gridSize(0,0),
|
||||||
m_gridPosition(),
|
m_gridPosition(),
|
||||||
m_spaceBetweenCell(15),
|
m_spaceBetweenCell(15),
|
||||||
m_skinName("original"),
|
|
||||||
m_font()
|
m_font()
|
||||||
{
|
{
|
||||||
//Set windows size
|
//Set windows size
|
||||||
m_windowSize=RenderWindow::getSize();
|
m_windowSize=RenderWindow::getSize();
|
||||||
|
|
||||||
//Set default grid position
|
|
||||||
m_gridPosition=sf::Vector2u(0,200);
|
m_gridPosition=sf::Vector2u(0,200);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//Load font
|
||||||
|
m_font.loadFromFile("./src/skin/original/Pragmatica-Medium.ttf");
|
||||||
|
|
||||||
//Define original skin:
|
//Define original skin:
|
||||||
m_skin.push_back(sf::Color(250,248,239)); //Background MainWindow
|
m_skin.push_back(sf::Color(250,248,239)); //Background MainWindow
|
||||||
m_skin.push_back(sf::Color(205,192,180)); //Background cells
|
m_skin.push_back(sf::Color(205,192,180)); //Background cells
|
||||||
|
@ -51,9 +54,6 @@ MainWindow::MainWindow(int width, int height, std::string title):
|
||||||
//Skin for other number
|
//Skin for other number
|
||||||
m_skin.push_back(sf::Color(60,58,50)); //More than 2048
|
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,22 +140,21 @@ void MainWindow::drawCell(int x, int y, int value){
|
||||||
text.setFont(m_font);
|
text.setFont(m_font);
|
||||||
text.setStyle(sf::Text::Bold);
|
text.setStyle(sf::Text::Bold);
|
||||||
text.setCharacterSize(fontSize);
|
text.setCharacterSize(fontSize);
|
||||||
text.setString(valueString);
|
text.setString(valueString);
|
||||||
if(value==2 || value==4)
|
if(value==2 || value==4)
|
||||||
text.setColor(m_skin.at(12));
|
text.setColor(m_skin.at(3));
|
||||||
else
|
else
|
||||||
text.setColor(m_skin.at(13));
|
text.setColor(m_skin.at(4));
|
||||||
text.setPosition(fontX,fontY);
|
text.setPosition(fontX,fontY);
|
||||||
|
|
||||||
if(value != 0)
|
if(value != 0)
|
||||||
RenderWindow::draw(text);
|
RenderWindow::draw(text);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sf::Color MainWindow::getCellColor(int value){
|
sf::Color MainWindow::getCellColor(int value){
|
||||||
|
|
||||||
//Id of the first cell color skin
|
//Id of the first cell color skin
|
||||||
int idStart=15;
|
int idStart=7;
|
||||||
|
|
||||||
if(value==0){
|
if(value==0){
|
||||||
return m_skin.at(1);
|
return m_skin.at(1);
|
||||||
|
@ -174,7 +173,7 @@ sf::Color MainWindow::getCellColor(int value){
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::drawGameOver(int gridX, int gridY){
|
void MainWindow::drawGameOver(int gridX, int gridY){
|
||||||
|
|
||||||
sf::RectangleShape gridShape(sf::Vector2f(m_gridSize.x,m_gridSize.y));
|
sf::RectangleShape gridShape(sf::Vector2f(m_gridSize.x,m_gridSize.y));
|
||||||
gridShape.setFillColor(m_skin.at(6));
|
gridShape.setFillColor(m_skin.at(6));
|
||||||
gridShape.setPosition(gridX,gridY);
|
gridShape.setPosition(gridX,gridY);
|
||||||
|
@ -188,14 +187,14 @@ void MainWindow::drawATH(Stats stats){
|
||||||
int titleY=m_gridPosition.y-190;
|
int titleY=m_gridPosition.y-190;
|
||||||
|
|
||||||
|
|
||||||
//==================== Draw title ====================
|
//==================== Draw title ====================
|
||||||
sf::Text text;
|
sf::Text text;
|
||||||
text.setFont(m_font);
|
text.setFont(m_font);
|
||||||
text.setStyle(sf::Text::Bold);
|
text.setStyle(sf::Text::Bold);
|
||||||
text.setCharacterSize(80);
|
text.setCharacterSize(80);
|
||||||
text.setColor(m_skin.at(7));
|
text.setColor(m_skin.at(3));
|
||||||
text.setPosition(titleX,titleY);
|
text.setPosition(titleX,titleY);
|
||||||
text.setString("2048");
|
text.setString("2048");
|
||||||
|
|
||||||
RenderWindow::draw(text);
|
RenderWindow::draw(text);
|
||||||
|
|
||||||
|
@ -210,19 +209,19 @@ void MainWindow::drawATH(Stats stats){
|
||||||
int bestScoreY=titleY+25;
|
int bestScoreY=titleY+25;
|
||||||
|
|
||||||
sf::RectangleShape bestScoreShape(sf::Vector2f(bestScoreSizeX,bestScoreSizeY));
|
sf::RectangleShape bestScoreShape(sf::Vector2f(bestScoreSizeX,bestScoreSizeY));
|
||||||
bestScoreShape.setFillColor(m_skin.at(4));
|
bestScoreShape.setFillColor(m_skin.at(2));
|
||||||
bestScoreShape.setPosition(bestScoreX,bestScoreY);
|
bestScoreShape.setPosition(bestScoreX,bestScoreY);
|
||||||
RenderWindow::draw(bestScoreShape);
|
RenderWindow::draw(bestScoreShape);
|
||||||
|
|
||||||
text.setString("BEST");
|
text.setString("BEST");
|
||||||
text.setPosition(bestScoreX+bestScoreSizeY-scoreAndBestScoreFontSize-5,bestScoreY+12);
|
text.setPosition(bestScoreX+bestScoreSizeY-scoreAndBestScoreFontSize-5,bestScoreY+12);
|
||||||
text.setCharacterSize(scoreAndBestScoreFontSize-5);
|
text.setCharacterSize(scoreAndBestScoreFontSize-5);
|
||||||
text.setColor(m_skin.at(9));
|
text.setColor(m_skin.at(7));
|
||||||
|
|
||||||
RenderWindow::draw(text);
|
RenderWindow::draw(text);
|
||||||
|
|
||||||
//==================== Draw score ====================
|
//==================== Draw score ====================
|
||||||
|
|
||||||
int scoreX=bestScoreX-bestScoreSizeX-5;
|
int scoreX=bestScoreX-bestScoreSizeX-5;
|
||||||
int scoreY=bestScoreY;
|
int scoreY=bestScoreY;
|
||||||
int scoreSizeX=bestScoreSizeX;
|
int scoreSizeX=bestScoreSizeX;
|
||||||
|
@ -230,7 +229,7 @@ void MainWindow::drawATH(Stats stats){
|
||||||
int scoreLength=std::to_string(stats.getScore()).size();
|
int scoreLength=std::to_string(stats.getScore()).size();
|
||||||
|
|
||||||
sf::RectangleShape scoreShape(sf::Vector2f(scoreSizeX,scoreSizeY));
|
sf::RectangleShape scoreShape(sf::Vector2f(scoreSizeX,scoreSizeY));
|
||||||
scoreShape.setFillColor(m_skin.at(3));
|
scoreShape.setFillColor(m_skin.at(2));
|
||||||
scoreShape.setPosition(scoreX,scoreY);
|
scoreShape.setPosition(scoreX,scoreY);
|
||||||
RenderWindow::draw(scoreShape);
|
RenderWindow::draw(scoreShape);
|
||||||
|
|
||||||
|
@ -238,23 +237,19 @@ void MainWindow::drawATH(Stats stats){
|
||||||
text.setString("SCORE");
|
text.setString("SCORE");
|
||||||
text.setPosition(scoreX+scoreSizeY-scoreAndBestScoreFontSize-10,scoreY+12);
|
text.setPosition(scoreX+scoreSizeY-scoreAndBestScoreFontSize-10,scoreY+12);
|
||||||
text.setCharacterSize(scoreAndBestScoreFontSize-5);
|
text.setCharacterSize(scoreAndBestScoreFontSize-5);
|
||||||
text.setColor(m_skin.at(8));
|
text.setColor(m_skin.at(7));
|
||||||
|
|
||||||
RenderWindow::draw(text);
|
RenderWindow::draw(text);
|
||||||
|
|
||||||
text.setString(std::to_string(stats.getScore()));
|
text.setString(std::to_string(stats.getScore()));
|
||||||
text.setPosition(scoreX+scoreSizeY-((scoreLength+20)/2)-scoreAndBestScoreFontSize+10,scoreY+scoreSizeY - scoreAndBestScoreFontSize-10);
|
text.setPosition(scoreX+scoreSizeY-((scoreLength+20)/2)-scoreAndBestScoreFontSize+10,scoreY+scoreSizeY - scoreAndBestScoreFontSize-10);
|
||||||
text.setCharacterSize(scoreAndBestScoreFontSize);
|
text.setCharacterSize(scoreAndBestScoreFontSize);
|
||||||
text.setColor(m_skin.at(10));
|
text.setColor(sf::Color::White);
|
||||||
|
|
||||||
RenderWindow::draw(text);
|
RenderWindow::draw(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::drawGame(std::vector<std::vector<int> > grid, bool gameIsOver, Stats stats){
|
void MainWindow::drawGame(std::vector<std::vector<int> > grid, bool gameIsOver, Stats stats){
|
||||||
#ifdef LIVESKINNING
|
|
||||||
m_skin=skin::loadSkin(m_skinName);
|
|
||||||
#endif
|
|
||||||
this->drawGrid(grid,gameIsOver);
|
this->drawGrid(grid,gameIsOver);
|
||||||
this->drawATH(stats);
|
this->drawATH(stats);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,10 +6,10 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include "../Model/Stats.hpp"
|
#include "../Model/Stats.hpp"
|
||||||
#include "../Helpers/Skin.hpp"
|
|
||||||
#include <SFML/Window.hpp>
|
#include <SFML/Window.hpp>
|
||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
|
|
||||||
|
|
||||||
class MainWindow : public sf::RenderWindow{
|
class MainWindow : public sf::RenderWindow{
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -26,8 +26,6 @@ class MainWindow : public sf::RenderWindow{
|
||||||
sf::Vector2u m_gridPosition;
|
sf::Vector2u m_gridPosition;
|
||||||
int m_spaceBetweenCell;
|
int m_spaceBetweenCell;
|
||||||
|
|
||||||
std::string m_skinName;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
MainWindow(int width, int height, std::string title);
|
MainWindow(int width, int height, std::string title);
|
||||||
~MainWindow();
|
~MainWindow();
|
||||||
|
@ -41,6 +39,5 @@ class MainWindow : public sf::RenderWindow{
|
||||||
|
|
||||||
void drawATH(Stats stats);
|
void drawATH(Stats stats);
|
||||||
|
|
||||||
void MoveCell();
|
|
||||||
void drawGame(std::vector<std::vector<int> > grid, bool gameIsOver, Stats stats);
|
void drawGame(std::vector<std::vector<int> > grid, bool gameIsOver, Stats stats);
|
||||||
};
|
};
|
||||||
|
|
|
@ -7,12 +7,12 @@
|
||||||
|
|
||||||
//----- Personnal include -----
|
//----- Personnal include -----
|
||||||
#include "./Controllers/SFMLController/SFMLController.hpp"
|
#include "./Controllers/SFMLController/SFMLController.hpp"
|
||||||
#include "./Controllers/ConsoleController/ConsoleController.hpp"
|
|
||||||
//-----------------------------
|
//-----------------------------
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//----- Start -----
|
//----- Start -----
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
@ -20,13 +20,8 @@ int main()
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
|
|
||||||
//Init controller
|
//Init controller
|
||||||
#if defined(CONSOLECONTROLLER)
|
|
||||||
ConsoleController controller;
|
|
||||||
#elif defined(SFMLCONTROLLER)
|
|
||||||
SFMLController controller;
|
SFMLController controller;
|
||||||
#else
|
|
||||||
#error Please defined which controller to use in CMakeList.txt
|
|
||||||
#endif
|
|
||||||
//Run the game
|
//Run the game
|
||||||
controller.run();
|
controller.run();
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue