小编Orf*_*fby的帖子

有没有一个很好的方法用Luabridge将sf :: Event暴露给Lua?

根据LuaBridge自述文件,LuaBridge不支持"枚举常数",我认为这只是enums.由于sf::Event几乎完全是enums,有没有什么办法,我可以揭露类?目前,我能想出的唯一其他解决方案是在C++中检测按键,然后向Lua发送一个字符串,用于描述事件.显然,在现代键盘上有大约100多个键,这将导致一个巨大的,丑陋的段if语句.

对于那些没有使用过SFML的人:链接到sf :: Event类源代码


更新:

在尝试创建我的问题中概述的函数之后,我发现它无论如何都不起作用,因为你不能在C++中返回多个字符串,因此大多数事件都被忽略.

示例源(不起作用):

std::string getEvent()
{
    sf::Event event;
    while (window.pollEvent(event))
    {
        if (event.type == sf::Event::Closed) {window.close(); return "";}
        else if (event.type == sf::Event::GainedFocus) {return "GainedFocus";}
        else if (event.type == sf::Event::LostFocus) {return "LostFocus";}
        else if (event.type == sf::Event::Resized) {return "Resized";}
        else if (event.type == sf::Event::TextEntered)
        {
            if ((event.text.unicode < 128) && (event.text.unicode > 0)) {return "" + static_cast<char>(event.text.unicode);}
        }
        else if (event.type == sf::Event::KeyPressed)
        {
            //If else for …
Run Code Online (Sandbox Code Playgroud)

c++ lua enums sfml luabridge

12
推荐指数
1
解决办法
595
查看次数

尝试发送第二个有效载荷后,"解码有效载荷时出错"

我正在尝试使用websocketpp库实现连接到WebSocket(精确的Discord网关)的客户端,但是当我尝试将JSON有效负载发送到服务器时,我收到错误

我正在使用的代码是:

//Standard C++:
#include <string>
//JSON Header (nlohmann's library):
#include <json.hpp>
//Networking Headers:
#include <websocketpp/client.hpp>
#include <websocketpp/config/asio_client.hpp>
#define WEBSOCKETPP_STRICT_MASKING

std::string token;

static websocketpp::lib::shared_ptr<boost::asio::ssl::context> on_tls_init(websocketpp::connection_hdl)
{
    websocketpp::lib::shared_ptr<boost::asio::ssl::context> ctx = websocketpp::lib::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::sslv23);

    ctx->set_options(boost::asio::ssl::context::default_workarounds |
                     boost::asio::ssl::context::no_sslv2 |
                     boost::asio::ssl::context::no_sslv3 |
                     boost::asio::ssl::context::single_dh_use);

    return ctx;
}

void onMessage(websocketpp::client<websocketpp::config::asio_tls_client>* client, websocketpp::connection_hdl hdl, websocketpp::config::asio_tls_client::message_type::ptr msg)
{
    //Get the payload
    nlohmann::json payload = nlohmann::json::parse(msg->get_payload());
    //If the op code is 'hello'
    if (payload.at("op") == 10)
    {
        //HEARTBEAT STUFF HAS BEEN REMOVED FOR SIMPLICITY
        //Create the identity …
Run Code Online (Sandbox Code Playgroud)

c++ boost-asio websocket

9
推荐指数
1
解决办法
692
查看次数

如何将C++联合公开给Lua

对于我正在研究的项目,我需要将另一个库中的一些C++类暴露给Lua.不幸的是,这个库中最重要的一个类有很多Unions和Enums(来自SFML的sf :: Event类),而且我从快速的Google搜索中发现,没有什么可以将C++ Unions暴露给Lua.我不介意它是用Lua/C API,库还是绑定生成器公开,只要它有效.但是,我不想使用绑定生成器,因为我希望能够在C++中创建一个对象,然后将该对象的实例暴露给Lua(除非可以使用绑定生成器)

c++ lua unions

7
推荐指数
1
解决办法
522
查看次数

Cin无需等待输入?

对于我正在进行的项目,我需要程序能够接收来自用户的输入,但是当他们输入内容时,程序可以继续循环.

例如:
while (true)
{
    if (userInput == true)
    {
        cin >> input
    }
    //DO SOMETHING
}
Run Code Online (Sandbox Code Playgroud)

这意味着//DO SOMETHING每次循环都会发生这种情况,而用户不会按下百万次输入.

之前,我的解决方案是使用kbhit()getch()从conio.h 创建我自己的输入,但是这非常混乱,我不喜欢使用conio.h以便于携带等.此外,它不需要cin专门使用,因为那里是一个很好的机会它只是不能用它,所以任何好的解决方案,不需要我用"不太好"的库自己输入,将非常感激.

c++

5
推荐指数
1
解决办法
1万
查看次数

"错误:在尝试嵌入Python时,未在cmath中声明':: hypot'

在尝试将Python嵌入到我的程序中时遇到了一些麻烦#include <Python.h>,我终于找到了所有正确的库,但是我有另一个错误.当我尝试使用#include <Python.h>它进行编译时,将我重定向到我的code :: blocks目录中的cmath,并按照说出的行放置一个错误标记using ::hypot;:error: '::hypot' has not been declared.我不知道为什么这是一个错误,特别是因为我的代码:: blocks安装,并且出现了,我想,因为Python试图包含它.我在Windows上,并使用最新版本的Python(3.4.2)

c++ python

4
推荐指数
1
解决办法
8455
查看次数

将 std::string 传递到 PyObject_CallFunction

当我运行时pResult = PyObject_CallFunction(pFunc, "s", &"String"),python 脚本返回正确的字符串。但是,如果我尝试运行这个:

std::string passedString = "String";
pResult = PyObject_CallFunction(pFunc, "s", &passedString)
Run Code Online (Sandbox Code Playgroud)

然后将 pResult 转换为 a std::string,我<NULL>在打印时得到。这是一些(可能)完整的返回代码<NULL>

C++代码:

#include <Python.h>
#include <string>
#include <iostream>

int main()
{
    PyObject *pName, *pModule, *pDict, *pFunc;

    // Set PYTHONPATH TO working directory
    setenv("PYTHONPATH",".",1); //This doesn't help
    setenv("PYTHONDONTWRITEBYTECODE", " ", 1);

    // Initialize the Python Interpreter
    Py_Initialize();

    // Build the name object
    pName = PyUnicode_FromString((char*)"string");
    // Load the module object
    pModule = PyImport_Import(pName);
    // pDict …
Run Code Online (Sandbox Code Playgroud)

c++ python python-embedding

4
推荐指数
1
解决办法
4255
查看次数

从 C++ 调用 python 脚本,无需每次加载文件

我有一个 python 脚本,我需要调用很多次(大约 160000 次),虽然我可以使用硬 C++ 代码在不到一秒的时间内完成此操作,但如果我尝试加载并调用 python 脚本来执行此操作,它将可能需要几个小时!我认为如果我加载脚本一次,然后一遍又一遍地运行加载的脚本,速度会明显加快。不幸的是,我不知道该怎么做。我认为我无法使用 加载文件ifstream,然后PyRun_SimpleString在字符串的所有行上使用。但是,如果速度不快,是否可以在 python 中返回一个 2D 数组,然后将该数组转换为std::vector

c++ python python-embedding

3
推荐指数
1
解决办法
900
查看次数

函数指针类型之间的转换

我有一个程序必须将函数存储为void (*) (void*). 使用该签名创建函数会导致代码重复(通常第一行是将 void 指针转换为正确的类型)并降低类型安全性(void 指针可以转换为任何内容,例如错误的类型),所以我想知道如果我可以采用类型 的函数void (*)(T*),然后将其转换为 avoid(*)(void*)并像这样调用它,类似于这样:

#include <iostream>
#include <string>

void printer(std::string* string)
{
    std::cout << *string << std::endl;
}

int main() 
{
    //Cast the function to a one that takes a void pointer
    auto func = reinterpret_cast<void(*)(void*)>(&printer);
    //Create a string and call the function with it
    std::string string = "Hello World";
    func(&string);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

上面的代码编译并运行良好(在ideone上),但我想知道它是否符合所有标准,或者它是否是未定义的行为并且仅适用于我的特定示例和操作系统

c++ casting function-pointers

3
推荐指数
1
解决办法
1598
查看次数

string.find在搜索"."时不返回nil.

这个问题并没有很多解释,所以如果我把它输入到Lua Interpreter(从这里修改):

print(tostring(string.find("Hello Lua user", "banana")))
Run Code Online (Sandbox Code Playgroud)

返回的输出nil正如预期的那样.但是,如果我用"."尝试同样的事情.而不是"香蕉",像这样:

print(tostring(string.find("Hello Lua user", ".")))
Run Code Online (Sandbox Code Playgroud)

我明白了1,这是为什么?有什么方法可以检测字符串中是否有点?(如果它是相关的,我需要知道,所以我可以检测目录是文件还是文件夹(是的,我知道你可以在文件夹中有点/在Linux上的文件中没有点))

string lua find

2
推荐指数
1
解决办法
107
查看次数