@@ -115,32 +115,77 @@ mod game_of_life {
115
115
}
116
116
}
117
117
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 =
120
124
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
121
128
{
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
+ }
135
168
}
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
+ }
139
184
}
140
185
}
141
- }
186
+ } ) . unwrap ( ) ;
142
187
}
143
- square_texture
188
+ ( square_texture1 , square_texture2 )
144
189
}
145
190
146
191
pub fn main ( ) {
@@ -180,7 +225,7 @@ pub fn main() {
180
225
let texture_creator : TextureCreator < _ > = canvas. texture_creator ( ) ;
181
226
182
227
// 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) ;
184
229
let mut game = game_of_life:: GameOfLife :: new ( ) ;
185
230
186
231
let mut event_pump = sdl_context. event_pump ( ) . unwrap ( ) ;
@@ -208,7 +253,7 @@ pub fn main() {
208
253
}
209
254
210
255
// update the game loop here
211
- if frame >= 29 {
256
+ if frame >= 30 {
212
257
game. update ( ) ;
213
258
frame = 0 ;
214
259
}
@@ -217,6 +262,11 @@ pub fn main() {
217
262
canvas. clear ( ) ;
218
263
for ( i, unit) in ( & game) . into_iter ( ) . enumerate ( ) {
219
264
let i = i as u32 ;
265
+ let square_texture = if frame >= 15 {
266
+ & square_texture1
267
+ } else {
268
+ & square_texture2
269
+ } ;
220
270
if * unit {
221
271
canvas. copy ( & square_texture,
222
272
None ,
0 commit comments