[Tutorial] Fades

Hi :3
I'll explain how to make "fades" in an easy way, ill use a 640x480 resolution.

first you need a scene system like this:
viewtopic.php?f=191&t=67246

then we will make a scene called Transition with this content:

  1. include Gosu
  2. class Transition
  3.     attr_reader :screen_x
  4.     attr_reader :screen_y
  5.  
  6.     def initialize(scene, type)
  7.         @next_scene = scene
  8.         @time = 0
  9.         @fading = type
  10.         @fade_time = 255 if @fading == :in
  11.         @fade_time = 0 if @fading == :out
  12.         @color = Color.new(@fade_time, 0, 0 ,0)
  13.     end
  14.    
  15.     def button_down(id)
  16.     end
  17.    
  18.     def update
  19.         @color = Color.new(@fade_time, 0, 0 ,0)
  20.         case @fading
  21.            
  22.         when :in
  23.             if @fade_time <= 0
  24.                 $scene = @next_scene
  25.             else
  26.                 @fade_time -= 15 # 15 is cool
  27.             end
  28.         when :out
  29.             if @fade_time >= 255
  30.                 $scene = @next_scene
  31.             else
  32.                 @fade_time += 15 # 15 is cool
  33.             end
  34.         end
  35.            
  36.         self.draw
  37.     end
  38.    
  39.     def draw
  40.         @next_scene.update
  41.         $window.draw_quad(0, 0, @color, 640, 0, @color, 0, 480, @color, 640, 480, @color, 500)  
  42.     end
  43.    
  44. end
  45.  
Expand to see the code.


If you see the code you will know that the fade is really a black image which is increasing (or decreasing) its opacity.
Fades are normally short, because while the fade is processing, the update is running and can origin problems =)

The correct use is: $scene = Transition.new(next_scene, type_of_fade)
$scene = Transition.new(Porn_Room.new, :in/:out)
Something sortof related but not related, am I right in thinking we don't have to dispose things in Gosu?

draw_quad for example, it draws the quad but then will disappear on the next frame unless it's drawn again, right?
Gosu images are not like RGSS Sprites.

If you know, RGSS have two types of images, bitmaps and sprites.

Bitmaps need a rect to be showed. Gosu makes Bitmaps with its own rect to be showed, so dont need to dispose nothing =)
But yes, from experience, if you don't draw it on every tick it WILL disappear. Or maybe I'm doing it wrong? If I don't redraw images, they disappear; now I haven't tried using the Window's draw_quad or draw_line method; do these stay until painted over?
Gosu includes at the beginning of all frames something like

"self.contents.clear" so you need to redraw all the things you want in the screen.

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=67313