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 |
// // // // Puzzel // // // #pragma once // Include the directX header file #include <d3d9.h> #include <d3dx9.h> // Windows Header Files: #include <windows.h> #include <assert.h> #include <mmsystem.h> // functionen LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); bool InitialiseDirectX(HWND hWnd); void CleanupDirectX(); bool InitGame(); void Render(); void Move(); // Struct struct SVertex { D3DXVECTOR3 pos; float fRHW; D3DCOLOR color; float textPos[2]; static const DWORD dwFVF; }; const DWORD SVertex::dwFVF = D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1; struct Part { SVertex Vertex[4]; int x; int y; int id; }; // Global variablen Part parts[8]; int field[3][3]; LPDIRECT3D9 gD3dObject = NULL; // the directX object LPDIRECT3DDEVICE9 gD3dDevice = NULL; // the directX device LPDIRECT3DTEXTURE9 gTexture=NULL; |
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 |
#include "main.h" int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow) { // Describe and register our window class WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = 0; wcex.lpfnWndProc = (WNDPROC)WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = 0; wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = 0; wcex.lpszMenuName = NULL; wcex.lpszClassName = "Puzzel"; wcex.hIconSm = NULL; if (RegisterClassEx(&wcex)==0) return 0; // Create the window HWND hWnd=CreateWindow("Puzzel", "Puzzel", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,CW_USEDEFAULT, 600, 600, NULL, NULL, hInstance, NULL); if (hWnd == NULL) { MessageBox(NULL, "Fehler beim Initialisieren der Anwendung!", "Fehler", MB_OK | MB_ICONEXCLAMATION); return 0; } if (!InitialiseDirectX(hWnd)) { MessageBox(NULL, "Fehler beim Initialisieren von DirectX !", "Fehler", MB_OK | MB_ICONEXCLAMATION); return 0; } if (!InitGame()) { MessageBox(NULL, "Fehler beim Initialisieren des Spieles!", "Fehler", MB_OK | MB_ICONEXCLAMATION); CleanupDirectX(); return 0; } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); // Enter our program loop - this loop continues until the program exits // For maximum performance whenever the application is idle (not getting messages) update and render our scene MSG msg; ZeroMemory( &msg, sizeof(msg) ); while( msg.message!= WM_QUIT ) { if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } else { /*Move();*/ Render(); } } // DirectX herunterfahren CleanupDirectX(); return 1; } LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_DESTROY: PostQuitMessage(0); break; default: break; } return DefWindowProc(hWnd, message, wParam, lParam); } void Render () { D3DXMATRIX mat; HRESULT hr=gD3dDevice->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER ,D3DCOLOR_XRGB(0,0,0),1.0f,0); if (FAILED(hr)) return; hr=gD3dDevice->BeginScene(); if ( FAILED(hr)) return; D3DXMatrixIdentity(&mat); gD3dDevice->SetTransform(D3DTS_PROJECTION,&mat); gD3dDevice->SetTransform(D3DTS_WORLD,&mat); gD3dDevice->SetTransform(D3DTS_VIEW,&mat); gD3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); gD3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID); gD3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE); gD3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); gD3dDevice->SetRenderState(D3DRS_DITHERENABLE, TRUE); for (int i = 0; i < 8;i++) { if(FAILED(hr = gD3dDevice->DrawPrimitiveUP(D3DPT_TRIANGLELIST, // Dreiecksliste 2, // 2 Dreiecke parts[i].Vertex, // Vertexdaten sizeof(SVertex)))) // Vertexgröße { // Fehler beim Zeichnen! MessageBox(NULL, "Fehler beim Zeichnen !", "Fehler", MB_OK | MB_ICONEXCLAMATION); return; } } MessageBox(NULL, "Ach Hallo !", "Fehler", MB_OK | MB_ICONEXCLAMATION); // Szene beenden gD3dDevice->EndScene(); // Der große Moment: den Bildpuffer sichtbar machen gD3dDevice->Present(NULL, NULL, NULL, NULL); } bool InitialiseDirectX(HWND hWnd) { // Create the D3D object gD3dObject=Direct3DCreate9(D3D_SDK_VERSION); if (gD3dObject==NULL) return FALSE; // Fill out some presentation parameters for running in a window D3DPRESENT_PARAMETERS presParams; ZeroMemory(&presParams,sizeof(presParams)); presParams.Windowed=TRUE; presParams.SwapEffect=D3DSWAPEFFECT_DISCARD; presParams.BackBufferFormat=D3DFMT_UNKNOWN; presParams.BackBufferCount=1; presParams.EnableAutoDepthStencil = true; presParams.AutoDepthStencilFormat = D3DFMT_D24X8; // Create the D3D device HRESULT hr=gD3dObject->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &presParams, &gD3dDevice); if (FAILED(hr)) { // Some cards may not do hardware vertex processing so fall back to software: hr=gD3dObject->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presParams, &gD3dDevice); if (FAILED(hr)) return FALSE; } return TRUE; } void CleanupDirectX() { if (gTexture) gTexture->Release(); if (gD3dObject) gD3dObject->Release(); if (gD3dDevice) gD3dDevice->Release(); } bool InitGame () { HRESULT hResult; if(FAILED(hResult = gD3dDevice->SetFVF(SVertex::dwFVF))) { // Fehler beim Setzen des Vertexformats! return false; } // Beleuchtung und Culling ausschalten, Dithering aktivieren gD3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE); gD3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); gD3dDevice->SetRenderState(D3DRS_DITHERENABLE, TRUE); // Hier werden die dreiecke angelegt for(int i = 0; i < 8;i++) { parts[i].x = (i % 3) * 200; parts[i].y = i / 3; parts[i].id = i; for( int j = 0; j < 4; j++) { parts[i].Vertex[j].color = D3DCOLOR_XRGB(255,0,0); parts[i].Vertex[j].fRHW = 1.0f; } // Position ******************************************* // Links oben parts[i].Vertex[0].pos.x = 0; parts[i].Vertex[0].pos.y = 0; parts[i].Vertex[0].pos.z = 0; // rechts oben parts[i].Vertex[1].pos.x = 200; parts[i].Vertex[1].pos.y = 0; parts[i].Vertex[1].pos.z = 0; // rechts unten parts[i].Vertex[2].pos.x = 200; parts[i].Vertex[2].pos.y = 200; parts[i].Vertex[2].pos.z = 0; // links unten parts[i].Vertex[3].pos.x = 0; parts[i].Vertex[3].pos.y = 200; parts[i].Vertex[3].pos.z = 0; // Textur-position ****************************************** // links oben parts[i].Vertex[0].textPos[0] = (float)(i % 3) * ( 1/4 ); parts[i].Vertex[0].textPos[1] = (int)(i / 3) * ( 1/4 ); // rechts oben parts[i].Vertex[1].textPos[0] = (float)(i % 3) * ( 1/4 ) + 1/4; parts[i].Vertex[1].textPos[1] = (int)(i / 3) * ( 1/4 ); // rechts unten parts[i].Vertex[2].textPos[0] = (float)(i % 3) * ( 1/4 ) + 1/4; parts[i].Vertex[2].textPos[1] = (int)(i / 3) * ( 1/4 ) + 1/4; // rechts oben parts[i].Vertex[3].textPos[0] = (float)(i % 3) * ( 1/4 ); parts[i].Vertex[3].textPos[1] = (int)(i / 3) * ( 1/4 ) + 1/4; } if ( FAILED(hResult = D3DXCreateTextureFromFile(gD3dDevice,"GamePic.bmp",&gTexture))) { return 0; } gD3dDevice->SetTexture(0, gTexture); return true; } |
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 |
D3DXMATRIX projmat; D3DXMATRIX viewmat; // Nun den Benutzerblickwinkel aufsetzen // Linke-Hand-orientiertes System aufbauen // Parameter 1: Kamera-Position // Parameter 2: Punkt, auf den geschaut wird // Parameter 3: Vektor, der angibt, wo oben ist (normal [0|1|0]) D3DXMatrixLookAtLH(&viewmat,new D3DXVECTOR3(0.0f, 2.0f, -5.0f), new D3DXVECTOR3 (0.0f, 0.0f, 0.0f), new D3DXVECTOR3 (0.0f, 1.0f, 0.0f)); // Und die Ansichtsmatrix aufsetzen // Linke-Hand-orientiertes System aufbauen // Parameter 1: Sichtbereich (Blickwinkel) // Parameter 2: Seitenverhältnis // Parameter 3: Abstand zum nächsten sichtbaren Punkt (nahe Ebene) // Parameter 4: Abstand zum letzten sichtbaren Punkt (ferne Ebene) D3DXMatrixPerspectiveFovLH(&projmat,(float)D3DX_PI / 4, 800 / (float)600, 0.1f, 100.0f); gD3dDevice->SetTransform(D3DTS_PROJECTION,&projmat); gD3dDevice->SetTransform(D3DTS_VIEW,&viewmat); |
Werbeanzeige