Advertisement

why is it that you access DX's interface via pointers..,

Started by May 11, 2002 10:22 PM
21 comments, last by mickey 22 years, 6 months ago
or um i mean, why is everything there access via pointers unlike OpenGl wheras you don''t so why did they designed it like that, hope you got my point, coz lots of codes instead of decalring an ordinary object they would declare it as a pointer to a class,t ie, ACLASS* a; why won''t they just declare it as an ordinary object ie, ACLASS a; this way, they wouldn''t have to initialize(don''t need to use the new keyword) it anymore why do they prefer the other one? thanks,
http://www.dualforcesolutions.comProfessional website designs and development, customized business systems, etc.,
In one word (okay, acronym): COM.

In a couple more, DirectX uses the Microsoft Component Object Model which uses the concept of querying interfaces to determine their capabilities (not the capabilities of the functionality represented by the interface; the caps of the interface itself). This is to allow any COM-compliant application to safely determine if the necessary interface functionality is in place and gracefully terminate if not.

I''ll add this: You don''t have to keep the interface pointer fact in mind or manage them if you use Microsofts ATL CComPtr class. It''s a template class that manages the lifetime of a COM interface including internal reference counting, and it can be found in the <atlbase.h> header file (part of the Platform SDK, available for download at MSDN). Here''s how it''s used:
CComPtr< IDirect3D8 >       D3DObject = NULL;CComPtr< IDirect3DDevice8 > DevicePtr = NULL;...// this is the only time you need to call Attach with DX interfaces:D3DObject.Attach( Direct3D8Create( ... ) );/** do object validation here **/ D3DObject->CreateDevice( ..., &DevicePtr, ... ); 

Never call Release again!
Advertisement
furthermore, because dx is designed using com instead of the flat function model of opengl, it is completly backwards compatible and changes to implementations wont affect apps using the older interfaces. also it would be impossible to link to static objects due to the use of virtual calls.

opengl on the other hand is extended through accessing function ptrs which represetn the different extensions a driver may implement. because of this you have to check caps and check for the functions. easier for venders to add fetaures for better hardware support, but makes creating a standard interface for newer features difficult. on the flip side d3d does not allow such vendor extensions so new hardware features cant be supported until a new release of d3d. this helps control hardware venders and ensure igher compatiblity. the down side of course is that new hardware will not be able to make its new features availible for use until ms and other hardware venders agree on a standard and a new d3d is released.

personally i perfer com style api, but feel opengl is one step ahead with new feature support of hardware. however d3d has better easier ability to for ensure compatiblity between hardware when using new fetaures since the venders must follow the spec.

remeber this is not meant to be a flame war so please dont start one. i am not saying dx nor opengl are better, they both have weaknesses and strengths. though for the most part they are equal.
i like the com style at a technical level. i swear the biggest turn off for me and directx is little old lettering. the CAPS NUMBER CAP thing eww. i mean you get to be flexible as a coder but CAPS NUMBER CAP _on everything_ starts to slow you down pretty fast. there had to be a more "ergonomic" naming scheme.

i guess to the pro''s its 20 bucks a routine when you start adding it up so whats a few more minutes typing. but to a hobbyist. cringe. CAPS NUMBER CAPS WORD NUMBER. ewww

quote: Original post by declspec
i swear the biggest turn off for me and directx is little old lettering. the CAPS NUMBER CAP thing eww.

Those are typdefs. Rather than using "LPDIRECT3DDEVICE8", you can type "IDirect3DDevice8 *" - which, IMO, is more readable.
thanks, you guys gave me more than enough information for my question..,

but anyway, so about my other question? am really wondering why you have to make a pointer to a class instead of just instantiating the object the normal way, that way, you dont''t have to allocate memory anymore on runtime, btw, am speaking in terms of declaring these objects as globals,

quote:
In a couple more, DirectX uses the Microsoft Component Object Model which uses the concept of querying interfaces to determine their capabilities (not the capabilities of the functionality represented by the interface; the caps of the interface itself).


so if it''s not a pointer, this won''t work? (hope this is not a stupid question)

thanks!
http://www.dualforcesolutions.comProfessional website designs and development, customized business systems, etc.,
Advertisement
okey, let me make it clear..,

I see lots of game engine, when they use their classes, they declare a pointer to that class.,

and what i did is, i experiemented with their code, removed all the pointers (thus removing also the new and delete) and made everything a normal declaration and it still works..,

so what am confused at is, why would people want to declare a pointer instead of just declaring it the normal way..,?

ie,

CMyGameEngine *gameEngine = NULL; // why does it have to be a pointer?main(){    // start of game    gameEngine = new CMyGameEngine;        } 


thanks! hope you got my point,


http://www.dualforcesolutions.comProfessional website designs and development, customized business systems, etc.,
If you use a pointer, your object lifetime is controlled by you, so you can destroy objects if you''re running short on memory. I''d never go as far as dynamically allocating a game engine object, but I would dynamically allocate a graphics engine object - especially if the graphics engine needs to support several rendering interfaces/APIs:
// psuedo-code GraphicsEngine * pGfxEngine = NULL;switch( GetUserSelection( GfxAPI ) ){case OpenGL:  pGfxEngine = new OGLEngine;  break; case DirectXGraphics:  pGfxEngine = new DXEngine;  break; case v3Dfx_Glide:  pGfxEngine = new GlideEngine;  break;// etc} 

This way takes advantage of inheritance and polymorphism to provide modular flexibility to the engine architecture.
hello,

is that the only reason? to dynamically allocate and destroy objects?

probably there''s any other reason why? hmm., okey, sample is CDX(dx wrapper), it''s app wizard genereates a pointer to it''s CDXScreen Class,

ie,

CDXScreen *Screen = 0;

all apps that uses CDX should have this and it''s in global anyways, so what bothers me is, why did they declare it as a pointer wherein it could also be just declared the normal way and at the same would also save you from memory leaks,

thanks,







http://www.dualforcesolutions.comProfessional website designs and development, customized business systems, etc.,
I think that another fact is that :



Writing this : Class myclass ;




The code is compiled so that memory is allocated from the heap space (a limited chunk of memory that is used to hold variables)
Thus if your object is larger than the reamining heap size, your app will crash.

Using pointer will allow you to use all the available memory space.



----
David Sporn AKA Sporniket

[edited by - davidsporn on May 13, 2002 7:49:38 AM]

This topic is closed to new replies.

Advertisement