Struct crow::Context [−][src]
pub struct Context { /* fields omitted */ }
Expand description
A struct storing the global state which is used for all operations which require access to the GPU.
Examples
use crow::{
glutin::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
},
Context, DrawConfig, Texture,
};
fn main() -> Result<(), crow::Error> {
let event_loop = EventLoop::new();
let mut ctx = Context::new(WindowBuilder::new(), &event_loop)?;
let texture = Texture::load(&mut ctx, "./textures/player.png")?;
event_loop.run(
move |event: Event<()>, _window_target: _, control_flow: &mut ControlFlow| match event {
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => *control_flow = ControlFlow::Exit,
Event::MainEventsCleared => ctx.window().request_redraw(),
Event::RedrawRequested(_) => {
let mut surface = ctx.surface();
ctx.clear_color(&mut surface, (0.4, 0.4, 0.8, 1.0));
ctx.draw(&mut surface, &texture, (100, 150), &DrawConfig::default());
ctx.present(surface).unwrap();
}
_ => (),
},
)
}
Implementations
pub fn new<T>(
window: WindowBuilder,
event_loop: &EventLoop<T>
) -> Result<Self, NewContextError>
pub fn new<T>(
window: WindowBuilder,
event_loop: &EventLoop<T>
) -> Result<Self, NewContextError>
Creates a new Context
. It is not possible to have more
than one Context
in a program.
To create a new Context
after a previous context was used,
The previous context has to be dropped using the method
Context::unlock_unchecked()
. This is a workaround and
will probably be fixed in a future release.
Returns the width of the used window.
Returns the height of the used window.
Sets the dimensions of the used window.
Returns the size of the biggest supported texture.
Trying to create a texture with a size
greater than maximum_texture_size
results in an
InvalidTextureSize
error.
use crow::{Context, glutin::{window::WindowBuilder, event_loop::EventLoop}};
let mut ctx = Context::new(WindowBuilder::new(), &EventLoop::new()).unwrap();
println!("maximum supported texture size: {:?}", ctx.maximum_texture_size());
pub fn draw<T>(
&mut self,
target: &mut T,
source: &Texture,
position: (i32, i32),
config: &DrawConfig
) where
T: DrawTarget,
pub fn draw<T>(
&mut self,
target: &mut T,
source: &Texture,
position: (i32, i32),
config: &DrawConfig
) where
T: DrawTarget,
Draws the source
onto target
.
To draw to the window, use Context::window_surface
as a target.
Draws the a line going from from
to to
onto target
with the given color
.
To draw this line to the window, use Context::window_surface
as a target.
Draws the bounding box of an axis-aligned rectangle specified by
its lower_left
and upper_right
corner.
In case lower_left
is to the right or above upper_right
, the two points will be flipped.
To draw this rectangle to the window, use Context::window_surface
as a target.
pub fn clear_color<T>(&mut self, target: &mut T, color: (f32, f32, f32, f32)) where
T: DrawTarget,
pub fn clear_color<T>(&mut self, target: &mut T, color: (f32, f32, f32, f32)) where
T: DrawTarget,
Clears the color of the given DrawTarget
, setting each pixel to color
Resets the depth buffer of the given DrawTarget
to 1.0
.
Loads the current state of a DrawTarget
into an image.
Returns the inner window.
Examples
use crow::{
glutin::{event_loop::EventLoop, window::WindowBuilder},
Context,
};
let context = Context::new(
WindowBuilder::new().with_title("Starting"),
&EventLoop::new(),
)?;
context.window().set_title("Running");
Returns a handle to the window surface.
This handle implements DrawTarget
and can be used to draw to the window.
Use fn Context::present
to actually display the resulting image.
Presents the current frame to the screen.
Drops this context while allowing the initialization of a new one afterwards.
Safety
This method may lead to undefined behavior if a struct, for example a Texture
, which was created using
the current context, is used with the new context.