Skip to content

add a image-crate feature to allow using much less dependencies #819

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.8", features = ["log-impl"] }
quad-rand = "0.2.3"
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 }
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, DroppedFile};

#[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 @@ -303,6 +304,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 @@ -327,13 +329,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 @@ -671,6 +675,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
Loading