DirectX介绍:

DirectX介绍:
1.DirectX的工作原理是什么呢?DirectX提供了几乎是硬件级控制所有设备的功能。这样就可以通过组件对象模型(COM)技术和由Microsoft和硬件厂商编写的一套驱动程序和库函数来完成。Microsoft提出了一套关于函数、变量、数据结构等内容的协议--硬件厂商也必须遵循以实现驱动程序与和硬件间的交流。
只要遵行这些约定,就不需要担心硬件的细节问题。只要将该内容调用到DirectX中,DirectX就会完成处理细节的工作。无论视频卡、音频卡、输入设备、网卡或是其他设备是什么类型,只要是DirectX支持的类型,即使不熟悉这些设备,在程序中也可以使用该设备。

2.HEL和HAL
在DirectX下有两个层面,分别是HEL(硬件模拟层)和HAL(硬件抽象层).

3深入DirectX基础类
(1)DirectDraw--控制视频显示的基本着色和2D图形引擎,是所有图形必须穿过的通道,可能是最重要的DirectX组件。DirectDraw的对象大都是系统中的视频卡。
(2)DirectSound--DirectX中的声音组件,只支持数字声音,不支持MIDI。
(3)DirectSound3D -- DirectSound的3D声音组件。
(4)DirectMusic--最新的DirectX组件,比DirectSound强大
(5)DirectInput--该系统处理所有的输入设备。
(6)DirectPlay--这是DirectX的网络方面的内容
(7)Direct3DRM--Direct3D保留模式,它是高级的、以对象、贞为基础的3D系统。
(8)Direct3DIM--Direct3D即时模式,它是低级的DirectX的3D支持。

3什么是COM
总之COM就是为了让对象改变后不必重新编译原来的程序就能工作。大多情况下COM对象都是DLL,随着使用的软件带来或者是下载,这样便于升级和更改。

4.COM对象究竟是什么?
一个COM对象实际上就是实现大量界面的一个C++类或者是一套C++类。
COM的一个规则是:如果已经存在一个界面的话,可以一直从该界面中申请其他的界面,条件是该界面来自同一个COM对象。

5COM是编写组件软件的一种新方式--允许创建可重复使用的、能够在运行时间中自动连接的软件模块。
如果创建了一个对旧对象进行升级的新的COM对象,应当同时执行旧界面和添加新界面。有一条非常重要的规则:所有的基于COM对象的程序依然能够基于新版COM对象运行,而不需要进行重新编译。

6.最后,COM通过利用基于组件类属的架构开辟了编写大型计算机程序的可能性。当然,DirectX,OLE和ActiveX都是基于COM技术的,因此应当掌握COM技术

7.一个创建COM对象的实例子:
// DEMO5_1.CPP - A ultra minimal working COM example
// NOTE: not fully COM compliant

// INCLUDES //

#include <stdio.h>
#include <malloc.h>
#include <iostream.h>
#include <objbase.h> // note: you must include this header it contains important constants
                     // you must use in COM programs

// GUIDS /

// these were all generated with GUIDGEN.EXE

// {B9B8ACE1-CE14-11d0-AE58-444553540000}
const IID IID_IX =
{ 0xb9b8ace1, 0xce14, 0x11d0, { 0xae, 0x58, 0x44, 0x45, 0x53, 0x54, 0x0, 0x0 } };


// {B9B8ACE2-CE14-11d0-AE58-444553540000}
const IID IID_IY =
{ 0xb9b8ace2, 0xce14, 0x11d0, { 0xae, 0x58, 0x44, 0x45, 0x53, 0x54, 0x0, 0x0 } };

// {B9B8ACE3-CE14-11d0-AE58-444553540000}
const IID IID_IZ =
{ 0xb9b8ace3, 0xce14, 0x11d0, { 0xae, 0x58, 0x44, 0x45, 0x53, 0x54, 0x0, 0x0 } };


// INTERFACES

// define the IX interface
interface IX: IUnknown
{

virtual void __stdcall fx(void)=0;

};

// define the IY interface
interface IY: IUnknown
{

virtual void __stdcall fy(void)=0;

};


// CLASSES AND COMPONENTS ///

// define the COM object
class CCOM_OBJECT : public IX,
                    public IY
{
public:

 CCOM_OBJECT() : ref_count(0) {}
 ~CCOM_OBJECT() {}

private:

virtual HRESULT __stdcall QueryInterface(const IID &iid, void **iface);
virtual ULONG __stdcall AddRef();
virtual ULONG __stdcall Release();

virtual void __stdcall fx(void) {cout << "Function fx has been called." << endl; }
virtual void __stdcall fy(void) {cout << "Function fy has been called." << endl; }

int ref_count;

};

// CLASS METHODS

HRESULT __stdcall CCOM_OBJECT::QueryInterface(const IID &iid, void **iface)
{
// this function basically casts the this pointer or the Iunknown
// pointer into the interface requested, notice the comparison with
// the GUIDs generated and defined in the begining of the program

// requesting the IUnknown base interface
if (iid==IID_IUnknown)
 {
 cout << "Requesting IUnknown interface" << endl;
 *iface = (IX*)this;
 
 } // end if

// maybe IX?
if (iid==IID_IX)
 {
 cout << "Requesting IX interface" << endl;
 *iface = (IX*)this;

 } // end if
else  // maybe IY
if (iid==IID_IY)
 {
 cout << "Requesting IY interface" << endl;
 *iface = (IY*)this;

 } // end if
else
 { // cant find it!
 cout << "Requesting unknown interaface!" << endl;
 *iface = NULL;
 return(E_NOINTERFACE);
 } // end else

// if everything went well cast pointer to IUnknown and call addref()
((IUnknown *)(*iface))->AddRef();

return(S_OK);

} // end QueryInterface

ULONG __stdcall CCOM_OBJECT::AddRef()
{
// increments reference count
cout << "Adding a reference" << endl;
return(++ref_count);

} // end AddRef

///

ULONG __stdcall CCOM_OBJECT::Release()
{
// decrements reference count
cout << "Deleting a reference" << endl;
if (--ref_count==0)
 {
 delete this;
 return(0);
 } // end if
else
 return(ref_count);

} // end Release

///

IUnknown *CoCreateInstance(void)
{
// this is a very basic implementation of CoCreateInstance()
// it creates an instance of the COM object, in this case
// I decided to start with a pointer to IX -- IY would have
// done just as well

IUnknown *comm_obj = (IX *)new(CCOM_OBJECT);

cout << "Creating Comm object" << endl;

// update reference count
comm_obj->AddRef();

return(comm_obj);

} // end CoCreateInstance

///

void main(void)
{

// create the main COM object
IUnknown *punknown = CoCreateInstance();

// create two NULL pointers the the IX and IY interfaces
IX *pix=NULL;
IY *piy=NULL;

// from the original COM object query for interface IX
punknown->QueryInterface(IID_IX, (void **)&pix);

// try some of the methods of IX
pix->fx();

// release the interface
pix->Release();


// now query for the IY interface
punknown->QueryInterface(IID_IY, (void **)&piy);

// try some of the methods
piy->fy();

// release the interface
piy->Release();

// release the COM object itself
punknown->Release();

} // end main


8创建和使用DirectX界面
首先,需要对DirectDraw进行下面三方面工作:
(1) DirectDraw运行时间COM对象和动态连接库必须进行装载并注册。这由DirectX安装程序来完成
(2)必须包含Win32程序中DDRAW.LIB输入库,以便于你调用封装函数的连接。
(3)必须包含Win32程序中的DDRAW.H文件,以便于编译器明白DirectDraw的头文件信息、原型以及数据类型

下面是DirectDraw界面指针的函数类型:
LPDIRECTDRAW lpdd = NULL;

要创建DirectDraw COM对象及检索指向DirectDraw对象的界面指针(该对象表示显卡),应当使用封装函数DirectDrawCreate(),如下所示:
DirectDrawCreate(NULL,&lpdd,NULL);

举个具体操作的例子:
lpdd->SetVideoMode(640,480,256);//将显示模式设定为640*480,256色
所以本质上讲,任何DirectX的调用都采用下面方式:
interface_pointer->method_name(parametor list);

当然也可以从原始DirectDraw界面通过应用QueryInterface()函数来使用其他的任何想使用的界面,如Direct3D;

9解决DirectX版本不同步的问题,如何获得最新版本的界面:
LPDIRECTDRAW lpdd;//version 1.0
LPDIRECTDRAW4 lpdd4;//version6.0,but called 4.0

//create version 1.0 DirectDraw object interface
DirectDrawCreate(NULL,&lpdd,NULL);

//now look in DDRAW.H header,find IDIRECTDRAW4 interface
//ID and use it to query for the interface
lpdd ->QueryInterface(IID_IDirectDraw4,&lpdd4)
lpdd->Release()//You had two,so drop the old one
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值