Skip to main content
GameDev.net gamedev.net
🔒 Locked

Pointer to an HDC?

Started by Utwo Apr 3, 2003 at 10:27 PM 6 replies 2.7k views
Original Post
Utwo
Utwo
Hello. I''m writing a sprite class, and I want this sprite class to contain a pointer to an HDC. That way, I can load one bitmap that contains all the sprite''s frames, and multiple instances of that sprite can all point to the same bitmap, rather than each containing a copy of that bitmap, which is wasteful. Although with Windows programming this is sort of confusing. I''m assuming HDC is simply a typedef''ed pointer to a MS-created object of some kind, like this:
typedef   MSCreatedType * HDC;
 
Right? Well in any case, if I want to have a pointer to an HDC in my sprite class, would the following code be ok?
HDC * bitmap;
 
And then could I dereference it just like any other pointer type? For example:
BitBlt(WindowDC, 0, 0, 50, 50, *bitmap, 0, 0, SRCCOPY);
 
---signature---" Actually, at the time, I didn't give a damn about the FoxNews issue... just that you would come swooping in with your Super Mith cape flapping behind you and debunk it just because it didn't happen in your living room." - InnocuousFox
Xai
Xai
yes and no ... YES, you can have a pointer to an HDC or an Hanything (HWND, etc) ... and dereference it like you want to ... but there is no point.

For 1, if HDC was a typedef for a pointer type ... then you could just declare:

HDC bitmap; and assign to it ... there would be very few reasons to use an aditional pointer layer ...

but the H types are NOT pointers ... they are indexes ... in other words simple integers ... which of course are USED by windows as lookups into tables ... but the point being, you treat them like integral types, just assign to them and compare them for equality as if they were any other form of index or iterator (or even pointer of course) ...

all of microsofts Handle types are also compatible with each other ... ie, identical types ... although I''ve been told that in STRICT mode, they do use pointer typedefs just to allow the compiler to warn you when you mix them ...
MattB
MattB
This might help. From winnt.h in the platform SDK:


#ifdef STRICT
typedef void *HANDLE;
#define DECLARE_HANDLE(name) struct name##__ { int unused; }; typedef struct name##__ *name
#else
typedef PVOID HANDLE;
#define DECLARE_HANDLE(name) typedef HANDLE name
#endif
Utwo
Utwo
Xai, so does that mean that I can literally just do this:

HDC spriteframes;

// Load bitmap into spriteframes, etc

...and then give my sprite object access to that DC by doing the following?

// ...

HDC bitmap = spriteframes;

//...

Since a Hanything simply holds a number that Microsoft uses to look up the item it is handling, would the above code suffice?
---signature---" Actually, at the time, I didn't give a damn about the FoxNews issue... just that you would come swooping in with your Super Mith cape flapping behind you and debunk it just because it didn't happen in your living room." - InnocuousFox
Xai
Xai
Yes, excatly ...

think of it like a numerical key or something ... you can''t make them up, or add or subtract them ... but you can copy them around at will ... for the same cost as copying pointers to them around ...
Teodric
Teodric
I don''t know what MS''s current recommendations are, but back in the day when handles were a scarce resource it was recommended that handles only be held for a minimum amount of time. You''d get yourself the handle you needed, use it, and release it (explicitly) as soon as possible. This way I believe windows was freed from the costly accounting of handles (which are a finite resource). If you needed to pass handles around, you''d use DuplicateHandle() (I think that''s what it was called) and the usual function for getting rid of it (ReleaseHandle()?)

Now, again, I''m not sure of current status, but since they are internally tracked it might be prudent to follow the same practice still. That way windows won''t be as likely to surprise you by, for instance, making a handle invalid while you''re using it.

Maybe this is all just nostalgia now. Anyway, good luck.
Leadorn
Leadorn
mohahahah i think handles are pointers!!! ms you cant hide it anymore.


mohahahaha
Xai
Xai
just like any ref-counted object ... you do not need to duplicate it, if there is one OWNER in your code, who will not release it until every other user is done ... the Duplicate function is for cases where you want your handles to be passed around, and owned by different people ... in other words .. if you duplicate, after the duplicates are made, each of the 2 people with the handle can and must release them when they are finished (with no need to worry about the other) ... if you do a simple assignment, then obviously you only release the handle once, and must wait until you are done with it to do so. It will NOT be free''d by windows, period, unless you call release on it ... and making a duplicate, do you can release ONE of them early does not reduce the amount of time you tie up handles, unless it allows you to write code that might release them both earlier than you could otherwise detect that it''s safe to delete the single owner ...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.