Module: DXOpal::Window

Defined in:
lib/dxopal/window.rb

Constant Summary collapse

@@fps =
60
@@fps_ts =
nil
@@fps_ct =
0
@@real_fps =
0
@@real_fps_ct =
1
@@real_fps_t =
Time.now
@@width =
640
@@height =
480
@@block =
nil
@@paused =
false
@@bgcolor =
Constants::Colors::C_BLACK

Class Method Summary collapse

Class Method Details

._imgObject

Return internal DXOpal::Image object (for experimental/hacking use)



130
# File 'lib/dxopal/window.rb', line 130

def self._img; @@img; end

._initObject



119
120
121
122
123
124
125
126
127
# File 'lib/dxopal/window.rb', line 119

def self._init
  canvas = `document.getElementById("dxopal-canvas")`
  # If user did not change Window.width/Window.height, set the canvas size here
  self.width = @@width
  self.height = @@height
  img = Image.new(self.width, self.height, canvas: canvas)
  Input._init(canvas)
  return img
end

._loop(timestamp) ⇒ Object

(internal) call @@block periodically



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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/dxopal/window.rb', line 53

def self._loop(timestamp)
  @@img ||= _init

  # Calculate fps
  frame_msec = 1000.0 / @@fps
  @@fps_ts ||= timestamp
  passed_msec = timestamp - @@fps_ts
  @@fps_ts = timestamp
  @@fps_ct += passed_msec
  if @@fps_ct >= frame_msec
    @@fps_ct -= frame_msec
  else
    self_ = self
    `window`.JS.requestAnimationFrame{|time| self_._loop(time) }
    return
  end

  # Calculate real_fps
  t = Time.now
  if t - @@real_fps_t >= 1.0
    @@real_fps = @@real_fps_ct
    @@real_fps_ct = 1
    @@real_fps_t = t
  else
    @@real_fps_ct += 1
  end

  # Update physics
  Sprite.matter_tick(timestamp) if Sprite.matter_enabled?

  # Detect inputs
  Input._on_tick

  # Call user code
  @@draw_queue = []
  if @@paused
    Window.draw_pause_screen
  else
    DXOpal.dump_error(&@@block)
  end

  # Draw
  @@img.box_fill(0, 0, @@width, @@height, @@bgcolor)
  sorted = @@draw_queue.sort{|a, b| a[0] == b[0] ? a[1] <=> b[1] : a[0] <=> b[0] }
  sorted.each do |item|
    case item[2]
    when :image then @@img.draw(*item.drop(3))
    when :image_rot then @@img.draw_rot(*item.drop(3))
    when :image_scale then @@img.draw_scale(*item.drop(3))
    when :draw_ex then @@img.draw_ex(*item.drop(3))
    when :font then @@img.draw_font(*item.drop(3)) 
    when :pixel then @@img.[]=(*item.drop(3))
    when :line then @@img.line(*item.drop(3))
    when :box then @@img.box(*item.drop(3))
    when :box_fill then @@img.box_fill(*item.drop(3))
    when :circle then @@img.circle(*item.drop(3))
    when :circle_fill then @@img.circle_fill(*item.drop(3))
    when :triangle then @@img.triangle(*item.drop(3))
    when :triangle_fill then @@img.triangle_fill(*item.drop(3))
    end
  end

  self_ = self
  `window`.JS.requestAnimationFrame{|time| self_._loop(time) }
end

.bgcolorObject



154
# File 'lib/dxopal/window.rb', line 154

def self.bgcolor; @@bgcolor; end

.bgcolor=(col) ⇒ Object



155
# File 'lib/dxopal/window.rb', line 155

def self.bgcolor=(col); @@bgcolor = col; end

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



157
158
159
# File 'lib/dxopal/window.rb', line 157

def self.draw(x, y, image, z=0)
  enqueue_draw(z, :image, x, y, image)
end

.draw_box(x1, y1, x2, y2, color, z = 0) ⇒ Object



187
188
189
# File 'lib/dxopal/window.rb', line 187

def self.draw_box(x1, y1, x2, y2, color, z=0)
  enqueue_draw(z, :box, x1, y1, x2, y2, color)
end

.draw_box_fill(x1, y1, x2, y2, color, z = 0) ⇒ Object



191
192
193
# File 'lib/dxopal/window.rb', line 191

def self.draw_box_fill(x1, y1, x2, y2, color, z=0)
  enqueue_draw(z, :box_fill, x1, y1, x2, y2, color)
end

.draw_circle(x, y, r, color, z = 0) ⇒ Object



195
196
197
# File 'lib/dxopal/window.rb', line 195

def self.draw_circle(x, y, r, color, z=0)
  enqueue_draw(z, :circle, x, y, r, color)
end

.draw_circle_fill(x, y, r, color, z = 0) ⇒ Object



199
200
201
# File 'lib/dxopal/window.rb', line 199

def self.draw_circle_fill(x, y, r, color, z=0)
  enqueue_draw(z, :circle_fill, x, y, r, color)
end

.draw_ex(x, y, image, options = {}) ⇒ Object



169
170
171
# File 'lib/dxopal/window.rb', line 169

def self.draw_ex(x, y, image, options={})
  enqueue_draw(options[:z] || 0, :draw_ex, x, y, image, options)
end

.draw_font(x, y, string, font, option = {}) ⇒ Object



173
174
175
176
177
# File 'lib/dxopal/window.rb', line 173

def self.draw_font(x, y, string, font, option={})
  z = option[:z] || 0
  color = option[:color] || [255, 255, 255]
  enqueue_draw(z, :font, x, y, string, font, color)
end

.draw_line(x1, y1, x2, y2, color, z = 0) ⇒ Object



183
184
185
# File 'lib/dxopal/window.rb', line 183

def self.draw_line(x1, y1, x2, y2, color, z=0)
  enqueue_draw(z, :line, x1, y1, x2, y2, color)
end

.draw_pause_screenObject



47
48
49
50
# File 'lib/dxopal/window.rb', line 47

def self.draw_pause_screen
  Window.draw_box_fill(0, 0, Window.width, Window.height, C_BLACK)
  Window.draw_font(0, 0, "...PAUSE...", Font.default, color: C_WHITE)
end

.draw_pixel(x, y, color, z = 0) ⇒ Object



179
180
181
# File 'lib/dxopal/window.rb', line 179

def self.draw_pixel(x, y, color, z=0)
  enqueue_draw(z, :pixel, x, y, color)
end

.draw_rot(x, y, image, angle, center_x = nil, center_y = nil, z = 0) ⇒ Object



165
166
167
# File 'lib/dxopal/window.rb', line 165

def self.draw_rot(x, y, image, angle, center_x=nil, center_y=nil, z=0)
  enqueue_draw(z, :image_rot, x, y, image, angle, center_x, center_y)
end

.draw_scale(x, y, image, scale_x, scale_y, center_x = nil, center_y = nil, z = 0) ⇒ Object



161
162
163
# File 'lib/dxopal/window.rb', line 161

def self.draw_scale(x, y, image, scale_x, scale_y, center_x=nil, center_y=nil, z=0)
  enqueue_draw(z, :image_scale, x, y, image, scale_x, scale_y, center_x, center_y)
end

.draw_triangle(x1, y1, x2, y2, x3, y3, color, z = 0) ⇒ Object



203
204
205
# File 'lib/dxopal/window.rb', line 203

def self.draw_triangle(x1, y1, x2, y2, x3, y3, color, z=0)
  enqueue_draw(z, :triangle, x1, y1, x2, y2, x3, y3, color)
end

.draw_triangle_fill(x1, y1, x2, y2, x3, y3, color, z = 0) ⇒ Object



207
208
209
# File 'lib/dxopal/window.rb', line 207

def self.draw_triangle_fill(x1, y1, x2, y2, x3, y3, color, z=0)
  enqueue_draw(z, :triangle_fill, x1, y1, x2, y2, x3, y3, color)
end

.enqueue_draw(z, *args) ⇒ Object

(internal)



212
213
214
# File 'lib/dxopal/window.rb', line 212

def self.enqueue_draw(z, *args)
  @@draw_queue.push([z, @@draw_queue.length, *args])
end

.fpsObject



132
# File 'lib/dxopal/window.rb', line 132

def self.fps; @@fps; end

.fps=(w) ⇒ Object



133
# File 'lib/dxopal/window.rb', line 133

def self.fps=(w); @@fps = w; end

.heightObject



144
# File 'lib/dxopal/window.rb', line 144

def self.height; @@height; end

.height=(h) ⇒ Object

Set window height and resize the canvas Set ‘nil` to maximize canvas



147
148
149
150
151
152
# File 'lib/dxopal/window.rb', line 147

def self.height=(h)
  canvas = `document.getElementById("dxopal-canvas")`
  @@height = h || `window.innerHeight`
  `canvas.height = #{@@height}`
  `canvas.style.height = #{@@height}`
end

.load_resources(&block) ⇒ Object

Load resources specified with Image.register or Sound.register Call block when loaded



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

def self.load_resources(&block)
  RemoteResource._load_resources do
    DXOpal.dump_error(&block)
  end
end

.loop(&block) ⇒ Object

Start main loop

When called twice, previous loop is stopped (this is useful when implementing interactive game editor, etc.)



28
29
30
31
32
33
34
# File 'lib/dxopal/window.rb', line 28

def self.loop(&block)
  already_running = !!@@block
  @@block = block
  return if already_running
  self_ = self
  `window`.JS.requestAnimationFrame{|time| self_._loop(time) }
end

.pauseObject

(DXOpal original) Pause & resume



37
38
39
40
41
# File 'lib/dxopal/window.rb', line 37

def self.pause
  @@paused = true
  @@draw_queue.clear
  draw_pause_screen
end

.paused?Boolean

Returns:

  • (Boolean)


42
# File 'lib/dxopal/window.rb', line 42

def self.paused?; @@paused; end

.real_fpsObject



134
# File 'lib/dxopal/window.rb', line 134

def self.real_fps; @@real_fps; end

.resumeObject



43
44
45
46
# File 'lib/dxopal/window.rb', line 43

def self.resume
  raise "Window.resume is called before Window.loop" if @@block.nil?
  @@paused = false; Window.loop(&@@block)
end

.widthObject



135
# File 'lib/dxopal/window.rb', line 135

def self.width; @@width; end

.width=(w) ⇒ Object

Set window width and resize the canvas Set ‘nil` to maximize canvas



138
139
140
141
142
143
# File 'lib/dxopal/window.rb', line 138

def self.width=(w)
  canvas = `document.getElementById("dxopal-canvas")`
  @@width = w || `window.innerWidth`
  `canvas.width = #{@@width}`
  `canvas.style.width = #{@@width}`
end