NGN_SPRITE.H

METHODS of the class


Method

NGN_Sprite(
    NGN_SpriteData* sprite,                     // Sprite data
    int32_t position_x = DEFAULT_VALUE,         // Initial X position (hidden by default)
    int32_t position_y = DEFAULT_VALUE,         // Initial Y position (hidden by default)
    uint32_t sprite_width = DEFAULT_VALUE,      // Sprite width (default is frame width)
    uint32_t sprite_height = DEFAULT_VALUE,     // Sprite height (default is frame height)
    uint32_t box_width = DEFAULT_VALUE,         // Collision box width (default is sprite width)
    uint32_t box_height = DEFAULT_VALUE,        // Collision box height (default is sprite height)
    int32_t box_offset_x = NGN_DEFAULT_VALUE,   // Horizontal offset of the collision box
    int32_t box_offset_y = NGN_DEFAULT_VALUE    // Vertical offset of the collision box
);

Description

Creates a new Sprite using previously loaded data.

Example

NGN_Sprite* player = new NGN_Sprite(wizard_sprite, 100, 200);
NGN_Sprite* player2 = new NGN_Sprite(wizard_sprite, 300, 300, 256, 256, 160, 256);

Method

void Position(float position_x, float position_y);
void Position(Vector2 pos);

Description

Positions the sprite at the given coordinates.

Example

player->Position(1200, 900);

Method

void Translate(float speed_x, float speed_y);
void Translate(Vector2 spd);

Description

Moves the sprite in the given direction and speeds.

Example

player->Translate(5.0f, 1.0f);

Method

void Size(float w, float h);

Description

Changes the size of the sprite.

Example

player->Size(64.0f, 48.0f);

Method

void Scale(float w, float h);
void Scale(float scale);

Description

Scales the sprite by the given factor. Depending on the overload used, it will scale the axes together or separately. The default scale is 1.0f.

Example

player->Scale(1.5f);
player->Scale(2.0f, 0.75f);

Method

void Rotate(double degrees);

Description

Rotates the sprite by the provided number of degrees.

Example

player->Rotate(1.2f);

Method

void SetCenter(float x, float y);

Description

Specifies, in relative coordinates from the actual center of the sprite, where the rotation center of the sprite will be located.

Example

player->SetCenter(-10, -5);

Method

int32_t AddCollider(
    std::string name,   // Collider name (must be unique)
    float offset_x,     // Offset X relative to the actual center of the sprite
    float offset_y,     // Offset Y relative to the actual center of the sprite
    float width,        // Collider width
    float height        // Collider height
);

Description

Adds an additional collider to the sprite with the specified name, size, and position. As many additional colliders as needed can be added. These colliders will be automatically detected by the "HitBox" instruction in the "NGN_Collisions" class. The rotation or scaling of the sprite does not affect these colliders. This function returns 0 if the collider is added successfully.

Example

player->AddCollider("bottom", -16.0f, 80.0f, 32.0f, 96.0f);
player->AddCollider("left", -64.0f, -80.0f, 64.0f, 32.0f);

Method

int32_t GetColliderId(std::string name);

Description

Returns the ID (position) in the collider vector of the collider with the given name. If not found, it returns -1.

Example

Int32_t id = player->GetColliderId(“left”);

Method

int32_t ColliderEnabled(std::string name, bool status);

Description

Enables or disables the collider with the given name. This function returns 0 if the state change is successful.

Example

player->ColliderEnabled(“left”, false);

Method

int32_t RemoveCollider(std::string name);

Description

Removes the collider with the specified name. If successful, this function returns 0.

Example

player->RemoveCollider(“left”);

Method

int32_t AddAnimation(
    std::string name,           // Animation name
    uint32_t first_frame,       // First frame of the animation
    uint32_t last_frame,        // Last frame of the animation
    uint32_t loop,              // "Loop point" frame
    uint32_t frame_duration     // Duration in cycles for each frame
);

Description

Adds a new animation to the created Sprite. It is possible to create animations with inverse kinematics. Returns 1 in case of an error.

Example

player->AddAnimation(“walk”, 1, 7, 1, 5);
player->AddAnimation(“moonwalk”, 7, 1, 7, 8);
player->AddAnimation(“run”, 0, 20, 8, 3);

Method

int32_t SetAnimation(std::string name = "");

Description

Selects an animation with the given name for playback. If the currently selected animation is requested, the command is ignored and it returns 2. In case of an error, it returns 1.

Example

player->SetAnimation(“walk”);

Method

void PlayAnimation();

Description

Plays the currently selected animation. Must be called once per cycle to update the animation. If using the 2D Camera from the library, calling this method for animation playback is not necessary if the sprite is registered with the camera.

Example

player->PlayAnimation();

Method

void SetTintColor(uint8_t r = 0xFF, uint8_t g = 0xFF, uint8_t b = 0xFF);

Description

Sets a tint color that will be applied to the sprite. A white tint (255, 255, 255) will display the sprite with its original unaltered colors.

Example

player->SetTintColor(96, 255, 192);

PROPERTIES of the class


Property

Vector2 position
Vector2 screen

Description

Sprite position (global or on the screen).

Property

float width
float height

Description

Sprite size.

Property

float box.width
float box.height
float box.offset.x
float box.offset.y
bool box_enabled

Description

Properties of the main collision box of the sprite. It can be enabled or disabled using the "box_enabled" flag.

Property

int32_t frame

Description

Sprite frame to be displayed (The first frame is 0).

Property

int32_t total_frames

Description

Its value indicates the total number of frames the sprite contains. This property should not be modified under any circumstances.

Property

string current_animation.id
uint32_t current_animation.first_frame
uint32_t current_animation.last_frame
uint32_t current_animation.loop
uint32_t current_animation.frame_duration

Description

Parameters of the current animation. These properties should not be modified under any circumstances.

Property

bool animation_pause

Description

If the value of this property is TRUE, the current animation is paused.

Property

bool visible

Description

Sets the visibility of the sprite.

Property

int32_t alpha

Description

Sprite transparency level, ranging from 0 to 255.

Property

SDL_BlendMode blend_mode

Description

Sprite color blend mode. Available modes are: NGN_BLENDMODE_NONE, NGN_BLENDMODE_ALPHA, NGN_BLENDMODE_ADDITIVE, and NGN_BLENDMODE_MODULATE. The default value for this property is NGN_BLENDMODE_ALPHA.

Property

bool on_screen

Description

Flag indicating whether the sprite is on the screen or not. If the sprite is registered with the 2D Camera, this flag will be updated automatically. Otherwise, it should be updated manually.

Property

double rotation

Description

Sprite rotation, in degrees.

Property

bool flip_h
bool flip_v

Description

Horizontal and vertical flipping of the sprite.

Property

bool ignore_camera_tint

Description

Indicates whether the sprite should ignore the tint color assigned to the camera layer.

Back to Index Versión en Español Back to Top