我很奇怪为什么只有dcu,应该也有pas,实际上他就是由c语言的头文件翻译过来的
如果你只用到2维的图象功能,你可以只use ddraw,下面是一个C++Builder的例子
,他随机填充颜色。可以很容易的该成delphi的。若用3维功能就很麻烦,不封装的
话会累死人的,我推荐CDX lib(VC++)写的,相当不错(封装的比较简单好学)
不象delphiX,很难用,很难改。我觉得DirectX调试极困难,动不动就死,nt支
持又不好,不太好学。
头文件
//---------------------------------------------------------------------------
#ifndef cfillH
#define cfillH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TMainForm : public TForm
{
__published: // IDE-managed Components
TTimer *GameTimer;
void __fastcall FormDestroy(TObject *Sender);
void __fastcall GameTimerTimer(TObject *Sender);
void __fastcall FormKeyDown(TObject *Sender, WORD &Key,
TShiftState Shift);
private: // User declarations
LPDIRECTDRAW lpDD;
LPDIRECTDRAWSURFACE lpDDSurPrimary,lpDDSurBack;
bool GameActive;
byte phase;
void __fastcall Start();
public: // User declarations
__fastcall TMainForm(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TMainForm *MainForm;
//---------------------------------------------------------------------------
#endif
主文件
cpp文件
//---------------------------------------------------------------------------
#include <vcl.h>
#include <ddraw.h>
#pragma hdrstop
#include "cfill.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TMainForm *MainForm;
//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
: TForm(Owner)
{
lpDD = NULL;
phase = 0;
GameActive = false;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormDestroy(TObject *Sender)
{
if(lpDD !=NULL)
{
if(lpDDSurPrimary !=NULL)
{
lpDDSurPrimary->Release();
lpDDSurPrimary = NULL;
}
lpDD->Release();
lpDD = NULL;
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Start()
{
HRESULT hres;
DDSURFACEDESC ddSurDesc;
char buf[256];
hres = DirectDrawCreate(NULL,&lpDD,NULL);
if(hres == DD_OK)
{
hres = lpDD->SetCooperativeLevel(Handle,
DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
if(hres == DD_OK)
{
hres = lpDD->SetDisplayMode(640, 480, 8);
if(hres == DD_OK)
{
ddSurDesc.dwSize = sizeof(ddSurDesc);
ddSurDesc.dwFlags = DDSD_CAPS;
ddSurDesc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
hres = lpDD->CreateSurface(&ddSurDesc,&lpDDSurPrimary, NULL);
if(hres == DD_OK)
{
GameTimer->Enabled = True;
GameActive = True;
return;
}
}
}
}
wsprintf(buf, "Direct Draw Init Failed (%08lx)/n", hres);
MessageBox(Handle, buf, "ERROR", MB_OK);
Close();
}
//--------------------------------------------------------------
void __fastcall TMainForm::GameTimerTimer(TObject *Sender)
{
DDBLTFX ddbltfx;
HRESULT hres;
randomize;
ddbltfx.dwSize= sizeof (DDBLTFX);
ddbltfx.dwFillColor= RGB(random(256),random(256),random(256));
//lpDDSurPrimary->Lock(NULL,NULL,DDLOCK_WAIT,NULL);
hres = lpDDSurPrimary->Blt (NULL,NULL, NULL, DDBLT_WAIT | DDBLT_COLORFILL, &ddbltfx);
//lpDDSurPrimary->Unlock(NULL);
//Form的属性最好是无边框,无标题栏
if(hres == DDERR_SURFACELOST)
{
hres = lpDDSurPrimary->Restore();
if (hres == DD_OK)
lpDDSurPrimary->Blt(NULL,NULL,NULL, DDBLT_WAIT | DDBLT_COLORFILL,&ddbltfx);
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormKeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
switch (Key)
{
case VK_F3:
Start();
break;
case VK_ESCAPE:
Close();
break;
}
}