2011-01-02 5 views
2

Allegro 또는 SDL이 Windows 용 창을 만드는 방법과 직접 만드는 방법은 무엇입니까? C++ Allegro 또는 SDL은 어떻게 창을 만들 수 있습니까?

나는 게임 WINAPI 래퍼를 쓰기 위해 노력하고있어,하지만 난 기본 WINAPI 템플릿을보고 그들은 CreateWindowEx을 사용하여이

init(); 
while() 
{ 
    update(); 
} 
exit(); 
+0

불쾌한 처참한 플랫폼 세부 정보. 너는 알고 싶지 않다. (힌트 : 둘다 오픈 소스 다. 왜 물어 보지 않고 그것을 보지 않겠습니까?) – delnan

+0

나는 놀았지만, 나에게 마술처럼 보인다. 특히 C가 아니기 때문에. C++. – Neomex

답변

5

처럼 뭔가를 포장 할 때 나는 completly, 잃었어요. 창을 생성하는 정말 간단 WinAPI를 응용 프로그램이 조금 다음과 같습니다

#include <Windows.h> 
// If you're using MSVC, this is the easiest HINSTANCE. Other compilers 
// get it from WinMain and pass in to constructor. 
extern "C" IMAGE_DOS_HEADER __ImageBase; 
HINSTANCE hInstance = (HINSTANCE)&__ImageBase; 
class Window { 
    HWND hWnd; 
    static LRESULT __stdcall WindowProc(
     HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { 
     if (Window* ptr = reinterpret_cast<Window*>(GetWindowLongPtr(hWnd, GWLP_USERDATA))) 
      return ptr->DoMessage(hWnd, message, wParam, lParam); 
     else 
      return DefWindowProc(hWnd, message, wParam, lParam);  
    } 
    LRESULT DoMessage(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { 
     switch(msg) { 
     case WM_DESTROY: 
      PostQuitMessage(0); 
      return 0; 
     } 
     return DefWindowProc(hWnd, msg, wParam, lParam); 
    } 
public: 
    bool DoMessages() { 
     MSG msg; 
     while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { 
      // Translate the message and dispatch it to WindowProc() 
      TranslateMessage(&msg); 
      DispatchMessage(&msg); 
     } 
     if (msg.message == WM_QUIT) { 
      return false; 
     } 
     return true; 
    } 
    Window() { 
     WNDCLASSEX wc; 
     // clear out the window class for use 
     ZeroMemory(&wc, sizeof(WNDCLASSEX)); 
     // fill in the struct with the needed information 
     wc.cbSize = sizeof(WNDCLASSEX); 
     wc.style = CS_HREDRAW | CS_VREDRAW; 
     wc.lpfnWndProc = WindowProc; 
     wc.hInstance = hInstance; 
     wc.hCursor = LoadCursor(NULL, IDC_ARROW); 
     wc.hbrBackground = (HBRUSH)COLOR_WINDOW; 
     wc.lpszClassName = L"WindowClass1"; 

     RegisterClassEx(&wc); 
     // create the window and use the result as the handle 
     hWnd = CreateWindowEx(NULL, 
      L"WindowClass1", // name of the window class 
      L"Wide::Development", // title of the window 
      WS_OVERLAPPEDWINDOW, // window style. Always windowed for now. 
      0, // x-position of the window 
      0, // y-position of the window 
      1, // width of the window 
      1, // height of the window 
      NULL, // we have no parent window, NULL 
      NULL, // we aren't using menus, NULL 
      hInstance, // application handle 
      NULL); 

     ShowWindow(hWnd, SW_MAXIMIZE); // Snap our window to the user's desktop res, minus taskbar etc. 
     SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this)); 
     SetWindowPos(hWnd, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED); // Make sure that our WindowLongPtr is updated. 
    } 
}; 
int main() { 
    Window window; 
    while(window.DoMessages()) { 
     // Do app updates, or sleep() if you're mostly waiting on user input 
     Sleep(50); 
    } 
    // When DoMessages() returns false, the window was destroyed, so return. 
} 

당신은이 함수가하는 일에 대한 자세한 내용은 MSDN 문서를 찾아 볼 수 있습니다. 본질적으로 모든 작업은 매우 간단한 최대화 된 전체 화면이 아닌 창을 생성하고 입력을 위해 등록하며 창을 없애면 애플리케이션을 종료합니다. 당신은 실제로 Window 객체에 입력을 전달했음을 알게 될 것입니다. 그래서 모든 프레임 워크의 가장 기본적인 것은 객체 지향적이며 원하는 경우 여기에서 상속을 사용할 수 있습니다. WindowLongPtr 함수가 void * 유형 안전하지 않습니다.

또한 #include <Windows.h> 인 경우 MSVC와 같은 일부 컴파일러에서는 main()이 아닌 WinMain 진입 점을 사용해야한다고 언급 할 가치가 있습니다.

게임 렌더링 및 업데이트 코드는 일반적으로 WinAPI보다 복잡하고 어려우므로 DirectX9.0c 또는 DirectX10에 대한 책을 가져옵니다.

+2

"정말 간단한 WinAPI App ..."와우. SDL, Allegro 등 감사합니다. – delnan

+0

@delnan : 알겠습니다. 그 일은 백 줄보다 적어야하고, 상속을 망치기 시작한다면 대다수가 재사용 될 것입니다. – Puppy

+0

@delnan 이것은 Windows 프로그래밍의 매력입니다. @DeadMG 멋진, 내 모든 문제를 해결하고있어! 큰 감사! :-) – Neomex

관련 문제