Class: Ray::Image

Inherits:
Object
  • Object
show all
Extended by:
ResourceSet
Includes:
Enumerable
Defined in:
lib/ray/image.rb,
ext/image.c

Constant Summary collapse

FLAG_ANYFORMAT =
INT2FIX(SDL_ANYFORMAT)
FLAG_ASYNCBLIT =
INT2FIX(SDL_ASYNCBLIT)
FLAG_DOUBLEBUF =
INT2FIX(SDL_DOUBLEBUF)
FLAG_HWPALETTE =
INT2FIX(SDL_HWPALETTE)
FLAG_HWACCEL =
INT2FIX(SDL_HWACCEL)
FLAG_HWSURFACE =
INT2FIX(SDL_HWSURFACE)
FLAG_FULLSCREEN =
INT2FIX(SDL_FULLSCREEN)
FLAG_OPENGL =
INT2FIX(SDL_OPENGL)
FLAG_OPENGLBLIT =
INT2FIX(SDL_OPENGLBLIT)
FLAG_RESIZABLE =
INT2FIX(SDL_RESIZABLE)
FLAG_RLEACCEL =
INT2FIX(SDL_RLEACCEL)
FLAG_SRCALPHA =
INT2FIX(SDL_SRCALPHA)
FLAG_SRCCOLORKEY =
INT2FIX(SDL_SRCCOLORKEY)
FLAG_PREALLOC =
INT2FIX(SDL_PREALLOC)

Instance Method Summary collapse

Methods included from ResourceSet

add_set, missing_pattern, need_argument_count, reject!, required_argument_count, select!, set_hash

Constructor Details

#initialize(hash) ⇒ Object #initialize(filename) ⇒ Object #initialize(io) ⇒ Object

Creates a new image.

Overloads:

  • #initialize(hash) ⇒ Object

    Options Hash (hash):

    • :width (Integer)

      Width of the surface

    • :height (Integer)

      Height of the surface

    • :w (Integer)

      Alias for width

    • :h (Integer)

      Alias for height

    • :bits_per_pixel (Integer)

      See Ray.create_window

    • :bpp (Integer)

      Alias for bits_per_pixel

    • :hw_surface (true, false)

      See Ray.create_window

    • :sw_surface (true, false)

      See Ray.create_window

  • #initialize(filename) ⇒ Object

    Loads the image from a file.

    Parameters:

    • filename (String, #to_str)

      The name of the file to open

  • #initialize(io) ⇒ Object

    Loads the image friom an IO object.

    Parameters:

    • io (IO, #read)

      Object the data will be loaded from.



126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'ext/image.c', line 126

VALUE ray_init_image(VALUE self, VALUE arg) {
   if (RAY_IS_A(arg, rb_cHash))
      ray_init_image_with_hash(self, arg);
   else if (rb_respond_to(arg, RAY_METH("to_str")))
      ray_init_image_with_filename(self, rb_String(arg));
   else if (rb_respond_to(arg, RAY_METH("read")))
      ray_init_image_with_io(self, arg);
   else {
      rb_raise(rb_eTypeError, "Can't convert %s into Hash",
               RAY_OBJ_CLASSNAME(arg));
   }

   return Qnil;
}

Instance Method Details

#==(obj) ⇒ true, false

Returns true if obj manipulates the same surface as the receiver.

Returns:

  • (true, false)

    true if obj manipulates the same surface as the receiver.



361
362
363
364
365
366
367
368
369
# File 'ext/image.c', line 361

VALUE ray_image_is_equal(VALUE self, VALUE obj) {
   if (!RAY_IS_A(obj, ray_cImage))
      return Qfalse;

   SDL_Surface *first_surface = ray_rb2surface(self);
   SDL_Surface *sec_surface = ray_rb2surface(obj);

   return (first_surface == sec_surface) ? Qtrue : Qfalse;
}

#[](x, y) ⇒ Ray::Color?

Returns Pixel at (x, y). Nil if the point is outside the image.

Returns:

  • (Ray::Color, nil)

    Pixel at (x, y). Nil if the point is outside the image.



407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# File 'ext/image.c', line 407

VALUE ray_image_at(VALUE self, VALUE rb_x, VALUE rb_y) {
   SDL_Surface *surface = ray_rb2surface(self);

   int x = NUM2INT(rb_x);
   int y = NUM2INT(rb_y);

   /* (w, h) is not a valid point. Surfaces use 0-based indexing. */
   if (x >= surface->w || y >= surface->h)
      return Qnil;

   int bytes = surface->format->BytesPerPixel;
   
   uint8_t *pix = (uint8_t*)surface->pixels + y * surface->pitch + x * bytes;
   
   uint32_t res;
   switch (bytes) {
      case 1:
         res = *pix;
         break;
      case 2:
         res = *(uint16_t*)pix;
         break;
      case 3:
         if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
            res = pix[0] << 16 | pix[1] << 8 | pix[2];
         else
            res = pix[0] | pix[1] << 8 | pix[2] << 16;
         break;
      case 4:
         res = *(uint32_t*)pix;
         break;
      default: /* should never happen */
         res = 0;
         break;
   }

   ray_color col;
   SDL_GetRGBA(res, surface->format, &(col.r), &(col.g), &(col.b), &(col.a));
   
   return ray_col2rb(col);
}

#[](x, y, color) ⇒ Object

Sets the color of the point at (x, y)

Raises:

  • ArgumentError If (x, y) is outside the image.



454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
# File 'ext/image.c', line 454

VALUE ray_image_set_at(VALUE self, VALUE rb_x, VALUE rb_y, VALUE rb_col) {
   SDL_Surface *surface = ray_rb2surface(self);
      
   int x = NUM2INT(rb_x);
   int y = NUM2INT(rb_y);

   if (x >= surface->w || y >= surface->h) {
      VALUE inspect = rb_inspect(self);
      rb_raise(rb_eArgError, "(%d, %d) is outside %s",
               x, y, StringValuePtr(inspect));
   }

   int bytes = surface->format->BytesPerPixel;
   
   uint8_t *pix = (uint8_t*)surface->pixels + y * surface->pitch + x * bytes;
   
   ray_color col = ray_rb2col(rb_col);

   uint32_t val = SDL_MapRGBA(surface->format, col.r, col.g, col.b, col.a);

   switch (bytes) {
      case 1:
         *pix = val;
         break;
      case 2:
         *(uint16_t*)pix = val;
         break;
      case 3:
         if (SDL_BYTEORDER == SDL_BIG_ENDIAN) {
            pix[0] = (val >> 16) & 0xff;
            pix[1] = (val >> 8) & 0xff;
            pix[2] = val & 0xff;
         }
         else {
            pix[0] = val & 0xff;
            pix[1] = (val >> 8) & 0xff;
            pix[2] = (val >> 16) & 0xff;
         }
         break;
      case 4:
         *(uint32_t*)pix = val;
         break;
   }

   return rb_col;
}

#alpha=(alpha) ⇒ Object

Sets the alpha transparency.

Parameters:

  • alpha (Integer, 0..255)

    the new transparency



323
324
325
326
327
# File 'ext/image.c', line 323

VALUE ray_image_set_alpha(VALUE self, VALUE alpha) {
   SDL_SetAlpha(ray_rb2surface(self), SDL_SRCALPHA | SDL_RLEACCEL,
                NUM2INT(alpha));
   return alpha;
}

#blit(hash) ⇒ Object Also known as: draw

Blits the receiver on another image.

Options Hash (hash):

  • :at (Ray::Rect, Array)

    Rects in which the image will be drawn. If an array is given, it passed to Ray::Rect.new. Only the position is read.

  • :rect (Ray::Rect, Array)

    Rects that will be copied. If an array is given, it passed to Ray::Rect.new. If the size is (0, 0), it will be reset to the image’s size.

  • :from (Ray::Rect, Array)

    Alias for rect

  • :on (Ray::Image, required)

    The image on which the receiver should be drawn.

  • :to (Ray::Image, required)

    Alias for on.

  • :angle (Float)

    Rotation in degrees.

  • :zoom (Float)

    Zoom level. 1.0 for the current size.



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'ext/image.c', line 234

VALUE ray_image_blit(VALUE self, VALUE hash) {
   SDL_Surface *origin = ray_rb2surface(self);

   SDL_Rect from_rect = {0, 0, 0, 0};
   SDL_Rect to_rect   = {0, 0, 0, 0};
   
   VALUE rect = rb_hash_aref(hash, RAY_SYM("at"));

   if (RTEST(rb_obj_is_kind_of(rect, ray_cRect)))
      to_rect = ray_rb2rect(rect);
   else if (RTEST(rb_obj_is_kind_of(rect, rb_cArray)))
      to_rect = ray_rb2rect(rb_apply(ray_cRect, RAY_METH("new"), rect));
   else if (rect != Qnil) {
      rb_raise(rb_eTypeError, "Can't convert %s into Ray::Rect",
               RAY_OBJ_CLASSNAME(rect));
   }

   rect = Qnil;
   rect = rb_hash_aref(hash, RAY_SYM("rect"));
   if (rect == Qnil) rect = rb_hash_aref(hash, RAY_SYM("from"));

   if (RAY_IS_A(rect, ray_cRect))
      from_rect = ray_rb2rect(rect);
   else if (RAY_IS_A(rect, rb_cArray))
      from_rect = ray_rb2rect(rb_apply(ray_cRect, RAY_METH("new"), rect));
   else if (!NIL_P(rect)) {
      rb_raise(rb_eTypeError, "Can't convert %s into Ray::Rect",
               RAY_OBJ_CLASSNAME(rect));
   }

   VALUE surf = rb_hash_aref(hash, RAY_SYM("on"));
   if (surf == Qnil) surf = rb_hash_aref(hash, RAY_SYM("to"));

   /* avoid raising an exception, which would prevent us from  freeing  the surface */
   SDL_Surface *target = ray_rb2surface(surf);

#ifdef HAVE_SDL_GFX
   VALUE rb_angle = Qnil, rb_zoom = Qnil;
   double angle = 0.0, zoom = 1.0; 

   if (!NIL_P(rb_angle = rb_hash_aref(hash, RAY_SYM("angle"))))
      angle = NUM2DBL(rb_angle);

   if (!NIL_P(rb_zoom = rb_hash_aref(hash, RAY_SYM("zoom"))))
      zoom = NUM2DBL(rb_zoom);

   if (!NIL_P(rb_angle) || !NIL_P(rb_zoom)) {
      SDL_Surface *res = rotozoomSurface(origin, angle, zoom, 1);
      if (!res) {
         rb_raise(rb_eRuntimeError, "Could not create the image (%s)",
                  SDL_GetError());
      }

      if (from_rect.w == 0 && from_rect.h == 0) {
         from_rect.w = res->w;
         from_rect.h = res->h;
      }

      if (SDL_BlitSurface(res, &from_rect,  target, &to_rect) == -1) {
         SDL_FreeSurface(res); /* Don't leak when an error occurs */
         rb_raise(rb_eRuntimeError, "Couldn't blit the image (%s)",
                  SDL_GetError());
      }

      SDL_FreeSurface(res);
      
      return surf;
   }
#endif

   if (from_rect.w == 0 && from_rect.h == 0) {
      from_rect.w = origin->w;
      from_rect.h = origin->h;
   }

   if (SDL_BlitSurface(origin, &from_rect, target,
                       &to_rect) == -1) {
      rb_raise(rb_eRuntimeError, "Couldn't blit the image (%s)",
                  SDL_GetError());
   }

   return surf;
}

#bppInteger Also known as: bits_per_pixel

Returns Bits per pixel.

Returns:

  • (Integer)

    Bits per pixel



349
350
351
352
353
354
# File 'ext/image.c', line 349

VALUE ray_image_bpp(VALUE self) {
   SDL_Surface *surf = ray_rb2surface(self);
   if (surf->format)
      return INT2FIX(ray_rb2surface(self)->format->BitsPerPixel);
   return Qnil;
}

#clipRay::Rect #clip(rect) ⇒ Object

Overloads:

  • #clipRay::Rect

    Returns The current clipping rect.

    Returns:

  • #clip(rect) ⇒ Object

    Changes the clipping rect (the rect which will be changed if something is drawn on this image, the rest of the image being ignored.)

    If a block is given, it is executed, and the old clipping rect is reset afterwards.

    Parameters:



525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
# File 'ext/image.c', line 525

VALUE ray_image_clip(int argc, VALUE *argv, VALUE self) {
   VALUE rb_rect = Qnil;
   rb_scan_args(argc, argv, "01", &rb_rect);

   SDL_Rect old_rect;
   SDL_GetClipRect(ray_rb2surface(self), &old_rect);

   if (NIL_P(rb_rect))
      return ray_rect2rb(old_rect);
   
   SDL_Rect rect = ray_convert_to_rect(rb_rect);
   SDL_SetClipRect(ray_rb2surface(self), &rect);
   
   if (rb_block_given_p()) {
      VALUE ary = rb_ary_new();
      rb_ary_push(ary, self);
      rb_ary_push(ary, ray_rect2rb(old_rect));
      
      rb_gc_register_address(&ary);

      rb_ensure(rb_yield, Qnil, ray_image_ensure_unclip, ary);
   }

   return ray_rect2rb(rect);
}

#draw_circle(center, radius, color) ⇒ Object

Draws a circle

Parameters:

  • center (Ray::Rect, Array<Integer>)

    Center of the circle.

  • radius (Integer)

    Radius of the circle.

  • color (Ray::Color)

    Color of the circle.



687
688
689
690
691
692
693
694
695
696
697
# File 'ext/image.c', line 687

VALUE ray_image_draw_circle(VALUE self, VALUE center, VALUE radius,
                            VALUE rb_color) {
   SDL_Surface *surface = ray_rb2surface(self);
   SDL_Rect rect = ray_convert_to_rect(center);
   ray_color color = ray_rb2col(rb_color);

   aacircleRGBA(surface, rect.x, rect.y, NUM2INT(radius),
                color.r, color.g, color.b, color.a);

   return self;
}

#draw_ellipse(rect, color) ⇒ Object

Draws an ellipse.

Parameters:

  • rect (Ray::Rect, Array<Integer>)

    Rect in which the ellipse should be drawn.

  • color (Ray::Color)

    Color in which the ellipse should be drawn.



726
727
728
729
730
731
732
733
734
735
# File 'ext/image.c', line 726

VALUE ray_image_draw_ellipse(VALUE self, VALUE rb_rect, VALUE rb_color) {
   SDL_Surface *surface = ray_rb2surface(self);
   SDL_Rect rect = ray_convert_to_rect(rb_rect);
   ray_color color = ray_rb2col(rb_color);

   aaellipseRGBA(surface, rect.x + rect.w / 2,
                 rect.y + rect.h / 2, rect.w / 2, rect.h / 2,
                 color.r, color.g, color.b, color.a);
   return self;
}

#draw_filled_circle(center, radius, color) ⇒ Object

Draws a filled circle

Parameters:

  • center (Ray::Rect, Array<Integer>)

    Center of the circle.

  • radius (Integer)

    Radius of the circle.

  • color (Ray::Color)

    Color of the circle.



707
708
709
710
711
712
713
714
715
716
717
# File 'ext/image.c', line 707

VALUE ray_image_draw_filled_circle(VALUE self, VALUE center, VALUE radius,
                            VALUE rb_color) {
   SDL_Surface *surface = ray_rb2surface(self);
   SDL_Rect rect = ray_convert_to_rect(center);
   ray_color color = ray_rb2col(rb_color);

   filledCircleRGBA(surface, rect.x, rect.y, NUM2INT(radius),
                color.r, color.g, color.b, color.a);

   return self;
}

#draw_flled_ellipse(rect, color) ⇒ Object

Draws a filled ellipse.

Parameters:

  • rect (Ray::Rect, Array<Integer>)

    Rect in which the ellipse should be drawn.

  • color (Ray::Color)

    Color in which the ellipse should be drawn.



744
745
746
747
748
749
750
751
752
753
# File 'ext/image.c', line 744

VALUE ray_image_draw_filled_ellipse(VALUE self, VALUE rb_rect, VALUE rb_color) {
   SDL_Surface *surface = ray_rb2surface(self);
   SDL_Rect rect = ray_convert_to_rect(rb_rect);
   ray_color color = ray_rb2col(rb_color);

   filledEllipseRGBA(surface, rect.x + rect.w / 2,
                     rect.y + rect.h / 2, rect.w / 2, rect.h / 2,
                     color.r, color.g, color.b, color.a);
   return self;
}

#draw_filled_polygon(points, color) ⇒ Object

Draws a filled polygon.

Parameters:

  • points (Array<Array<Integer>, Ray::Rect>)

    Points which should be joined to draw the polygon.

  • color (Ray::Color)

    The color in which the polygon should be drawn.



840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
# File 'ext/image.c', line 840

VALUE ray_image_draw_filled_polygon(VALUE self, VALUE points, VALUE rb_color) {
   SDL_Surface *surface = ray_rb2surface(self);

   size_t size = RARRAY_LEN(points);
   int16_t *x_array = malloc(sizeof(int16_t) * size);
   int16_t *y_array = malloc(sizeof(int16_t) * size);

   size_t i = 0;
   for (; i < size; i++) {
      SDL_Rect rect = ray_convert_to_rect(rb_ary_entry(points, i));
      
      x_array[i] = (int16_t)rect.x;
      y_array[i] = (int16_t)rect.y;
   }

   ray_color color = ray_rb2col(rb_color);
   filledPolygonRGBA(surface, x_array, y_array, (int)size,
                 color.r, color.g, color.b, color.a);

   free(x_array);
   free(y_array);

   return self;
}

#draw_filled_rect(rect, color) ⇒ Object

Draws a filled rect.

Parameters:



668
669
670
671
672
673
674
675
676
677
# File 'ext/image.c', line 668

VALUE ray_image_draw_filled_rect(VALUE self, VALUE rb_rect, VALUE rb_color) {
   SDL_Surface *surface = ray_rb2surface(self);
   SDL_Rect rect = ray_convert_to_rect(rb_rect);
   ray_color color = ray_rb2col(rb_color);

   boxRGBA(surface, rect.x, rect.y, rect.x + rect.w, rect.y + rect.h,
           color.r, color.g, color.b, color.a);

   return self;
}

#draw_filled_triangle(p1, p2, p3, color) ⇒ Object

Draws a filled triangle.



781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
# File 'ext/image.c', line 781

VALUE ray_image_draw_filled_triangle(VALUE self, VALUE p1, VALUE p2, VALUE p3,
                                     VALUE rb_color) {
   SDL_Surface *surface = ray_rb2surface(self);

   SDL_Rect first_point  = ray_convert_to_rect(p1);
   SDL_Rect second_point = ray_convert_to_rect(p2);
   SDL_Rect third_point  = ray_convert_to_rect(p3);
   
   ray_color color = ray_rb2col(rb_color);

   filledTrigonRGBA(surface, first_point.x, first_point.y,
                    second_point.x, second_point.y,
                    third_point.x, third_point.y,
                    color.r, color.g, color.b, color.a);

   return self;
}

#draw_line(p1, p2, color) ⇒ Object

Draws a line.

Parameters:



630
631
632
633
634
635
636
637
638
639
640
641
# File 'ext/image.c', line 630

VALUE ray_image_draw_line(VALUE self, VALUE p1, VALUE p2, VALUE rb_color) {
   SDL_Surface *surface = ray_rb2surface(self);

   SDL_Rect first_point  = ray_convert_to_rect(p1);
   SDL_Rect second_point = ray_convert_to_rect(p2);   
   ray_color color = ray_rb2col(rb_color);
   
   aalineRGBA(surface, first_point.x, first_point.y,
              second_point.x, second_point.y,
              color.r, color.g, color.b, color.a);
   return self;
}

#draw_pixel(point, color) ⇒ Object

Draws a point.

Parameters:



611
612
613
614
615
616
617
618
619
620
# File 'ext/image.c', line 611

VALUE ray_image_draw_pixel(VALUE self, VALUE rb_point, VALUE rb_color) {
   SDL_Surface *surface = ray_rb2surface(self);

   SDL_Rect point = ray_convert_to_rect(rb_point);
   ray_color color = ray_rb2col(rb_color);
   
   pixelRGBA(surface, point.x, point.y, color.r, color.g, color.b, color.a);

   return self;
}

#draw_polygon(points, color) ⇒ Object

Draws a polygon.

Parameters:

  • points (Array<Array<Integer>, Ray::Rect>)

    Points which should be joined to draw the polygon.

  • color (Ray::Color)

    The color in which the polygon should be drawn.



807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
# File 'ext/image.c', line 807

VALUE ray_image_draw_polygon(VALUE self, VALUE points, VALUE rb_color) {
   SDL_Surface *surface = ray_rb2surface(self);

   size_t size = RARRAY_LEN(points);
   int16_t *x_array = malloc(sizeof(int16_t) * size);
   int16_t *y_array = malloc(sizeof(int16_t) * size);

   size_t i = 0;
   for (; i < size; i++) {
      SDL_Rect rect = ray_convert_to_rect(rb_ary_entry(points, i));
      
      x_array[i] = (int16_t)rect.x;
      y_array[i] = (int16_t)rect.y;
   }

   ray_color color = ray_rb2col(rb_color);
   aapolygonRGBA(surface, x_array, y_array, (int)size,
                 color.r, color.g, color.b, color.a);

   free(x_array);
   free(y_array);

   return self;
}

#draw_rect(rect, color) ⇒ Object

Draws a rect.

Parameters:



650
651
652
653
654
655
656
657
658
659
# File 'ext/image.c', line 650

VALUE ray_image_draw_rect(VALUE self, VALUE rb_rect, VALUE rb_color) {
   SDL_Surface *surface = ray_rb2surface(self);
   SDL_Rect rect = ray_convert_to_rect(rb_rect);
   ray_color color = ray_rb2col(rb_color);

   rectangleRGBA(surface, rect.x, rect.y, rect.x + rect.w, rect.y + rect.h,
                 color.r, color.g, color.b, color.a);

   return self;
}

#draw_triangle(p1, p2, p3, color) ⇒ Object

Draws a triangle.



759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
# File 'ext/image.c', line 759

VALUE ray_image_draw_triangle(VALUE self, VALUE p1, VALUE p2, VALUE p3,
                              VALUE rb_color) {
   SDL_Surface *surface = ray_rb2surface(self);

   SDL_Rect first_point  = ray_convert_to_rect(p1);
   SDL_Rect second_point = ray_convert_to_rect(p2);
   SDL_Rect third_point  = ray_convert_to_rect(p3);
   
   ray_color color = ray_rb2col(rb_color);

   aatrigonRGBA(surface, first_point.x, first_point.y,
              second_point.x, second_point.y,
              third_point.x, third_point.y,
              color.r, color.g, color.b, color.a);

   return self;
}

#each {|pixel| ... } ⇒ Object

Yields:

  • (pixel)

Yield Parameters:



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ray/image.rb', line 19

def each
  return Enumerator.new(self, :each) unless block_given?

  (0...w).each do |x|
    (0...h).each do |y|
      yield self[x, y]
    end
  end

  self
end

#each_with_pos {|x, y, pixel| ... } ⇒ Object

Same as each, but also yields the position of each point.

Yields:

  • (x, y, pixel)


33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ray/image.rb', line 33

def each_with_pos
  return Enumerator.new(self, :each_with_pos) unless block_given?

  (0...w).each do |x|
    (0...h).each do |y|
      yield x, y, self[x, y]
    end
  end

  self
end

#fill(col) ⇒ Object

Fills the image with a given color.

Parameters:

  • col (Ray::Color)

    The color used to fill the image.



193
194
195
196
197
198
199
200
201
202
203
# File 'ext/image.c', line 193

VALUE ray_image_fill(VALUE self, VALUE col) {
   ray_color rcol = ray_rb2col(col);
   
   SDL_Surface *surf = ray_rb2surface(self);
   uint32_t specific_col = SDL_MapRGBA(surf->format,
                                       rcol.r, rcol.g, rcol.b,
                                       rcol.a);
   
   SDL_FillRect(surf, NULL, specific_col);
   return self;
}

#flagsObject

Returns the flags of an image (an OR’d combination of the Ray::Image::FLAG_* constants)



333
334
335
336
# File 'ext/image.c', line 333

VALUE ray_image_flags(VALUE self) {
   uint32_t flags = ray_rb2surface(self)->flags;
   return INT2NUM(flags);
}

#flipObject Also known as: update

Updates the image.



206
207
208
209
# File 'ext/image.c', line 206

VALUE ray_image_flip(VALUE self) {
   SDL_Flip(ray_rb2surface(self));
   return self;
}

#heightInteger Also known as: h

Returns Height of the surface.

Returns:

  • (Integer)

    Height of the surface



344
345
346
# File 'ext/image.c', line 344

VALUE ray_image_height(VALUE self) {
   return INT2FIX(ray_rb2surface(self)->h);
}

#initialize_copy(obj) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
# File 'ext/image.c', line 141

VALUE ray_init_image_copy(VALUE self, VALUE obj) {
   ray_image *img = ray_rb2image(self);
   
   SDL_Surface *src = ray_rb2surface(obj);
   img->surface = SDL_ConvertSurface(src, src->format, src->flags);
   if (!img->surface) {
      rb_raise(rb_eRuntimeError, "Could not create the image (%s)",
               SDL_GetError());
   }

   return self;
}

#inspectObject



3
4
5
# File 'lib/ray/image.rb', line 3

def inspect
  "#<#{self.class} w=#{w} h=#{h}>"
end

#lockObject

Locks the image (allow pixel-per-pixel modifications). Don’t forget to call unlock when you’re done. You can also pass a bock which will be called before the image gets unlocked automatically.



384
385
386
387
388
389
390
391
392
# File 'ext/image.c', line 384

VALUE ray_image_lock(VALUE self) {
   SDL_Surface *surface = ray_rb2surface(self);
   SDL_LockSurface(surface);

   if (rb_block_given_p())
      rb_ensure(rb_yield, Qnil, ray_image_ensure_unlock, self);

   return self;
}

#map(&block) ⇒ Ray::Image

Returns New image created according to a block.

Returns:

  • (Ray::Image)

    New image created according to a block.



76
77
78
# File 'lib/ray/image.rb', line 76

def map(&block)
  dup.map!(&block)
end

#map! {|pixel| ... } ⇒ Object

Yields:

  • (pixel)

    Block returning the new color of this pixel.



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ray/image.rb', line 46

def map!
  return Enumerator.new(self, :map!) unless block_given?

  lock do
    (0...w).each do |x|
      (0...h).each do |y|
        self[x, y] = yield self[x, y]
      end
    end
  end

  self
end

#map_with_pos(&block) ⇒ Ray::Image

Returns New image created according to a block.

Returns:

  • (Ray::Image)

    New image created according to a block.



81
82
83
# File 'lib/ray/image.rb', line 81

def map_with_pos(&block)
  dup.map_with_pos!(&block)
end

#map_with_pos! {|x, y, pixel| ... } ⇒ Object

Yields:

  • (x, y, pixel)

    Block returning the new color of this pixel



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ray/image.rb', line 61

def map_with_pos!
  return Enumerator.new(self, :map_with_pos!) unless block_given?

  lock do
    (0...w).each do |x|
      (0...h).each do |y|
        self[x, y] = yield x, y, self[x, y]
      end
    end
  end

  self
end

#rotozoom(angle, zoom) ⇒ SDL::Image

Rotates and zoomes on the image.

Parameters:

  • angle (Float)

    Angle in degrees

  • zoom (Float)

Returns:

  • (SDL::Image)

    the modified image.

See Also:



562
563
564
565
566
567
568
569
570
571
572
573
# File 'ext/image.c', line 562

VALUE ray_image_rotozoom(VALUE self, VALUE angle, VALUE zoom) {
   SDL_Surface *surface = ray_rb2surface(self);
   SDL_Surface *res = rotozoomSurface(surface, NUM2DBL(angle),
                                      NUM2DBL(zoom), 1);
   
   if (!res) {
      rb_raise(rb_eRuntimeError, "Could not create the image (%s)",
               SDL_GetError());
   }

   return ray_create_gc_image(res);
}

#rotozoom!(angle, zoom) ⇒ Object

Rotates and zoomes on the image, but does not create a new instance of Ray::Image, which may be better for memory management.

Notice Ray.create_window(…).rotozoom!(…) will not modify the image used as the screen.

See Also:



585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
# File 'ext/image.c', line 585

VALUE ray_image_rotozoom_bang(VALUE self, VALUE angle, VALUE zoom) {
   ray_image *img = ray_rb2image(self);
   SDL_Surface *res = rotozoomSurface(img->surface, NUM2DBL(angle),
                                      NUM2DBL(zoom), 1);
   
   if (!res) {
      rb_raise(rb_eRuntimeError, "Could not create the image (%s)",
               SDL_GetError());
   }

   if (img->must_free)
      SDL_FreeSurface(img->surface);

   img->surface   = res;
   img->must_free = 1;

   return self;
}

#to_imageObject



85
86
87
# File 'lib/ray/image.rb', line 85

def to_image
  self
end

#unlockObject

Unlocks the image. You must call this once you are done modifying the image.



398
399
400
# File 'ext/image.c', line 398

VALUE ray_image_unlock(VALUE self) {
   return ray_image_ensure_unlock(self);
}

#widthInteger Also known as: w

Returns Width of the surface.

Returns:

  • (Integer)

    Width of the surface



339
340
341
# File 'ext/image.c', line 339

VALUE ray_image_width(VALUE self) {
   return INT2FIX(ray_rb2surface(self)->w);
}