Class OcsfmlGraphics.render_target

class render_target : RenderTarget.t -> object .. end
Base class for all render targets (window, texture, ...)

OcsfmlGraphics.render_target defines the common behaviour of all the 2D render targets usable in the graphics module.

It makes it possible to draw 2D entities like sprites, shapes, text without using any OpenGL command directly.

A OcsfmlGraphics.render_target is also able to use views (OcsfmlGraphics.view), which are a kind of 2D cameras. With views you can globally scroll, rotate or zoom everything that is drawn, without having to transform every single entity. See the documentation of OcsfmlGraphics.view for more details and sample pieces of code about this class.

On top of that, render targets are still able to render direct OpenGL stuff. It is even possible to mix together OpenGL calls and regular SFML drawing commands. When doing so, make sure that OpenGL states are not messed up by calling the push_gl_states/pop_gl_states methods.

method clear : ?color:Color.t -> unit -> unit
Clear the entire target with a single color.

This function is usually called once every frame, to clear the previous contents of the target.

method map_pixel_to_coords : 'a. ?view:(#const_view as 'a) -> int * int -> float * float
Convert a point from target coordinates to view coordinates.

Initially, a unit of the 2D world matches a pixel of the render target. But if you define a custom view, this assertion is not true anymore, ie. a point located at (10, 50) in your render target (for example a window) may map to the point (150, 75) in your 2D world -- for example if the view is translated by (140, 25).

For render windows, this function is typically used to find which point (or object) is located below the mouse cursor.
Returns The converted point, in "world" units

view : The view to use for converting the point (default is the current view of the render_target)
method map_coords_to_pixel : 'b. ?view:(#const_view as 'b) -> float * float -> int * int
method destroy : unit
method draw : 'c.
?blend_mode:BlendMode.t ->
?transform:transform ->
?texture:texture ->
?shader:shader ->
(< rep__sf_Drawable : Drawable.t; .. > as 'c) -> unit
Draw a drawable object to the render-target.
method get_default_view : const_view reference
Get the default view of the render target.

The default view has the initial size of the render target, and never changes after the target has been created.
Returns The default view of the render target.

method get_size : int * int
Return the size of the rendering region of the target.
Returns Size in pixels.
method get_view : const_view reference
Get the view currently in use in the render target.
Returns The view object that is currently used.
method get_viewport : 'd. (#const_view as 'd) -> IntRect.t
Get the viewport of a view, applied to this render target.

The viewport is defined in the view as a ratio, this function simply applies this ratio to the current dimensions of the render target to calculate the pixels rectangle that the viewport actually covers in the target.
Returns Viewport rectangle, expressed in pixels

method pop_gl_states : unit
Restore the previously saved OpenGL render states and matrices.

See the description of pushGLStates to get a detailed description of these functions.

method push_gl_states : unit
Save the current OpenGL render states and matrices.

This function can be used when you mix SFML drawing and direct OpenGL rendering. Combined with pop_gl_states, it ensures that:

More specifically, it must be used around code that calls Draw functions. Example:
      (* OpenGL code here... *)
      window#draw (* ... *);
      window#draw (* ... *);
      (* OpenGL code here... *)

Note that this function is quite expensive: it saves all the possible OpenGL states and matrices, even the ones you don't care about. Therefore it should be used wisely. It is provided for convenience, but the best results will be achieved if you handle OpenGL states yourself (because you know which states have really changed, and need to be saved and restored). Take a look at the ResetGLStates function if you do so.

method reset_gl_states : unit
Reset the internal OpenGL states so that the target is ready for drawing.

This function can be used when you mix SFML drawing and direct OpenGL rendering, if you choose not to use pushGLStates/popGLStates. It makes sure that all OpenGL states needed by SFML are set, so that subsequent draw calls will work as expected.


  (* OpenGL code here... *)
      glPushAttrib (* ... *) ;
      window#reset_gl_states ;
      window#draw (* ... *) ;
      window#draw (* ... *) ;
      glPopAttrib (* ... *) ;
      // OpenGL code here...

method set_view : 'e. (#const_view as 'e) -> unit
Change the current active view.

The view is like a 2D camera, it controls which part of the 2D scene is visible, and how it is viewed in the render-target. The new view will affect everything that is drawn, until another view is set. The render target keeps its own copy of the view object, so it is not necessary to keep the original one alive after calling this function. To restore the original view of the target, you can pass the result of get_default_view to this function.