Skip to content

Commit 7060dac

Browse files
author
Thumperrr
committed
Modified Application and AppStateGame to fit with AppState having an sf::RenderWindow as a protected member.
Integrated GraphicsHandler into AppStateGame.
1 parent 6c8c530 commit 7060dac

File tree

4 files changed

+17
-97
lines changed

4 files changed

+17
-97
lines changed

src/AppStateGame.cpp

Lines changed: 6 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -4,90 +4,19 @@ const std::string FILEPATH = "C:/Users/Steve/Documents/Visual Studio 2012/Projec
44

55
namespace chesspp
66
{
7-
AppStateGame::AppStateGame( Application* _app ) : app(_app)
7+
AppStateGame::AppStateGame( Application* _app, sf::RenderWindow *_display ) : app(_app), AppState(_display), graphics(display)
88
{
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));
189

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-
}
10+
std::string gamePath(FILEPATH + "res/games/new_game.txt");
11+
board = new Board();
12+
board->newGame(gamePath);
3413
}
3514

3615
int AppStateGame::id() { return 1; }
3716

38-
void AppStateGame::OnRender(sf::RenderWindow &display)
17+
void AppStateGame::OnRender()
3918
{
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-
}
19+
graphics.drawBoard(board);
9120
}
9221

9322
void AppStateGame::OnLButtonPressed(int x, int y)

src/AppStateGame.h

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

78
#ifdef _DEBUG
89
#include <iostream>
@@ -14,26 +15,16 @@ namespace chesspp
1415
class Application;
1516
class AppStateGame : public AppState
1617
{
17-
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-
18+
Application* app;
2919
Board* board;
20+
graphics::GraphicsHandler graphics;
3021

3122
public:
32-
AppStateGame(Application* _app);
23+
AppStateGame(Application* _app, sf::RenderWindow *_display);
3324
virtual ~AppStateGame() {}
3425

3526
virtual int id();
36-
virtual void OnRender(sf::RenderWindow &display);
27+
virtual void OnRender();
3728

3829
virtual void OnLButtonPressed(int x, int y);
3930
virtual void OnLButtonReleased(int x, int y);

src/Application.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace chesspp
66
Application::Application()
77
: display(sf::VideoMode(640, 640), "ChessPlusPlus", sf::Style::Close),
88
running(true),
9-
state(new AppStateGame(this))
9+
state(new AppStateGame(this, &display))
1010
{
1111
display.setVerticalSyncEnabled(true);
1212
}
@@ -19,7 +19,7 @@ namespace chesspp
1919
while(display.pollEvent(Event))
2020
OnEvent(&Event);
2121

22-
state->OnRender(display);
22+
state->OnRender();
2323
display.display();
2424
}
2525

src/board/Board.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ namespace chesspp
6868
// Returns the Piece* at pos. NULL if pos is out of bounds
6969
Piece* at(const Position& pos) const;
7070

71-
// Given screen coordinates, set the currentPiece to the proper piece
72-
// Note that this uses magic number 80 to know how large the screen is
73-
void setCurrent(int screenX, int screenY);
71+
// Given screen coordinates, set the currentPiece to the proper piece
72+
// Note that this uses magic number 80 to know how large the screen is
73+
void setCurrent(int screenX, int screenY);
7474
Piece* getCurrent(void) const;
7575

7676
// Set the selected piece to the parameter

0 commit comments

Comments
 (0)