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

.bgcolorObject



36
37
38
# File 'lib/dxruby_sdl/window.rb', line 36

def bgcolor
  @bgcolor ||= DEFAULTS[:background_color]
end

.bgcolor=(val) ⇒ Object



40
41
42
# File 'lib/dxruby_sdl/window.rb', line 40

def bgcolor=(val)
  @bgcolor = val
end

.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



70
71
72
73
74
75
# File 'lib/dxruby_sdl/window.rb', line 70

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



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

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],
                              0)
end

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



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

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

.draw_scale(x, y, image, scalex, scaley, centerx = nil, centery = nil, z = 0) ⇒ Object Also known as: drawScale



77
78
79
80
81
82
83
84
85
# File 'lib/dxruby_sdl/window.rb', line 77

def draw_scale(x, y, image, scalex, scaley, centerx = nil, centery = nil, z = 0)
  opt = {
    scale_x: scalex,
    scale_y: scaley,
    center_x: centerx,
    center_y: centery,
  }
  draw_ex(x, y, image, opt)
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



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

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, bgcolor)

      yield

      screen.flip

      Input.send(:store_last_state)
    end
  end
end

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



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/dxruby_sdl/window.rb', line 125

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

.windowed=(val) ⇒ Object



149
150
151
# File 'lib/dxruby_sdl/window.rb', line 149

def windowed=(val)
  @_fullscreen = !val
end

.windowed?Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/dxruby_sdl/window.rb', line 145

def windowed?
  !@_fullscreen
end