Module: DXRubySDL::Window

Defined in:
lib/dxruby_sdl/window.rb,
lib/dxruby_sdl/window/fpstimer.rb

Defined Under Namespace

Classes: FPSTimer

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.heightObject



14
15
16
17
# File 'lib/dxruby_sdl/window.rb', line 14

def height
  @height ||= DEFAULTS[:height]
  return @height
end

.scaleObject



19
20
21
22
# File 'lib/dxruby_sdl/window.rb', line 19

def scale
  @scale ||= DEFAULTS[:scale]
  return @scale
end

.widthObject



9
10
11
12
# File 'lib/dxruby_sdl/window.rb', line 9

def width
  @width ||= DEFAULTS[:width]
  return @width
end

Class Method Details

.captionObject



24
25
26
# File 'lib/dxruby_sdl/window.rb', line 24

def caption
  return SDL::WM.caption[0]
end

.caption=(val) ⇒ Object



28
29
30
# File 'lib/dxruby_sdl/window.rb', line 28

def caption=(val)
  SDL::WM.set_caption(val, '')
end

.draw(x, y, image, z = 0) ⇒ Object



62
63
64
65
66
67
# File 'lib/dxruby_sdl/window.rb', line 62

def draw(x, y, image, z = 0)
  if z != 0
    raise NotImplementedError, 'Window.draw(x, y, image, z != 0)'
  end
  screen.put(image._surface, x, y)
end

.draw_ex(x, y, image, hash = {}) ⇒ Object Also known as: drawEx



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/dxruby_sdl/window.rb', line 69

def draw_ex(x, y, image, hash = {})
  if hash[:z] && hash[:z] != 0
    raise NotImplementedError, 'Window.draw_ex(x, y, image, z: != 0)'
  end
  option = {
    angle: 0,
    scale_x: 1,
    scale_y: 1,
    center_x: 0,
    center_y: 0,
  }.merge(hash)
  SDL::Surface.transform_blit(image._surface, screen,
                              option[:angle],
                              option[:scale_x], option[:scale_y],
                              option[:center_x], option[:center_y],
                              x + option[:center_x], y + option[:center_y],
                              SDL::Surface::TRANSFORM_SAFE)
end

.draw_font(x, y, string, font, hash = {}) ⇒ Object Also known as: drawFont



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/dxruby_sdl/window.rb', line 88

def draw_font(x, y, string, font, hash = {})
  if string.empty?
    return
  end
  if hash[:color]
    r, g, b = *hash[:color]
  else
    r, g, b = 255, 255, 255
  end
  h = font._ttf.height + 1
  string.lines.each.with_index do |line, i|
    line.chomp!
    if line.empty?
      next
    end
    font._ttf.draw_blended_utf8(screen, line, x, y + i * h, r, g, b)
  end
end

.fps=(val) ⇒ Object



32
33
34
# File 'lib/dxruby_sdl/window.rb', line 32

def fps=(val)
  FPSTimer.instance.fps = val
end

.loop(&block) ⇒ Object



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
# File 'lib/dxruby_sdl/window.rb', line 36

def loop(&block)
  timer = FPSTimer.instance
  timer.reset

  Kernel.loop do
    timer.wait_frame do
      while (event = SDL::Event.poll)
        case event
        when SDL::Event::Quit
          exit
        when SDL::Event::KeyDown, SDL::Event::KeyUp
          Input.send(:handle_key_event, event)
        end
      end

      screen.fill_rect(0, 0, width, height, DEFAULTS[:background_color])

      yield

      screen.flip

      Input.send(:store_last_state)
    end
  end
end

.open_filename(filter, title) ⇒ Object Also known as: openFilename



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/dxruby_sdl/window.rb', line 107

def open_filename(filter, title)
  # :nocov:
  if /darwin/ =~ RUBY_PLATFORM
    apple_script = <<-EOS
on run
  tell application "Finder"
activate
set theImage to choose file
return theImage as Unicode text
  end tell
end run
    EOS
    s = `osascript -e '#{apple_script}'`
    return s.chomp.sub(/^ "/, '').gsub(/:/, '/')
  else
    raise NotImplementedError, 'Window.open_filename'
  end
  # :nocov:
end