1. Raylib and Classes

Hello all,

I am doing this as an expermint for now.

I'm using the classes.e file to help with the structs that Raylib has. I'm wondering if this works or could get extran help to make sure I'm doing this correctly.

C Code

typedef struct Vector2 { 
    float x;                // Vector x component 
    float y;                // Vector y component 
} Vector2; 
 
typedef struct Color { 
    unsigned char r;        // Color red value 
    unsigned char g;        // Color green value 
    unsigned char b;        // Color blue value 
    unsigned char a;        // Color alpha value 
} Color; 
 
RLAPI void DrawPixelV(Vector2 position, Color color);     

Eu code

include classes.e 
 
sequence TVector2 = class("TVector2") 
TVector2 = addProperty(TVector2,"x",0) 
TVector2 = addProperty(TVector2,"y",0) 
addMethod(TVector2,"Vector2",routine_id("Vector2")) 
 
sequence Vector2 = classes:new(TVector2) 
 
sequence TColor = class("TColor") 
TColor = addProperty(TColor,"r",0) 
TColor = addProperty(TColor,"g",0) 
TColor = addProperty(TColor,"b",0) 
TColor = addProperty(TColor,"a",0) 
 
sequence Color = classes:new(TColor) 
 
public constant xDrawPixelV = define_c_proc(ray,"DrawPixelV",{C_FLOAT,C_UCHAR}) 
 
public procedure DrawPixelV(void = callMethod(Vector2,"Vector2",{"Vector2"}), void = callMethod(Color,"Color",{"Color"})) 
 
 c_proc(xDrawPixelV,{Something here}) 
 
end procedure 

I'm a bit confused on how to use it with DLL functions.

new topic     » topic index » view message » categorize

2. Re: Raylib and Classes

Icy_Viking said...

typedef struct Vector2 { 
    float x;                // Vector x component 
    float y;                // Vector y component 
} Vector2; 
 
typedef struct Color { 
    unsigned char r;        // Color red value 
    unsigned char g;        // Color green value 
    unsigned char b;        // Color blue value 
    unsigned char a;        // Color alpha value 
} Color; 
 
RLAPI void DrawPixelV(Vector2 position, Color color);     

I'm a bit confused on how to use it with DLL functions.

A lot of Raylib functions pass structures by value instead of by pointer. You can get around this by "expanding" the structure members as parameters. But there's not much you can do about returning structure by value since Euphoria doesn't support that at this time. This is why I made raylibshim which accepts and returns structures by pointer instead. (Apparently I need to update raylibshim for version 4.0.) I can't help you with this classes library per se but here's how you would expand the structure members.

constant xDrawPixelV = define_c_proc(ray,"DrawPixelV",{ 
    C_FLOAT, -- position.X 
    C_FLOAT, -- position.Y 
    C_UCHAR, -- color.r 
    C_UCHAR, -- color.g 
    C_UCHAR, -- color.b 
    C_UCHAR  -- color.a 
}) 
 
public procedure DrawPixelV(sequence position, sequence color) 
 
    c_proc(xDrawPixelV,{ 
        position[1], -- float 
        position[2], -- float 
        color[1], -- unsigned char 
        color[2], -- unsigned char 
        color[3], -- unsigned char 
        color[4]  -- unsigned char 
    }) 
 
end procedure 

-Greg

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

3. Re: Raylib and Classes

ghaberek said...
Icy_Viking said...

typedef struct Vector2 { 
    float x;                // Vector x component 
    float y;                // Vector y component 
} Vector2; 
 
typedef struct Color { 
    unsigned char r;        // Color red value 
    unsigned char g;        // Color green value 
    unsigned char b;        // Color blue value 
    unsigned char a;        // Color alpha value 
} Color; 
 
RLAPI void DrawPixelV(Vector2 position, Color color);     

I'm a bit confused on how to use it with DLL functions.

A lot of Raylib functions pass structures by value instead of by pointer. You can get around this by "expanding" the structure members as parameters. But there's not much you can do about returning structure by value since Euphoria doesn't support that at this time. This is why I made raylibshim which accepts and returns structures by pointer instead. (Apparently I need to update raylibshim for version 4.0.) I can't help you with this classes library per se but here's how you would expand the structure members.

constant xDrawPixelV = define_c_proc(ray,"DrawPixelV",{ 
    C_FLOAT, -- position.X 
    C_FLOAT, -- position.Y 
    C_UCHAR, -- color.r 
    C_UCHAR, -- color.g 
    C_UCHAR, -- color.b 
    C_UCHAR  -- color.a 
}) 
 
public procedure DrawPixelV(sequence position, sequence color) 
 
    c_proc(xDrawPixelV,{ 
        position[1], -- float 
        position[2], -- float 
        color[1], -- unsigned char 
        color[2], -- unsigned char 
        color[3], -- unsigned char 
        color[4]  -- unsigned char 
    }) 
 
end procedure 

-Greg

Thanks Greg. Maybe I'll just wait for Eu 4.2 with memstruct. Hopefully it can get out the door this year!

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

4. Re: Raylib and Classes

Icy_Viking said...

Maybe I'll just wait for Eu 4.2 with memstruct. Hopefully it can get out the door this year!

Maybe, but memstruct wouldn't fix this. Memstruct is for accessing structures in memory. This involves passing structures members as a list of parameters, which are scattered around the CPU registers depending on their type (int, float, etc.). See Parameter passing and Return values. Hypothetically the interface for that would look something like what I have below. Although I'm unsure of what to call that member_ctypes() function and that's the best I've got for now. What I'd really prefer to do is integrate the declared memstruct names into the type system so we can avoid this change of concept for "dot" halfway through a statement, but I'm still trying to figure out all the necessary parts to make that happen.

typedef struct Vector2 { 
    float x;                // Vector x component 
    float y;                // Vector y component 
} Vector2; 
 
typedef struct Color { 
    unsigned char r;        // Color red value 
    unsigned char g;        // Color green value 
    unsigned char b;        // Color blue value 
    unsigned char a;        // Color alpha value 
} Color; 
 
RLAPI void DrawPixelV(Vector2 position, Color color);    // Draw a pixel (Vector version) 
RLAPI Vector2 GetMonitorPosition(int monitor);           // Get specified monitor position 

memstruct rlVector2 
    float x             -- Vector x component 
    float y             -- Vector y component 
end memstruct 
 
memstruct rlColor 
    unsigned char r     -- Color red value 
    unsigned char g     -- Color green value 
    unsigned char b     -- Color blue value 
    unsigned char a     -- Color alpha value 
end memstruct 
 
constant RL_VECTOR2 = member_ctypes(rlVector2) -- {C_FLOAT,C_FLOAT} 
constant RL_COLOR = member_ctypes(rlColor) -- {C_UCHAR,C_UCHAR,C_UCHAR,C_UCHAR} 
 
constant xDrawPixelV = define_c_proc(raylib,"DrawPixelV",{RL_VECTOR2,RL_COLOR}) 
constant xGetMonitorPosition = define_c_func(raylib,"GetMonitorPosition",{C_INT},RL_VECTOR2) 
 
public procedure DrawPixelV(atom position, atom color) 
    c_proc(xDrawPixelV, { 
        position.rlVector2, -- passes {x,y} 
        color.rlColor       -- passes {r,g,b,a} 
    } ) 
end procedure 
 
public function GetMonitorPosition(integer monitor) 
    return c_func(xGetMonitorPosition, {monitor}) -- returns {x,y} 
end function 

-Greg

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

5. Re: Raylib and Classes

ghaberek said...
Icy_Viking said...

Maybe I'll just wait for Eu 4.2 with memstruct. Hopefully it can get out the door this year!

Maybe, but memstruct wouldn't fix this. Memstruct is for accessing structures in memory. This involves passing structures members as a list of parameters, which are scattered around the CPU registers depending on their type (int, float, etc.). See Parameter passing and Return values. Hypothetically the interface for that would look something like what I have below. Although I'm unsure of what to call that member_ctypes() function and that's the best I've got for now. What I'd really prefer to do is integrate the declared memstruct names into the type system so we can avoid this change of concept for "dot" halfway through a statement, but I'm still trying to figure out all the necessary parts to make that happen.

typedef struct Vector2 { 
    float x;                // Vector x component 
    float y;                // Vector y component 
} Vector2; 
 
typedef struct Color { 
    unsigned char r;        // Color red value 
    unsigned char g;        // Color green value 
    unsigned char b;        // Color blue value 
    unsigned char a;        // Color alpha value 
} Color; 
 
RLAPI void DrawPixelV(Vector2 position, Color color);    // Draw a pixel (Vector version) 
RLAPI Vector2 GetMonitorPosition(int monitor);           // Get specified monitor position 

memstruct rlVector2 
    float x             -- Vector x component 
    float y             -- Vector y component 
end memstruct 
 
memstruct rlColor 
    unsigned char r     -- Color red value 
    unsigned char g     -- Color green value 
    unsigned char b     -- Color blue value 
    unsigned char a     -- Color alpha value 
end memstruct 
 
constant RL_VECTOR2 = member_ctypes(rlVector2) -- {C_FLOAT,C_FLOAT} 
constant RL_COLOR = member_ctypes(rlColor) -- {C_UCHAR,C_UCHAR,C_UCHAR,C_UCHAR} 
 
constant xDrawPixelV = define_c_proc(raylib,"DrawPixelV",{RL_VECTOR2,RL_COLOR}) 
constant xGetMonitorPosition = define_c_func(raylib,"GetMonitorPosition",{C_INT},RL_VECTOR2) 
 
public procedure DrawPixelV(atom position, atom color) 
    c_proc(xDrawPixelV, { 
        position.rlVector2, -- passes {x,y} 
        color.rlColor       -- passes {r,g,b,a} 
    } ) 
end procedure 
 
public function GetMonitorPosition(integer monitor) 
    return c_func(xGetMonitorPosition, {monitor}) -- returns {x,y} 
end function 

-Greg

I see the memstruct is not without its caveats. Still I'm sure it would still be useful when dealing with wrapping libraries that have many structs as Raylib does.

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

Search



Quick Links

User menu

Not signed in.

Misc Menu