Fenster schließt sofort soll es aber nicht.Hilfe!
-
Hi ich habe mal ein bischen mit Directx programmiert und das ist hier mal was ganz einfaches.Es soll nur eine Line zeichnen.Aber wenn ich es kompiliere und ausführe beendet es sofort das soll es aber nicht.Hab schon alles ausprobiert vielen Dank für Hilfe
#include <d3d9.h> #include <Windows.h> #include <WindowsX.h> #include <d3dx9.h> #include <D3Dcompiler.h> #include <strsafe.h> #include <D3D10.h> #include <DxErr.h> #include <conio.h> #include <iostream> using namespace std; #define D3DFVF_MY_VERTEX (D§DFVF_XYZ | D3DFVF_DIIFUSE) #define SUCCEEDED(hr) (((HRESULT)(hr)) >= 0) struct Vertex { float x, y, z; float tu, tv; }; Vertex triangle[]= {-1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f}; //---------------------------------- //PRÄPROZESSOR //---------------------------------- #pragma comment(lib,"d3d9.lib") //Ohne die beiden geht Direct3DCreate9 nicht!!! #pragma comment(lib,"d3dx9.lib") // #include <d3d9.h> #include <d3dx9.h> //---------------------------------- //Globals //---------------------------------- LPDIRECT3D9 g_pD3D =NULL; LPDIRECT3DDEVICE9 g_pd3dDevice=NULL; //---------------------------------- //PROTOTYPEN //---------------------------------- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); //Zum Epfangen der Eingaben des Benutzers const char szAppName[] = "Ein eigenes Fenster"; //Fensterklassen-Name void init(void); void shutDown(void); void render(void); //---------------------------------- //WinMain //---------------------------------- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, //Sub Beginn LPSTR lpCmdLine, int nCmdShow) { WNDCLASSEX winClass; MSG uMsg; memset(&uMsg,0,sizeof(uMsg)); winClass.lpszClassName = "Final Times"; winClass.cbSize = sizeof(WNDCLASSEX); winClass.style = CS_HREDRAW | CS_VREDRAW; winClass.lpfnWndProc = WndProc; winClass.hInstance = hInstance; winClass.hIcon = LoadIcon(hInstance,(LPCSTR)IDI_APPLICATION); winClass.hIconSm = LoadIcon(NULL,(LPCSTR)IDC_ARROW); winClass.hCursor = LoadCursor(NULL,IDC_ARROW); winClass.hbrBackground =(HBRUSH)GetStockObject(BLACK_BRUSH); winClass.lpszMenuName = NULL; winClass.cbClsExtra = 0; winClass.cbWndExtra = 0; }; //---------------------------------- //Message-Handler //---------------------------------- LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam) { switch(message) { case WM_DESTROY: { PostQuitMessage(0); return 0; } } return DefWindowProc(hWnd,message, wParam,lParam); } //---------------------------------- //init() //---------------------------------- void init( void ) { g_pD3D = Direct3DCreate9(D3D_SDK_VERSION); //Variablen anlegen und Parameter füllen HRESULT initD3D(HWND hWnd); { D3DPRESENT_PARAMETERS D3Dpp; memset(&D3Dpp,0,sizeof(D3Dpp)); D3Dpp.Windowed =FALSE; D3Dpp.EnableAutoDepthStencil=TRUE; D3Dpp.AutoDepthStencilFormat=D3DFMT_D16; D3Dpp.SwapEffect =D3DSWAPEFFECT_DISCARD; D3Dpp.BackBufferWidth =1920; D3Dpp.BackBufferHeight =1080; D3Dpp.BackBufferFormat =D3DFMT_X8R8G8B8; D3Dpp.PresentationInterval =D3DPRESENT_INTERVAL_IMMEDIATE; g_pD3D = Direct3DCreate9(D3D_SDK_VERSION); if(NULL == (g_pD3D = Direct3DCreate9(D3D_SDK_VERSION))) { MessageBox(0,"Direct3DCreate9 FAILES",0,0); } } //Device erzeugen LPDIRECT3DDEVICE9 pD3DDEVICE = NULL; //Displaymode erfragen int nMode = 0; bool bDesiredAdapterModeFound = false; int nMaxAdapterModes = g_pD3D->GetAdapterModeCount(D3DADAPTER_DEFAULT,D3DFMT_X8R8G8B8); //Hardware T und L? D3DCAPS9 D3DCaps; g_pD3D->GetDeviceCaps(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,&D3DCaps); DWORD vertexProcessing; vertexProcessing = 0; if(D3DCaps.DevCaps& D3DDEVCAPS_HWTRANSFORMANDLIGHT) { vertexProcessing = D3DCREATE_HARDWARE_VERTEXPROCESSING; if(D3DCaps.DevCaps &D3DDEVCAPS_PUREDEVICE) { vertexProcessing|= D3DCREATE_PUREDEVICE; } }else{ vertexProcessing= D3DCREATE_SOFTWARE_VERTEXPROCESSING; } } VOID Render() { //HINTERGRUNF LÖSCHEN UND MIT FARBE FÜLLEN g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0 /*FARBE*/ ), 1.0f, 0); //SZENE BEGINNEN g_pd3dDevice->BeginScene(); g_pd3dDevice->EndScene(); g_pd3dDevice->Present( NULL, NULL, NULL, NULL ); }
-
Vielleicht solltest du doch erstmal die WinAPI bzw. C/C++ Grundlagen lernen..
Guck dir einfach mal deinen Einstiegspunkt an und denkt dir dann Schritt für Schritt was passiert oO
-
Dir fehlt eine Schleife in der Main. Sobald die Main zu ende ist, hört auch dein Programm auf zu laufen.
Ein wenig Pseudo-Code zur veranschaulichung:int WinMain(...) { ErzeugeFenster(); ErzeugeNötigeProgrammteile(); while(ProgrammNichtBeendet) { FrageEventNachrichtenAb(); VerarbeiteEvents(); Render(); Logik(); } }
So sieht im groben eine Programm-Schleife aus.
ZIeh dir unbedingt ein Win32 Tutorial rein oder, alternativ, kann man auch wxWidgets oder QT zur Fenstererzeugung nehmen.
-
Mir war grade langweilig:
Window.hpp:
#ifndef WINDOW_HPP #define WINDOW_HPP #include <string> namespace DXWindow{ class Window { public: Window(HINSTANCE hInstance); virtual ~Window(); void Create(int width, int height, int depth = 32); void SetCaption(const wchar_t* caption); void EnterMainLoop(); inline HWND GetHwnd() const { return m_hwnd; } inline bool HasFocus()const { return m_isActive; } private: void InitDirectX(int width, int height, int depth); LRESULT WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); static LRESULT __stdcall sWinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); void Render(); void ShutDown(); void UpdateLogic(); void OnMouse(int x, int y, int button); void OnKey(int key, bool down); void SetFocus(bool state); protected: HWND m_hwnd; HINSTANCE m_hInstance; bool m_isActive; static const std::basic_string<wchar_t> m_className; }; } // namespace #endif // WINDOW_HPP
Window.cpp:
#include <windows.h> #include "Window.hpp" namespace DXWindow { const std::basic_string<wchar_t> Window::m_className = L"MYDXWINDOW"; Window::Window(HINSTANCE hInstance) : m_hInstance(hInstance), m_isActive(true) {} Window::~Window() {} void Window::Create( int width, int height, int depth ) { WNDCLASSEX wc; wc.cbSize = sizeof(WNDCLASSEX); wc.style = CS_HREDRAW|CS_VREDRAW; wc.lpfnWndProc = Window::sWinProc;; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = m_hInstance; wc.hIcon = ::LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = ::LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)::GetStockObject(BLACK_BRUSH); wc.lpszMenuName = NULL; wc.lpszClassName = Window::m_className.c_str(); wc.hIconSm = ::LoadIcon(NULL, IDI_APPLICATION); if ( !::RegisterClassEx(&wc) ) { ::MessageBox(HWND_DESKTOP, L"RegisterClass() failed!", L"DXWindow", MB_ICONERROR|MB_OK); return; } m_hwnd = ::CreateWindowEx(WS_EX_CLIENTEDGE, Window::m_className.c_str(), L"", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, width, height, NULL, NULL, m_hInstance, NULL); if ( m_hwnd == NULL ) { ::MessageBox(HWND_DESKTOP, L"CreateWindowEx() failed!", L"DXWindow", MB_ICONERROR|MB_OK); return; } ::SetWindowLongPtr(m_hwnd, GWL_USERDATA, reinterpret_cast<long>(this)); ::ShowWindow(m_hwnd, true); ::UpdateWindow(m_hwnd); } void Window::InitDirectX( int width, int height, int depth ) { // TODO: DirectX Initalisierung } void Window::EnterMainLoop() { // Main Loop MSG msg; while ( ::GetMessage(&msg, m_hwnd, 0, 0) > 0 ) { ::TranslateMessage(&msg); ::DispatchMessage(&msg); UpdateLogic(); } } LRESULT Window::WindowProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam ) { switch(msg) { case WM_CLOSE: { ::DestroyWindow(m_hwnd); return 0; } case WM_PAINT: { PAINTSTRUCT ps; ::BeginPaint(hwnd, &ps); Render(); ::EndPaint(m_hwnd, &ps); return 0; } case WM_DESTROY: { this->ShutDown(); ::PostQuitMessage(0); return 0; } case WM_MOUSEMOVE: { int x = LOWORD(lParam); int y = HIWORD(lParam); this->OnMouse(x, y, wParam); return 0; } case WM_KEYDOWN: { this->OnKey(wParam, true); return 0; } case WM_KEYUP: { this->OnKey(wParam, false); return 0; } case WM_ACTIVATE: { if ( LOWORD(wParam) == 0 ) { this->SetFocus(false); } else { this->SetFocus(true); } return 0; } } return ::DefWindowProc(hwnd, msg, wParam, lParam); } ////////////////////////////////////////////////////////////////////////// // Static WindowProc ////////////////////////////////////////////////////////////////////////// LRESULT __stdcall Window::sWinProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam ) { Window* ptr = reinterpret_cast<Window*>(GetWindowLong(hwnd, GWL_USERDATA)); return ptr->WindowProc(hwnd, msg, wParam, lParam); } ////////////////////////////////////////////////////////////////////////// // Setzt den Text für das Fenster ////////////////////////////////////////////////////////////////////////// void Window::SetCaption(const wchar_t* caption) { ::SetWindowText(m_hwnd, caption); } void Window::Render() { // TODO: //DirectX Render Code } void Window::UpdateLogic() { // TODO: // Hier kommt die Logik rein } void Window::OnMouse( int x, int y, int button ) { // TODO: // Hier kann man die Maustasten abfragen } void Window::OnKey( int key, bool down ) { // Wenn down == true ist, dann wird die taste gerade gedrückt, // wenn nicht wurde sie wieder freigegeben. } void Window::ShutDown() { // TODO: // Aufräumarbeiten für DirectX hier durchführen } void Window::SetFocus(bool state) { m_isActive = state; } }
WinMain.cpp:
#include <windows.h> #include "window.hpp" int __stdcall WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd ) { DXWindow::Window win(hInstance); win.Create(1024,768); win.SetCaption(L"Hallo Welt!"); win.EnterMainLoop(); }