Class: SFML::Texture
- Inherits:
-
Object
- Object
- SFML::Texture
- Defined in:
- lib/sfml/graphics/texture.rb
Overview
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
-
#handle ⇒ Object
readonly
:nodoc:.
Class Method Summary collapse
-
._borrow(ptr) ⇒ Object
Internal — borrow a CSFML-owned ‘sfTexture*` (e.g. one returned by `sfFont_getTexture`) without registering an auto-destroy hook.
-
.create(width, height) ⇒ Object
Allocate a blank texture on the GPU at the given size — use ‘update(image)` afterwards to upload pixels.
-
.from_image(image, smooth: false, repeated: false) ⇒ Object
Upload a CPU-side SFML::Image to the GPU as a new Texture.
- .load(path, smooth: false, repeated: false) ⇒ Object
-
.maximum_size ⇒ Object
Maximum texture dimension the driver will allocate.
- .unbind ⇒ Object
Instance Method Summary collapse
- #bind(coord: :normalized) ⇒ Object
-
#dup ⇒ Object
(also: #clone)
Deep copy.
-
#generate_mipmap ⇒ Object
Generate mipmaps for this texture.
- #repeated=(value) ⇒ Object
- #repeated? ⇒ Boolean
- #size ⇒ Object
- #smooth=(value) ⇒ Object
- #smooth? ⇒ Boolean
- #srgb? ⇒ Boolean
-
#to_image ⇒ Object
Read the texture back from the GPU into a fresh SFML::Image.
-
#update(image) ⇒ Object
Re-upload an Image’s pixels to this texture in place.
Instance Attribute Details
#handle ⇒ Object (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.
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.
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
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
#bind(coord: :normalized) ⇒ Object
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 |
#dup ⇒ Object Also known as: clone
Deep copy. The returned texture has its own GPU memory.
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_mipmap ⇒ Object
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
80 |
# File 'lib/sfml/graphics/texture.rb', line 80 def repeated? = C::Graphics.sfTexture_isRepeated(@handle) |
#size ⇒ Object
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
74 |
# File 'lib/sfml/graphics/texture.rb', line 74 def smooth? = C::Graphics.sfTexture_isSmooth(@handle) |
#srgb? ⇒ Boolean
86 |
# File 'lib/sfml/graphics/texture.rb', line 86 def srgb? = C::Graphics.sfTexture_isSrgb(@handle) |
#to_image ⇒ Object
Read the texture back from the GPU into a fresh SFML::Image. Slow — useful for screenshots or post-processing inspection.
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.
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 |