编辑:删除了一些无用的代码
我正在使用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语句正确地打印出应该是
的数组内容没有引号,正如图所示正确获得第一个值。我是否有任何错误涉及名称变量的初始化? sizeof是奇怪的,因为IMGUI的imgui_demo.cpp中的代码将这个宏用于listbox上的sizeof
#define IM_ARRAYSIZE(_ARR) ((int)(sizeof(_ARR)/sizeof(*_ARR)))
所以我只是使用原始输入而不是使用define。
答案 0 :(得分:0)
它正在做你所说的:在列表框中显示1个选项。您对列表元素数量的计算是错误的。 sizeof(names)
与sizeof(*names)
相同,因为它们都是指针。
答案 1 :(得分:0)
编辑:提出更好的答案
我看了一整天后就明白了!我通过说
来修复它availableChassis.size()
我设法解决了这个问题,谢谢@Ismail Badawi @Paul和@ 1201ProgramAlarm指出我正确的方向。
答案 2 :(得分:0)
此外,如果您的数据不是const char * []格式,那么您可以将lambda传递给ListBox,直接从availableChassis []名称访问字符串,而不是创建临时数组。