2011-01-07 56 views
0

使用我有一个字符串数组C++的std :: string到char与OpenGL的LoadBMP

string name[1000]; 

int counter; 
counter = 0; 

while(FindNextFile(fHandle, &wf)) 
{ 

... //some more code which is checking if its a folder 

string theName = wf.cFileName; 
if(theName.find(".bmp") != std::string::npos) 
{ 
    name[counter] = theName; 
    counter++; 
} 
} 

我加入每个.bmp文件到我的名字排列。

使用NeHe's Tutorial我将纹理添加到我的Qubes中,工作得很好。

现在我定制的代码如下所示:

int n; string imageFileString[1000]; char *imageFile[1000]; 
for(n=0; n<1000; n++) 
{ 
    imageFileString[n] = name[n]; 
    imageFile[n] = new char[imageFileString[n].length()]; 
    strcpy(imageFile[n], imageFileString[n].c_str()); 

    if(TextureImage[n] = loadBMP(imageFile[n])) 
    { 
     ... // Some more Functions to set textures 
    } 
} 

一切运作良好,只是我的BMP文件的arent加载。

如果我设置int n; string imageFileString...,所以对于for(...)循环是在不改变任何东西装我的照片前添加

name[0] = "pic1.bmp"; 
name[1] = "pic2.bmp"; 
name[2] = "pic2.bmp"; 
name[3] = "pic2.bmp"; 

。我的第一个观点是,这个名字数组有没有entrys,但我创建了一个日志文件与输出

name[0] << endl << name[1] << endl << name[2] ... 

,并在我的日志文件是相同的名称

pic1.bmp 
pic2.bmp 
pic3.bmp 

所以我认为有一些其他错误将cFileName添加到我的数组中。

任何人都可以帮助我吗?我不知道如何解决这个问题,我的意思是我不知道什么是错的...

+1

而不是字符串名称[1000]你为什么不使用std :: vector的名;.并使用names.push_back(theName);. – yasouser 2011-01-07 21:30:04

回答

4
imageFile[n] = new char[imageFileString[n].length()]; 

你不占空终止符。添加一个长度:

imageFile[n] = new char[imageFileString[n].length() + 1]; 
+0

嗯,不会改变任何东西。 – ahmet2106 2011-01-07 21:29:23

1

既然你说当你做name[0] = "pic1.bmp"等一切工作正常,你需要打印出/调试string theName = wf.cFileName;我猜想这是一个路径问题。 wf.cFileName可能会返回一个您不期待的文件路径。

比如我敢打赌,它返回像\MyData\Bitmaps\pic1.bmp下,如果只希望pic1.bmp

更新

考虑所有其他奇妙的变化,还可以缩短甚至更进一步,做到这一点

int counter = 0; 

while (FindNextFile(fHandle, &wf)) 
{ 
    if (strstr(wf.cFileName, ".bmp") != 0) 
    { 
      if(TextureImage[counter] = loadBMP(wf.cFileName) 
      { 
      ... // Some more Functions to set textures 
      counter++ 
      } 
    } 
} 

没有任何理由分配更多的内存来检查是否存在字符串(“.bmp”)。另请注意,除非加载成功,否则我不更新计数器。

你真的应该将TextureImage切换到std::vector那么你不必做任何计数。如果事情有效,请检查是否将wf.cFileName直接传递给您的loadBMP。而且我意识到这可能会出现溢出,因为TextureImage[]和计数器,这就是为什么我建议切换到std::vector。我们没有看到他如何分配TextureImage[],如果它与其他所有其他数字一样都是1000的幻数。

还要记住.cFileName定义为可以保存unicode值的TCHAR []。

+0

不,如我所说,如果即时通讯创建名称为[0]的日志文件,我的日志文件中有pic1.bmp。所以我不认为路径有问题。 – ahmet2106 2011-01-07 21:31:04

3

这不是一个答案,但要发表评论太难了。

你为什么要这么做?

int n; string imageFileString[1000]; char *imageFile[1000]; 
for(n=0; n<1000; n++) 
{ 
    imageFileString[n] = name[n]; 
    imageFile[n] = new char[imageFileString[n].length()]; 
    strcpy(imageFile[n], imageFileString[n].c_str()); 

    if(TextureImage[n] = loadBMP(imageFile[n])) 
    { 
     ... // Some more Functions to set textures 
    } 
} 

当你可以这样做吗?

int n; 
for(n=0; n<1000; n++) 
{ 
    if(TextureImage[n] = loadBMP(name[n].c_str())) 
    { 
     ... // Some more Functions to set textures 
    } 
} 
+0

在char *中转换const char *是不可能的,那就是错误。 – ahmet2106 2011-01-07 21:42:59

+0

@ ahmet2106然后修复loadBMP。它应该接受一个const char *而不是char * – 2011-01-07 21:45:15

0

好吧,我发现自己的问题,

更新,修正版(WinMain函数):

void ScanTheDirectory() 
{ 
    // this function is scanning the directory and is adding 
    // each bmp file to the string array "name" 
} 

int initGL() 
{ 
    // this function calls the loadTextures() function 
} 

int loadTextures() 
{ 
    // this function is loading all files of the string array "name" 
    // converts them to a const char * and is adding them to the "textures" GLuint (array) 
} 

int WINAPI WinMain() 
{ 
    // this function is the main window which is showing the 
    // qubes (GL_QUBES) 

    ScanTheDirectory(); 
    initGL(); 
} 

的问题是在WinMain函数(),因为它是这样的:

initGL(); 
ScanTheDirectory(); 

如果它是第一次调用initGL(),所以它正在创建纹理,并且因为名称数组是空的,所以没有Textu res添加到我的Textures数组中。

这个不断变化的,以

ScanTheDirectory(); 
initGL(); 

现在,它首先调用ScanTheDirectory()无效,所以我的名字数组充满了BMP图像文件的名称后。 现在它可以调用initGL,并且这是从我的图像创建纹理。

感谢您的帮助,现在我的代码正在一点点清晰的:d

艾哈迈德