[C++\WinAPI] Fenster in einer Klasse erstellen funktioniert nicht

Ninjasturm

Mitglied
Hallo tutorials.de,

ich habe mir einen Klasse erstellt die das erstellen eines Win32 Fensters übernimmt doch
irgendwie funktioniert das nicht, es gibt keine Fehler beim Kompilieren nur das Fenster wird nicht erstellt könntet ihr mal drüber schauen:

System.hpp
C++:
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// System
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include <Windows.h>
#include <StdIO.h>
#include <IOStream>
#include <D3D9.h>

namespace Heaven
{
    namespace System
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // WindowHandle                                                                                                                         //
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        struct WindowHandle
        {
            //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            // WindowClass                                                                                                                          //
            //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            WNDCLASSEX      WindowClass;
            //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            // Handle                                                                                                                               //
            //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            HWND            Handle;
        };
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // VideoMode                                                                                                                            //
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        struct VideoMode
        {
            //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            // X , Y
            //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            int X, Y;
            //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            // Width, Height                                                                                                                        //
            //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            int Width, Height;
            //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            // Set                                                                                                                       //
            // -------------------------------------------------------------------------------------------------------------------------------------//
            // X                                                                                                                                    //
            // Y                                                                                                                                    //
            // Width                                                                                                                                //
            // Height                                                                                                                               //
            //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            void Set(int iX, int iY, int iWidth, int iHeight);
        };
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // Window                                                                                                                               //
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        class Window
        {
            public:
                //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                // Window                                                                                                                               //
                // -------------------------------------------------------------------------------------------------------------------------------------//
                // Caption                                                                                                                              //
                // VideoMode                                                                                                                            //
                // Direct3D                                                                                                                             //
                //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                WindowHandle Create(const CHAR* acCaption, VideoMode vmVideoMode);
                //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                // Message                                                                                                                              //
                //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                static LRESULT WINAPI Message(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
                //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                // Release
                //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                void Release();
            protected:
                //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                // Caption                                                                                                                              //
                //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                const CHAR* m_acCaption;
                //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                // VideoMode                                                                                                                           //
                //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                VideoMode m_vmVideoMode;
                //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                // Handle
                WindowHandle m_Handle;
        };
    }
}
System.cpp
C++:
#include <System/System.hpp>

void Heaven::System::VideoMode::Set(int iX, int iY, int iWidth, int iHeight)
{
    X            = iX;
    Y            = iY;
    Width        = iWidth;
    Height       = iHeight;
}
LRESULT WINAPI Heaven::System::Window::Message (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch( msg )
    {
        case WM_DESTROY:
            PostQuitMessage( 0 );
            return 0;

        case WM_PAINT:
            ValidateRect( hWnd, NULL );
            return 0;
    }

    return DefWindowProc( hWnd, msg, wParam, lParam );
}
Heaven::System::WindowHandle Heaven::System::Window::Create(const CHAR* acCaption, VideoMode vmVideoMode)
{
    this->m_acCaption       = acCaption;
    this->m_vmVideoMode     = vmVideoMode;

    std::cout << "Heaven3D v0.1" << std::endl;

    // Fensterklasse
    this->m_Handle.WindowClass.cbSize = sizeof(WNDCLASSEX);
    this->m_Handle.WindowClass.style = CS_CLASSDC;
    this->m_Handle.WindowClass.lpfnWndProc = this->Message;
    this->m_Handle.WindowClass.cbClsExtra = 0L;
    this->m_Handle.WindowClass.cbWndExtra = sizeof(Heaven::System::Window*);
    this->m_Handle.WindowClass.hInstance = GetModuleHandle(0);
    this->m_Handle.WindowClass.hIcon = 0;
    this->m_Handle.WindowClass.hCursor = 0;
    this->m_Handle.WindowClass.hbrBackground = 0;
    this->m_Handle.WindowClass.lpszMenuName = 0;
    this->m_Handle.WindowClass.lpszClassName = this->m_acCaption;
    this->m_Handle.WindowClass.hIconSm = 0;
    RegisterClassEx( &this->m_Handle.WindowClass );

    // Fenster
    HWND hWnd = CreateWindow( this->m_acCaption , this->m_acCaption ,
                              WS_OVERLAPPEDWINDOW, this->m_vmVideoMode.X , this->m_vmVideoMode.Y  , this->m_vmVideoMode.Width, this->m_vmVideoMode.Height,
                              NULL, NULL, this->m_Handle.WindowClass.hInstance, NULL );

    std::cout << "Caption:        " << this->m_acCaption            << std::endl;
    std::cout << "X:              " << this->m_vmVideoMode.X        << std::endl;
    std::cout << "Y:              " << this->m_vmVideoMode.Y        << std::endl;
    std::cout << "Width:          " << this->m_vmVideoMode.Width    << std::endl;
    std::cout << "Height:         " << this->m_vmVideoMode.Height   << std::endl;
    return this->m_Handle;
}
void Heaven::System::Window::Release()
{
    UnregisterClass( this->m_acCaption, this->m_Handle.WindowClass.hInstance);
}
main.cpp
C++:
#include <d3d9.h>
#include <strsafe.h>
#include <Heaven3D.hpp>

LPDIRECT3D9         g_pD3D = NULL; // Used to create the D3DDevice
LPDIRECT3DDEVICE9   g_pd3dDevice = NULL; // Our rendering device

HRESULT InitD3D( HWND hWnd )
{
    if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
        return E_FAIL;

    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory( &d3dpp, sizeof( d3dpp ) );
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;

    if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
                                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                      &d3dpp, &g_pd3dDevice ) ) )
    {
        return E_FAIL;
    }

    return S_OK;
}

VOID Cleanup()
{
    if( g_pd3dDevice != NULL )
        g_pd3dDevice->Release();

    if( g_pD3D != NULL )
        g_pD3D->Release();
}

VOID Render()
{
    if( NULL == g_pd3dDevice )
        return;

    // Clear the backbuffer to a blue color
    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 0, 0, 255 ), 1.0f, 0 );

    // Begin the scene
    if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
    {
        // Rendering of scene objects can happen here

        // End the scene
        g_pd3dDevice->EndScene();
    }

    // Present the backbuffer contents to the display
    g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}

LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
    switch( msg )
    {
        case WM_DESTROY:
            Cleanup();
            PostQuitMessage( 0 );
            return 0;

        case WM_PAINT:
            Render();
            ValidateRect( hWnd, NULL );
            return 0;
    }

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

INT main( HINSTANCE hInst, HINSTANCE, LPWSTR, INT )
{

    Heaven::System::VideoMode VideoMode;
    VideoMode.Set(0, 0, 1024, 768);

    Heaven::System::Window Window;
    Heaven::System::WindowHandle Handle = Window.Create("Hello World", VideoMode);

    // Initialize Direct3D
    if( SUCCEEDED( InitD3D(Handle.Handle) ) )
    {
        // Show the window
        ShowWindow(Handle.Handle, SW_SHOWDEFAULT );
        UpdateWindow(Handle.Handle);

        // Enter the message loop
        MSG msg;
        while( GetMessage( &msg, NULL, 0, 0 ) )
        {
            TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
    }
    return 0;
}

Die Engine also Heaven3D compiliere ich in eine DLL Datei.
Es werden auch die Informationen zum VideoMode angezeigt, die Funktion findet er also.

IDE: Code::Blocks
Compiler: MinGW
DirectX: 9
 
Zuletzt bearbeitet von einem Moderator:
Hallo Ninjasturm,

verwende doch einfach einen Debugger und verfolge den Programmablauf Schritt für Schritt. Dann solltest du auch sehen, dass CreateDevice einen Fehler zurückliefert.

Grüße,
Matthias
 
Und warum funktioniert
C++:
if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
                                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                      &d3dpp, &g_pd3dDevice ) ) )
nicht. Ich kann mir das irgendwie nicht erklären warum das nicht funktioniert.
 
Zuletzt bearbeitet von einem Moderator:
Und warum funktioniert
C++:
if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
                                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                      &d3dpp, &g_pd3dDevice ) ) )
nicht. Ich kann mir das irgendwie nicht erklären warum das nicht funktioniert.
Lass dir mal vom Debugger anzeigen, welchen Wert hWnd hat. Allgemeiner Tipp zur Direct3D-9-Entwicklung: aktiviere die Debug-Version von Direct3D 9 im DirectX Control Panel. Dann erhältst du nützliche Fehlermeldungen auf der Debugkonsole. Hier bekomme ich z.B. Folgendes ausgegeben:
Code:
Direct3D9: (ERROR) :Invalid HWND specified for hwndFocusWindow, CreateDeviceEx fails

Grüße,
Matthias
 
Zuletzt bearbeitet von einem Moderator:
Zurück