1
1
#include " AppState.h"
2
2
3
+ const std::string FILEPATH = " C:/Users/Steve/Documents/Visual Studio 2012/Projects/ChessPlusPlus/" ;
4
+
3
5
namespace chesspp
4
6
{
7
+ AppStateGame::AppStateGame ( Application* _app ) : app(_app)
8
+ {
9
+ backgroundPath = FILEPATH + " res/img/chessboard_640x640.png" ;
10
+ piecePath = FILEPATH + " res/img/chess_pieces_80x80_each.png" ;
11
+ validMovePath = FILEPATH + " res/img/valid_move.png" ;
12
+ std::string gamePath (FILEPATH);
13
+ try
14
+ {
15
+ background = sf::Sprite (TextureManager::getInstance ().Load (backgroundPath));
16
+ pieces = sf::Sprite (TextureManager::getInstance ().Load (piecePath));
17
+ validMove = sf::Sprite (TextureManager::getInstance ().Load (validMovePath));
18
+
19
+ board = new Board ();
20
+
21
+ gamePath += " res/games/new_game.txt" ;
22
+ board->newGame (gamePath);
23
+ }
24
+ catch (chesspp::exception e)
25
+ {
26
+ std::cout << " Files not loaded here are the paths:\n "
27
+ << backgroundPath << ' \n '
28
+ << piecePath << ' \n '
29
+ << validMovePath << ' \n '
30
+ << gamePath << ' \n '
31
+ << " \n Do Not Hit Enter. Quit the Game\n " ;
32
+ std::cin.get ();
33
+ }
34
+ }
35
+
5
36
int AppStateGame::id () { return 1 ; }
6
37
7
38
void AppStateGame::OnRender (sf::RenderWindow &display)
8
39
{
9
- display.draw (sf::Sprite (TextureManager::getInstance ().Load (" ../res/img/chessboard_640x640.png" )));
40
+ // We can't do anything with these sprites unless we declare them
41
+ // Might as well only call load once...
42
+
43
+ // Draw the background
44
+ display.draw (background);
45
+
46
+
47
+ static const int SIZE = 80 ; // To Be defined somwhere
48
+
49
+ // Valid moves are drawn for the piece being hovered over
50
+ // Or the piece that is currently selected
51
+
52
+ Piece* pCurrent = board->getCurrent ();
53
+ Piece* pSelect = board->getSelected ();
54
+ if (pSelect)
55
+ {
56
+ for (posList::const_iterator iter = pSelect->getTrajectory ().begin (); iter != pSelect->getTrajectory ().end (); iter++)
57
+ {
58
+ if (!iter->isValid ()) continue ;
59
+ validMove.setPosition (iter->getX () * SIZE, iter->getY () * SIZE);
60
+ display.draw (validMove);
61
+ }
62
+ }
63
+ else if (pCurrent)
64
+ {
65
+ for (posList::const_iterator iter = pCurrent->getTrajectory ().begin (); iter != pCurrent->getTrajectory ().end (); iter++)
66
+ {
67
+ if (!iter->isValid ()) continue ;
68
+ validMove.setPosition (iter->getX () * SIZE, iter->getY () * SIZE);
69
+ display.draw (validMove);
70
+ }
71
+ }
72
+
73
+ // Draw the non-selected pieces
74
+ for (auto iter = board->pieces .begin (); iter != board->pieces .end (); iter++)
75
+ {
76
+ if (!*iter || *iter == board->getSelected ()) continue ;
77
+
78
+ pieces.setTextureRect (sf::IntRect ((*iter)->getTexturePos ().getX (), (*iter)->getTexturePos ().getY (), SIZE, SIZE));
79
+ pieces.setPosition ((*iter)->getBoardPos ().getX () * SIZE, (*iter)->getBoardPos ().getY () * SIZE);
80
+ display.draw (pieces);
81
+ }
82
+
83
+ // Draw the selected piece
84
+ if (board->getSelected ())
85
+ {
86
+ pieces.setTextureRect (sf::IntRect (board->getSelected ()->getTexturePos ().getX (), board->getSelected ()->getTexturePos ().getY (), SIZE, SIZE));
87
+ sf::Vector2i mPos = sf::Mouse::getPosition (display);
88
+ pieces.setPosition (mPos .x - (SIZE / 2 ), mPos .y - (SIZE / 2 ));
89
+ display.draw (pieces);
90
+ }
10
91
}
11
92
12
93
void AppStateGame::OnLButtonPressed (int x, int y)
13
94
{
14
- #ifdef _DEBUG
15
- cout << " Left clicked at (" << x << " , " << y << " )\n " ;
16
- #endif // _DEBUG
95
+ board->setSelected (board->getCurrent ()); // No matter if NULL
96
+ }
97
+ void AppStateGame::OnMouseMoved (int x, int y)
98
+ {
99
+ board->setCurrent (x,y);
100
+ }
101
+ void AppStateGame::OnLButtonReleased (int x, int y)
102
+ {
103
+ // board knows what is selected, but I think this looks more clear
104
+ board->move (board->getSelected (), x, y);
105
+ board->setSelected (NULL );
17
106
}
18
107
}
0 commit comments