<div dir="ltr">I apologise for the long code at the end but I felt it was necessary to fully ask my question.<div>My SDL2 wrapper is progressing nicely, I can draw lines, rectangles, filled rectangles and load and render textures so far all without issue but....boiler plate.</div><div><br></div><div>For example. SDL_RenderCopy can be passed either a NULL or an &SDL_Rect to indicate the source and destination rectangles from the source texture and destination respectively allowing sprite sheets etc or image scaling to be performed.</div><div><br></div><div>However, with my limited knowledge I have only so far produced the code shown at the end and to be perfectly honest I think it stinks and is riddled with boiler plate. My question then is what would be "the best way" to handle this case. With two rectangles I have had to create four predicates, with three it would be 8 etc etc and it feels like there is a better way.</div><div><br></div><div>I have seen code that creates mercury predaites then exportds them so they can be called from "C" but I don't yet fully understand that as the code I read (<a href="https://github.com/FlyingJester/SDL2.m/blob/master/sdl2.m">https://github.com/FlyingJester/SDL2.m/blob/master/sdl2.m</a>) is a bit over my head for the moment!</div><div>This also comes back to my previous question about being able to use SDL_Rect as a foreign type etc. >From what I have learned so far I know that parameters are passed in as MR_Word sized values so would it be safe for example to cast an sdl_rect() declared as:</div><div><br></div><div><b><font face="courier new, monospace">:- type sdl_rect ---> sdl_rect(x::int, y::int, w::int, h::int).<br></font></b></div><div><br></div><div><font face="courier new, monospace"><b>typedef struct SDL_Rect<br>{<br>    int x, y;<br>    int w, h;<br>} SDL_Rect;<br></b></font></div><div><br></div><div>to an SDL_Rect which has the same size and shape?!?!?!?! These are thing things holding me back right now.</div><div><br></div><div>Thanks,</div><div>Sean.</div><div><br></div><div>---> the code</div><div><br></div><div><font face="courier new, monospace"><b>:- pred sdl_rendercopy(sdl_renderer::in, sdl_texture::in, maybe(sdl_rect)::in, maybe(sdl_rect)::in, io::di, io::uo) is det.<br></b></font></div><div><br><font face="courier new, monospace">    % SDL_RenderCopy<br>sdl_rendercopy(Rnd, Tex, no, no, !IO) :-<br>    sdl_rendercopy_no_no(Rnd, Tex, !IO).<br><br>sdl_rendercopy(Rnd, Tex, no, yes(Dst), !IO) :-<br>    sdl_rendercopy_no_yes(Rnd, Tex, Dst^x, Dst^y, Dst^w, Dst^h, !IO).<br><br>sdl_rendercopy(Rnd, Tex, yes(Src), no, !IO) :-<br>    sdl_rendercopy_yes_no(Rnd, Tex, Src^x, Src^y, Src^w, Src^h, !IO).<br><br>sdl_rendercopy(Rnd, Tex, yes(Src), yes(Dst), !IO) :-<br>    sdl_rendercopy_yes_yes(Rnd, Tex,<br>        Src^x, Src^y, Src^w, Src^h,<br>        Dst^x, Dst^y, Dst^w, Dst^h,<br>        !IO).<br><br><b>:- pred sdl_rendercopy_no_no(sdl_renderer::in, sdl_texture::in, io::di, io::uo) is det.<br>:</b>- pragma foreign_proc("C",<br>    sdl_rendercopy_no_no(Rnd::in, Tex::in, _IO0::di, _IO::uo),<br>    [promise_pure,will_not_call_mercury,does_not_affect_liveness],<br>    "SDL_RenderCopy(Rnd, Tex, NULL, NULL);").<br><br><b>:- pred sdl_rendercopy_no_yes(sdl_renderer::in, sdl_texture::in, int::in, int::in, int::in, int::in, io::di, io::uo) is det.<br></b>:- pragma foreign_proc("C",<br>    sdl_rendercopy_no_yes(Rnd::in, Tex::in, X::in, Y::in, W::in, H::in, _IO0::di, _IO::uo),<br>    [promise_pure,will_not_call_mercury,does_not_affect_liveness],<br>    "SDL_Rect rDst;<br>    rDst.x = (Uint32)X;<br>    rDst.y = (Uint32)Y;<br>    rDst.w = (Uint32)W;<br>    rDst.h = (Uint32)H;<br>    SDL_RenderCopy(Rnd, Tex, NULL, &rDst);").<br><br><b>:- pred sdl_rendercopy_yes_no(sdl_renderer::in, sdl_texture::in, int::in, int::in, int::in, int::in, io::di, io::uo) is det.<br></b>:- pragma foreign_proc("C",<br>    sdl_rendercopy_yes_no(Rnd::in, Tex::in, X::in, Y::in, W::in, H::in, _IO0::di, _IO::uo),<br>    [promise_pure,will_not_call_mercury,does_not_affect_liveness],<br>    "SDL_Rect rSrc;<br>    rSrc.x = (Uint32)X;<br>    rSrc.y = (Uint32)Y;<br>    rSrc.w = (Uint32)W;<br>    rSrc.h = (Uint32)H;<br>    SDL_RenderCopy(Rnd, Tex, &rSrc, NULL);").<br><br><b>:- pred sdl_rendercopy_yes_yes(sdl_renderer::in, sdl_texture::in, int::in, int::in, int::in, int::in,int::in, int::in, int::in, int::in, io::di, io::uo) is det.<br></b>:- pragma foreign_proc("C",<br>    sdl_rendercopy_yes_yes(Rnd::in, Tex::in,<br>        XS::in, YS::in, WS::in, HS::in,<br>        XD::in, YD::in, WD::in, HD::in,<br>        _IO0::di, _IO::uo),<br>    [promise_pure,will_not_call_mercury,does_not_affect_liveness],<br>    "SDL_Rect rDst;<br>    SDL_Rect rSrc;<br>    rSrc.x = (Uint32)XS;<br>    rSrc.y = (Uint32)YS;<br>    rSrc.w = (Uint32)WS;<br>    rSrc.h = (Uint32)HS;<br>    rDst.x = (Uint32)XD;<br>    rDst.y = (Uint32)YD;<br>    rDst.w = (Uint32)WD;<br>    rDst.h = (Uint32)HD;<br>    SDL_RenderCopy(Rnd, Tex, &rSrc, &rDst);").</font><br></div></div>