Advertisement

Initializing Win32 structures

Started by January 08, 2000 11:27 AM
5 comments, last by Alastair 24 years, 11 months ago
Anybody spend a lot of time typing stuff like this :- DDSURFACEDESC2 ddsd; memset(&ddsd, 0, sizeof(ddsd)); ddsd.dwSize = sizeof(ddsd); Tedious isn''t it? However, I discovered this pleasantly shorter version and I thought I would share it with you :- DDSURFACEDESC2 ddsd = { sizeof(ddsd) }; This does exactly the same thing. It sets the first field (dwSize) to the size of a DDSURFACEDESC2 structure. It initializes the remaining fields to zero. The same goes for other structures that have a size field :- BITMAPINFOHEADER bmih = { sizeof(bmih) }; D3DDEVICEDESC desc = { sizeof(desc) }; ...
Are you sure this works with all DirectX structures? It''ll only work if dwSize is the first one, right? And how does it zero out the remaining fields?

-Jey Kottalam
14 Year Old Computer Programmer
http://www.armageddongames.com
-Jey Kottalam14 Year Old Computer Programmerhttp://www.armageddongames.com
Advertisement
You''ll have to initialize everything up to and including the size variable in the order of the decleration of the structure your initializing.

This is the way static initialization works. You can initialize all of the elements of a structure/array, whatever or only the first ones that you care about. C initializes the rest of the structure with 0''s.

int ary[10] = {1,3};

causes c to initialize the array as {1, 3, 0, 0, 0, 0, 0, 0, 0, 0}

Notwen!
Notwenwww.xbox.com
Tell me is I''m wrong, but I think dwSize is allways first in the structs.
size is always first for all DX / WindowsWhateverEx structures. It''s basically used as Version number so whatever is handling it needs to take a quick peek and decide what version of the code to use.
-the logistical one-http://members.bellatlantic.net/~olsongt
I thought of another solution to this problem...
pretty simple, but would clean up code and make for less typing:

It is just an idea, and I haven''t compiled it yet, so maybe there is something wrong, but you get the idea...

------------------------------------

template
class DirectXStruct
{
private:

DIRECTX_STRUCT iStruct;

public:

DirectXStruct()
{
memset( &iStruct, 0, sizeof(DIRECTX_STRUCT) );
iStruct.dwSize = sizeof(DIRECTX_STRUCT);
}

// address of operator
DIRECTX_STRUCT* operator&(void)
{
return &iStruct
}

// cast operator to type
operator DIRECTX_STRUCT(void)
{
return iStruct;
}

// cast operator to const type
operator const DIRECTX_STRUCT(void) const
{
return iStruct;
}
};

---------------------

so then, in theory, you can use it like so

DirectXStruct surfaceDesc;

// this may be suspect...
surfaceDesc.dwHeight = 12;

SOME_DDRAW_FUNC( &surfaceDesc );

Anyway, at least you don''t have to worry about initing the struct everywhere.





Advertisement
My last post lost the angle brackets and stuff in between ''em.

I don''t remember how to make them show up in HTML, oh well, that''s what I get for using Frontpage...

anyway that template class is supposed to have the angle brackets as is the variable declaration

you get the idea...

This topic is closed to new replies.

Advertisement