Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
// Square.h // #include <d3d9.h> #pragma comment (lib, "d3d9.lib") static LPDIRECT3D9 d3d; static LPDIRECT3DDEVICE9 d3ddev; struct CUSTOMVERTEX {FLOAT X, Y, Z, RHW; DWORD COLOR;}; #define CUSTOMFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE) // Klasse erzeugen // class CSquare { public: void InitSquare(); void RenderSquare(); void ClearSquare(); private: }; #include "Square.h" LPDIRECT3DVERTEXBUFFER9 v_buffer; void CSquare::InitSquare() { // create the vertices using the CUSTOMVERTEX struct CUSTOMVERTEX vertices[] = { { 400.0f, 62.5f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), }, { 650.0f, 500.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), }, { 150.0f, 500.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), }, }; MessageBox(NULL, "Bis hier funktioniert es!", "Test", MB_OK | MB_ICONEXCLAMATION); // create a vertex buffer interface called v_buffer d3ddev->CreateVertexBuffer(3*sizeof(CUSTOMVERTEX), 0, CUSTOMFVF, D3DPOOL_DEFAULT, &v_buffer, NULL); MessageBox(NULL, "Bis hier funktionert es auch!", "Test", MB_OK | MB_ICONEXCLAMATION); VOID * pVoid; // a void pointer // lock v_buffer and load the vertices into it v_buffer->Lock(0, 0, (void**)&pVoid, 0); memcpy(pVoid, vertices, sizeof(vertices)); v_buffer->Unlock(); } void CSquare::RenderSquare() { // select which vertex format we are using d3ddev->SetFVF(CUSTOMFVF); // select the vertex buffer to display d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX)); // copy the vertex buffer to the back buffer d3ddev->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1); } void CSquare::ClearSquare() { v_buffer->Release(); } |
C-/C++-Quelltext |
|
1 2 3 4 5 6 |
d3ddev->CreateVertexBuffer(3*sizeof(CUSTOMVERTEX), 0, CUSTOMFVF, D3DPOOL_DEFAULT, &v_buffer, NULL); // Hier ist der Fehlerort |
C-/C++-Quelltext |
|
1 2 |
if( FAILED(CreateDevice...) )
MessageBox(...);
|
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
#include <windows.h> #include <windowsx.h> #include "Square.h" #define SCREEN_WIDTH 800 #define SCREEN_HEIGHT 600 // Klasse erzeugen // class CD3D { public: void initD3D(HWND hWnd); // sets up and initializes Direct3D void render_frame(void); // renders a single frame void cleanD3D(void); // closes Direct3D and releases memory void init_graphics(void); // 3D declarations private: }; #include "D3D.h" CSquare * pSquare = new CSquare(); void CD3D::initD3D(HWND hWnd) { HRESULT hResult; d3d = Direct3DCreate9(D3D_SDK_VERSION); D3DPRESENT_PARAMETERS d3dpp; ZeroMemory(&d3dpp, sizeof(d3dpp)); d3dpp.Windowed = TRUE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.hDeviceWindow = hWnd; d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; d3dpp.BackBufferWidth = SCREEN_WIDTH; d3dpp.BackBufferHeight = SCREEN_HEIGHT; // create a device class using this information and the info from the d3dpp stuct if(FAILED(hResult = d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev))) { MessageBox(NULL, "Fehler beim erzeugen des Gerätes!", "Fehler aufgetreten", MB_OK | MB_ICONEXCLAMATION); } init_graphics(); // call the function to initialize the triangle } // this is the function used to render a single frame void CD3D::render_frame(void) { d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0); d3ddev->BeginScene(); pSquare->RenderSquare(); d3ddev->EndScene(); d3ddev->Present(NULL, NULL, NULL, NULL); } // this is the function that cleans up Direct3D and COM void CD3D::cleanD3D(void) { d3d->Release(); d3ddev->Release(); pSquare->ClearSquare(); } // this is the function that puts the 3D models into video RAM void CD3D::init_graphics(void) { pSquare->InitSquare(); } |
Werbeanzeige