Module: Graphics

Extended by:
Hooks
Includes:
Hooks
Defined in:
scripts/00000 Dependencies/00001 LiteRGSS2/00001 Graphics.rb,
scripts/00700 Ajout_PSDK/00002 FPSBalancer.rb,
scripts/00700 Ajout_PSDK/00001 Graphics Icon.rb,
scripts/00700 Ajout_PSDK/00001 Graphics Debug.rb,
scripts/00700 Ajout_PSDK/00001 Graphics FPS+Mouse.rb

Overview

Module responsive of showing graphics into the main window

Defined Under Namespace

Classes: FPSBalancer

Constant Summary collapse

TRANSITION_FRAG_SHADER =

Shader used to perform transition

<<~EOFRAGMENT
  uniform float param;
  uniform sampler2D texture;
  uniform sampler2D transition;
  uniform sampler2D nextFrame;
  const float sensibilite = 0.05;
  const float scale = 1.0 + sensibilite;
  void main()
  {
    vec4 frag = texture2D(texture, gl_TexCoord[0].xy);
    vec4 tran = texture2D(transition, gl_TexCoord[0].xy);
    float pixel = max(max(tran.r, tran.g), tran.b);
    pixel -= (param * scale);
    if(pixel < sensibilite)
    {
      vec4 nextFrag = texture2D(nextFrame, gl_TexCoord[0].xy);
      frag = mix(frag, nextFrag, max(0.0, sensibilite + pixel / sensibilite));
    }
    gl_FragColor = frag;
  }
EOFRAGMENT
STATIC_TRANSITION_FRAG_SHADER =

Shader used to perform static transition

<<~EOFRAGMENT
  uniform float param;
  uniform sampler2D texture;
  uniform sampler2D nextFrame;
  void main()
  {
    vec4 frag = texture2D(texture, gl_TexCoord[0].xy);
    vec4 nextFrag = texture2D(nextFrame, gl_TexCoord[0].xy);
    frag = mix(frag, nextFrag, max(0.0, param));
    gl_FragColor = frag;
  }
EOFRAGMENT

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Hooks

exec_hooks, force_return, included, register, remove, remove_without_name

Class Attribute Details

.current_timeTime (readonly)

Get the current time

Returns:

  • (Time)


35
36
37
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Graphics.rb', line 35

def current_time
  @current_time
end

.frame_countInteger

Get the global frame count

Returns:



29
30
31
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Graphics.rb', line 29

def frame_count
  @frame_count
end

.frame_rateInteger

Get the framerate

Returns:



32
33
34
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Graphics.rb', line 32

def frame_rate
  @frame_rate
end

.fullscreen_toggle_enabled

Tell if it is allowed to go fullscreen with ALT+ENTER



40
41
42
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Graphics.rb', line 40

def fullscreen_toggle_enabled
  @fullscreen_toggle_enabled
end

.last_timeTime (readonly)

Get the time when the last frame was executed

Returns:

  • (Time)


38
39
40
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Graphics.rb', line 38

def last_time
  @last_time
end

.windowLiteRGSS::DisplayWindow (readonly)

Get the game window



26
27
28
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Graphics.rb', line 26

def window
  @window
end

Class Method Details

.brightnessInteger

Get the brightness of the main game window

Returns:



62
63
64
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Graphics.rb', line 62

def brightness
  return window&.brightness || 0
end

.brightness=(brightness)

Set the brightness of the main game window

Parameters:



68
69
70
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Graphics.rb', line 68

def brightness=(brightness)
  window&.brightness = brightness
end

.deltaFloat

Tell how much time there was since last frame

Returns:

  • (Float)


56
57
58
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Graphics.rb', line 56

def delta
  return @current_time - @last_time
end

.focus?Boolean

Tell if the graphics window has focus

Returns:

  • (Boolean)


44
45
46
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Graphics.rb', line 44

def focus?
  return @has_focus
end

.frame_reset

Reset frame counter (for FPS reason)



269
270
271
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Graphics.rb', line 269

def frame_reset
  exec_hooks(Graphics, :frame_reset, binding)
end

.freeze

Freeze the graphics



97
98
99
100
101
102
103
104
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Graphics.rb', line 97

def freeze
  return unless @window

  @frozen_sprite.dispose if @frozen_sprite && !@frozen_sprite.disposed?
  @frozen_sprite = LiteRGSS::ShaderedSprite.new(window)
  @frozen_sprite.bitmap = snap_to_bitmap
  @frozen = 10
end

.frozen?Boolean

Tell if the graphics are frozen

Returns:

  • (Boolean)


50
51
52
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Graphics.rb', line 50

def frozen?
  @frozen > 0
end

.heightInteger

Get the height of the graphics

Returns:



74
75
76
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Graphics.rb', line 74

def height
  return window.height
end

.init_sprite

Init the Sprite used by the Graphics module



274
275
276
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Graphics.rb', line 274

def init_sprite
  exec_hooks(Graphics, :init_sprite, binding)
end

.load_icon

Load the window icon



8
9
10
11
12
13
14
15
16
17
18
# File 'scripts/00700 Ajout_PSDK/00001 Graphics Icon.rb', line 8

def load_icon
  return unless RPG::Cache.icon_exist?('game')

  # @type [Yuki::VD, nil]
  windowskin_vd = RPG::Cache.instance_variable_get(:@icon_data)
  data = windowskin_vd&.read_data('game')
  # @type [Image]
  image = data ? Image.new(data, true) : Image.new('graphics/icons/game.png')
  window.icon = image
  image.dispose
end

.on_start(&block)

Register an event on start of graphics

Parameters:

  • block (Proc)


246
247
248
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Graphics.rb', line 246

def on_start(&block)
  @on_start << block
end

.original_swap_fullscreen



20
# File 'scripts/00700 Ajout_PSDK/00001 Graphics Icon.rb', line 20

alias original_swap_fullscreen swap_fullscreen

.original_update



88
# File 'scripts/00700 Ajout_PSDK/00002 FPSBalancer.rb', line 88

alias original_update update

.register_viewport(viewport) ⇒ self

Register a viewport to the graphics (for special handling)

Parameters:

Returns:

  • (self)


253
254
255
256
257
258
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Graphics.rb', line 253

def register_viewport(viewport)
  return self unless viewport.is_a?(Viewport)

  @viewports << viewport unless @viewports.include?(viewport)
  return self
end

.reset_mouse_viewport

Function that resets the mouse viewport



4
5
6
# File 'scripts/00700 Ajout_PSDK/00001 Graphics FPS+Mouse.rb', line 4

def reset_mouse_viewport
  @mouse_fps_viewport&.rect&.set(0, 0, width, height)
end

.resize_screen(width, height)

Resize the window screen

Parameters:



109
110
111
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Graphics.rb', line 109

def resize_screen(width, height)
  window&.resize_screen(width, height)
end

.screen_scale=(scale)



290
291
292
293
294
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Graphics.rb', line 290

def screen_scale=(scale)
  settings = window.settings
  settings[3] = scale
  window.settings = settings
end

.shaderShader

Get the shader of the graphics

Returns:



86
87
88
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Graphics.rb', line 86

def shader
  return window&.shader
end

.shader=(shader)

Set the shader of the graphics

Parameters:



92
93
94
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Graphics.rb', line 92

def shader=(shader)
  window&.shader = shader
end

.snap_to_bitmapLiteRGSS::Bitmap

Snap the graphics to bitmap

Returns:



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Graphics.rb', line 115

def snap_to_bitmap
  all_viewport = viewports_in_order.select(&:visible)
  tmp = LiteRGSS::Viewport.new(window, 0, 0, width, height)
  bk = Image.new(width, height)
  bk.fill_rect(0, 0, width, height, Color.new(0, 0, 0, 255))
  sp = LiteRGSS::Sprite.new(tmp)
  sp.bitmap = LiteRGSS::Bitmap.new(width, height)
  bk.copy_to_bitmap(sp.bitmap)
  texture_to_dispose = all_viewport.map do |vp|
    shader = vp.shader
    vp.shader = nil
    texture = vp.snap_to_bitmap
    vp.shader = shader
    sprite = LiteRGSS::ShaderedSprite.new(tmp)
    sprite.shader = shader
    sprite.bitmap = texture
    sprite.set_position(vp.rect.x, vp.rect.y)
    next texture
  end
  texture_to_dispose << bk
  texture_to_dispose << sp.bitmap
  result_texture = tmp.snap_to_bitmap
  texture_to_dispose.each(&:dispose)
  tmp.dispose
  return result_texture
end

.sort_z

Sort the graphics in z



279
280
281
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Graphics.rb', line 279

def sort_z
  @window&.sort_z
end

.start

Start the graphics



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Graphics.rb', line 143

def start
  return if @window

  @window = LiteRGSS::DisplayWindow.new(
    Configs.infos.game_title, *PSDK_CONFIG.choose_best_resolution, PSDK_CONFIG.window_scale,
    32, 0, PSDK_CONFIG.vsync_enabled, PSDK_CONFIG.running_in_full_screen, !Configs.devices.mouse_skin
  )
  @on_start.each(&:call)
  @on_start.clear
  @last_time = @current_time = Time.new
  Input.register_events(@window)
  Mouse.register_events(@window)
  @window.on_lost_focus = proc { @has_focus = false }
  @window.on_gained_focus = proc { @has_focus = true }
  @window.on_closed = proc do
    @window = nil
    next true
  end
  init_sprite
end

.stop

Stop the graphics



165
166
167
168
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Graphics.rb', line 165

def stop
  window&.dispose
  @window = nil
end

.swap_fullscreen

Swap the fullscreen state



22
23
24
25
# File 'scripts/00700 Ajout_PSDK/00001 Graphics Icon.rb', line 22

def swap_fullscreen
  original_swap_fullscreen
  load_icon
end

.transition(frame_count_or_sec = 8, texture = nil)

Transition the graphics between a scene to another

Parameters:

  • frame_count_or_sec (Integer, Float) (defaults to: 8)

    integer = frames, float = seconds; duration of the transition

  • texture (Texture) (defaults to: nil)

    texture used to perform the transition (optional)



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Graphics.rb', line 173

def transition(frame_count_or_sec = 8, texture = nil)
  return unless @window

  exec_hooks(Graphics, :transition, binding)
  return if frame_count_or_sec <= 0 || !@frozen_sprite

  transition_internal(frame_count_or_sec, texture)
  exec_hooks(Graphics, :post_transition, binding)
rescue Hooks::ForceReturn => e
  return e.data
ensure
  @frozen_sprite&.bitmap&.dispose
  @frozen_sprite&.shader = nil
  @frozen_sprite&.dispose
  @frozen_sprite = nil
  @frozen = 0
end

.unregitser_viewport(viewport) ⇒ self

Unregister a viewport

Parameters:

Returns:

  • (self)


263
264
265
266
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Graphics.rb', line 263

def unregitser_viewport(viewport)
  @viewports.delete(viewport)
  return self
end

.update

Update graphics window content & events. This method might wait for vsync before updating events



90
91
92
93
94
95
96
97
98
99
# File 'scripts/00700 Ajout_PSDK/00002 FPSBalancer.rb', line 90

def update
  FPSBalancer.global.update
  if FPSBalancer.global.skipping? && !frozen? && $scene.is_a?(FPSBalancer::Marker)
    fps_update if respond_to?(:fps_update, true)
    update_no_input
    fps_gpu_update if respond_to?(:fps_gpu_update, true)
  else
    original_update
  end
end

.update_no_input

Update the graphics window content. This method might wait for vsync before returning



210
211
212
213
214
215
216
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Graphics.rb', line 210

def update_no_input
  return unless @window

  window.update_no_input
  @last_time = @current_time
  @current_time = Time.new
end

.update_only_input

Update the graphics window event without drawing anything.



219
220
221
222
223
224
225
226
227
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Graphics.rb', line 219

def update_only_input
  return unless @window

  Input.swap_states
  Mouse.swap_states
  window.update_only_input
  @last_time = @current_time
  @current_time = Time.new
end

.wait(frame_count_or_sec) { ... }

Make the graphics wait for an amout of time

Parameters:

  • frame_count_or_sec (Integer, Float)

    Integer => frames, Float = actual time

Yields:



232
233
234
235
236
237
238
239
240
241
242
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Graphics.rb', line 232

def wait(frame_count_or_sec)
  return unless @window

  total_time = frame_count_or_sec.is_a?(Float) ? frame_count_or_sec : frame_count_or_sec.to_f / frame_rate
  initial_time = Graphics.current_time
  next_time = initial_time + total_time
  while Graphics.current_time < next_time
    Graphics.update
    yield if block_given?
  end
end

.widthInteger

Get the width of the graphics

Returns:



80
81
82
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00001 Graphics.rb', line 80

def width
  return window.width
end