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 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 |
// Dreieck.cpp // author : Simon Klausner // version : 0.1 // purpose : - Get Direct3D configs // - Create Window // - Create Triangles with textures and move #define D3D_DEBUG_INFO #include <windows.h> #include <TriBase.h> #include <I:\\Spiele Programmierung\\Beispiele\\Allgemeines\\InitWindow.h> #include <I:\\Spiele Programmierung\\Beispiele\\Allgemeines\\Direct3DEnum.h> #include <I:\\Spiele Programmierung\\Beispiele\\Allgemeines\\InitDirect3D.h> #include <I:\\Spiele Programmierung\\Beispiele\\Allgemeines\\Allgemeines.h> #include <d3dx9.h> // Globals struct SVertex // Vertex { tbVector3 vPosition; DWORD dwColor; tbVector2 vTexture; static const DWORD dwFVF; }; const DWORD SVertex::dwFVF = D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1; SDirect3DParameters g_Direct3DParameters; // Direct3DParameters PDIRECT3DTEXTURE9 g_pTexture; // Texturepointer float g_fTime = 0.0f; // Timecounter const int g_iNumTriangles = 1024; struct STriangle // Triangle { tbVector3 vPosition; tbVector3 vVelocity; tbVector3 vRotation; tbVector3 vRotVelocity; float fSize; SVertex aVertex[3]; }; STriangle g_aTriangle[g_iNumTriangles]; // function : Render tbResult Render(float fNumSecsPassed) { // Variables HRESULT hResult; tbMatrix mScaling; tbMatrix mRotationX; tbMatrix mRotationY; tbMatrix mRotationZ; tbMatrix mTranslation; tbMatrix mWorld; // Clear Picturepuffer and Z-Buffer if(FAILED(hResult = g_pD3DDevice->Clear(0,NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(25, 25, 25), 1.0f, 0))) { // Error TB_ERROR_DIRECTX("g_pD3DDEVICE->Clear", hResult, TB_STOP); } // Begin Scene g_pD3DDevice->BeginScene(); // set texture filter if((int)(g_fTime/3.0f)%3 == 0) { // bilinear filter with linear MIP-mapping g_pD3DDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); g_pD3DDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); g_pD3DDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR); SetWindowText(g_hWindow, "Möp (MIN:lin, MAG:lin, MIP:lin)"); } else if((int)(g_fTime/3.0f)%3 == 1) { // no filters and no mip-mapping g_pD3DDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT); g_pD3DDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT); g_pD3DDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_NONE); SetWindowText(g_hWindow, "Möp (MIN:point, MAG:point, MIP:none)"); } else { // max anisotropic filter without mip-mapping g_pD3DDevice->SetSamplerState(0, D3DSAMP_MAXANISOTROPY, g_Direct3DParameters.DeviceCaps.MaxAnisotropy); g_pD3DDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_ANISOTROPIC); g_pD3DDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); g_pD3DDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_NONE); SetWindowText(g_hWindow, "Möp (MIN:aniso, MAG:lin, MIP:none)"); } // Render Triangles for(int i=0; i<g_iNumTriangles; i++) { // Create Scalingmatrix mScaling = tbMatrixScaling(tbVector3(g_aTriangle[i].fSize)); // Create Translation- Rotationmatrix for Triangle mRotationX = tbMatrixRotationX(g_aTriangle[i].vRotation.x); mRotationY = tbMatrixRotationY(g_aTriangle[i].vRotation.y); mRotationZ = tbMatrixRotationZ(g_aTriangle[i].vRotation.z); mTranslation = tbMatrixTranslation(g_aTriangle[i].vPosition); // Multi Scale, Rotation and Translation for Worldmatrix mWorld = mScaling * mRotationX * mRotationY * mRotationZ * mTranslation; g_pD3DDevice->SetTransform(D3DTS_WORLD, (D3DMATRIX*)(&mWorld)); // Draw Triangle if(FAILED(hResult = g_pD3DDevice->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 1, g_aTriangle[i].aVertex, sizeof(SVertex)))) { // Error TB_ERROR_DIRECTX("g_pD3DDEVICE->DrawPrimitiveUP", hResult, TB_STOP); } // End Scene g_pD3DDevice->EndScene(); // Present Scene g_pD3DDevice->Present(NULL,NULL,NULL,NULL); } return TB_OK; } // end function : Render // function : Move tbResult Move(float fNumSecsPassed) { // Increase Timecounter g_fTime += fNumSecsPassed; // if button pressed = space stop movement if(GetAsyncKeyState(VK_SPACE)) fNumSecsPassed = 0.0f; // move triangles float fDistance; for(int i=0; i<g_iNumTriangles; i++) { g_aTriangle[i].vPosition += g_aTriangle[i].vVelocity*fNumSecsPassed; g_aTriangle[i].vRotation += g_aTriangle[i].vRotVelocity*fNumSecsPassed; // check distance fDistance = tbVector3Length(g_aTriangle[i].vPosition); // if distance > 100, move triangle back if(fDistance > 100.0f) g_aTriangle[i].vVelocity *= -1.0f; } return TB_OK; } // end function : Move // function : InitScene tbResult InitScene() { // Variables HRESULT hResult; // Set Vertex Format if(FAILED(hResult = g_pD3DDevice->SetFVF(SVertex::dwFVF))) { // Error TB_ERROR_DIRECTX("g_pD3DDevice->SetFVF", hResult, TB_ERROR); } // Turn off Lightning and Culling g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE); g_pD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); // Activate Dithering g_pD3DDevice->SetRenderState(D3DRS_DITHERENABLE, TRUE); // Set Projectionmatrix // Picture Proportion float fAspect = (float)(g_Direct3DParameters.VideoMode.Width) / (float)(g_Direct3DParameters.VideoMode.Height); // Create Projectionmatrix tbMatrix mProjection = tbMatrixProjection(TB_DEG_TO_RAD(90.0f), fAspect, 0.1f, 100.0f); // Set Matrix g_pD3DDevice->SetTransform(D3DTS_PROJECTION, (D3DMATRIX*)(&mProjection)); // Initiate Triangle for(int i=0; i< g_iNumTriangles; i++) { // All triangles start in background g_aTriangle[i].vPosition = tbVector3(0.0f, 0.0f, 50.0f); // random velocity float fSpeed = tbFloatRandom(0.1f, 5.0f); g_aTriangle[i].vVelocity = tbVector3Random() * fSpeed; // Rotation default g_aTriangle[i].vRotation = tbVector3(0.0f, 0.0f, 0.0f); // random rotation velocity at all axis g_aTriangle[i].vRotVelocity.x = tbFloatRandom(-1.0f, 1.0f); g_aTriangle[i].vRotVelocity.y = tbFloatRandom(-1.0f, 1.0f); g_aTriangle[i].vRotVelocity.z = tbFloatRandom(-1.0f, 1.0f); // random size g_aTriangle[i].fSize = tbFloatRandom(1.0f, 5.0f); // create vertex for(int j=0; j<3; j++) { // Position g_aTriangle[i].aVertex[j].vPosition = tbVector3Random(); // Color tbColor VertexColor(tbFloatRandom(0.0f, 1.0f), tbFloatRandom(0.0f, 1.0f), tbFloatRandom(0.0f, 1.0f)); g_aTriangle[i].aVertex[j].dwColor = (DWORD)(VertexColor); // Texture coordinate g_aTriangle[i].aVertex[j].vTexture.u = tbFloatRandom(-1.0f, 2.0f); g_aTriangle[i].aVertex[j].vTexture.v = tbFloatRandom(-1.0f, 2.0f); } } // Load Texture if(FAILED(hResult = D3DXCreateTextureFromFileEx(g_pD3DDevice, "Texture.bmp", D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_FILTER_NONE, D3DX_DEFAULT, 0, NULL, NULL, &g_pTexture))) { // Error TB_ERROR_DIRECTX("D3DXCreateTextureFromFileEx", hResult, TB_ERROR); } // set texture g_pD3DDevice->SetTexture(0, g_pTexture); return TB_OK; } // end function : Init Scene // function : Exit Scene tbResult ExitScene() { // deactivate texture and delete g_pD3DDevice->SetTexture(0, NULL); TB_SAFE_RELEASE(g_pTexture); // Exit D3D and window ExitDirect3D(); ExitWindow(); // Exit Engine tbExit(); return TB_OK; } // end function : Exit Scene // function : windows main int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { // Initiate TriBase Engine tbInit(); // Get Direct3D Options with Dialog tbResult Result = GetDirect3DParameters(&g_Direct3DParameters); if(Result == TB_ERROR) { // Error MessageBox(NULL, "Fehler beim Abzählen!", "Fehler", MB_OK | MB_ICONEXCLAMATION); tbExit(); return 1; } if(Result == TB_CANCELED) { // Dialog canceled tbExit(); return 0; } // Initiate window with D3D options if(InitWindow(g_Direct3DParameters.VideoMode.Width, g_Direct3DParameters.VideoMode.Height, "Legend of the Druids", LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1)))) { // Error MessageBox(NULL, "Fehler beim Erstellen des Fensters!", "Fehler", MB_OK | MB_ICONEXCLAMATION); tbExit(); return 1; } // Initiate D3D with options if(InitDirect3D(&g_Direct3DParameters,g_hWindow)) { // Error MessageBox(NULL, "Fehler beim Initialisieren von Direct3D!", "Fehler", MB_OK | MB_ICONEXCLAMATION); ExitWindow(); tbExit(); return 1; } // Initiate Scene InitScene(); // Messageloop tbDoMessageLoop(Render, Move); // Exit Program ExitScene(); return 0; } // end function : windows main |
C-/C++-Quelltext |
|
1 2 3 4 5 |
// End Scene g_pD3DDevice->EndScene(); // Present Scene g_pD3DDevice->Present(NULL,NULL,NULL,NULL); |
Werbeanzeige