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
Quellcode |
|
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 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 |
#include <d3d9.h> #include <d3dx9.h> #include <mmsystem.h> //#include "Direct3d.h" #include "Sprite.h" #include <stdlib.h> #include <iostream.h> #include <vector> #include <tribase.h> using namespace std; //Spielschleife Funktion //************************* MSG msg; void Spielschleife(void); //************************* //Ende der Funktion //MESSAGEHANDLER Funktion //************************* LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam); //************************* //Ende //Clear Funktion //************************* void Clearr(void); //************************* //Ende //Initialisierung der Engine //************************** CSprite RC; PDIRECT3D9 nD3D=NULL; LPDIRECT3DDEVICE9 nD3D_DEVICE=NULL; //************************** //Ende //Weitere Globale Variablen //************************** D3DCOLOR m_ClearColor=0; BOOL AbfrageD3D; struct SVertex { tbVector3 vPosition; DWORD dwColor; tbVector2 vTexture; }; const int NumCube=2048; PDIRECT3DTEXTURE9 Texture=NULL; PDIRECT3DVERTEXBUFFER9 pVertexBuffer=NULL; PDIRECT3DINDEXBUFFER9 pIndexBuffer =NULL; tbVector3 CameraPosition; float CameraAngle =0.0f; float FOV=TB_DEG_TO_RAD(90.0f); int tanzahl=0; DWORD zeit1; DWORD zeit2; DWORD zeit3; LPD3DXFONT D3DFont; LPD3DXFONT D3DFont2; //************************** //Ende //CreateMainWindow Funktion //************************* HWND hWnd; HWND CreateMainWindow(HINSTANCE hInstance,int iWidth,int iHeight,int anfx,int anfy); //************************* //Ende der Funktion //Ultimative Draw-Funktionen //************************* void MALEBITTE(LPCSTR file,CSprite *Sprite,int posx,int posy); void Drawbitte(CSprite Sprib); //************************* //Ende //Move-Funktion //************************* void MoveSprite(CSprite *Sprite); //************************* //Ende //3D Render-Funktion //************************* void DRender(); //************************* //Ende //3D DREIECK initialisieren //************************* void InitScene(); //************************* //Ende //3D DREIECKE MOVEN //************************* bool Move(float fNumSecsPassed); //************************* //Ende BOOL InitDirect3dUndCo(BOOL VOLLBILD); BOOL VOLLBILDODERNICHT(BOOL trufal); void Create3DFont(int Groese); void Draw3DText(LPCSTR Text,int Groese); void wait(DWORD ms); void Error(LPCSTR ErrorText); HWND CreateMainWindow(HINSTANCE hInstance,int iWidth,int iHeight,int anfx,int anfy) { WNDCLASSEX wndClass; // WNDCLASSEX Struktur // Struktur initialisieren wndClass.cbSize = sizeof(WNDCLASSEX); // Größe angeben (nie vergessen!) wndClass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW; // Standard Stile wndClass.lpfnWndProc = WindowProc; // Die Callback Funktion angeben wndClass.cbClsExtra = 0; // Zusätzliche Angaben, wird nicht benötigt wndClass.cbWndExtra = 0; // Zusätzliche Angaben, wird nicht benötigt wndClass.hInstance = hInstance; // Anwendungsinstanz wndClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); // Weisser Pinsel wndClass.hCursor = LoadCursor(NULL, IDC_ARROW); // Normaler Cursor wndClass.lpszMenuName = NULL; // Das Fenster hat kein Menü wndClass.lpszClassName = "WindowClass"; // Der Name der Klasse wndClass.hIcon = LoadIcon(NULL, IDI_WINLOGO); // Windows Logo wndClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO); // Windows Logo RegisterClassEx(&wndClass); return CreateWindowEx(NULL, // Keine erweiterten Stile nutzen "WindowClass", // Klassenname "Hello Windows", // Fenstertitel WS_OVERLAPPEDWINDOW | // Fenster WS_VISIBLE, // Eigenschaften anfx,anfy, // Anfangsposition und iWidth, iHeight, // Größe NULL, // Handle des Elternfensters NULL, // Handle des Menüs hInstance, // Anwendungsinstanz NULL); } LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch(msg) { // wenn das Fenster geschlossen wird, eine Nachricht senden, // die das Programm beendet case WM_DESTROY: PostQuitMessage(0); return 0; Clearr(); //Direct3D.Clear(); break; case WM_KEYDOWN: switch(wParam) { case VK_ESCAPE: PostQuitMessage(0); return 0; Clearr(); //Direct3D.Clear(); }; break; break; } // Wenn wir uns nicht um die Nachricht gekümmert haben // wird sie an die Standardnachrichtenverarbeitung von Windows // geschickt return (DefWindowProc(hwnd, msg, wParam, lParam)); }; void Spielschleife(void) { if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } }; void Clearr(void) { nD3D->Release(); nD3D_DEVICE->Release(); nD3D_DEVICE->SetTexture(0, NULL); TB_SAFE_RELEASE(Texture); tbExit(); }; void MALEBITTE(LPCSTR file,CSprite *Sprite,int posx,int posy) { Sprite->Create(nD3D_DEVICE,0); Sprite->AddTexture(file,nD3D_DEVICE); Sprite->SetPosition(posx,posy); } void Drawbitte(CSprite Sprib) { nD3D_DEVICE->BeginScene(); Sprib.Draw(); nD3D_DEVICE->EndScene(); nD3D_DEVICE->Present(0,0,0,0); }; void MoveSprite(CSprite *Sprite) { if(GetAsyncKeyState(VK_UP) &0x8000) { Sprite->Move(UP); nD3D_DEVICE->Clear(0,0,D3DCLEAR_TARGET,m_ClearColor,0,0); } if(GetAsyncKeyState(VK_DOWN) & 0x8000) { Sprite->Move(DOWN); nD3D_DEVICE->Clear(0,0,D3DCLEAR_TARGET,m_ClearColor,0,0); } if(GetAsyncKeyState(VK_RIGHT) & 0x8000) { Sprite->Move(RIGHT); nD3D_DEVICE->Clear(0,0,D3DCLEAR_TARGET,m_ClearColor,0,0); } if(GetAsyncKeyState(VK_LEFT) & 0x8000) { Sprite->Move(LEFT); nD3D_DEVICE->Clear(0,0,D3DCLEAR_TARGET,m_ClearColor,0,0); } } void DRender(void) { HRESULT hResult; // Skalierungsmatrix float fAspect; // Rotationsmatrix für die x-Achse tbMatrix mCamera; // Rotationsmatrix für die y-Achse tbMatrix mProjection; // Rotationsmatrix für die z-Achse // Vereinende Weltmatrix // Den Bildpuffer und den Z-Buffer leeren if(FAILED(hResult = nD3D_DEVICE->Clear(0, NULL, D3DCLEAR_TARGET , D3DCOLOR_XRGB(0, 0, 63), 1.0f, 0))) { // Fehler beim Leeren! //Direct3D.Error("g_pD3DDevice->Clear"); } // Szene beginnen nD3D_DEVICE->BeginScene(); // Bilineare Filter mit linearem MIP-Mapping nD3D_DEVICE->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); nD3D_DEVICE->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); nD3D_DEVICE->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR); //SetWindowText(g_hWindow, "Beispielprogramm Nr. 4: Texturen (MIN: Linear, MAG: Linear, MIP: Linear)"); // Jedes einzelne Dreieck zeichnen mCamera=tbMatrixCamera(CameraPosition, CameraPosition + tbVector3(sinf(CameraAngle), 0.0f, cosf(CameraAngle)), tbVector3(0.0f,1.0f,0.0f)); nD3D_DEVICE->SetTransform(D3DTS_VIEW,(D3DMATRIX*)(&mCamera)); mProjection = tbMatrixProjection(FOV, 1024/756, 0.1f, 250.0f); nD3D_DEVICE->SetTransform(D3DTS_PROJECTION,(D3DMATRIX*)(&mProjection)); nD3D_DEVICE->SetStreamSource(0,pVertexBuffer,0,sizeof(SVertex)); nD3D_DEVICE->SetIndices(pIndexBuffer); // Alle Matrizen kombinieren und als Weltmatrix einsetzen if(FAILED(hResult = nD3D_DEVICE->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, // Dreiecksliste 0, 0, NumCube*8, 0, NumCube*12))) // Vertexgröße { // Fehler beim Zeichnen! //Direct3D.Error("g_pD3DDevice->DrawPrimitiveUP"); } // Szene beenden nD3D_DEVICE->EndScene(); // Der große Moment: den Bildpuffer sichtbar machen nD3D_DEVICE->Present(NULL, NULL, NULL, NULL); }; void InitScene() { HRESULT hResult; SVertex* pVertices; unsigned short* pusIndices; tbVector3 CubePosition; int StartVertex; int StartIndex; // Vertexformat setzen - Positions-, Farb- und Texturkoordinatenangabe nD3D_DEVICE->SetFVF(D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1); // Beleuchtung und Culling ausschalten, Dithering aktivieren nD3D_DEVICE->SetRenderState(D3DRS_LIGHTING, FALSE); nD3D_DEVICE->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); nD3D_DEVICE->SetRenderState(D3DRS_DITHERENABLE, TRUE); if(FAILED(hResult=nD3D_DEVICE->CreateVertexBuffer(NumCube*8*sizeof(SVertex), 0, D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1, D3DPOOL_MANAGED, &pVertexBuffer, NULL))) { //Direct3D.Error("VertexBuffer fehlgeschllagen"); } if(FAILED(hResult=nD3D_DEVICE->CreateIndexBuffer(NumCube*36*2, 0, D3DFMT_INDEX16, D3DPOOL_MANAGED, &pIndexBuffer, NULL))) { //Direct3D.Error("VertexBuffer fehlgeschllagen"); } pVertexBuffer->Lock(0,0,(void**)(&pVertices),D3DLOCK_NOSYSLOCK); pIndexBuffer->Lock(0,0,(void**)(&pusIndices),D3DLOCK_NOSYSLOCK); for(int iCube=0;iCube<NumCube;iCube++) { CubePosition=tbVector3Random()*tbFloatRandom(20.0f,250.0f); StartVertex=iCube*8; StartIndex =iCube*36; pVertices[StartVertex+0].vPosition=CubePosition+tbVector3(-1.0f,1.0f,-1.0f); pVertices[StartVertex+1].vPosition=CubePosition+tbVector3(-1.0f,1.0f,1.0f); pVertices[StartVertex+2].vPosition=CubePosition+tbVector3(1.0f,1.0f,1.0f); pVertices[StartVertex+3].vPosition=CubePosition+tbVector3(1.0f,1.0f,-1.0f); pVertices[StartVertex+4].vPosition=CubePosition+tbVector3(-1.0f,-1.0f,-1.0f); pVertices[StartVertex+5].vPosition=CubePosition+tbVector3(-1.0f,-1.0f,1.0f); pVertices[StartVertex+6].vPosition=CubePosition+tbVector3(1.0f,-1.0f,1.0f); pVertices[StartVertex+7].vPosition=CubePosition+tbVector3(1.0f,-1.0f,-1.0f); for(int iVertex=StartVertex;iVertex<StartVertex+8;iVertex++) { pVertices[iVertex].dwColor=tbColorRandom(1.0f)*2.0f; pVertices[iVertex].vTexture=tbVector2Random()*2.0f; } int aiIndex[36] = {0, 3, 7, 0, 7, 4, // Vorderseite 2, 1, 5, 2, 5, 6, // Hinterseite 1, 0, 4, 1, 4, 5, // Linke Seite 3, 2, 6, 3, 6, 7, // Rechte Seite 0, 1, 2, 0, 2, 3, // Oberseite 6, 5, 4, 6, 4, 7}; // Unterseite for(int iIndex=0;iIndex<36;iIndex++) { pusIndices[StartIndex+iIndex]=aiIndex[iIndex]+StartVertex; } } pVertexBuffer->Unlock(); pIndexBuffer->Unlock(); if(FAILED(hResult = D3DXCreateTextureFromFileEx(nD3D_DEVICE, // Device "Texture.bmp", // Dateiname D3DX_DEFAULT, // Breite D3DX_DEFAULT, // Höhe D3DX_DEFAULT, // MIP-Maps 0, // Verwendungszweck D3DFMT_UNKNOWN, // Format D3DPOOL_MANAGED, // Speicherklasse D3DX_FILTER_NONE, // Filter D3DX_DEFAULT, // MIP-Map-Filter 0, // Color-Key NULL, // Unwichtig NULL, // Unwichtig &Texture))) // Die Textur { // Fehler! //Direct3D.Error("D3DXCreateTextureFromFileEx"); } // Und nun die Textur einsetzen nD3D_DEVICE->SetTexture(0, Texture); }; bool Move(float fNumSecsPassed) { float time=(float)zeit3; tbVector3 CameraDirection; if(GetAsyncKeyState(VK_LEFT))CameraAngle -=TB_DEG_TO_RAD(0.2f)*time; if(GetAsyncKeyState(VK_RIGHT))CameraAngle+=TB_DEG_TO_RAD(0.2f)*time; CameraDirection=tbVector3(sinf(CameraAngle),0.0f,cosf(CameraAngle)); if(GetAsyncKeyState(VK_UP))CameraPosition +=CameraDirection*0.01f*time; if(GetAsyncKeyState(VK_DOWN))CameraPosition -=CameraDirection*0.01f*time; if(GetAsyncKeyState(VK_PRIOR)) FOV -= TB_DEG_TO_RAD(0.05f)*time; if(GetAsyncKeyState(VK_NEXT)) FOV += TB_DEG_TO_RAD(0.05f)*time; if(FOV>=TB_DEG_TO_RAD(180.0f))FOV=TB_DEG_TO_RAD(180.0f); if(FOV<=TB_DEG_TO_RAD(0.0f))FOV=TB_DEG_TO_RAD(0.0f); return true; } BOOL InitDirect3dUndCo(BOOL VOLLBILD) { nD3D=Direct3DCreate9(D3D_SDK_VERSION); D3DPRESENT_PARAMETERS P; ZeroMemory(&P,sizeof(P)); P.SwapEffect=D3DSWAPEFFECT_DISCARD; P.hDeviceWindow=hWnd; P.Windowed=VOLLBILD; P.BackBufferWidth=1024; P.BackBufferHeight=756; P.BackBufferFormat=D3DFMT_A8R8G8B8; if(FAILED(nD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &P, &nD3D_DEVICE))) { return false; } return true; }; void wait(DWORD ms) { DWORD now=timeGetTime(); while((timeGetTime()-now)<ms); }; void Error(LPCSTR ErrorText) { MessageBox(hWnd,ErrorText,"Fehler",MB_OK); }; void Draw3DText(LPCSTR Text,int Groese) { Create3DFont(Groese); D3DCOLOR TextColor = D3DCOLOR_XRGB(0xFF,0,0); D3DCOLOR ClearColor=D3DCOLOR_XRGB(0,0,0); RECT r={20,20,0,0}; nD3D_DEVICE->BeginScene(); D3DFont->DrawText(Text,-1,&r,DT_CALCRECT,TextColor); D3DFont->DrawText(Text,-1,&r,DT_CENTER,TextColor); nD3D_DEVICE->EndScene(); nD3D_DEVICE->Present(0,0,0,0); }; void Create3DFont(int Groese) { LOGFONT LogFont={ 0 }; strcpy(LogFont.lfFaceName,"Arial"); LogFont.lfHeight-=Groese; LogFont.lfUnderline=true; D3DXCreateFontIndirect(nD3D_DEVICE, &LogFont, &D3DFont); }; BOOL VOLLBILDODERNICHT(BOOL trufal) { return trufal; }; |
Werbeanzeige