Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into error
Browse files Browse the repository at this point in the history
  • Loading branch information
revmischa committed Oct 19, 2024
2 parents 924bd22 + 1dcab29 commit 1db4c17
Show file tree
Hide file tree
Showing 7 changed files with 479 additions and 510 deletions.
2 changes: 1 addition & 1 deletion examples/game-of-life-unsafe-textures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ pub fn main() -> Result<(), String> {
.map_err(|e| e.to_string())?;

// the canvas allows us to both manipulate the property of the window and to change its content
// via hardware or software rendering. See CanvasBuilder for more info.
// via hardware or software rendering.
let mut canvas = window
.into_canvas()
.target_texture()
Expand Down
121 changes: 60 additions & 61 deletions examples/game-of-life.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use sdl3::keyboard::Keycode;
use sdl3::mouse::MouseButton;
use sdl3::pixels::Color;
use sdl3::rect::Point;
use sdl3::render::{Canvas, Texture, TextureCreator, FRect};
use sdl3::render::{Canvas, FRect, Texture, TextureCreator};
use sdl3::video::{Window, WindowContext};

mod game_of_life {
Expand Down Expand Up @@ -131,73 +131,72 @@ fn dummy_texture<'a>(
(&mut square_texture1, TextureColor::Yellow),
(&mut square_texture2, TextureColor::White),
];
canvas
.with_multiple_texture_canvas(textures.iter(), |texture_canvas, user_context| {
texture_canvas.set_draw_color(Color::RGB(0, 0, 0));
texture_canvas.clear();
match *user_context {
TextureColor::Yellow => {
for i in 0..SQUARE_SIZE {
for j in 0..SQUARE_SIZE {
if (i + j) % 4 == 0 {
texture_canvas.set_draw_color(Color::RGB(255, 255, 0));
texture_canvas
.draw_point(Point::new(i as i32, j as i32))
.expect("could not draw point");
}
if (i + j * 2) % 9 == 0 {
texture_canvas.set_draw_color(Color::RGB(200, 200, 0));
texture_canvas
.draw_point(Point::new(i as i32, j as i32))
.expect("could not draw point");
}
canvas.with_multiple_texture_canvas(textures.iter(), |texture_canvas, user_context| {
texture_canvas.set_draw_color(Color::RGB(0, 0, 0));
texture_canvas.clear();
match *user_context {
TextureColor::Yellow => {
for i in 0..SQUARE_SIZE {
for j in 0..SQUARE_SIZE {
if (i + j) % 4 == 0 {
texture_canvas.set_draw_color(Color::RGB(255, 255, 0));
texture_canvas
.draw_point(Point::new(i as i32, j as i32))
.expect("could not draw point");
}
if (i + j * 2) % 9 == 0 {
texture_canvas.set_draw_color(Color::RGB(200, 200, 0));
texture_canvas
.draw_point(Point::new(i as i32, j as i32))
.expect("could not draw point");
}
}
}
TextureColor::White => {
for i in 0..SQUARE_SIZE {
for j in 0..SQUARE_SIZE {
// drawing pixel by pixel isn't very effective, but we only do it once and store
// the texture afterwards so it's still alright!
if (i + j) % 7 == 0 {
// this doesn't mean anything, there was some trial and error to find
// something that wasn't too ugly
texture_canvas.set_draw_color(Color::RGB(192, 192, 192));
texture_canvas
.draw_point(Point::new(i as i32, j as i32))
.expect("could not draw point");
}
if (i + j * 2) % 5 == 0 {
texture_canvas.set_draw_color(Color::RGB(64, 64, 64));
texture_canvas
.draw_point(Point::new(i as i32, j as i32))
.expect("could not draw point");
}
}
TextureColor::White => {
for i in 0..SQUARE_SIZE {
for j in 0..SQUARE_SIZE {
// drawing pixel by pixel isn't very effective, but we only do it once and store
// the texture afterwards so it's still alright!
if (i + j) % 7 == 0 {
// this doesn't mean anything, there was some trial and error to find
// something that wasn't too ugly
texture_canvas.set_draw_color(Color::RGB(192, 192, 192));
texture_canvas
.draw_point(Point::new(i as i32, j as i32))
.expect("could not draw point");
}
if (i + j * 2) % 5 == 0 {
texture_canvas.set_draw_color(Color::RGB(64, 64, 64));
texture_canvas
.draw_point(Point::new(i as i32, j as i32))
.expect("could not draw point");
}
}
}
};
for i in 0..SQUARE_SIZE {
for j in 0..SQUARE_SIZE {
// drawing pixel by pixel isn't very effective, but we only do it once and store
// the texture afterwards so it's still alright!
if (i + j) % 7 == 0 {
// this doesn't mean anything, there was some trial and serror to find
// something that wasn't too ugly
texture_canvas.set_draw_color(Color::RGB(192, 192, 192));
texture_canvas
.draw_point(Point::new(i as i32, j as i32))
.expect("could not draw point");
}
if (i + j * 2) % 5 == 0 {
texture_canvas.set_draw_color(Color::RGB(64, 64, 64));
texture_canvas
.draw_point(Point::new(i as i32, j as i32))
.expect("could not draw point");
}
}
};
for i in 0..SQUARE_SIZE {
for j in 0..SQUARE_SIZE {
// drawing pixel by pixel isn't very effective, but we only do it once and store
// the texture afterwards so it's still alright!
if (i + j) % 7 == 0 {
// this doesn't mean anything, there was some trial and serror to find
// something that wasn't too ugly
texture_canvas.set_draw_color(Color::RGB(192, 192, 192));
texture_canvas
.draw_point(Point::new(i as i32, j as i32))
.expect("could not draw point");
}
if (i + j * 2) % 5 == 0 {
texture_canvas.set_draw_color(Color::RGB(64, 64, 64));
texture_canvas
.draw_point(Point::new(i as i32, j as i32))
.expect("could not draw point");
}
}
});
}
});
}
Ok((square_texture1, square_texture2))
}
Expand All @@ -221,7 +220,7 @@ pub fn main() -> Result<(), String> {
.map_err(|e| e.to_string())?;

// the canvas allows us to both manipulate the property of the window and to change its content
// via hardware or software rendering. See CanvasBuilder for more info.
// via hardware or software rendering.
let mut canvas = window
.into_canvas()
//.target_texture() //FIXME: Unclear how to migrate this to SDL3, cf: https://github.com/libsdl-org/SDL/issues/8059
Expand Down
13 changes: 12 additions & 1 deletion src/sdl3/rect.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Rectangles and points.

use crate::sys;
use render::FRect;
use std::convert::{AsMut, AsRef};
use std::hash::{Hash, Hasher};
use std::mem;
Expand Down Expand Up @@ -514,7 +515,6 @@ impl Rect {

let success = unsafe {
sys::rect::SDL_GetRectIntersection(self.raw(), other.raw(), out.as_mut_ptr())

};

if success {
Expand Down Expand Up @@ -626,6 +626,17 @@ impl From<sys::rect::SDL_Rect> for Rect {
}
}

impl Into<Option<FRect>> for Rect {
fn into(self) -> Option<FRect> {
Some(FRect::new(
self.raw.x as f32,
self.raw.y as f32,
self.raw.w as f32,
self.raw.h as f32,
))
}
}

impl From<(i32, i32, u32, u32)> for Rect {
fn from((x, y, width, height): (i32, i32, u32, u32)) -> Rect {
Rect::new(x, y, width, height)
Expand Down
Loading

0 comments on commit 1db4c17

Please sign in to comment.