Class: Graphics::FPSBalancer

Inherits:
Object
  • Object
show all
Defined in:
scripts/00700 Ajout_PSDK/00002 FPSBalancer.rb

Overview

Class helping to balance FPS on FPS based things

Defined Under Namespace

Modules: Marker

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFPSBalancer

Create a new FPSBalancer



7
8
9
10
11
12
13
14
15
16
# File 'scripts/00700 Ajout_PSDK/00002 FPSBalancer.rb', line 7

def initialize
  # Tell the number of frame to execute
  @frame_to_execute = 0
  # Get the last framerate
  @last_frame_rate = 0
  # Get the frame delta in usec
  @frame_delta = 1
  # Get the last interval index when the graphics were updated
  @last_interval_index = 0
end

Class Attribute Details

.globalFPSBalancer (readonly)

Get the global balancer

Returns:



74
75
76
# File 'scripts/00700 Ajout_PSDK/00002 FPSBalancer.rb', line 74

def global
  @global
end

.globally_enabledBoolean

Get if the FPS balancing is globally enabled

Returns:

  • (Boolean)


68
69
70
# File 'scripts/00700 Ajout_PSDK/00002 FPSBalancer.rb', line 68

def globally_enabled
  @globally_enabled
end

.last_f3_upTime

Get last time F3 was pressed

Returns:

  • (Time)


71
72
73
# File 'scripts/00700 Ajout_PSDK/00002 FPSBalancer.rb', line 71

def last_f3_up
  @last_f3_up
end

Instance Method Details

#run(&block)

Run code according to FPS Balancing (block will be executed only if it's ok)

Parameters:

  • block (Proc)

    code to execute as much as needed



40
41
42
43
44
45
# File 'scripts/00700 Ajout_PSDK/00002 FPSBalancer.rb', line 40

def run(&block)
  return unless block_given?
  return block.call unless FPSBalancer.globally_enabled

  @frame_to_execute.times(&block)
end

#skipping?Boolean

Tell if the balancer is skipping frames

Returns:

  • (Boolean)


48
49
50
# File 'scripts/00700 Ajout_PSDK/00002 FPSBalancer.rb', line 48

def skipping?
  FPSBalancer.globally_enabled && @frame_to_execute == 0
end

#update

Update the metrics of the FPSBalancer



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'scripts/00700 Ajout_PSDK/00002 FPSBalancer.rb', line 19

def update
  update_intervals if @last_frame_rate != Graphics.frame_rate
  current_index = (Graphics.current_time.usec / @frame_delta).floor
  if current_index == @last_interval_index
    @frame_to_execute = 0
  elsif current_index > @last_interval_index
    @frame_to_execute = current_index - @last_interval_index
  else
    @frame_to_execute = Graphics.frame_rate - @last_interval_index + current_index
  end
  @last_interval_index = current_index
  if Sf::Keyboard.press?(Sf::Keyboard::F3)
    FPSBalancer.last_f3_up = Graphics.current_time
  elsif FPSBalancer.last_f3_up == Graphics.last_time
    FPSBalancer.globally_enabled = !FPSBalancer.globally_enabled
    FPSBalancer.last_f3_up -= 1
  end
end