2012-08-05 38 views
6

Tôi đã cố gắng để có được Glew và opengl 3.2 làm việc với các khối mã (minGW) trong một bối cảnh win32. tôi đã tìm thấy một hướng dẫn nhỏ tuyệt vời herekhông thể chuyển đổi 'LPCWSTR {aka const wchar_t *}' thành 'LPCSTR {aka const char *}

Vì tôi đã cố gắng giải quyết nếu biên dịch glew trong codeblocks thực sự có thể tôi muốn thử nguồn trước khi thực hiện hướng dẫn để xem nó có hoạt động hay không.

sau khi chỉnh sửa mã một chút, tôi đã cố gắng biên dịch và nhận một số lỗi mà tôi chưa từng thấy trước đây. họ là như sau

|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| 

Mã của tôi là

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; 
} 

(Xin lỗi về sự bức tường của văn bản) cũng có những file khác nhưng tất cả các lỗi này dường như trở thành từ đây. đánh dấu phần còn lại nếu cần.

Tôi chưa thực sự thấy bất kỳ lỗi nào tương tự như vậy nên tôi đã làm một số googling và phát hiện ra rằng lỗi đã được gây ra bởi trình biên dịch không được thiết lập để đa byte (một thiết lập trong VS2010). tôi đã làm một số nhìn xung quanh và couldnt tìm thấy bất kỳ cài đặt như vậy trong codeblocks. là mã này chỉ có thể sử dụng trong VS hoặc tôi đã bỏ lỡ một cái gì đó? im lo lắng nó có thể là một cái gì đó để làm với liên kết của tôi do tôi có rất nhiều vấn đề với điều này trong quá khứ. Bất kỳ trợ giúp sẽ được đánh giá cao.

Trả lời

8

Thay đổi CreateWindowEx thành CreateWindowExW hoặc xác định macro UNICODE trước khi bao gồm bất kỳ tiêu đề nào.

+0

Xin cảm ơn sự giúp đỡ của nó dường như đã thực hiện các trick. im nhận được một lỗi khác mặc dù "mbstowcs_s (& conversionChars, wcstring, origsize, orig, _TRUNCATE);" đây có phải là một cuộc gọi đến một chức năng cửa sổ? nó nói rằng nó là không khai báo. –

+1

@IPhantasmI: Theo như tôi biết, 'mbstowcs_s' là VC++ - cụ thể. Với MinGW, chỉ cần sử dụng ['std :: mbstowcs'] (http://en.cppreference.com/w/cpp/string/multibyte/mbstowcs). – ildjarn

+0

Các biến đầu vào có cần thay đổi không. im đoán như vậy là im nhận được rất nhiều lỗi từ chỉ cần thay thế các cuộc gọi chức năng cho những gì bạn đề nghị. Tôi không có nhiều kinh nghiệm với các cửa sổ api vì vậy im không thực sự chắc chắn chính xác làm thế nào anh ta đang hoàn thành việc chuyển đổi biến. –

2

Tôi chưa bao giờ sử dụng minGW, vì vậy hãy sử dụng loại hạt này với một lượng muối khổng lồ. (VS Express là miễn phí để sử dụng, BTW.)

Quyết định Unicode/Ascii phần lớn được kiểm soát bởi UNICODE xác định. Vì vậy, nếu bạn #define UNICODE 1, hoặc có thể vượt qua điều đó trong dòng lệnh biên dịch của bạn, có một cơ hội tốt để giải quyết vấn đề của bạn.

+0

ty trợ giúp có vẻ là vấn đề. –

Các vấn đề liên quan