//--------------------------------------------------------------------------- #include #pragma hdrstop #include "TMainForm.h" //--------------------------------------------------------------------------- #pragma link "TDx_3D" #pragma link "dxguid.lib" #pragma link "tdx_draw" #pragma link "TDx_Draw_Library_Install" #pragma link "TDx_3DI_Library_Install" #pragma link "tdx_drawsurface" #pragma link "tddscaps" #pragma link "tddsurfacedesc" #pragma link "tdx_drawclipper" #pragma link "TDx_3DDevice" #pragma link "TDx_3D" #pragma link "tdx_draw" #pragma link "tdx_drawsurface" #pragma link "tddscaps" #pragma link "tddsurfacedesc" #pragma link "tdx_drawclipper" #pragma link "TDx_3DDevice" #pragma link "TD3DViewPort" #pragma resource "*.dfm" TMainForm *MainForm; //--------------------------------------------------------------------------- __fastcall TMainForm::TMainForm(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TMainForm::StartButtonClick(TObject *Sender) { // disable start button StartButton->Enabled = false; // create DirectDraw interface if (DirectDraw->Create( NULL )) { DirectDraw_Label->Caption = "Created"; DirectDraw_Label->Update(); } // choose non-fullscreen or full-screen/exclusive if (!FullScreenCheckBox->Checked) { // non-fullscreen DirectDraw setup if (DirectDraw->SetCooperativeLevel( Handle, DDSCL_NORMAL )) { CooperativeLevel_Label->Caption = "Normal"; CooperativeLevel_Label->Update(); } // create DirectDraw primary surface PrimarySurfaceDesc->Flags = 0; PrimarySurfaceDesc->SCaps = PrimarySurfaceCaps; PrimarySurfaceDesc->SCaps->Caps = DDSCAPS_PRIMARYSURFACE; if (PrimarySurface->Create( PrimarySurfaceDesc, DirectDraw )) { PrimarySurface_Label->Caption = "Created"; PrimarySurface_Label->Update(); } // create / retrieve backbuffer PrimarySurfaceDesc->Flags = DDSD_WIDTH | DDSD_HEIGHT; PrimarySurfaceDesc->SCaps = PrimarySurfaceCaps; PrimarySurfaceDesc->SCaps->Caps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE; PrimarySurfaceDesc->Width = ClientWidth; PrimarySurfaceDesc->Height = ClientHeight; if (BackBuffer->Create( PrimarySurfaceDesc, DirectDraw )) { BackBuffer_Label->Caption = "Created"; BackBuffer_Label->Update(); } } else { // fullscreen complex setup if (DirectDraw->SetCooperativeLevel( Handle, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE )) { CooperativeLevel_Label->Caption = "Fullscreen / Exclusive"; CooperativeLevel_Label->Update(); } // create DirectDraw primary surface PrimarySurfaceDesc->Flags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT; PrimarySurfaceDesc->SCaps = PrimarySurfaceCaps; PrimarySurfaceDesc->SCaps->Caps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_3DDEVICE | DDSCAPS_COMPLEX; PrimarySurfaceDesc->BackBufferCount = 1; if (PrimarySurface->Create( PrimarySurfaceDesc, DirectDraw )) { PrimarySurface_Label->Caption = "Created"; PrimarySurface_Label->Update(); } // create / retrieve backbuffer PrimarySurfaceCaps->Clear(); PrimarySurfaceCaps->Caps = DDSCAPS_BACKBUFFER; if (PrimarySurface->GetAttachedSurface( PrimarySurfaceCaps, BackBuffer )) { BackBuffer_Label->Caption = "Created"; BackBuffer_Label->Update(); } } // create a clipper for the primary surface if (PrimaryClipper->Create( DirectDraw )) { PrimaryClipper_Label->Caption = "Created"; PrimaryClipper_Label->Update(); } PrimaryClipper->SetHWnd( Handle ); if (PrimarySurface->SetClipper( PrimaryClipper )) { PrimaryClipper_Label->Caption = PrimaryClipper_Label->Caption + ", Attached to Primary Surface"; PrimaryClipper_Label->Update(); } // create d3d interface if (Direct3DI->Create( DirectDraw )) { Direct3DI_Label->Caption = "Created"; Direct3DI_Label->Update(); } // create hardware device //if (Direct3DIDevice->Create( Direct3DI, IID_IDirect3DHALDevice, BackBuffer )) // not working? if (Direct3DI->CreateDevice( IID_IDirect3DHALDevice, BackBuffer, Direct3DIDevice )) { Direct3DIDevice_Label->Caption = "Created"; Direct3DIDevice_Label->Update(); } // create the viewport ViewPort3D->Clear(); ViewPort3D->X = 0; ViewPort3D->Y = 0; ViewPort3D->Width = ClientWidth; ViewPort3D->Height = ClientHeight; ViewPort3D->MinZ = 0.0; ViewPort3D->MaxZ = 1.0; if (Direct3DIDevice->SetViewPort( ViewPort3D )) { ViewPort3D_Label->Caption = "Assigned to Direct3D Device"; ViewPort3D_Label->Update(); } // create a texture TextureSurfaceCaps->Clear(); TextureSurfaceDesc->Clear(); TextureSurfaceDesc->Flags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT; TextureSurfaceDesc->SCaps = TextureSurfaceCaps; TextureSurfaceDesc->SCaps->Caps = DDSCAPS_TEXTURE; TextureSurfaceDesc->Width = Image1->Picture->Bitmap->Width; TextureSurfaceDesc->Height = Image1->Picture->Bitmap->Height; if (Texture3D->Create( TextureSurfaceDesc, DirectDraw )) { Texture3D_Label->Caption = "Created"; Texture3D_Label->Update(); HDC hdc; if (Texture3D->GetDC( &hdc )) { BitBlt( hdc, 0,0, PrimarySurfaceDesc->Width, PrimarySurfaceDesc->Height, Image1->Picture->Bitmap->Canvas->Handle, 0, 0, SRCCOPY ); Texture3D->ReleaseDC( hdc ); Texture3D_Label->Caption = Texture3D_Label->Caption + ", Contains Valid Image Data"; Texture3D_Label->Update(); } } // assign texture to 3d device if (Direct3DIDevice->SetTexture( 0, Texture3D )) { Texture3D_Label->Caption = Texture3D_Label->Caption + ", Assigned to 3D Device"; Texture3D_Label->Update(); } // begin animation Timer1->Enabled = true; } //--------------------------------------------------------------------------- void __fastcall TMainForm::ExitButtonClick(TObject *Sender) { Timer1->Enabled = false; // not actually needed as the components auto-cleanup on destroy Texture3D->Destroy(); Direct3DIDevice->Destroy(); Direct3DI->Destroy(); PrimaryClipper->Destroy(); BackBuffer->Destroy(); PrimarySurface->Destroy(); DirectDraw->FlipToGDISurface(); DirectDraw->Destroy(); Close(); } //--------------------------------------------------------------------------- void __fastcall TMainForm::DirectDrawError(TObject *Sender, AnsiString Function, AnsiString Error, AnsiString ErrorMessage) { ErrorMemo->Lines->Add( "-----" ); ErrorMemo->Lines->Add( "Error" ); ErrorMemo->Lines->Add( "-----" ); ErrorMemo->Lines->Add( "Function: "+Function ); ErrorMemo->Lines->Add( "Error: "+Error ); ErrorMemo->Lines->Add( "ErrorMessage: "+ErrorMessage ); ErrorMemo->Lines->Add( "-----" ); } //---------------------------------------------------------------------------7 void __fastcall TMainForm::Timer1Timer(TObject *Sender) { Animating_Label->Caption = "Animating"; Animating_Label->Update(); Application->ProcessMessages(); RenderScene(); // paint to the backbuffer DisplayScene(); // flip to primary surface } //--------------------------------------------------------------------------- void __fastcall TMainForm::RenderScene() { static DWORD LastTime = timeGetTime(); static float SkyHorizPan = 0.0f; D3DTLVERTEX Sky[4]; SkyHorizPan += (timeGetTime()-LastTime) * 0.0002f; // scroll background LastTime = timeGetTime(); if (Direct3DIDevice->BeginScene()) { BeginScene_Label->Caption = "worked"; BeginScene_Label->Update(); } Sky[0] = D3DTLVERTEX(D3DVECTOR(0, 0, 0.99f), 0.01f, 0xffffffff, 0xffffffff, SkyHorizPan, 0); Sky[1] = D3DTLVERTEX(D3DVECTOR(ClientWidth, 0, 0.99f), 0.01f, 0xffffffff, 0xffffffff, SkyHorizPan+1, 0); Sky[2] = D3DTLVERTEX(D3DVECTOR(0, ClientHeight, 0.99f), 0.01f, 0xffffffff, 0xffffffff, SkyHorizPan, 1); Sky[3] = D3DTLVERTEX(D3DVECTOR(ClientWidth, ClientHeight, 0.99f), 0.01f, 0xffffffff, 0xffffffff, SkyHorizPan+1, 1); if (Direct3DIDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, D3DFVF_TLVERTEX, Sky, 4, 0)) { DrawPrimitive_Label->Caption = "working"; DrawPrimitive_Label->Update(); } if (Direct3DIDevice->EndScene()) { EndScene_Label->Caption = "working"; EndScene_Label->Update(); } } //--------------------------------------------------------------------------- void __fastcall TMainForm::DisplayScene() { // done. TRect r = BoundsRect; PrimarySurface->Flip( BackBuffer, DDFLIP_WAIT ); DirectDraw->WaitForVerticalBlank( DDWAITVB_BLOCKBEGIN ); PrimarySurface->Blt( &r, BackBuffer, NULL, DDBLT_WAIT, NULL ); if (PrimarySurface->ErrorValue == DDERR_SURFACELOST) DirectDraw->RestoreAllSurfaces(); } //--------------------------------------------------------------------------- void __fastcall TMainForm::ShowStuffCheckBoxClick(TObject *Sender) { if (ShowStuffCheckBox->Checked) { Stuff->Visible = true; ErrorMessages->Visible = true; ShowStuffCheckBox->Caption = "Hide Stuff"; } else { Stuff->Visible = false; ErrorMessages->Visible = false; ShowStuffCheckBox->Caption = "Show Stuff"; } } //---------------------------------------------------------------------------