Skip to content

Commit 08676b9

Browse files
committed
2 parents fe8040b + 0cea265 commit 08676b9

9 files changed

+149
-300
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ endif()
114114
if(MSVC)
115115
add_definitions(/W4 /w44640)
116116
add_definitions(/bigobj)
117+
# add_definitions(-DSFML_STATIC)
117118
# Note on MSVC compiler flags.
118119
# The code base selective disables warnings as necessary when the compiler is complaining too much
119120
# about something that is perfectly valid, or there is simply no technical way around it

chaiscript_bindings.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
std::shared_ptr<chaiscript::Module> create_chaiscript_bindings()
1111
{
12-
auto module = std::shared_ptr<chaiscript::Module>(new chaiscript::Module());
12+
auto module = std::make_shared<chaiscript::Module>();
1313
module->add(chaiscript::vector_conversion<std::vector<Tile_Defaults>>());
1414
module->add(chaiscript::vector_conversion<std::vector<Answer>>());
1515
module->add(chaiscript::vector_conversion<std::vector<Question>>());
@@ -29,6 +29,11 @@ std::shared_ptr<chaiscript::Module> create_chaiscript_bindings()
2929
ADD_FUN(Game, show_message_box);
3030
ADD_FUN(Game, show_object_interaction_menu);
3131
ADD_FUN(Game, show_selection_menu);
32+
module->add(
33+
chaiscript::fun([](Game &t_game, const float t_game_time, const float t_simulation_time, const std::vector<Game_Action> &t_selections)
34+
{
35+
t_game.show_selection_menu(t_game_time, t_simulation_time, t_selections);
36+
}), "show_selection_menu");
3237
ADD_FUN(Game, show_conversation);
3338
ADD_FUN(Game, has_pending_events);
3439
ADD_FUN(Game, get_current_event);

game.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void Game::teleport_to_tile(const int t_x, const int t_y)
5151
const auto x = (tile_size.x * t_x) + ((tile_size.x - avatar_size.width) / 2);
5252
const auto y = (tile_size.y * t_y) + ((tile_size.y - avatar_size.height) / 2);
5353

54-
teleport_to(x,y);
54+
teleport_to(float(x), float(y));
5555
}
5656

5757
void Game::set_avatar(const sf::Texture &t_avatar)
@@ -136,9 +136,9 @@ void Game::show_object_interaction_menu(const float t_game_time, const float t_s
136136
m_game_events.emplace_back(new Object_Interaction_Menu(t_obj, get_font("resources/FreeMonoBold.ttf"), 17, sf::Color(255,255,255,255), sf::Color(0,200,200,255), sf::Color(0,0,0,128), sf::Color(255,255,255,200), 3, t_obj.get_actions(t_game_time, t_simulation_time, *this)));
137137
}
138138

139-
void Game::show_selection_menu(const float t_game_time, const float t_simulation_time, const std::vector<Game_Action> &t_selections)
139+
void Game::show_selection_menu(const float /*t_game_time*/, const float /*t_simulation_time*/, const std::vector<Game_Action> &t_selections, const size_t t_selection)
140140
{
141-
m_game_events.emplace_back(new Selection_Menu(get_font("resources/FreeMonoBold.ttf"), 17, sf::Color(255,255,255,255), sf::Color(0,200,200,255), sf::Color(0,0,0,128), sf::Color(255,255,255,200), 3, t_selections));
141+
m_game_events.emplace_back(new Selection_Menu(get_font("resources/FreeMonoBold.ttf"), 17, sf::Color(255,255,255,255), sf::Color(0,200,200,255), sf::Color(0,0,0,128), sf::Color(255,255,255,200), 3, t_selections, t_selection));
142142
}
143143

144144
bool Game::has_pending_events() const

game.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
class Tile_Map;
1212
class Object;
1313
class Game_Event;
14-
class Game_Action;
15-
class Conversation;
14+
struct Game_Action;
15+
struct Conversation;
1616

1717
class Game : public sf::Drawable
1818
{
@@ -38,7 +38,7 @@ class Game : public sf::Drawable
3838

3939
void show_message_box(const sf::String &t_msg);
4040

41-
void show_selection_menu(const float t_game_time, const float t_simulation_time, const std::vector<Game_Action> &t_selections);
41+
void show_selection_menu(const float t_game_time, const float t_simulation_time, const std::vector<Game_Action> &t_selections, const size_t t_selection = 0);
4242
void show_object_interaction_menu(const float t_game_time, const float t_simulation_time, Object &t_obj);
4343
void show_conversation(const float t_game_time, const float t_simulation_time, Object &t_obj, const Conversation &t_conversation);
4444

game_event.cpp

+19-13
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,14 @@ void Message_Box::draw(sf::RenderTarget& target, sf::RenderStates states) const
8181

8282
Selection_Menu::Selection_Menu(sf::Font t_font, int t_font_size,
8383
sf::Color t_font_color, sf::Color t_selected_font_color, sf::Color t_fill_color, sf::Color t_outline_color, float t_outlineThickness,
84-
std::vector<Game_Action> t_actions)
84+
std::vector<Game_Action> t_actions, const size_t t_selection)
8585
: Game_Event(),
8686
m_font(std::move(t_font)), m_font_color(std::move(t_font_color)),
8787
m_selected_color(std::move(t_selected_font_color)),
8888
m_selected_cur_color(m_selected_color),
8989
m_fill_color(std::move(t_fill_color)), m_outline_color(std::move(t_outline_color)),
90-
m_outline_thickness(t_outlineThickness), m_actions(std::move(t_actions))
90+
m_outline_thickness(t_outlineThickness), m_actions(std::move(t_actions)),
91+
m_current_item(t_selection)
9192
{
9293
setPosition(10,10);
9394

@@ -135,17 +136,22 @@ void Selection_Menu::update(const float t_game_time, const float t_simulation_ti
135136
}
136137
}(Game::get_input_direction_vector());
137138

138-
if (m_last_direction != direction)
139-
{
140-
m_current_item += direction;
141-
}
142-
143-
if (m_current_item < 0) {
144-
m_current_item = m_actions.size() - 1;
145-
} else if (m_current_item == static_cast<int>(m_actions.size())) {
146-
m_current_item = 0;
147-
}
139+
const auto new_item = [&]() {
140+
if (m_last_direction != direction)
141+
{
142+
if (m_current_item == 0 && direction == -1) {
143+
return m_actions.size() - 1;
144+
} else if (m_current_item == (m_actions.size() - 1) && direction == 1) {
145+
return size_t(0);
146+
} else {
147+
return m_current_item += direction;
148+
}
149+
} else {
150+
return m_current_item;
151+
}
152+
}();
148153

154+
m_current_item = new_item;
149155
m_last_direction = direction;
150156
}
151157

@@ -204,7 +210,7 @@ Object_Interaction_Menu::Object_Interaction_Menu(Object &t_obj, sf::Font t_font,
204210
}
205211

206212
return res;
207-
}(t_actions, t_obj))
213+
}(t_actions, t_obj), 0)
208214
{
209215
}
210216

game_event.hpp

+8-7
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
class Object;
99
class Game;
10-
class Object_Action;
11-
class Game_Action;
10+
struct Object_Action;
11+
struct Game_Action;
1212

1313
struct Answer
1414
{
@@ -114,16 +114,17 @@ class Selection_Menu : public Game_Event
114114
public:
115115
Selection_Menu(sf::Font t_font, int t_font_size,
116116
sf::Color t_font_color, sf::Color t_selected_font_color, sf::Color t_fill_color, sf::Color t_outline_color, float t_outlineThickness,
117-
std::vector<Game_Action> t_actions);
117+
std::vector<Game_Action> t_actions,
118+
const size_t t_selection);
118119

119120
virtual ~Selection_Menu() = default;
120121

121-
virtual void update(const float t_game_time, const float t_simulation_time, Game &t_game);
122+
virtual void update(const float t_game_time, const float t_simulation_time, Game &t_game) override;
122123

123-
virtual bool is_done() const;
124+
virtual bool is_done() const override;
124125

125126
protected:
126-
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
127+
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
127128

128129
private:
129130
sf::Font m_font;
@@ -135,7 +136,7 @@ class Selection_Menu : public Game_Event
135136
float m_outline_thickness;
136137
std::vector<sf::Text> m_texts;
137138

138-
int m_current_item = 0;
139+
size_t m_current_item = 0;
139140
int m_last_direction = 0;
140141
float m_start_time = 0;
141142
bool m_is_done = false;

main.cpp

+5-183
Original file line numberDiff line numberDiff line change
@@ -34,187 +34,6 @@ Game build_chai_game(chaiscript::ChaiScript &chai)
3434
return game;
3535
}
3636

37-
Game build_game()
38-
{
39-
Game game;
40-
41-
// create the tilemap from the level definition
42-
Tile_Map map(game, "resources/Maps/worldmap.json", {{2, Tile_Properties(false)}});
43-
// Tile_Map map(game, "resources/Maps/test.json", {{2, Tile_Properties(false)}});
44-
45-
46-
const auto collision_action = [](const float t_game_time, const float t_simulation_time, Game &t_game, Object &t_obj, sf::Sprite &/*t_collision*/)
47-
{
48-
t_game.show_object_interaction_menu(t_game_time, t_simulation_time, t_obj);
49-
};
50-
51-
const auto bobby_pinhead_conversation_tree =
52-
Conversation({
53-
Question("Pinhead Town?",
54-
{Answer("Bobby Pinhead", "Pinhead town is where we call home. Be sure to check out the shops.")},
55-
[](const float, const float, Game &t_game, Object &) { return t_game.get_flag("pinhead_town"); },
56-
[](const float, const float, Game &, Object &) { }
57-
),
58-
Question("Treasure in forest?",
59-
{Answer("Bobby Pinhead", "I've heard about it...\nbut I don't know anyone who has found it yet."),
60-
Answer("Random Pinhead", "Bah, treasure in the forest.")},
61-
[](const float, const float, Game &t_game, Object &) { return t_game.get_flag("treasure_adventure"); },
62-
[](const float, const float, Game &, Object &) { }
63-
)
64-
});
65-
66-
67-
68-
const auto random_pinhead_conversation_tree =
69-
Conversation({
70-
Question("Where am I?",
71-
{Answer("Random Pinhead", "You are in the Pinhead Town Center."),
72-
Answer("Bobby Pinhead", "Pinhead Town is a great place, you'll like it here.\nThere is lots of adventure.")},
73-
[](const float, const float, Game &, Object &) { return true; },
74-
[](const float, const float, Game &t_game, Object &) { t_game.set_flag("pinhead_town", true); }
75-
),
76-
Question("Treasure In Forest?",
77-
{Answer("Random Pinhead", "I'm skeptical about that treasure.\nSome people say they have seen it,\nbut why haven't they become rich?"),
78-
Answer("Random Pinhead", "I think it's just a pipe dream of that Baker.\nHe's the one who likes to talk about it so much.")},
79-
[](const float, const float, Game &t_game, Object &) { return t_game.get_flag("treasure_adventure"); },
80-
[](const float, const float, Game &, Object &) { }
81-
),
82-
});
83-
84-
85-
const auto bobby_pinhead_actions = [bobby_pinhead_conversation_tree](const float /*t_game_time*/, const float /*t_simulation_time*/, Game &/*t_game*/, Object &/*t_obj*/){
86-
return std::vector<Object_Action>{
87-
{ "Look",
88-
[](const float, const float, Game &t_game, Object &)
89-
{
90-
t_game.show_message_box("Bobby Pinhead.\n\nA pinhead youth.");
91-
}
92-
},
93-
{ "Talk To",
94-
[bobby_pinhead_conversation_tree](const float t_game_time, const float t_simulation_time, Game &t_game, Object &t_obj)
95-
{
96-
t_game.show_conversation(t_game_time, t_simulation_time, t_obj, bobby_pinhead_conversation_tree);
97-
}
98-
},
99-
};
100-
};
101-
102-
const auto random_pinhead_actions = [random_pinhead_conversation_tree](const float /*t_game_time*/, const float /*t_simulation_time*/, Game &/*t_game*/, Object &/*t_obj*/){
103-
return std::vector<Object_Action>{
104-
{ "Look",
105-
[](const float, const float, Game &t_game, Object &)
106-
{
107-
t_game.show_message_box("A Random Pinhead.\n\nOne of the local pinheads.");
108-
}
109-
},
110-
{ "Talk To",
111-
[random_pinhead_conversation_tree](const float t_game_time, const float t_simulation_time, Game &t_game, Object &t_obj)
112-
{
113-
t_game.show_conversation(t_game_time, t_simulation_time, t_obj, random_pinhead_conversation_tree);
114-
}
115-
},
116-
};
117-
};
118-
119-
const auto left_door_actions = [](const float /*t_game_time*/, const float /*t_simulation_time*/, Game &/*t_game*/, Object &/*t_obj*/){
120-
return std::vector<Object_Action>{
121-
{ "Look",
122-
[](const float, const float, Game &t_game, Object &)
123-
{
124-
t_game.show_message_box("The Bakery");
125-
}
126-
},
127-
};
128-
};
129-
130-
const auto path_actions = [](const float /*t_game_time*/, const float /*t_simulation_time*/, Game &/*t_game*/, Object &/*t_obj*/){
131-
return std::vector<Object_Action>{
132-
{ "Look",
133-
[](const float, const float, Game &t_game, Object &)
134-
{
135-
t_game.show_message_box("Forest Path");
136-
}
137-
},
138-
};
139-
};
140-
141-
const auto gate_actions = [](const float /*t_game_time*/, const float /*t_simulation_time*/, Game &/*t_game*/, Object &/*t_obj*/){
142-
return std::vector<Object_Action>{
143-
{ "Look",
144-
[](const float, const float, Game &t_game, Object &)
145-
{
146-
t_game.show_message_box("Garden Gate");
147-
}
148-
},
149-
};
150-
};
151-
152-
153-
const auto up_door_actions = [](const float /*t_game_time*/, const float /*t_simulation_time*/, Game &/*t_game*/, Object &/*t_obj*/){
154-
return std::vector<Object_Action>{
155-
{ "Look",
156-
[](const float, const float, Game &t_game, Object &)
157-
{
158-
t_game.show_message_box("The General Store");
159-
}
160-
},
161-
{ "Talk To",
162-
[](const float , const float , Game &t_game, Object &)
163-
{
164-
t_game.show_message_box("You hear a voice from beyond the door:\n'Come in, we're open!'");
165-
}
166-
},
167-
};
168-
};
169-
170-
/*
171-
Object general_store_door("General Store Door", game.get_texture("resources/pinheads_up_door.png"), 48, 26, 0, collision_action, up_door_actions);
172-
general_store_door.setPosition(299,134);
173-
map.add_object(general_store_door);
174-
175-
Object bakery_door("Bakery Store Door", game.get_texture("resources/pinheads_left_door.png"), 22, 40, 0, collision_action, left_door_actions);
176-
bakery_door.setPosition(138, 191);
177-
map.add_object(bakery_door);
178-
179-
Object path("Forest Path", game.get_texture("resources/pinheads_path.png"), 22, 11, 0, collision_action, path_actions);
180-
path.setPosition(137, 168);
181-
map.add_object(path);
182-
183-
Object gate("Garden Gate", game.get_texture("resources/pinheads_gate.png"), 39, 12, 0, collision_action, gate_actions);
184-
gate.setPosition(344, 148);
185-
map.add_object(gate);
186-
187-
Object bobby_pinhead("Bobby Pinhead", game.get_texture("resources/pinheads_pinhead.png"), 26, 26, 0, collision_action, bobby_pinhead_actions);
188-
bobby_pinhead.setPosition(270,220);
189-
map.add_object(bobby_pinhead);
190-
191-
Object random_pinhead("Random Pinhead", game.get_texture("resources/pinheads_pinhead.png"), 26, 26, 0, collision_action, random_pinhead_actions);
192-
random_pinhead.setPosition(240,250);
193-
map.add_object(random_pinhead);
194-
*/
195-
196-
map.add_enter_action(
197-
[](Game &t_game){
198-
t_game.teleport_to(200, 200);
199-
t_game.show_message_box("Welcome to Pinhead Town.");
200-
}
201-
);
202-
203-
game.add_map("town", map);
204-
game.set_avatar(game.get_texture("resources/pinheads_marble.png"));
205-
206-
207-
game.add_start_action(
208-
[](Game &t_game) {
209-
t_game.show_message_box("Welcome to\nPinheads:\n Everything You Need.");
210-
t_game.add_queued_action([](const float, const float, Game &t_t_game) {
211-
t_t_game.enter_map("town");
212-
});
213-
}
214-
);
215-
216-
return game;
217-
}
21837

21938
int main()
22039
{
@@ -223,6 +42,7 @@ int main()
22342
// create the window
22443
sf::RenderWindow window(sf::VideoMode(800, 600), "Tilemap");
22544

45+
window.setVerticalSyncEnabled(true);
22646
auto chaiscript = create_chaiscript();
22747

22848
auto game = build_chai_game(*chaiscript);
@@ -246,7 +66,9 @@ int main()
24666

24767
if (frame_count % 100 == 0)
24868
{
249-
std::cout << 1/time_elapsed << "fps avg fps: " << frame_count / game_time << '\n';
69+
const auto avgfps = frame_count / game_time;
70+
const auto curfps = 1 / time_elapsed;
71+
std::cout << curfps << "fps avg fps: " << avgfps << '\n';
25072
}
25173

25274
last_frame = cur_frame;
@@ -262,7 +84,7 @@ int main()
26284
{
26385
// update the view to the new size of the window
26486
window.setSize(sf::Vector2u(event.size.width, event.size.height));
265-
fixed = sf::View(sf::FloatRect(0,0,event.size.width, event.size.height));
87+
fixed = sf::View(sf::FloatRect(0,0, float(event.size.width), float(event.size.height)));
26688
}
26789
}
26890

0 commit comments

Comments
 (0)