Skip to content

Commit

Permalink
add a image-crate feature
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrgani committed Sep 23, 2024
1 parent 2e48cf9 commit 25ebf84
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ license = "MIT OR Apache-2.0"
audio = ["quad-snd"]
log-rs = ["log"]
glam-serde = ["glam/serde"]
default = []
default = ["image-crate"]
image-crate = ["image"]

[package.metadata.android]
assets = "examples/"
Expand All @@ -30,7 +31,7 @@ all-features = true
miniquad = { version = "=0.4.6", features = ["log-impl"] }
quad-rand = "0.2.2"
glam = { version = "0.27", features = ["scalar-math"] }
image = { version = "0.24", default-features = false, features = ["png", "tga"] }
image = { version = "0.24", default-features = false, features = ["png", "tga"], optional = true }
macroquad_macro = { version = "0.1.8", path = "macroquad_macro" }
fontdue = "0.9"
backtrace = { version = "0.3.60", optional = true, default-features = false, features = [ "std", "libbacktrace" ] }
Expand Down
3 changes: 3 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ pub enum Error {
path: String,
},
ShaderError(miniquad::ShaderError),

#[cfg(feature = "image-crate")]
ImageError(image::ImageError),
UnknownError(&'static str),
}
Expand All @@ -22,6 +24,7 @@ impl From<miniquad::ShaderError> for Error {
}
}

#[cfg(feature = "image-crate")]
impl From<image::ImageError> for Error {
fn from(s: image::ImageError) -> Self {
Error::ImageError(s)
Expand Down
1 change: 1 addition & 0 deletions src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ pub use crate::logging::*;

pub use crate::color_u8;

#[cfg(feature = "image-crate")]
pub use image::ImageFormat;
13 changes: 9 additions & 4 deletions src/texture.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! Loading and rendering textures. Also render textures, per-pixel image manipulations.
use crate::{
color::Color, file::load_file, get_context, get_quad_context, math::Rect,
text::atlas::SpriteKey, Error,
};
use crate::{color::Color, get_context, get_quad_context, math::Rect, text::atlas::SpriteKey};

#[cfg(feature = "image-crate")]
use crate::{file::load_file, Error};

pub use crate::quad_gl::FilterMode;
use crate::quad_gl::{DrawMode, Vertex};
Expand Down Expand Up @@ -94,6 +94,7 @@ impl Image {
}
}

#[cfg(feature = "image-crate")]
/// Creates an Image from a slice of bytes that contains an encoded image.
///
/// If `format` is None, it will make an educated guess on the
Expand Down Expand Up @@ -301,6 +302,7 @@ impl Image {
}
}

#[cfg(feature = "image-crate")]
/// Saves this image as a PNG file.
/// This method is not supported on web and will panic.
pub fn export_png(&self, path: &str) {
Expand All @@ -325,13 +327,15 @@ impl Image {
}
}

#[cfg(feature = "image-crate")]
/// Loads an [Image] from a file into CPU memory.
pub async fn load_image(path: &str) -> Result<Image, Error> {
let bytes = load_file(path).await?;

Image::from_file_with_format(&bytes, None)
}

#[cfg(feature = "image-crate")]
/// Loads a [Texture2D] from a file into GPU memory.
pub async fn load_texture(path: &str) -> Result<Texture2D, Error> {
let bytes = load_file(path).await?;
Expand Down Expand Up @@ -632,6 +636,7 @@ impl Texture2D {
Texture2D::unmanaged(ctx.gl.white_texture)
}

#[cfg(feature = "image-crate")]
/// Creates a Texture2D from a slice of bytes that contains an encoded image.
///
/// If `format` is None, it will make an educated guess on the
Expand Down

0 comments on commit 25ebf84

Please sign in to comment.