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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
//----------------------------------------------------------------------------- // File: CreateDevice.cpp // // Desc: This is the first tutorial for using Direct3D. In this tutorial, all // we are doing is creating a Direct3D device and using it to clear the // window. // // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- #include <d3d9.h> #pragma warning( disable : 4996 ) // disable deprecated warning #include <strsafe.h> #pragma warning( default : 4996 ) #define WIN32_LEAN_AND_MEAN #include <windows.h> #include <atltypes.h> //----------------------------------------------------------------------------- // Global variables //----------------------------------------------------------------------------- LPDIRECT3D9 g_pD3D = NULL; // Used to create the D3DDevice LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // Our rendering device bool g_isDragging = false; // signals if a dragging is in process RECT g_dragRect; // the dragging rectangle //----------------------------------------------------------------------------- // Name: InitD3D() // Desc: Initializes Direct3D //----------------------------------------------------------------------------- HRESULT InitD3D( HWND hWnd ) { // Create the D3D object, which is needed to create the D3DDevice. if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) ) return E_FAIL; // Set up the structure used to create the D3DDevice. Most parameters are // zeroed out. We set Windowed to TRUE, since we want to do D3D in a // window, and then set the SwapEffect to "discard", which is the most // efficient method of presenting the back buffer to the display. And // we request a back buffer format that matches the current desktop display // format. D3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof(d3dpp) ); d3dpp.Windowed = TRUE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; // Create the Direct3D device. Here we are using the default adapter (most // systems only have one, unless they have multiple graphics hardware cards // installed) and requesting the HAL (which is saying we want the hardware // device rather than a software one). Software vertex processing is // specified since we know it will work on all cards. On cards that support // hardware vertex processing, though, we would see a big performance gain // by specifying hardware vertex processing. if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice ) ) ) { return E_FAIL; } // Device state would normally be set here g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE ); g_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_DIFFUSE ); return S_OK; } //----------------------------------------------------------------------------- // Name: Cleanup() // Desc: Releases all previously initialized objects //----------------------------------------------------------------------------- VOID Cleanup() { if( g_pd3dDevice != NULL) g_pd3dDevice->Release(); if( g_pD3D != NULL) g_pD3D->Release(); } //----------------------------------------------------------------------------- // Name: Render() // Desc: Draws the scene //----------------------------------------------------------------------------- VOID Render() { if( NULL == g_pd3dDevice ) return; // Clear the backbuffer to a black color g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0 ); // Begin the scene if( SUCCEEDED( g_pd3dDevice->BeginScene() ) ) { // Rendering of scene objects can happen here if( g_isDragging ) { struct Vertex { float x,y,z,rhw; D3DCOLOR color; }; #pragma warning(disable: 4244) Vertex vertices[5]; vertices[0].x = g_dragRect.left; //top left vertices[0].y = g_dragRect.top; vertices[0].z = 1.0f; vertices[0].rhw = 1.0f; vertices[0].color = D3DCOLOR_XRGB(0,255,0); vertices[1].x = g_dragRect.right; //top right vertices[1].y = g_dragRect.top; vertices[1].z = 1.0f; vertices[1].rhw = 1.0f; vertices[1].color = D3DCOLOR_XRGB(0,255,0); vertices[2].x = g_dragRect.right; //right bottom vertices[2].y = g_dragRect.bottom; vertices[2].z = 1.0f; vertices[2].rhw = 1.0f; vertices[2].color = D3DCOLOR_XRGB(0,255,0); vertices[3].x = g_dragRect.left; //bottom left vertices[3].y = g_dragRect.bottom; vertices[3].z = 1.0f; vertices[3].rhw = 1.0f; vertices[3].color = D3DCOLOR_XRGB(0,255,0); vertices[4].x = g_dragRect.left; //top left again vertices[4].y = g_dragRect.top; vertices[4].z = 1.0f; vertices[4].rhw = 1.0f; vertices[4].color = D3DCOLOR_XRGB(0,255,0); #pragma warning(default: 4244) g_pd3dDevice->DrawPrimitiveUP( D3DPT_LINESTRIP, 4, (void*) vertices, sizeof( Vertex ) ); } // End the scene g_pd3dDevice->EndScene(); } // Present the backbuffer contents to the display g_pd3dDevice->Present( NULL, NULL, NULL, NULL ); } //----------------------------------------------------------------------------- // Name: MsgProc() // Desc: The window's message handler //----------------------------------------------------------------------------- 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; case WM_LBUTTONDOWN: g_isDragging = true; g_dragRect.right = g_dragRect.left = LOWORD( lParam ); g_dragRect.bottom = g_dragRect.top = HIWORD( lParam ); return 0; case WM_LBUTTONUP: if( g_isDragging ) { ReleaseCapture(); InvalidateRect( hWnd, NULL, TRUE ); } g_isDragging = false; return 0; case WM_MOUSEMOVE: if( g_isDragging ) { SetCapture( hWnd ); POINT cp; GetCursorPos( &cp ); ScreenToClient( hWnd, &cp ); RECT cr; GetClientRect( hWnd, &cr ); //Validating ranges if( cp.x < 0 ) cp.x = 0; if( cp.y < 0 ) cp.y = 0; if( cp.x > cr.right ) cp.x = cr.right - 1; if( cp.y > cr.bottom ) cp.y = cr.bottom - 1; g_dragRect.right = cp.x; g_dragRect.bottom = cp.y; Render(); ValidateRect( hWnd, NULL ); } return 0; } return DefWindowProc( hWnd, msg, wParam, lParam ); } //----------------------------------------------------------------------------- // Name: WinMain() // Desc: The application's entry point //----------------------------------------------------------------------------- INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT ) { // Register the window class WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC | CS_HREDRAW | CS_VREDRAW, MsgProc, 0L, 0L, GetModuleHandle(NULL), NULL, LoadCursor( hInst, IDC_ARROW ), NULL, NULL, "D3D Tutorial", NULL }; RegisterClassEx( &wc ); // Create the application's window HWND hWnd = CreateWindow( "D3D Tutorial", "D3D Tutorial 01: CreateDevice", WS_OVERLAPPEDWINDOW, 100, 100, 300, 300, NULL, NULL, wc.hInstance, NULL ); // Initialize Direct3D if( SUCCEEDED( InitD3D( hWnd ) ) ) { // Show the window ShowWindow( hWnd, SW_SHOWDEFAULT ); UpdateWindow( hWnd ); // Enter the message loop MSG msg; while( GetMessage( &msg, NULL, 0, 0 ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } } UnregisterClass( "D3D Tutorial", wc.hInstance ); return 0; } |
Zitat von »"grek40"«
Is auch keine echte Kritik. Ich fand es nur lustig.
Werbeanzeige