Class: RedBird::DynamicSprite

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

Overview

A sprite is rectangular part of an texture.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(texture, x, y, width, height, mods) ⇒ Object

Parameters:

  • texture (RedBird::Texture)
  • x (Integer)
  • y (Integer)
  • width (Integer)
  • height (Integer)
  • mods (Hash)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'ext/red_bird/dynamic_sprite.c', line 64

VALUE
bird_cDynamicSprite_initialize(VALUE self, VALUE texture, VALUE  x, VALUE  y,
                               VALUE  width, VALUE  height, VALUE mods)
{
  struct bird_dynamic_sprite_data *ptr;

  VALUE hash_val;

  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_dynamic_sprite_data,
                       &bird_dynamic_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);

  ptr->angle = 0;
  ptr->center.x = 0;
  ptr->center.y = 0;

  // Get 'flip'.
  ptr->flip = SDL_FLIP_NONE; // By default do not flip.
  hash_val = rb_hash_aref(mods, id_flip);
  if(SYMBOL_P(hash_val))
  {
    if(hash_val == id_vertical)
      ptr->flip = SDL_FLIP_VERTICAL;
    else if(hash_val == id_horizontal)
      ptr->flip = SDL_FLIP_HORIZONTAL;
    else if(hash_val == id_both)
      ptr->flip = SDL_FLIP_VERTICAL | SDL_FLIP_HORIZONTAL;
  }

  return self;
}

Class Method Details

.grid(texture, init_x, init_y, num_hor_sprites, num_ver_sprites, width, height, mods) ⇒ 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.

  • mods (Hash)

Returns:

  • (Array)

Author:

  • Frederico Linhares



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/red_bird/dynamic_sprite.rb', line 24

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

  return sprites
end

Instance Method Details

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

Render itself directly to the screen.

Parameters:

  • x (Integer)

    x coordinate of the screen.

  • y (Integer)

    y coordinate of the screen.

Returns:

Author:

  • Frederico Linhares



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'ext/red_bird/dynamic_sprite.c', line 117

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

  struct bird_dynamic_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_dynamic_sprite_data,
                       &bird_dynamic_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_RenderCopyEx(bird_core.renderer, texture_ptr->data, &ptr->rect,
                   &dst_rect, ptr->angle, &ptr->center, ptr->flip);

  return self;
}