Skip to content

Commit 5a4f655

Browse files
author
Thumperrr
committed
Integrated Lowest0ne's progress into the updated framework
1 parent fa5d978 commit 5a4f655

26 files changed

+1313
-6
lines changed

res/games/new_game.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
rnbqkbnr
2+
pppppppp
3+
********
4+
********
5+
********
6+
********
7+
PPPPPPPP
8+
RNBQKBNR

res/img/chess_pieces.png

3.09 KB
Loading

res/img/valid_move.png

217 Bytes
Loading

src/AppStateGame.cpp

Lines changed: 93 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,107 @@
11
#include "AppState.h"
22

3+
const std::string FILEPATH = "C:/Users/Steve/Documents/Visual Studio 2012/Projects/ChessPlusPlus/";
4+
35
namespace chesspp
46
{
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+
<< "\nDo Not Hit Enter. Quit the Game\n";
32+
std::cin.get();
33+
}
34+
}
35+
536
int AppStateGame::id() { return 1; }
637

738
void AppStateGame::OnRender(sf::RenderWindow &display)
839
{
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+
}
1091
}
1192

1293
void AppStateGame::OnLButtonPressed(int x, int y)
1394
{
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);
17106
}
18107
}

src/AppStateGame.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define _APPSTATEGAME_H
33
#include "SFML.hpp"
44
#include "TextureManager.h"
5+
#include "board\Board.h"
56

67
#ifdef _DEBUG
78
#include <iostream>
@@ -14,15 +15,29 @@ namespace chesspp
1415
class AppStateGame : public AppState
1516
{
1617
Application* app;
18+
19+
// Adding some variables to avoid initializing for every render
20+
// I know that this doesn't go with the flow, but the only way I see it now.
21+
sf::Sprite background;
22+
sf::Sprite pieces;
23+
sf::Sprite validMove;
24+
25+
std::string backgroundPath;
26+
std::string piecePath;
27+
std::string validMovePath;
28+
29+
Board* board;
1730

1831
public:
19-
AppStateGame(Application* _app) : app(_app) {}
32+
AppStateGame(Application* _app);
2033
virtual ~AppStateGame() {}
2134

2235
virtual int id();
2336
virtual void OnRender(sf::RenderWindow &display);
2437

25-
virtual void OnLButtonPressed(int x, int y); //example implementation
38+
virtual void OnLButtonPressed(int x, int y);
39+
virtual void OnLButtonReleased(int x, int y);
40+
virtual void OnMouseMoved(int x, int y);
2641
};
2742
}
2843

src/board/Bishop.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include "Bishop.h"
2+
3+
namespace chesspp
4+
{
5+
6+
Bishop::Bishop(const Position& bPos, Color c)
7+
:Piece(bPos, Position(80 * 3,0), c)
8+
{
9+
10+
}
11+
12+
void Bishop::makeTrajectory(const Board* board)
13+
{
14+
15+
Log::Debug::write("BISHOP: ");
16+
Log::Debug::write(this->boardPos);
17+
Log::Debug::writeln("makeTrajectory");
18+
19+
this->trajectory.clear();
20+
21+
// Do the diagonals
22+
shootPath(board, NORTH_EAST);
23+
shootPath(board, SOUTH_EAST);
24+
shootPath(board, SOUTH_WEST);
25+
shootPath(board, NORTH_WEST);
26+
}
27+
}

src/board/Bishop.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef LOWBISHOP_H
2+
#define LOWBISHOP_H
3+
4+
// Warning:
5+
// Texture positions are hard coded into constructor
6+
// I think they should at least be brought to the Board
7+
// So they can be changed at once
8+
9+
#include "Piece.h"
10+
11+
namespace chesspp
12+
{
13+
14+
class Bishop : public Piece
15+
{
16+
private:
17+
public:
18+
Bishop(const Position& bPos, Color c);
19+
20+
virtual void makeTrajectory(const Board* board);
21+
};
22+
23+
}
24+
#endif

0 commit comments

Comments
 (0)