Sign in to follow this  
Followers 0
AdmiralAlkex

Need help converting a struct

9 posts in this topic

I realize this probably count as a AutoIt question, but I believe the advanced nature warrants posting here. Any mods that disagree are free to move it to support.

Ok, so while I have been working on my SDL UDF I bumbed into a struct I have no ideo how to convert, I am just stumped. Here is the typedef:

typedef struct SDL_VideoInfo {
    Uint32 hw_available :1; /* Flag: Can you create hardware surfaces? */
    Uint32 wm_available :1; /* Flag: Can you talk to a window manager? */
    Uint32 UnusedBits1  :6;
    Uint32 UnusedBits2  :1;
    Uint32 blit_hw    :1;   /* Flag: Accelerated blits HW --> HW */
    Uint32 blit_hw_CC   :1; /* Flag: Accelerated blits with Colorkey */
    Uint32 blit_hw_A    :1; /* Flag: Accelerated blits with Alpha */
    Uint32 blit_sw    :1;   /* Flag: Accelerated blits SW --> HW */
    Uint32 blit_sw_CC   :1; /* Flag: Accelerated blits with Colorkey */
    Uint32 blit_sw_A    :1; /* Flag: Accelerated blits with Alpha */
    Uint32 blit_fill    :1; /* Flag: Accelerated color fill */
    Uint32 UnusedBits3  :16;
    Uint32 video_mem;   /* The total amount of video memory (in K) */
    SDL_PixelFormat *vfmt;  /* Value: The format of the video surface */
    int current_w;  /* Value: The current video mode width */
    int current_h;  /* Value: The current video mode height */
} SDL_VideoInfo;

Everything I know is that according to monoceres he did a sizeof() and it returned 20 bytes, and to be only 20 bytes with all those Uint32 has to mean there are some sort of "memory sharing" (I would guess all those :1, :6 and :16 are for that) but I don't know how or what or how to convert it to AutoIt.

I would be very grateful for any help on this.

Share this post


Link to post
Share on other sites



All of those :1 things are the widths of the fields in bytes. It's not sharing memory but actually truncating the space that the variable is allowed to occupy. Poor design if you ask me.

Share this post


Link to post
Share on other sites

Yeah I thought it could be something like that, so I tried this:

#Include "SDL v5.au3"
#Include <Array.au3>

_SDL_Init($_SDL_INIT_EVERYTHING)

$Struct = "align 1;ubyte;ubyte;ubyte;ubyte;ubyte;ubyte;ubyte;ubyte;ubyte;uint;ptr;int;int"

$Surface = _SDL_SetVideoMode(640, 480, 32, $_SDL_SWSURFACE)
$pTemp = DllCall($__SDL_DLL, "ptr:cdecl", "SDL_GetVideoInfo")

$Created = DllStructCreate($Struct, $pTemp[0])
ConsoleWrite(@error & "/" & DllStructGetSize($Created) & "/" & @error & "/" & @CRLF)

$Array = Struct2Array($Created)

_ArrayDisplay($Array)

_SDL_Quit()

Func Struct2Array($s)
    Local $arr[1]
    For $i=1 To 100
        $Tesmp = DllStructGetData($s,$i)
        If @error Then
            ReDim $arr[UBound($arr)-1]
            ExitLoop
        EndIf
        $arr[UBound($arr)-1] = $Tesmp
        ConsoleWrite($Tesmp & @CRLF)
        ReDim $arr[UBound($arr)+1]
    Next
    Return $arr
EndFunc;==>Struct2Array

But not only is it bigger than it should be (?), the text it outputs is just gibberish (if you run GetVideoInfo before SetVideoMode it should return the "best" values for your computer, otherwize it returns what is currently used). I also tried lots of variants like:

$Struct = "ubyte;ubyte;uint unused1;ushort unused2;ubyte unused3;ubyte;ubyte;ubyte;ubyte;ubyte;ubyte;ubyte;uint64 unused4;uint64 unused5;uint;ptr;int;int"

But it still looks like gibberish :)

Do you have any idea how to read this from AutoIt? Or any other way?

Share this post


Link to post
Share on other sites

All of those :1 things are the widths of the fields in bytes. It's not sharing memory but actually truncating the space that the variable is allowed to occupy. Poor design if you ask me.

Richard, it's a good thing nobody asked you because you're completely wrong. Those are bits, not bytes. Those 12 variables take up just 32-bits of memory. It's certainly not poor design, either. I bet code that uses that is a damn good bit more readable than the alternative "if (var & 0xF0000000)" all over the place. I just don't think the code is portable (I could be wrong, however). I know there is a good reason to avoid using that notation unless absolutely necessary for optimization, however, I just can't recall what it is.

At any rate, since those 12 variables take up 4-bytes and there are 4 other variables which are also 4-bytes that brings the total size of the structure up to 20 bytes (24-bytes on x64 due to the pointer).

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Sooo... I don't know much about this C/C++ jazz, but I am assuming that those ":n" sections are in bits not bytes? Since if you add all of them up with that assumption (also assuming the SDL_PixelFormat pointer is 32 bits) then you end up with 20 bytes. Might be wayyy off though. Also, since they are in essence just boolean flags it seems ridiculous that they'd be more than 1 bit.

EDIT:

Looks like I was beat to the punch.

EDIT the EDIT:

Can you just pick them out as 32 bit ints and pick them apart with bitmasks like Valik showed in AutoIT?

Edited by Wus

Share this post


Link to post
Share on other sites

@Valik & Wus

Thank you both, that makes alot more sense, I run the code above with "uint;uint;ptr;int;int" and and it returns:

0/20/0/

2

0

0x001A5E50

640

480

If i am getting this right I just need to BitAnd the first value to get what is enabled, right? Than means that a window manager is available, which is good I guess, but having zero hardware acceleration is a bit sad :)

Share this post


Link to post
Share on other sites

Bit-field! That's it. Thank you, I couldn't remember what that syntax was called.

Share this post


Link to post
Share on other sites

Ok, so I failed. I've only ever used field alignment in C# and I believe it is byte based.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0