0

処理のために新しい文字列を Lua に返す前に、指定されたテキストを中央に揃えるゲームサーバー DLL 用の関数を C++ で作成しようとしています。さまざまなサイトの例を見てかなりの時間を費やしましたが、「cout」しか見つけることができませんでした。これは、コンソール アプリケーションに表示されたくないためです。

私はC++が初めてで、これにアプローチする方法について本当に混乱しています。誰かが例を挙げてそれがどのように機能するかを説明できれば、将来のためにこれを行う方法を学ぶことができます.

基本的に、これは次のことを行います。

  1. 文字列を Lua から C++ に転送します。
  2. C++ は、転送したばかりの文字列を中央揃えにします。
  3. 完成した文字列を Lua に返します。

これが私がやろうとしてきたことのサンプルです:

int CScriptBind_GameRules::CentreTextForConsole(IFunctionHandler *pH, const char *input)
{
    if (input)
    {
        int l=strlen(input);
        int pos=(int)((113-l)/2);
        for(int i=0;i<pos;i++)
            std::cout<<" ";
        std::cout<<input;
        return pH->EndFunction(input); 
    }
    else
    {
        CryLog("[System] Error in CScriptBind_GameRules::CentreTextForConsole: Failed to align");
        return pH->EndFunction();
    }
    return pH->EndFunction();
}

どちらがビルドされますが、完成した文字列を転送するのではなく、テキストをコンソールに出力します。

4

3 に答える 3

3

I'm going to assume you already know how to pass a string from Lua to C++ and return the result from C++ to Lua, so the only part we need to deal with is producing the centered string.

That, however, is pretty easy:

std::string center(std::string input, int width = 113) { 
    return std::string((width - input.length()) / 2, ' ') + input;
}
于 2013-07-07T15:26:43.293 に答える
0
std::string center (const std::string& s, unsigned width)
{
    assert (width > 0);
    if (int padding = width - s.size (), pad = padding >> 1; pad > 0)
        return std::string (padding, ' ').insert (pad, s);
    return s;
}
于 2019-08-21T14:57:44.293 に答える