Struct bevy_doryen::doryen::Console
source · pub struct Console { /* private fields */ }
Expand description
This contains the data for a console (including the one displayed on the screen) and methods to draw on it.
Implementations§
source§impl Console
impl Console
sourcepub fn new(width: u32, height: u32) -> Console
pub fn new(width: u32, height: u32) -> Console
create a new offscreen console that you can blit on another console width and height are in cells (characters), not pixels.
Examples found in repository?
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
fn init(mut commands: Commands) {
let mut c1 = DoryenConsole::new(20, 20);
let mut c2 = DoryenConsole::new(20, 20);
for y in 0..20 {
for x in 0..20 {
c1.back(x, y, (((x + y * 10) % 255) as u8, 0, 0, 255));
c2.back(
x,
y,
if (x - 10) * (x - 10) + (y - 10) * (y - 10) < 100 {
(255, 192, 32, 255 - x as u8 * 10)
} else {
(0, 0, 0, 255)
},
);
}
}
c1.print(10, 10, "Hello", TextAlign::Center, None, None);
c2.print(10, 10, "Circle", TextAlign::Center, None, None);
commands.spawn(ConsoleBundle {
console: Console(c1),
position: Position { x: 5, y: 5 },
speed: Speed { x: 1, y: 1 },
alpha: Alpha {
value: 1.0,
step: 0.01,
inverted: false,
},
key_color: KeyColor(None),
});
commands.spawn(ConsoleBundle {
console: Console(c2),
position: Position { x: 15, y: 20 },
speed: Speed { x: -1, y: 1 },
alpha: Alpha {
value: 1.0,
step: 0.01,
inverted: true,
},
key_color: KeyColor(Some((0, 0, 0, 255))),
});
}
sourcepub fn register_color(&mut self, name: &str, value: (u8, u8, u8, u8))
pub fn register_color(&mut self, name: &str, value: (u8, u8, u8, u8))
associate a name with a color for this console.
The color name can then be used in Console::print_color
Example
use doryen_rs::{Console, TextAlign};
let mut con=Console::new(80,25);
con.register_color("pink", (255, 0, 255, 255));
con.print_color(5, 5, "This text contains a #[pink]pink#[] word", TextAlign::Left, None);
Examples found in repository?
More examples
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
fn init(mut root_console: ResMut<RootConsole>, mut commands: Commands) {
root_console.register_color("white", (255, 255, 255, 255));
root_console.register_color("red", (255, 92, 92, 255));
root_console.register_color("blue", (192, 192, 255, 255));
let player = commands
.spawn(PlayerBundle {
player: Player,
position: Position {
x: (CONSOLE_WIDTH / 2) as i32,
y: (CONSOLE_HEIGHT / 2) as i32,
},
})
.id();
let mouse = commands
.spawn(MouseBundle {
mouse: Mouse,
position: Position { x: 0., y: 0. },
})
.id();
commands.insert_resource(Entities { player, mouse });
}
sourcepub fn get_width(&self) -> u32
pub fn get_width(&self) -> u32
Examples found in repository?
More examples
35 36 37 38 39 40 41 42 43 44 45 46 47 48
fn render(fps: Res<FpsInfo>, mut root_console: ResMut<RootConsole>) {
let fps = fps.fps;
let x = (root_console.get_width() / 2) as i32;
let y = (root_console.get_height() / 2) as i32;
root_console.print_color(
x,
y,
&format!("Frames since last second : #[red]{}", fps),
TextAlign::Center,
None,
);
}
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
fn render(mut root_console: ResMut<RootConsole>, mut skull: NonSendMut<SkullImage>) {
let root_console = &mut **root_console;
let skull = &mut *skull;
let scale = skull.scale_time.cos();
root_console.clear(None, Some((0, 0, 0, 255)), None);
skull.skull.blit_ex(
root_console,
(root_console.get_width() / 2) as f32,
(root_console.get_height() / 2) as f32,
scale,
scale,
skull.angle,
None,
);
}
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
fn update_circle(
root_console: Res<RootConsole>,
entities: Res<Entities>,
mut circle_query: Query<(&mut Position, &mut Radius, &mut Angle, &Circle)>,
) {
let (mut position, mut radius, mut angle, _) = circle_query.get_mut(entities.circle).unwrap();
// update the circle radius and center position
angle.0 += 0.6;
radius.0 = 10.0 + 3.0 * (angle.0 / 10.0).sin();
let cs = (angle.0 / 20.0).cos();
let sn = (angle.0 / 15.0).sin();
position.x = (root_console.get_width() / 2) as f32 + cs * 15.0;
position.y = (root_console.get_height() / 2) as f32 + sn * 15.0;
}
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
fn update_position_and_speed(
root_console: Res<RootConsole>,
step: Res<Step>,
mut position_speed_query: Query<(&mut Position, &mut Speed)>,
) {
if step.0 == 0 {
for (mut position, mut speed) in position_speed_query.iter_mut() {
let size = (
root_console.get_width() as i32,
root_console.get_height() as i32,
);
position.x += speed.x;
if position.x == size.0 - 20 || position.x == 0 {
speed.x = -speed.x;
}
position.y += speed.y;
if position.y == size.1 - 20 || position.y == 0 {
speed.y = -speed.y;
}
}
}
}
fn update_alpha(mut console_query: Query<(&mut Alpha, &Console)>) {
for (mut alpha, _) in console_query.iter_mut() {
if alpha.value <= 0.0 || alpha.value >= 1.0 {
alpha.step = -alpha.step;
}
alpha.value += alpha.step;
}
}
fn update_step(mut step: ResMut<Step>) {
step.0 = (step.0 + 1) % 10;
}
fn render(
mut root_console: ResMut<RootConsole>,
console_query: Query<(&Position, &Alpha, &KeyColor, &Console)>,
) {
let root_console = &mut **root_console;
root_console.clear(Some((0, 0, 0, 255)), None, Some(' ' as u16));
for x in 0..root_console.get_width() as i32 {
for y in 0..root_console.get_height() as i32 {
root_console.back(
x,
y,
if (x + y) & 1 == 1 {
(96, 64, 32, 255)
} else {
(32, 64, 96, 255)
},
);
}
}
root_console.print(
(root_console.get_width() / 2) as i32,
(root_console.get_height() / 2) as i32,
"You create offscreen consoles\nand blit them on other consoles",
TextAlign::Center,
Some((255, 255, 255, 255)),
None,
);
for (position, alpha, key_color, console) in console_query.iter() {
let alpha = if alpha.inverted {
1.0 - alpha.value
} else {
alpha.value
};
console.0.blit(
position.x,
position.y,
root_console,
alpha,
alpha,
key_color.0,
);
}
}
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
fn render(
mut root_console: ResMut<RootConsole>,
mut perf_test: ResMut<PerfTest>,
fps_info: Res<FpsInfo>,
) {
let fps = fps_info.fps;
let con_width = root_console.get_width();
let con_height = root_console.get_height();
for y in 0..con_height as i32 {
for x in 0..con_width as i32 {
let val = perf_test.rnd();
root_console.back(
x,
y,
(
(val & 0xFF) as u8,
((val >> 8) & 0x5F) as u8,
((val >> 16) & 0x3F) as u8,
255,
),
);
root_console.fore(
x,
y,
(
((val >> 16) & 0xFF) as u8,
((val >> 24) & 0xFF) as u8,
((val >> 32) & 0xFF) as u8,
255,
),
);
root_console.ascii(x, y, ((val >> 40) & 0xFF) as u16);
}
}
root_console.rectangle(
(con_width / 2 - 10) as i32,
(con_height / 2 - 2) as i32,
20,
5,
Some((255, 255, 255, 255)),
Some((0, 0, 0, 255)),
Some(' ' as u16),
);
root_console.print(
(con_width / 2) as i32,
(con_height / 2) as i32,
&format!("{} fps", fps),
TextAlign::Center,
Some((255, 255, 255, 255)),
None,
);
}
sourcepub fn get_height(&self) -> u32
pub fn get_height(&self) -> u32
Examples found in repository?
More examples
35 36 37 38 39 40 41 42 43 44 45 46 47 48
fn render(fps: Res<FpsInfo>, mut root_console: ResMut<RootConsole>) {
let fps = fps.fps;
let x = (root_console.get_width() / 2) as i32;
let y = (root_console.get_height() / 2) as i32;
root_console.print_color(
x,
y,
&format!("Frames since last second : #[red]{}", fps),
TextAlign::Center,
None,
);
}
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
fn render(mut root_console: ResMut<RootConsole>, mut skull: NonSendMut<SkullImage>) {
let root_console = &mut **root_console;
let skull = &mut *skull;
let scale = skull.scale_time.cos();
root_console.clear(None, Some((0, 0, 0, 255)), None);
skull.skull.blit_ex(
root_console,
(root_console.get_width() / 2) as f32,
(root_console.get_height() / 2) as f32,
scale,
scale,
skull.angle,
None,
);
}
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
fn update_circle(
root_console: Res<RootConsole>,
entities: Res<Entities>,
mut circle_query: Query<(&mut Position, &mut Radius, &mut Angle, &Circle)>,
) {
let (mut position, mut radius, mut angle, _) = circle_query.get_mut(entities.circle).unwrap();
// update the circle radius and center position
angle.0 += 0.6;
radius.0 = 10.0 + 3.0 * (angle.0 / 10.0).sin();
let cs = (angle.0 / 20.0).cos();
let sn = (angle.0 / 15.0).sin();
position.x = (root_console.get_width() / 2) as f32 + cs * 15.0;
position.y = (root_console.get_height() / 2) as f32 + sn * 15.0;
}
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
fn update_position_and_speed(
root_console: Res<RootConsole>,
step: Res<Step>,
mut position_speed_query: Query<(&mut Position, &mut Speed)>,
) {
if step.0 == 0 {
for (mut position, mut speed) in position_speed_query.iter_mut() {
let size = (
root_console.get_width() as i32,
root_console.get_height() as i32,
);
position.x += speed.x;
if position.x == size.0 - 20 || position.x == 0 {
speed.x = -speed.x;
}
position.y += speed.y;
if position.y == size.1 - 20 || position.y == 0 {
speed.y = -speed.y;
}
}
}
}
fn update_alpha(mut console_query: Query<(&mut Alpha, &Console)>) {
for (mut alpha, _) in console_query.iter_mut() {
if alpha.value <= 0.0 || alpha.value >= 1.0 {
alpha.step = -alpha.step;
}
alpha.value += alpha.step;
}
}
fn update_step(mut step: ResMut<Step>) {
step.0 = (step.0 + 1) % 10;
}
fn render(
mut root_console: ResMut<RootConsole>,
console_query: Query<(&Position, &Alpha, &KeyColor, &Console)>,
) {
let root_console = &mut **root_console;
root_console.clear(Some((0, 0, 0, 255)), None, Some(' ' as u16));
for x in 0..root_console.get_width() as i32 {
for y in 0..root_console.get_height() as i32 {
root_console.back(
x,
y,
if (x + y) & 1 == 1 {
(96, 64, 32, 255)
} else {
(32, 64, 96, 255)
},
);
}
}
root_console.print(
(root_console.get_width() / 2) as i32,
(root_console.get_height() / 2) as i32,
"You create offscreen consoles\nand blit them on other consoles",
TextAlign::Center,
Some((255, 255, 255, 255)),
None,
);
for (position, alpha, key_color, console) in console_query.iter() {
let alpha = if alpha.inverted {
1.0 - alpha.value
} else {
alpha.value
};
console.0.blit(
position.x,
position.y,
root_console,
alpha,
alpha,
key_color.0,
);
}
}
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
fn render(
mut root_console: ResMut<RootConsole>,
mut perf_test: ResMut<PerfTest>,
fps_info: Res<FpsInfo>,
) {
let fps = fps_info.fps;
let con_width = root_console.get_width();
let con_height = root_console.get_height();
for y in 0..con_height as i32 {
for x in 0..con_width as i32 {
let val = perf_test.rnd();
root_console.back(
x,
y,
(
(val & 0xFF) as u8,
((val >> 8) & 0x5F) as u8,
((val >> 16) & 0x3F) as u8,
255,
),
);
root_console.fore(
x,
y,
(
((val >> 16) & 0xFF) as u8,
((val >> 24) & 0xFF) as u8,
((val >> 32) & 0xFF) as u8,
255,
),
);
root_console.ascii(x, y, ((val >> 40) & 0xFF) as u16);
}
}
root_console.rectangle(
(con_width / 2 - 10) as i32,
(con_height / 2 - 2) as i32,
20,
5,
Some((255, 255, 255, 255)),
Some((0, 0, 0, 255)),
Some(' ' as u16),
);
root_console.print(
(con_width / 2) as i32,
(con_height / 2) as i32,
&format!("{} fps", fps),
TextAlign::Center,
Some((255, 255, 255, 255)),
None,
);
}
pub fn get_size(&self) -> (u32, u32)
pub fn get_pot_width(&self) -> u32
pub fn get_pot_height(&self) -> u32
sourcepub fn borrow_ascii(&self) -> &Vec<u32>
pub fn borrow_ascii(&self) -> &Vec<u32>
for fast reading of the characters values
sourcepub fn borrow_foreground(&self) -> &Vec<(u8, u8, u8, u8)>
pub fn borrow_foreground(&self) -> &Vec<(u8, u8, u8, u8)>
for fast reading of the characters colors
sourcepub fn borrow_background(&self) -> &Vec<(u8, u8, u8, u8)>
pub fn borrow_background(&self) -> &Vec<(u8, u8, u8, u8)>
for fast reading of the background colors
sourcepub fn borrow_mut_ascii(&mut self) -> &mut Vec<u32>
pub fn borrow_mut_ascii(&mut self) -> &mut Vec<u32>
for fast writing of the characters values
sourcepub fn borrow_mut_foreground(&mut self) -> &mut Vec<(u8, u8, u8, u8)>
pub fn borrow_mut_foreground(&mut self) -> &mut Vec<(u8, u8, u8, u8)>
for fast writing of the characters colors
sourcepub fn borrow_mut_background(&mut self) -> &mut Vec<(u8, u8, u8, u8)>
pub fn borrow_mut_background(&mut self) -> &mut Vec<(u8, u8, u8, u8)>
for fast writing of the background colors
sourcepub fn get_back(&self, x: i32, y: i32) -> Option<(u8, u8, u8, u8)>
pub fn get_back(&self, x: i32, y: i32) -> Option<(u8, u8, u8, u8)>
get the background color of a cell (if x,y inside the console)
sourcepub fn get_fore(&self, x: i32, y: i32) -> Option<(u8, u8, u8, u8)>
pub fn get_fore(&self, x: i32, y: i32) -> Option<(u8, u8, u8, u8)>
get the foreground color of a cell (if x,y inside the console)
sourcepub fn get_ascii(&self, x: i32, y: i32) -> Option<u16>
pub fn get_ascii(&self, x: i32, y: i32) -> Option<u16>
get the ascii code of a cell (if x,y inside the console)
sourcepub fn unsafe_get_back(&self, x: i32, y: i32) -> (u8, u8, u8, u8)
pub fn unsafe_get_back(&self, x: i32, y: i32) -> (u8, u8, u8, u8)
get the background color of a cell (no boundary check)
sourcepub fn unsafe_get_fore(&self, x: i32, y: i32) -> (u8, u8, u8, u8)
pub fn unsafe_get_fore(&self, x: i32, y: i32) -> (u8, u8, u8, u8)
get the foreground color of a cell (no boundary check)
sourcepub fn unsafe_get_ascii(&self, x: i32, y: i32) -> u16
pub fn unsafe_get_ascii(&self, x: i32, y: i32) -> u16
get the ascii code of a cell (no boundary check)
sourcepub fn ascii(&mut self, x: i32, y: i32, ascii: u16)
pub fn ascii(&mut self, x: i32, y: i32, ascii: u16)
set the character at a specific position (doesn’t change the color).
Since the glyph associated with an ascii code depends on the font you’re using, doryen-rs can’t provide constants for specific characters except for a few ones used internally.
More information about this here.
You can find some constants that work with most fonts in this file provided by Alex Mooney.
Examples found in repository?
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
fn render_player(
mut root_console: ResMut<RootConsole>,
light_map: NonSend<LightMap>,
query: Query<&Position, With<Player>>,
) {
let player_position = query.single();
let light = light_map
.0
.pixel(player_position.x as u32, player_position.y as u32)
.unwrap();
root_console.ascii(
player_position.character_x(),
player_position.character_y(),
'@' as u16,
);
root_console.fore(
player_position.character_x(),
player_position.character_y(),
light,
);
}
More examples
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
fn render_character(
mut root_console: ResMut<RootConsole>,
query: Query<(&Character, &Position)>,
level_map: Res<LevelMap>,
light_map: NonSend<LightMap>,
) {
for (character, position) in &query {
if !level_map
.0
.is_in_fov(position.x as usize, position.y as usize)
{
continue;
}
let (color, penumbra) = if character.light {
(character.color, false)
} else {
let light = light_map
.0
.pixel(position.x as u32, position.y as u32)
.unwrap();
let penumbra = is_penumbra(light, 100);
let mut color = color_mul(character.color, light);
if penumbra {
color = color_scale(color, LIGHT_COEF);
}
(color, penumbra)
};
root_console.ascii(
position.character_x(),
position.character_y(),
if penumbra { '?' as u16 } else { character.ch },
);
root_console.fore(position.character_x(), position.character_y(), color);
}
}
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
fn render(mut root_console: ResMut<RootConsole>, font: Res<Font>) {
root_console.rectangle(
0,
0,
CONSOLE_WIDTH,
CONSOLE_HEIGHT,
Some((128, 128, 128, 255)),
None,
Some('.' as u16),
);
root_console.area(
10,
10,
5,
5,
Some((255, 64, 64, 255)),
Some((128, 32, 32, 255)),
Some('&' as u16),
);
root_console.ascii(
(CONSOLE_WIDTH / 2) as i32,
(CONSOLE_HEIGHT / 2 - 10) as i32,
'@' as u16,
);
root_console.fore(
(CONSOLE_WIDTH / 2) as i32,
(CONSOLE_HEIGHT / 2 - 10) as i32,
(255, 255, 255, 255),
);
root_console.rectangle(
(CONSOLE_WIDTH / 2 - 20) as i32,
(CONSOLE_HEIGHT / 2 - 2) as i32,
40,
7,
Some((255, 255, 255, 255)),
Some((0, 0, 0, 255)),
Some(' ' as u16),
);
root_console.print(
(CONSOLE_WIDTH / 2) as i32,
(CONSOLE_HEIGHT / 2) as i32,
font.current_font_name,
TextAlign::Center,
Some((255, 255, 255, 255)),
None,
);
root_console.print(
(CONSOLE_WIDTH / 2) as i32,
(CONSOLE_HEIGHT / 2) as i32 + 2,
"PageUp/PageDown to change font",
TextAlign::Center,
Some((255, 192, 128, 255)),
None,
);
}
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
fn render(
mut root_console: ResMut<RootConsole>,
mut perf_test: ResMut<PerfTest>,
fps_info: Res<FpsInfo>,
) {
let fps = fps_info.fps;
let con_width = root_console.get_width();
let con_height = root_console.get_height();
for y in 0..con_height as i32 {
for x in 0..con_width as i32 {
let val = perf_test.rnd();
root_console.back(
x,
y,
(
(val & 0xFF) as u8,
((val >> 8) & 0x5F) as u8,
((val >> 16) & 0x3F) as u8,
255,
),
);
root_console.fore(
x,
y,
(
((val >> 16) & 0xFF) as u8,
((val >> 24) & 0xFF) as u8,
((val >> 32) & 0xFF) as u8,
255,
),
);
root_console.ascii(x, y, ((val >> 40) & 0xFF) as u16);
}
}
root_console.rectangle(
(con_width / 2 - 10) as i32,
(con_height / 2 - 2) as i32,
20,
5,
Some((255, 255, 255, 255)),
Some((0, 0, 0, 255)),
Some(' ' as u16),
);
root_console.print(
(con_width / 2) as i32,
(con_height / 2) as i32,
&format!("{} fps", fps),
TextAlign::Center,
Some((255, 255, 255, 255)),
None,
);
}
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
fn render(
entities: Res<Entities>,
mut root_console: ResMut<RootConsole>,
player_query: Query<(&Position<i32>, &Player)>,
mouse_query: Query<(&Position<f32>, &Mouse)>,
) {
root_console.rectangle(
0,
0,
CONSOLE_WIDTH,
CONSOLE_HEIGHT,
Some((128, 128, 128, 255)),
Some((0, 0, 0, 255)),
Some('.' as u16),
);
root_console.area(
10,
10,
5,
5,
Some((255, 64, 64, 255)),
Some((128, 32, 32, 255)),
Some('&' as u16),
);
let (player_position, _) = player_query.get(entities.player).unwrap();
root_console.ascii(player_position.x, player_position.y, '@' as u16);
root_console.fore(player_position.x, player_position.y, (255, 255, 255, 255));
root_console.print_color(
(CONSOLE_WIDTH / 2) as i32,
(CONSOLE_HEIGHT - 1) as i32,
"#[red]arrows#[white] : move - #[red]CTRL-S#[white] : save screenshot",
TextAlign::Center,
None,
);
let (mouse_position, _) = mouse_query.get(entities.mouse).unwrap();
root_console.print_color(
(CONSOLE_WIDTH / 2) as i32,
(CONSOLE_HEIGHT - 3) as i32,
&format!(
"#[white]Mouse coordinates: #[red]{}, {}",
mouse_position.x, mouse_position.y
),
TextAlign::Center,
None,
);
root_console.print_color(
5,
5,
"#[blue]This blue text contains a #[red]red#[] word",
TextAlign::Left,
None,
);
root_console.back(
mouse_position.x as i32,
mouse_position.y as i32,
(255, 255, 255, 255),
);
}
sourcepub fn fore(&mut self, x: i32, y: i32, col: (u8, u8, u8, u8))
pub fn fore(&mut self, x: i32, y: i32, col: (u8, u8, u8, u8))
set the character color at a specific position
Examples found in repository?
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
fn render_player(
mut root_console: ResMut<RootConsole>,
light_map: NonSend<LightMap>,
query: Query<&Position, With<Player>>,
) {
let player_position = query.single();
let light = light_map
.0
.pixel(player_position.x as u32, player_position.y as u32)
.unwrap();
root_console.ascii(
player_position.character_x(),
player_position.character_y(),
'@' as u16,
);
root_console.fore(
player_position.character_x(),
player_position.character_y(),
light,
);
}
More examples
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
fn render_character(
mut root_console: ResMut<RootConsole>,
query: Query<(&Character, &Position)>,
level_map: Res<LevelMap>,
light_map: NonSend<LightMap>,
) {
for (character, position) in &query {
if !level_map
.0
.is_in_fov(position.x as usize, position.y as usize)
{
continue;
}
let (color, penumbra) = if character.light {
(character.color, false)
} else {
let light = light_map
.0
.pixel(position.x as u32, position.y as u32)
.unwrap();
let penumbra = is_penumbra(light, 100);
let mut color = color_mul(character.color, light);
if penumbra {
color = color_scale(color, LIGHT_COEF);
}
(color, penumbra)
};
root_console.ascii(
position.character_x(),
position.character_y(),
if penumbra { '?' as u16 } else { character.ch },
);
root_console.fore(position.character_x(), position.character_y(), color);
}
}
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
fn render(mut root_console: ResMut<RootConsole>, font: Res<Font>) {
root_console.rectangle(
0,
0,
CONSOLE_WIDTH,
CONSOLE_HEIGHT,
Some((128, 128, 128, 255)),
None,
Some('.' as u16),
);
root_console.area(
10,
10,
5,
5,
Some((255, 64, 64, 255)),
Some((128, 32, 32, 255)),
Some('&' as u16),
);
root_console.ascii(
(CONSOLE_WIDTH / 2) as i32,
(CONSOLE_HEIGHT / 2 - 10) as i32,
'@' as u16,
);
root_console.fore(
(CONSOLE_WIDTH / 2) as i32,
(CONSOLE_HEIGHT / 2 - 10) as i32,
(255, 255, 255, 255),
);
root_console.rectangle(
(CONSOLE_WIDTH / 2 - 20) as i32,
(CONSOLE_HEIGHT / 2 - 2) as i32,
40,
7,
Some((255, 255, 255, 255)),
Some((0, 0, 0, 255)),
Some(' ' as u16),
);
root_console.print(
(CONSOLE_WIDTH / 2) as i32,
(CONSOLE_HEIGHT / 2) as i32,
font.current_font_name,
TextAlign::Center,
Some((255, 255, 255, 255)),
None,
);
root_console.print(
(CONSOLE_WIDTH / 2) as i32,
(CONSOLE_HEIGHT / 2) as i32 + 2,
"PageUp/PageDown to change font",
TextAlign::Center,
Some((255, 192, 128, 255)),
None,
);
}
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
fn render(
mut root_console: ResMut<RootConsole>,
mut perf_test: ResMut<PerfTest>,
fps_info: Res<FpsInfo>,
) {
let fps = fps_info.fps;
let con_width = root_console.get_width();
let con_height = root_console.get_height();
for y in 0..con_height as i32 {
for x in 0..con_width as i32 {
let val = perf_test.rnd();
root_console.back(
x,
y,
(
(val & 0xFF) as u8,
((val >> 8) & 0x5F) as u8,
((val >> 16) & 0x3F) as u8,
255,
),
);
root_console.fore(
x,
y,
(
((val >> 16) & 0xFF) as u8,
((val >> 24) & 0xFF) as u8,
((val >> 32) & 0xFF) as u8,
255,
),
);
root_console.ascii(x, y, ((val >> 40) & 0xFF) as u16);
}
}
root_console.rectangle(
(con_width / 2 - 10) as i32,
(con_height / 2 - 2) as i32,
20,
5,
Some((255, 255, 255, 255)),
Some((0, 0, 0, 255)),
Some(' ' as u16),
);
root_console.print(
(con_width / 2) as i32,
(con_height / 2) as i32,
&format!("{} fps", fps),
TextAlign::Center,
Some((255, 255, 255, 255)),
None,
);
}
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
fn render(
entities: Res<Entities>,
mut root_console: ResMut<RootConsole>,
player_query: Query<(&Position<i32>, &Player)>,
mouse_query: Query<(&Position<f32>, &Mouse)>,
) {
root_console.rectangle(
0,
0,
CONSOLE_WIDTH,
CONSOLE_HEIGHT,
Some((128, 128, 128, 255)),
Some((0, 0, 0, 255)),
Some('.' as u16),
);
root_console.area(
10,
10,
5,
5,
Some((255, 64, 64, 255)),
Some((128, 32, 32, 255)),
Some('&' as u16),
);
let (player_position, _) = player_query.get(entities.player).unwrap();
root_console.ascii(player_position.x, player_position.y, '@' as u16);
root_console.fore(player_position.x, player_position.y, (255, 255, 255, 255));
root_console.print_color(
(CONSOLE_WIDTH / 2) as i32,
(CONSOLE_HEIGHT - 1) as i32,
"#[red]arrows#[white] : move - #[red]CTRL-S#[white] : save screenshot",
TextAlign::Center,
None,
);
let (mouse_position, _) = mouse_query.get(entities.mouse).unwrap();
root_console.print_color(
(CONSOLE_WIDTH / 2) as i32,
(CONSOLE_HEIGHT - 3) as i32,
&format!(
"#[white]Mouse coordinates: #[red]{}, {}",
mouse_position.x, mouse_position.y
),
TextAlign::Center,
None,
);
root_console.print_color(
5,
5,
"#[blue]This blue text contains a #[red]red#[] word",
TextAlign::Left,
None,
);
root_console.back(
mouse_position.x as i32,
mouse_position.y as i32,
(255, 255, 255, 255),
);
}
sourcepub fn back(&mut self, x: i32, y: i32, col: (u8, u8, u8, u8))
pub fn back(&mut self, x: i32, y: i32, col: (u8, u8, u8, u8))
set the background color at a specific position
Examples found in repository?
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
fn render(mut root_console: ResMut<RootConsole>, resize_data: Res<ResizeData>) {
root_console.rectangle(
0,
0,
resize_data.width,
resize_data.height,
Some((128, 128, 128, 255)),
Some((0, 0, 0, 255)),
Some(' ' as u16),
);
root_console.area(
10,
10,
5,
5,
Some((255, 64, 64, 255)),
Some((128, 32, 32, 255)),
Some('&' as u16),
);
root_console.print(
(resize_data.width / 2) as i32,
(resize_data.height / 2) as i32,
&format!("{} x {}", resize_data.width, resize_data.height),
TextAlign::Center,
None,
None,
);
root_console.back(
resize_data.mouse_pos.0 as i32,
resize_data.mouse_pos.1 as i32,
(255, 255, 255, 255),
);
}
More examples
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
fn render(
entities: Res<Entities>,
mut root_console: ResMut<RootConsole>,
circle_query: Query<(&Position, &Radius, &Angle, &Circle)>,
) {
// fill the console with transparent black. The more opaque it is, the faster the previous frames will fade to black.
// replace alpha with a lower value, like 10 or 5 and the effect will last longer.
root_console.clear(None, Some((0, 0, 0, 20)), None);
let (position, radius, angle, _) = circle_query.get(entities.circle).unwrap();
// here we render current frame (only a circle of blue dots)
for r in 0..10 {
let angle = angle.0 + r as f32 * std::f32::consts::PI * 2.0 / 10.0;
let cs = angle.cos();
let sn = angle.sin();
let x = position.x + radius.0 * cs;
let y = position.y + radius.0 * sn;
root_console.back(x as i32, y as i32, (0, 0, 255, 255));
}
}
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
fn init(mut commands: Commands) {
let mut c1 = DoryenConsole::new(20, 20);
let mut c2 = DoryenConsole::new(20, 20);
for y in 0..20 {
for x in 0..20 {
c1.back(x, y, (((x + y * 10) % 255) as u8, 0, 0, 255));
c2.back(
x,
y,
if (x - 10) * (x - 10) + (y - 10) * (y - 10) < 100 {
(255, 192, 32, 255 - x as u8 * 10)
} else {
(0, 0, 0, 255)
},
);
}
}
c1.print(10, 10, "Hello", TextAlign::Center, None, None);
c2.print(10, 10, "Circle", TextAlign::Center, None, None);
commands.spawn(ConsoleBundle {
console: Console(c1),
position: Position { x: 5, y: 5 },
speed: Speed { x: 1, y: 1 },
alpha: Alpha {
value: 1.0,
step: 0.01,
inverted: false,
},
key_color: KeyColor(None),
});
commands.spawn(ConsoleBundle {
console: Console(c2),
position: Position { x: 15, y: 20 },
speed: Speed { x: -1, y: 1 },
alpha: Alpha {
value: 1.0,
step: 0.01,
inverted: true,
},
key_color: KeyColor(Some((0, 0, 0, 255))),
});
}
fn update_position_and_speed(
root_console: Res<RootConsole>,
step: Res<Step>,
mut position_speed_query: Query<(&mut Position, &mut Speed)>,
) {
if step.0 == 0 {
for (mut position, mut speed) in position_speed_query.iter_mut() {
let size = (
root_console.get_width() as i32,
root_console.get_height() as i32,
);
position.x += speed.x;
if position.x == size.0 - 20 || position.x == 0 {
speed.x = -speed.x;
}
position.y += speed.y;
if position.y == size.1 - 20 || position.y == 0 {
speed.y = -speed.y;
}
}
}
}
fn update_alpha(mut console_query: Query<(&mut Alpha, &Console)>) {
for (mut alpha, _) in console_query.iter_mut() {
if alpha.value <= 0.0 || alpha.value >= 1.0 {
alpha.step = -alpha.step;
}
alpha.value += alpha.step;
}
}
fn update_step(mut step: ResMut<Step>) {
step.0 = (step.0 + 1) % 10;
}
fn render(
mut root_console: ResMut<RootConsole>,
console_query: Query<(&Position, &Alpha, &KeyColor, &Console)>,
) {
let root_console = &mut **root_console;
root_console.clear(Some((0, 0, 0, 255)), None, Some(' ' as u16));
for x in 0..root_console.get_width() as i32 {
for y in 0..root_console.get_height() as i32 {
root_console.back(
x,
y,
if (x + y) & 1 == 1 {
(96, 64, 32, 255)
} else {
(32, 64, 96, 255)
},
);
}
}
root_console.print(
(root_console.get_width() / 2) as i32,
(root_console.get_height() / 2) as i32,
"You create offscreen consoles\nand blit them on other consoles",
TextAlign::Center,
Some((255, 255, 255, 255)),
None,
);
for (position, alpha, key_color, console) in console_query.iter() {
let alpha = if alpha.inverted {
1.0 - alpha.value
} else {
alpha.value
};
console.0.blit(
position.x,
position.y,
root_console,
alpha,
alpha,
key_color.0,
);
}
}
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
fn render(
mut root_console: ResMut<RootConsole>,
mut perf_test: ResMut<PerfTest>,
fps_info: Res<FpsInfo>,
) {
let fps = fps_info.fps;
let con_width = root_console.get_width();
let con_height = root_console.get_height();
for y in 0..con_height as i32 {
for x in 0..con_width as i32 {
let val = perf_test.rnd();
root_console.back(
x,
y,
(
(val & 0xFF) as u8,
((val >> 8) & 0x5F) as u8,
((val >> 16) & 0x3F) as u8,
255,
),
);
root_console.fore(
x,
y,
(
((val >> 16) & 0xFF) as u8,
((val >> 24) & 0xFF) as u8,
((val >> 32) & 0xFF) as u8,
255,
),
);
root_console.ascii(x, y, ((val >> 40) & 0xFF) as u16);
}
}
root_console.rectangle(
(con_width / 2 - 10) as i32,
(con_height / 2 - 2) as i32,
20,
5,
Some((255, 255, 255, 255)),
Some((0, 0, 0, 255)),
Some(' ' as u16),
);
root_console.print(
(con_width / 2) as i32,
(con_height / 2) as i32,
&format!("{} fps", fps),
TextAlign::Center,
Some((255, 255, 255, 255)),
None,
);
}
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
fn render(
entities: Res<Entities>,
mut root_console: ResMut<RootConsole>,
player_query: Query<(&Position<i32>, &Player)>,
mouse_query: Query<(&Position<f32>, &Mouse)>,
) {
root_console.rectangle(
0,
0,
CONSOLE_WIDTH,
CONSOLE_HEIGHT,
Some((128, 128, 128, 255)),
Some((0, 0, 0, 255)),
Some('.' as u16),
);
root_console.area(
10,
10,
5,
5,
Some((255, 64, 64, 255)),
Some((128, 32, 32, 255)),
Some('&' as u16),
);
let (player_position, _) = player_query.get(entities.player).unwrap();
root_console.ascii(player_position.x, player_position.y, '@' as u16);
root_console.fore(player_position.x, player_position.y, (255, 255, 255, 255));
root_console.print_color(
(CONSOLE_WIDTH / 2) as i32,
(CONSOLE_HEIGHT - 1) as i32,
"#[red]arrows#[white] : move - #[red]CTRL-S#[white] : save screenshot",
TextAlign::Center,
None,
);
let (mouse_position, _) = mouse_query.get(entities.mouse).unwrap();
root_console.print_color(
(CONSOLE_WIDTH / 2) as i32,
(CONSOLE_HEIGHT - 3) as i32,
&format!(
"#[white]Mouse coordinates: #[red]{}, {}",
mouse_position.x, mouse_position.y
),
TextAlign::Center,
None,
);
root_console.print_color(
5,
5,
"#[blue]This blue text contains a #[red]red#[] word",
TextAlign::Left,
None,
);
root_console.back(
mouse_position.x as i32,
mouse_position.y as i32,
(255, 255, 255, 255),
);
}
sourcepub fn unsafe_ascii(&mut self, x: i32, y: i32, ascii: u16)
pub fn unsafe_ascii(&mut self, x: i32, y: i32, ascii: u16)
set the character at a specific position (no boundary check)
sourcepub fn unsafe_fore(&mut self, x: i32, y: i32, col: (u8, u8, u8, u8))
pub fn unsafe_fore(&mut self, x: i32, y: i32, col: (u8, u8, u8, u8))
set the character color at a specific position (no boundary check)
sourcepub fn unsafe_back(&mut self, x: i32, y: i32, col: (u8, u8, u8, u8))
pub fn unsafe_back(&mut self, x: i32, y: i32, col: (u8, u8, u8, u8))
set the background color at a specific position (no boundary check)
sourcepub fn clear(
&mut self,
fore: Option<(u8, u8, u8, u8)>,
back: Option<(u8, u8, u8, u8)>,
fillchar: Option<u16>
)
pub fn clear( &mut self, fore: Option<(u8, u8, u8, u8)>, back: Option<(u8, u8, u8, u8)>, fillchar: Option<u16> )
fill the whole console with values
Examples found in repository?
More examples
41 42 43 44 45 46 47 48 49 50 51 52 53 54
fn render(mut root_console: ResMut<RootConsole>, mut skull: NonSendMut<SkullImage>) {
root_console.clear(None, Some((0, 0, 0, 255)), None);
skull
.skull
.blit_2x(&mut root_console, 23, 0, 0, 0, None, None, None);
root_console.print(
40,
4,
"Those pixels\nare twice smaller\nthan a console cell.\nMagic!",
TextAlign::Center,
Some((0, 0, 0, 255)),
None,
);
}
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
fn render(mut root_console: ResMut<RootConsole>, mut skull: NonSendMut<SkullImage>) {
let root_console = &mut **root_console;
let skull = &mut *skull;
let scale = skull.scale_time.cos();
root_console.clear(None, Some((0, 0, 0, 255)), None);
skull.skull.blit_ex(
root_console,
(root_console.get_width() / 2) as f32,
(root_console.get_height() / 2) as f32,
scale,
scale,
skull.angle,
None,
);
}
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
fn render(mut root_console: ResMut<RootConsole>, text_input: Res<TextInput>) {
root_console.clear(None, None, Some(' ' as u16));
root_console.print(
5,
5,
&format!(
"Type some text : {}{}",
text_input.text,
// blinking cursor
if text_input.cursor % 25 < 12 {
'_'
} else {
' '
}
),
TextAlign::Left,
Some(WHITE),
None,
);
}
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
fn render(mut root_console: ResMut<RootConsole>, close_requested: Res<CloseRequested>) {
root_console.clear(None, None, Some(' ' as u16));
if close_requested.0 {
root_console.print(
5,
5,
"Exit game ? (press Y or N)",
TextAlign::Left,
Some(WHITE),
None,
);
} else {
root_console.print(
5,
5,
"Press ESC to exit (on web, this does nothing)",
TextAlign::Left,
Some(WHITE),
None,
);
}
}
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
fn render(
entities: Res<Entities>,
mut root_console: ResMut<RootConsole>,
circle_query: Query<(&Position, &Radius, &Angle, &Circle)>,
) {
// fill the console with transparent black. The more opaque it is, the faster the previous frames will fade to black.
// replace alpha with a lower value, like 10 or 5 and the effect will last longer.
root_console.clear(None, Some((0, 0, 0, 20)), None);
let (position, radius, angle, _) = circle_query.get(entities.circle).unwrap();
// here we render current frame (only a circle of blue dots)
for r in 0..10 {
let angle = angle.0 + r as f32 * std::f32::consts::PI * 2.0 / 10.0;
let cs = angle.cos();
let sn = angle.sin();
let x = position.x + radius.0 * cs;
let y = position.y + radius.0 * sn;
root_console.back(x as i32, y as i32, (0, 0, 255, 255));
}
}
sourcepub fn print_color(
&mut self,
x: i32,
y: i32,
text: &str,
align: TextAlign,
back: Option<(u8, u8, u8, u8)>
)
pub fn print_color( &mut self, x: i32, y: i32, text: &str, align: TextAlign, back: Option<(u8, u8, u8, u8)> )
write a multi-color string. Foreground color is defined by #[color_name] patterns inside the string.
color_name must have been registered with Console::register_color
before.
Default foreground color is white, at the start of the string.
When an unknown color name is used, the color goes back to its previous value.
You can then use an empty name to end a color span.
Example
use doryen_rs::{Console, TextAlign};
let mut con=Console::new(80,25);
con.register_color("pink", (255, 0, 255, 255));
con.register_color("blue", (0, 0, 255, 255));
con.print_color(5, 5, "#[blue]This blue text contains a #[pink]pink#[] word", TextAlign::Left, None);
Examples found in repository?
More examples
35 36 37 38 39 40 41 42 43 44 45 46 47 48
fn render(fps: Res<FpsInfo>, mut root_console: ResMut<RootConsole>) {
let fps = fps.fps;
let x = (root_console.get_width() / 2) as i32;
let y = (root_console.get_height() / 2) as i32;
root_console.print_color(
x,
y,
&format!("Frames since last second : #[red]{}", fps),
TextAlign::Center,
None,
);
}
111 112 113 114 115 116 117 118 119 120 121 122
fn render_instructions(mut root_console: ResMut<RootConsole>, fps: Res<FpsInfo>) {
root_console.print_color(
(CONSOLE_WIDTH / 2) as i32,
(CONSOLE_HEIGHT - 2) as i32,
&format!(
"#[white]Move with #[red]arrows or WSAD #[white]Fire with #[red]mouse {:4} fps",
fps.fps
),
TextAlign::Center,
None,
);
}
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
fn render(
entities: Res<Entities>,
mut root_console: ResMut<RootConsole>,
player_query: Query<(&Position<i32>, &Player)>,
mouse_query: Query<(&Position<f32>, &Mouse)>,
) {
root_console.rectangle(
0,
0,
CONSOLE_WIDTH,
CONSOLE_HEIGHT,
Some((128, 128, 128, 255)),
Some((0, 0, 0, 255)),
Some('.' as u16),
);
root_console.area(
10,
10,
5,
5,
Some((255, 64, 64, 255)),
Some((128, 32, 32, 255)),
Some('&' as u16),
);
let (player_position, _) = player_query.get(entities.player).unwrap();
root_console.ascii(player_position.x, player_position.y, '@' as u16);
root_console.fore(player_position.x, player_position.y, (255, 255, 255, 255));
root_console.print_color(
(CONSOLE_WIDTH / 2) as i32,
(CONSOLE_HEIGHT - 1) as i32,
"#[red]arrows#[white] : move - #[red]CTRL-S#[white] : save screenshot",
TextAlign::Center,
None,
);
let (mouse_position, _) = mouse_query.get(entities.mouse).unwrap();
root_console.print_color(
(CONSOLE_WIDTH / 2) as i32,
(CONSOLE_HEIGHT - 3) as i32,
&format!(
"#[white]Mouse coordinates: #[red]{}, {}",
mouse_position.x, mouse_position.y
),
TextAlign::Center,
None,
);
root_console.print_color(
5,
5,
"#[blue]This blue text contains a #[red]red#[] word",
TextAlign::Left,
None,
);
root_console.back(
mouse_position.x as i32,
mouse_position.y as i32,
(255, 255, 255, 255),
);
}
sourcepub fn text_color_len(text: &str) -> usize
pub fn text_color_len(text: &str) -> usize
compute the length of a string containing color codes. Example :
use doryen_rs::Console;
let len = Console::text_color_len("#[red]red text with a #[blue]blue#[] word");
assert_eq!(len, 25); // actual string : "red text with a blue word"
let len = Console::text_color_len("#[red]a\nb");
assert_eq!(len, 3); // actual string : "a\nb"
let len = Console::text_color_len("normal string");
assert_eq!(len, 13);
sourcepub fn print(
&mut self,
x: i32,
y: i32,
text: &str,
align: TextAlign,
fore: Option<(u8, u8, u8, u8)>,
back: Option<(u8, u8, u8, u8)>
)
pub fn print( &mut self, x: i32, y: i32, text: &str, align: TextAlign, fore: Option<(u8, u8, u8, u8)>, back: Option<(u8, u8, u8, u8)> )
write a string. If the string reaches the border of the console, it’s truncated.
If the string contains carriage return "\n"
, multiple lines are printed.
Examples found in repository?
41 42 43 44 45 46 47 48 49 50 51 52 53 54
fn render(mut root_console: ResMut<RootConsole>, mut skull: NonSendMut<SkullImage>) {
root_console.clear(None, Some((0, 0, 0, 255)), None);
skull
.skull
.blit_2x(&mut root_console, 23, 0, 0, 0, None, None, None);
root_console.print(
40,
4,
"Those pixels\nare twice smaller\nthan a console cell.\nMagic!",
TextAlign::Center,
Some((0, 0, 0, 255)),
None,
);
}
More examples
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
fn render(mut root_console: ResMut<RootConsole>, text_input: Res<TextInput>) {
root_console.clear(None, None, Some(' ' as u16));
root_console.print(
5,
5,
&format!(
"Type some text : {}{}",
text_input.text,
// blinking cursor
if text_input.cursor % 25 < 12 {
'_'
} else {
' '
}
),
TextAlign::Left,
Some(WHITE),
None,
);
}
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
fn render(mut root_console: ResMut<RootConsole>, close_requested: Res<CloseRequested>) {
root_console.clear(None, None, Some(' ' as u16));
if close_requested.0 {
root_console.print(
5,
5,
"Exit game ? (press Y or N)",
TextAlign::Left,
Some(WHITE),
None,
);
} else {
root_console.print(
5,
5,
"Press ESC to exit (on web, this does nothing)",
TextAlign::Left,
Some(WHITE),
None,
);
}
}
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
fn render(mut root_console: ResMut<RootConsole>, resize_data: Res<ResizeData>) {
root_console.rectangle(
0,
0,
resize_data.width,
resize_data.height,
Some((128, 128, 128, 255)),
Some((0, 0, 0, 255)),
Some(' ' as u16),
);
root_console.area(
10,
10,
5,
5,
Some((255, 64, 64, 255)),
Some((128, 32, 32, 255)),
Some('&' as u16),
);
root_console.print(
(resize_data.width / 2) as i32,
(resize_data.height / 2) as i32,
&format!("{} x {}", resize_data.width, resize_data.height),
TextAlign::Center,
None,
None,
);
root_console.back(
resize_data.mouse_pos.0 as i32,
resize_data.mouse_pos.1 as i32,
(255, 255, 255, 255),
);
}
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
fn init(mut commands: Commands) {
let mut c1 = DoryenConsole::new(20, 20);
let mut c2 = DoryenConsole::new(20, 20);
for y in 0..20 {
for x in 0..20 {
c1.back(x, y, (((x + y * 10) % 255) as u8, 0, 0, 255));
c2.back(
x,
y,
if (x - 10) * (x - 10) + (y - 10) * (y - 10) < 100 {
(255, 192, 32, 255 - x as u8 * 10)
} else {
(0, 0, 0, 255)
},
);
}
}
c1.print(10, 10, "Hello", TextAlign::Center, None, None);
c2.print(10, 10, "Circle", TextAlign::Center, None, None);
commands.spawn(ConsoleBundle {
console: Console(c1),
position: Position { x: 5, y: 5 },
speed: Speed { x: 1, y: 1 },
alpha: Alpha {
value: 1.0,
step: 0.01,
inverted: false,
},
key_color: KeyColor(None),
});
commands.spawn(ConsoleBundle {
console: Console(c2),
position: Position { x: 15, y: 20 },
speed: Speed { x: -1, y: 1 },
alpha: Alpha {
value: 1.0,
step: 0.01,
inverted: true,
},
key_color: KeyColor(Some((0, 0, 0, 255))),
});
}
fn update_position_and_speed(
root_console: Res<RootConsole>,
step: Res<Step>,
mut position_speed_query: Query<(&mut Position, &mut Speed)>,
) {
if step.0 == 0 {
for (mut position, mut speed) in position_speed_query.iter_mut() {
let size = (
root_console.get_width() as i32,
root_console.get_height() as i32,
);
position.x += speed.x;
if position.x == size.0 - 20 || position.x == 0 {
speed.x = -speed.x;
}
position.y += speed.y;
if position.y == size.1 - 20 || position.y == 0 {
speed.y = -speed.y;
}
}
}
}
fn update_alpha(mut console_query: Query<(&mut Alpha, &Console)>) {
for (mut alpha, _) in console_query.iter_mut() {
if alpha.value <= 0.0 || alpha.value >= 1.0 {
alpha.step = -alpha.step;
}
alpha.value += alpha.step;
}
}
fn update_step(mut step: ResMut<Step>) {
step.0 = (step.0 + 1) % 10;
}
fn render(
mut root_console: ResMut<RootConsole>,
console_query: Query<(&Position, &Alpha, &KeyColor, &Console)>,
) {
let root_console = &mut **root_console;
root_console.clear(Some((0, 0, 0, 255)), None, Some(' ' as u16));
for x in 0..root_console.get_width() as i32 {
for y in 0..root_console.get_height() as i32 {
root_console.back(
x,
y,
if (x + y) & 1 == 1 {
(96, 64, 32, 255)
} else {
(32, 64, 96, 255)
},
);
}
}
root_console.print(
(root_console.get_width() / 2) as i32,
(root_console.get_height() / 2) as i32,
"You create offscreen consoles\nand blit them on other consoles",
TextAlign::Center,
Some((255, 255, 255, 255)),
None,
);
for (position, alpha, key_color, console) in console_query.iter() {
let alpha = if alpha.inverted {
1.0 - alpha.value
} else {
alpha.value
};
console.0.blit(
position.x,
position.y,
root_console,
alpha,
alpha,
key_color.0,
);
}
}
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
fn render(mut root_console: ResMut<RootConsole>, font: Res<Font>) {
root_console.rectangle(
0,
0,
CONSOLE_WIDTH,
CONSOLE_HEIGHT,
Some((128, 128, 128, 255)),
None,
Some('.' as u16),
);
root_console.area(
10,
10,
5,
5,
Some((255, 64, 64, 255)),
Some((128, 32, 32, 255)),
Some('&' as u16),
);
root_console.ascii(
(CONSOLE_WIDTH / 2) as i32,
(CONSOLE_HEIGHT / 2 - 10) as i32,
'@' as u16,
);
root_console.fore(
(CONSOLE_WIDTH / 2) as i32,
(CONSOLE_HEIGHT / 2 - 10) as i32,
(255, 255, 255, 255),
);
root_console.rectangle(
(CONSOLE_WIDTH / 2 - 20) as i32,
(CONSOLE_HEIGHT / 2 - 2) as i32,
40,
7,
Some((255, 255, 255, 255)),
Some((0, 0, 0, 255)),
Some(' ' as u16),
);
root_console.print(
(CONSOLE_WIDTH / 2) as i32,
(CONSOLE_HEIGHT / 2) as i32,
font.current_font_name,
TextAlign::Center,
Some((255, 255, 255, 255)),
None,
);
root_console.print(
(CONSOLE_WIDTH / 2) as i32,
(CONSOLE_HEIGHT / 2) as i32 + 2,
"PageUp/PageDown to change font",
TextAlign::Center,
Some((255, 192, 128, 255)),
None,
);
}
sourcepub fn rectangle(
&mut self,
x: i32,
y: i32,
w: u32,
h: u32,
fore: Option<(u8, u8, u8, u8)>,
back: Option<(u8, u8, u8, u8)>,
fill: Option<u16>
)
pub fn rectangle( &mut self, x: i32, y: i32, w: u32, h: u32, fore: Option<(u8, u8, u8, u8)>, back: Option<(u8, u8, u8, u8)>, fill: Option<u16> )
draw a rectangle, possibly filling it with a character.
Examples found in repository?
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
fn render(mut root_console: ResMut<RootConsole>, resize_data: Res<ResizeData>) {
root_console.rectangle(
0,
0,
resize_data.width,
resize_data.height,
Some((128, 128, 128, 255)),
Some((0, 0, 0, 255)),
Some(' ' as u16),
);
root_console.area(
10,
10,
5,
5,
Some((255, 64, 64, 255)),
Some((128, 32, 32, 255)),
Some('&' as u16),
);
root_console.print(
(resize_data.width / 2) as i32,
(resize_data.height / 2) as i32,
&format!("{} x {}", resize_data.width, resize_data.height),
TextAlign::Center,
None,
None,
);
root_console.back(
resize_data.mouse_pos.0 as i32,
resize_data.mouse_pos.1 as i32,
(255, 255, 255, 255),
);
}
More examples
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
fn render(mut root_console: ResMut<RootConsole>, font: Res<Font>) {
root_console.rectangle(
0,
0,
CONSOLE_WIDTH,
CONSOLE_HEIGHT,
Some((128, 128, 128, 255)),
None,
Some('.' as u16),
);
root_console.area(
10,
10,
5,
5,
Some((255, 64, 64, 255)),
Some((128, 32, 32, 255)),
Some('&' as u16),
);
root_console.ascii(
(CONSOLE_WIDTH / 2) as i32,
(CONSOLE_HEIGHT / 2 - 10) as i32,
'@' as u16,
);
root_console.fore(
(CONSOLE_WIDTH / 2) as i32,
(CONSOLE_HEIGHT / 2 - 10) as i32,
(255, 255, 255, 255),
);
root_console.rectangle(
(CONSOLE_WIDTH / 2 - 20) as i32,
(CONSOLE_HEIGHT / 2 - 2) as i32,
40,
7,
Some((255, 255, 255, 255)),
Some((0, 0, 0, 255)),
Some(' ' as u16),
);
root_console.print(
(CONSOLE_WIDTH / 2) as i32,
(CONSOLE_HEIGHT / 2) as i32,
font.current_font_name,
TextAlign::Center,
Some((255, 255, 255, 255)),
None,
);
root_console.print(
(CONSOLE_WIDTH / 2) as i32,
(CONSOLE_HEIGHT / 2) as i32 + 2,
"PageUp/PageDown to change font",
TextAlign::Center,
Some((255, 192, 128, 255)),
None,
);
}
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
fn render(
mut root_console: ResMut<RootConsole>,
mut perf_test: ResMut<PerfTest>,
fps_info: Res<FpsInfo>,
) {
let fps = fps_info.fps;
let con_width = root_console.get_width();
let con_height = root_console.get_height();
for y in 0..con_height as i32 {
for x in 0..con_width as i32 {
let val = perf_test.rnd();
root_console.back(
x,
y,
(
(val & 0xFF) as u8,
((val >> 8) & 0x5F) as u8,
((val >> 16) & 0x3F) as u8,
255,
),
);
root_console.fore(
x,
y,
(
((val >> 16) & 0xFF) as u8,
((val >> 24) & 0xFF) as u8,
((val >> 32) & 0xFF) as u8,
255,
),
);
root_console.ascii(x, y, ((val >> 40) & 0xFF) as u16);
}
}
root_console.rectangle(
(con_width / 2 - 10) as i32,
(con_height / 2 - 2) as i32,
20,
5,
Some((255, 255, 255, 255)),
Some((0, 0, 0, 255)),
Some(' ' as u16),
);
root_console.print(
(con_width / 2) as i32,
(con_height / 2) as i32,
&format!("{} fps", fps),
TextAlign::Center,
Some((255, 255, 255, 255)),
None,
);
}
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
fn render(
entities: Res<Entities>,
mut root_console: ResMut<RootConsole>,
player_query: Query<(&Position<i32>, &Player)>,
mouse_query: Query<(&Position<f32>, &Mouse)>,
) {
root_console.rectangle(
0,
0,
CONSOLE_WIDTH,
CONSOLE_HEIGHT,
Some((128, 128, 128, 255)),
Some((0, 0, 0, 255)),
Some('.' as u16),
);
root_console.area(
10,
10,
5,
5,
Some((255, 64, 64, 255)),
Some((128, 32, 32, 255)),
Some('&' as u16),
);
let (player_position, _) = player_query.get(entities.player).unwrap();
root_console.ascii(player_position.x, player_position.y, '@' as u16);
root_console.fore(player_position.x, player_position.y, (255, 255, 255, 255));
root_console.print_color(
(CONSOLE_WIDTH / 2) as i32,
(CONSOLE_HEIGHT - 1) as i32,
"#[red]arrows#[white] : move - #[red]CTRL-S#[white] : save screenshot",
TextAlign::Center,
None,
);
let (mouse_position, _) = mouse_query.get(entities.mouse).unwrap();
root_console.print_color(
(CONSOLE_WIDTH / 2) as i32,
(CONSOLE_HEIGHT - 3) as i32,
&format!(
"#[white]Mouse coordinates: #[red]{}, {}",
mouse_position.x, mouse_position.y
),
TextAlign::Center,
None,
);
root_console.print_color(
5,
5,
"#[blue]This blue text contains a #[red]red#[] word",
TextAlign::Left,
None,
);
root_console.back(
mouse_position.x as i32,
mouse_position.y as i32,
(255, 255, 255, 255),
);
}
sourcepub fn area(
&mut self,
x: i32,
y: i32,
w: u32,
h: u32,
fore: Option<(u8, u8, u8, u8)>,
back: Option<(u8, u8, u8, u8)>,
fillchar: Option<u16>
)
pub fn area( &mut self, x: i32, y: i32, w: u32, h: u32, fore: Option<(u8, u8, u8, u8)>, back: Option<(u8, u8, u8, u8)>, fillchar: Option<u16> )
fill an area with values
Examples found in repository?
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
fn render(mut root_console: ResMut<RootConsole>, resize_data: Res<ResizeData>) {
root_console.rectangle(
0,
0,
resize_data.width,
resize_data.height,
Some((128, 128, 128, 255)),
Some((0, 0, 0, 255)),
Some(' ' as u16),
);
root_console.area(
10,
10,
5,
5,
Some((255, 64, 64, 255)),
Some((128, 32, 32, 255)),
Some('&' as u16),
);
root_console.print(
(resize_data.width / 2) as i32,
(resize_data.height / 2) as i32,
&format!("{} x {}", resize_data.width, resize_data.height),
TextAlign::Center,
None,
None,
);
root_console.back(
resize_data.mouse_pos.0 as i32,
resize_data.mouse_pos.1 as i32,
(255, 255, 255, 255),
);
}
More examples
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
fn render(mut root_console: ResMut<RootConsole>, font: Res<Font>) {
root_console.rectangle(
0,
0,
CONSOLE_WIDTH,
CONSOLE_HEIGHT,
Some((128, 128, 128, 255)),
None,
Some('.' as u16),
);
root_console.area(
10,
10,
5,
5,
Some((255, 64, 64, 255)),
Some((128, 32, 32, 255)),
Some('&' as u16),
);
root_console.ascii(
(CONSOLE_WIDTH / 2) as i32,
(CONSOLE_HEIGHT / 2 - 10) as i32,
'@' as u16,
);
root_console.fore(
(CONSOLE_WIDTH / 2) as i32,
(CONSOLE_HEIGHT / 2 - 10) as i32,
(255, 255, 255, 255),
);
root_console.rectangle(
(CONSOLE_WIDTH / 2 - 20) as i32,
(CONSOLE_HEIGHT / 2 - 2) as i32,
40,
7,
Some((255, 255, 255, 255)),
Some((0, 0, 0, 255)),
Some(' ' as u16),
);
root_console.print(
(CONSOLE_WIDTH / 2) as i32,
(CONSOLE_HEIGHT / 2) as i32,
font.current_font_name,
TextAlign::Center,
Some((255, 255, 255, 255)),
None,
);
root_console.print(
(CONSOLE_WIDTH / 2) as i32,
(CONSOLE_HEIGHT / 2) as i32 + 2,
"PageUp/PageDown to change font",
TextAlign::Center,
Some((255, 192, 128, 255)),
None,
);
}
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
fn render(
entities: Res<Entities>,
mut root_console: ResMut<RootConsole>,
player_query: Query<(&Position<i32>, &Player)>,
mouse_query: Query<(&Position<f32>, &Mouse)>,
) {
root_console.rectangle(
0,
0,
CONSOLE_WIDTH,
CONSOLE_HEIGHT,
Some((128, 128, 128, 255)),
Some((0, 0, 0, 255)),
Some('.' as u16),
);
root_console.area(
10,
10,
5,
5,
Some((255, 64, 64, 255)),
Some((128, 32, 32, 255)),
Some('&' as u16),
);
let (player_position, _) = player_query.get(entities.player).unwrap();
root_console.ascii(player_position.x, player_position.y, '@' as u16);
root_console.fore(player_position.x, player_position.y, (255, 255, 255, 255));
root_console.print_color(
(CONSOLE_WIDTH / 2) as i32,
(CONSOLE_HEIGHT - 1) as i32,
"#[red]arrows#[white] : move - #[red]CTRL-S#[white] : save screenshot",
TextAlign::Center,
None,
);
let (mouse_position, _) = mouse_query.get(entities.mouse).unwrap();
root_console.print_color(
(CONSOLE_WIDTH / 2) as i32,
(CONSOLE_HEIGHT - 3) as i32,
&format!(
"#[white]Mouse coordinates: #[red]{}, {}",
mouse_position.x, mouse_position.y
),
TextAlign::Center,
None,
);
root_console.print_color(
5,
5,
"#[blue]This blue text contains a #[red]red#[] word",
TextAlign::Left,
None,
);
root_console.back(
mouse_position.x as i32,
mouse_position.y as i32,
(255, 255, 255, 255),
);
}
sourcepub fn cell(
&mut self,
x: i32,
y: i32,
ascii: Option<u16>,
fore: Option<(u8, u8, u8, u8)>,
back: Option<(u8, u8, u8, u8)>
)
pub fn cell( &mut self, x: i32, y: i32, ascii: Option<u16>, fore: Option<(u8, u8, u8, u8)>, back: Option<(u8, u8, u8, u8)> )
can change all properties of a console cell at once
sourcepub fn blit(
&self,
x: i32,
y: i32,
destination: &mut Console,
fore_alpha: f32,
back_alpha: f32,
key_color: Option<(u8, u8, u8, u8)>
)
pub fn blit( &self, x: i32, y: i32, destination: &mut Console, fore_alpha: f32, back_alpha: f32, key_color: Option<(u8, u8, u8, u8)> )
blit (draw) a console onto another one You can use fore_alpha and back_alpha to blend this console with existing background on the destination. If you define a key color, the cells using this color as background will be ignored. This makes it possible to blit non rectangular zones.
Examples found in repository?
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
fn render(
mut root_console: ResMut<RootConsole>,
console_query: Query<(&Position, &Alpha, &KeyColor, &Console)>,
) {
let root_console = &mut **root_console;
root_console.clear(Some((0, 0, 0, 255)), None, Some(' ' as u16));
for x in 0..root_console.get_width() as i32 {
for y in 0..root_console.get_height() as i32 {
root_console.back(
x,
y,
if (x + y) & 1 == 1 {
(96, 64, 32, 255)
} else {
(32, 64, 96, 255)
},
);
}
}
root_console.print(
(root_console.get_width() / 2) as i32,
(root_console.get_height() / 2) as i32,
"You create offscreen consoles\nand blit them on other consoles",
TextAlign::Center,
Some((255, 255, 255, 255)),
None,
);
for (position, alpha, key_color, console) in console_query.iter() {
let alpha = if alpha.inverted {
1.0 - alpha.value
} else {
alpha.value
};
console.0.blit(
position.x,
position.y,
root_console,
alpha,
alpha,
key_color.0,
);
}
}
Auto Trait Implementations§
impl RefUnwindSafe for Console
impl Send for Console
impl Sync for Console
impl Unpin for Console
impl UnwindSafe for Console
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.