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.
-
.from_memory(bytes, smooth: false, repeated: false) ⇒ Object
Decode + upload a Ruby String of bytes (PNG, JPG, BMP, …) as a 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.
-
#native_handle ⇒ Object
The OpenGL texture-object name (a ‘glGenTextures` ID).
- #repeated=(value) ⇒ Object
- #repeated? ⇒ Boolean
-
#resize(width, height) ⇒ Object
Reallocate this texture’s GPU memory at a new size.
- #size ⇒ Object
- #smooth=(value) ⇒ Object
- #smooth? ⇒ Boolean
- #srgb? ⇒ Boolean
-
#swap(other) ⇒ Object
Atomically swap the GPU memory between two textures — cheaper than ‘dup` + reassign for double-buffer-style patterns (paint-buffer ⇄ visible-buffer).
-
#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.
-
#update_from_render_window(window, offset: [0, 0]) ⇒ Object
Read the back-buffer of a ‘RenderWindow` into this texture at `offset`.
-
#update_from_texture(source, offset: [0, 0]) ⇒ Object
Upload the contents of another texture into this one at ‘offset` (`[x, y]`).
-
#update_from_window(window, offset: [0, 0]) ⇒ Object
Same as ‘update_from_render_window` for the bare `Window` (when you’re managing GL yourself).
Instance Attribute Details
#handle ⇒ Object (readonly)
:nodoc:
192 193 194 |
# File 'lib/sfml/graphics/texture.rb', line 192 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.
198 199 200 201 202 |
# File 'lib/sfml/graphics/texture.rb', line 198 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.
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/sfml/graphics/texture.rb', line 54 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 |
.from_memory(bytes, smooth: false, repeated: false) ⇒ Object
Decode + upload a Ruby String of bytes (PNG, JPG, BMP, …) as a texture. Useful for embedded assets / network responses that bypass the disk.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/sfml/graphics/texture.rb', line 37 def self.from_memory(bytes, smooth: false, repeated: false) raise ArgumentError, "expected a String, got #{bytes.class}" unless bytes.is_a?(String) buf = FFI::MemoryPointer.new(:uint8, bytes.bytesize) buf.write_bytes(bytes) ptr = C::Graphics.sfTexture_createFromMemory(buf, bytes.bytesize, nil) raise Error, "sfTexture_createFromMemory returned NULL — unsupported format?" 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
118 119 120 121 |
# File 'lib/sfml/graphics/texture.rb', line 118 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.
134 135 136 137 138 139 140 141 |
# File 'lib/sfml/graphics/texture.rb', line 134 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.
109 |
# File 'lib/sfml/graphics/texture.rb', line 109 def generate_mipmap = C::Graphics.sfTexture_generateMipmap(@handle) |
#native_handle ⇒ Object
The OpenGL texture-object name (a ‘glGenTextures` ID). Useful when feeding this texture into raw GL calls.
164 |
# File 'lib/sfml/graphics/texture.rb', line 164 def native_handle = C::Graphics.sfTexture_getNativeHandle(@handle) |
#repeated=(value) ⇒ Object
100 101 102 |
# File 'lib/sfml/graphics/texture.rb', line 100 def repeated=(value) C::Graphics.sfTexture_setRepeated(@handle, !!value) end |
#repeated? ⇒ Boolean
98 |
# File 'lib/sfml/graphics/texture.rb', line 98 def repeated? = C::Graphics.sfTexture_isRepeated(@handle) |
#resize(width, height) ⇒ Object
Reallocate this texture’s GPU memory at a new size. Returns ‘false` if the GPU rejects the size (driver limit / OOM); the texture’s contents become undefined on success.
147 148 149 150 151 |
# File 'lib/sfml/graphics/texture.rb', line 147 def resize(width, height) size = C::System::Vector2u.new size[:x] = Integer(width); size[:y] = Integer(height) C::Graphics.sfTexture_resize(@handle, size) end |
#size ⇒ Object
88 89 90 |
# File 'lib/sfml/graphics/texture.rb', line 88 def size Vector2.from_native(C::Graphics.sfTexture_getSize(@handle)) end |
#smooth=(value) ⇒ Object
94 95 96 |
# File 'lib/sfml/graphics/texture.rb', line 94 def smooth=(value) C::Graphics.sfTexture_setSmooth(@handle, !!value) end |
#smooth? ⇒ Boolean
92 |
# File 'lib/sfml/graphics/texture.rb', line 92 def smooth? = C::Graphics.sfTexture_isSmooth(@handle) |
#srgb? ⇒ Boolean
104 |
# File 'lib/sfml/graphics/texture.rb', line 104 def srgb? = C::Graphics.sfTexture_isSrgb(@handle) |
#swap(other) ⇒ Object
Atomically swap the GPU memory between two textures —cheaper than ‘dup` + reassign for double-buffer-style patterns (paint-buffer ⇄ visible-buffer).
156 157 158 159 160 |
# File 'lib/sfml/graphics/texture.rb', line 156 def swap(other) raise ArgumentError, "Texture#swap needs a Texture" unless other.is_a?(Texture) C::Graphics.sfTexture_swap(@handle, other.handle) self end |
#to_image ⇒ Object
Read the texture back from the GPU into a fresh SFML::Image. Slow — useful for screenshots or post-processing inspection.
80 81 82 83 84 85 86 |
# File 'lib/sfml/graphics/texture.rb', line 80 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.
70 71 72 73 74 75 76 |
# File 'lib/sfml/graphics/texture.rb', line 70 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 |
#update_from_render_window(window, offset: [0, 0]) ⇒ Object
Read the back-buffer of a ‘RenderWindow` into this texture at `offset`. Useful for capturing the rendered scene without re-drawing into a separate `RenderTexture`.
178 179 180 181 182 |
# File 'lib/sfml/graphics/texture.rb', line 178 def update_from_render_window(window, offset: [0, 0]) raise ArgumentError, "expected a RenderWindow" unless window.is_a?(RenderWindow) C::Graphics.sfTexture_updateFromRenderWindow(@handle, window.handle, _vec2u(offset)) self end |
#update_from_texture(source, offset: [0, 0]) ⇒ Object
Upload the contents of another texture into this one at ‘offset` (`[x, y]`). Both textures must remain alive for the duration of the call.
169 170 171 172 173 |
# File 'lib/sfml/graphics/texture.rb', line 169 def update_from_texture(source, offset: [0, 0]) raise ArgumentError, "expected a Texture" unless source.is_a?(Texture) C::Graphics.sfTexture_updateFromTexture(@handle, source.handle, _vec2u(offset)) self end |
#update_from_window(window, offset: [0, 0]) ⇒ Object
Same as ‘update_from_render_window` for the bare `Window` (when you’re managing GL yourself).
186 187 188 189 190 |
# File 'lib/sfml/graphics/texture.rb', line 186 def update_from_window(window, offset: [0, 0]) raise ArgumentError, "expected a Window" unless window.is_a?(SFML::Window) C::Graphics.sfTexture_updateFromWindow(@handle, window.handle, _vec2u(offset)) self end |