Skip to content

Commit 6560f39

Browse files
author
Thumperrr
committed
Merge pull request #14 from Thumperrr/master
Large update
2 parents 2c19792 + 32a89db commit 6560f39

40 files changed

+1705
-24
lines changed

.gitignore

+9-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,16 @@
66
*.vcxproj
77
*.filters
88
*.opensdf
9+
*.exe
10+
*.pdb
11+
*.ilk
12+
*.DS_Store
13+
14+
chesspp
15+
debug.txt
916

1017
ChessPlusPlus/
1118
Debug/
1219
Release/
13-
libs/SFML-4fbefe7/build_vs11/
20+
libs/SFML-4fbefe7/build_vs11/
21+
bin/

MacOS/bundle.sh

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/sh
2+
3+
# bundle.sh
4+
#
5+
#
6+
# Created by Steve Hall on 3/8/13.
7+
#
8+
9+
# Remove old .app if exists
10+
if [ -d bin/ChessPlusPlus.app ]; then
11+
echo "Removing old .app"
12+
rm -rf bin/ChessPlusPlus.app
13+
fi
14+
15+
# Create bundle structure in bin/
16+
echo "Creating bundle structure"
17+
mkdir -p bin bin/ChessPlusPlus.app bin/ChessPlusPlus.app/Contents bin/ChessPlusPlus.app/Contents/MacOS bin/ChessPlusPlus.app/Contents/Resources/res bin/ChessPlusPlus.app/Contents/Frameworks
18+
19+
# Copy resources
20+
echo "Copying resources to bundle"
21+
cp -pR res bin/ChessPlusPlus.app/Contents/Resources/
22+
cp -pR MacOS/info.plist bin/ChessPlusPlus.app/Contents
23+
cp -pR ./config.xml bin/ChessPlusPlus.app/Contents/Resources
24+
25+
# Copy SFML frameworks
26+
echo "Copying SFML Frameworks to bundle"
27+
cp -Rn /Library/Frameworks/SFML.framework bin/ChessPlusPlus.app/Contents/Frameworks
28+
cp -Rn /Library/Frameworks/sfml-system.framework bin/ChessPlusPlus.app/Contents/Frameworks
29+
cp -Rn /Library/Frameworks/sfml-window.framework bin/ChessPlusPlus.app/Contents/Frameworks
30+
cp -Rn /Library/Frameworks/sfml-graphics.framework bin/ChessPlusPlus.app/Contents/Frameworks
31+
32+
# Move executable
33+
echo "Moving executable to bundle"
34+
mv -f ./chesspp bin/ChessPlusPlus.app/Contents/MacOS

MacOS/info.plist

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleShortVersionString</key>
6+
<string>0.0.1</string>
7+
<key>CFBundleIdentifier</key>
8+
<string>com.cplusplus.chesspp</string>
9+
<key>CFBundleName</key>
10+
<string>ChessPlusPlus</string>
11+
<key>CFBundleSignature</key>
12+
<string>com.cplusplus.chesspp</string>
13+
<key>CFBundleInfoDictionaryVersion</key>
14+
<string>6.0</string>
15+
<key>CFBundleGetInfoString</key>
16+
<string>ChessPlusPlus</string>
17+
<key>CFBundleExecutable</key>
18+
<string>chesspp</string>
19+
<key>CFBundlePackageType</key>
20+
<string>APPL</string>
21+
<key>CFBundleVersion</key>
22+
<string>0.0.0.1</string>
23+
<key>NSAppleScriptEnabled</key>
24+
<true/>
25+
</dict>
26+
</plist>

Makefile

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
UNAME = $(shell uname -s)
2+
3+
COMPILER_Linux = g++
4+
CXX_FLAGS_Linux = -Wall -std=c++11
5+
DEBUG_LIBS_Linux = -lsfml-system-d -lsfml-window-d -lsfml-graphics-d
6+
RELEASE_LIBS_Linux = -lsfml-system -lsfml-window -lsfml-graphics
7+
8+
COMPILER_Darwin = clang++
9+
CXX_FLAGS_Darwin = -Wall -std=c++11 -stdlib=libc++
10+
CXX_INCLUDE_Darwin = -I/opt/local/include
11+
DEBUG_LIBS_Darwin = -framework SFML -framework sfml-system -framework sfml-window -framework sfml-graphics
12+
RELEASE_LIBS_Darwin = $(DEBUG_LIBS)
13+
14+
COMPILER = $(COMPILER_$(UNAME))
15+
CXX_FLAGS = $(CXX_FLAGS_$(UNAME))
16+
CXX_INCLUDE = $(CXX_INCLUDE_$(UNAME))
17+
DEBUG_LIBS = $(DEBUG_LIBS_$(UNAME))
18+
RELEASE_LIBS = $(RELEASE_LIBS_$(UNAME))
19+
20+
DEBUG_PREPROCESSOR = -D_DEBUG
21+
RELEASE_PREPROCESSOR = -D_RELEASE
22+
23+
EXECUTABLE = chesspp
24+
TARGET = ../$(EXECUTABLE)
25+
SOURCE = $(shell cd src ; ls *.cpp) $(shell cd src ; ls board/*.cpp)
26+
27+
all: debug
28+
29+
debug: CXX_PREPROCESSOR=$(DEBUG_PREPROCESSOR)
30+
debug: LIBS=$(DEBUG_LIBS)
31+
debug: main
32+
33+
release: CXX_PREPROCESSOR=$(RELEASE_PREPROCESSOR)
34+
release: LIBS=$(RELEASE_LIBS)
35+
release: main
36+
37+
main:
38+
cd src; \
39+
$(COMPILER) $(CXX_FLAGS) $(CXX_PREPROCESSOR) -o $(TARGET) $(CXX_INCLUDE) $(SOURCE) $(LIBS)
40+
41+
ifeq ($(UNAME), Darwin)
42+
sh MacOS/bundle.sh
43+
endif

config.xml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<chesspp>
2+
<data>
3+
<board>
4+
<initial_layout>res/games/new_game.txt</initial_layout>
5+
<width>8</width>
6+
<height>8</height>
7+
<cell_width>80</cell_width>
8+
<cell_height>80</cell_height>
9+
</board>
10+
</data>
11+
<images>
12+
<board>res/img/chessboard_640x640.png</board>
13+
<pieces>res/img/chess_pieces_80x80_each.png</pieces>
14+
<validMove>res/img/valid_move.png</validMove>
15+
</images>
16+
</chesspp>

res/games/new_game.txt

+8
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/AppState.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ namespace chesspp
1010
class AppState : public SFMLEvent
1111
{
1212
public:
13-
AppState() {}
13+
AppState(sf::RenderWindow *_display) : display(_display) {}
1414
virtual ~AppState() {}
1515

1616
virtual int id() = 0;
17-
virtual void OnRender(sf::RenderWindow &display) = 0;
17+
virtual void OnRender() = 0;
18+
19+
protected:
20+
sf::RenderWindow *display;
1821
};
1922
}
2023

src/AppStateGame.cpp

+19-5
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,31 @@
22

33
namespace chesspp
44
{
5+
AppStateGame::AppStateGame( Application* _app, sf::RenderWindow *_display ) : app(_app), AppState(_display), graphics(display)
6+
{
7+
board = new Board();
8+
board->newGame(boardConfig.getInitialLayout());
9+
}
10+
511
int AppStateGame::id() { return 1; }
612

7-
void AppStateGame::OnRender(sf::RenderWindow &display)
13+
void AppStateGame::OnRender()
814
{
9-
display.draw(sf::Sprite(TextureManager::getInstance().Load("../res/img/chessboard_640x640.png")));
15+
graphics.drawBoard(board);
1016
}
1117

1218
void AppStateGame::OnLButtonPressed(int x, int y)
1319
{
14-
#ifdef _DEBUG
15-
cout << "Left clicked at (" << x << ", " << y << ")\n";
16-
#endif // _DEBUG
20+
board->setSelected(board->getCurrent()); // No matter if NULL
21+
}
22+
void AppStateGame::OnMouseMoved(int x, int y)
23+
{
24+
board->setCurrent(x,y);
25+
}
26+
void AppStateGame::OnLButtonReleased(int x, int y)
27+
{
28+
// board knows what is selected, but I think this looks more clear
29+
board->move(board->getSelected(), x, y);
30+
board->setSelected(NULL);
1731
}
1832
}

src/AppStateGame.h

+13-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
#define _APPSTATEGAME_H
33
#include "SFML.hpp"
44
#include "TextureManager.h"
5+
#include "Graphics.h"
6+
#include "Configuration.h"
7+
#include "board/Board.h"
58

69
#ifdef _DEBUG
710
#include <iostream>
@@ -13,16 +16,22 @@ namespace chesspp
1316
class Application;
1417
class AppStateGame : public AppState
1518
{
16-
Application* app;
19+
Application* app;
20+
Board* board;
21+
22+
graphics::GraphicsHandler graphics;
23+
configuration::BoardConfig boardConfig;
1724

1825
public:
19-
AppStateGame(Application* _app) : app(_app) {}
26+
AppStateGame(Application* _app, sf::RenderWindow *_display);
2027
virtual ~AppStateGame() {}
2128

2229
virtual int id();
23-
virtual void OnRender(sf::RenderWindow &display);
30+
virtual void OnRender();
2431

25-
virtual void OnLButtonPressed(int x, int y); //example implementation
32+
virtual void OnLButtonPressed(int x, int y);
33+
virtual void OnLButtonReleased(int x, int y);
34+
virtual void OnMouseMoved(int x, int y);
2635
};
2736
}
2837

src/Application.cpp

+2-2
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/Configuration.h

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#ifndef _CONFIGURATION_H
2+
#define _CONFIGURATION_H
3+
4+
#include <string.h>
5+
#include <stdint.h>
6+
#include <boost/algorithm/string/replace.hpp>
7+
8+
#if defined(__linux__)
9+
#include <unistd.h>
10+
#elif defined(_WIN32)
11+
#include <Windows.h>
12+
#elif defined(__APPLE__)
13+
#include <mach-o/dyld.h>
14+
#endif
15+
16+
17+
#include "Exception.h"
18+
#include "XMLReader.h"
19+
#include "board/logger.h"
20+
21+
namespace chesspp
22+
{
23+
namespace configuration
24+
{
25+
class configuration
26+
{
27+
protected:
28+
std::string res_path;
29+
30+
//Linux and Windows, resource path is defined as the absolute path the folder where the application executable is stored.
31+
// <exe_location>/res/img/... should be where resources are stored.
32+
//OS x, resource path is defined as the absolute path to the Resources folder of the .app structure.
33+
// <.app>/Contents/Resources/res/img... should be where resources are stored.
34+
std::string getResourcePath()
35+
{
36+
char buf[1024];
37+
uint32_t size = sizeof(buf);
38+
memset(buf, 0, sizeof(buf));
39+
std::string ret;
40+
#if defined(__linux__)
41+
if(readlink("/proc/self/exe", buf, sizeof(buf)) == -1)
42+
throw chesspp::exception("Unable to determine executable path on Linux.");
43+
ret = buf;
44+
ret = ret.substr(0, ret.find_last_of('/')+1);
45+
46+
#elif defined(_WIN32)
47+
if(GetModuleFileNameA(NULL, buf, sizeof(buf)) == 0)
48+
throw chesspp::exception("Unable to determine executable path on Windows.");
49+
ret = buf;
50+
boost::replace_all(ret, "\\", "/");
51+
ret = ret.substr(0, ret.find_last_of('/')+1);
52+
53+
#elif defined(__APPLE__)
54+
if (_NSGetExecutablePath(buf, &size) != 0)
55+
throw chesspp::exception("Unable to determine executable path on OS x. (Buffer size too small?)");
56+
ret = buf;
57+
ret = ret.substr(0, ret.find_last_of('/')+1) + "../Resources/";
58+
//Need to go up one directory because the exe is stored in <.app>/Contents/MacOS/,
59+
//And we need <.app>/Contents/Resources
60+
61+
#else
62+
throw chesspp::exception("Unknown OS. Unable to determine executable path.");
63+
#endif
64+
65+
return ret;
66+
}
67+
68+
XMLReader reader;
69+
public:
70+
configuration(const std::string &configFile) : res_path(getResourcePath()), reader(getResourcePath() + configFile) {}
71+
virtual ~configuration() {}
72+
73+
};
74+
75+
class BoardConfig : public configuration
76+
{
77+
std::string initial_layout;
78+
uint8_t board_width, board_height;
79+
uint16_t cell_width, cell_height;
80+
81+
public:
82+
BoardConfig() : configuration("config.xml")
83+
{
84+
initial_layout = res_path + reader.getProperty<std::string>("chesspp.data.board.initial_layout");
85+
board_width = reader.getProperty<uint8_t>("chesspp.data.board.width");
86+
board_height = reader.getProperty<uint8_t>("chesspp.data.board.height");
87+
cell_width = reader.getProperty<uint16_t>("chesspp.data.board.cell_width");
88+
cell_height = reader.getProperty<uint16_t>("chesspp.data.board.cell_height");
89+
}
90+
91+
std::string getInitialLayout() { return initial_layout; }
92+
uint8_t getBoardWidth() { return board_width; }
93+
uint8_t getBoardHeight() { return board_height; }
94+
uint16_t getCellWidth() { return cell_width; }
95+
uint16_t getCellHeight() { return cell_height; }
96+
};
97+
98+
class GraphicsConfig : public configuration
99+
{
100+
std::string path_board, path_pieces, path_validMove;
101+
102+
public:
103+
GraphicsConfig() : configuration("config.xml")
104+
{
105+
path_board = res_path + reader.getProperty<std::string>("chesspp.images.board");
106+
path_pieces = res_path + reader.getProperty<std::string>("chesspp.images.pieces");
107+
path_validMove = res_path + reader.getProperty<std::string>("chesspp.images.validMove");
108+
}
109+
110+
std::string getSpritePath_board() { return path_board; }
111+
std::string getSpritePath_pieces() { return path_pieces; }
112+
std::string getSpritePath_validMove() { return path_validMove; }
113+
};
114+
}
115+
}
116+
117+
#endif

src/Exception.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
namespace chesspp
88
{
99
class exception : public std::exception
10-
{
10+
{
11+
using std::exception::what;
1112
public:
1213
exception() throw() {}
1314
virtual ~exception() throw() {};

0 commit comments

Comments
 (0)