Closed aryx closed 3 years ago
Something like this? https://gist.github.com/fccm/f537701f7da8a413b14c25feb93bac4b
run it with:
ocaml -I ../../OCamlSDL2/src sdl2.cma sdl2-ba.cma -I $(ocamlfind query cairo2) cairo.cma ex_cairo.ml
It seems quite slow though, you will maybe be more lucky wrapping the SDL2_gfx
lib:
/usr/lib/libSDL2_gfx.so
/usr/include/SDL2/SDL2_gfxPrimitives.h
You can take example of the "accompanying libraries":
I just made a template bindings for the SDL2_gfx
lib:
You can easily add the functions you need.
Merci beaucoup florent!
In the end someone on discuss.ocaml.org answered my question via some existing code he wrote: https://discuss.ocaml.org/t/ocaml-native-library-to-draw-scale-rotate-shapes-on-screen/6706/10?u=aryx and even proposed a faster implementation using Cairo.Image.create_for_data32 which avoids some extra copy. It's very fast. https://github.com/igarnier/vplot/blob/master/lib/display.ml
I think you should add a similar example in your SDL binding; it's a big limitation of SDL do not be able to rotate or have powerful drawing like in Cairo, and the ability to mix easily cairo and SDL through Cairo.Image.create_for_data32 is very nice.
Of course you do can scale and rotate with SDL2!
In the example I provided please have a look at lines 95 and 96, the parameter named angle
is the angle to rotate, and I even put a comment (* rotate *)
.
Use the function Render.copyEx
to rotate and scale:
https://fccm.github.io/OCamlSDL2/Sdlrender.html#VALcopyEx
I also put a comment at line 92 to explain how to scale:
(* Use rectangles of different width and height to scale *)
Oh thx, but this seems still a bit more tedious than the draw_rect, draw_ellipse, draw_line, draw_text that Cairo provides that can be rotated/scaled/translated in many ways.
Then switching to ocaml-sfml could be a good choice. It provides all these functions that you need in a native way.
I would like to draw shapes on a screen, but I also need to rotate them, scale them, etc. and it looks like SDL by itself does not provide this functionality. I know Cairo which can do all of that but I need some toy example showing how to mix cairo and SDL code together.
For reference, here is some example showing how to mix Cairo with the Graphics library: https://github.com/Chris00/ocaml-cairo/blob/master/examples/graphics_demo.ml
Would be great to have something similar but for SDL! That way, as advertised here: https://www.cairographics.org/SDL/ we can mix the best of both worlds (cairo for advanced drawing and SDL for cross-platform game platform).