Class: RedBird::Sprite
- Inherits:
-
Data
- Object
- Data
- RedBird::Sprite
- 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.
Class Method Summary collapse
-
.grid(texture, init_x, init_y, num_hor_sprites, num_ver_sprites, width, height) ⇒ Array
Create an array of sprites and return it.
Instance Method Summary collapse
-
#initialize(texture, x, y, width, height) ⇒ RedBird::Sprite
constructor
Self.
-
#render_to_screen(x, y) ⇒ RedBird::Sprite
Render this sprite to the screen.
Constructor Details
#initialize(texture, x, y, width, height) ⇒ RedBird::Sprite
Returns self.
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
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.
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;
}
|