[m-users.] FFI mapping header values to constructor types

Sean Charles (emacstheviking) objitsu at gmail.com
Thu Sep 23 06:18:01 AEST 2021


A long time ago (2019) I started an SDL wrapper, then, I found this site:
https://github.com/FlyingJester/SDL2.m/blob/master/sdl2.m <https://github.com/FlyingJester/SDL2.m/blob/master/sdl2.m>

I decided to persevere with my own version as it would both help me to learn and to only write the code that my application needs, but that single file has been an immense source of inspiration and education for sure, thanks to FlyingJester’s efforts.

I find myself now stumbling to see the light on how to convert C world values into corresponding Mercury world values, first, the code:

:- type button    ---> left ; middle ; right.

:- pragma foreign_enum("C", button/0, [
    left   - "SDL_BUTTON_LEFT",
    middle - "SDL_BUTTON_MIDDLE",
    right  - "SDL_BUTTON_RIGHT"
]).

:- pragma foreign_export_enum("C", button/0, [uppercase]).

…and in my event handling C code…

711             case SDL_MOUSEBUTTONDOWN: {
712                 int buttonType = LEFT;
713                 if (event.button.button == SDL_BUTTON_MIDDLE) {
714                     buttonType = MIDDLE;
715                 } else if (event.button.button == SDL_BUTTON_LEFT) {
716                     buttonType = RIGHT;
717                 }
718                 Event = createYesEvent(
719                     createMouseDownEvent(
720                         buttonType,
721                         event.button.x, event.button.y,
722                         event.button.clicks));
723                 } break

but I get this when I try to compile it, I thought I had done well to get this far, it’s taken me a while!

mmc -s hlc.gc -O4 -lsdl2 -lSDL2_mixer -lSDL2_image --make main
Making Mercury/int3s/sdl2.int3
Making Mercury/ints/sdl2.int
Making Mercury/cs/sdl2.c
sdl2.m:289: In clause for function `create_mouse_down_event'/4:
sdl2.m:289:   in function result term of clause head:
sdl2.m:289:   in unification of variable `HeadVar__5'
sdl2.m:289:   and term `mouse_down(Btn, V_10, Clicks)':
sdl2.m:289:   type error in argument of functor `mouse_down'/3.
sdl2.m:289:   Argument 1 (Btn) has type `int',
sdl2.m:289:   expected type was `sdl2.button'.
sdl2.m:297: In clause for function `create_mouse_up_event'/4:
sdl2.m:297:   in function result term of clause head:
sdl2.m:297:   in unification of variable `HeadVar__5'
sdl2.m:297:   and term `mouse_up(Btn, V_10, Clicks)':
sdl2.m:297:   type error in argument of functor `mouse_up'/3.
sdl2.m:297:   Argument 1 (Btn) has type `int',
sdl2.m:297:   expected type was `sdl2.button’.

This is something that will come up again and again and again as I make progress towards a graphics framework, possibly based around Queaks’ Morhpic framework which I have always admired since I first got into Squeak two decades ago.

Thanks,
Sean.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurylang.org/archives/users/attachments/20210922/44e8b820/attachment.html>


More information about the users mailing list