<div dir="ltr">Zoltan,<div><br></div><div>It's nice to meet somebody working on Mercury!</div><div><br></div><div>My current goal is to create a low-level wrapper around SDL2 to the point where I can create applications with it. Maybe it was a bit ambitious for a first project but what the hell, I started so I have to finish now. I need it!!!</div><div><br></div><div>I understand your comments but it will take some time to turn it into code for me.</div><div><br></div><div>Thanks,</div><div>Sean.</div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, 14 Jul 2019 at 23:43, Zoltan Somogyi <<a href="mailto:zoltan.somogyi@runbox.com">zoltan.somogyi@runbox.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><br>
<br>
On Sun, 14 Jul 2019 23:08:13 +0100, emacstheviking <<a href="mailto:objitsu@gmail.com" target="_blank">objitsu@gmail.com</a>> wrote:<br>
> My question then is what would be "the best way"<br>
> to handle this case. With two rectangles I have had to create four<br>
> predicates, with three it would be 8 etc etc and it feels like there is a<br>
> better way.<br>
<br>
The "best way" depends on information you haven't told us: whether<br>
most of the code working on rectangles is in Mercury or in C.<br>
<br>
If most of the code that manipulates values of a type is in C, then you should<br>
define the type in C, and write foreign_proc predicates and/or functions to<br>
export the needed functionality to Mercury code.<br>
<br>
If most of the code that manipulates values of a type is in Mercury, then you should<br>
define the type in Mercury, and write Mercury predicates and/or functions, exported<br>
to C, to provide the needed functionality to C code.<br>
<br>
If the code sample you gave is representative, then I would have a C declaration<br>
<br>
typedef SDL_Rect *SDL_RectPtr;<br>
<br>
and Mercury definitions<br>
<br>
:- type sdl_rect;<br>
:- pragma foreign_type(c, sdl_rect, "SDL_RectPtr").<br>
<br>
% Return the x and y coords, width and height of a rectangle if and only if non-null.<br>
%<br>
:- pred sdl_rect_is_real(sdl_rect::in, int::out, int::out, int::out, int::out) is semidet.<br>
<br>
Then you would need only one sdl_rendercopy predicate, which could do its<br>
own null/nonnull tests as needed.<br>
<br>
> From what I have learned so far I know that<br>
> parameters are passed in as MR_Word sized values so would it be safe for<br>
> example to cast an sdl_rect() declared as:<br>
> <br>
> *:- type sdl_rect ---> sdl_rect(x::int, y::int, w::int, h::int).*<br>
> <br>
> *typedef struct SDL_Rect{ int x, y; int w, h;} SDL_Rect;*<br>
> <br>
> to an SDL_Rect which has the same size and shape?!?!?!?!<br>
<br>
No, that would not be safe. It may work with the current data representation<br>
scheme used by the Mercury compiler, but that scheme may change.<br>
I should know; I am working on such a change right now.<br>
<br>
Zoltan.<br>
</blockquote></div>