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 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 |
// Kapitel 3 // Beispielprogramm 03 // =================== // Hier wird gezeigt, wie man Texte darstellt. #include <TriBase.h> #include "Resource.h" struct SVertex { tbVector3 vPosition; // Position des Vertex DWORD dwColor; tbVector2 vTexture; // Texturkoordinaten static const DWORD dwFVF; // Vertexformat (statisch) }; struct SCube { tbVector3 vPosition; tbVector2 vCoords; int y; SVertex pVertices[4]; // SVertex }; struct SCube2 { tbVector3 vPosition; SVertex pVertices[10]; // SVertex }; int Compare (const void *a, const void *b) { float fDist; fDist = ((SCube*)(a))->y - ((SCube*)(b))->y; return fDist > 0.0f ? 1 : -1; } const DWORD SVertex::dwFVF = D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1; // ****************************************************************** // Globale Variablen tbConfig g_Config; // Konfigurationsstruktur tbFont* g_pFont1 = NULL; // Erste Schriftart float g_fTime = 0.0f; // Globaler Zeitzähler const int g_iNumCubes = 8; float g_fCameraAngle = 0.0f; // Drehwinkel der Kamera tbVector3 g_vCameraPosition; // Die Kameraposition PDIRECT3DTEXTURE9 pTexture [2]; // Die Textur SCube Cube[g_iNumCubes]; SCube2 Cube2[g_iNumCubes]; SCube Ground; tbVector2 g_vCoord(1,4); // Tasten gesperrt? bool und Time bool Enable = true; float Time; tbResult InitScene () { g_vCameraPosition = tbVector3(2.5f, 2.5f, -3.0f); pTexture [0] = tbTextureManager::Instance().LoadTexture("World.jpg"); pTexture [1] = tbTextureManager::Instance().LoadTexture("grau2"); Ground.pVertices[0].vTexture = tbVector2(0, 0); Ground.pVertices[1].vTexture = tbVector2(0, 1); Ground.pVertices[2].vTexture = tbVector2(1, 0); Ground.pVertices[3].vTexture = tbVector2(1, 1); Ground.pVertices[0].vPosition = tbVector3(1.0, 4.0f, 0.25f); Ground.pVertices[1].vPosition = tbVector3(1.0, 1.0f, 0.25f); Ground.pVertices[2].vPosition = tbVector3(4.0, 4.0f, 0.25f); Ground.pVertices[3].vPosition = tbVector3(4.0, 1.0f, 0.25f); for (int x=0; x<4; x++) Ground.pVertices[x].dwColor = (DWORD)(tbColor (0.25f, 0.15f, 0.55f)); // Bild-Coords Cube[0].vCoords = tbVector2 (1,3); Cube[1].vCoords = tbVector2 (1,2); Cube[2].vCoords = tbVector2 (2,4); Cube[3].vCoords = tbVector2 (2,3); Cube[4].vCoords = tbVector2 (2,2); Cube[5].vCoords = tbVector2 (3,4); Cube[6].vCoords = tbVector2 (3,3); Cube[7].vCoords = tbVector2 (3,2); for (int iCube=0; iCube<g_iNumCubes; iCube++) { Cube[iCube].pVertices[0].vPosition = tbVector3(Cube[iCube].vCoords.x, Cube[iCube].vCoords.y, 0); Cube[iCube].pVertices[1].vPosition = tbVector3(Cube[iCube].vCoords.x, Cube[iCube].vCoords.y - 1, 0); Cube[iCube].pVertices[2].vPosition = tbVector3(Cube[iCube].vCoords.x + 1, Cube[iCube].vCoords.y, 0); Cube[iCube].pVertices[3].vPosition = tbVector3(Cube[iCube].vCoords.x + 1, Cube[iCube].vCoords.y - 1, 0); Cube[iCube].pVertices[0].vTexture = tbVector2((Cube[iCube].pVertices[0].vPosition.x -1) *0.33f, (Cube[iCube].pVertices[0].vPosition.y - 4) *-0.33f); Cube[iCube].pVertices[1].vTexture = tbVector2((Cube[iCube].pVertices[1].vPosition.x -1) *0.33f, (Cube[iCube].pVertices[1].vPosition.y - 4) *-0.33f); Cube[iCube].pVertices[2].vTexture = tbVector2((Cube[iCube].pVertices[2].vPosition.x -1) *0.33f, (Cube[iCube].pVertices[2].vPosition.y - 4) *-0.33f); Cube[iCube].pVertices[3].vTexture = tbVector2((Cube[iCube].pVertices[3].vPosition.x -1) *0.33f, (Cube[iCube].pVertices[3].vPosition.y - 4) *-0.33f); for (int x=0; x<4; x++) Cube[iCube].pVertices[x].dwColor = (DWORD)(tbColor (1.0f, 1.0f, 1.0f)); } for (int x=0; x<8; x++) Cube[x].y = rand()%1000; qsort(Cube, g_iNumCubes,sizeof(SCube), Compare); Cube[0].vCoords = tbVector2 (1,3); Cube[1].vCoords = tbVector2 (1,2); Cube[2].vCoords = tbVector2 (2,4); Cube[3].vCoords = tbVector2 (2,3); Cube[4].vCoords = tbVector2 (2,2); Cube[5].vCoords = tbVector2 (3,4); Cube[6].vCoords = tbVector2 (3,3); Cube[7].vCoords = tbVector2 (3,2); for (int x=0; x<8; x++) { Cube2[x].pVertices[0].vPosition = tbVector3(Cube[x].pVertices[0].vPosition.x, Cube[x].pVertices[0].vPosition.y, 0); Cube2[x].pVertices[1].vPosition = tbVector3(Cube[x].pVertices[0].vPosition.x, Cube[x].pVertices[0].vPosition.y, 0.25f); Cube2[x].pVertices[2].vPosition = tbVector3(Cube[x].pVertices[1].vPosition.x, Cube[x].pVertices[1].vPosition.y, 0); Cube2[x].pVertices[3].vPosition = tbVector3(Cube[x].pVertices[1].vPosition.x, Cube[x].pVertices[1].vPosition.y, 0.25f); Cube2[x].pVertices[4].vPosition = tbVector3(Cube[x].pVertices[3].vPosition.x, Cube[x].pVertices[3].vPosition.y, 0); Cube2[x].pVertices[5].vPosition = tbVector3(Cube[x].pVertices[3].vPosition.x, Cube[x].pVertices[3].vPosition.y, 0.25f); Cube2[x].pVertices[6].vPosition = tbVector3(Cube[x].pVertices[2].vPosition.x, Cube[x].pVertices[2].vPosition.y, 0); Cube2[x].pVertices[7].vPosition = tbVector3(Cube[x].pVertices[2].vPosition.x, Cube[x].pVertices[2].vPosition.y, 0.25f); Cube2[x].pVertices[8].vPosition = tbVector3(Cube[x].pVertices[0].vPosition.x, Cube[x].pVertices[0].vPosition.y, 0); Cube2[x].pVertices[9].vPosition = tbVector3(Cube[x].pVertices[0].vPosition.x, Cube[x].pVertices[0].vPosition.y, 0.25f); for (int y=0; y<10; y++) Cube2[x].pVertices[y].dwColor = (DWORD)(tbColor (0.0f, 4.3f, 0.7f)); } return TB_OK; } // ****************************************************************** // Die Move-Funktion tbResult MoveProc(float fNumSecsPassed) { // Zeitzähler erhöhen g_fTime += fNumSecsPassed; // Prüfe, ob Tasten frei sind if (Enable == true) { if(GetAsyncKeyState(VK_LEFT)) { g_vCoord += tbVector2 (1,0); if (g_vCoord.x < 4) { for (int x=0; x<g_iNumCubes; x++) { if (Cube[x].vCoords == g_vCoord) Cube[x].vCoords -= tbVector2 (1,0); } } else g_vCoord -= tbVector2 (1,0); // Tasten sperren Enable = false; Time = g_fTime; } if(GetAsyncKeyState(VK_RIGHT)) { g_vCoord -= tbVector2 (1,0); if (g_vCoord.x > 0) { for (int x=0; x<g_iNumCubes; x++) { if (Cube[x].vCoords == g_vCoord) Cube[x].vCoords += tbVector2 (1,0); } } else g_vCoord += tbVector2 (1,0); Enable = false; Time = g_fTime; } if(GetAsyncKeyState(VK_UP)) { g_vCoord -= tbVector2 (0,1); if (g_vCoord.y > 1) { for (int x=0; x<g_iNumCubes; x++) { if (Cube[x].vCoords == g_vCoord) Cube[x].vCoords += tbVector2 (0,1); } } else g_vCoord += tbVector2 (0,1); Enable = false; Time = g_fTime; } if(GetAsyncKeyState(VK_DOWN)) { g_vCoord += tbVector2 (0,1); if (g_vCoord.y < 5) { for (int x=0; x<g_iNumCubes; x++) { if (Cube[x].vCoords == g_vCoord) Cube[x].vCoords -= tbVector2 (0,1); } } else g_vCoord -= tbVector2 (0,1); Enable = false; Time = g_fTime; } } // Karten Koordinaten neu berechnen for (int iCube=0; iCube<g_iNumCubes; iCube++) { Cube[iCube].pVertices[0].vPosition = tbVector3(Cube[iCube].vCoords.x, Cube[iCube].vCoords.y, 0); Cube[iCube].pVertices[1].vPosition = tbVector3(Cube[iCube].vCoords.x, Cube[iCube].vCoords.y - 1, 0); Cube[iCube].pVertices[2].vPosition = tbVector3(Cube[iCube].vCoords.x + 1, Cube[iCube].vCoords.y, 0); Cube[iCube].pVertices[3].vPosition = tbVector3(Cube[iCube].vCoords.x + 1, Cube[iCube].vCoords.y - 1, 0); } // Karten Koordinaten neu berechnen for (int x=0; x<8; x++) { Cube2[x].pVertices[0].vPosition = tbVector3(Cube[x].pVertices[0].vPosition.x, Cube[x].pVertices[0].vPosition.y, 0); Cube2[x].pVertices[1].vPosition = tbVector3(Cube[x].pVertices[0].vPosition.x, Cube[x].pVertices[0].vPosition.y, 0.1f); Cube2[x].pVertices[2].vPosition = tbVector3(Cube[x].pVertices[1].vPosition.x, Cube[x].pVertices[1].vPosition.y, 0); Cube2[x].pVertices[3].vPosition = tbVector3(Cube[x].pVertices[1].vPosition.x, Cube[x].pVertices[1].vPosition.y, 0.1f); Cube2[x].pVertices[4].vPosition = tbVector3(Cube[x].pVertices[3].vPosition.x, Cube[x].pVertices[3].vPosition.y, 0); Cube2[x].pVertices[5].vPosition = tbVector3(Cube[x].pVertices[3].vPosition.x, Cube[x].pVertices[3].vPosition.y, 0.1f); Cube2[x].pVertices[6].vPosition = tbVector3(Cube[x].pVertices[2].vPosition.x, Cube[x].pVertices[2].vPosition.y, 0); Cube2[x].pVertices[7].vPosition = tbVector3(Cube[x].pVertices[2].vPosition.x, Cube[x].pVertices[2].vPosition.y, 0.1f); Cube2[x].pVertices[8].vPosition = tbVector3(Cube[x].pVertices[0].vPosition.x, Cube[x].pVertices[0].vPosition.y, 0); Cube2[x].pVertices[9].vPosition = tbVector3(Cube[x].pVertices[0].vPosition.x, Cube[x].pVertices[0].vPosition.y, 0.1f); } // wenn seit dem letzen drücken 0.125 sekunden vorbei sind // sind die tasten wieder frei. if (g_fTime - Time >= 0.125) { Time = 0.0f; Enable = true; } return TB_OK; } // ****************************************************************** // Die Render-Funktion tbResult RenderProc(float fNumSecsPassed) { char acText[256]; tbVector2 vPosition; POINT Cursor; tbMatrix mRotation; // Rotationsmatrix tbMatrix mTranslation; // Translationsmatrix tbMatrix mWorld; // Vereinende Weltmatrix tbMatrix mCamera; HRESULT hResult; // Puffer leeren und die Szene beginnen tbDirect3D& D3D = tbDirect3D::Instance(); D3D->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR(0.23f), 1.0f, 0); D3D->BeginScene(); mCamera = tbMatrixCamera(tbVector3 (2.5f + sinf(g_fTime)*5, 2.5f + cosf(g_fTime)*0.5f, -3.5f), tbVector3 (2.5f, 2.5f, 0), tbVector3(0.0f, 1.0f, 0.0f)); D3D->SetTransform(D3DTS_VIEW, (D3DMATRIX*)(&mCamera)); D3D->SetTransform(D3DTS_WORLD, (D3DMATRIX*)(&tbMatrixIdentity ())); D3D->SetFVF (SVertex::dwFVF); for (int iCube=0; iCube<g_iNumCubes; iCube++) { D3D.SetTexture(0, pTexture [0]); // Zeichnen! D3D->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, // Dreiecksliste 2, // Basisvertexindex Cube[iCube].pVertices, // Der kleinste Index sizeof(SVertex)); // 12 Dreiecke pro Würfel } for (int iCube=0; iCube<g_iNumCubes; iCube++) { D3D.SetTexture(0, NULL); // Zeichnen! D3D->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, // Dreiecksliste 8, // Basisvertexindex Cube2[iCube].pVertices, // Der kleinste Index sizeof(SVertex)); // 12 Dreiecke pro Würfel } D3D.SetTexture(0, pTexture[1]); // Zeichnen! D3D->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, // Dreiecksliste 2, // Basisvertexindex Ground.pVertices, // Der kleinste Index sizeof(SVertex)); // 12 Dreiecke pro Würfel // ------------------------------------------------------------------ // Text mit der ersten Schriftart zeichnen, bei (10, 10) g_pFont1->Begin(); g_pFont1->DrawText(tbVector2(150.0f, 10.0f), "Das ist die Schriftart <Futura>!"); g_pFont1->End(); // ------------------------------------------------------------------ // Szene beenden D3D->EndScene(); return TB_OK; } // ****************************************************************** // Aufräumen tbResult CleanUp() { // Alles löschen TB_SAFE_DELETE(g_pFont1); // Direct3D und den Texturmanager herunterfahren tbDirect3D::Instance().Exit(); tbTextureManager::Instance().Exit(); tbTextureManager::Instance().ReleaseTexture(pTexture[0]); tbTextureManager::Instance().ReleaseTexture(pTexture[1]); // Die TriBase-Engine herunterfahren tbExit(); return TB_OK; } // ****************************************************************** // Windows-Hauptfunktion int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, char* pcCommandLine, int iShowCommand) { // TriBase-Engine initialisieren tbInit(); // Konfiguration abfragen tbResult r; if(r = tbDoConfigDialog(&g_Config)) { if(r == TB_CANCELED) return 0; else { // Fehler! MessageBox(NULL, "Fehler im Konfigurationsdialog!", "Fehler", MB_OK | MB_ICONEXCLAMATION); return 1; } } // Direct3D initialisieren if(tbDirect3D::Instance().Init(&g_Config, "Beispielprogramm Nr. 3: Texte", NULL, LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1)))) { // Fehler! MessageBox(NULL, "Fehler bei der Direct3D-Initialisierung!", "Fehler", MB_OK | MB_ICONEXCLAMATION); CleanUp(); return 1; } // Texturmanager erstellen if(tbTextureManager::Instance().Init()) { MessageBox(tbDirect3D::Instance().GetWindow(), "Fehler beim Initialisieren des Texturmanagers!", "Fehler", MB_OK | MB_ICONEXCLAMATION); CleanUp(); return 1; } // Erste Schriftart... g_pFont1 = new tbFont; if(g_pFont1->Init("Data\\Futura_8_22.tga", "Data\\Futura_8_22.tbf")) { // Fehler... return TB_ERROR; } InitScene (); // Nachrichtenschleife betreten if(tbDoMessageLoop(MoveProc, RenderProc)) { MessageBox(tbDirect3D::Instance().GetWindow(), "Fehler beim Zeichnen!", "Fehler", MB_OK | MB_ICONEXCLAMATION); CleanUp(); return 1; } // Aufräumen CleanUp(); return 0; } // ****************************************************************** |
C-/C++-Quelltext |
|
1 2 3 4 5 |
char acText[256]; // Speichert die Nachricht g_pFont1->Begin(); sprintf(acText, "Camera: %.2f | %.2f | %.2f", g_vCameraPos.x, g_vCameraPos.y, g_vCameraPos.z); g_pFont1->DrawTextA(tbVector2(10.0f,10.0f), acText); g_pFont1->End(); |
Administrator
Werbeanzeige