pub struct Context { /* private fields */ }
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§
Source§impl Context
impl Context
Sourcepub 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.
Sourcepub fn window_dimensions(&self) -> (u32, u32)
pub fn window_dimensions(&self) -> (u32, u32)
Returns the dimensions of the used window.
Sourcepub fn window_width(&self) -> u32
pub fn window_width(&self) -> u32
Returns the width of the used window.
Sourcepub fn window_height(&self) -> u32
pub fn window_height(&self) -> u32
Returns the height of the used window.
Sourcepub fn resize_window(&mut self, width: u32, height: u32)
pub fn resize_window(&mut self, width: u32, height: u32)
Sets the dimensions of the used window.
Sourcepub fn maximum_texture_size(&self) -> (u32, u32)
pub fn maximum_texture_size(&self) -> (u32, u32)
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());
Sourcepub 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.
Sourcepub fn debug_line<T>(
&mut self,
target: &mut T,
from: (i32, i32),
to: (i32, i32),
color: (f32, f32, f32, f32),
)where
T: DrawTarget,
pub fn debug_line<T>(
&mut self,
target: &mut T,
from: (i32, i32),
to: (i32, i32),
color: (f32, f32, f32, f32),
)where
T: DrawTarget,
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.
Sourcepub fn debug_rectangle<T>(
&mut self,
target: &mut T,
lower_left: (i32, i32),
upper_right: (i32, i32),
color: (f32, f32, f32, f32),
)where
T: DrawTarget,
pub fn debug_rectangle<T>(
&mut self,
target: &mut T,
lower_left: (i32, i32),
upper_right: (i32, i32),
color: (f32, f32, f32, f32),
)where
T: DrawTarget,
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.
Sourcepub 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
Sourcepub fn clear_depth<T>(&mut self, target: &mut T)where
T: DrawTarget,
pub fn clear_depth<T>(&mut self, target: &mut T)where
T: DrawTarget,
Resets the depth buffer of the given DrawTarget
to 1.0
.
Sourcepub fn image_data<T>(&mut self, image: &T) -> RgbaImagewhere
T: DrawTarget,
pub fn image_data<T>(&mut self, image: &T) -> RgbaImagewhere
T: DrawTarget,
Loads the current state of a DrawTarget
into an image.
Sourcepub fn window(&self) -> &Window
pub fn window(&self) -> &Window
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");
Sourcepub fn surface(&mut self) -> WindowSurface
pub fn surface(&mut self) -> WindowSurface
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.
Sourcepub fn present(&mut self, surface: WindowSurface) -> Result<(), FinalizeError>
pub fn present(&mut self, surface: WindowSurface) -> Result<(), FinalizeError>
Presents the current frame to the screen.
Sourcepub unsafe fn unlock_unchecked(self)
pub unsafe fn unlock_unchecked(self)
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.