Skip to content

Commit

Permalink
More bits and pieces
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkleiny committed Apr 8, 2024
1 parent 6bc5ec0 commit 339cfe8
Show file tree
Hide file tree
Showing 19 changed files with 140 additions and 155 deletions.
8 changes: 0 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions common/src/abstractions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
pub use assets::*;
pub use clipboard::*;
pub use scenes::*;
pub use services::*;
pub use variant::*;

mod assets;
mod clipboard;
mod scenes;
mod services;
mod variant;
64 changes: 0 additions & 64 deletions common/src/abstractions/services.rs

This file was deleted.

5 changes: 5 additions & 0 deletions common/src/io/virtualfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ pub struct VirtualPath {
}

impl VirtualPath {
/// Creates a new [`VirtualPath`] from a raw string.
pub fn new(raw: impl AsRef<str>) -> Self {
raw.as_ref().to_virtual_path()
}

/// The scheme of the path.
pub fn scheme(&self) -> &StringName {
&self.scheme
Expand Down
4 changes: 4 additions & 0 deletions modules/graphics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ serde = { workspace = true, optional = true }
bitflags = { workspace = true }
image = { version = "0.24.8", default-features = false, features = ["png"] }
gl = "0.14.0"

[features]
default = ["shaderlib"]
shaderlib = []
7 changes: 4 additions & 3 deletions modules/graphics/src/images.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Image loading and manipulation.
use common::FileSystemError;

use super::*;

// common image types
Expand All @@ -9,7 +11,7 @@ pub type Color32Image = image::ImageBuffer<Color32, Vec<u8>>;
/// An error that occurred while loading an image.
#[derive(Debug)]
pub enum ImageError {
IoError,
IoError(FileSystemError),
ImageError(image::ImageError),
}

Expand All @@ -24,7 +26,7 @@ pub trait Image {
Self: Sized + FromDynamicImage,
{
let path = path.to_virtual_path();
let stream = path.open_input_stream().map_err(|_| ImageError::IoError)?;
let stream = path.open_input_stream().map_err(|error| ImageError::IoError(error))?;

Self::from_stream(stream)
}
Expand Down Expand Up @@ -220,7 +222,6 @@ impl_pixel!(Color32, "RGBA");

/// Converts the given [`image::DynamicImage`] into the given image type.
pub trait FromDynamicImage {
/// The image type to convert to.
fn from_dynamic_image(image: image::DynamicImage) -> Self;
}

Expand Down
1 change: 1 addition & 0 deletions modules/graphics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pub enum BufferError {
#[derive(Debug)]
pub enum TextureError {
InvalidId(TextureId),
InvalidImage(ImageError),
}

/// A possible error when interacting with shaders.
Expand Down
4 changes: 2 additions & 2 deletions modules/graphics/src/rendering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
//! complex render pipelines than using the 'material', 'mesh', 'render targets'
//! etc. do alone.
pub use commands::*;
pub use contexts::*;
pub use culling::*;
pub use graphs::*;
pub use pipelines::*;
pub use queue::*;

mod commands;
mod contexts;
mod culling;
mod graphs;
mod pipelines;
mod queue;

use super::*;
2 changes: 1 addition & 1 deletion modules/graphics/src/rendering/contexts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub trait RenderContext: Any + Send + Sync {

/// A manager for [`RenderContext`]s.
///
/// A [`RenderContext`] encodes all of the required details and lifecycle for
/// A [`RenderContext`] encodes all the required details and lifecycle for
/// textures, materials, render targets, shaders, necessary in a single
/// invocation of some rendering state.
pub struct Renderer {
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion modules/graphics/src/shaders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ impl_uniform!(Color as Color);
impl_uniform!(Color32 as Color32);

/// Allow for the conversion of a slice of values into a shader uniform array,
/// provided all of the values can be individually converted into a uniform.
/// provided all the values can be individually converted into a uniform.
impl<U> From<&[U]> for ShaderUniform
where
for<'a> &'a U: Into<ShaderUniform>,
Expand Down
2 changes: 1 addition & 1 deletion modules/graphics/src/shaders/lang/glsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl ShaderLanguage for GLSL {
let version = match environment.version {
OpenGLVersion::Glsl300 => "300 core",
OpenGLVersion::Glsl330 => "330 core",
OpenGLVersion::Glsl400 => "400 core2",
OpenGLVersion::Glsl400 => "400 core",
OpenGLVersion::Glsl410 => "410 core",
OpenGLVersion::Glsl420 => "420 core",
OpenGLVersion::Glsl430 => "430 core",
Expand Down
6 changes: 5 additions & 1 deletion modules/graphics/src/shaders/templates.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[cfg(feature = "shaderlib")]
pub use embedded::*;

use super::*;

/// A templated shader program that can be used to generate new
Expand Down Expand Up @@ -36,7 +39,8 @@ macro_rules! include_shader {
// Embedded shader code.
#[rustfmt::skip]
#[allow(dead_code)]
pub(crate) mod embedded {
#[cfg(feature = "shaderlib")]
mod embedded {
use super::*;

pub const SHADER_CANVAS_STANDARD: ShaderTemplate<GLSL> = include_shader!("./embedded/canvas-standard.glsl");
Expand Down
29 changes: 28 additions & 1 deletion modules/graphics/src/textures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::{cell::RefCell, rc::Rc};

use common::{uvec2, Rectangle, UVec2};
use common::{uvec2, Rectangle, ToVirtualPath, UVec2};

use super::*;

Expand Down Expand Up @@ -85,6 +85,13 @@ impl Texture {
Self::with_options(graphics, &TextureOptions::default())
}

/// Loads a texture from the given path.
pub fn from_path(graphics: &GraphicsEngine, path: impl ToVirtualPath) -> Result<Self, TextureError> {
let image = ColorImage::from_path(path).map_err(|error| TextureError::InvalidImage(error))?;

Self::from_image(graphics, &image)
}

/// Loads a texture from the given image.
pub fn from_image<T: Texel>(graphics: &GraphicsEngine, image: &impl Image<Pixel = T>) -> Result<Self, TextureError> {
let texture = Self::new(graphics)?;
Expand Down Expand Up @@ -279,6 +286,26 @@ pub struct TextureRegion {
}

impl TextureRegion {
pub fn new(texture: &Texture) -> Self {
Self {
texture: texture.clone(),
offset: uvec2(0, 0),
size: uvec2(texture.width(), texture.height()),
}
}

/// Sets the offset of the texture region.
pub fn with_offset(mut self, offset: UVec2) -> Self {
self.offset = offset;
self
}

/// Sets the size of the texture region.
pub fn with_size(mut self, size: UVec2) -> Self {
self.size = size;
self
}

/// Calculates the UV rectangle for the given texture region.
pub fn calculate_uv(&self) -> Rectangle {
let left = self.offset.x as f32 / self.texture.width() as f32;
Expand Down
9 changes: 0 additions & 9 deletions samples/asteroids/Cargo.toml

This file was deleted.

25 changes: 0 additions & 25 deletions samples/asteroids/src/main.rs

This file was deleted.

2 changes: 1 addition & 1 deletion samples/flappy-bird/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ edition.workspace = true
version.workspace = true

[dependencies]
surreal = { path = "../../", features = ["graphics", "sdl"] }
surreal = { path = "../../", features = ["graphics", "input", "sdl"] }
hecs = "0.10.4"
Binary file added samples/flappy-bird/assets/sprites/player.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 339cfe8

Please sign in to comment.