2016-08-28 5 views
0

編集:役に立たないコードを削除const charを初期化する**

私のゲームのGUIライブラリにIMGUIを使用しています。リストボックスを実装しようとしています。しかし、私のconst char **はIMGUIによって正しく読み込まれていないようです。

私のプロジェクトの残りの部分と、このファイルをコンパイルする
#include <tinydir.h> 
#include <tinyxml2.h> 
#include <cpplocate/ModuleInfo.h> 
#include <cpplocate/cpplocate.h> 
#include <iostream> 
#include <easylogging++.h> 

#include "DmuxCommon.hpp" 
#include "Garage.hpp" 
#include "client/Game.hpp" 

irr::f32 garageRotationRate = irr::f32(1.0f); 
irr::f32 gChassisRotation; 
irr::f32 gCameraRotation; 
int selection = 0; 

namespace menu { 

const char **Garage::names; 


    Garage::Garage() : 
    Gui(), 
    pMainScreen(Game::device->getSceneManager()->addEmptySceneNode()), 
    pMoonScreen(Game::device->getSceneManager()->addEmptySceneNode()), 
    availableChassis(getAvailableChassises()) { 

    // Prepare double rendering 
    pRenderTarget = Game::device->getVideoDriver()->addRenderTargetTexture(irr::core::dimension2d<irr::u32>(384, 300), "Moon"); 

    pRenderTextureID = pGUI->createTexture(pRenderTarget); 

    names = new const char *[availableChassis.size()]; 
    for(unsigned int i = 0; i < availableChassis.size(); ++i) { 
     names[i] = availableChassis[i].c_str(); 
    } 

    for(unsigned int i = 0; i < availableChassis.size(); ++i) { 
     std::cout << names[i] << std::endl; // This shows the content of the const char ** correctly 
    } 
    } 

    void Garage::show() { 

    //Rendering the node 
    Game::device->getVideoDriver()->setRenderTarget(pRenderTarget, true, true, irr::video::SColor(255, 120.0f, 120.0f, 120.0f)); 
    pMoonScreen->setVisible(true); 
    pMainScreen->setVisible(false); 
    Game::device->getSceneManager()->setActiveCamera(pMoonCam); 
    Game::device->getSceneManager()->drawAll(); 
    Game::device->getVideoDriver()->setRenderTarget(0, true, true, irr::video::SColor(255, 100, 101, 140)); 

    pMoonScreen->setVisible(false); 
    pMainScreen->setVisible(true); 
    Game::device->getSceneManager()->setActiveCamera(pMainCam); 

    pGUI->updateTexture(pRenderTextureID, pRenderTarget); 

    if(gChassis == nullptr) { 
     gChassis = Game::device->getSceneManager()->addMeshSceneNode(Game::device->getSceneManager()->getMesh((std::string(cpplocate::findModule("dmux").value("chassisDir") + "el-camino/el-camino.obj")).c_str())); 
     gChassis->setParent(pMoonScreen); 
     gChassis->setPosition(irr::core::vector3df(0, 0, 0)); 
     gChassis->setMaterialFlag(irr::video::EMF_LIGHTING, false); 
     gChassis->setMaterialFlag(irr::video::EMF_BACK_FACE_CULLING, false); 

     pMainCam = Game::device->getSceneManager()->addCameraSceneNode(pMoonScreen, irr::core::vector3df(0, 0, 0), irr::core::vector3df(0, 0, 0)); 
     pMoonCam = Game::device->getSceneManager()->addCameraSceneNode(pMoonScreen, irr::core::vector3df(0, 0, -5), irr::core::vector3df(0, 0, 0)); 
     pMoonCam->setTarget(gChassis->getPosition()); 
     gCameraRotation = irr::f32(1.0f); 
    } 

    ImGui::SetNextWindowPos(ImVec2(0.0f, 0.0f)); 
    ImGui::SetNextWindowSize(ImVec2(Game::playerSettings.currentWindowSize.first, 
            Game::playerSettings.currentWindowSize.second - (Game::playerSettings.currentWindowSize.second/9))); 

    ImGui::Begin("Customize a combat vehicle"); 

    ImGui::PushItemWidth(120); 
    ImGui::ListBox("", &selection, names, ((int)(sizeof(names)/sizeof(*names)))); 
    ImGui::PopItemWidth(); 

    ImGui::End(); 
    } 
} 

私はあなたはそれがconstのchar型の内容全体をプリントアウトしていない見ることができるように、この

https://s4.postimg.org/cyrdl2p8d/DMUX_130.png

のように見えるウィンドウを取得* *それはすべきである。 coutの文が正しく引用符なしで

  • 「エル・カミーノ」
  • 「Moscovitch」
  • 「ElCamino」

あるべき配列の内容をプリントアウトしている。しかし、それはなっています最初の値は正しく描かれています。名前変数の初期化に関連して何か間違っていますか? IMGUIのimgui_demo.cpp内のコードのそれはだから私はちょうど生の入力を取ったのではなく定義し使用してリストボックス

にはsizeofのためにこのマクロを使用しているため はsizeofは奇妙です。

+0

ヒント: 'sizeof(names)'とは何ですか? –

+1

問題を示すMCVEを作成してみてください。あなたがあなた自身を悩まされていない場合、他の人があなたの問題を見つけるために歩き回ることをほとんど期待することができないということをあなたが投稿したコードにあまり関係のない塊がある。無関係なものを切り抜ける過程で、あなたは "aha!"を持っているかもしれません。問題が何であるか自分で解決してください。そうでない場合は、他の人があなたを助けるチャンスを得るでしょう。 – Peter

+0

もし私が「最小」の例を提供していたら、それはちょうど私が既に言及した上記のconst char **の内容を私が含んでいることになります。問題はGUIコード内にありますので私はそれを私の質問に含めました。 – bkeys

答えて

0

編集:より良い答えを入れて

私はそれを見て一日中それを得ました!私は、私はそれを修正するために管理

availableChassis.size() 

を言って、それを修正し、正しい方向に私を指しているためバダウィ@Paul @Ismailと1201ProgramAlarm @ありがとう。

0

あなたが言っていることです:リストボックスに1つの選択肢を表示しています。リスト要素の数の計算が間違っています。 sizeof(names)は両方ともポインタなのでsizeof(*names)と同じです。

+0

私はそれを何に変更するべきですか?私はavailableChassis.size()を試しましたが、それは同じことをやっています。 – bkeys

+0

@bkeys 'names'のために' availableChassis.size() '要素を割り当てていますので、' names'のサイズを渡す必要があるところで 'availableChassis.size()'を渡すだけです。 'sizeof(array)/ sizeof(* array)'は、 'sizeof(array)'が 'array'の実際のサイズになるとき、スタック上の配列に対して機能します。あなたのケースでは 'sizeof(names)== sizeof(char **)'が必要なものではありません。 – Paul

+0

@Paul ListBox関数の呼び出しにsizeof(availableChassis.size())を渡そうとしましたが、試みたときにフォールトが発生します。 – bkeys

0

また、データがconst char * []形式でない場合、一時配列を作成する代わりに、ラムダをListBoxに渡してavailableChassis []名から直接文字列にアクセスすることができます。