Bugfixes and improvements, simplify examples
This commit is contained in:
parent
fa0183a3bf
commit
c2089568f3
4 changed files with 18 additions and 53 deletions
|
@ -17,6 +17,7 @@
|
|||
|
||||
#include "wrenpp/vm_fun.hpp"
|
||||
#include "wrenpp/def_configuration.hpp"
|
||||
#include "wrenpp/callback_manager.hpp"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
|
@ -47,36 +48,6 @@ namespace {
|
|||
set(vm, 0, cust_data->distrib(cust_data->generator));
|
||||
}
|
||||
|
||||
class MyConfig : public wren::DefConfiguration {
|
||||
public:
|
||||
wren::foreign_method_t foreign_method_fn (
|
||||
wren::VM* vm,
|
||||
std::string_view module,
|
||||
std::string_view class_name,
|
||||
bool is_static,
|
||||
std::string_view signature
|
||||
) {
|
||||
//std::cout << "requested: \"" << module << "\" \"" <<
|
||||
// class_name << "\" \"" << signature << "\" static: " <<
|
||||
// std::boolalpha << is_static << '\n';
|
||||
|
||||
if ("main" == module) {
|
||||
if ("Game" == class_name) {
|
||||
if ("user_input()" == signature and is_static)
|
||||
return &user_input;
|
||||
else if ("random()" == signature and is_static)
|
||||
return &random_num;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
//char* load_module_fn (wren::VM* vm, const char* name) {
|
||||
// std::cout << "asked to load module \"" << name << "\"\n";
|
||||
// return nullptr;
|
||||
//}
|
||||
};
|
||||
|
||||
std::string load_file (const std::string& path) {
|
||||
std::ifstream ifs(path);
|
||||
ifs >> std::noskipws;
|
||||
|
@ -92,8 +63,11 @@ int main() {
|
|||
std::random_device rand_dev;
|
||||
CustomData custom_data(rand_dev());
|
||||
|
||||
MyConfig config;
|
||||
wren::DefConfiguration config;
|
||||
wren::VM vm(&config, &custom_data);
|
||||
vm.callback_manager().add_callback(true, "main", "Game", "user_input()", &user_input)
|
||||
.add_callback(true, "main", "Game", "random()", &random_num);
|
||||
|
||||
interpret(vm, "main", load_file("main.wren"));
|
||||
wren::call<void>(vm, {"main", "the_game"}, "play_game");
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue