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)

Constant Summary collapse

COORDINATE_TYPES =

Bind this texture to the active OpenGL texture unit. ‘coord` is one of `:normalized` (default — UVs in [0..1]) or `:pixels` (UVs in [0..size]). Useful when mixing raw OpenGL with SFML rendering. Pass `nil` to unbind:

`SFML::Texture.unbind`.
{normalized: 0, pixels: 1}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#handleObject (readonly)

:nodoc:



126
127
128
# File 'lib/sfml/graphics/texture.rb', line 126

def handle
  @handle
end

Class Method Details

._borrow(ptr) ⇒ Object

Internal — borrow a CSFML-owned ‘sfTexture*` (e.g. one returned by `sfFont_getTexture`) without registering an auto-destroy hook. The owning object is responsible for outliving any draw call that uses this borrowed handle.



132
133
134
135
136
# File 'lib/sfml/graphics/texture.rb', line 132

def self._borrow(ptr)
  tex = allocate
  tex.instance_variable_set(:@handle, ptr)
  tex
end

.create(width, height) ⇒ Object

Allocate a blank texture on the GPU at the given size — use ‘update(image)` afterwards to upload pixels. Useful when you’ll be filling the texture from a procedurally-generated Image or repeatedly streaming pixel data into it.

Raises:



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

def self.create(width, height)
  size = C::System::Vector2u.new
  size[:x] = Integer(width); size[:y] = Integer(height)
  ptr = C::Graphics.sfTexture_create(size)
  raise Error, "sfTexture_create returned NULL — out of GPU memory?" if ptr.null?

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

.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)


36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sfml/graphics/texture.rb', line 36

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

.maximum_sizeObject

Maximum texture dimension the driver will allocate. Tied to the GL state, so it’s a class-level call (no instance).



111
112
113
# File 'lib/sfml/graphics/texture.rb', line 111

def self.maximum_size
  C::Graphics.sfTexture_getMaximumSize
end

.unbindObject



105
106
107
# File 'lib/sfml/graphics/texture.rb', line 105

def self.unbind
  C::Graphics.sfTexture_bind(nil, 0)
end

Instance Method Details

#bind(coord: :normalized) ⇒ Object

Raises:

  • (ArgumentError)


100
101
102
103
# File 'lib/sfml/graphics/texture.rb', line 100

def bind(coord: :normalized)
  raise ArgumentError, "coord must be :normalized or :pixels" unless COORDINATE_TYPES.key?(coord)
  C::Graphics.sfTexture_bind(@handle, COORDINATE_TYPES[coord])
end

#dupObject Also known as: clone

Deep copy. The returned texture has its own GPU memory.

Raises:



116
117
118
119
120
121
122
123
# File 'lib/sfml/graphics/texture.rb', line 116

def dup
  ptr = C::Graphics.sfTexture_copy(@handle)
  raise Error, "sfTexture_copy returned NULL" if ptr.null?

  tex = self.class.allocate
  tex.send(:_take_ownership, ptr)
  tex
end

#generate_mipmapObject

Generate mipmaps for this texture. Returns ‘true` if the GPU honoured it. Required for the `MIPMAP*` minification filters; otherwise downscaled samples alias.



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

def generate_mipmap = C::Graphics.sfTexture_generateMipmap(@handle)

#repeated=(value) ⇒ Object



82
83
84
# File 'lib/sfml/graphics/texture.rb', line 82

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

#repeated?Boolean

Returns:

  • (Boolean)


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

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

#sizeObject



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

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

#smooth=(value) ⇒ Object



76
77
78
# File 'lib/sfml/graphics/texture.rb', line 76

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

#smooth?Boolean

Returns:

  • (Boolean)


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

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

#srgb?Boolean

Returns:

  • (Boolean)


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

def srgb? = C::Graphics.sfTexture_isSrgb(@handle)

#to_imageObject

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

Raises:



62
63
64
65
66
67
68
# File 'lib/sfml/graphics/texture.rb', line 62

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)


52
53
54
55
56
57
58
# File 'lib/sfml/graphics/texture.rb', line 52

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