105 lines
2.8 KiB
C++
105 lines
2.8 KiB
C++
/* Copyright 2020, Michele Santullo
|
|
* This file is part of wrenpp.
|
|
*
|
|
* Wrenpp is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* Wrenpp is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with wrenpp. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "wrenpp/vm_fun.hpp"
|
|
#include "wrenpp/def_configuration.hpp"
|
|
#include <iostream>
|
|
#include <string>
|
|
#include <fstream>
|
|
#include <random>
|
|
#include <chrono>
|
|
#include <thread>
|
|
|
|
namespace {
|
|
struct CustomData {
|
|
CustomData (std::random_device::result_type r) :
|
|
generator(r),
|
|
distrib(1, 6)
|
|
{
|
|
}
|
|
|
|
std::mt19937 generator;
|
|
std::uniform_int_distribution<int> distrib;
|
|
};
|
|
|
|
void user_input(wren::VM& vm) {
|
|
std::string retval;
|
|
std::cin >> retval;
|
|
set(vm, 0, retval);
|
|
}
|
|
|
|
void random_num(wren::VM& vm) {
|
|
auto* const cust_data = vm.user_data<CustomData>();
|
|
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,
|
|
const char* module_ptr,
|
|
const char* class_name_ptr,
|
|
bool is_static,
|
|
const char* signature_ptr
|
|
) {
|
|
std::string_view module(module_ptr);
|
|
std::string_view class_name(class_name_ptr);
|
|
std::string_view signature(signature_ptr);
|
|
//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;
|
|
|
|
return std::string(
|
|
std::istreambuf_iterator<char>(ifs),
|
|
std::istreambuf_iterator<char>()
|
|
);
|
|
}
|
|
} //unnamed namespace
|
|
|
|
int main() {
|
|
std::random_device rand_dev;
|
|
CustomData custom_data(rand_dev());
|
|
|
|
MyConfig config;
|
|
wren::VM vm(&config, &custom_data);
|
|
interpret(vm, "main", load_file("main.wren"));
|
|
wren::call<void>(vm, {"main", "the_game"}, "play_game");
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
|
return 0;
|
|
}
|