Skip to content

Commit

Permalink
add examples for rect and text, refactor sugarloaf and remove custom …
Browse files Browse the repository at this point in the history
…renderer
  • Loading branch information
raphamorim committed May 12, 2023
1 parent 245852b commit dd6dd28
Show file tree
Hide file tree
Showing 13 changed files with 231 additions and 189 deletions.
7 changes: 0 additions & 7 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion rio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ tokio = { version = "1.26.0", features = ["full"] }
winit = { workspace = true }
libc = "0.2.141"
image = { version = "0.24.5", default-features = false, features = ["ico"] }
glam = "0.21.3"
bytemuck = { workspace = true }
mio = "0.6.20"
base64 = "0.21.0"
Expand Down
2 changes: 1 addition & 1 deletion rio/src/layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod mouse;
use crate::crosswords::grid::Dimensions;
use crate::crosswords::{MIN_COLUMNS, MIN_VISIBLE_ROWS};
use mouse::{AccumulatedScroll, Mouse};
use sugarloaf::SugarloafStyle;
use sugarloaf::core::SugarloafStyle;

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Delta<T: Default> {
Expand Down
2 changes: 1 addition & 1 deletion rio/src/screen/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl State {
rows: Vec<Row<Square>>,
cursor: (pos::Column, pos::Line),
sugarloaf: &mut Sugarloaf,
style: sugarloaf::SugarloafStyle,
style: sugarloaf::core::SugarloafStyle,
) {
self.cursor.position = cursor;
for (i, row) in rows.iter().enumerate() {
Expand Down
51 changes: 39 additions & 12 deletions sugarloaf/examples/row.rs → sugarloaf/examples/rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,35 @@ use winit::{
window::WindowBuilder,
};

use sugarloaf::components::row::Row;
use sugarloaf::{CustomRenderer, Renderable, RendererTarget};
use sugarloaf::components::rect::Rect;
use sugarloaf::{RendererTarget, Sugarloaf};

#[tokio::main]
async fn main() {
let mut event_loop = EventLoop::new();

let window = WindowBuilder::new()
.with_title("Row example")
.with_title("Rect example")
.with_inner_size(LogicalSize::new(1200.0, 800.0))
.with_resizable(true)
.build(&event_loop)
.unwrap();

let mut renderer = CustomRenderer::new(
let mut sugarloaf = Sugarloaf::new(
RendererTarget::Desktop,
&window,
wgpu::PowerPreference::HighPerformance,
"Firamono".to_string(),
)
.await;
let mut row = Row::init(renderer.get_context());
renderer.add_component(&mut row);
.await
.expect("Sugarloaf instance should be created");

event_loop.run_return(move |event, _, control_flow| {
control_flow.set_wait();

match event {
Event::Resumed => {
// renderer.add_component(&mut rect);
// window.request_redraw();
window.request_redraw();
}
Event::WindowEvent { event, .. } => match event {
WindowEvent::CloseRequested => control_flow.set_exit(),
Expand All @@ -57,15 +56,43 @@ async fn main() {
scale_factor,
..
} => {
renderer
sugarloaf
.rescale(scale_factor as f32)
.resize(new_inner_size.width, new_inner_size.height)
.render();
.render(wgpu::Color::BLUE);
}
_ => (),
},
Event::RedrawRequested { .. } => {
renderer.render();
sugarloaf
.pile_rect(vec![
Rect {
position: [10.0, 10.0],
color: [1.0, 1.0, 1.0, 1.0],
size: [1.0, 1.0],
},
Rect {
position: [15.0, 10.0],
color: [1.0, 1.0, 1.0, 1.0],
size: [10.0, 10.0],
},
Rect {
position: [30.0, 20.0],
color: [1.0, 1.0, 0.0, 1.0],
size: [50.0, 50.0],
},
Rect {
position: [200., 200.0],
color: [0.0, 1.0, 0.0, 1.0],
size: [100.0, 100.0],
},
Rect {
position: [500.0, 200.0],
color: [1.0, 1.0, 0.0, 1.0],
size: [200.0, 200.0],
},
])
.render(wgpu::Color::BLUE);
}
_ => {
*control_flow = winit::event_loop::ControlFlow::Wait;
Expand Down
141 changes: 141 additions & 0 deletions sugarloaf/examples/text.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
extern crate tokio;

use winit::platform::run_return::EventLoopExtRunReturn;
use winit::{
dpi::LogicalSize,
event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent},
event_loop::EventLoop,
window::WindowBuilder,
};

use sugarloaf::core::{Sugar, SugarloafStyle};
use sugarloaf::{RendererTarget, Sugarloaf};

#[tokio::main]
async fn main() {
let mut event_loop = EventLoop::new();
let width = 1200.0;
let height = 800.0;

let window = WindowBuilder::new()
.with_title("Text example")
.with_inner_size(LogicalSize::new(1200.0, 800.0))
.with_resizable(true)
.build(&event_loop)
.unwrap();

let mut sugarloaf = Sugarloaf::new(
RendererTarget::Desktop,
&window,
wgpu::PowerPreference::HighPerformance,
"Firamono".to_string(),
)
.await
.expect("Sugarloaf instance should be created");

event_loop.run_return(move |event, _, control_flow| {
control_flow.set_wait();

match event {
Event::Resumed => {
window.request_redraw();
}
Event::WindowEvent { event, .. } => match event {
WindowEvent::CloseRequested => control_flow.set_exit(),
WindowEvent::KeyboardInput {
input:
KeyboardInput {
virtual_keycode: Some(VirtualKeyCode::Space),
state: ElementState::Released,
..
},
..
} => {
//
}
WindowEvent::ScaleFactorChanged {
new_inner_size,
scale_factor,
..
} => {
sugarloaf
.rescale(scale_factor as f32)
.resize(new_inner_size.width, new_inner_size.height)
.render(wgpu::Color::BLACK);
}
_ => (),
},
Event::RedrawRequested { .. } => {
let sugar = vec![
Sugar {
content: 'S',
foreground_color: [1.0, 1.0, 1.0, 1.0],
background_color: [0.0, 0.0, 0.0, 1.0],
},
Sugar {
content: 'u',
foreground_color: [0.0, 0.0, 0.0, 1.0],
background_color: [0.0, 0.0, 0.0, 1.0],
},
Sugar {
content: 'g',
foreground_color: [1.0, 1.0, 1.0, 1.0],
background_color: [0.0, 0.0, 0.0, 1.0],
},
Sugar {
content: 'a',
foreground_color: [0.0, 0.0, 0.0, 1.0],
background_color: [0.0, 0.0, 0.0, 1.0],
},
Sugar {
content: 'r',
foreground_color: [1.0, 1.0, 1.0, 1.0],
background_color: [0.0, 0.0, 0.0, 1.0],
},
];

let loaf = vec![
Sugar {
content: 'l',
foreground_color: [1.0, 1.0, 1.0, 1.0],
background_color: [0.0, 0.0, 0.0, 1.0],
},
Sugar {
content: 'o',
foreground_color: [0.0, 0.0, 0.0, 1.0],
background_color: [0.0, 0.0, 0.0, 1.0],
},
Sugar {
content: 'a',
foreground_color: [1.0, 1.0, 1.0, 1.0],
background_color: [0.0, 0.0, 0.0, 1.0],
},
Sugar {
content: 'f',
foreground_color: [0.0, 0.0, 0.0, 1.0],
background_color: [0.0, 0.0, 0.0, 1.0],
},
];

let scale_factor = 2.;
let font_size = 150.;

let style = SugarloafStyle {
screen_position: (
(20. + 10.) * scale_factor,
(20. + font_size) * scale_factor,
),
text_scale: font_size * scale_factor,
bounds: (width * scale_factor, height * scale_factor),
};

sugarloaf.stack(sugar, style);
sugarloaf.stack(loaf, style);
sugarloaf.render(wgpu::Color::RED);
}
_ => {
*control_flow = winit::event_loop::ControlFlow::Wait;
}
}
});
}
2 changes: 1 addition & 1 deletion sugarloaf/src/components/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod row;
pub mod rect;
pub mod text;
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub const BLEND: Option<wgpu::BlendState> = Some(wgpu::BlendState {
},
});

pub struct Row {
pub struct RectBrush {
vertex_buf: wgpu::Buffer,
index_buf: wgpu::Buffer,
instances: wgpu::Buffer,
Expand All @@ -107,7 +107,7 @@ pub struct Row {
scale: f32,
}

impl Renderable for Row {
impl Renderable for RectBrush {
fn init(context: &Context) -> Self {
let device = &context.device;
let vertex_data = create_vertices_rect();
Expand Down Expand Up @@ -231,7 +231,7 @@ impl Renderable for Row {
});

// Done
Row {
RectBrush {
scale: 1.0,
vertex_buf,
index_buf,
Expand Down
File renamed without changes.
10 changes: 0 additions & 10 deletions sugarloaf/src/components/text/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,16 +178,6 @@ where
suggested
};

// if log_enabled!(log::Level::Warn) {
// warn!(
// "Increasing glyph texture size {old:?} -> {new:?}. \
// Consider building with `.initial_cache_size({new:?})` to avoid \
// resizing",
// old = self.glyph_brush.texture_dimensions(),
// new = (new_width, new_height),
// );
// }

pipeline.increase_cache_size(device, new_width, new_height);
self.glyph_brush.resize_texture(new_width, new_height);
}
Expand Down
7 changes: 7 additions & 0 deletions sugarloaf/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ pub struct Sugar {
pub type SugarStack = Vec<Sugar>;
pub type SugarPile = Vec<SugarStack>;

#[derive(Copy, Default, Debug, Clone)]
pub struct SugarloafStyle {
pub screen_position: (f32, f32),
pub bounds: (f32, f32),
pub text_scale: f32,
}

pub fn empty_sugar_pile() -> SugarPile {
vec![vec![]]
}
4 changes: 1 addition & 3 deletions sugarloaf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,4 @@ mod font;
mod sugarloaf;
mod tools;

pub use crate::sugarloaf::{
CustomRenderer, Renderable, RendererTarget, Sugarloaf, SugarloafStyle,
};
pub use crate::sugarloaf::{Renderable, RendererTarget, Sugarloaf};
Loading

0 comments on commit dd6dd28

Please sign in to comment.