Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2c19792

Browse files
author
Thumperrr
committedFeb 24, 2013
Merge pull request #12 from Thumperrr/master
Framework overhaul
2 parents 01adfd1 + 1a8e1a3 commit 2c19792

13 files changed

+231
-427
lines changed
 

‎src/AppState.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef _APPSTATE_H
22
#define _APPSTATE_H
3+
34
#include "SFMLEvent.h"
5+
#include <SFML/Graphics.hpp>
46

57
namespace chesspp
68
{
@@ -11,12 +13,12 @@ namespace chesspp
1113
AppState() {}
1214
virtual ~AppState() {}
1315

14-
virtual void onActivate() = 0;
15-
void onEvent(sf::Event *Event) { SFMLEvent::OnEvent(Event); }
16-
virtual void onLoop() = 0;
17-
virtual void onRender(sf::RenderWindow *display) = 0;
18-
virtual void onDeactivate() = 0;
16+
virtual int id() = 0;
17+
virtual void OnRender(sf::RenderWindow &display) = 0;
1918
};
2019
}
2120

21+
#include "AppStateGame.h"
22+
/* Convenience header inclusion so we don't have to include each individual inheritor */
23+
2224
#endif

‎src/AppStateGame.cpp

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,18 @@
1-
#include "AppStateGame.h"
1+
#include "AppState.h"
22

33
namespace chesspp
44
{
5-
void AppStateGame::onRender(sf::RenderWindow *display)
5+
int AppStateGame::id() { return 1; }
6+
7+
void AppStateGame::OnRender(sf::RenderWindow &display)
68
{
7-
display->draw(sf::Sprite(TextureManager::getInstance().Load("../res/img/chessboard_640x640.png")));
8-
}
9-
10-
void AppStateGame::onLoop()
11-
{
12-
13-
}
14-
15-
void AppStateGame::onActivate()
16-
{
17-
#ifdef _DEBUG
18-
cout << "AppStateGame activated." << endl; //<--- placeholder for proof of concept
19-
#endif // _DEBUG
20-
}
21-
22-
void AppStateGame::onDeactivate()
23-
{
24-
#ifdef _DEBUG
25-
cout << "AppStateGame deactivated." << endl; //<--- placeholder for proof of concept
26-
#endif // _DEBUG
9+
display.draw(sf::Sprite(TextureManager::getInstance().Load("../res/img/chessboard_640x640.png")));
2710
}
2811

2912
void AppStateGame::OnLButtonPressed(int x, int y)
3013
{
3114
#ifdef _DEBUG
32-
cout << "Left button clicked." << endl;
15+
cout << "Left clicked at (" << x << ", " << y << ")\n";
3316
#endif // _DEBUG
34-
}
17+
}
3518
}

‎src/AppStateGame.h

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#define _APPSTATEGAME_H
33
#include "SFML.hpp"
44
#include "TextureManager.h"
5-
#include "AppState.h"
65

76
#ifdef _DEBUG
87
#include <iostream>
@@ -11,29 +10,19 @@
1110

1211
namespace chesspp
1312
{
13+
class Application;
1414
class AppStateGame : public AppState
1515
{
16+
Application* app;
17+
1618
public:
17-
AppStateGame(const AppStateGame&);
18-
AppStateGame() {}
19-
~AppStateGame() {}
19+
AppStateGame(Application* _app) : app(_app) {}
20+
virtual ~AppStateGame() {}
21+
22+
virtual int id();
23+
virtual void OnRender(sf::RenderWindow &display);
2024

21-
static AppStateGame *getInstance() //singleton class
22-
{
23-
static AppStateGame instance;
24-
return &instance;
25-
}
26-
27-
void onActivate();
28-
void onLoop();
29-
void onRender(sf::RenderWindow *display);
30-
void onDeactivate();
31-
32-
//Event handler example. SFMLEvent interface redirects certain events into function calls.
33-
virtual void OnLButtonPressed(int x, int y);
34-
35-
private:
36-
//members of game can go here, board, etc etc.
25+
virtual void OnLButtonPressed(int x, int y); //example implementation
3726
};
3827
}
3928

‎src/AppStateManager.cpp

Lines changed: 0 additions & 42 deletions
This file was deleted.

‎src/AppStateManager.h

Lines changed: 0 additions & 31 deletions
This file was deleted.

‎src/Application.cpp

Lines changed: 157 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,178 @@
11
#include "Application.h"
2+
#include <iostream>
23

34
namespace chesspp
45
{
5-
bool Application::onInit()
6+
Application::Application()
7+
: display(sf::VideoMode(640, 640), "ChessPlusPlus", sf::Style::Close),
8+
running(true),
9+
state(new AppStateGame(this))
610
{
7-
//start the first active app state (will be intro in the future)
8-
AppStateManager::setActiveAppState(APPSTATE_GAME);
9-
return true;
10-
}
11-
12-
void Application::onEvent(sf::Event *Event)
13-
{
14-
SFMLEvent::OnEvent(Event);
15-
AppStateManager::onEvent(Event);
16-
}
17-
18-
void Application::onLoop()
19-
{
20-
AppStateManager::onLoop();
21-
}
22-
23-
void Application::onRender()
24-
{
25-
AppStateManager::onRender(&display); //pass display to the app state.
26-
27-
display.display(); //flip the display
28-
}
29-
30-
void Application::onCleanup()
31-
{
32-
//cleanup things that need to be cleaned up
11+
display.setVerticalSyncEnabled(true);
3312
}
3413

3514
int Application::Exec()
3615
{
37-
if(!onInit())
38-
return -1;
39-
4016
sf::Event Event;
4117
while(running)
4218
{
4319
while(display.pollEvent(Event))
44-
onEvent(&Event);
20+
OnEvent(&Event);
4521

46-
onLoop();
47-
onRender();
22+
state->OnRender(display);
23+
display.display();
4824
}
49-
50-
onCleanup();
25+
5126
return 0;
5227
}
5328

54-
void Application::OnClosed()
29+
Application::~Application()
5530
{
56-
running = false;
31+
delete state; //Even if it's null, no matter.
5732
}
33+
34+
void Application::OnEvent(sf::Event *Event)
35+
{
36+
switch(Event->type)
37+
{
38+
case sf::Event::Closed:
39+
{
40+
state->OnClosed();
41+
running = false;
42+
break;
43+
}
44+
case sf::Event::Resized:
45+
{
46+
state->OnResized(Event->size.width, Event->size.height);
47+
break;
48+
}
49+
case sf::Event::LostFocus:
50+
{
51+
state->OnLostFocus();
52+
break;
53+
}
54+
case sf::Event::GainedFocus:
55+
{
56+
state->OnGainedFocus();
57+
break;
58+
}
59+
case sf::Event::TextEntered:
60+
{
61+
state->OnTextEntered(Event->text.unicode);
62+
break;
63+
}
64+
case sf::Event::KeyPressed:
65+
{
66+
state->OnKeyPressed(Event->key.code, Event->key.alt, Event->key.control, Event->key.shift, Event->key.system);
67+
break;
68+
}
69+
case sf::Event::KeyReleased:
70+
{
71+
state->OnKeyReleased(Event->key.code, Event->key.alt, Event->key.control, Event->key.shift, Event->key.system);
72+
break;
73+
}
74+
case sf::Event::MouseWheelMoved:
75+
{
76+
state->OnMouseWheelMoved(Event->mouseWheel.delta, Event->mouseWheel.x, Event->mouseWheel.y);
77+
break;
78+
}
79+
case sf::Event::MouseButtonPressed:
80+
{
81+
switch(Event->mouseButton.button)
82+
{
83+
case sf::Mouse::Left:
84+
{
85+
state->OnLButtonPressed(Event->mouseButton.x, Event->mouseButton.y);
86+
break;
87+
}
88+
case sf::Mouse::Right:
89+
{
90+
state->OnRButtonPressed(Event->mouseButton.x, Event->mouseButton.y);
91+
break;
92+
}
93+
case sf::Mouse::Middle:
94+
{
95+
state->OnMButtonPressed(Event->mouseButton.x, Event->mouseButton.y);
96+
break;
97+
}
98+
default:
99+
{
100+
state->OnMouseButtonPressed(Event->mouseButton.button, Event->mouseButton.x, Event->mouseButton.y);
101+
break;
102+
}
103+
}
104+
break;
105+
}
106+
case sf::Event::MouseButtonReleased:
107+
{
108+
switch(Event->mouseButton.button)
109+
{
110+
case sf::Mouse::Left:
111+
{
112+
state->OnLButtonReleased(Event->mouseButton.x, Event->mouseButton.y);
113+
break;
114+
}
115+
case sf::Mouse::Right:
116+
{
117+
state->OnRButtonReleased(Event->mouseButton.x, Event->mouseButton.y);
118+
break;
119+
}
120+
case sf::Mouse::Middle:
121+
{
122+
state->OnMButtonReleased(Event->mouseButton.x, Event->mouseButton.y);
123+
break;
124+
}
125+
default:
126+
{
127+
state->OnMouseButtonPressed(Event->mouseButton.button, Event->mouseButton.x, Event->mouseButton.y);
128+
break;
129+
}
130+
}
131+
break;
132+
}
133+
case sf::Event::MouseMoved:
134+
{
135+
state->OnMouseMoved(Event->mouseMove.x, Event->mouseMove.y);
136+
break;
137+
}
138+
case sf::Event::MouseEntered:
139+
{
140+
state->OnMouseEnteredWindow();
141+
break;
142+
}
143+
case sf::Event::MouseLeft:
144+
{
145+
state->OnMouseLeftWindow();
146+
break;
147+
}
148+
case sf::Event::JoystickButtonPressed:
149+
{
150+
state->OnJoystickButtonPressed(Event->joystickButton.joystickId, Event->joystickButton.button);
151+
break;
152+
}
153+
case sf::Event::JoystickButtonReleased:
154+
{
155+
state->OnJoystickButtonReleased(Event->joystickButton.joystickId, Event->joystickButton.button);
156+
break;
157+
}
158+
case sf::Event::JoystickMoved:
159+
{
160+
state->OnJoystickMoved(Event->joystickMove.joystickId, Event->joystickMove.axis, Event->joystickMove.position);
161+
break;
162+
}
163+
case sf::Event::JoystickConnected:
164+
{
165+
state->OnJoystickConnected(Event->joystickConnect.joystickId);
166+
break;
167+
}
168+
case sf::Event::JoystickDisconnected:
169+
{
170+
state->OnJoystickDisconnected(Event->joystickConnect.joystickId);
171+
break;
172+
}
173+
default:
174+
break;
175+
}
176+
}
177+
58178
}

‎src/Application.h

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,27 @@
11
#ifndef _APPLICATION_H
22
#define _APPLICATION_H
33

4-
#include "AppStateManager.h"
5-
#include "SFMLEvent.h"
6-
#include <iostream>
4+
#include "AppState.h"
75

86
namespace chesspp
97
{
10-
class Application : public SFMLEvent
8+
class Application
119
{
12-
protected:
13-
Application(const Application&);
14-
Application &operator=(const Application&);
15-
16-
public:
17-
Application() : display(sf::VideoMode(640, 640), "ChessPlusPlus"), running(true) {}
10+
sf::RenderWindow display;
11+
bool running;
12+
AppState* state;
13+
void OnEvent(sf::Event *Event);
1814

19-
bool onInit();
20-
void onEvent(sf::Event *Event);
21-
void onLoop();
22-
void onRender();
23-
void onCleanup();
15+
public:
16+
Application();
17+
~Application();
2418

19+
template<class NewState> void ChangeState()
20+
{
21+
delete state;
22+
state = new NewState;
23+
}
2524
int Exec();
26-
27-
virtual void OnClosed(); //example of Event handling.
28-
29-
private:
30-
sf::RenderWindow display;
31-
bool running;
3225
};
3326
}
3427

‎src/ChessLogic.h++

Lines changed: 0 additions & 1 deletion
This file was deleted.

‎src/exception.h renamed to ‎src/Exception.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,20 @@
22
#define _EXCEPTION_H
33

44
#include <string>
5+
#include <exception>
56

67
namespace chesspp
78
{
8-
class exception
9-
{
10-
protected:
11-
exception &operator=(const exception &) throw();
12-
9+
class exception : public std::exception
10+
{
1311
public:
1412
exception() throw() {}
15-
exception(const std::string &_e) throw() : e(_e) {}
16-
exception(const exception &) throw() {}
1713
virtual ~exception() throw() {};
14+
exception(const exception &) throw() {}
1815

19-
virtual std::string what() const throw() {return e;}
16+
exception(const std::string &_e) throw() : e(_e) {}
17+
virtual const char *what() { return e.c_str(); }
18+
2019
private:
2120
std::string e;
2221
};

‎src/ResourceManager.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#include <map>
55
#include <memory>
6-
#include "exception.h"
6+
#include "Exception.h"
77

88
#ifdef _DEBUG
99
#include <iostream>
@@ -27,7 +27,7 @@ namespace chesspp
2727

2828
protected:
2929
inline ResourceManager() {}
30-
inline virtual ~ResourceManager() {}
30+
inline ~ResourceManager() {}
3131

3232
//pure virtual, defined depending on what is being loaded.
3333
virtual T *onLoadResource(const std::string &key) = 0;

‎src/SFMLEvent.cpp

Lines changed: 0 additions & 205 deletions
This file was deleted.

‎src/SFMLEvent.h

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef _SDLEVENT_H
2-
#define _SDLEVENT_H
1+
#ifndef _EVENT_H
2+
#define _EVENT_H
33

44
#include "SFML.hpp"
55

@@ -9,35 +9,31 @@ namespace chesspp
99
{
1010
public:
1111
typedef unsigned int uint;
12-
SFMLEvent();
13-
virtual ~SFMLEvent();
14-
15-
virtual void OnEvent(sf::Event *Event);
16-
virtual void OnClosed();
17-
virtual void OnResized(uint w, uint h);
18-
virtual void OnLostFocus();
19-
virtual void OnGainedFocus();
20-
virtual void OnTextEntered(sf::Uint32 unicode);
21-
virtual void OnKeyPressed(sf::Keyboard::Key key, bool alt, bool control, bool shift, bool system);
22-
virtual void OnKeyReleased(sf::Keyboard::Key key, bool alt, bool control, bool shift, bool system);
23-
virtual void OnMouseWheelMoved(int delta, int x, int y);
24-
virtual void OnLButtonPressed(int x, int y);
25-
virtual void OnLButtonReleased(int x, int y);
26-
virtual void OnRButtonPressed(int x, int y);
27-
virtual void OnRButtonReleased(int x, int y);
28-
virtual void OnMButtonPressed(int x, int y);
29-
virtual void OnMButtonReleased(int x, int y);
30-
virtual void OnMouseButtonPressed(sf::Mouse::Button button, int x, int y);
31-
virtual void OnMouseButtonReleased(sf::Mouse::Button button, int x, int y);
32-
virtual void OnMouseMoved(int x, int y);
33-
virtual void OnMouseEnteredWindow();
34-
virtual void OnMouseLeftWindow();
35-
virtual void OnJoystickButtonPressed(uint joystickID, uint button);
36-
virtual void OnJoystickButtonReleased(uint joystickID, uint button);
37-
virtual void OnJoystickMoved(uint joystickID, sf::Joystick::Axis axis, float position);
38-
virtual void OnJoystickConnected(uint joystickID);
39-
virtual void OnJoystickDisconnected(uint joystickID);
12+
virtual void OnClosed() {}
13+
virtual void OnResized(uint w, uint h) {}
14+
virtual void OnLostFocus() {}
15+
virtual void OnGainedFocus() {}
16+
virtual void OnTextEntered(sf::Uint32 unicode) {}
17+
virtual void OnKeyPressed(sf::Keyboard::Key key, bool alt, bool control, bool shift, bool system) {}
18+
virtual void OnKeyReleased(sf::Keyboard::Key key, bool alt, bool control, bool shift, bool system) {}
19+
virtual void OnMouseWheelMoved(int delta, int x, int y) {}
20+
virtual void OnLButtonPressed(int x, int y) {}
21+
virtual void OnLButtonReleased(int x, int y) {}
22+
virtual void OnRButtonPressed(int x, int y) {}
23+
virtual void OnRButtonReleased(int x, int y) {}
24+
virtual void OnMButtonPressed(int x, int y) {}
25+
virtual void OnMButtonReleased(int x, int y) {}
26+
virtual void OnMouseButtonPressed(sf::Mouse::Button button, int x, int y) {}
27+
virtual void OnMouseButtonReleased(sf::Mouse::Button button, int x, int y) {}
28+
virtual void OnMouseMoved(int x, int y) {}
29+
virtual void OnMouseEnteredWindow() {}
30+
virtual void OnMouseLeftWindow() {}
31+
virtual void OnJoystickButtonPressed(uint joystickID, uint button) {}
32+
virtual void OnJoystickButtonReleased(uint joystickID, uint button) {}
33+
virtual void OnJoystickMoved(uint joystickID, sf::Joystick::Axis axis, float position) {}
34+
virtual void OnJoystickConnected(uint joystickID) {}
35+
virtual void OnJoystickDisconnected(uint joystickID) {}
4036
};
4137
}
4238

43-
#endif
39+
#endif

‎src/TextureManager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ namespace chesspp
5858
//************************************
5959
sf::Texture *onLoadResource(const std::string &location)
6060
{
61+
cout << "Loading texture: " << location << endl;
6162
sf::Texture *ret = new sf::Texture();
6263
if(!ret->loadFromFile(location))
6364
return NULL;

0 commit comments

Comments
 (0)
Please sign in to comment.