2012-08-05 133 views
6

我一直試圖讓代碼塊(minGW)在win32上下文中使用Glew和opengl 3.2。我發現一個很好的小教程here無法將'LPCWSTR {aka const wchar_t *}'轉換爲'LPCSTR {aka const char *}

正如我一直在努力解決,如果在代碼塊中編譯glew實際上是可能的,我想在做教程之前嘗試源出來看看它是否會工作。

稍微調整了代碼後,我嘗試編譯並得到了幾個我從未見過的錯誤。他們是如下

|In function 'bool createWindow(LPCWSTR, int, int)':| 
|73|error: cannot convert 'LPCWSTR {aka const wchar_t*}' to 'LPCSTR {aka const char*}' in assignment| 
|80|error: cannot convert 'LPCWSTR {aka const wchar_t*}' to 'LPCSTR {aka const char*}' for argument '2' to 'HWND__* CreateWindowExA(DWORD, LPCSTR, LPCSTR, DWORD, int, int, int, int, HWND, HMENU, HINSTANCE, LPVOID)'| 
|In function 'int WinMain(HINSTANCE, HINSTANCE, LPSTR, int)':| 
|105|warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]| 
|110|error: '_TRUNCATE' was not declared in this scope| 
|110|error: 'mbstowcs_s' was not declared in this scope| 

我的代碼是

include <iostream> 
#include <Windows.h> 

#ifndef GLEW_STATIC 
#define GLEW_STATIC 
#endif //GLEW_STATIC 



#include <GL/glew.h> 
#include <GL/wglew.h> 

//using namespace std; 
// 
//int main() 
//{ 
// cout << "Hello world!" << endl; 
// return 0; 
//} 


#include "opengl_3.h" 

OpenGLContext openglContext; // Our OpenGL Context class 

bool running = true; // Whether or not the application is currently running 

HINSTANCE hInstance; // The HINSTANCE of this application 
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Standard window callback 

/** 
    WndProc is a standard method used in Win32 programming for handling Window messages. Here we 
    handle our window resizing and tell our OpenGLContext the new window size. 
*/ 
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { 
    switch (message) { 
     case WM_SIZE: // If our window is resizing 
     { 
      openglContext.reshapeWindow(LOWORD(lParam), HIWORD(lParam)); // Send the new window size to our OpenGLContext 
      break; 
     } 

     case WM_DESTROY: 
     { 
      PostQuitMessage(0); 
      break; 
     } 
    } 

    return DefWindowProc(hWnd, message, wParam, lParam); 
} 

/** 
    createWindow is going to create our window using Windows API calls. It is then going to 
    create our OpenGL context on the window and then show our window, making it visible. 
*/ 
bool createWindow(LPCWSTR title, int width, int height) { 
    WNDCLASS windowClass; 
    HWND hWnd; 
    DWORD dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; 

    hInstance = GetModuleHandle(NULL); 

    windowClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; 
    windowClass.lpfnWndProc = (WNDPROC) WndProc; 
    windowClass.cbClsExtra = 0; 
    windowClass.cbWndExtra = 0; 
    windowClass.hInstance = hInstance; 
    windowClass.hIcon = LoadIcon(NULL, IDI_WINLOGO); 
    windowClass.hCursor = LoadCursor(NULL, IDC_ARROW); 
    windowClass.hbrBackground = NULL; 
    windowClass.lpszMenuName = NULL; 
    windowClass.lpszClassName = title; 

    if (!RegisterClass(&windowClass)) { 
     return false; 
    } 

    hWnd = CreateWindowEx(dwExStyle, title, title, WS_OVERLAPPEDWINDOW, 
     CW_USEDEFAULT, 0, width, height, NULL, NULL, hInstance, NULL); 

    openglContext.create30Context(hWnd); // Create our OpenGL context on the given window we just created 

    ShowWindow(hWnd, SW_SHOW); 
    UpdateWindow(hWnd); 

    return true; 
} 

/** 
    WinMain is the main entry point for Windows based applications as opposed to 'main' for console 
    applications. Here we will make the calls to create our window, setup our scene and then 
    perform our 'infinite' loop which processes messages and renders. 
*/ 
int WINAPI WinMain(HINSTANCE hInstance, 
        HINSTANCE hPrevInstance, 
        LPSTR lpCmdLine, 
        int  nCmdShow) { 
    MSG msg; 

    /** 
     The following 6 lines of code do conversion between char arrays and LPCWSTR variables 
     which are used in the Windows API. 
    */ 
    char *orig = "OpenGL 3 Project"; // Our windows title 
    size_t origsize = strlen(orig) + 1; 
    const size_t newsize = 100; 
    size_t convertedChars = 0; 
    wchar_t wcstring[newsize]; 
    mbstowcs_s(&convertedChars, wcstring, origsize, orig, _TRUNCATE); 

    createWindow(wcstring, 500, 500); // Create our OpenGL window 

    openglContext.setupScene(); // Setup our OpenGL scene 

    /** 
     This is our main loop, it continues for as long as running is true 
    */ 
    while (running) 
    { 
     if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { // If we have a message to process, process it 
      if (msg.message == WM_QUIT) { 
       running = false; // Set running to false if we have a message to quit 
      } 
      else { 
       TranslateMessage(&msg); 
       DispatchMessage(&msg); 
      } 
     } 
     else { // If we don't have a message to process 
      openglContext.renderScene(); // Render our scene (which also handles swapping of buffers) 
     } 
    } 

    return (int) msg.wParam; 
} 

(抱歉文字的牆) 也有其他的文件,但所有的錯誤似乎從這裏變成。 如果需要的話,發佈其餘的部分。

我還沒有真正看到類似這樣的任何錯誤,所以我做了一些Google搜索,發現錯誤是由於編譯器未設置爲多字節(VS2010中的設置)而導致的。我做了一些環顧四周,找不到代碼塊中的任何設置。這個代碼只能在VS中使用,或者我錯過了什麼?即時通訊擔心這可能與我的鏈接有關,因爲我過去有很多這方面的問題。任何幫助,將不勝感激。

回答

8

CreateWindowEx更改爲CreateWindowExW或在包含任何標頭之前定義宏UNICODE

+0

嘿謝謝它的幫助,它似乎已經完成了這個訣竅。即時獲取另一個錯誤,雖然「mbstowcs_s(&convertedChars,wcstring,origsize,orig,_TRUNCATE);」這是否是對Windows函數的調用?它說它是未申報的。 – 2012-08-05 05:27:53

+1

@IPhantasmI:據我所知,'mbstowcs_s'是VC++特有的。使用MinGW,只需使用['std :: mbstowcs'](http://en.cppreference.com/w/cpp/string/multibyte/mbstowcs)。 – ildjarn 2012-08-05 05:29:43

+0

輸入變量是否需要更改。即時通訊猜測,所以即時通訊只是取代很多錯誤,從簡單的函數調用取代你的建議。我對windows api沒有多少經驗,所以我不太確定他是如何完成變量轉換的。 – 2012-08-05 05:40:42

2

我從來沒有用過minGW,所以把它與一大片鹽。 (VS Express可免費使用,順便說一句)。

Unicode/Ascii的決定主要由UNICODE定義來控制。因此,如果您定義了UNICODE 1,或者可能將它傳遞給編譯命令行,那麼很可能會解決您的問題。

+0

ty的幫助似乎是問題。 – 2012-08-05 05:28:36

相關問題