11use ggez:: audio:: { self , SoundSource } ;
2- use ggez:: event:: { EventHandler , KeyCode } ;
3- use ggez:: graphics:: { self , Image } ;
4- use ggez:: input :: keyboard :: is_key_pressed ;
2+ use ggez:: event:: EventHandler ;
3+ use ggez:: graphics:: { self , Image , Rect } ;
4+ use ggez:: winit :: event :: VirtualKeyCode ;
55use ggez:: { timer, Context , GameResult } ;
66
77use crate :: ball:: Ball ;
@@ -21,6 +21,9 @@ pub struct GlobalState {
2121 space_down : bool ,
2222 fire_down : bool ,
2323
24+ viewport_rect : Rect ,
25+ scissors_rect : Rect ,
26+
2427 menu_images : Vec < Image > ,
2528 game_over_image : Image ,
2629
@@ -31,15 +34,15 @@ pub struct GlobalState {
3134}
3235
3336impl GlobalState {
34- pub fn new ( context : & mut Context ) -> Self {
37+ pub fn new ( context : & mut Context , viewport_rect : Rect , scissors_rect : Rect ) -> Self {
3538 let menu_images = ( 0 ..2 )
3639 . map ( |i| {
3740 let menu_image_filename = format ! ( "/menu{}.png" , i) ;
38- Image :: new ( context, menu_image_filename) . unwrap ( )
41+ Image :: from_path ( context, menu_image_filename) . unwrap ( )
3942 } )
4043 . collect ( ) ;
4144
42- let game_over_image = Image :: new ( context, "/over.png" ) . unwrap ( ) ;
45+ let game_over_image = Image :: from_path ( context, "/over.png" ) . unwrap ( ) ;
4346
4447 // For simplicity, we always assume that it's possible to play the music.
4548 let music = audio:: Source :: new ( context, "/theme.ogg" ) . unwrap ( ) ;
@@ -55,6 +58,8 @@ impl GlobalState {
5558 num_players : 1 ,
5659 space_down : false ,
5760 fire_down : false ,
61+ viewport_rect,
62+ scissors_rect,
5863 menu_images,
5964 game_over_image,
6065 music,
@@ -76,15 +81,16 @@ impl EventHandler for GlobalState {
7681
7782 // Work out whether the space key has just been pressed - i.e. in the previous frame it wasn't
7883 // down, and in this frame it is.
79- let space_pressed = is_key_pressed ( context, KeyCode :: Space ) && !self . space_down ;
80- self . space_down = is_key_pressed ( context, KeyCode :: Space ) ;
84+ let space_pressed =
85+ context. keyboard . is_key_pressed ( VirtualKeyCode :: Space ) && !self . space_down ;
86+ self . space_down = context. keyboard . is_key_pressed ( VirtualKeyCode :: Space ) ;
8187
8288 // We mimick the source project structure for the pad.
8389 let fire_pressed = is_fire_button_pressed ( context, PadNum :: Zero ) && !self . fire_down ;
8490 self . fire_down = is_fire_button_pressed ( context, PadNum :: Zero ) ;
8591
8692 if is_quit_button_pressed ( context, PadNum :: Zero ) {
87- ggez :: event :: quit ( context) ;
93+ context. request_quit ( ) ;
8894 }
8995
9096 match self . state {
@@ -106,9 +112,9 @@ impl EventHandler for GlobalState {
106112
107113 self . game = Game :: new ( context, controls) ;
108114 } else {
109- let input_up = is_key_pressed ( context , KeyCode :: Up )
115+ let input_up = context . keyboard . is_key_pressed ( VirtualKeyCode :: Up )
110116 || is_pad_up_pressed ( context, PadNum :: Zero ) ;
111- let input_down = is_key_pressed ( context , KeyCode :: Down )
117+ let input_down = context . keyboard . is_key_pressed ( VirtualKeyCode :: Down )
112118 || is_pad_down_pressed ( context, PadNum :: Zero ) ;
113119
114120 if self . num_players == 2 && input_up {
@@ -147,23 +153,26 @@ impl EventHandler for GlobalState {
147153 }
148154
149155 fn draw ( & mut self , context : & mut Context ) -> GameResult {
150- self . game . draw ( context) ?;
156+ let mut canvas = graphics:: Canvas :: from_frame ( context, graphics:: Color :: BLACK ) ;
157+ canvas. set_screen_coordinates ( self . viewport_rect ) ;
158+ canvas. set_scissor_rect ( self . scissors_rect ) ?;
159+
160+ self . game . draw ( & mut canvas) ?;
151161
152162 match self . state {
153163 State :: Menu => {
154- graphics:: draw (
155- context,
164+ canvas. draw (
156165 & self . menu_images [ self . num_players - 1 ] ,
157166 graphics:: DrawParam :: new ( ) ,
158- ) ? ;
167+ ) ;
159168 }
160169 State :: GameOver => {
161- graphics :: draw ( context , & self . game_over_image , graphics:: DrawParam :: new ( ) ) ? ;
170+ canvas . draw ( & self . game_over_image , graphics:: DrawParam :: new ( ) ) ;
162171 }
163172 State :: Play => { }
164173 }
165174
166- graphics :: present ( context) ?;
175+ canvas . finish ( context) ?;
167176
168177 timer:: yield_now ( ) ;
169178
0 commit comments