Class: SFML::Texture

Inherits:
Object
  • Object
show all
Defined in:
lib/sfml/graphics/texture.rb

Overview

A 2D image stored on the GPU. Used by Sprite (and shapes) to draw textured geometry. Build one with .load:

tex = SFML::Texture.load("assets/hero.png")
tex = SFML::Texture.load("assets/tile.png", smooth: true, repeated: true)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#handleObject (readonly)

:nodoc:



71
72
73
# File 'lib/sfml/graphics/texture.rb', line 71

def handle
  @handle
end

Class Method Details

.from_image(image, smooth: false, repeated: false) ⇒ Object

Upload a CPU-side SFML::Image to the GPU as a new Texture. Keeps the RGBA byte order and dimensions of the source image.

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/sfml/graphics/texture.rb', line 21

def self.from_image(image, smooth: false, repeated: false)
  raise ArgumentError, "Texture.from_image needs a SFML::Image" unless image.is_a?(Image)

  ptr = C::Graphics.sfTexture_createFromImage(image.handle, nil)
  raise Error, "sfTexture_createFromImage returned NULL" if ptr.null?

  tex = allocate
  tex.send(:_take_ownership, ptr)
  tex.smooth   = smooth
  tex.repeated = repeated
  tex
end

.load(path, smooth: false, repeated: false) ⇒ Object

Raises:



8
9
10
11
12
13
14
15
16
17
# File 'lib/sfml/graphics/texture.rb', line 8

def self.load(path, smooth: false, repeated: false)
  ptr = C::Graphics.sfTexture_createFromFile(path.to_s, nil)
  raise Error, "Could not load texture from #{path.inspect}" if ptr.null?

  tex = allocate
  tex.send(:_take_ownership, ptr)
  tex.smooth   = smooth
  tex.repeated = repeated
  tex
end

Instance Method Details

#repeated=(value) ⇒ Object



67
68
69
# File 'lib/sfml/graphics/texture.rb', line 67

def repeated=(value)
  C::Graphics.sfTexture_setRepeated(@handle, !!value)
end

#repeated?Boolean

Returns:

  • (Boolean)


65
# File 'lib/sfml/graphics/texture.rb', line 65

def repeated? = C::Graphics.sfTexture_isRepeated(@handle)

#sizeObject



55
56
57
# File 'lib/sfml/graphics/texture.rb', line 55

def size
  Vector2.from_native(C::Graphics.sfTexture_getSize(@handle))
end

#smooth=(value) ⇒ Object



61
62
63
# File 'lib/sfml/graphics/texture.rb', line 61

def smooth=(value)
  C::Graphics.sfTexture_setSmooth(@handle, !!value)
end

#smooth?Boolean

Returns:

  • (Boolean)


59
# File 'lib/sfml/graphics/texture.rb', line 59

def smooth?  = C::Graphics.sfTexture_isSmooth(@handle)

#to_imageObject

Read the texture back from the GPU into a fresh SFML::Image. Slow — useful for screenshots or post-processing inspection.

Raises:



47
48
49
50
51
52
53
# File 'lib/sfml/graphics/texture.rb', line 47

def to_image
  ptr = C::Graphics.sfTexture_copyToImage(@handle)
  raise Error, "sfTexture_copyToImage returned NULL" if ptr.null?
  img = Image.allocate
  img.send(:_take_ownership, ptr)
  img
end

#update(image) ⇒ Object

Re-upload an Image’s pixels to this texture in place. The image must match the texture’s size — use this for animated procedural textures (paint-buffer style) without re-allocating GPU memory.

Raises:

  • (ArgumentError)


37
38
39
40
41
42
43
# File 'lib/sfml/graphics/texture.rb', line 37

def update(image)
  raise ArgumentError, "Texture#update needs a SFML::Image" unless image.is_a?(Image)
  offset = C::System::Vector2u.new
  offset[:x] = 0; offset[:y] = 0
  C::Graphics.sfTexture_updateFromImage(@handle, image.handle, offset)
  self
end