chore: renamed the resources directory to res
4
.gitignore
vendored
|
@ -1,2 +1,4 @@
|
||||||
|
/Cargo.lock
|
||||||
/target
|
/target
|
||||||
/build.rs
|
/build.rs
|
||||||
|
/src/main.rs
|
||||||
|
|
64
README.md
|
@ -3,9 +3,9 @@ a free and open source games framework
|
||||||
|
|
||||||
> [!WARNING]
|
> [!WARNING]
|
||||||
> This project is in early development and is not yet ready for use.
|
> This project is in early development and is not yet ready for use.
|
||||||
>
|
>
|
||||||
> It could be potentially used to make something very basic in a very hacky way, but it is not a good experience.
|
> It could be potentially used to make something very basic in a very hacky way, but it is not a good experience.
|
||||||
>
|
>
|
||||||
> Installation is manual as of right now but if it reaches an acceptable state I will publish the crate on crates.
|
> Installation is manual as of right now but if it reaches an acceptable state I will publish the crate on crates.
|
||||||
>
|
>
|
||||||
> There is a plan for a project creation tool that will automate the project setup process.
|
> There is a plan for a project creation tool that will automate the project setup process.
|
||||||
|
@ -20,7 +20,7 @@ project
|
||||||
│ build.rs
|
│ build.rs
|
||||||
│ crates
|
│ crates
|
||||||
│ └── comet
|
│ └── comet
|
||||||
│ resources
|
│ res
|
||||||
│ └── shaders
|
│ └── shaders
|
||||||
│ └── textures
|
│ └── textures
|
||||||
│ └── sounds
|
│ └── sounds
|
||||||
|
@ -29,15 +29,15 @@ project
|
||||||
```
|
```
|
||||||
|
|
||||||
```toml
|
```toml
|
||||||
# Cargo.toml
|
# Cargo.toml
|
||||||
# ...
|
# ...
|
||||||
[dependencies]
|
[dependencies]
|
||||||
comet = { path = "path/of/the/comet/crate" }
|
comet = { path = "path/of/the/comet/crate" }
|
||||||
# ...
|
# ...
|
||||||
```
|
```
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
// main.rs example
|
// main.rs example
|
||||||
use comet::prelude::*;
|
use comet::prelude::*;
|
||||||
|
|
||||||
struct GameState {}
|
struct GameState {}
|
||||||
|
@ -50,17 +50,17 @@ impl GameState {
|
||||||
|
|
||||||
// This function will be called once before the event loop starts
|
// This function will be called once before the event loop starts
|
||||||
fn setup(app: &mut App, renderer: &mut Renderer2D) {}
|
fn setup(app: &mut App, renderer: &mut Renderer2D) {}
|
||||||
// This function will be called every tick
|
// This function will be called every tick
|
||||||
fn update(app: &mut App, renderer: &mut Renderer2D, dt: f32) {}
|
fn update(app: &mut App, renderer: &mut Renderer2D, dt: f32) {}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
App::new() // Generate a basic 2D app
|
App::new() // Generate a basic 2D app
|
||||||
.with_preset(App2D) // Pre-registers the `Transform2D` component in the scene
|
.with_preset(App2D) // Pre-registers the `Transform2D` component in the scene
|
||||||
.with_title("Comet App") // Sets the window title
|
.with_title("Comet App") // Sets the window title
|
||||||
.with_icon(r"resources/textures/comet_icon.png") // Sets the window icon
|
.with_icon(r"res/textures/comet_icon.png") // Sets the window icon
|
||||||
.with_size(1920, 1080) // Sets the window size
|
.with_size(1920, 1080) // Sets the window size
|
||||||
.with_game_state(GameState::new()) // Adds a custom game state struct
|
.with_game_state(GameState::new()) // Adds a custom game state struct
|
||||||
.run::<Renderer2D>(setup, update) // Starts app with the given
|
.run::<Renderer2D>(setup, update) // Starts app with the given
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -75,41 +75,41 @@ use std::path::PathBuf;
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
// Watch resource directories for changes
|
// Watch resource directories for changes
|
||||||
println!("cargo:rerun-if-changed=resources/materials/*");
|
println!("cargo:rerun-if-changed=res/materials/*");
|
||||||
println!("cargo:rerun-if-changed=resources/objects/*");
|
println!("cargo:rerun-if-changed=res/objects/*");
|
||||||
println!("cargo:rerun-if-changed=resources/textures/*");
|
println!("cargo:rerun-if-changed=res/textures/*");
|
||||||
println!("cargo:rerun-if-changed=resources/shaders/*");
|
println!("cargo:rerun-if-changed=res/shaders/*");
|
||||||
println!("cargo:rerun-if-changed=resources/data/*");
|
println!("cargo:rerun-if-changed=res/data/*");
|
||||||
println!("cargo:rerun-if-changed=resources/sounds/*");
|
println!("cargo:rerun-if-changed=res/sounds/*");
|
||||||
println!("cargo:rerun-if-changed=resources/fonts/*");
|
println!("cargo:rerun-if-changed=res/fonts/*");
|
||||||
|
|
||||||
let profile = env::var("PROFILE")?;
|
let profile = env::var("PROFILE")?;
|
||||||
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?);
|
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?);
|
||||||
let target_dir = manifest_dir.join("target").join(&profile);
|
let target_dir = manifest_dir.join("target").join(&profile);
|
||||||
|
|
||||||
let dest_resources_dir = target_dir.join("resources");
|
let dest_resources_dir = target_dir.join("res");
|
||||||
|
|
||||||
std::fs::create_dir_all(&dest_resources_dir)?;
|
std::fs::create_dir_all(&dest_resources_dir)?;
|
||||||
|
|
||||||
let mut copy_options = CopyOptions::new();
|
let mut copy_options = CopyOptions::new();
|
||||||
copy_options.overwrite = true;
|
copy_options.overwrite = true;
|
||||||
copy_options.copy_inside = true;
|
copy_options.copy_inside = true;
|
||||||
|
|
||||||
let resource_folders = vec![
|
let resource_folders = vec![
|
||||||
"resources/materials/",
|
"res/materials/",
|
||||||
"resources/objects/",
|
"res/objects/",
|
||||||
"resources/textures/",
|
"res/textures/",
|
||||||
"resources/shaders/",
|
"res/shaders/",
|
||||||
"resources/data/",
|
"res/data/",
|
||||||
"resources/sounds/",
|
"res/sounds/",
|
||||||
"resources/fonts/",
|
"res/fonts/",
|
||||||
];
|
];
|
||||||
|
|
||||||
let resource_paths: Vec<PathBuf> = resource_folders
|
let resource_paths: Vec<PathBuf> = resource_folders
|
||||||
.iter()
|
.iter()
|
||||||
.map(|p| manifest_dir.join(p))
|
.map(|p| manifest_dir.join(p))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
copy_items(&resource_paths, &dest_resources_dir, ©_options)?;
|
copy_items(&resource_paths, &dest_resources_dir, ©_options)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -1,313 +1,313 @@
|
||||||
|
use crate::camera::{CameraUniform, RenderCamera};
|
||||||
|
use crate::draw_info::DrawInfo;
|
||||||
|
use crate::render_pass::{RenderPassInfo, RenderPassType};
|
||||||
|
use crate::renderer::Renderer;
|
||||||
|
use comet_colors::Color;
|
||||||
|
use comet_ecs::{Camera2D, Component, Position2D, Render, Render2D, Scene, Text, Transform2D};
|
||||||
|
use comet_log::*;
|
||||||
|
use comet_math::{p2, p3, v2, v3};
|
||||||
|
use comet_resources::texture_atlas::TextureRegion;
|
||||||
|
use comet_resources::{graphic_resource_manager::GraphicResourceManager, Texture, Vertex};
|
||||||
|
use comet_structs::ComponentSet;
|
||||||
use std::iter;
|
use std::iter;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
use wgpu::BufferUsages;
|
|
||||||
use wgpu::core::command::DrawKind::Draw;
|
use wgpu::core::command::DrawKind::Draw;
|
||||||
use wgpu::naga::ShaderStage;
|
use wgpu::naga::ShaderStage;
|
||||||
use wgpu::util::DeviceExt;
|
use wgpu::util::DeviceExt;
|
||||||
|
use wgpu::BufferUsages;
|
||||||
use winit::dpi::PhysicalSize;
|
use winit::dpi::PhysicalSize;
|
||||||
use winit::window::Window;
|
use winit::window::Window;
|
||||||
use comet_colors::Color;
|
|
||||||
use comet_ecs::{Camera2D, Component, Position2D, Render, Render2D, Transform2D, Scene, Text};
|
|
||||||
use comet_log::*;
|
|
||||||
use comet_math::{p2, p3, v2, v3};
|
|
||||||
use comet_resources::{graphic_resource_manager::GraphicResourceManager, Texture, Vertex};
|
|
||||||
use comet_resources::texture_atlas::TextureRegion;
|
|
||||||
use comet_structs::ComponentSet;
|
|
||||||
use crate::camera::{RenderCamera, CameraUniform};
|
|
||||||
use crate::draw_info::DrawInfo;
|
|
||||||
use crate::render_pass::{RenderPassInfo, RenderPassType};
|
|
||||||
use crate::renderer::Renderer;
|
|
||||||
|
|
||||||
pub struct Renderer2D<'a> {
|
pub struct Renderer2D<'a> {
|
||||||
surface: wgpu::Surface<'a>,
|
surface: wgpu::Surface<'a>,
|
||||||
device: wgpu::Device,
|
device: wgpu::Device,
|
||||||
queue: wgpu::Queue,
|
queue: wgpu::Queue,
|
||||||
config: wgpu::SurfaceConfiguration,
|
config: wgpu::SurfaceConfiguration,
|
||||||
size: PhysicalSize<u32>,
|
size: PhysicalSize<u32>,
|
||||||
render_pipeline_layout: wgpu::PipelineLayout,
|
render_pipeline_layout: wgpu::PipelineLayout,
|
||||||
universal_render_pipeline: wgpu::RenderPipeline,
|
universal_render_pipeline: wgpu::RenderPipeline,
|
||||||
texture_bind_group_layout: wgpu::BindGroupLayout,
|
texture_bind_group_layout: wgpu::BindGroupLayout,
|
||||||
dummy_texture_bind_group: wgpu::BindGroup,
|
dummy_texture_bind_group: wgpu::BindGroup,
|
||||||
texture_sampler: wgpu::Sampler,
|
texture_sampler: wgpu::Sampler,
|
||||||
camera: RenderCamera,
|
camera: RenderCamera,
|
||||||
camera_uniform: CameraUniform,
|
camera_uniform: CameraUniform,
|
||||||
camera_buffer: wgpu::Buffer,
|
camera_buffer: wgpu::Buffer,
|
||||||
camera_bind_group: wgpu::BindGroup,
|
camera_bind_group: wgpu::BindGroup,
|
||||||
render_pass: Vec<RenderPassInfo>,
|
render_pass: Vec<RenderPassInfo>,
|
||||||
draw_info: Vec<DrawInfo>,
|
draw_info: Vec<DrawInfo>,
|
||||||
graphic_resource_manager: GraphicResourceManager,
|
graphic_resource_manager: GraphicResourceManager,
|
||||||
delta_time: f32,
|
delta_time: f32,
|
||||||
last_frame_time: Instant,
|
last_frame_time: Instant,
|
||||||
clear_color: wgpu::Color,
|
clear_color: wgpu::Color,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Renderer2D<'a> {
|
impl<'a> Renderer2D<'a> {
|
||||||
pub fn new(window: Arc<Window>, clear_color: Option<impl Color>) -> Renderer2D<'a> {
|
pub fn new(window: Arc<Window>, clear_color: Option<impl Color>) -> Renderer2D<'a> {
|
||||||
let size = PhysicalSize::<u32>::new(1920, 1080);
|
let size = PhysicalSize::<u32>::new(1920, 1080);
|
||||||
|
|
||||||
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
|
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
|
||||||
backends: wgpu::Backends::PRIMARY,
|
backends: wgpu::Backends::PRIMARY,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
});
|
});
|
||||||
|
|
||||||
let surface = instance.create_surface(window).unwrap();
|
let surface = instance.create_surface(window).unwrap();
|
||||||
|
|
||||||
let adapter = pollster::block_on(instance
|
let adapter = pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions {
|
||||||
.request_adapter(&wgpu::RequestAdapterOptions {
|
power_preference: wgpu::PowerPreference::default(),
|
||||||
power_preference: wgpu::PowerPreference::default(),
|
compatible_surface: Some(&surface),
|
||||||
compatible_surface: Some(&surface),
|
force_fallback_adapter: false,
|
||||||
force_fallback_adapter: false,
|
}))
|
||||||
}))
|
.unwrap();
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let (device, queue) = pollster::block_on(adapter
|
let (device, queue) = pollster::block_on(adapter.request_device(
|
||||||
.request_device(
|
&wgpu::DeviceDescriptor {
|
||||||
&wgpu::DeviceDescriptor {
|
label: None,
|
||||||
label: None,
|
required_features: wgpu::Features::empty(),
|
||||||
required_features: wgpu::Features::empty(),
|
required_limits: wgpu::Limits::default(),
|
||||||
required_limits: wgpu::Limits::default(),
|
memory_hints: Default::default(),
|
||||||
memory_hints: Default::default(),
|
},
|
||||||
},
|
None, // Trace path
|
||||||
None, // Trace path
|
))
|
||||||
))
|
.unwrap();
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let surface_caps = surface.get_capabilities(&adapter);
|
let surface_caps = surface.get_capabilities(&adapter);
|
||||||
let surface_format = surface_caps
|
let surface_format = surface_caps
|
||||||
.formats
|
.formats
|
||||||
.iter()
|
.iter()
|
||||||
.copied()
|
.copied()
|
||||||
.find(|f| f.is_srgb())
|
.find(|f| f.is_srgb())
|
||||||
.unwrap_or(surface_caps.formats[0]);
|
.unwrap_or(surface_caps.formats[0]);
|
||||||
let config = wgpu::SurfaceConfiguration {
|
let config = wgpu::SurfaceConfiguration {
|
||||||
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
|
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
|
||||||
format: surface_format,
|
format: surface_format,
|
||||||
width: size.width,
|
width: size.width,
|
||||||
height: size.height,
|
height: size.height,
|
||||||
present_mode: surface_caps.present_modes[0],
|
present_mode: surface_caps.present_modes[0],
|
||||||
alpha_mode: surface_caps.alpha_modes[0],
|
alpha_mode: surface_caps.alpha_modes[0],
|
||||||
view_formats: vec![],
|
view_formats: vec![],
|
||||||
desired_maximum_frame_latency: 2,
|
desired_maximum_frame_latency: 2,
|
||||||
};
|
};
|
||||||
|
|
||||||
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
|
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
|
||||||
label: Some("Shader"),
|
label: Some("Shader"),
|
||||||
source: wgpu::ShaderSource::Wgsl(include_str!("base2d.wgsl").into()),
|
source: wgpu::ShaderSource::Wgsl(include_str!("base2d.wgsl").into()),
|
||||||
});
|
});
|
||||||
|
|
||||||
let graphic_resource_manager = GraphicResourceManager::new();
|
let graphic_resource_manager = GraphicResourceManager::new();
|
||||||
|
|
||||||
let diffuse_bytes = include_bytes!(r"../../../resources/textures/comet_icon.png");
|
let diffuse_bytes = include_bytes!(r"../../../res/textures/comet_icon.png");
|
||||||
let diffuse_texture =
|
let diffuse_texture =
|
||||||
Texture::from_bytes(&device, &queue, diffuse_bytes, "comet_icon.png", false).unwrap();
|
Texture::from_bytes(&device, &queue, diffuse_bytes, "comet_icon.png", false).unwrap();
|
||||||
|
|
||||||
let texture_bind_group_layout =
|
let texture_bind_group_layout =
|
||||||
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||||
entries: &[
|
entries: &[
|
||||||
wgpu::BindGroupLayoutEntry {
|
wgpu::BindGroupLayoutEntry {
|
||||||
binding: 0,
|
binding: 0,
|
||||||
visibility: wgpu::ShaderStages::FRAGMENT,
|
visibility: wgpu::ShaderStages::FRAGMENT,
|
||||||
ty: wgpu::BindingType::Texture {
|
ty: wgpu::BindingType::Texture {
|
||||||
multisampled: false,
|
multisampled: false,
|
||||||
view_dimension: wgpu::TextureViewDimension::D2,
|
view_dimension: wgpu::TextureViewDimension::D2,
|
||||||
sample_type: wgpu::TextureSampleType::Float { filterable: true },
|
sample_type: wgpu::TextureSampleType::Float { filterable: true },
|
||||||
},
|
},
|
||||||
count: None,
|
count: None,
|
||||||
},
|
},
|
||||||
wgpu::BindGroupLayoutEntry {
|
wgpu::BindGroupLayoutEntry {
|
||||||
binding: 1,
|
binding: 1,
|
||||||
visibility: wgpu::ShaderStages::FRAGMENT,
|
visibility: wgpu::ShaderStages::FRAGMENT,
|
||||||
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
|
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
|
||||||
count: None,
|
count: None,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
label: Some("texture_bind_group_layout"),
|
label: Some("texture_bind_group_layout"),
|
||||||
});
|
});
|
||||||
|
|
||||||
let camera = RenderCamera::new(1.0, v2::new(2.0, 2.0), v3::new(0.0, 0.0, 0.0));
|
let camera = RenderCamera::new(1.0, v2::new(2.0, 2.0), v3::new(0.0, 0.0, 0.0));
|
||||||
|
|
||||||
let mut camera_uniform = CameraUniform::new();
|
let mut camera_uniform = CameraUniform::new();
|
||||||
camera_uniform.update_view_proj(&camera);
|
camera_uniform.update_view_proj(&camera);
|
||||||
|
|
||||||
let camera_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
let camera_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||||
label: Some("Camera Buffer"),
|
label: Some("Camera Buffer"),
|
||||||
contents: bytemuck::cast_slice(&[camera_uniform]),
|
contents: bytemuck::cast_slice(&[camera_uniform]),
|
||||||
usage: BufferUsages::UNIFORM | BufferUsages::COPY_DST,
|
usage: BufferUsages::UNIFORM | BufferUsages::COPY_DST,
|
||||||
});
|
});
|
||||||
|
|
||||||
let camera_bind_group_layout =
|
let camera_bind_group_layout =
|
||||||
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||||
entries: &[wgpu::BindGroupLayoutEntry {
|
entries: &[wgpu::BindGroupLayoutEntry {
|
||||||
binding: 0,
|
binding: 0,
|
||||||
visibility: wgpu::ShaderStages::VERTEX,
|
visibility: wgpu::ShaderStages::VERTEX,
|
||||||
ty: wgpu::BindingType::Buffer {
|
ty: wgpu::BindingType::Buffer {
|
||||||
ty: wgpu::BufferBindingType::Uniform,
|
ty: wgpu::BufferBindingType::Uniform,
|
||||||
has_dynamic_offset: false,
|
has_dynamic_offset: false,
|
||||||
min_binding_size: None,
|
min_binding_size: None,
|
||||||
},
|
},
|
||||||
count: None,
|
count: None,
|
||||||
}],
|
}],
|
||||||
label: Some("camera_bind_group_layout"),
|
label: Some("camera_bind_group_layout"),
|
||||||
});
|
});
|
||||||
|
|
||||||
let camera_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
let camera_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||||
layout: &camera_bind_group_layout,
|
layout: &camera_bind_group_layout,
|
||||||
entries: &[wgpu::BindGroupEntry {
|
entries: &[wgpu::BindGroupEntry {
|
||||||
binding: 0,
|
binding: 0,
|
||||||
resource: camera_buffer.as_entire_binding(),
|
resource: camera_buffer.as_entire_binding(),
|
||||||
}],
|
}],
|
||||||
label: Some("camera_bind_group"),
|
label: Some("camera_bind_group"),
|
||||||
});
|
});
|
||||||
|
|
||||||
let render_pipeline_layout =
|
let render_pipeline_layout =
|
||||||
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||||
label: Some("Render Pipeline Layout"),
|
label: Some("Render Pipeline Layout"),
|
||||||
bind_group_layouts: &[
|
bind_group_layouts: &[&texture_bind_group_layout, &camera_bind_group_layout],
|
||||||
&texture_bind_group_layout,
|
push_constant_ranges: &[],
|
||||||
&camera_bind_group_layout,
|
});
|
||||||
],
|
|
||||||
push_constant_ranges: &[],
|
|
||||||
});
|
|
||||||
|
|
||||||
let universal_render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
let universal_render_pipeline =
|
||||||
label: Some("Render Pipeline"),
|
device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||||
layout: Some(&render_pipeline_layout),
|
label: Some("Render Pipeline"),
|
||||||
vertex: wgpu::VertexState {
|
layout: Some(&render_pipeline_layout),
|
||||||
module: &shader,
|
vertex: wgpu::VertexState {
|
||||||
entry_point: "vs_main",
|
module: &shader,
|
||||||
buffers: &[Vertex::desc()],
|
entry_point: "vs_main",
|
||||||
compilation_options: Default::default(),
|
buffers: &[Vertex::desc()],
|
||||||
},
|
compilation_options: Default::default(),
|
||||||
fragment: Some(wgpu::FragmentState {
|
},
|
||||||
module: &shader,
|
fragment: Some(wgpu::FragmentState {
|
||||||
entry_point: "fs_main",
|
module: &shader,
|
||||||
targets: &[Some(wgpu::ColorTargetState {
|
entry_point: "fs_main",
|
||||||
format: config.format,
|
targets: &[Some(wgpu::ColorTargetState {
|
||||||
blend: Some(wgpu::BlendState {
|
format: config.format,
|
||||||
color: wgpu::BlendComponent {
|
blend: Some(wgpu::BlendState {
|
||||||
src_factor: wgpu::BlendFactor::SrcAlpha,
|
color: wgpu::BlendComponent {
|
||||||
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
|
src_factor: wgpu::BlendFactor::SrcAlpha,
|
||||||
operation: wgpu::BlendOperation::Add,
|
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
|
||||||
},
|
operation: wgpu::BlendOperation::Add,
|
||||||
alpha: wgpu::BlendComponent {
|
},
|
||||||
src_factor: wgpu::BlendFactor::One,
|
alpha: wgpu::BlendComponent {
|
||||||
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
|
src_factor: wgpu::BlendFactor::One,
|
||||||
operation: wgpu::BlendOperation::Add,
|
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
|
||||||
},
|
operation: wgpu::BlendOperation::Add,
|
||||||
}),
|
},
|
||||||
write_mask: wgpu::ColorWrites::ALL,
|
}),
|
||||||
})],
|
write_mask: wgpu::ColorWrites::ALL,
|
||||||
compilation_options: Default::default(),
|
})],
|
||||||
}),
|
compilation_options: Default::default(),
|
||||||
primitive: wgpu::PrimitiveState {
|
}),
|
||||||
topology: wgpu::PrimitiveTopology::TriangleList,
|
primitive: wgpu::PrimitiveState {
|
||||||
strip_index_format: None,
|
topology: wgpu::PrimitiveTopology::TriangleList,
|
||||||
front_face: wgpu::FrontFace::Ccw,
|
strip_index_format: None,
|
||||||
cull_mode: Some(wgpu::Face::Back),
|
front_face: wgpu::FrontFace::Ccw,
|
||||||
polygon_mode: wgpu::PolygonMode::Fill,
|
cull_mode: Some(wgpu::Face::Back),
|
||||||
unclipped_depth: false,
|
polygon_mode: wgpu::PolygonMode::Fill,
|
||||||
conservative: false,
|
unclipped_depth: false,
|
||||||
},
|
conservative: false,
|
||||||
depth_stencil: None,
|
},
|
||||||
multisample: wgpu::MultisampleState {
|
depth_stencil: None,
|
||||||
count: 1,
|
multisample: wgpu::MultisampleState {
|
||||||
mask: !0,
|
count: 1,
|
||||||
alpha_to_coverage_enabled: false,
|
mask: !0,
|
||||||
},
|
alpha_to_coverage_enabled: false,
|
||||||
multiview: None,
|
},
|
||||||
cache: None,
|
multiview: None,
|
||||||
});
|
cache: None,
|
||||||
|
});
|
||||||
|
|
||||||
let mut render_pass: Vec<RenderPassInfo> = Vec::new();
|
let mut render_pass: Vec<RenderPassInfo> = Vec::new();
|
||||||
/*render_pass.push(RenderPassInfo::new_engine_pass(
|
/*render_pass.push(RenderPassInfo::new_engine_pass(
|
||||||
&device,
|
&device,
|
||||||
"Standard Render Pass".to_string(),
|
"Standard Render Pass".to_string(),
|
||||||
&texture_bind_group_layout,
|
&texture_bind_group_layout,
|
||||||
&diffuse_texture,
|
&diffuse_texture,
|
||||||
vec![],
|
vec![],
|
||||||
vec![],
|
vec![],
|
||||||
));*/
|
));*/
|
||||||
|
|
||||||
let clear_color = match clear_color {
|
let clear_color = match clear_color {
|
||||||
Some(color) => color.to_wgpu(),
|
Some(color) => color.to_wgpu(),
|
||||||
None => wgpu::Color {
|
None => wgpu::Color {
|
||||||
r: 0.0,
|
r: 0.0,
|
||||||
g: 0.0,
|
g: 0.0,
|
||||||
b: 0.0,
|
b: 0.0,
|
||||||
a: 1.0,
|
a: 1.0,
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
let texture_sampler = device.create_sampler(&wgpu::SamplerDescriptor {
|
let texture_sampler = device.create_sampler(&wgpu::SamplerDescriptor {
|
||||||
address_mode_u: wgpu::AddressMode::ClampToEdge,
|
address_mode_u: wgpu::AddressMode::ClampToEdge,
|
||||||
address_mode_v: wgpu::AddressMode::ClampToEdge,
|
address_mode_v: wgpu::AddressMode::ClampToEdge,
|
||||||
address_mode_w: wgpu::AddressMode::ClampToEdge,
|
address_mode_w: wgpu::AddressMode::ClampToEdge,
|
||||||
mag_filter: wgpu::FilterMode::Linear,
|
mag_filter: wgpu::FilterMode::Linear,
|
||||||
min_filter: wgpu::FilterMode::Linear,
|
min_filter: wgpu::FilterMode::Linear,
|
||||||
mipmap_filter: wgpu::FilterMode::Linear,
|
mipmap_filter: wgpu::FilterMode::Linear,
|
||||||
lod_min_clamp: 0.0,
|
lod_min_clamp: 0.0,
|
||||||
lod_max_clamp: 100.0,
|
lod_max_clamp: 100.0,
|
||||||
compare: None,
|
compare: None,
|
||||||
anisotropy_clamp: 16,
|
anisotropy_clamp: 16,
|
||||||
border_color: None,
|
border_color: None,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
});
|
});
|
||||||
|
|
||||||
let empty_texture = device.create_texture(&wgpu::TextureDescriptor {
|
let empty_texture = device.create_texture(&wgpu::TextureDescriptor {
|
||||||
label: Some("Empty Texture"),
|
label: Some("Empty Texture"),
|
||||||
size: wgpu::Extent3d {
|
size: wgpu::Extent3d {
|
||||||
width: config.width,
|
width: config.width,
|
||||||
height: config.height,
|
height: config.height,
|
||||||
depth_or_array_layers: 1,
|
depth_or_array_layers: 1,
|
||||||
},
|
},
|
||||||
mip_level_count: 1,
|
mip_level_count: 1,
|
||||||
sample_count: 1,
|
sample_count: 1,
|
||||||
dimension: wgpu::TextureDimension::D2,
|
dimension: wgpu::TextureDimension::D2,
|
||||||
format: wgpu::TextureFormat::Bgra8UnormSrgb,
|
format: wgpu::TextureFormat::Bgra8UnormSrgb,
|
||||||
usage: wgpu::TextureUsages::COPY_SRC | wgpu::TextureUsages::COPY_DST | wgpu::TextureUsages::TEXTURE_BINDING,
|
usage: wgpu::TextureUsages::COPY_SRC
|
||||||
view_formats: &[wgpu::TextureFormat::Bgra8UnormSrgb],
|
| wgpu::TextureUsages::COPY_DST
|
||||||
});
|
| wgpu::TextureUsages::TEXTURE_BINDING,
|
||||||
|
view_formats: &[wgpu::TextureFormat::Bgra8UnormSrgb],
|
||||||
|
});
|
||||||
|
|
||||||
let dummy_texture_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
let dummy_texture_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||||
layout: &texture_bind_group_layout,
|
layout: &texture_bind_group_layout,
|
||||||
entries: &[
|
entries: &[
|
||||||
wgpu::BindGroupEntry {
|
wgpu::BindGroupEntry {
|
||||||
binding: 0,
|
binding: 0,
|
||||||
resource: wgpu::BindingResource::TextureView(&empty_texture.create_view(&wgpu::TextureViewDescriptor::default())),
|
resource: wgpu::BindingResource::TextureView(
|
||||||
},
|
&empty_texture.create_view(&wgpu::TextureViewDescriptor::default()),
|
||||||
wgpu::BindGroupEntry {
|
),
|
||||||
binding: 1,
|
},
|
||||||
resource: wgpu::BindingResource::Sampler(&texture_sampler),
|
wgpu::BindGroupEntry {
|
||||||
},
|
binding: 1,
|
||||||
],
|
resource: wgpu::BindingResource::Sampler(&texture_sampler),
|
||||||
label: Some("dummy_texture_bind_group"),
|
},
|
||||||
});
|
],
|
||||||
|
label: Some("dummy_texture_bind_group"),
|
||||||
let mut draw_info: Vec<DrawInfo> = Vec::new();
|
});
|
||||||
|
|
||||||
Self {
|
let mut draw_info: Vec<DrawInfo> = Vec::new();
|
||||||
surface,
|
|
||||||
device,
|
Self {
|
||||||
queue,
|
surface,
|
||||||
config,
|
device,
|
||||||
size,
|
queue,
|
||||||
render_pipeline_layout,
|
config,
|
||||||
universal_render_pipeline,
|
size,
|
||||||
texture_bind_group_layout,
|
render_pipeline_layout,
|
||||||
dummy_texture_bind_group,
|
universal_render_pipeline,
|
||||||
texture_sampler,
|
texture_bind_group_layout,
|
||||||
camera,
|
dummy_texture_bind_group,
|
||||||
camera_uniform,
|
texture_sampler,
|
||||||
camera_buffer,
|
camera,
|
||||||
camera_bind_group,
|
camera_uniform,
|
||||||
render_pass,
|
camera_buffer,
|
||||||
draw_info,
|
camera_bind_group,
|
||||||
graphic_resource_manager,
|
render_pass,
|
||||||
delta_time: 0.0,
|
draw_info,
|
||||||
last_frame_time: Instant::now(),
|
graphic_resource_manager,
|
||||||
clear_color,
|
delta_time: 0.0,
|
||||||
}
|
last_frame_time: Instant::now(),
|
||||||
}
|
clear_color,
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,73 +1,73 @@
|
||||||
use comet::prelude::*;
|
use comet::prelude::*;
|
||||||
use winit_input_helper::WinitInputHelper;
|
|
||||||
use comet_input::keyboard::Key;
|
use comet_input::keyboard::Key;
|
||||||
|
use winit_input_helper::WinitInputHelper;
|
||||||
|
|
||||||
fn setup(app: &mut App, renderer: &mut Renderer2D) {
|
fn setup(app: &mut App, renderer: &mut Renderer2D) {
|
||||||
// Takes all the textures from resources/textures and puts them into a texture atlas
|
// Takes all the textures from res/textures and puts them into a texture atlas
|
||||||
renderer.initialize_atlas();
|
renderer.initialize_atlas();
|
||||||
|
|
||||||
let camera = app.new_entity();
|
let camera = app.new_entity();
|
||||||
app.add_component(camera, Transform2D::new());
|
app.add_component(camera, Transform2D::new());
|
||||||
app.add_component(camera, Camera2D::new(v2::new(2.0, 2.0), 1.0, 1));
|
app.add_component(camera, Camera2D::new(v2::new(2.0, 2.0), 1.0, 1));
|
||||||
|
|
||||||
let e1 = app.new_entity();
|
let e1 = app.new_entity();
|
||||||
|
|
||||||
app.add_component(e1, Transform2D::new());
|
app.add_component(e1, Transform2D::new());
|
||||||
|
|
||||||
let mut renderer2d = Render2D::new();
|
let mut renderer2d = Render2D::new();
|
||||||
renderer2d.set_texture(r"resources/textures/comet_icon.png");
|
renderer2d.set_texture(r"res/textures/comet_icon.png");
|
||||||
renderer2d.set_visibility(true);
|
renderer2d.set_visibility(true);
|
||||||
|
|
||||||
app.add_component(e1, renderer2d);
|
app.add_component(e1, renderer2d);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update(app: &mut App, renderer: &mut Renderer2D, dt: f32) {
|
fn update(app: &mut App, renderer: &mut Renderer2D, dt: f32) {
|
||||||
handle_input(app, dt);
|
handle_input(app, dt);
|
||||||
|
|
||||||
renderer.render_scene_2d(app.scene());
|
renderer.render_scene_2d(app.scene());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_input(app: &mut App, dt: f32) {
|
fn handle_input(app: &mut App, dt: f32) {
|
||||||
if app.key_held(Key::KeyW)
|
if app.key_held(Key::KeyW)
|
||||||
|| app.key_held(Key::KeyA)
|
|| app.key_held(Key::KeyA)
|
||||||
|| app.key_held(Key::KeyS)
|
|| app.key_held(Key::KeyS)
|
||||||
|| app.key_held(Key::KeyD)
|
|| app.key_held(Key::KeyD)
|
||||||
{
|
{
|
||||||
update_position(
|
update_position(
|
||||||
app.input_manager().clone(),
|
app.input_manager().clone(),
|
||||||
app.get_component_mut::<Transform2D>(1).unwrap(),
|
app.get_component_mut::<Transform2D>(1).unwrap(),
|
||||||
dt
|
dt,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_position(input: WinitInputHelper, transform: &mut Transform2D, dt: f32) {
|
fn update_position(input: WinitInputHelper, transform: &mut Transform2D, dt: f32) {
|
||||||
let mut direction = v2::ZERO;
|
let mut direction = v2::ZERO;
|
||||||
|
|
||||||
if input.key_held(Key::KeyW) {
|
if input.key_held(Key::KeyW) {
|
||||||
direction += v2::Y;
|
direction += v2::Y;
|
||||||
}
|
}
|
||||||
if input.key_held(Key::KeyA) {
|
if input.key_held(Key::KeyA) {
|
||||||
direction -= v2::X;
|
direction -= v2::X;
|
||||||
}
|
}
|
||||||
if input.key_held(Key::KeyS) {
|
if input.key_held(Key::KeyS) {
|
||||||
direction -= v2::Y;
|
direction -= v2::Y;
|
||||||
}
|
}
|
||||||
if input.key_held(Key::KeyD) {
|
if input.key_held(Key::KeyD) {
|
||||||
direction += v2::X;
|
direction += v2::X;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If check to prevent division by zero and the comet to fly off into infinity...
|
// If check to prevent division by zero and the comet to fly off into infinity...
|
||||||
if direction != v2::ZERO {
|
if direction != v2::ZERO {
|
||||||
let normalized_dir = direction.normalize();
|
let normalized_dir = direction.normalize();
|
||||||
let displacement = normalized_dir * 777.7 * dt;
|
let displacement = normalized_dir * 777.7 * dt;
|
||||||
transform.translate(displacement);
|
transform.translate(displacement);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
App::new()
|
App::new()
|
||||||
.with_title("Simple Move 2D")
|
.with_title("Simple Move 2D")
|
||||||
.with_preset(App2D)
|
.with_preset(App2D)
|
||||||
.run::<Renderer2D>(setup, update);
|
.run::<Renderer2D>(setup, update);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,42 +1,45 @@
|
||||||
use comet::prelude::*;
|
use comet::prelude::*;
|
||||||
|
|
||||||
fn setup(app: &mut App, renderer: &mut Renderer2D) {
|
fn setup(app: &mut App, renderer: &mut Renderer2D) {
|
||||||
// Loading the font from the resources/fonts directory with a rendered size of 77px
|
// Loading the font from the res/fonts directory with a rendered size of 77px
|
||||||
renderer.load_font("./resources/fonts/PressStart2P-Regular.ttf", 77.0);
|
renderer.load_font("./res/fonts/PressStart2P-Regular.ttf", 77.0);
|
||||||
|
|
||||||
// Setting up camera
|
// Setting up camera
|
||||||
let camera = app.new_entity();
|
let camera = app.new_entity();
|
||||||
|
|
||||||
app.add_component(camera, Transform2D::new());
|
app.add_component(camera, Transform2D::new());
|
||||||
app.add_component(camera, Camera2D::new(v2::new(2.0, 2.0), 1.0, 1));
|
app.add_component(camera, Camera2D::new(v2::new(2.0, 2.0), 1.0, 1));
|
||||||
|
|
||||||
// Creating the text entity
|
// Creating the text entity
|
||||||
let text = app.new_entity();
|
let text = app.new_entity();
|
||||||
app.add_component(text, Transform2D::new());
|
app.add_component(text, Transform2D::new());
|
||||||
app.add_component(text, Text::new(
|
app.add_component(
|
||||||
"comet", // The content of the text
|
text,
|
||||||
"./resources/fonts/PressStart2P-Regular.ttf", // The used font (right now exact to the font path)
|
Text::new(
|
||||||
77.0, // Pixel size at which the font will be drawn
|
"comet", // The content of the text
|
||||||
true, // Should the text be visible
|
"./res/fonts/PressStart2P-Regular.ttf", // The used font (right now exact to the font path)
|
||||||
sRgba::<f32>::from_hex("#abb2bfff") // Color of the text
|
77.0, // Pixel size at which the font will be drawn
|
||||||
));
|
true, // Should the text be visible
|
||||||
|
sRgba::<f32>::from_hex("#abb2bfff"), // Color of the text
|
||||||
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update(app: &mut App, renderer: &mut Renderer2D, dt: f32) {
|
fn update(app: &mut App, renderer: &mut Renderer2D, dt: f32) {
|
||||||
// Getting the windows size
|
// Getting the windows size
|
||||||
let size = renderer.size();
|
let size = renderer.size();
|
||||||
|
|
||||||
// Recalculating the position of the text every frame to ensure the same relative position
|
// Recalculating the position of the text every frame to ensure the same relative position
|
||||||
let transform = app.get_component_mut::<Transform2D>(1).unwrap();
|
let transform = app.get_component_mut::<Transform2D>(1).unwrap();
|
||||||
transform.position_mut().set_x(-((size.width-50) as f32));
|
transform.position_mut().set_x(-((size.width - 50) as f32));
|
||||||
transform.position_mut().set_y((size.height-100) as f32);
|
transform.position_mut().set_y((size.height - 100) as f32);
|
||||||
|
|
||||||
renderer.render_scene_2d(app.scene());
|
renderer.render_scene_2d(app.scene());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
App::new()
|
App::new()
|
||||||
.with_preset(App2D)
|
.with_preset(App2D)
|
||||||
.with_title("Simple Text")
|
.with_title("Simple Text")
|
||||||
.run::<Renderer2D>(setup, update);
|
.run::<Renderer2D>(setup, update);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,32 +1,32 @@
|
||||||
use comet::prelude::*;
|
use comet::prelude::*;
|
||||||
|
|
||||||
fn setup(app: &mut App, renderer: &mut Renderer2D) {
|
fn setup(app: &mut App, renderer: &mut Renderer2D) {
|
||||||
// Creating a texture atlas from the provided textures in the vector
|
// Creating a texture atlas from the provided textures in the vector
|
||||||
renderer.set_texture_atlas_by_paths(vec!["./resources/textures/comet_icon.png".to_string()]);
|
renderer.set_texture_atlas_by_paths(vec!["./res/textures/comet_icon.png".to_string()]);
|
||||||
|
|
||||||
// Creating a camera entity
|
// Creating a camera entity
|
||||||
let cam = app.new_entity();
|
let cam = app.new_entity();
|
||||||
app.add_component(cam, Transform2D::new());
|
app.add_component(cam, Transform2D::new());
|
||||||
app.add_component(cam, Camera2D::new(v2::new(2.0, 2.0), 1.0, 1));
|
app.add_component(cam, Camera2D::new(v2::new(2.0, 2.0), 1.0, 1));
|
||||||
|
|
||||||
// Creating a textured entity
|
// Creating a textured entity
|
||||||
let e0 = app.new_entity();
|
let e0 = app.new_entity();
|
||||||
app.add_component(e0, Transform2D::new());
|
app.add_component(e0, Transform2D::new());
|
||||||
|
|
||||||
let mut render = Render2D::new();
|
let mut render = Render2D::new();
|
||||||
render.set_visibility(true);
|
render.set_visibility(true);
|
||||||
render.set_texture("./resources/textures/comet_icon.png");
|
render.set_texture("./res/textures/comet_icon.png");
|
||||||
|
|
||||||
app.add_component(e0, render);
|
app.add_component(e0, render);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update(app: &mut App, renderer: &mut Renderer2D, dt: f32) {
|
fn update(app: &mut App, renderer: &mut Renderer2D, dt: f32) {
|
||||||
renderer.render_scene_2d(app.scene())
|
renderer.render_scene_2d(app.scene())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
App::new()
|
App::new()
|
||||||
.with_title("Textured Entity")
|
.with_title("Textured Entity")
|
||||||
.with_preset(App2D)
|
.with_preset(App2D)
|
||||||
.run::<Renderer2D>(setup, update);
|
.run::<Renderer2D>(setup, update);
|
||||||
}
|
}
|
||||||
|
|
Before Width: | Height: | Size: 9.9 KiB After Width: | Height: | Size: 9.9 KiB |
Before Width: | Height: | Size: 4.6 KiB After Width: | Height: | Size: 4.6 KiB |
Before Width: | Height: | Size: 320 B After Width: | Height: | Size: 320 B |
Before Width: | Height: | Size: 5 KiB After Width: | Height: | Size: 5 KiB |
Before Width: | Height: | Size: 425 B After Width: | Height: | Size: 425 B |
Before Width: | Height: | Size: 6.1 KiB After Width: | Height: | Size: 6.1 KiB |
Before Width: | Height: | Size: 4.5 KiB After Width: | Height: | Size: 4.5 KiB |
Before Width: | Height: | Size: 736 B After Width: | Height: | Size: 736 B |
Before Width: | Height: | Size: 788 B After Width: | Height: | Size: 788 B |
Before Width: | Height: | Size: 878 B After Width: | Height: | Size: 878 B |
Before Width: | Height: | Size: 5 KiB After Width: | Height: | Size: 5 KiB |