Re: SFML2 Demo Dilema - Solved

new topic     » goto parent     » topic index » view thread      » older message » newer message

Here's the traditional way to wrap structures pre-4.1, which adds (will add?) memstruct support. Some knowledge of C is required to get this right, especially with type sizes and signage. Most structure elements are 4 bytes but some are more tightly packed. Unions and embedded structures can complicate matters. Adding to that, type sizes typically change between 32-bit and 64-bit (but not always), which is why we need memstruct support for 64-bit 4.1.

Basically, we define each offset as a constant, then we don't have to peek/poke with "magic numbers" in our code. This helps prevent bugs imposed by improper offsets in the structure, as well as adding to overall code clarity.

-- sfKeyEvent struct 
public constant 
    sfKeyEvent__event   =  0, -- sfEventType 
    sfKeyEvent__code    =  4, -- sfKeyCode 
    sfKeyEvent__alt     =  8, -- sfBool 
    sfKeyEvent__control = 12, -- sfBool 
    sfKeyEvent__shift   = 16, -- sfBool 
    sfKeyEvent__system  = 20, -- sfBool 
    SIZEOF_SFKEYEVENT   = 24, 
$ 
 
-- example: 
atom event = allocate_data( SIZEOF_SFKEYEVENT, 1 ) 
atom eventType = peek4s( event + sfKeyEvent__event ) 
atom code      = peek4s( event + sfKeyEvent__code ) 
-- etc. 

Here's how you'd do it with memstruct in 4.1.

public memtype 
    int as sfEventType, 
    int as sfKeyCode, 
    int as sfBool, 
$ 
 
public memstruct sfKeyEvent 
    sfEventType event 
    sfKeyCode code 
    sfBool alt 
    sfBool control 
    sfBool shift 
    sfBool system 
end memstruct 
 
-- example: 
atom event = allocate_data( sizeof(sfKeyEvent), 1 ) 
atom eventType = event.sfKeyEvent.event 
atom code      = event.sfKeyEvent.code 
-- etc. 

-Greg

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu