2022-04-28 20:56:11 +00:00
|
|
|
/* Copyright 2020-2022, Michele Santullo
|
2020-05-02 20:55:10 +00:00
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2020-05-02 20:41:58 +00:00
|
|
|
#include "wrenpp/vm_fun.hpp"
|
|
|
|
#include "wrenpp/def_configuration.hpp"
|
2022-04-29 16:35:46 +00:00
|
|
|
#include "wrenpp/callback_manager.hpp"
|
2020-05-02 20:41:58 +00:00
|
|
|
#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;
|
|
|
|
};
|
|
|
|
|
2022-05-16 23:00:00 +00:00
|
|
|
void user_input(wren::VM& vm, wren::ModuleAndName) {
|
2020-05-02 20:41:58 +00:00
|
|
|
std::string retval;
|
|
|
|
std::cin >> retval;
|
|
|
|
set(vm, 0, retval);
|
|
|
|
}
|
|
|
|
|
2022-05-16 23:00:00 +00:00
|
|
|
void random_num(wren::VM& vm, wren::ModuleAndName) {
|
2020-05-02 20:41:58 +00:00
|
|
|
auto* const cust_data = vm.user_data<CustomData>();
|
|
|
|
set(vm, 0, cust_data->distrib(cust_data->generator));
|
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
2022-04-29 16:35:46 +00:00
|
|
|
wren::DefConfiguration config;
|
2020-05-02 20:41:58 +00:00
|
|
|
wren::VM vm(&config, &custom_data);
|
2022-05-18 23:06:09 +00:00
|
|
|
vm.callback_manager().add_callback(true, "main", "Game", "user_input()", [](){return &user_input;})
|
|
|
|
.add_callback(true, "main", "Game", "random()", [](){return &random_num;});
|
2022-04-29 16:35:46 +00:00
|
|
|
|
2020-05-02 20:41:58 +00:00
|
|
|
interpret(vm, "main", load_file("main.wren"));
|
2022-05-16 23:00:00 +00:00
|
|
|
wren::call<void>(vm, wren::MN<"main", "the_game">, "play_game");
|
2020-05-02 20:41:58 +00:00
|
|
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
|
|
|
return 0;
|
|
|
|
}
|