1. Help wrapping SDL2.0
- Posted by mb7001 Oct 14, 2014
- 1852 views
Hi I am new to this forum, but I have been using EU4.0 for a while. I want to wrap SDL 2.0 and am using Mark Akita's SDL_wrap.ew as a backbone. I think I have the hang of most of the wraps, but I do not think I understand some of the dll wrappings. The file I am working on is the SDL Hints. I don't think I need it, but it is worth learning. The header file shows the following code
#define SDL_HINT_WINRT_HANDLE_BACK_BUTTON "SDL_HINT_WINRT_HANDLE_BACK_BUTTON" --And many other definitions extern DECLSPEC SDL_bool SDLCALL SDL_SetHint(const char *name, const char *value);
I think that the wrap for this is something like:
global constant SDL_HINT_WINRT_HANDLE_BACK_BUTTON = "SDL_HINT_WINRT_HANDLE_BACK_BUTTON" --I have already opened the SDL in another header so I have this constant xSDL_SetHint = define_c_func( sdl, "SDL_SetHint", {C_POINTER, C_POINTER}, C_BOOL) global function SDL_SetHint(sequence name, sequence value) hint = allocate_string(length(name)) val = allocate_string(length(value)) hint = poke_string(hint, length(name), {name}) val = poke_string(val, length(value), {value}) ret = c_func(xSDL_SetHint, {hint, val}) return ret end function
Is this the way to go about this?
The call would look something like this:
atom junk
junk = SDL_SetHint("SDL_HINT_NEW_HINT", "1")
2. Re: Help wrapping SDL2.0
- Posted by ghaberek (admin) Oct 14, 2014
- 1863 views
Close! The allocate_string() function actually accepts a string and pokes it into memory for you. Just use allocate_string() directly. You could use allocate() and poke_string() but that's just twice the work.
A few other points on this as well...
- It doesn't look like you're free()-ing the values you've allocated. Always make sure to free anything you allocate.
- You can also use automatic cleanup on any allocate function by adding a 1 as the second parameter.
- Thanks to the magic of automatic cleanup, you can one-line the entire function call.
- You can return the value from c_func() directly without storing it. Unless you need it for some reason.
- Since you're using Euphoria 4.0, you should be using public, not global, which is generally discouraged (see Defining the scope of an identifier).
public constant SDL_HINT_WINRT_HANDLE_BACK_BUTTON = "SDL_HINT_WINRT_HANDLE_BACK_BUTTON" -- I have already opened the SDL in another header so I have this constant xSDL_SetHint = define_c_func( sdl, "SDL_SetHint", {C_POINTER, C_POINTER}, C_BOOL) public function SDL_SetHint( sequence name, sequence value ) -- allocate the strings into memory with automatic cleanup atom p_name = allocate_string( name, 1 ) atom p_value = allocate_string( value, 1 ) -- don't bother storing the return value, -- just return whatever c_func() gives us return c_func( xSDL_SetHint, {p_name,p_value} ) end function -- Thanks to automatic cleanup, you can also one-line this whole function call... public function SDL_SetHint( sequence name, sequence value ) return c_func( xSDL_SetHint, {allocate_string(name,1),allocate_string(value,1)} ) end function
Hope that helps,
-Greg
3. Re: Help wrapping SDL2.0
- Posted by mb7001 Oct 14, 2014
- 1841 views
It is good to know that I am on the right track. Thank you for the tips, they will come in handy for sure. I found a wrapper in the archives for SDL2 but it is for a 64 bit version and I will be making it for the 32 bit version. I will apply the tips immediately. I wish there were some tutorials on using the memory functions in Euphoria. The docs are good, but it is a little difficult to see in my mind how they work.
4. Re: Help wrapping SDL2.0
- Posted by Icy_Viking Oct 18, 2014
- 1772 views
It is good to know that I am on the right track. Thank you for the tips, they will come in handy for sure. I found a wrapper in the archives for SDL2 but it is for a 64 bit version and I will be making it for the 32 bit version. I will apply the tips immediately. I wish there were some tutorials on using the memory functions in Euphoria. The docs are good, but it is a little difficult to see in my mind how they work.
I believe I still have the 32-bit version on my computer somewhere. If I can find it, I'll upload it. You're welcome to improve/edit upon my wrapper if you'd like.
5. Re: Help wrapping SDL2.0
- Posted by mb7001 Oct 18, 2014
- 1745 views
That would be great. I have wrapped several of the functions, but I do not know if they will work or not, because I can't test until most of the functions are wrapped. This is definitely a great learning experience for me. I have even used struct.e by ghaberek (Greg) to recreate the structures. On that note, I was wondering if you can explain what this line means to me in C header file:
typedef struct SDL_RWops { Sint64 (SDLCALL * size) (struct SDL_RWops * context); . . . }
I can see that there is a structure being declared there with the type of SDL_RWops, but not sure how to call a struct within a struct. Or even exactly what this line is saying.
6. Re: Help wrapping SDL2.0
- Posted by ghaberek (admin) Oct 18, 2014
- 1768 views
typedef struct SDL_RWops { Sint64 (SDLCALL * size) (struct SDL_RWops * context); . . . }
I can see that there is a structure being declared there with the type of SDL_RWops, but not sure how to call a struct within a struct. Or even exactly what this line is saying.
According to the SDL Wiki, that structure is a set of callbacks for low-level SDL programming with some additional data tacked on then end (a "type" and some "hidden" data). You can probably skip wrapping it.
If you do want to wrap it, each function callback should be a pointer (e.g. C_POINTER), in which you will store (using poke4()) a value from call_back() that points to a routine_id() of a function with the matching parameters (which are always atoms). In this case, that first callback wants to accept a single parameter that is a pointer to a "SDL_RWopts" struct called "context".
function SDL_RWops_size_callback( atom context ) -- 'context' is a pointer to an SDL_RWops structure, use it here as you need return 0 -- callbacks must return a value, see relevant documentation for possible values end function atom SDL_RWops_size_callback_cb = call_back( routine_id("SDL_RWops_size_callback") )
Here's a good StackOverflow discussion on Understanding typedefs for function pointers in C.
-Greg
7. Re: Help wrapping SDL2.0
- Posted by mb7001 Oct 18, 2014
- 1705 views
Thank you sir. Probably will skip the low-level stuff then, but it is good to be able to learn this. It is advance for me with my knowledge of the C-language.
8. Re: Help wrapping SDL2.0
- Posted by Icy_Viking Oct 18, 2014
- 1694 views
That would be great. I have wrapped several of the functions, but I do not know if they will work or not, because I can't test until most of the functions are wrapped. This is definitely a great learning experience for me. I have even used struct.e by ghaberek (Greg) to recreate the structures. On that note, I was wondering if you can explain what this line means to me in C header file:
typedef struct SDL_RWops { Sint64 (SDLCALL * size) (struct SDL_RWops * context); . . . }
I can see that there is a structure being declared there with the type of SDL_RWops, but not sure how to call a struct within a struct. Or even exactly what this line is saying.
I had forgot about Greg's struct.e I could have used that to wrap the Rect functions in SDL. It would come in handy for when you need to declare Rect variables in SDL. Since Euphoria has no native support for structs at this time, hopefully with the next release, struct support will be added.
9. Re: Help wrapping SDL2.0
- Posted by mb7001 Oct 20, 2014
- 1634 views
I had forgot about Greg's struct.e I could have used that to wrap the Rect functions in SDL. It would come in handy for when you need to declare Rect variables in SDL. Since Euphoria has no native support for structs at this time, hopefully with the next release, struct support will be added.
Yes Greg has done some good work. I have declared structs in EU as I have found them in C. I also went ahead and declared some of the special types used in SDL, just for clarification.
10. Re: Help wrapping SDL2.0
- Posted by ghaberek (admin) Oct 20, 2014
- 1684 views
I had forgot about Greg's struct.e I could have used that to wrap the Rect functions in SDL. It would come in handy for when you need to declare Rect variables in SDL. Since Euphoria has no native support for structs at this time, hopefully with the next release, struct support will be added.
Yes Greg has done some good work. I have declared structs in EU as I have found them in C. I also went ahead and declared some of the special types used in SDL, just for clarification.
I just realized you guys were talking about this: http://openeuphoria.org/forum/109791.wc#109791. I had forgotten all about that until now. Good to see it's actually getting tested, since I had said this...
I just whipped this up. Let me know what you think. Perhaps we could polish it and include it in the 4.0 release. (I'm not sure how much testing needs to be done, or how well this actually works for that matter.)
It's basically the same idea as the memory management routines in Win32Lib, but slimmed down and re-using the C_* constants from std/dll.e.
If it seems useful, then I can update it and added to my Euphoria Advanced Library. This will open up a lot more available types that I have added to adv/dll.e.
-Greg