get rendering semi fixed
This commit is contained in:
parent
93af87b482
commit
c2cb73fbc7
3 changed files with 30 additions and 12 deletions
|
@ -1,4 +1,4 @@
|
||||||
const GAME_SIZE: (u32, u32) = (500, 800);
|
const GAME_SIZE: (u32, u32) = (1200, 800);
|
||||||
use std::{
|
use std::{
|
||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
num::NonZeroU32,
|
num::NonZeroU32,
|
||||||
|
|
|
@ -35,13 +35,15 @@ impl<'buf, 'win> RenderCtx<'buf, 'win> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn clear(&mut self, color: u32) {
|
pub fn clear(&mut self, color: u32) {
|
||||||
self.rect(0, 0, self.win_size.0, self.win_size.1, color)
|
self.rect(0, 0, self.context_size.0, self.context_size.1, color)
|
||||||
}
|
}
|
||||||
|
|
||||||
// draw a rectangle in the context
|
/// draw a rectangle in the context
|
||||||
//
|
///
|
||||||
// coordinates are relative to the context
|
/// coordinates are relative to the context
|
||||||
pub fn rect(&mut self, x: u32, y: u32, width: u32, height: u32, color: u32) {
|
///
|
||||||
|
/// does not check against overflows
|
||||||
|
pub fn rect_unchecked(&mut self, x: u32, y: u32, width: u32, height: u32, color: u32) {
|
||||||
// position in buffer coordinates and not relative coordinates
|
// position in buffer coordinates and not relative coordinates
|
||||||
let x_buf_pos = self.context_pos.0 + x;
|
let x_buf_pos = self.context_pos.0 + x;
|
||||||
let y_buf_pos = self.context_pos.1 + y;
|
let y_buf_pos = self.context_pos.1 + y;
|
||||||
|
@ -56,6 +58,25 @@ impl<'buf, 'win> RenderCtx<'buf, 'win> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn rect(&mut self, x: u32, y: u32, width: u32, height: u32, color: u32) {
|
||||||
|
if x >= self.context_size.0 || y >= self.context_size.1 || width == 0 || height == 0 {
|
||||||
|
} else {
|
||||||
|
let width = if (x + width) >= self.context_size.0 {
|
||||||
|
width.saturating_sub((x + width) - self.context_size.0)
|
||||||
|
} else {
|
||||||
|
width
|
||||||
|
};
|
||||||
|
|
||||||
|
let height = if (y + height) >= self.context_size.1 {
|
||||||
|
height.saturating_sub((y + height) - self.context_size.1)
|
||||||
|
} else {
|
||||||
|
height
|
||||||
|
};
|
||||||
|
|
||||||
|
self.rect_unchecked(x, y, width, height, color)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn force_present(self) {
|
pub fn force_present(self) {
|
||||||
self.buffer.present().unwrap()
|
self.buffer.present().unwrap()
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,16 +7,13 @@ use engine::{
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut engine = Engine::new();
|
let mut engine = Engine::new();
|
||||||
let rect_id = engine.insert_into_world(Rc::new(MovingRect::square(0, 0, 1)));
|
let _ = engine.insert_into_world(Rc::new(MovingRect::new(0, 0, 50, 100)));
|
||||||
engine
|
engine
|
||||||
.set_render_fn(|ctx, world, timer| {
|
.set_render_fn(|ctx, world, timer| {
|
||||||
println!("t: {}", timer.game_time_passed());
|
println!("t: {}", timer.game_time_passed());
|
||||||
let obj = Rc::get_mut(world.get_mut(0)).unwrap();
|
let obj = Rc::get_mut(world.get_mut(0)).unwrap();
|
||||||
obj.update_pos(
|
obj.move_obj(1, 0);
|
||||||
((timer.game_time_passed().sin() + 1.0) * 256.) as u32,
|
obj.draw_move(ctx)
|
||||||
((timer.game_time_passed().cos() + 1.0) * 256.) as u32,
|
|
||||||
);
|
|
||||||
obj.display(ctx)
|
|
||||||
// Rect::square((timer.game_time_passed() * 20.) as u32, 0, 200).display(ctx);
|
// Rect::square((timer.game_time_passed() * 20.) as u32, 0, 200).display(ctx);
|
||||||
})
|
})
|
||||||
.run();
|
.run();
|
||||||
|
|
Loading…
Reference in a new issue