Class: RedBird::Text

Inherits:
Data
  • Object
show all
Defined in:
ext/red_bird/text.c,
ext/red_bird/text.c

Overview

A text to be printed on the screen. This class renders the text received during the initialization as an image and store the image internally for rendering.

Author:

  • Frederico Linhares

Instance Method Summary collapse

Constructor Details

#initialize(text, font, tx_color, bg_color) ⇒ Object

Parameters:

  • text (String)

    text to rendered.

  • font (RedBird::Font)

    font to be used for rendering the text.

  • tx_color (RedBird::Color)

    text color.

  • bg_color (RedBird::Color, nil)

    if the attribute is a color, this color will be used as background; if the attribute is nil, there will be no background.

Author:

  • Frederico Linhares



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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'ext/red_bird/text.c', line 68

VALUE
bird_cText_initialize(VALUE self, VALUE text, VALUE font, VALUE tx_color,
                      VALUE bg_color)
{
  SDL_Surface *surface = NULL;

  struct bird_text_data *ptr;
  struct bird_font_data *font_ptr;
  struct bird_color_data *tx_color_ptr;
  struct bird_color_data *bg_color_ptr;

  if(!engine_initialized)
    rb_raise(rb_eRuntimeError, "%s",
             "can not create a RedBird::Text instance before RedBird::Engine "
             "is started");

  SafeStringValue(text);

  if(!rb_obj_is_kind_of(font, bird_cFont))
  {
    rb_raise(rb_eTypeError, "%s", "font must be an instance of RedBird::Font");
    return self;
  }

  if(!rb_obj_is_kind_of(tx_color, bird_cColor))
  {
    rb_raise(rb_eTypeError, "%s",
             "tx_color must be an instance of RedBird::Color");
    return self;
  }

  font_ptr = bird_cFont_get_data(font);
  tx_color_ptr = bird_cColor_get_data(tx_color);
  if(TYPE(bg_color) == T_NIL)
  {
    surface = TTF_RenderUTF8_Solid(font_ptr->data, StringValueCStr(text),
                                   tx_color_ptr->data);
  }
  else if(rb_obj_is_kind_of(bg_color, bird_cColor))
  {
    bg_color_ptr = bird_cColor_get_data(bg_color);
    surface = TTF_RenderUTF8_Shaded(font_ptr->data, StringValueCStr(text),
                                    tx_color_ptr->data, bg_color_ptr->data);
  }
  else
  {
    rb_raise(rb_eTypeError, "%s",
             "bg_color must be an instance of RedBird::Color or nil");
    return self;
  }

  if(surface == NULL)
  {
    rb_raise(rb_eRuntimeError, "failed to render text: %s", TTF_GetError());
    return self;
  }

  TypedData_Get_Struct(self, struct bird_text_data, &bird_text_type, ptr);

  ptr->data = NULL;
  ptr->data = SDL_CreateTextureFromSurface(bird_core.renderer, surface);
  SDL_FreeSurface(surface);
  if(ptr->data == NULL)
  {
    rb_raise(rb_eRuntimeError, "failed to convert text: %s", SDL_GetError());
    return self;
  }

  SDL_QueryTexture(ptr->data, NULL, NULL, &ptr->width, &ptr->height);
  return self;
}

Instance Method Details

#heightInteger

Returns the height of the image.

Returns:

  • (Integer)

    text height.

Author:

  • Frederico Linhares



162
163
164
165
166
167
168
169
170
# File 'ext/red_bird/text.c', line 162

VALUE
bird_cText_height(VALUE self)
{
  struct bird_text_data *ptr;

  TypedData_Get_Struct(self, struct bird_text_data, &bird_text_type, ptr);

  return INT2FIX(ptr->height);
}

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

Render this text to the screen.

Parameters:

  • x (Integer)

    screen horizontal position.

  • y (Integer)

    screen vertical position.

Returns:

Author:

  • Frederico Linhares



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'ext/red_bird/text.c', line 180

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

  struct bird_text_data *ptr;

  RB_INTEGER_TYPE_P(x);
  RB_INTEGER_TYPE_P(y);

  TypedData_Get_Struct(self, struct bird_text_data, &bird_text_type, ptr);

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

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

  return self;
}

#widthInteger

Returns the width of the image.

Returns:

  • (Integer)

    text width.

Author:

  • Frederico Linhares



146
147
148
149
150
151
152
153
154
# File 'ext/red_bird/text.c', line 146

VALUE
bird_cText_width(VALUE self)
{
  struct bird_text_data *ptr;

  TypedData_Get_Struct(self, struct bird_text_data, &bird_text_type, ptr);

  return INT2FIX(ptr->width);
}