ippa-movie_maker 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History.txt +27 -0
- data/README.rdoc +154 -0
- data/Rakefile +19 -0
- data/examples/axe.rb +24 -0
- data/examples/balls.rb +20 -0
- data/examples/echoes.rb +30 -0
- data/examples/gosu_base.rb +40 -0
- data/examples/gosu_rain.rb +34 -0
- data/examples/gosu_river_of_stars.rb +35 -0
- data/examples/gosu_triangles.rb +34 -0
- data/examples/gosu_wish_upon_a_star.rb +48 -0
- data/examples/ippagaming_intro.rb +23 -0
- data/examples/media/23700__hazure__chop.wav +0 -0
- data/examples/media/Establo.ttf +0 -0
- data/examples/media/FeaturedItem.ttf +0 -0
- data/examples/media/FreeSans.ttf +0 -0
- data/examples/media/Hursheys.ttf +0 -0
- data/examples/media/axe.png +0 -0
- data/examples/media/axe.svg +136 -0
- data/examples/media/ball.png +0 -0
- data/examples/media/ball.svg +182 -0
- data/examples/media/black.bmp +0 -0
- data/examples/media/blue_triangle.png +0 -0
- data/examples/media/blue_triangle.svg +81 -0
- data/examples/media/chop.wav +0 -0
- data/examples/media/cloth_background.png +0 -0
- data/examples/media/drawing.svg +86 -0
- data/examples/media/drip.wav +0 -0
- data/examples/media/green_triangle.png +0 -0
- data/examples/media/green_triangle.svg +104 -0
- data/examples/media/hit.wav +0 -0
- data/examples/media/ippa_gaming.png +0 -0
- data/examples/media/ippa_gaming.svg +463 -0
- data/examples/media/oil_drip.wav +0 -0
- data/examples/media/outdoor_scene.bmp +0 -0
- data/examples/media/outdoor_scene.png +0 -0
- data/examples/media/outdoor_scene.svg +316 -0
- data/examples/media/rain-bak1.wav +0 -0
- data/examples/media/rain.wav +0 -0
- data/examples/media/rain2.wav +0 -0
- data/examples/media/raindrop.png +0 -0
- data/examples/media/raindrop.svg +87 -0
- data/examples/media/raindrop_small.bmp +0 -0
- data/examples/media/raindrop_small.png +0 -0
- data/examples/media/red_triangle.png +0 -0
- data/examples/media/red_triangle.svg +81 -0
- data/examples/media/spaceship_noalpha.png +0 -0
- data/examples/media/star_5.png +0 -0
- data/examples/media/star_5.svg +83 -0
- data/examples/media/star_6.png +0 -0
- data/examples/media/star_6.svg +86 -0
- data/examples/rain.rb +26 -0
- data/examples/rain_advanced.rb +26 -0
- data/examples/rubygame_river_of_stars.rb +28 -0
- data/examples/zoom.rb +27 -0
- data/lib/movie_maker.rb +411 -0
- data/lib/movie_maker/action.rb +389 -0
- data/lib/movie_maker/core_extensions.rb +17 -0
- data/lib/movie_maker/gosu_autoload.rb +38 -0
- data/lib/movie_maker/gosu_clock.rb +139 -0
- data/lib/movie_maker/named_resource.rb +254 -0
- data/lib/movie_maker/sprite.rb +157 -0
- metadata +126 -0
@@ -0,0 +1,389 @@
|
|
1
|
+
#
|
2
|
+
# One Action specifies a certain action, for example the movement of a sprite
|
3
|
+
# Or to show a sprite, rotate, zoom or hide etc
|
4
|
+
#
|
5
|
+
#
|
6
|
+
module MovieMaker
|
7
|
+
module Action
|
8
|
+
#
|
9
|
+
# All actions inherit from this base-class and Should call super in their initialize.
|
10
|
+
# Takes an option-hash:
|
11
|
+
# :start_at - start at X millisec into the movie
|
12
|
+
# :stop_at - stop at X millisec into the movie
|
13
|
+
# :sprite - the actuall spriteobject, needs to answer to x,x=,y,y= and image
|
14
|
+
# :screen - the screenobject, so it knows where to blit itself
|
15
|
+
# :background - the backgroundsurface so it can undraw itself properly
|
16
|
+
#
|
17
|
+
#
|
18
|
+
class SpriteAction
|
19
|
+
attr_accessor :sprite, :background, :screen
|
20
|
+
attr_reader :start_at, :stop_at, :image
|
21
|
+
def initialize(options = {}, *ignore)
|
22
|
+
@sprite = options[:object]
|
23
|
+
@background = options[:background]
|
24
|
+
@screen = options[:screen]
|
25
|
+
@start_at = (options[:start_at]||0) * 1000
|
26
|
+
@stop_at = (options[:stop_at]||0) * 1000
|
27
|
+
@cache = options[:cache] || false
|
28
|
+
@framework = options[:framework] || :rubygame
|
29
|
+
|
30
|
+
@duration = @stop_at - @start_at
|
31
|
+
@playing = true
|
32
|
+
@finalized = false
|
33
|
+
@setup_done = false
|
34
|
+
@image = @sprite.image # used in MovieMaker#play
|
35
|
+
end
|
36
|
+
|
37
|
+
def setup
|
38
|
+
@setup_done = true
|
39
|
+
end
|
40
|
+
|
41
|
+
def finalize
|
42
|
+
@finalized = true
|
43
|
+
end
|
44
|
+
|
45
|
+
def finalized?
|
46
|
+
@finalized
|
47
|
+
end
|
48
|
+
|
49
|
+
def started?(current_time)
|
50
|
+
current_time >= self.start_at
|
51
|
+
end
|
52
|
+
|
53
|
+
def playing?(current_time)
|
54
|
+
@playing and (current_time >= self.start_at) and (current_time < self.stop_at)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
#
|
59
|
+
# Moves a sprite to a set of coordinates
|
60
|
+
#
|
61
|
+
class Move < SpriteAction
|
62
|
+
|
63
|
+
def initialize(options = {}, coordinates = [0,0])
|
64
|
+
super(options)
|
65
|
+
@to_x = coordinates[0]
|
66
|
+
@to_y = coordinates[1]
|
67
|
+
@prev_time = 0
|
68
|
+
end
|
69
|
+
|
70
|
+
def setup
|
71
|
+
@from_x = @sprite.x
|
72
|
+
@from_y = @sprite.y
|
73
|
+
@x_step = (@to_x - @from_x).to_f / @duration.to_f
|
74
|
+
@y_step = (@to_y - @from_y).to_f / @duration.to_f
|
75
|
+
@setup_done = true
|
76
|
+
end
|
77
|
+
|
78
|
+
# The core of the MoveClass, the actual move-logic
|
79
|
+
def update(time)
|
80
|
+
setup unless @setup_done
|
81
|
+
|
82
|
+
@diff = (time - @prev_time)
|
83
|
+
@prev_time = time
|
84
|
+
|
85
|
+
@sprite.x += @diff * @x_step.to_f
|
86
|
+
@sprite.y += @diff * @y_step.to_f
|
87
|
+
end
|
88
|
+
|
89
|
+
def finalize
|
90
|
+
@sprite.x = @to_x
|
91
|
+
@sprite.y = @to_y
|
92
|
+
@finalized = true
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
#
|
98
|
+
# ACCELERATION
|
99
|
+
#
|
100
|
+
class Acceleration < SpriteAction
|
101
|
+
def initialize(options = {}, acceleration = [0,0])
|
102
|
+
super(options)
|
103
|
+
@acceleration_x = acceleration[0]
|
104
|
+
@acceleration_y = acceleration[1]
|
105
|
+
@velocity_x = @acceleration_x
|
106
|
+
@velocity_y = @acceleration_y
|
107
|
+
end
|
108
|
+
|
109
|
+
def update(time)
|
110
|
+
@velocity_x += @acceleration_x
|
111
|
+
@velocity_y += @acceleration_y
|
112
|
+
|
113
|
+
@sprite.x += @velocity_x
|
114
|
+
@sprite.y += @velocity_y
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
#
|
119
|
+
# ACCELERATE
|
120
|
+
#
|
121
|
+
class Velocity < SpriteAction
|
122
|
+
def initialize(options = {}, velocity = [0,0])
|
123
|
+
super(options)
|
124
|
+
@velocity_x = velocity[0]
|
125
|
+
@velocity_y = velocity[1]
|
126
|
+
end
|
127
|
+
|
128
|
+
def update(time)
|
129
|
+
@sprite.x += @velocity_x
|
130
|
+
@sprite.y += @velocity_y
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
#
|
135
|
+
# ROTATE
|
136
|
+
#
|
137
|
+
class Rotate < SpriteAction
|
138
|
+
attr_reader :direction
|
139
|
+
def initialize(options = {}, angle = 360)
|
140
|
+
super(options)
|
141
|
+
@angle = angle
|
142
|
+
end
|
143
|
+
|
144
|
+
def setup
|
145
|
+
@angle_step = @angle.to_f / @duration.to_f
|
146
|
+
@angle_step = -@angle_step if @framework == :rubygame
|
147
|
+
@setup_done = true
|
148
|
+
end
|
149
|
+
|
150
|
+
def update(time)
|
151
|
+
setup unless @setup_done
|
152
|
+
@sprite.angle = (@angle_step * time)
|
153
|
+
end
|
154
|
+
|
155
|
+
def finalize
|
156
|
+
@sprite.angle = @angle
|
157
|
+
@finalized = true
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
# Zoom a sprite
|
162
|
+
class Zoom < SpriteAction
|
163
|
+
|
164
|
+
def initialize(options = {}, factor = 1)
|
165
|
+
super(options)
|
166
|
+
@factor = factor
|
167
|
+
end
|
168
|
+
|
169
|
+
def setup
|
170
|
+
@scale_from = @sprite.width_scaling || 1
|
171
|
+
@scale = (@scale_from - @factor).abs
|
172
|
+
|
173
|
+
@scale_step = @scale.to_f / @duration.to_f
|
174
|
+
@scale_step = -@scale_step if @factor < @scale_from
|
175
|
+
|
176
|
+
@setup_done = true
|
177
|
+
end
|
178
|
+
|
179
|
+
def update(time)
|
180
|
+
setup unless @setup_done
|
181
|
+
|
182
|
+
scale = @scale_from + @scale_step * time
|
183
|
+
#puts "#{scale} = #{@scale_from} + #{@scale_step} * #{time}"
|
184
|
+
@sprite.width_scaling = scale
|
185
|
+
@sprite.height_scaling = scale
|
186
|
+
end
|
187
|
+
|
188
|
+
def finalize
|
189
|
+
@sprite.width_scaling = @factor
|
190
|
+
@sprite.height_scaling = @factor
|
191
|
+
@finalized = true
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
#
|
196
|
+
# Moves a sprite from X,Y --> X2,Y2
|
197
|
+
#
|
198
|
+
class MoveFacingDirection < SpriteAction
|
199
|
+
|
200
|
+
def initialize(options = {}, coordinates = [0,0])
|
201
|
+
super(options)
|
202
|
+
@to_x = coordinates[0]
|
203
|
+
@to_y = coordinates[1]
|
204
|
+
end
|
205
|
+
|
206
|
+
def setup
|
207
|
+
@from_x = @sprite.x
|
208
|
+
@from_y = @sprite.y
|
209
|
+
|
210
|
+
@x_step = (@to_x - @from_x).to_f / @duration.to_f
|
211
|
+
@y_step = (@to_y - @from_y).to_f / @duration.to_f
|
212
|
+
|
213
|
+
#
|
214
|
+
# investigate this further later ...
|
215
|
+
#
|
216
|
+
@sprite.angle = (Math.atan(@y_step / @x_step) * 180.0/Math::PI) + 315
|
217
|
+
@sprite.angle -= 45 if @framework == :gosu
|
218
|
+
|
219
|
+
@setup_done = true
|
220
|
+
end
|
221
|
+
|
222
|
+
# The core of the MoveClass, the actual move-logic
|
223
|
+
def update(time)
|
224
|
+
setup unless @setup_done
|
225
|
+
|
226
|
+
@sprite.x = @from_x + time * @x_step
|
227
|
+
@sprite.y = @from_y + time * @y_step
|
228
|
+
end
|
229
|
+
|
230
|
+
def finalize
|
231
|
+
@finalized = true
|
232
|
+
end
|
233
|
+
|
234
|
+
end
|
235
|
+
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
|
240
|
+
# Shows a sprite
|
241
|
+
class Show < SpriteAction
|
242
|
+
def initialize(options = {})
|
243
|
+
super
|
244
|
+
end
|
245
|
+
|
246
|
+
def update(time)
|
247
|
+
end
|
248
|
+
end
|
249
|
+
|
250
|
+
|
251
|
+
# Fades a sprite
|
252
|
+
class FadeTo < SpriteAction
|
253
|
+
|
254
|
+
def initialize(options = {})
|
255
|
+
super(options)
|
256
|
+
@from = options[:from]
|
257
|
+
@to = options[:to]
|
258
|
+
@alpha = 255
|
259
|
+
end
|
260
|
+
|
261
|
+
def update(time)
|
262
|
+
@sprite.image.set_alpha(@alpha)
|
263
|
+
@alpha -= 1 if @alpha > 0
|
264
|
+
end
|
265
|
+
end
|
266
|
+
|
267
|
+
#
|
268
|
+
# PULSATE
|
269
|
+
#
|
270
|
+
class Pulsate < SpriteAction
|
271
|
+
attr_reader :direction
|
272
|
+
def initialize(options = {})
|
273
|
+
super(options)
|
274
|
+
@pulse_duration = options[:duration] || 1
|
275
|
+
@times = options[:times] || 1
|
276
|
+
end
|
277
|
+
|
278
|
+
def setup
|
279
|
+
@setup_done = true
|
280
|
+
end
|
281
|
+
|
282
|
+
def update(time)
|
283
|
+
setup unless @setup_done
|
284
|
+
end
|
285
|
+
|
286
|
+
end
|
287
|
+
|
288
|
+
#
|
289
|
+
# Fades color to total trans
|
290
|
+
#
|
291
|
+
class FadeOut < SpriteAction
|
292
|
+
def initialize(options = {}, color = 0xFFFFFFFF)
|
293
|
+
super(options)
|
294
|
+
@sprite = options[:object]
|
295
|
+
@prev_time = 0
|
296
|
+
end
|
297
|
+
|
298
|
+
def setup
|
299
|
+
@alpha_step = @sprite.color.alpha / @duration
|
300
|
+
@setup_done = true
|
301
|
+
end
|
302
|
+
|
303
|
+
def update(time)
|
304
|
+
setup unless @setup_done
|
305
|
+
|
306
|
+
@diff = (time - @prev_time)
|
307
|
+
@prev_time = time
|
308
|
+
|
309
|
+
@sprite.color.alpha = @sprite.color.alpha - (@diff.to_f * @alpha_step.to_f).to_i
|
310
|
+
end
|
311
|
+
|
312
|
+
def finalize
|
313
|
+
@sprite.color.alpha = 0
|
314
|
+
@finalized = true
|
315
|
+
end
|
316
|
+
end
|
317
|
+
|
318
|
+
#
|
319
|
+
#
|
320
|
+
#
|
321
|
+
class SimpleAction
|
322
|
+
attr_reader :start_at, :stop_at
|
323
|
+
def initialize(options = {})
|
324
|
+
@start_at = (options[:start_at]||0) * 1000
|
325
|
+
@stop_at = (options[:stop_at]||0) * 1000
|
326
|
+
@duration = @stop_at - @start_at
|
327
|
+
@finalized = false
|
328
|
+
end
|
329
|
+
|
330
|
+
def started?(current_time)
|
331
|
+
current_time > self.start_at
|
332
|
+
end
|
333
|
+
|
334
|
+
def finalized?
|
335
|
+
@finalized
|
336
|
+
end
|
337
|
+
|
338
|
+
end
|
339
|
+
|
340
|
+
#
|
341
|
+
# Sets Color
|
342
|
+
#
|
343
|
+
class Color < SimpleAction
|
344
|
+
def initialize(options = {}, color = 0xFFFFFFFF)
|
345
|
+
super(options)
|
346
|
+
@sprite = options[:object]
|
347
|
+
@color = color
|
348
|
+
@color = ::Gosu::Color.new(color) if color.is_a? Fixnum
|
349
|
+
end
|
350
|
+
def finalize
|
351
|
+
@sprite.color = @color
|
352
|
+
@finalized = true
|
353
|
+
end
|
354
|
+
end
|
355
|
+
|
356
|
+
|
357
|
+
#
|
358
|
+
# Plays a sound
|
359
|
+
#
|
360
|
+
class PlaySound < SimpleAction
|
361
|
+
attr_reader :playing
|
362
|
+
def initialize(options = {}, sound = nil)
|
363
|
+
super(options)
|
364
|
+
@sound = sound || options[:object]
|
365
|
+
@volume = options[:volume] || 1.0
|
366
|
+
@repeats = options[:repeats] || 1
|
367
|
+
@fade_in = options[:fade_in] || nil
|
368
|
+
@stop_after = @duration
|
369
|
+
|
370
|
+
@sound.volume = @volume if @sound.respond_to? :volume
|
371
|
+
end
|
372
|
+
|
373
|
+
def playing?(current_time)
|
374
|
+
@playing
|
375
|
+
end
|
376
|
+
|
377
|
+
def finalize
|
378
|
+
@sound.play
|
379
|
+
@finalized = true
|
380
|
+
end
|
381
|
+
|
382
|
+
def stop
|
383
|
+
@sound.stop
|
384
|
+
end
|
385
|
+
|
386
|
+
end
|
387
|
+
|
388
|
+
end
|
389
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Rubygame
|
2
|
+
class Surface
|
3
|
+
|
4
|
+
def rotozoom_cached(angle, zoom, smooth=true, file="default")
|
5
|
+
@@rotozoom_cached_surfaces ||= {}
|
6
|
+
@@rotozoom_cached_surfaces[file] ||= []
|
7
|
+
key = angle.to_i.abs
|
8
|
+
|
9
|
+
## DEBUG
|
10
|
+
#puts "[#{file}][#{key}]: HIT" if @@rotozoom_cached_surfaces[file][key]
|
11
|
+
#puts "[#{file}][#{key}]: MISS" if !@@rotozoom_cached_surfaces[file][key]
|
12
|
+
|
13
|
+
@@rotozoom_cached_surfaces[file][key] ||= self.rotozoom(angle, zoom, smooth)
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
#
|
2
|
+
# Rubygames Named Resources for GOSU
|
3
|
+
# Assumes a global variable $screen having the Gosu::Window instance.
|
4
|
+
# Quick 'n easy access to sprites, sounds and tiles!
|
5
|
+
#
|
6
|
+
begin
|
7
|
+
require 'named_resource' # Part of rubygame 2.3+
|
8
|
+
rescue
|
9
|
+
require 'rubygame'
|
10
|
+
end
|
11
|
+
include Gosu
|
12
|
+
|
13
|
+
class Image
|
14
|
+
include Rubygame::NamedResource
|
15
|
+
|
16
|
+
def self.autoload(name)
|
17
|
+
(path = find_file(name)) ? Image.new($screen, path, true) : nil
|
18
|
+
end
|
19
|
+
end
|
20
|
+
Surface = Image
|
21
|
+
|
22
|
+
class Sample
|
23
|
+
include Rubygame::NamedResource
|
24
|
+
|
25
|
+
|
26
|
+
def self.autoload(name)
|
27
|
+
(path = find_file(name)) ? Sample.new($screen, path) : nil
|
28
|
+
end
|
29
|
+
end
|
30
|
+
Sound = Sample
|
31
|
+
|
32
|
+
class Tile
|
33
|
+
include Rubygame::NamedResource
|
34
|
+
|
35
|
+
def self.autoload(name)
|
36
|
+
(path = find_file(name)) ? Image.load_tiles($screen, path, 32, 32, true) : nil
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,139 @@
|
|
1
|
+
#--
|
2
|
+
# Rubygame -- Ruby code and bindings to SDL to facilitate game creation
|
3
|
+
# Copyright (C) 2004-2007 John Croisant
|
4
|
+
#
|
5
|
+
# This library is free software; you can redistribute it and/or
|
6
|
+
# modify it under the terms of the GNU Lesser General Public
|
7
|
+
# License as published by the Free Software Foundation; either
|
8
|
+
# version 2.1 of the License, or (at your option) any later version.
|
9
|
+
#
|
10
|
+
# This library is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
13
|
+
# Lesser General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU Lesser General Public
|
16
|
+
# License along with this library; if not, write to the Free Software
|
17
|
+
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
18
|
+
#++
|
19
|
+
|
20
|
+
module Gosu
|
21
|
+
# Clock provides class methods for tracking running time and delaying
|
22
|
+
# execution of the program for specified time periods. This is used to
|
23
|
+
# provide a consistent framerate, prevent the program from using
|
24
|
+
# all the processor time, etc.
|
25
|
+
#
|
26
|
+
# Clock also provides instance methods to make it convenient to
|
27
|
+
# monitor and limit application framerate. See #tick.
|
28
|
+
class Clock
|
29
|
+
# The runtime when the Clock was initialized.
|
30
|
+
attr_reader :start
|
31
|
+
# The number of times #tick has been called.
|
32
|
+
attr_reader :ticks
|
33
|
+
|
34
|
+
# Make Rubygame::Clock work with GOSUs "milliseconds"
|
35
|
+
def self.runtime
|
36
|
+
Gosu::milliseconds
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.delay(time)
|
40
|
+
sleep_time = time/1000.0
|
41
|
+
sleep_time = 0 if sleep_time < 0
|
42
|
+
sleep(sleep_time)
|
43
|
+
end
|
44
|
+
|
45
|
+
# Create a new Clock instance.
|
46
|
+
def initialize()
|
47
|
+
@start = Clock.runtime()
|
48
|
+
@last_tick = @start
|
49
|
+
@ticks = 0
|
50
|
+
@target_frametime = nil
|
51
|
+
yield self if block_given?
|
52
|
+
end
|
53
|
+
|
54
|
+
# The target frametime (milliseconds/frame). See #tick
|
55
|
+
attr_accessor :target_frametime
|
56
|
+
|
57
|
+
# Returns the current target framerate (frames/second).
|
58
|
+
# This is an alternate way to access @target_frametime.
|
59
|
+
# Same as: 1000.0 / #target_frametime
|
60
|
+
def target_framerate
|
61
|
+
if @target_frametime
|
62
|
+
1000.0 / @target_frametime
|
63
|
+
else
|
64
|
+
nil
|
65
|
+
end
|
66
|
+
rescue ZeroDivisionError
|
67
|
+
return nil
|
68
|
+
end
|
69
|
+
|
70
|
+
# Sets the target number of frames per second to +framerate+.
|
71
|
+
# This is an alternate way to access @target_frametime.
|
72
|
+
# Same as: #target_frametime = 1000.0 / framerate
|
73
|
+
def target_framerate=( framerate )
|
74
|
+
if framerate
|
75
|
+
@target_frametime = 1000.0 / framerate
|
76
|
+
else
|
77
|
+
@target_frametime = nil
|
78
|
+
end
|
79
|
+
rescue ZeroDivisionError
|
80
|
+
@target_frametime = nil
|
81
|
+
end
|
82
|
+
|
83
|
+
# call-seq: lifetime() -> Numeric
|
84
|
+
#
|
85
|
+
# Returns time in milliseconds since this Clock instance was created.
|
86
|
+
def lifetime
|
87
|
+
Clock.runtime() - @start
|
88
|
+
end
|
89
|
+
|
90
|
+
# call-seq: framerate() -> Numeric
|
91
|
+
#
|
92
|
+
# Return the actual framerate (frames per second) recorded by the Clock.
|
93
|
+
# See #tick.
|
94
|
+
#
|
95
|
+
# TODO: sample only a few seconds in the past, instead of the
|
96
|
+
# entire lifetime of the Clock.
|
97
|
+
def framerate
|
98
|
+
# below is same as: return @ticks / (lifetime / 1000.0)
|
99
|
+
return 1000.0 * @ticks / lifetime()
|
100
|
+
rescue ZeroDivisionError
|
101
|
+
return 0
|
102
|
+
end
|
103
|
+
|
104
|
+
# Returns the number of milliseconds since you last called this method.
|
105
|
+
#
|
106
|
+
# You must call this method once per frame (i.e. per iteration of
|
107
|
+
# your main loop) if you want to use the framerate monitoring and/or
|
108
|
+
# framerate limiting features.
|
109
|
+
#
|
110
|
+
# Framerate monitoring allows you to check the framerate (frames per
|
111
|
+
# second) with the #framerate method.
|
112
|
+
#
|
113
|
+
# Framerate limiting allows you to prevent the application from running
|
114
|
+
# too fast (and using 100% of processor time) by pausing the program
|
115
|
+
# very briefly each frame. The pause duration is calculated each frame
|
116
|
+
# to maintain a constant framerate.
|
117
|
+
#
|
118
|
+
# Framerate limiting is only enabled if you have set the
|
119
|
+
# #target_framerate= or #target_frametime=.
|
120
|
+
# If you have done that, this method will automatically perform the
|
121
|
+
# delay each time you call it.
|
122
|
+
#
|
123
|
+
# (Please note that no effort is made to correct a framerate
|
124
|
+
# which is *slower* than the target framerate. Clock can't
|
125
|
+
# make your code run faster, only slow it down if it is
|
126
|
+
# running too fast.)
|
127
|
+
def tick()
|
128
|
+
passed = Clock.runtime() - @last_tick # how long since the last tick?
|
129
|
+
if @target_frametime
|
130
|
+
return Clock.delay(@target_frametime - passed) + passed
|
131
|
+
end
|
132
|
+
return passed
|
133
|
+
ensure
|
134
|
+
@last_tick = Clock.runtime()
|
135
|
+
@ticks += 1
|
136
|
+
end
|
137
|
+
|
138
|
+
end # class Clock
|
139
|
+
end #module Gosu
|