C++ Graphikprogramm mit chat Kreuzen



  • Hallo,

    Ich versuche mein Graphikprogramm mit einem Chat clientprogramm zu kreuzen , habe aber keine ahnung wie das geht. Ich hoffe mir kann jemand helfen

    Code des Graphikprogramms:

    #pragma comment (lib, "wsock32.lib")
    
    #include "Client.h"
    #include "stdio.h"
    #include <conio.h>
    
    #include <iostream>
    #include <winsock.h>
    #include <windows.h>
    #include <string>
    
    #include "resource.h" 
    #include <windows.h>
    #include <math.h>
    #include <d3d9.h>
    #include <d3dx9.h> // DirectX Extensions Utility library
    
    #define DIRECTINPUT_VERSION 0x0800
    #include <dinput.h> // DirectX/Direct Input
    
    #define PI 3.141592654f
    
    typedef struct
    {
    
       float x, y, z;
    } point_t;
    
    typedef struct
    {
    	float neigen;
       float Rotation;   
       point_t Location; 
    } camera_t;
    
    LPDIRECT3D9          g_pDirect3D = NULL;
    LPDIRECT3DDEVICE9    g_pDirect3D_Device = NULL;
    LPDIRECTINPUT8       g_pDI = NULL; // The DirectInput object
    LPDIRECTINPUTDEVICE8 g_pKeyboard = NULL; // The keyboard device 
    BYTE                 dx_keyboard_state[256]; // DirectInput keyboard state buffer 
    camera_t             g_Camera;
    
    LRESULT WINAPI WndProc(HWND hwnd,  UINT msg, WPARAM wParam, LPARAM lParam);
    void CalcMatrices(void);
    void RenderFrame(void);
    
    HRESULT CreateKeyboardDirectInputDevice(HWND hWnd);
    void HandleKeys(void);
    HRESULT ReadImmediateData(HWND hWnd);
    VOID FreeDirectInput(void);
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR lpCmd, int nShow)
    {
       MSG msg;
       WNDCLASSEX wc = {sizeof(WNDCLASSEX), CS_VREDRAW|CS_HREDRAW|CS_OWNDC, WndProc, 0, 0, hInstance, 
                        NULL, NULL, NULL, NULL, "DX9_TUTORIAL3_CLASS", NULL};
       RegisterClassEx(&wc);
       HWND hMainWnd = CreateWindow("DX9_TUTORIAL3_CLASS", "DirectX 9 Tutorial 3 - Simple 3D Room", WS_OVERLAPPEDWINDOW,
                                    0, 0, 1024, 768, NULL, NULL, hInstance, NULL);
    
       g_pDirect3D = Direct3DCreate9(D3D_SDK_VERSION);
       D3DPRESENT_PARAMETERS PresentParams;
       memset(&PresentParams, 0, sizeof(D3DPRESENT_PARAMETERS));
       PresentParams.Windowed = TRUE;
       PresentParams.SwapEffect = D3DSWAPEFFECT_DISCARD;
       PresentParams.EnableAutoDepthStencil = TRUE; // turn on z-buffering
       PresentParams.AutoDepthStencilFormat = D3DFMT_D16;
    
       g_pDirect3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hMainWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &PresentParams, &g_pDirect3D_Device);
          D3DXMATRIXA16 ProjectionMatrix;
       D3DXMatrixPerspectiveFovLH(&ProjectionMatrix, PI/4, 1.0f, 1.0f, 5000.0f);
       g_pDirect3D_Device->SetTransform(D3DTS_PROJECTION, &ProjectionMatrix);
        D3DXMATRIXA16 WorldTransformMatrix;
       D3DXMatrixIdentity(&WorldTransformMatrix);
       g_pDirect3D_Device->SetTransform(D3DTS_WORLD, &WorldTransformMatrix);
         g_pDirect3D_Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);    g_pDirect3D_Device->SetRenderState(D3DRS_LIGHTING, FALSE);
       ShowWindow(hMainWnd, nShow);
       UpdateWindow(hMainWnd);
       while(PeekMessage(&msg, NULL, 0, 0, TRUE))
       {
          DispatchMessage(&msg);
          ReadImmediateData(hMainWnd); // get keyboard state
          HandleKeys();
          RenderFrame();
          InvalidateRect(hMainWnd, NULL, FALSE);
       }
       g_pDirect3D_Device->Release();
       g_pDirect3D->Release();
       FreeDirectInput();
    
       return(0);
    }
    LRESULT WINAPI WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
       HRESULT hr = S_OK;
    
       switch(msg)
       {
          case WM_CREATE:
             hr = CreateKeyboardDirectInputDevice(hwnd);
    
             if(FAILED(hr))
             {
                ::MessageBox(hwnd, "Could not create DirectInput device for keyboard.", "Error", MB_OK);
                PostQuitMessage(0);
                break;
             }
             g_Camera.Location.x = -64;
             g_Camera.Location.y = 40;
             g_Camera.Location.z = -550;
             g_Camera.Rotation = PI/2.0f;
    
             return(0);
          case WM_DESTROY:
             PostQuitMessage(0);
             return(0);
          case WM_ACTIVATE:
             if(WA_INACTIVE != wParam && g_pKeyboard)
             {
                hr = g_pKeyboard->Acquire();
    
                #ifdef _DEBUG
                if(!SUCCEEDED(hr))
                   MessageBox(hwnd, "Acquire() for DirectInput keyboard failed.", "Error", MB_OK);
                #endif
             }
             return(0);
          case WM_PAINT:
             RenderFrame();
             ValidateRect(hwnd, NULL);
    
             return(0);
       }
       return(DefWindowProc(hwnd, msg, wParam, lParam));
    }
    void RenderFrame(void)
    {
    	float höhe_mauer = 190;
    	float x_wall_front1 = -325;
    	float z_wall_right1 = -400;
       // give DirectX the vertex information of the scene
       #pragma region Setup Vertices
       #define NUM_VERTICES 220
    
       #define FRONT_WALL_COLOR 0xa9a9a9;
    
        struct D3DVERTEX {float x, y, z; DWORD color;} vertices[NUM_VERTICES];
    
       vertices[0].x = x_wall_front1;
       vertices[0].y = 0; 
       vertices[0].z = 0; 
       vertices[0].color = FRONT_WALL_COLOR;
    
       vertices[1].x = 0;
       vertices[1].y = 0;
       vertices[1].z = 0; 
       vertices[1].color = FRONT_WALL_COLOR;
    
       vertices[2].x = 0;
       vertices[2].y = höhe_mauer;
       vertices[2].z = 0; 
       vertices[2].color = FRONT_WALL_COLOR;
    
       vertices[3].x = x_wall_front1;
       vertices[3].y = 0;
       vertices[3].z = 0; 
       vertices[3].color = FRONT_WALL_COLOR;
    
       vertices[4].x = 0;
       vertices[4].y = höhe_mauer;
       vertices[4].z = 0; 
       vertices[4].color = FRONT_WALL_COLOR;
    
       vertices[5].x = x_wall_front1;
       vertices[5].y = höhe_mauer;
       vertices[5].z = 0; 
       vertices[5].color = FRONT_WALL_COLOR;
       LPDIRECT3DVERTEXBUFFER9 pVertexObject = NULL;
       void *pVertexBuffer = NULL;
    
       if(FAILED(g_pDirect3D_Device->CreateVertexBuffer(NUM_VERTICES*sizeof(D3DVERTEX), 0, 
                 D3DFVF_XYZ|D3DFVF_DIFFUSE, D3DPOOL_DEFAULT, &pVertexObject, NULL)))
          return;
    
       if(FAILED(pVertexObject->Lock(0, NUM_VERTICES*sizeof(D3DVERTEX), &pVertexBuffer, 0)))
          return;
       memcpy(pVertexBuffer, vertices, NUM_VERTICES*sizeof(D3DVERTEX));
       pVertexObject->Unlock();
       #pragma endregion 
    
         #pragma region Render Scene
       g_pDirect3D_Device->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0); // clear frame
       if(SUCCEEDED(g_pDirect3D_Device->BeginScene()))
       {
          CalcMatrices();
          g_pDirect3D_Device->SetStreamSource(0, pVertexObject, 0, sizeof(D3DVERTEX));
          g_pDirect3D_Device->SetFVF(D3DFVF_XYZ|D3DFVF_DIFFUSE);
          g_pDirect3D_Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, NUM_VERTICES/3);
          g_pDirect3D_Device->EndScene();
       }
    
       g_pDirect3D_Device->Present(NULL, NULL, NULL, NULL);
       pVertexObject->Release();
       #pragma endregion
    }
    
    void CalcMatrices(void)
    {
       D3DXMATRIXA16 ViewMatrix;
       D3DXVECTOR3 EyePoint(g_Camera.Location.x, g_Camera.Location.y, g_Camera.Location.z);
       D3DXVECTOR3 LookAt(g_Camera.Location.x+cos(g_Camera.Rotation), g_Camera.Location.y+tan(g_Camera.neigen), g_Camera.Location.z+sin(g_Camera.Rotation));
       D3DXVECTOR3 UpVector(0.0f, 1.0f, 0.0f);
       D3DXMatrixLookAtLH(&ViewMatrix, &EyePoint, &LookAt, &UpVector);
       g_pDirect3D_Device->SetTransform(D3DTS_VIEW, &ViewMatrix); 
    }
    HRESULT CreateKeyboardDirectInputDevice(HWND hWnd)
    {
        HRESULT hr;
        BOOL    bExclusive;
        BOOL    bForeground;
        BOOL    bImmediate;
        BOOL    bDisableWindowsKey;
        DWORD   dwCoopFlags;
    
        FreeDirectInput();
    
        bExclusive         = FALSE;
        bForeground        = TRUE;
        bImmediate         = TRUE;
        bDisableWindowsKey = FALSE;
    
        if( bExclusive )
            dwCoopFlags = DISCL_EXCLUSIVE;
        else
            dwCoopFlags = DISCL_NONEXCLUSIVE;
    
        if( bForeground )
            dwCoopFlags |= DISCL_FOREGROUND;
        else
            dwCoopFlags |= DISCL_BACKGROUND;
    
        if( bDisableWindowsKey && !bExclusive && bForeground )
            dwCoopFlags |= DISCL_NOWINKEY;
    
        if( FAILED( hr = DirectInput8Create( GetModuleHandle(NULL), DIRECTINPUT_VERSION, 
                                             IID_IDirectInput8, (VOID**)&g_pDI, NULL ) ) )
            return hr;
    
        if( FAILED( hr = g_pDI->CreateDevice( GUID_SysKeyboard, &g_pKeyboard, NULL ) ) )
            return hr;
    
        if( FAILED( hr = g_pKeyboard->SetDataFormat( &c_dfDIKeyboard ) ) )
            return hr;
    
          hr = g_pKeyboard->SetCooperativeLevel( hWnd, dwCoopFlags );
        if( hr == DIERR_UNSUPPORTED && !bForeground && bExclusive )
        {
            FreeDirectInput();
            MessageBox( hWnd, "SetCooperativeLevel() returned DIERR_UNSUPPORTED.\n"
                              "For security reasons, background exclusive keyboard\n"
                              "access is not allowed.", "Keyboard", MB_OK );
            return S_OK;
        }
    
        if( FAILED(hr) )
            return hr;
    
            hr = g_pKeyboard->Acquire(); 
    
        return S_OK;
    }
    
    HRESULT ReadImmediateData(HWND hWnd)
    {
        HRESULT hr;
    
        if( NULL == g_pKeyboard ) 
            return S_OK;
    
        ZeroMemory( dx_keyboard_state, sizeof(dx_keyboard_state) );
        hr = g_pKeyboard->GetDeviceState( sizeof(dx_keyboard_state), dx_keyboard_state );
        if( FAILED(hr) ) 
        {
            hr = g_pKeyboard->Acquire();
            while( hr == DIERR_INPUTLOST ) 
                hr = g_pKeyboard->Acquire();
            return S_OK; 
        }
    
        return S_OK;
    }
    void HandleKeys(void)
    {
    ...
    
    }
    
    #define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p)=NULL; } }
    
    VOID FreeDirectInput(void)
    {
          if( g_pKeyboard ) 
            g_pKeyboard->Unacquire();
    
        // Release any DirectInput objects.
        SAFE_RELEASE( g_pKeyboard );
        SAFE_RELEASE( g_pDI );
    }
    

    Code des Chat-Clients:

    #include "Client.h"
    
    #include <conio.h>
    
    Client Client::m_Instance;
    bool Client::StartUp()
    {
    	WSADATA wsa;
    	int rc = WSAStartup(MAKEWORD(2, 0), &wsa);
    	if(rc == SOCKET_ERROR)
    	{
    		std::cout << "Fehler beim Starten des Servers..." << std::endl;
    		return false;
    	}
    	return true;
    }
    bool Client::CreateConnectSocket(char* argv[2])
    {
    	int rc;
    
    	s = socket(AF_INET, SOCK_STREAM, 0);
    	if(s == INVALID_SOCKET)
    	{
    		std::cout << "Socket konnte nicht erstellt werden.\n";
    		return false;
    	}
    
    	addr.sin_addr.s_addr = inet_addr("192.168.2.107");
    	addr.sin_port        = htons(111);
    	addr.sin_family      = AF_INET;
    
    	rc = connect(s, (SOCKADDR*)&addr, sizeof(SOCKADDR));
    	if(rc == SOCKET_ERROR)
    	{
    		std::cout << "Verbindung fehlgeschlagen.\n";
    		return false;
    	}
    
    	return true;
    }
    int Client::ClientMain(char* argv[2])
    {
    	if(!StartUp())
    		return 1;
    
    	if(!CreateConnectSocket(argv))
    		return 2;
    
    	int         rc = 0;
    	fd_set      fdSetRead;
    	char        buffer[1024];
    	char        buffera[1024];
    	char        c;
    	int         bufpos = 0;
    	TIMEVAL     timeout;
    	bool        mainloop = true;
    
    	std::cout << "Alles erfolgreich gestartet und verbunden.\n";
    
    	while(rc != SOCKET_ERROR && mainloop)
    	{
    		while(kbhit())
    		{
    			c = getch(); 
    			if(c==13)
    			{ 
    				if(strcmp(buffer, "exit") == 0)
    				{
    					mainloop = false;
    					break;
    				}
    				rc = send(s,buffer,bufpos, 0); 
    			    bufpos = 0; 
    			}
    			else
    				buffer[bufpos++]=c; 
    		} 
    		buffer[bufpos] = '\0'; 
    
    		FD_ZERO(&fdSetRead);
    		FD_SET(s, &fdSetRead);
    
    		timeout.tv_sec  = 0;
    		timeout.tv_usec = 0;
    
    		while((rc = select(0, &fdSetRead, 0, 0, &timeout)) > 0)
    		{
    			rc = recv(s, buffera, 1023, 0);
    
    			if(rc == 0)
    			{
    				std::cout << "Der Server hat die Verbindung unterbrochen.\n";
    				break;
    			}
    			else if(rc == SOCKET_ERROR)
    			{
    				std::cout << "Empfangen der Nachricht fehlgeschlagen: " << WSAGetLastError() << '\n';
    				break;
    			}
    
    			buffera[rc] = '\0';
    			std::cout << buffera << '\n';
    		}	
    
    		Sleep(1);
    	}
    
    	std::cout << "Client wird beendet.\n";
    
    	closesocket(s);
    	WSACleanup();
    
    	return 0;
    }
    

    Ich hoffe es kann mir jemand helfen



  • Nöö...
    Falsches Forum,
    Falsches Code-Tags,
    Falsche Fehlerbeschreibung (bzw KEINE!!)

    Viel Spass weiterhin.



  • Um Firefighter zu ergänzen:

    • Zu viel Code
    • Keine Eigeninitiative

    Lies dir bitte durch, wie man Fragen richtig stellt. Fast alle dieser Punkte hast du nämlich missachtet.


Anmelden zum Antworten