Module: Visuals

Includes:
SDL2
Defined in:
lib/frontend.rb,
lib/graphical.rb

Constant Summary collapse

COLORS =
{
black:   [   0,    0,    0],
red:     [ 180,  100,  100],
green:   [ 100,  180,  100],
yellow:  [ 180,  180,  100],
blue:    [ 100,  100,  180],
magenta: [ 180,  100,  180],
cyan:    [ 100,  180,  180],
white:   [ 255,  255,  255],
grey:    [ 158,  158,  158],
dark_red:     [  90,   30,   30],
dark_green:   [  30,   90,   30],
dark_yellow:  [  90,   80,   30],
dark_blue:    [  30,   30,   90],
dark_magenta: [  90,   30,   90],
dark_cyan:    [  30,   80,   80] }
WINDOW =
Window.create("visuals",
Window::POS_CENTERED,
Window::POS_CENTERED,
1324, 768, 0)
GO =
WINDOW.create_renderer(-1,
Renderer::Flags::ACCELERATED|Renderer::Flags::TARGETTEXTURE)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.columnObject



21
# File 'lib/graphical.rb', line 21

def self.column; $col end

.exitObject



95
96
# File 'lib/graphical.rb', line 95

def self.exit
end

.heightObject



19
# File 'lib/graphical.rb', line 19

def self.height; WINDOW.size[1] / $charsize[0] end

.init(args = {}) ⇒ Object



98
99
100
101
102
103
104
105
106
# File 'lib/graphical.rb', line 98

def self.init args={}
  $default = [
    color_for( args[:foreground] || :grey80 ),
    color_for( args[:background] || :grey10 ) ]
  $color = $default.dup
  $charsize = args[:charsize] || [18,11]
  $font = TTF.open( args[:font] || "/usr/share/fonts/X11/TTF/VeraMono.ttf", $charsize[0] )
  move 0, 0
end

.rowObject



20
# File 'lib/graphical.rb', line 20

def self.row; $row end

.widthObject



18
# File 'lib/graphical.rb', line 18

def self.width;  WINDOW.size[0] / $charsize[1] end

.xObject



22
# File 'lib/graphical.rb', line 22

def self.x; $x end

.yObject



23
# File 'lib/graphical.rb', line 23

def self.y; $y end

Instance Method Details

#background(color) ⇒ Object



33
34
# File 'lib/graphical.rb', line 33

def background color; return unless color
$color[1] = color_for( color ) end

#clear(type = :screen) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/graphical.rb', line 49

def clear type = :screen
  GO.draw_color = $default[1]
  case type
    when :screen; GO.fill_rect Rect.new(0, 0, *WINDOW.size)
    when :line; GO.fill_rect Rect.new(
      Visuals.x, Visuals.y, WINDOW.size[0], Visuals.y+$charsize[0])
    when :down; GO.fill_rect Rect.new(
      0, Visuals.y, *WINDOW.size)
  end
end

#color(c) ⇒ Object



38
39
40
41
42
# File 'lib/graphical.rb', line 38

def color c
  c = [c] unless c.is_a? Array and c.count == 2
  foreground c[0]
  background c[1]
end

#color_for(c) ⇒ Object



25
26
27
28
29
# File 'lib/graphical.rb', line 25

def color_for c
  return unless c
  c = COLORS[c] if c.is_a?( Symbol )
  c.map{ |c| (c/1000.0*256).to_i }
end

#draw(str) ⇒ Object



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

def draw( str )
  return unless str
  if get_background
    GO.draw_color = get_background
    GO.fill_rect Rect.new($x, $y, str.size*$charsize[1], $charsize[0])
  end
  text = str.rstrip
  unless text.empty?
    image = $font.render_blended( text, get_foreground )
    GO.copy GO.create_texture_from( image ), nil,
      Rect.new($x, $y, image.w, $charsize[0])
  end
  move $col+str.size, $row
end

#foreground(color) ⇒ Object



35
36
# File 'lib/graphical.rb', line 35

def foreground color;
$color[0] = (color ? color_for( color ) : $default[0]) end

#get_backgroundObject



31
# File 'lib/graphical.rb', line 31

def get_background; $color[1] end

#get_foregroundObject



32
# File 'lib/graphical.rb', line 32

def get_foreground; $color[0] end

#move(col, row) ⇒ Object



44
45
46
47
# File 'lib/graphical.rb', line 44

def move col, row
  $x = ($col = col) * $charsize[1]
  $y = ($row = row) * $charsize[0]
end

#read(input) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/graphical.rb', line 77

def read input
  case input
    when :event then e = Event.poll until e
    when :key then
      e = Event.poll until e.is_a? Event::KeyDown
      return e.sym.between?(32, 126) ? e.sym.chr : e.sym
    when :line
      str = ''
      loop do
        e = Event.poll
        next unless e.is_a? Event::KeyDown
        return nil if e.sym == KEY_ESCAPE
        return str if e.sym == KEY_RETURN
        str << e.sym.chr if e.sym.between? 32, 126
      end
  end
end

#refreshObject



75
# File 'lib/graphical.rb', line 75

def refresh; GO.present end