Class: SDL2::Palette

Inherits:
Struct
  • Object
show all
Defined in:
lib/sdl2/palette.rb

Overview

SDL_pixels.h:261~267

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Struct

#==, cast, #initialize

Methods included from StructHelper

#member_readers, #member_writers

Constructor Details

This class inherits a constructor from SDL2::Struct

Class Method Details

.create(num_colors) ⇒ Object



12
13
14
# File 'lib/sdl2/palette.rb', line 12

def self.create(num_colors)
  SDL2.alloc_palette!(num_colors)
end

.release(pointer) ⇒ Object



16
17
18
# File 'lib/sdl2/palette.rb', line 16

def self.release(pointer)
  SDL2.free_palette(pointer)
end

Instance Method Details

#set_colors(colors, firstcolor = 0, ncolors = nil) ⇒ Object

  • Elements that are Color structs are copied into the array.

param firstcolor: The index of the first palette entry to modify. param ncolors: The number of entries to modify

  • This defaults to the length of colors if colors is a Ruby Array



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/sdl2/palette.rb', line 31

def set_colors(colors, firstcolor = 0, ncolors = nil)
  
  if colors.kind_of? Array
    
    ncolors = colors.count if ncolors.nil?
    
    raise "(ncolors = #{ncolors} > colors.count #{colors.count}" if ncolors > colors.count
    
    # We must build our on colors pointer:
    colors_ptr = FFI::MemoryPointer.new(Color, ncolors)
    
    colors.each_with_index() { |color, idx|
      
      puts "color##{idx} = #{color.inspect}"
      
      if idx < ncolors # Skip when the array count is more than ncolors
    
        color_ptr = Color.new(colors_ptr[idx])
        
        if color.kind_of? Array          
          color_ptr.set(*color)
        elsif color.kind_of? Color
          color_ptr.copy_from(color)
        else
          raise "Invalid Array Member"
        end#if color.kind_of?
        
      end
      
    }#colors.each_with_index
    
    colors = colors_ptr # Replace the passed in argument with our pointer
    
  elsif colors.kind_of? FFI::Pointer
    raise "ncolors must be specified" if ncolors.nil?
    # Then we can just continue using it as is below.
  end# if
  
  SDL2.set_palette_colors(self, colors, firstcolor, ncolors)
        
end