2011-01-02 8 views
2

Wie erstellt Allegro oder SDL Fenster für Windows und wie mache ich es selbst?C++ Wie erstellt Allegro oder SDL Fenster?

Ich versuche WinApi Wrapper für Spiele zu schreiben, aber ich bin völlig verloren, wenn ich grundlegende WinApi Vorlage und will sehen, es wickeln, um so etwas wie diese Sie

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

Nasty gory Plattform Details. Du willst es nicht wissen. (Hinweis: Sie sind beide Open-Source, warum nicht nachschlagen anstatt zu fragen?) – delnan

+0

Ich war, aber es sieht magisch aus, vor allem weil es C nicht OO C++ ist. – Neomex

Antwort

5

CreateWindowEx verwenden. Eine wirklich einfache WinAPI-Anwendung, die ein Fenster erstellt sieht ein wenig wie folgt aus:

#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. 
} 

Sie können die MSDN-Dokumentation für weitere Informationen nachschlagen, was diese Funktionen tun. Im Wesentlichen wird lediglich ein sehr einfaches maximiertes Fenster ohne Vollbild erstellt, das für die Eingabe registriert wird. Wenn das Fenster zerstört ist, beenden Sie die Anwendung. Sie werden feststellen, dass ich die Eingabe tatsächlich an das Window-Objekt weitergeleitet habe, also ist das grundlegendste aller Frameworks objektorientiert und Sie können hier mit Vererbung spielen, wenn Sie möchten, vergessen Sie nicht, dass die WindowLongPtr-Funktionen eine void * verwenden und sind nicht typsicher.

Es ist auch erwähnenswert, dass auf einigen Compilern wie MSVC, wenn Sie #include <Windows.h>, sie erwarten, dass Sie den Einstiegspunkt WinMain, nicht Main() verwenden.

Das Spiel Rendering und Update-Code ist in der Regel Meilen komplexer und schwieriger als die WinAPI, so würde ich ein Buch auf DirectX9.0c oder DirectX10 greifen.

+2

"Eine wirklich einfache WinAPI App ..." wow. Danke, SDL, Allegro usw. – delnan

+0

@delnan: Ich verstehe es nicht. Das Ding muss weniger als hundert Zeilen umfassen, und wenn Sie anfangen würden, mit der Vererbung zu spielen, wäre die große Mehrheit davon wiederverwendbar. – Puppy

+0

@ delnan Das ist Charme der Windows-Programmierung :-) @DeadMG Wunderbar, löst es ganze meine Probleme! Vielen Dank! :-) – Neomex