Class: Rubydraw::Window

Inherits:
Object show all
Defined in:
lib/rubydraw/window.rb

Overview

Instances of Rubydraw::Window can draw themselves on the screen after you open them. Note: there can only be one window on the screen at a time; it is a limit of SDL. One important things about instances of Rubydraw::Window: its main loop (which starts when Rubydraw::Window#open is called) is not forked! It will break when Rubydraw::Window#close is called.

Instance Method Summary collapse

Constructor Details

#initialize(width, height) ⇒ Window

Create a new window.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rubydraw/window.rb', line 9

def initialize(width, height)
  unless height.is_a?(Numeric) and width.is_a?(Numeric)
    # Raise an error
    raise SDLError "Window width and height have to be a number."
  end

  @width = width
  @height = height
  @open = false

  @event_queue = EventQueue.new

  @registered_actions = {}
end

Instance Method Details

#break_main_loopObject

This causes the main loop to exit. Use Rubydraw::Window#close to close the window, not this.



70
71
72
# File 'lib/rubydraw/window.rb', line 70

def break_main_loop
  @open = false
end

#clearObject

Clear the window’s contents by filling it with black. A bit of a cheat; I don’t know if there is a better way to do this.



46
47
48
# File 'lib/rubydraw/window.rb', line 46

def clear
  SDL::FillRect(@screen, nil, 0)
end

#closeObject

Call this method to tell SDL to quit drawing. The loop (started in Rubydraw::Window#open) would continue if close only stopped drawing, so break the loop too.



53
54
55
56
# File 'lib/rubydraw/window.rb', line 53

def close
  break_main_loop
  SDL.QuitSubSystem(SDL::INIT_VIDEO)
end

#handle_eventsObject

Collect and handle new events by executing blocks in @regestered_events. See Rubydraw::Window#register_action on how to use it.



60
61
62
63
64
65
66
# File 'lib/rubydraw/window.rb', line 60

def handle_events
  events = @event_queue.get_events

  events.each {|event|
    block = @registered_actions[event.class]
    block.call(event) unless block.nil?}
end

#heightObject

Return the height of this window.



99
100
101
# File 'lib/rubydraw/window.rb', line 99

def height
  @height
end

#openObject

Call this method to start updating and drawing.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rubydraw/window.rb', line 25

def open
  @open = true
  # Behold, the main loop. Drumroll!
  @screen = SDL::SetVideoMode(@width, @height, 0, 0)
  loop do
    handle_events
    if @open
      # Clear the contents of the window
      clear
      tick
      # Update the screen to show any changes.
      SDL::UpdateRect(@screen, 0, 0, 0, 0)
    else
      # This is where the loop is broken after Rubydraw::Window#close is called.
      break
    end
  end
end

#open?Boolean

Returns if this window is open.

Returns:

  • (Boolean)


75
76
77
# File 'lib/rubydraw/window.rb', line 75

def open?
  @open
end

#sdl_surfaceObject

Return the SDL surface object. Only used in Rubydraw::Image#draw for blitting to this window.



81
82
83
# File 'lib/rubydraw/window.rb', line 81

def sdl_surface
  @screen
end

#tickObject

Redefine Rubydraw::Window#tick with any code you want to be executed every frame, like drawing functions.

Does nothing by default.



90
91
# File 'lib/rubydraw/window.rb', line 90

def tick
end

#whenever(event, &block) ⇒ Object

Execute the given block on the appearance of an instance of event.

Example:

class MyWindow < Rubydraw::Window
  def initialize
    super(300, 300)

    register_action(Rubydraw::Events::MouseMove) {|event| new_pos = event.position; puts "Mouse moved to #{new_pos.x}, #{new_pos.y}"}
    register_action(Rubydraw::Events::QuitRequest) {puts "Goodbye!"; close}
  end
end


117
118
119
# File 'lib/rubydraw/window.rb', line 117

def whenever(event, &block)
  @registered_actions[event] = block
end

#widthObject

Return the width of this window.



94
95
96
# File 'lib/rubydraw/window.rb', line 94

def width
  @width
end