Class: Ashton::Texture

Inherits:
Object
  • Object
show all
Includes:
Mixins::VersionChecking
Defined in:
lib/ashton/texture.rb

Direct Known Subclasses

WindowBuffer

Constant Summary collapse

DEFAULT_DRAW_COLOR =
Gosu::Color::WHITE
VALID_DRAW_MODES =
[:alpha_blend, :add, :multiply, :replace]

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixins::VersionChecking

#check_opengl_extension, #check_opengl_version

Constructor Details

#initialize(image) ⇒ Texture #initialize(blob, width, height) ⇒ Texture #initialize(width, height) ⇒ Texture

Returns a new instance of Texture.

Overloads:

  • #initialize(image) ⇒ Texture

    Create a texture from a Gosu::Image

    Parameters:

    See Also:

    • Image#to_texture
  • #initialize(blob, width, height) ⇒ Texture

    Create a texture from a binary blob.

    Parameters:

    • blob (String)
    • width (Integer)
    • height (Integer)
  • #initialize(width, height) ⇒ Texture

    Create a blank (transparent) texture.

    Parameters:

    • width (Integer)
    • height (Integer)


37
38
39
40
41
42
43
44
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
78
79
80
81
82
83
84
85
86
87
# File 'lib/ashton/texture.rb', line 37

def initialize(*args)
  case args.size
    when 1
      # Create from Gosu::Image
      image = args[0]
      raise TypeError, "Expected Gosu::Image" unless image.is_a? Gosu::Image
      initialize_ image.width, image.height, nil

      render do
        # TODO: Ideally we'd draw the image in replacement mode, but Gosu doesn't support that.
        $window.gl do
          info = image.gl_tex_info
          glEnable GL_TEXTURE_2D
          glBindTexture GL_TEXTURE_2D, info.tex_name
          glEnable GL_BLEND
          glBlendFunc GL_ONE, GL_ZERO

          glBegin GL_QUADS do
            glTexCoord2d info.left, info.top
            glVertex2d 0, height # BL

            glTexCoord2d info.left, info.bottom
            glVertex2d 0, 0 # TL

            glTexCoord2d info.right, info.bottom
            glVertex2d width, 0 # TR

            glTexCoord2d info.right, info.top
            glVertex2d width, height # BR
          end
        end
      end

    when 2
      # Create blank image.
      width, height = *args
      initialize_ width, height, nil
      clear

    when 3
      # Create from blob - create a Gosu image first.
      blob, width, height = *args
      raise ArgumentError, "Blob data is not of expected size" if blob.length != width * height * 4
      initialize_ width, height, blob

    else
      raise ArgumentError, "Expected 1, 2 or 3 parameters."
  end

  @rendering = false
end

Class Attribute Details

.pixelated=(value) ⇒ Object (writeonly)

Boolean

Whether or not to pixelate (rather than smooth) on #draw



10
11
12
# File 'lib/ashton/texture.rb', line 10

def pixelated=(value)
  @pixelated = value
end

Class Method Details

.pixelated?Boolean

Boolean

Whether or not to pixelate (rather than smooth) on #draw. Set true when Gosu::enable_undocumented_retrofication called.

Returns:

  • (Boolean)


12
# File 'lib/ashton/texture.rb', line 12

def pixelated?; @pixelated end

Instance Method Details

#clear(options = {}) ⇒ Object

Clears the buffer, optionally to a specific color.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :color (Gosu::Color, Array<Float>) — default: transparent


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/ashton/texture.rb', line 93

def clear(options = {})
  options = {
      color: [0.0, 0.0, 0.0, 0.0],
  }.merge! options

  color = options[:color]
  color = color.to_opengl if color.is_a? Gosu::Color

  glBindFramebufferEXT GL_FRAMEBUFFER_EXT, fbo_id unless rendering?

  glDisable GL_BLEND # Need to replace the alpha too.
  glClearColor(*color)
  glClear GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
  glEnable GL_BLEND

  glBindFramebufferEXT GL_FRAMEBUFFER_EXT, 0 unless rendering?

  nil
end

#dupObject



177
178
179
180
181
182
183
184
# File 'lib/ashton/texture.rb', line 177

def dup
  # Create a new texture and draw self into it.
  new_texture = Texture.new width, height
  new_texture.render do
    draw 0, 0, 0, mode: :replace
  end
  new_texture
end

#renderObject

Enable the texture to use (e.g. to draw or convert it).

Raises:

  • (ArgumentError)


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
141
142
143
144
145
146
# File 'lib/ashton/texture.rb', line 115

def render
  raise ArgumentError, "Block required" unless block_given?
  raise Error, "Can't nest rendering" if rendering?

  $window.flush # Ensure that any drawing _before_ the render block is drawn to screen, rather than into the buffer.

  render_

  @rendering = true

  # Project onto the texture itself, using Gosu (inverted) coordinates.
  glPushMatrix
  glMatrixMode GL_PROJECTION
  glLoadIdentity
  glViewport 0, 0, width, height
  glOrtho 0, width, height, 0, -1, 1

  begin
    yield self
  ensure
    $window.flush # Force all the drawing to draw now!
    glBindFramebufferEXT GL_FRAMEBUFFER_EXT, 0

    @rendering = false

    glPopMatrix

    cache.refresh # Force lazy reloading of the cache.
  end

  self
end

#rendering?Boolean

Boolean

Is this texture being rendered to currently?

Returns:

  • (Boolean)


17
# File 'lib/ashton/texture.rb', line 17

def rendering?; @rendering end

#to_image(*args) ⇒ Object

Convert the current contents of the buffer into a Gosu::Image

Parameters:

  • options (Hash)

    a customizable set of options



173
174
175
# File 'lib/ashton/texture.rb', line 173

def to_image(*args)
  cache.to_image(*args)
end