Skip to content

Commit ca83f9f

Browse files
authored
193 berror (#196)
* #193 - Baseline, adds return_error example as a playground showing how it currently works. Messy. * #193 - Initial testing of a BResult system. Not perfect or tested on all platforms yet. * Fixup #193 for Curses builds. * Fixup #193 for Crossterm builds. * #193 - Add the file I forgot. * #193 - Remove commented out result/error type maps.
1 parent e1e90a2 commit ca83f9f

27 files changed

+85
-57
lines changed

bracket-terminal/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ amethyst = { version = "=0.15.0", features = [ "tiles" ], optional = true }
3030
ultraviolet = "~0.7.5"
3131
parking_lot = { version = "~0.11.1" }
3232
ctrlc = { version = "~3.1", optional=true }
33+
anyhow = "~1.0"
3334

3435
[target.'cfg(not(any(target_arch = "wasm32")))'.dependencies]
3536
glutin = {version = "0.26.0", optional = true }
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// This example demonstrates error returning.
2+
//////////////////////////////////////////////////////////////
3+
4+
use bracket_terminal::prelude::*;
5+
6+
bracket_terminal::add_wasm_support!();
7+
8+
struct State {}
9+
10+
impl GameState for State {
11+
fn tick(&mut self, ctx: &mut BTerm) {
12+
ctx.print(1, 1, "Hello Bracket World");
13+
}
14+
}
15+
16+
fn build_context() -> BResult<BTerm> {
17+
BTermBuilder::simple80x50()
18+
.with_title("Hello Minimal Bracket World")
19+
.build()
20+
}
21+
22+
fn main() -> BError {
23+
let context = build_context()?;
24+
let gs: State = State {};
25+
26+
main_loop(context, gs)
27+
}

bracket-terminal/src/bterm.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{
66
GameState, InitHints, Radians, RenderSprite, Shader, SimpleConsole, SpriteConsole,
77
SpriteSheet, TextAlign, VirtualKeyCode, XpFile, XpLayer, BACKEND, INPUT,
88
},
9-
Result,
9+
BResult,
1010
};
1111
use bracket_color::prelude::RGBA;
1212
use bracket_geometry::prelude::{Point, PointF, Rect};
@@ -87,7 +87,7 @@ impl BTerm {
8787
height_pixels: T,
8888
window_title: S,
8989
platform_hints: InitHints,
90-
) -> Result<BTerm>
90+
) -> BResult<BTerm>
9191
where
9292
T: TryInto<u32>,
9393
{
@@ -148,7 +148,7 @@ impl BTerm {
148148
}
149149

150150
/// Registers a font, and returns its handle number. Do not use after initialization!
151-
pub(crate) fn register_font(&mut self, font: Font) -> Result<usize> {
151+
pub(crate) fn register_font(&mut self, font: Font) -> BResult<usize> {
152152
let mut bi = BACKEND_INTERNAL.lock();
153153
bi.fonts.push(font);
154154
Ok(bi.fonts.len() - 1)
@@ -1058,7 +1058,7 @@ impl BTerm {
10581058
}
10591059

10601060
/// Runs the BTerm application, calling into the provided gamestate handler every tick.
1061-
pub fn main_loop<GS: GameState>(bterm: BTerm, gamestate: GS) -> Result<()> {
1061+
pub fn main_loop<GS: GameState>(bterm: BTerm, gamestate: GS) -> BResult<()> {
10621062
super::hal::main_loop(bterm, gamestate)?;
10631063
Ok(())
10641064
}

bracket-terminal/src/consoles/command_buffer.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// multi-threaded environment.
44

55
use crate::prelude::{BTerm, FontCharType, TextAlign};
6-
use crate::Result;
6+
use crate::BResult;
77
use bracket_color::prelude::{ColorPair, RGBA};
88
use bracket_geometry::prelude::{Point, PointF, Radians, Rect};
99
use object_pool::{Pool, Reusable};
@@ -24,7 +24,7 @@ lazy_static! {
2424

2525
/// Clears the global command buffer. This is called internally by BTerm at the end of each
2626
/// frame. You really shouldn't need to call this yourself.
27-
pub fn clear_command_buffer() -> Result<()> {
27+
pub fn clear_command_buffer() -> BResult<()> {
2828
COMMAND_BUFFER.lock().clear();
2929
Ok(())
3030
}
@@ -161,7 +161,7 @@ impl DrawBatch {
161161
}
162162

163163
/// Submits a batch to the global drawing buffer, and empties the batch.
164-
pub fn submit(&mut self, z_order: usize) -> Result<()> {
164+
pub fn submit(&mut self, z_order: usize) -> BResult<()> {
165165
self.batch.iter_mut().enumerate().for_each(|(i, (z, _))| {
166166
*z = z_order + i;
167167
});
@@ -504,7 +504,7 @@ impl DrawBatch {
504504
}
505505

506506
/// Submits the current batch to the BTerm buffer and empties it
507-
pub fn render_draw_buffer(bterm: &mut BTerm) -> Result<()> {
507+
pub fn render_draw_buffer(bterm: &mut BTerm) -> BResult<()> {
508508
let mut buffer = COMMAND_BUFFER.lock();
509509
buffer.sort_unstable_by(|a, b| a.0.cmp(&b.0));
510510
buffer.iter().for_each(|(_, cmd)| match cmd {

bracket-terminal/src/hal/amethyst_be/font.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::prelude::BACKEND_INTERNAL;
2-
use crate::Result;
2+
use crate::BResult;
33
use amethyst::{
44
assets::Handle,
55
assets::{AssetStorage, Loader},
@@ -31,14 +31,14 @@ impl Font {
3131
}
3232
}
3333

34-
pub fn setup_gl_texture(&mut self, _gl: &crate::hal::BTermPlatform) -> Result<()> {
34+
pub fn setup_gl_texture(&mut self, _gl: &crate::hal::BTermPlatform) -> BResult<()> {
3535
Ok(())
3636
}
3737

3838
pub fn bind_texture(&self, _gl: &crate::hal::BTermPlatform) {}
3939
}
4040

41-
pub fn initialize_fonts(world: &mut World) -> Result<()> {
41+
pub fn initialize_fonts(world: &mut World) -> BResult<()> {
4242
use crate::embedding;
4343
use amethyst::renderer::rendy::texture::TextureBuilder;
4444
use amethyst::renderer::types::TextureData;

bracket-terminal/src/hal/amethyst_be/init.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::prelude::BTerm;
22
use crate::prelude::InitHints;
3-
use crate::Result;
3+
use crate::BResult;
44
use parking_lot::Mutex;
55

66
pub struct PlatformGL {
@@ -23,7 +23,7 @@ pub fn init_raw<S: ToString>(
2323
height_pixels: u32,
2424
window_title: S,
2525
platform_hints: InitHints,
26-
) -> Result<BTerm> {
26+
) -> BResult<BTerm> {
2727
let mut be = BACKEND.lock();
2828
be.window_title = window_title.to_string();
2929
be.platform_hints = platform_hints;

bracket-terminal/src/hal/amethyst_be/mainloop.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::*;
22
use crate::prelude::{BEvent, BTerm, GameState, BACKEND_INTERNAL};
3-
use crate::{clear_input_state, Result};
3+
use crate::{clear_input_state, BResult};
44

55
use amethyst::{
66
core::math::{Point3, Vector3},
@@ -297,7 +297,7 @@ impl BTermGemBridge {
297297
}
298298
}
299299

300-
pub fn main_loop<GS: GameState>(bterm: BTerm, gamestate: GS) -> Result<()> {
300+
pub fn main_loop<GS: GameState>(bterm: BTerm, gamestate: GS) -> BResult<()> {
301301
amethyst::start_logger(Default::default());
302302

303303
let mut cfg = amethyst::window::DisplayConfig::default();

bracket-terminal/src/hal/crossterm_be/font.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::Result;
1+
use crate::BResult;
22

33
#[derive(Clone)]
44
pub struct Font {
@@ -14,7 +14,7 @@ impl Font {
1414
Font { tile_size: (0, 0) }
1515
}
1616

17-
pub fn setup_gl_texture(&mut self, _gl: &crate::hal::BTermPlatform) -> Result<()> {
17+
pub fn setup_gl_texture(&mut self, _gl: &crate::hal::BTermPlatform) -> BResult<()> {
1818
Ok(())
1919
}
2020

bracket-terminal/src/hal/crossterm_be/init.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::{InitHints, BACKEND};
22
use crate::prelude::BTerm;
3-
use crate::Result;
3+
use crate::BResult;
44
use crossterm::{
55
execute,
66
terminal::{size, SetSize},
@@ -12,7 +12,7 @@ pub fn init_raw<S: ToString>(
1212
height_pixels: u32,
1313
_window_title: S,
1414
platform_hints: InitHints,
15-
) -> Result<BTerm> {
15+
) -> BResult<BTerm> {
1616
let old_size = size().expect("Unable to get console size");
1717
execute!(
1818
stdout(),

bracket-terminal/src/hal/crossterm_be/main_loop.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::prelude::{
55
to_char, BEvent, BTerm, GameState, SimpleConsole, SparseConsole, VirtualKeyCode,
66
BACKEND_INTERNAL,
77
};
8-
use crate::{clear_input_state, Result};
8+
use crate::{clear_input_state, BResult};
99
use bracket_color::prelude::*;
1010
use crossterm::event::{poll, read, Event};
1111
use crossterm::execute;
@@ -17,7 +17,7 @@ use std::io::{stdout, Write};
1717
use std::time::Duration;
1818
use std::time::Instant;
1919

20-
pub fn main_loop<GS: GameState>(mut bterm: BTerm, mut gamestate: GS) -> Result<()> {
20+
pub fn main_loop<GS: GameState>(mut bterm: BTerm, mut gamestate: GS) -> BResult<()> {
2121
let now = Instant::now();
2222
let mut prev_seconds = now.elapsed().as_secs();
2323
let mut prev_ms = now.elapsed().as_millis();
@@ -176,7 +176,7 @@ impl Default for OutputBuffer {
176176
}
177177
}
178178

179-
fn full_redraw() -> Result<Vec<OutputBuffer>> {
179+
fn full_redraw() -> BResult<Vec<OutputBuffer>> {
180180
let be = BACKEND.lock();
181181
let mut bi = BACKEND_INTERNAL.lock();
182182

bracket-terminal/src/hal/curses/font.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::Result;
1+
use crate::BResult;
22

33
#[derive(Clone)]
44
pub struct Font {
@@ -14,7 +14,7 @@ impl Font {
1414
Font { tile_size: (1, 1) }
1515
}
1616

17-
pub fn setup_gl_texture(&mut self, _gl: &crate::hal::BTermPlatform) -> Result<()> {
17+
pub fn setup_gl_texture(&mut self, _gl: &crate::hal::BTermPlatform) -> BResult<()> {
1818
Ok(())
1919
}
2020

bracket-terminal/src/hal/curses/init.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub fn init_raw<S: ToString>(
66
height_pixels: u32,
77
_window_title: S,
88
platform_hints: InitHints,
9-
) -> Result<BTerm> {
9+
) -> BResult<BTerm> {
1010
let window = initscr();
1111
resize_term(height_pixels as i32 / 8, width_pixels as i32 / 8);
1212
noecho();

bracket-terminal/src/hal/curses/main_loop.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ use crate::hal::VirtualKeyCode;
44
use crate::prelude::{
55
to_char, BEvent, BTerm, GameState, SimpleConsole, SparseConsole, BACKEND_INTERNAL, RGBA,
66
};
7-
use crate::{clear_input_state, Result};
7+
use crate::{clear_input_state, BResult};
88
use pancurses::endwin;
99
use std::collections::HashSet;
1010
use std::convert::TryInto;
1111
use std::time::Instant;
1212

13-
pub fn main_loop<GS: GameState>(mut bterm: BTerm, mut gamestate: GS) -> Result<()> {
13+
pub fn main_loop<GS: GameState>(mut bterm: BTerm, mut gamestate: GS) -> BResult<()> {
1414
let now = Instant::now();
1515
let mut prev_seconds = now.elapsed().as_secs();
1616
let mut prev_ms = now.elapsed().as_millis();
@@ -145,7 +145,7 @@ impl Default for OutputBuffer {
145145
}
146146

147147
// Completely redraws the back-end
148-
fn full_redraw() -> Result<Vec<OutputBuffer>> {
148+
fn full_redraw() -> BResult<Vec<OutputBuffer>> {
149149
let be = BACKEND.lock();
150150
let window = be.window.as_ref().unwrap();
151151

bracket-terminal/src/hal/curses/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Dummy platform to let it compile and do nothing. Only useful if you don't want a graphical backend.
22
use crate::prelude::BTerm;
3-
use crate::Result;
3+
use crate::BResult;
44
use parking_lot::Mutex;
55

66
pub use winit::event::VirtualKeyCode;

bracket-terminal/src/hal/gl_common/backing/fancy_console_backing.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::hal::{Font, Shader, VertexArray, VertexArrayEntry};
22
use crate::prelude::FlexiTile;
3-
use crate::Result;
3+
use crate::BResult;
44
use bracket_color::prelude::RGBA;
55
use bracket_geometry::prelude::PointF;
66

@@ -176,7 +176,7 @@ impl FancyConsoleBackend {
176176
self.vao.upload_buffers();
177177
}
178178

179-
pub fn gl_draw(&mut self, font: &Font, shader: &Shader) -> Result<()> {
179+
pub fn gl_draw(&mut self, font: &Font, shader: &Shader) -> BResult<()> {
180180
self.vao.draw_elements(shader, font);
181181
Ok(())
182182
}

bracket-terminal/src/hal/gl_common/backing/shared_main_loop.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::hal::{
33
SpriteConsoleBackend, BACKEND, CONSOLE_BACKING,
44
};
55
use crate::prelude::{FlexiConsole, SimpleConsole, SparseConsole, SpriteConsole, BACKEND_INTERNAL};
6-
use crate::Result;
6+
use crate::BResult;
77

88
pub(crate) fn check_console_backing() {
99
let mut be = BACKEND.lock();
@@ -143,7 +143,7 @@ pub(crate) fn rebuild_consoles() {
143143
}
144144
}
145145

146-
pub(crate) fn render_consoles() -> Result<()> {
146+
pub(crate) fn render_consoles() -> BResult<()> {
147147
let bi = BACKEND_INTERNAL.lock();
148148
let mut consoles = CONSOLE_BACKING.lock();
149149
for (i, c) in consoles.iter_mut().enumerate() {

bracket-terminal/src/hal/gl_common/backing/simple_console_backing.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::hal::{Font, Shader, VertexArray, VertexArrayEntry};
22
use crate::prelude::Tile;
3-
use crate::Result;
3+
use crate::BResult;
44
use bracket_color::prelude::RGBA;
55

66
pub struct SimpleConsoleBackend {
@@ -181,7 +181,7 @@ impl SimpleConsoleBackend {
181181
self.vao.upload_buffers();
182182
}
183183

184-
pub fn gl_draw(&mut self, font: &Font, shader: &Shader) -> Result<()> {
184+
pub fn gl_draw(&mut self, font: &Font, shader: &Shader) -> BResult<()> {
185185
self.vao.draw_elements(shader, font);
186186
Ok(())
187187
}

bracket-terminal/src/hal/gl_common/backing/sparse_console_backing.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::hal::{Font, Shader, VertexArray, VertexArrayEntry};
22
use crate::prelude::SparseTile;
3-
use crate::Result;
3+
use crate::BResult;
44
use bracket_color::prelude::RGBA;
55

66
pub struct SparseConsoleBackend {
@@ -143,7 +143,7 @@ impl SparseConsoleBackend {
143143
self.vao.upload_buffers();
144144
}
145145

146-
pub fn gl_draw(&mut self, font: &Font, shader: &Shader) -> Result<()> {
146+
pub fn gl_draw(&mut self, font: &Font, shader: &Shader) -> BResult<()> {
147147
self.vao.draw_elements(shader, font);
148148
Ok(())
149149
}

bracket-terminal/src/hal/gl_common/backing/sprite_console_backing.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::hal::{Font, Shader, VertexArray, VertexArrayEntry};
22
use crate::prelude::{RenderSprite, SpriteSheet};
3-
use crate::Result;
3+
use crate::BResult;
44
use bracket_color::prelude::RGBA;
55

66
pub struct SpriteConsoleBackend {
@@ -151,7 +151,7 @@ impl SpriteConsoleBackend {
151151
self.vao.upload_buffers();
152152
}
153153

154-
pub fn gl_draw(&mut self, font: &Font, shader: &Shader) -> Result<()> {
154+
pub fn gl_draw(&mut self, font: &Font, shader: &Shader) -> BResult<()> {
155155
self.vao.draw_elements(shader, font);
156156
Ok(())
157157
}

bracket-terminal/src/hal/gl_common/font.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::{gl_error, TextureId};
22
use crate::prelude::embedding;
3-
use crate::Result;
3+
use crate::BResult;
44
use bracket_color::prelude::RGB;
55
use glow::HasContext;
66
use image::GenericImageView;
@@ -62,7 +62,7 @@ impl Font {
6262
}
6363

6464
/// Load a font, and allocate it as an OpenGL resource. Returns the OpenGL binding number (which is also set in the structure).
65-
pub fn setup_gl_texture(&mut self, gl: &glow::Context) -> Result<TextureId> {
65+
pub fn setup_gl_texture(&mut self, gl: &glow::Context) -> BResult<TextureId> {
6666
let texture;
6767

6868
unsafe {

0 commit comments

Comments
 (0)