Skip to content

Commit a282962

Browse files
committed
Initial port of demo code / engine to chaiscript
1 parent 24e077b commit a282962

15 files changed

+415
-17
lines changed

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
[submodule "SimpleJSON"]
22
path = SimpleJSON
33
url = git@github.com:lefticus/SimpleJSON.git
4+
[submodule "ChaiScript"]
5+
path = ChaiScript
6+
url = git@github.com:ChaiScript/ChaiScript

CMakeLists.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ endif()
152152
include_directories(include)
153153

154154
if(NOT MULTITHREAD_SUPPORT_ENABLED)
155-
add_definitions(-DCHAISCRIPT_NO_THREADS)
155+
add_definitions(-DCHAISCRIPT_NO_THREADS -DCHAISCRIPT_NO_THREADS_WARNING)
156156
endif()
157157

158158
if(CMAKE_HOST_UNIX)
@@ -181,8 +181,8 @@ set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${LINKER_FLAGS}")
181181
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${LINKER_FLAGS}")
182182
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${LINKER_FLAGS}")
183183

184-
add_executable(game_engine main.cpp game.cpp game_event.cpp map.cpp)
185-
target_link_libraries(game_engine ${SFML_GRAPHICS} ${SFML_WINDOW} ${SFML_SYSTEM})
184+
add_executable(game_engine main.cpp game.cpp game_event.cpp map.cpp chaiscript_stdlib.cpp chaiscript_bindings.cpp chaiscript_creator.cpp)
185+
target_link_libraries(game_engine ${SFML_GRAPHICS} ${SFML_WINDOW} ${SFML_SYSTEM} dl)
186186
file(COPY resources DESTINATION ${CMAKE_BINARY_DIR})
187187

188188
install(TARGETS game_engine RUNTIME DESTINATION bin)

ChaiScript

Submodule ChaiScript added at b270a19

chaiscript_bindings.cpp

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#include "ChaiScript/include/chaiscript/chaiscript.hpp"
2+
3+
#include "game.hpp"
4+
#include "map.hpp"
5+
#include "game_event.hpp"
6+
7+
#define ADD_FUN(Class, Name) module->add(chaiscript::fun(&Class::Name), "Name")
8+
9+
std::shared_ptr<chaiscript::Module> create_chaiscript_bindings()
10+
{
11+
auto module = std::shared_ptr<chaiscript::Module>(new chaiscript::Module());
12+
13+
ADD_FUN(Game, get_texture);
14+
ADD_FUN(Game, get_font);
15+
ADD_FUN(Game, teleport_to);
16+
ADD_FUN(Game, set_avatar);
17+
ADD_FUN(Game, add_map);
18+
ADD_FUN(Game, add_start_action);
19+
ADD_FUN(Game, add_queued_action);
20+
ADD_FUN(Game, show_message_box);
21+
ADD_FUN(Game, show_object_interaction_menu);
22+
ADD_FUN(Game, show_conversation);
23+
ADD_FUN(Game, has_pending_events);
24+
ADD_FUN(Game, get_current_event);
25+
ADD_FUN(Game, update);
26+
ADD_FUN(Game, draw);
27+
ADD_FUN(Game, get_avatar_position);
28+
ADD_FUN(Game, enter_map);
29+
ADD_FUN(Game, has_current_map);
30+
ADD_FUN(Game, get_current_map);
31+
ADD_FUN(Game, start);
32+
ADD_FUN(Game, set_flag);
33+
ADD_FUN(Game, get_flag);
34+
ADD_FUN(Game, set_rotate);
35+
ADD_FUN(Game, set_zoom);
36+
ADD_FUN(Game, rotate);
37+
ADD_FUN(Game, zoom);
38+
39+
module->add(chaiscript::fun(&Game::get_input_direction_vector), "get_input_direction_vector");
40+
41+
module->add(chaiscript::constructor<Answer (std::string, std::string)>(), "Answer");
42+
43+
module->add(chaiscript::constructor<
44+
Question (std::string, std::vector<Answer>,
45+
std::function<bool (const float, const float, Game &, Object &)>,
46+
std::function<void (const float, const float, Game &, Object &)>)>(), "Question");
47+
48+
module->add(chaiscript::constructor<Conversation(std::vector<Question>)>(), "Conversation");
49+
50+
module->add(chaiscript::constructor<Game_Action(std::string, std::function<void (const float, const float, Game &)>)>(), "Game_Action");
51+
ADD_FUN(Game_Action, description);
52+
ADD_FUN(Game_Action, action);
53+
54+
module->add(chaiscript::constructor<Object_Action(std::string, std::function<void (const float, const float, Game &, Object &)>)>(), "Object_Action");
55+
ADD_FUN(Object_Action, description);
56+
ADD_FUN(Object_Action, action);
57+
58+
59+
module->add(chaiscript::constructor<Object(std::string, const sf::Texture &, const int, const int, const float,
60+
std::function<void (const float, const float, Game &, Object &, sf::Sprite &)> ,
61+
std::function<std::vector<Object_Action> (const float, const float, Game &, Object &)> )>(), "Object");
62+
63+
ADD_FUN(Object, update);
64+
ADD_FUN(Object, get_actions);
65+
ADD_FUN(Object, do_collision);
66+
67+
module->add(chaiscript::constructor<Tile_Properties(bool)>(), "Tile_Properties");
68+
module->add(chaiscript::constructor<Tile_Properties(bool, std::function<void (float, float)>)>(), "Tile_Properties");
69+
ADD_FUN(Tile_Properties, do_movement_action);
70+
ADD_FUN(Tile_Properties, passable);
71+
ADD_FUN(Tile_Properties, movement_action);
72+
73+
module->add(chaiscript::constructor<Tile_Defaults(const int, Tile_Properties)>(), "Tile_Defaults");
74+
75+
module->add(chaiscript::constructor<Tile_Map(Game &, const std::string &, std::vector<Tile_Defaults>)>(), "Tile_Map");
76+
77+
ADD_FUN(Tile_Map, add_enter_action);
78+
ADD_FUN(Tile_Map, enter);
79+
ADD_FUN(Tile_Map, dimensions_in_pixels);
80+
ADD_FUN(Tile_Map, add_object);
81+
ADD_FUN(Tile_Map, get_bounding_box);
82+
ADD_FUN(Tile_Map, test_move);
83+
ADD_FUN(Tile_Map, get_collisions);
84+
ADD_FUN(Tile_Map, adjust_move);
85+
ADD_FUN(Tile_Map, do_move);
86+
ADD_FUN(Tile_Map, update);
87+
88+
89+
return module;
90+
}
91+

chaiscript_bindings.hpp

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
#ifndef CHAISCRIPT_BINDINGS
3+
#define CHAISCRIPT_BINDINGS
4+
5+
namespace chaiscript {
6+
class Module;
7+
}
8+
9+
std::shared_ptr<chaiscript::Module> create_chaiscript_bindings();
10+
11+
#endif

chaiscript_creator.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "ChaiScript/include/chaiscript/chaiscript.hpp"
2+
#include "chaiscript_stdlib.hpp"
3+
#include "chaiscript_bindings.hpp"
4+
5+
std::unique_ptr<chaiscript::ChaiScript> create_chaiscript()
6+
{
7+
auto chai = std::unique_ptr<chaiscript::ChaiScript>(new chaiscript::ChaiScript(create_chaiscript_stdlib()));
8+
chai->add(create_chaiscript_bindings());
9+
return chai;
10+
}
11+

chaiscript_creator.hpp

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
#ifndef CHAISCRIPT_CREATOR
3+
#define CHAISCRIPT_CREATOR
4+
5+
namespace chaiscript {
6+
class ChaiScript;
7+
}
8+
9+
std::unique_ptr<chaiscript::ChaiScript> create_chaiscript();
10+
11+
#endif

chaiscript_stdlib.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include "ChaiScript/include/chaiscript/chaiscript_stdlib.hpp"
2+
#include "chaiscript_stdlib.hpp"
3+
4+
std::shared_ptr<chaiscript::Module> create_chaiscript_stdlib()
5+
{
6+
return chaiscript::Std_Lib::library();
7+
}
8+

chaiscript_stdlib.hpp

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
#ifndef CHAISCRIPT_STDLIB
3+
#define CHAISCRIPT_STDLIB
4+
5+
namespace chaiscript {
6+
class Module;
7+
}
8+
9+
std::shared_ptr<chaiscript::Module> create_chaiscript_stdlib();
10+
11+
#endif

game.cpp

+25-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212

1313

1414
Game::Game()
15-
: m_map(m_maps.end())
15+
: m_map(m_maps.end()),
16+
m_rotate(0),
17+
m_zoom(1)
1618
{
1719
}
1820

@@ -252,10 +254,7 @@ void Game::start()
252254
}
253255
}
254256

255-
void Game::set_flag(const std::string &t_name)
256-
{
257-
set_flag(t_name, true);
258-
}
257+
259258

260259
void Game::set_flag(const std::string &t_name, bool t_value)
261260
{
@@ -273,4 +272,25 @@ bool Game::get_flag(const std::string &t_name) const
273272
}
274273
}
275274

275+
void Game::set_rotate(const float t_r)
276+
{
277+
m_rotate = t_r;
278+
}
279+
280+
void Game::set_zoom(const float t_z)
281+
{
282+
m_zoom = t_z;
283+
}
284+
285+
float Game::rotate()
286+
{
287+
return m_rotate;
288+
}
289+
290+
float Game::zoom()
291+
{
292+
return m_zoom;
293+
}
294+
295+
276296

game.hpp

+12-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ class Game : public sf::Drawable
1717
{
1818
public:
1919
Game();
20+
Game(const Game &) = delete;
21+
Game(Game &&) = default;
22+
2023

2124
const sf::Texture &get_texture(const std::string &t_filename) const;
2225
const sf::Font &get_font(const std::string &t_filename) const;
@@ -56,11 +59,16 @@ class Game : public sf::Drawable
5659

5760
void start();
5861

59-
void set_flag(const std::string &t_name);
6062
void set_flag(const std::string &t_name, bool t_value);
6163

6264
bool get_flag(const std::string &t_name) const;
6365

66+
void set_rotate(const float);
67+
void set_zoom(const float);
68+
69+
float rotate();
70+
float zoom();
71+
6472
private:
6573

6674
mutable std::map<std::string, sf::Texture> m_textures;
@@ -74,6 +82,9 @@ class Game : public sf::Drawable
7482
std::vector<std::function<void (Game &)>> m_start_actions;
7583

7684
std::map<std::string, bool> m_flags;
85+
86+
float m_rotate;
87+
float m_zoom;
7788
};
7889

7990

0 commit comments

Comments
 (0)