1. Freeglut Wrapper Help

Hello,

I am working on a freeglut wrapper for Euphoria. This will be based off the most current version of Freeglut, which is 3.2.1. I just wanna make sure I'm wrapping the callback functions correctly.

//C Code 
FGAPI void    FGAPIENTRY glutJoystickFunc( void (* callback)( unsigned int, int, int, int ), int pollInterval ); 

--Eu Code 
public constant xglutJoystickFunc = define_c_proc(glut,"+glutJoystickFunc",{C_POINTER,C_UINT,C_INT,C_INT,C_INT,C_INT,C_INT}) 
 
public procedure glutJoystickFunc(atom cb,atom x,atom x2,atom x3,atom x4,atom poll) 
 
 c_proc(xglutJoystickFunc,{cb,x,x2,x3,x4,poll}) 
	 
end procedure 
new topic     » topic index » view message » categorize

2. Re: Freeglut Wrapper Help

Icy_Viking said...

//C Code 
FGAPI void    FGAPIENTRY glutJoystickFunc( void (* callback)( unsigned int, int, int, int ), int pollInterval ); 

I believe "void (* callback)( unsigned int, int, int, int )" translates to just a single function pointer (a callback).
Also, it should be define_c_func not define_c_proc.

new topic     » goto parent     » topic index » view message » categorize

3. Re: Freeglut Wrapper Help

petelomax said...

I believe "void (* callback)( unsigned int, int, int, int )" translates to just a single function pointer (a callback).

Yeah like Pete said, callbacks are pointers to user-supplied functions. So this function only takes two arguments:

-- 
-- FGAPI void FGAPIENTRY glutJoystickFunc( void (* callback)( unsigned int, int, int, int ), int pollInterval ); 
-- 
constant xglutJoystickFunc = define_c_proc( glut, "+glutJoystickFunc", {C_POINTER,C_INT} ) 
  
public procedure glutJoystickFunc( atom cb, atom poll ) 
    c_proc( xglutJoystickFunc, {cb,poll} ) 
end procedure 

And then you'd use it like this:

public constant 
    GLUT_JOYSTICK_BUTTON_A = 0x0001, 
    GLUT_JOYSTICK_BUTTON_B = 0x0002, 
    GLUT_JOYSTICK_BUTTON_C = 0x0004, 
    GLUT_JOYSTICK_BUTTON_D = 0x0008 
 
function myJoystickCallback( atom buttons, atom xaxis, atom yaxis, atom zaxis ) 
 
    -- 
    -- print something like this: 
    -- 
    -- buttons: A___, xaxis: 123, yaxis: -456, zaxis: 789 
    -- 
 
    sequence btnstate = repeat( '_', 4 ) 
 
    if and_bits( buttons, GLUT_JOYSTICK_BUTTON_A ) then btnstate[1] = 'A' end if 
    if and_bits( buttons, GLUT_JOYSTICK_BUTTON_B ) then btnstate[2] = 'B' end if 
    if and_bits( buttons, GLUT_JOYSTICK_BUTTON_C ) then btnstate[3] = 'C' end if 
    if and_bits( buttons, GLUT_JOYSTICK_BUTTON_D ) then btnstate[4] = 'D' end if 
 
    printf( 1, "buttons: %s, xaxis: %d, yaxis: %d, zaxis: %d\n" ) 
 
    return NULL 
end function 
constant myJoystickCallback_id = routine_id("myJoystickCallback") 
constant myJoystickCallback_cb = call_back(myJoystickCallback_id) 
 
glutJoystickFunc( myJoystickCallback_cb, 100 ) 

Personally, I would use the default parameter nesting feature to clean up the routine_id/call_back stuff:

public procedure glutJoystickFunc( sequence func, atom poll, integer id=routine_id(func) ) 
    c_proc( xglutJoystickFunc, {call_back(id),poll} ) 
end procedure 

Then just call the procedure like this to assign your callback:

glutJoystickFunc( "myJoystickCallback", 100 ) 
petelomax said...

Also, it should be define_c_func not define_c_proc.

The function is marked void in the header: glutJoystickFunc. Should be a proc.

-Greg

new topic     » goto parent     » topic index » view message » categorize

4. Re: Freeglut Wrapper Help

ghaberek said...
petelomax said...

I believe "void (* callback)( unsigned int, int, int, int )" translates to just a single function pointer (a callback).

Yeah like Pete said, callbacks are pointers to user-supplied functions. So this function only takes two arguments:

-- 
-- FGAPI void FGAPIENTRY glutJoystickFunc( void (* callback)( unsigned int, int, int, int ), int pollInterval ); 
-- 
constant xglutJoystickFunc = define_c_proc( glut, "+glutJoystickFunc", {C_POINTER,C_INT} ) 
  
public procedure glutJoystickFunc( atom cb, atom poll ) 
    c_proc( xglutJoystickFunc, {cb,poll} ) 
end procedure 

And then you'd use it like this:

public constant 
    GLUT_JOYSTICK_BUTTON_A = 0x0001, 
    GLUT_JOYSTICK_BUTTON_B = 0x0002, 
    GLUT_JOYSTICK_BUTTON_C = 0x0004, 
    GLUT_JOYSTICK_BUTTON_D = 0x0008 
 
function myJoystickCallback( atom buttons, atom xaxis, atom yaxis, atom zaxis ) 
 
    -- 
    -- print something like this: 
    -- 
    -- buttons: A___, xaxis: 123, yaxis: -456, zaxis: 789 
    -- 
 
    sequence btnstate = repeat( '_', 4 ) 
 
    if and_bits( buttons, GLUT_JOYSTICK_BUTTON_A ) then btnstate[1] = 'A' end if 
    if and_bits( buttons, GLUT_JOYSTICK_BUTTON_B ) then btnstate[2] = 'B' end if 
    if and_bits( buttons, GLUT_JOYSTICK_BUTTON_C ) then btnstate[3] = 'C' end if 
    if and_bits( buttons, GLUT_JOYSTICK_BUTTON_D ) then btnstate[4] = 'D' end if 
 
    printf( 1, "buttons: %s, xaxis: %d, yaxis: %d, zaxis: %d\n" ) 
 
    return NULL 
end function 
constant myJoystickCallback_id = routine_id("myJoystickCallback") 
constant myJoystickCallback_cb = call_back(myJoystickCallback_id) 
 
glutJoystickFunc( myJoystickCallback_cb, 100 ) 

Personally, I would use the default parameter nesting feature to clean up the routine_id/call_back stuff:

public procedure glutJoystickFunc( sequence func, atom poll, integer id=routine_id(func) ) 
    c_proc( xglutJoystickFunc, {call_back(id),poll} ) 
end procedure 

Then just call the procedure like this to assign your callback:

glutJoystickFunc( "myJoystickCallback", 100 ) 
petelomax said...

Also, it should be define_c_func not define_c_proc.

The function is marked void in the header: glutJoystickFunc. Should be a proc.

-Greg

Thanks for the help guys. This has really helped me.

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu