Class: RedBird::Texture

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

Overview

An image optimized for rendering on the screen.

Instance Method Summary collapse

Constructor Details

#initialize(file_path, palette) ⇒ Object

Parameters:

  • path to an image to be loaded into a texture.



61
62
63
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
# File 'ext/red_bird/texture.c', line 61

VALUE
bird_cTexture_initialize(VALUE self, VALUE file_path, VALUE palette)
{
  struct bird_texture_data *ptr;
  struct bird_palette_data *palette_data;
  struct bird_cTexture_PGM pgm_img;
  SDL_Surface *surface = NULL;

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

  if(!rb_obj_is_kind_of(palette, bird_cPalette))
    rb_raise(
        rb_eArgError, "%s",
        "palette must be and instance of RedBird::Palette");

  SafeStringValue(file_path);
  if(!bird_cTexture_PGM_load(&pgm_img, StringValueCStr(file_path)))
    rb_raise(
        rb_eArgError, "%s",
        "failed to load image: it does not exists or is in an invalid format");

  // Depth = 8 bits image.
  // Pitch = 1 byte (8 bits) * image width.
  surface = SDL_CreateRGBSurfaceWithFormatFrom(
      pgm_img.data, pgm_img.width, pgm_img.height, 8, pgm_img.width,
      SDL_PIXELFORMAT_INDEX8);
  bird_cTexture_PGM_unload(&pgm_img);
  if(surface == NULL)
    rb_raise(rb_eArgError, "failed to create surface: %s\n", SDL_GetError());

  TypedData_Get_Struct(self, struct bird_texture_data, &bird_texture_type,
                       ptr);
  palette_data = bird_cPalette_get_data(palette);
  SDL_SetPaletteColors(surface->format->palette, palette_data->colors, 0, 256);

  ptr->data = SDL_CreateTextureFromSurface(bird_core.renderer, surface);
  SDL_FreeSurface(surface);
  if(ptr->data == NULL)
    rb_raise(rb_eArgError, "failed to convert image: %s\n", SDL_GetError());

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

Instance Method Details

#heightInteger

Returns:



127
128
129
130
131
132
133
134
135
136
# File 'ext/red_bird/texture.c', line 127

VALUE
bird_cTexture_height(VALUE self)
{
  struct bird_texture_data *ptr;

  TypedData_Get_Struct(self, struct bird_texture_data, &bird_texture_type,
                       ptr);

  return INT2FIX(ptr->height);
}

#widthInteger

Returns:



112
113
114
115
116
117
118
119
120
121
# File 'ext/red_bird/texture.c', line 112

VALUE
bird_cTexture_width(VALUE self)
{
  struct bird_texture_data *ptr;

  TypedData_Get_Struct(self, struct bird_texture_data, &bird_texture_type,
                       ptr);

  return INT2FIX(ptr->width);
}