Class: BadSdl::Application

Inherits:
Engine::Engines show all
Defined in:
lib/bad_sdl/application.rb

Overview

A Top-Level SDL Application: An application TODO: Review SDL2::Application for removal

Instance Attribute Summary collapse

Attributes inherited from Engine::Engines

#engines

Attributes inherited from Engine

#surface

Instance Method Summary collapse

Methods inherited from Engine::Engines

#current_engine, #handle_event, #paint_to

Methods inherited from Engine

#handle_event, #on, #on_handlers, #paint_to, #painter

Constructor Details

#initialize(opts = {}) ⇒ Application

Returns a new instance of Application.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/bad_sdl/application.rb', line 14

def initialize(opts = {})
  super(opts)
  
  opts[:title] ||= self.to_s
  opts[:width] ||= 640
  opts[:height] ||= 480
  opts[:flags] ||= :SHOWN
  
  @window = Window.create(opts)
  @window.surface.fill_rect(@window.surface.rect, [0,0,0,SDL2::ALPHA_OPAQUE])
  
  # Default ON handler for :QUIT events:
  self.on({type: :QUIT}) do |event|
    @quit_loop = true
  end
  
end

Instance Attribute Details

#loop_count_limitObject

Returns the value of attribute loop_count_limit.



52
53
54
# File 'lib/bad_sdl/application.rb', line 52

def loop_count_limit
  @loop_count_limit
end

#poll_count_limitObject

Returns the value of attribute poll_count_limit.



37
38
39
# File 'lib/bad_sdl/application.rb', line 37

def poll_count_limit
  @poll_count_limit
end

#windowObject

Returns the value of attribute window.



12
13
14
# File 'lib/bad_sdl/application.rb', line 12

def window
  @window
end

Instance Method Details

#after_loop(&block) ⇒ Object



76
77
78
79
80
# File 'lib/bad_sdl/application.rb', line 76

def after_loop(&block)
  @after_loop ||= []
  @after_loop << block unless block.nil?
  @after_loop
end

#before_loop(&block) ⇒ Object



70
71
72
73
74
# File 'lib/bad_sdl/application.rb', line 70

def before_loop(&block)
  @before_loop ||= []
  @before_loop << block unless block.nil?
  @before_loop
end

#loop(cnt = loop_count_limit, opts = {}) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/bad_sdl/application.rb', line 54

def loop(cnt = loop_count_limit, opts ={})
  @quit_loop = false
  times = 0
  
  while (cnt.nil?) or ((times+=1) <= cnt)
    before_loop.each(&:call)
    poll # Process input
    break if @quit_loop
    # Update the surface when we are painted to
    @window.update_surface if paint_to(@window.surface)
    delay(opts[:delay]) if opts.has_key? :delay
    after_loop.each(&:call)
  end
  
end

#poll(cnt = poll_count_limit) ⇒ Object

What makes an engine an “Application” is that it takes control of event polling. There should only ever be one “Application”



41
42
43
44
45
46
47
48
49
50
# File 'lib/bad_sdl/application.rb', line 41

def poll(cnt = poll_count_limit)
  Debug.log(self){"Poll Start"}
  times = 0
  while (event = Event.poll()) and (cnt.nil? or (times+=1 < cnt))
    Debug.log(self){"GOT: #{event.type}"}
    handle_event(event)
  end
  
  Debug.log(self){"Poll End"}
end

#quitObject



32
33
34
35
# File 'lib/bad_sdl/application.rb', line 32

def quit()
  @window.destroy
  super()
end