2011-01-07 101 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

艾哈邁德