Class: RedBird::Sprite

Inherits:
Data
  • Object
show all
Defined in:
lib/red_bird/sprite.rb,
ext/red_bird/sprite.c

Overview

A rectangular part of a texture. It allows you to print only a portion of the image instead of it all. This will, for example, enable you to insert several animation frames into the same texture instead of having to load several small files.

Author:

  • Frederico Linhares

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(texture, x, y, width, height) ⇒ RedBird::Sprite

Returns self.

Parameters:

  • texture (RedBird::Texture)

    image to use as source for this sprite.

  • x (Integer)

    vertical position in the texture.

  • y (Integer)

    horizontal position in the texture.

  • width (Integer)

    width of the sprite.

  • height (Integer)

    height of the sprite.

Author:

  • Frederico Linhares



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'ext/red_bird/sprite.c', line 59

VALUE
bird_cSprite_initialize(VALUE self, VALUE texture, VALUE  x, VALUE  y,
                        VALUE  width, VALUE  height)
{
  struct bird_sprite_data *ptr;

  if(!rb_obj_is_kind_of(texture, bird_cTexture))
    rb_raise(rb_eTypeError, "%s",
             "texture must be an instance of RedBird::Texture");
  RB_INTEGER_TYPE_P(x);
  RB_INTEGER_TYPE_P(y);
  RB_INTEGER_TYPE_P(width);
  RB_INTEGER_TYPE_P(height);

  TypedData_Get_Struct(self, struct bird_sprite_data, &bird_sprite_type, ptr);

  rb_ivar_set(self, id_at_texture, texture);
  ptr->rect.x = NUM2INT(x);
  ptr->rect.y = NUM2INT(y);
  ptr->rect.w = NUM2INT(width);
  ptr->rect.h = NUM2INT(height);

  return self;
}

Class Method Details

.grid(texture, init_x, init_y, num_hor_sprites, num_ver_sprites, width, height) ⇒ Array

Create an array of sprites and return it. The array has homogeneous sprites that do not overlap and have no gaps between them.

To create a grid starting from the position x 20, y 20, containing three columns and two rows, each sprite, having 100x80 pixels:

RedBird::Texture.grid sprite, 20, 20, 3, 2, 100, 80

Parameters:

  • texture (RedBird::Texture)

    a texture that will be used as the source for all sprites.

  • init_x (Integer)

    initial x coordinate for the grid.

  • init_y (Integer)

    initial y coordinate for the grid.

  • num_hor_sprites (Integer)

    number of columns the grid will have.

  • num_ver_sprites (Integer)

    number of rows the grid will have.

  • width (Integer)

    width of every sprite.

  • height (Integer)

    height of every sprite.

Returns:

  • (Array)

Author:

  • Frederico Linhares



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/red_bird/sprite.rb', line 27

def self.grid(texture, init_x, init_y, num_hor_sprites, num_ver_sprites,
              width, height)
  sprites = []
  num_ver_sprites.times do |v|
    num_hor_sprites.times do |h|
      sprites << Sprite.new(
        texture, init_x + h * width, init_y + v * height, width, height)
    end
  end

  return sprites
end

Instance Method Details

#render_to_screen(x, y) ⇒ RedBird::Sprite

Render this sprite to the screen.

Parameters:

  • x (Integer)

    screen horizontal position.

  • y (Integer)

    screen vertical position.

Returns:

Author:

  • Frederico Linhares



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'ext/red_bird/sprite.c', line 92

VALUE
bird_cSprite_render_to_screen(VALUE self, VALUE  x, VALUE  y)
{
  SDL_Rect dst_rect;

  struct bird_sprite_data *ptr;
  struct bird_texture_data *texture_ptr;

  VALUE texture;

  RB_INTEGER_TYPE_P(x);
  RB_INTEGER_TYPE_P(y);

  texture = rb_ivar_get(self, id_at_texture);

  TypedData_Get_Struct(self, struct bird_sprite_data, &bird_sprite_type, ptr);
  texture_ptr = bird_cTexture_get_data(texture);

  dst_rect.x = NUM2INT(x);
  dst_rect.y = NUM2INT(y);
  dst_rect.w = ptr->rect.w;
  dst_rect.h = ptr->rect.h;

  SDL_RenderCopy(bird_core.renderer, texture_ptr->data, &ptr->rect,
                 &dst_rect);

  return self;
}