Skip to content

Commit bcab218

Browse files
committed
Change the pub API behind SDL_SetRenderTarget
* Extensive changes to Canvas * Deleted RendererTarget and TextureCanvas * Updated documentation * Updated examples as well Closes #657
1 parent d762363 commit bcab218

File tree

3 files changed

+239
-139
lines changed

3 files changed

+239
-139
lines changed

examples/game-of-life.rs

Lines changed: 72 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -115,32 +115,77 @@ mod game_of_life {
115115
}
116116
}
117117

118-
fn dummy_texture<'a>(canvas: &mut Canvas<Window>, texture_creator: &'a TextureCreator<WindowContext>) -> Texture<'a>{
119-
let mut square_texture : Texture =
118+
fn dummy_texture<'a>(canvas: &mut Canvas<Window>, texture_creator: &'a TextureCreator<WindowContext>) -> (Texture<'a>, Texture<'a>) {
119+
enum TextureColor {
120+
Yellow,
121+
White,
122+
};
123+
let mut square_texture1 : Texture =
120124
texture_creator.create_texture_target(None, SQUARE_SIZE, SQUARE_SIZE).unwrap();
125+
let mut square_texture2 : Texture =
126+
texture_creator.create_texture_target(None, SQUARE_SIZE, SQUARE_SIZE).unwrap();
127+
// let's change the textures we just created
121128
{
122-
// let's change the texture we just created
123-
let mut texture_canvas = canvas.with_target(&mut square_texture).unwrap();
124-
texture_canvas.set_draw_color(Color::RGB(0, 0, 0));
125-
texture_canvas.clear();
126-
for i in 0..SQUARE_SIZE {
127-
for j in 0..SQUARE_SIZE {
128-
// drawing pixel by pixel isn't very effective, but we only do it once and store
129-
// the texture afterwards so it's still alright!
130-
if (i+j) % 7 == 0 {
131-
// this doesn't mean anything, there was some trial and serror to find
132-
// something that wasn't too ugly
133-
texture_canvas.set_draw_color(Color::RGB(192, 192, 192));
134-
texture_canvas.draw_point(Point::new(i as i32, j as i32)).unwrap();
129+
let textures = vec![
130+
(&mut square_texture1, TextureColor::Yellow),
131+
(&mut square_texture2, TextureColor::White)
132+
];
133+
canvas.with_multiple_texture_canvas(textures.iter(), |texture_canvas, user_context| {
134+
texture_canvas.set_draw_color(Color::RGB(0, 0, 0));
135+
texture_canvas.clear();
136+
match *user_context {
137+
TextureColor::Yellow => {
138+
for i in 0..SQUARE_SIZE {
139+
for j in 0..SQUARE_SIZE {
140+
if (i+j) % 4 == 0 {
141+
texture_canvas.set_draw_color(Color::RGB(255, 255, 0));
142+
texture_canvas.draw_point(Point::new(i as i32, j as i32)).unwrap();
143+
}
144+
if (i+j*2) % 9 == 0 {
145+
texture_canvas.set_draw_color(Color::RGB(200, 200, 0));
146+
texture_canvas.draw_point(Point::new(i as i32, j as i32)).unwrap();
147+
}
148+
}
149+
}
150+
},
151+
TextureColor::White => {
152+
for i in 0..SQUARE_SIZE {
153+
for j in 0..SQUARE_SIZE {
154+
// drawing pixel by pixel isn't very effective, but we only do it once and store
155+
// the texture afterwards so it's still alright!
156+
if (i+j) % 7 == 0 {
157+
// this doesn't mean anything, there was some trial and error to find
158+
// something that wasn't too ugly
159+
texture_canvas.set_draw_color(Color::RGB(192, 192, 192));
160+
texture_canvas.draw_point(Point::new(i as i32, j as i32)).unwrap();
161+
}
162+
if (i+j*2) % 5 == 0 {
163+
texture_canvas.set_draw_color(Color::RGB(64, 64, 64));
164+
texture_canvas.draw_point(Point::new(i as i32, j as i32)).unwrap();
165+
}
166+
}
167+
}
135168
}
136-
if (i+j*2) % 5 == 0 {
137-
texture_canvas.set_draw_color(Color::RGB(64, 64, 64));
138-
texture_canvas.draw_point(Point::new(i as i32, j as i32)).unwrap();
169+
};
170+
for i in 0..SQUARE_SIZE {
171+
for j in 0..SQUARE_SIZE {
172+
// drawing pixel by pixel isn't very effective, but we only do it once and store
173+
// the texture afterwards so it's still alright!
174+
if (i+j) % 7 == 0 {
175+
// this doesn't mean anything, there was some trial and serror to find
176+
// something that wasn't too ugly
177+
texture_canvas.set_draw_color(Color::RGB(192, 192, 192));
178+
texture_canvas.draw_point(Point::new(i as i32, j as i32)).unwrap();
179+
}
180+
if (i+j*2) % 5 == 0 {
181+
texture_canvas.set_draw_color(Color::RGB(64, 64, 64));
182+
texture_canvas.draw_point(Point::new(i as i32, j as i32)).unwrap();
183+
}
139184
}
140185
}
141-
}
186+
}).unwrap();
142187
}
143-
square_texture
188+
(square_texture1, square_texture2)
144189
}
145190

146191
pub fn main() {
@@ -180,7 +225,7 @@ pub fn main() {
180225
let texture_creator : TextureCreator<_> = canvas.texture_creator();
181226

182227
// Create a "target" texture so that we can use our Renderer with it later
183-
let square_texture = dummy_texture(&mut canvas, &texture_creator);
228+
let (square_texture1, square_texture2) = dummy_texture(&mut canvas, &texture_creator);
184229
let mut game = game_of_life::GameOfLife::new();
185230

186231
let mut event_pump = sdl_context.event_pump().unwrap();
@@ -208,7 +253,7 @@ pub fn main() {
208253
}
209254

210255
// update the game loop here
211-
if frame >= 29 {
256+
if frame >= 30 {
212257
game.update();
213258
frame = 0;
214259
}
@@ -217,6 +262,11 @@ pub fn main() {
217262
canvas.clear();
218263
for (i, unit) in (&game).into_iter().enumerate() {
219264
let i = i as u32;
265+
let square_texture = if frame >= 15 {
266+
&square_texture1
267+
} else {
268+
&square_texture2
269+
};
220270
if *unit {
221271
canvas.copy(&square_texture,
222272
None,

examples/renderer-target.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,11 @@ fn main() {
3030
}
3131
}
3232
angle = (angle + 0.5) % 360.;
33-
{
34-
let mut target = canvas.with_target(&mut texture).unwrap();
35-
target.clear();
36-
target.set_draw_color(Color::RGBA(255, 0, 0, 255));
37-
target.fill_rect(Rect::new(0, 0, 400, 300)).unwrap();
38-
} // <- drops the `target` so that the `canvas` can be used again
33+
canvas.with_texture_canvas(&mut texture, |texture_canvas| {
34+
texture_canvas.clear();
35+
texture_canvas.set_draw_color(Color::RGBA(255, 0, 0, 255));
36+
texture_canvas.fill_rect(Rect::new(0, 0, 400, 300)).unwrap();
37+
}).unwrap();
3938
canvas.set_draw_color(Color::RGBA(0, 0, 0, 255));
4039
let dst = Some(Rect::new(0, 0, 400, 300));
4140
canvas.clear();

0 commit comments

Comments
 (0)