Class OcsfmlGraphics.sprite

class sprite : ?texture:#const_texture -> ?position:float * float -> ?scale:float * float -> ?rotation:float -> ?origin:float * float -> ?color:Color.t -> ?texture_rect:IntRect.t -> unit -> object .. end
Drawable representation of a texture, with its own transformations, color, etc.

sprite is a drawable class that allows to easily display a texture (or a part of it) on a render target.

It inherits all the functions from sf::Transformable: position, rotation, scale, origin. It also adds sprite-specific properties such as the texture to use, the part of it to display, and some convenience functions to change the overall color of the sprite, or to get its bounding rectangle.

sprite works in combination with the OcsfmlGraphics.texture class, which loads and provides the pixel data of a given texture.

The separation of sf::Sprite and sf::Texture allows more flexibility and better performances: indeed a sf::Texture is a heavy resource, and any operation on it is slow (often too slow for real-time applications). On the other side, a sf::Sprite is a lightweight object which can use the pixel data of a sf::Texture and draw it with its own transformation/color/blending attributes.

It is important to note that the sf::Sprite instance doesn't copy the texture that it uses, it only keeps a reference to it. Thus, a OcsfmlGraphics.texture must not be destroyed while it is used by a sprite (i.e. never write a function that uses a local sf::Texture instance for creating a sprite).

Usage example:

    (* Declare and load a texture *)
    let texture = new texture (`File "texture.png"in
    
    (* Create a sprite *)
    let texture_rect = { top = 10 ; left = 10 ; width = 50 ; height = 30 }
    let sprite = new sprite ~texture 
                            ~texture_rect
                            ~color:(Color.rgba 255 255 255 200)
                            ~position:(100., 25.) () 
    in
    
    (* Draw it *)
    window#draw sprite
    

Inherits
method destroy : unit
method get_color : Color.t
Get the global color of the sprite.
Returns Global color of the sprite.
method get_global_bounds : FloatRect.t
Get the global bounding rectangle of the entity.

The returned rectangle is in global coordinates, which means that it takes in account the transformations (translation, rotation, scale, ...) that are applied to the entity. In other words, this function returns the bounds of the sprite in the global 2D world's coordinate system.
Returns Global bounding rectangle of the entity.

method get_local_bounds : FloatRect.t
Get the local bounding rectangle of the entity.

The returned rectangle is in local coordinates, which means that it ignores the transformations (translation, rotation, scale, ...) that are applied to the entity. In other words, this function returns the bounds of the entity in the entity's coordinate system.
Returns Local bounding rectangle of the entity.

method get_texture : const_texture reference option
Get the source texture of the sprite.

If the sprite has no source texture, none is returned. The returned value is const, which means that you can't modify the texture when you retrieve it with this function.
Returns The sprite's texture.

method get_texture_rect : IntRect.t
Get the sub-rectangle of the texture displayed by the sprite.
Returns Texture rectangle of the sprite.
method set_color : Color.t -> unit
Set the global color of the sprite.

This color is modulated (multiplied) with the sprite's texture. It can be used to colorize the sprite, or change its global opacity. By default, the sprite's color is opaque white.

method set_texture : 'a. ?resize:bool -> (#const_texture as 'a) -> unit
Change the source texture of the sprite.

The texture argument refers to a texture that must exist as long as the sprite uses it. Indeed, the sprite doesn't store its own copy of the texture, but rather keeps a pointer to the one that you passed to this function. If the source texture is destroyed and the sprite tries to use it, the behaviour is undefined. If resetRect is true, the TextureRect property of the sprite is automatically adjusted to the size of the new texture. If it is false, the texture rect is left unchanged.

method set_texture_rect : IntRect.t -> unit
Set the sub-rectangle of the texture that the sprite will display.

The texture rect is useful when you don't want to display the whole texture, but rather a part of it. By default, the texture rect covers the entire texture.