Files
Picross/Sources/SDL/Texture.swift
T
2024-10-24 18:29:07 -04:00

88 lines
3.4 KiB
Swift

import CSDL3
import CSDL3_Image
/// An efficent, driver-specific representation of pixel data.
public class Texture {
// MARK - Public properties
/// An additional color value multiplied into render copy operations.
public var colorMod: Color {
get {
var color = Color(r: 0, g: 0, b: 0)
SDL_GetTextureColorMod(handle, &color.r, &color.g, &color.b)
return color
}
}
/// Sets an additional color value multiplied into render copy operations.
///
/// - Parameters:
/// - color: The color value to be multiplied into copy operations.
/// - Throws: ``SDLError`` on failure.
public func setColorMod(color: Color) throws(SDLError) {
try SDL_SetTextureColorMod(handle, color.r, color.g, color.b).sdlThrow(type: type(of: self))
}
// MARK - Private properties
/// The pointer refrence to the underlying `SDL_Texture`
var handle: UnsafeMutablePointer<SDL_Texture>
// MARK - Initalizers
/// Create an empty texture for a rendering context.
///
/// Chances are you won't use this directly.
///
/// - Parameters:
/// - renderer: The rendering context.
/// - width: The width of the texture in pixels.
/// - height: The height of the texture in pixels.
/// - Throws: ``SDLError`` if no rendering context was active, the format was unsupported, or the width or height were out of range.
public init(_ renderer: Renderer, width: Int, height: Int) throws(SDLError) {
handle = try SDL_CreateTexture(
renderer.handle,
SDL_PIXELFORMAT_RGB24,
SDL_TEXTUREACCESS_TARGET,
Int32(width), Int32(height)
).sdlThrow(type: type(of: self))
}
/// Creates a ``Texture`` from an image file.
///
/// This can be signfintally more efficient than using a CPU-bound ``Surface``,
/// if you dont need to manipulate the image directly after loading it.
///
/// If the loaded image has transparency or a colorkey, a texture with an alpha channel will be created.
/// Otherwise, a ``Texture`` will be created in the most reasonable format that represents the image data.
/// In most cases, this will end up being 32-bit RGB or 32-bit RGBA.
///
/// - Parameters:
/// - imagePath: The filesystem path to the texture file.
/// - renderer: The `Renderer` to attach the created `Texture` to.
/// - Throws: ``SDLError`` on failure.
public init(from imagePath: String, renderer: Renderer) throws(SDLError) {
handle = try IMG_LoadTexture(renderer.handle, imagePath).sdlThrow(type: type(of: self))
}
/// Creates a ``Texture`` from an existing surface.
///
/// The surface is not modified or freed by this function. The pixel format of the created ``Texture`` may be
/// different from the pixel format of the surface.
/// - Parameters:
/// - surface: The ``Surface`` structure containing pixel data used to fill the ``Texture``.
/// - renderer: The ``Renderer`` to attach the newly created ``Texture`` to.
/// - Throws: ``SDLError`` on failure.
public init(from surface: Surface, renderer: Renderer) throws(SDLError) {
handle = try SDL_CreateTextureFromSurface(renderer.handle, surface.handle).sdlThrow(type: type(of: self))
}
/// Calls SDL_DestroyTexture on the texture. Should be safe.
deinit {
SDL_DestroyTexture(handle)
}
// MARK - Methods
}