Initializing a const char **

Initializing a const char **

本文关键字:char const Initializing      更新时间:2023-10-16

编辑:删除一些无用的代码

我使用IMGUI为我的游戏的GUI库,我正试图实现列表框。然而,我的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();
  }
}

编译这个文件与我的项目的其余部分,我得到一个窗口,看起来像这样

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

可以看到,它没有打印const char **的全部内容。但是cout语句正确地打印出了数组的内容应该是

    "捷径"
  • "以及"
  • "ElCamino"

如果不加引号,它将正确获取第一个值,如图所示。是否有什么错误的我正在做涉及名称变量的初始化?sizeof很奇怪,因为IMGUI的imgui_demo.cpp中的代码使用了这个宏来处理列表框的sizeof

#define IM_ARRAYSIZE(_ARR) ((int)(sizeof(_ARR)/sizeof(*_ARR)))

所以我只是使用原始输入,而不是使用定义。

它正在做你告诉它的事情:在列表框中显示一个选择。你对列表元素数目的计算是错误的。sizeof(names)sizeof(*names)相同,因为它们都是指针。

编辑:输入更好的答案

我看了一整天才找到它!我修改了
availableChassis.size()

我设法修复了它,感谢@Ismail Badawi @Paul和@ 120programalarm为我指出了正确的方向。

如果你的数据不是在const char*[]格式,那么,而不是创建一个临时数组,你可以传递一个lambda到ListBox来访问你的字符串直接从你的availablech西斯[]名称