2010-10-18 58 views
0

我想呈現文字到屏幕上,但不斷得到垃圾的符號,我無法弄清楚我的方法有什麼不同。爲什麼我的char []存儲垃圾?

此代碼將在我的主程序工作:

char vectorText[80]; 
sprintf(vectorText, "vector%i = [%f, %f, %f]", 1, vector[0]->x, vector[0]->y, vector[0]->z); 
char* newText = vectorText; 
Text* finalText = NULL; 
finalText = new Text(22, 0, 400, false, newText, 0.0f, 0.0f, 1024.0f, 200.0f); 
scene.AddText(finalText); 

然而,當我把它放在一個函數裏面開始吐出垃圾。我知道這是char vectorText [],因爲當我繞過它然後我得到文本。

char vectorText[80] = "This prints garbage"; 
//sprintf(vectorText, "vector%i = [%f, %f, %f]", 1, vector[0]->x, vector[0]->y, vector[0]->z); 
//char* newText = vectorText; 
Text* finalText = NULL; 
finalText = new Text(22, 0, 400, false, vectorText, 0.0f, 0.0f, 1024.0f, 200.0f); 
scene.AddText(finalText); 

底部的工作,但顯然它不會給我我需要的東西。

//char vectorText[80]; 
//sprintf(vectorText, "vector%i = [%f, %f, %f]", 1, vector[0]->x, vector[0]->y, vector[0]->z); 
char* newText = "Text will work from here"; 
Text* finalText = NULL; 
finalText = new Text(22, 0, 400, false, newText, 0.0f, 0.0f, 1024.0f, 200.0f); 
scene.AddText(finalText); 

任何幫助將是不錯的,我完全不知所措:(

這是我整個的main.cpp,減去所有不相關的廢話。

// Trim fat from windows 
#ifndef WIN32_LEAN_AND_MEAN 
#define WIN32_LEAN_AND_MEAN 
#endif // WIN32_LEAN_AND_MEAN 

#include <Windows.h> 
#include "directXManager.h" 
#include "Timer.h" 
#include "Renderer.h" 
#include "Camera.h" 
#include "Text.h" 
#include <time.h> 

// Message Pump prototype 
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); 

void setupWindow(WNDCLASS &wc, HINSTANCE hinstance, HWND &wnd); 

void function(D3DXVECTOR3* vector[3], Renderer &scene); 

// Program entry point 
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow) 
{ 
// Timer functionality 
Timer timer; 
timer.Init(); 

/* initialize random seed: */ 
srand (time(NULL)); 


// Step One: Register a window type 
WNDCLASS wc;  

// Step Two: Create a window of that type. 
HWND wnd = NULL; 

setupWindow(wc, hinstance, wnd); 


// Initialise Direct3D 
if (!DirectXManager::Instance().InitDirect3D(wnd, WINDOW_WIDTH, WINDOW_HEIGHT, true)) { return 0; } 

// Make a local copy of our IDirect3DDevice 
IDirect3DDevice9* device = NULL; 
device = DirectXManager::Instance().GetDevice(); 

// Start Timer 
timer.Reset(); 
float timeCurrent = timer.GetTime(); 
float timePrevious = timer.GetTime(); 
float deltaTime = 0.0f; 

// create camera 
Camera camera; 

// create renderer 
Renderer scene; 

if (device) { } // end if() 


// Step Four: Create a message pump. 
MSG msg; 
::ZeroMemory(&msg, sizeof(MSG)); 
while(msg.message != WM_QUIT) 
{ 
    // Handle time for current frame 
    timeCurrent = timer.GetTime(); 
    deltaTime = timeCurrent - timePrevious; 

    // Process all windows messages, any messages that are 
    // currently available will be processed now. 
    while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) 
    { 
     TranslateMessage(&msg); 
     DispatchMessage(&msg); 
    } // end while() 

    if (msg.message != WM_QUIT) 
    { 
     // create vectors 
     D3DXVECTOR3* vector[3]; 
     vector[0] = new D3DXVECTOR3(-3.57f, 6.43f, 8.30f); 
     vector[1] = new D3DXVECTOR3(5.67f, 3.23f, -8.72f); 
     vector[2] = new D3DXVECTOR3(0.0f, 0.0f, 0.0f); 

     function(vector, scene); 

     // render scene 
     scene.Render(deltaTime, DirectXManager::Instance().GetDevice(), camera); 

    } // end if() 

    // Update time values 
    timePrevious = timeCurrent; 
} // end while() 

// Cleanup 
DirectXManager::Instance().Release(); 

return 0; 
} // end WinMain 


// Message Pump body 
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) 
{ 
    switch (msg) 
    { 
    case WM_DESTROY: 
{ 
    DirectXManager::Instance().Release(); 
    PostQuitMessage(0); 
    break; 
} 

    // Pressing of escape key - quit 
    case WM_KEYDOWN: 
    { 
     switch (wParam) 
     { 
    case VK_ESCAPE: PostQuitMessage(0); DirectXManager::Instance().Release();  break; 
    } 

    break; 
} 
    default: 
    break; 
    } 

    return DefWindowProc(hwnd, msg, wParam, lParam); 
} 


// this will setup all the values for the DX window 
void setupWindow(WNDCLASS &wc, HINSTANCE hinstance, HWND &wnd) 
{ 
wc.cbClsExtra = NULL; 
wc.cbWndExtra = NULL; 
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); 
wc.hCursor = NULL; 
wc.hIcon = LoadIcon(hinstance, "ApplicationIcon"); 
wc.hInstance = hinstance; 
wc.lpfnWndProc = WndProc; 
wc.lpszClassName = "TuteWindow"; 
wc.lpszMenuName = ""; 
wc.style = CS_CLASSDC; 


if (RegisterClass(&wc)) 
{ 
    // Step Two: Create a window of that type 
    wnd = CreateWindow("TuteWindow", WINDOW_TITLE, WS_SYSMENU, SCREEN_WIDTH/2 - (WINDOW_WIDTH/2), SCREEN_HEIGHT/2 - (WINDOW_HEIGHT/2), WINDOW_WIDTH, WINDOW_HEIGHT, NULL, NULL, hinstance, 0); 
} else { 
    //return 0; 
} // end if() 

// Step Three: Show the window. 
if (wnd) 
{ 
    UpdateWindow(wnd); 
    ShowWindow(wnd, SW_SHOW); 
} // end if() 
} // end setupWindow() 



void function(D3DXVECTOR3* vector[3], Renderer &scene) { 

char vectorText[80]; 
sprintf(vectorText, "vector%i = [%f, %f, %f]", 1, vector[0]->x, vector[0]->y, vector[0]->z); 

char* newText = vectorText; 
Text* finalText = NULL; 
finalText = new Text(22, 0, 400, false, newText, 0.0f, 0.0f, 1024.0f, 200.0f); 
scene.AddText(finalText); 

} // end function 
+0

請將代碼放在代碼塊中進行正確的格式化。 – 2010-10-18 09:39:03

+1

什麼是文字?什麼場景?請檢查文檔中是否包含您正在使用的軟件包... – Kricket 2010-10-18 09:40:34

+1

人們需要看到的代碼是Text類的實現.... – 2010-10-18 09:41:50

回答

1

你可以檢查如果sprintf在您調用新文本之前正常工作()

我認爲您可能正在吹動堆棧。堆棧爆炸可能不是由於分配,而是由於緩衝區用戶發生了什麼。

您可能需要考慮發送給Text()的緩衝區會發生什麼情況。

然後沖洗並重復。

+0

80字節不會堆棧..如果他們這樣做,你會得到分段錯誤。 – ypnos 2010-10-18 09:43:44

+0

char * vectorText =「這會打印垃圾」; Works,但與sprintf不兼容:( – 2010-10-18 09:49:39

+0

第二個問題怎麼樣? – 2010-10-18 09:51:09

6

問題是你的char []位於堆棧上,可能directx會在你離開函數後訪問它,並且它變得無效。

在你的後一種情況下,char* newText = "Text will work from here",該數組是一個常量,它存儲在堆中的程序存儲器中。