Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/AppState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ namespace chesspp
class AppState : public SFMLEvent
{
public:
AppState(sf::RenderWindow *_display) : display(_display) {}
virtual ~AppState() {}
AppState(sf::RenderWindow *_display)
: display(_display)
{
}
virtual ~AppState()
{
}

virtual int id() = 0;
virtual void OnRender() = 0;
Expand Down
27 changes: 15 additions & 12 deletions src/AppStateGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,37 @@

namespace chesspp
{
AppStateGame::AppStateGame( Application*_app, sf::RenderWindow *_display )
: AppState(_display)
, app(_app)
, graphics(display)
AppStateGame::AppStateGame(Application *_app, sf::RenderWindow *_display)
: AppState(_display)
, app(_app)
, graphics(display)
{
board = new board::Board();
board->newGame(boardConfig.getInitialLayout());
}

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

void AppStateGame::OnRender()
{
graphics.drawBoard(board);
}

void AppStateGame::OnLButtonPressed(int x, int y)
void AppStateGame::OnLButtonPressed(int x, int y) noexcept
{
board->setSelected(board->getCurrent()); // No matter if NULL
board->setSelected(board->getCurrent()); //No matter if nullptr
}
void AppStateGame::OnMouseMoved(int x, int y)
void AppStateGame::OnMouseMoved(int x, int y) noexcept
{
board->setCurrent(x,y);
board->setCurrent(x, y);
}
void AppStateGame::OnLButtonReleased(int x, int y)
void AppStateGame::OnLButtonReleased(int x, int y) noexcept
{
// board knows what is selected, but I think this looks more clear
//board knows what is selected, but I think this looks more clear
board->move(board->getSelected(), x, y);
board->setSelected(NULL);
board->setSelected(nullptr);
}
}
16 changes: 9 additions & 7 deletions src/AppStateGame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,24 @@ namespace chesspp
class Application;
class AppStateGame : public AppState
{
Application*app;
board::Board*board;
Application *app;
board::Board *board;

graphics::GraphicsHandler graphics;
config::BoardConfig boardConfig;

public:
AppStateGame(Application*_app, sf::RenderWindow *_display);
virtual ~AppStateGame() {}
AppStateGame(Application *_app, sf::RenderWindow *_display);
virtual ~AppStateGame()
{
}

virtual int id();
virtual void OnRender();

virtual void OnLButtonPressed(int x, int y);
virtual void OnLButtonReleased(int x, int y);
virtual void OnMouseMoved(int x, int y);
virtual void OnLButtonPressed(int x, int y) noexcept;
virtual void OnLButtonReleased(int x, int y) noexcept;
virtual void OnMouseMoved(int x, int y) noexcept;
};
}

Expand Down
10 changes: 6 additions & 4 deletions src/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
namespace chesspp
{
Application::Application()
: display(sf::VideoMode(640, 640), "ChessPlusPlus", sf::Style::Close),
running(true),
state(new AppStateGame(this, &display))
: display(sf::VideoMode(640, 640), "ChessPlusPlus", sf::Style::Close)
, running(true)
, state(new AppStateGame(this, &display))
{
display.setVerticalSyncEnabled(true);
}
Expand All @@ -17,7 +17,9 @@ namespace chesspp
while(running)
{
while(display.pollEvent(Event))
{
OnEvent(&Event);
}

state->OnRender();
display.display();
Expand All @@ -28,7 +30,7 @@ namespace chesspp

Application::~Application()
{
delete state; //Even if it's null, no matter.
delete state;
}

void Application::OnEvent(sf::Event *Event)
Expand Down
5 changes: 3 additions & 2 deletions src/Application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ namespace chesspp
{
sf::RenderWindow display;
bool running;
AppState*state;
AppState *state;
void OnEvent(sf::Event *Event);

public:
Application();
~Application();

template<class NewState> void ChangeState()
template<class NewState>
void ChangeState()
{
delete state;
state = new NewState;
Expand Down
13 changes: 12 additions & 1 deletion src/Debug.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,25 @@
#include <streambuf>

class LogUtil //replaces std::clog, std::cerr, std::cout with file streams
{ //should eventually convert to singleton
{
std::ofstream log {"debug_log.log", std::ios::out|std::ios::trunc}
, err {"debug_err.log", std::ios::out|std::ios::trunc}
, out {"debug_out.log", std::ios::out|std::ios::trunc};
std::streambuf *clogbuf {log ? std::clog.rdbuf(log.rdbuf()) : std::clog.rdbuf()}
, *cerrbuf {err ? std::cerr.rdbuf(err.rdbuf()) : std::cerr.rdbuf()}
, *coutbuf {out ? std::cout.rdbuf(out.rdbuf()) : std::cout.rdbuf()};
LogUtil() noexcept
{
}
LogUtil(LogUtil const &) = delete;
LogUtil(LogUtil &&) = delete;
LogUtil &operator=(LogUtil const &) = delete;
LogUtil &operator=(LogUtil &&) = delete;
public:
static void EnableRedirection() noexcept
{
static LogUtil lu;
}
~LogUtil() noexcept
{
std::clog.rdbuf(clogbuf), clogbuf = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

int main()
{
LogUtil lu; //replaces std::clog, std::cerr, std::cout with file streams
LogUtil::EnableRedirection();

try
{
Expand Down
64 changes: 28 additions & 36 deletions src/ResourceManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,67 +7,59 @@

namespace chesspp
{
template <class T, class key_type, class deleter_type = std::default_delete<T>>
template<class T, class key_type, class deleter_type = std::default_delete<T>>
class ResourceManager
{
private:
//no copying
ResourceManager(const ResourceManager<T, key_type, deleter_type>&);
ResourceManager<T, deleter_type> &operator=(const ResourceManager<T, key_type, deleter_type>&);
ResourceManager(ResourceManager<T, key_type, deleter_type> const &) = delete;
ResourceManager<T, deleter_type> &operator=(ResourceManager<T, key_type, deleter_type> const &) = delete;

private:
typedef std::unique_ptr<T, deleter_type> ptr_t;
typedef typename std::map<key_type, ptr_t> map_t;
typedef typename map_t::iterator map_i;
using ptr_t = std::unique_ptr<T, deleter_type>;
using map_t = typename std::map<key_type, ptr_t>;
using map_i = typename map_t::iterator;

map_t m_map; //resource map

protected:
inline ResourceManager() {}
inline ~ResourceManager() {}
ResourceManager() noexcept
{
}
~ResourceManager() noexcept
{
}

//pure virtual, defined depending on what is being loaded.
virtual T *onLoadResource(const key_type &key) = 0;
virtual T *onLoadResource(key_type const &key) noexcept = 0;

public:
//************************************
// Method: Free
// FullName: ResourceManager<T, deleter_type>::Free
// Access: public
// Returns: void
// Parameter: const std::string &key
// Deletes the entry of a key in the resource map.
// This will call deleter_type to deallocate the resource from memory as well.
//************************************
void Free(const key_type &key) {
//Deletes the entry of a key in the resource map.
//This will call deleter_type to deallocate the resource from memory as well.
void Free(key_type const &key) noexcept
{
map_i i = m_map.find(key);
m_map.erase(i);
}

//************************************
// Method: Load
// FullName: ResourceManager<T, deleter_type>::Load
// Access: public
// Returns: ptr_t
// Parameter: const std::string &key
// Returns a reference to the resource associated with the file name 'key' if it exists in memory.
// Otherwise it loads the texture into memory, and returns a reference to the the resource.
//************************************
T &Load(const std::string &key)
//Returns a reference to the resource associated with the file name 'key' if it exists in memory.
//Otherwise it loads the texture into memory, and returns a reference to the the resource.
T &Load(key_type const &key) noexcept(false)
{
map_i i = m_map.find(key);
if(i != m_map.end())
{
return *i->second.get(); //return resource if exists
}

//else, load resource
ptr_t p(onLoadResource(key));
ptr_t p {onLoadResource(key)};
if(p.get() == NULL)
throw Exception("Error loading Image at " + key); //figure out better way to throw exceptions later
{
throw Exception(std::string("Error loading Image at ") + key);
}

m_map.insert(std::make_pair(key, std::move(p)));
return *m_map[key].get();
}

private:
map_t m_map; //resource map
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/SFML.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#define SFMLFULL_INCLUDED

#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Graphics.hpp> //\
#include <SFML/Network.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
Expand Down
54 changes: 29 additions & 25 deletions src/SFMLEvent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,35 @@ namespace chesspp
class SFMLEvent
{
public:
typedef unsigned int uint;
virtual void OnClosed() {}
virtual void OnResized(uint w, uint h) {}
virtual void OnLostFocus() {}
virtual void OnGainedFocus() {}
virtual void OnTextEntered(sf::Uint32 unicode) {}
virtual void OnKeyPressed(sf::Keyboard::Key key, bool alt, bool control, bool shift, bool system) {}
virtual void OnKeyReleased(sf::Keyboard::Key key, bool alt, bool control, bool shift, bool system) {}
virtual void OnMouseWheelMoved(int delta, int x, int y) {}
virtual void OnLButtonPressed(int x, int y) {}
virtual void OnLButtonReleased(int x, int y) {}
virtual void OnRButtonPressed(int x, int y) {}
virtual void OnRButtonReleased(int x, int y) {}
virtual void OnMButtonPressed(int x, int y) {}
virtual void OnMButtonReleased(int x, int y) {}
virtual void OnMouseButtonPressed(sf::Mouse::Button button, int x, int y) {}
virtual void OnMouseButtonReleased(sf::Mouse::Button button, int x, int y) {}
virtual void OnMouseMoved(int x, int y) {}
virtual void OnMouseEnteredWindow() {}
virtual void OnMouseLeftWindow() {}
virtual void OnJoystickButtonPressed(uint joystickID, uint button) {}
virtual void OnJoystickButtonReleased(uint joystickID, uint button) {}
virtual void OnJoystickMoved(uint joystickID, sf::Joystick::Axis axis, float position) {}
virtual void OnJoystickConnected(uint joystickID) {}
virtual void OnJoystickDisconnected(uint joystickID) {}
using uint = unsigned int;
virtual void OnClosed () noexcept {}
virtual void OnResized (uint w, uint h) noexcept {}
virtual void OnLostFocus () noexcept {}
virtual void OnGainedFocus () noexcept {}
virtual void OnTextEntered (sf::Uint32 unicode) noexcept {}
virtual void OnKeyPressed (sf::Keyboard::Key key, bool alt, bool control, bool shift, bool system) noexcept {}
virtual void OnKeyReleased (sf::Keyboard::Key key, bool alt, bool control, bool shift, bool system) noexcept {}
virtual void OnMouseWheelMoved (int delta, int x, int y) noexcept {}
virtual void OnLButtonPressed (int x, int y) noexcept {}
virtual void OnLButtonReleased (int x, int y) noexcept {}
virtual void OnRButtonPressed (int x, int y) noexcept {}
virtual void OnRButtonReleased (int x, int y) noexcept {}
virtual void OnMButtonPressed (int x, int y) noexcept {}
virtual void OnMButtonReleased (int x, int y) noexcept {}
virtual void OnMouseButtonPressed (sf::Mouse::Button button, int x, int y) noexcept {}
virtual void OnMouseButtonReleased (sf::Mouse::Button button, int x, int y) noexcept {}
virtual void OnMouseMoved (int x, int y) noexcept {}
virtual void OnMouseEnteredWindow () noexcept {}
virtual void OnMouseLeftWindow () noexcept {}
virtual void OnJoystickButtonPressed (uint joystickID, uint button) noexcept {}
virtual void OnJoystickButtonReleased(uint joystickID, uint button) noexcept {}
virtual void OnJoystickMoved (uint joystickID, sf::Joystick::Axis axis, float position) noexcept {}
virtual void OnJoystickConnected (uint joystickID) noexcept {}
virtual void OnJoystickDisconnected (uint joystickID) noexcept {}

virtual ~SFMLEvent() noexcept
{
}
};
}

Expand Down
Loading