Class: Gosu::Window

Inherits:
Object
  • Object
show all
Defined in:
lib/ashton/gosu_ext/window.rb

Constant Summary collapse

WHITE_PIXEL_BLOB =
"\xFF" * 4

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Window

Returns a new instance of Window.



26
27
28
29
# File 'lib/ashton/gosu_ext/window.rb', line 26

def initialize(*args, &block)
  $window = self
  ashton_initialize(*args, &block)
end

Class Method Details

.pixelObject

An image containing a single white pixel. Useful for drawing effects (rectangle, lines, etc).



16
17
18
# File 'lib/ashton/gosu_ext/window.rb', line 16

def pixel
  @pixel ||= Gosu::Image.new $window, Ashton::ImageStub.new(WHITE_PIXEL_BLOB, 1, 1), true
end

.primary_bufferObject

Used for post-processing effects, but could be used by anyone needing to have a temporary, full-window render buffer.



9
# File 'lib/ashton/gosu_ext/window.rb', line 9

def primary_buffer; @primary_buffer ||= Ashton::WindowBuffer.new end

.secondary_bufferObject

Used for post-processing effects, but could be used by anyone needing to have a temporary, full-window render buffer.



13
# File 'lib/ashton/gosu_ext/window.rb', line 13

def secondary_buffer; @secondary_buffer ||= Ashton::WindowBuffer.new end

Instance Method Details

#ashton_initializeObject



25
# File 'lib/ashton/gosu_ext/window.rb', line 25

alias_method :ashton_initialize, :initialize

#gl(z = nil, &block) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/ashton/gosu_ext/window.rb', line 33

def gl(z = nil, &block)
  if z
    gl_not_liking_nil(z, &block)
  else
    gl_not_liking_nil(&block)
  end
end

#pixelObject



21
# File 'lib/ashton/gosu_ext/window.rb', line 21

def pixel; Window.pixel end

#post_process(*shaders) ⇒ Object

Full screen post-processing using a fragment shader.

Variables set for you in the fragment shader:

uniform sampler2D in_Texture; // Texture containing the screen image.

Raises:

  • (ArgumentError)


45
46
47
48
49
50
51
52
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
# File 'lib/ashton/gosu_ext/window.rb', line 45

def post_process(*shaders)
  raise ArgumentError, "Block required" unless block_given?
  raise TypeError, "Can only process with Shaders" unless shaders.all? {|s| s.is_a? Ashton::Shader }

  # In case no shaders are passed, just run the contents of the block.
  unless shaders.size > 0
    yield
    return
  end

  buffer1 = primary_buffer
  buffer1.clear

  # Allow user to draw into a buffer, rather than the window.
  buffer1.render do
    yield
  end

  if shaders.size > 1
    buffer2 = secondary_buffer # Don't need to clear, since we will :replace.

    # Draw into alternating buffers, applying each shader in turn.
    shaders[0...-1].each do |shader|
      buffer1, buffer2 = buffer2, buffer1
      buffer1.render do
        buffer2.draw 0, 0, nil, shader: shader, mode: :replace
      end
    end
  end

  # Draw the buffer directly onto the window, utilising the (last) shader.
  buffer1.draw 0, 0, nil, shader: shaders.last
end

#primary_bufferObject



22
# File 'lib/ashton/gosu_ext/window.rb', line 22

def primary_buffer; Window.primary_buffer end

#secondary_bufferObject



23
# File 'lib/ashton/gosu_ext/window.rb', line 23

def secondary_buffer; Window.secondary_buffer end