Class: CRT

Inherits:
Gosu::Window
  • Object
show all
Defined in:
lib/HDLRuby/hdr_samples/ruby_program/sw_cpu_terminal.rb

Overview

Class representing the CRT monitor in Gosu

Instance Method Summary collapse

Constructor Details

#initialize(width = 640, height = 480, px_width, px_height) ⇒ CRT

Returns a new instance of CRT.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/HDLRuby/hdr_samples/ruby_program/sw_cpu_terminal.rb', line 16

def initialize(width=640,height=480,px_width,px_height)
  super(width,height,false)

  @px_width  = px_width
  @px_height = px_height
  @cols = width / @px_width
  @rows = height / @px_height
  # puts "px_width=#{px_width} px_height=#{px_height} cols=#{@cols} rows=#{@rows}"

  # The screen buffer.
  @buf = [ Gosu::Color::BLACK ] * (@cols * @rows)

  # The last pressed key
  @key = nil
end

Instance Method Details

#button_down(id) ⇒ Object



48
49
50
# File 'lib/HDLRuby/hdr_samples/ruby_program/sw_cpu_terminal.rb', line 48

def button_down(id)
  @key = id
end

#drawObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/HDLRuby/hdr_samples/ruby_program/sw_cpu_terminal.rb', line 36

def draw
  @rows.times do |y|
    pos_y = y * @px_height
    # puts "y=#{y} pos_y=#{pos_y}"
    off_y = y * @cols
    @cols.times do |x|
      pos_x = x * @px_width
      draw_rect(pos_x,pos_y, @px_width, @px_height,@buf[x+off_y])
    end
  end
end

#getchObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/HDLRuby/hdr_samples/ruby_program/sw_cpu_terminal.rb', line 57

def getch
  sleep(0.02) while(@key == nil) 
  case(@key)
  when Gosu::KB_ESCAPE
    res = 27.chr
  when Gosu::KB_RETURN
    res = 10.chr
  when Gosu::KB_BACKSPACE
    res = 127.chr
  else
    res = Gosu.button_id_to_char(@key)
  end
  # puts "res=#{res}" if !res.empty?
  @key = nil
  return res
end

#put_pixel(x, y, val) ⇒ Object



52
53
54
55
# File 'lib/HDLRuby/hdr_samples/ruby_program/sw_cpu_terminal.rb', line 52

def put_pixel(x,y,val)
  # puts "put_pixel x=#{x} y=#{y}"
  @buf[x+y*@cols] = val
end

#updateObject



32
33
34
# File 'lib/HDLRuby/hdr_samples/ruby_program/sw_cpu_terminal.rb', line 32

def update
  # puts "Updated."
end