Class: Xibe::Image

Inherits:
Layer
  • Object
show all
Defined in:
lib/xibe.rb

Instance Attribute Summary collapse

Attributes inherited from Layer

#height, #visible, #width, #x, #y, #z

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Layer

#bottom, #hide, #left, #pos, #right, #show, #top

Constructor Details

#initialize(filename, transparent = false) ⇒ Image

Returns a new instance of Image.



478
479
480
481
482
483
484
485
486
# File 'lib/xibe.rb', line 478

def initialize(filename, transparent=false)
  super()
  @filename = filename
  @image = Image.create(@filename, transparent)
  @width = @image.w
  @height = @image.h
  @src_x = 0
  @src_y = 0
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



476
477
478
# File 'lib/xibe.rb', line 476

def filename
  @filename
end

#src_xObject

Returns the value of attribute src_x.



477
478
479
# File 'lib/xibe.rb', line 477

def src_x
  @src_x
end

#src_yObject

Returns the value of attribute src_y.



477
478
479
# File 'lib/xibe.rb', line 477

def src_y
  @src_y
end

Class Method Details

.create(filename, transparent = false) ⇒ Object

Create a image



489
490
491
492
493
494
# File 'lib/xibe.rb', line 489

def self.create(filename, transparent=false)
  img = SDL::Surface.load(filename)
  color_key = img.get_pixel(0,0)
  img.set_color_key(SDL::SRCCOLORKEY ,color_key) if transparent == true
  img.display_format
end

.to_tiles(filename, width, height, transparent = false, margin = 0) ⇒ Object

Create a tiles from a image file



497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
# File 'lib/xibe.rb', line 497

def self.to_tiles(filename, width, height, transparent = false, margin = 0)
  img = create(filename, false)
  color_key = img.get_pixel(0,0)
  t_width = (img.w-margin) / (width + margin)
  t_height = (img.h-margin) / (height + margin)
  tiles = []
  i = 0
  while i < t_height do
    j = 0;
    while j < t_width do
      tiles << img.copy_rect(j*(width+margin)+margin, i*(height+margin)+margin, width, height)
      tiles[tiles.length - 1].set_color_key(SDL::SRCCOLORKEY, color_key) if transparent == true
      j += 1
    end
    i += 1
  end
  tiles
end

Instance Method Details

#drawObject

Draw image



517
518
519
# File 'lib/xibe.rb', line 517

def draw
  SDL::Surface.blit(@image, @src_x, @src_y, @width, @height, SDL.get_video_surface, @x, @y)
end