Class: Twitterpunch::Viewer

Inherits:
Object
  • Object
show all
Includes:
Rubygame
Defined in:
lib/twitterpunch/viewer.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Viewer

Returns a new instance of Viewer.



10
11
12
13
# File 'lib/twitterpunch/viewer.rb', line 10

def initialize(config)
  @config = config
  srand
end

Instance Method Details

#displayObject



24
25
26
27
28
29
30
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/twitterpunch/viewer.rb', line 24

def display
  onscreen = @config[:viewer][:count] || 5

  # Set up the TrueType Font module
  TTF.setup
  point_size = 20
  $font = TTF.new("#{@config[:resources]}/Tahoma Bold.ttf", point_size)

  #@screen = Screen.open [ 640, 480]
  default_depth = 0
  @maximum_resolution = Screen.get_resolution

  screen = Screen.open(@maximum_resolution, default_depth, [ HWSURFACE, DOUBLEBUF, FULLSCREEN])

  screen.show_cursor = false

  clock = Clock.new
  clock.target_framerate = 60

  # Ask Clock.tick() to return ClockTicked objects instead of the number of
  # milliseconds that have passed:
  clock.enable_tick_events

  # Create a new group of sprites so that all sprites in the group may be updated
  # or drawn with a single method invocation.
  @sprites = Sprites::Group.new
  Sprites::UpdateGroup.extend_object(@sprites)
  onscreen.times do
    @sprites << Twitterpunch::Sprite.new(@maximum_resolution, *next_image)
  end

  #@background = Surface.load("background.png").zoom_to(maximum_resolution[0], maximum_resolution[1])
  # Create a background image and copy it to the screen. With no image, it's just black.
  background = Surface.new(@maximum_resolution)
  background.blit(screen, [ 0, 0])

  event_queue = EventQueue.new
  event_queue.enable_new_style_events

  should_run = true
  while should_run do
    seconds_passed = clock.tick().seconds

    event_queue.each do |event|
      case event
        when Events::QuitRequested, Events::KeyReleased
          should_run = false
      end
    end

    # remove all sprites who've gone out of sight
    @sprites.reject { |sprite| sprite.visible }.each do |sprite|
      sprite.undraw(screen, background)
      sprite.kill
      next if sprite.popped?

      @sprites << Twitterpunch::Sprite.new(@maximum_resolution, *next_image)
    end

    # "undraw" all of the sprites by drawing the background image at their
    # current location ( before their location has been changed by the animation)
    @sprites.undraw(screen, background)

    # Give all of the sprites an opportunity to move themselves to a new location
    @sprites.update(seconds_passed)

    # Draw all of the sprites
    @sprites.draw(screen)

    # Then redraw the popped sprite to ensure it's on top
    @sprites.select { |sprite| sprite.popped? }.each { |popped| popped.draw(screen) }

    screen.flip
  end

end

#next_imageObject



106
107
108
109
110
# File 'lib/twitterpunch/viewer.rb', line 106

def next_image
  image = Dir.glob(File.expand_path("#{@config[:photodir]}/*")).sample
  text  = @config[:state][File.basename(image)] rescue nil
  return image, text
end

#pop(image, text) ⇒ Object



101
102
103
104
# File 'lib/twitterpunch/viewer.rb', line 101

def pop(image, text)
  path = File.expand_path("#{@config[:photodir]}/#{image}")
  @sprites << Twitterpunch::Sprite.new(@maximum_resolution, path, text, true)
end

#runObject



15
16
17
18
19
20
21
22
# File 'lib/twitterpunch/viewer.rb', line 15

def run
  if @config.has_key? :viewer
    display
  else
    puts 'Press enter to exit'
    STDIN.gets
  end
end