Skip to content

Commit 9bc0c20

Browse files
committed
boing-ggez: Port ggez APIs to 0.8
1 parent 8d41b6e commit 9bc0c20

File tree

9 files changed

+104
-85
lines changed

9 files changed

+104
-85
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ The completed ports are:
3030

3131
| Game | Part of | Source Language | Source Libraries | Port Libraries | Tested on |
3232
| :------------------------------------: | :--------------------------------------------------------------------------: | :-------------: | :------------------------------------------: | :------------------------------------------------: | :-------: |
33-
| [Boing][Boing] | [Code the Classics Vol. 1][Code the Classics Vol. 1] | Python | [PyGame Zero][PyGame Zero] | [ggez][ggez] 0.7 | Linux |
33+
| [Boing][Boing] | [Code the Classics Vol. 1][Code the Classics Vol. 1] | Python | [PyGame Zero][PyGame Zero] | [ggez][ggez] 0.8 | Linux |
3434
| [Catacomb II (SDL)][Catacomb II (SDL)] | - | C | [SDL 2][SDL 2] | [Rust-SDL2][Rust-SDL2] 0.35 | Linux |
3535
| [Cavern][Cavern] | [Code the Classics Vol. 1][Code the Classics Vol. 1] | Python | [PyGame Zero][PyGame Zero] | [Macroquad][Macroquad] 0.3 | Linux |
3636
| [Soccer][Soccer] | [Code the Classics Vol. 1][Code the Classics Vol. 1] | Python | [PyGame Zero][PyGame Zero] | [Fyrox][Fyrox] 0.26 | Linux |

boing-ggez/src/ball.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl GraphicEntity for Ball {
4949

5050
impl Ball {
5151
pub fn new(context: &mut Context, dx: f32) -> Self {
52-
let image = Image::new(context, "/ball.png").unwrap();
52+
let image = Image::from_path(context, "/ball.png").unwrap();
5353
let hit_sounds = (0..5)
5454
.map(|i| {
5555
let sound_name = format!("/hit{}.ogg", i);

boing-ggez/src/bat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ impl Bat {
5959
(0..3)
6060
.map(|image_i| {
6161
let image_name = format!("/bat{}{}.png", player, image_i);
62-
Image::new(context, image_name).unwrap()
62+
Image::from_path(context, image_name).unwrap()
6363
})
6464
.collect()
6565
})

boing-ggez/src/controls.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
use ggez::{
2-
event::{Axis, Button, KeyCode},
3-
input::{
4-
gamepad::{self, Gamepad},
5-
keyboard,
6-
},
2+
event::{Axis, Button},
3+
input::gamepad::Gamepad,
4+
winit::event::VirtualKeyCode,
75
Context,
86
};
97

@@ -22,7 +20,7 @@ pub const ANALOG_STICK_TOLERANCE: f32 = 0.1;
2220
// The pad functions are for convenience.
2321
//
2422
pub fn pad_input(context: &Context, pad_number: PadNum, test: fn(&Gamepad) -> bool) -> bool {
25-
let mut pad_iter = gamepad::gamepads(context);
23+
let mut pad_iter = context.gamepad.gamepads();
2624

2725
let pad = match pad_number {
2826
PadNum::Zero => pad_iter.next(),
@@ -74,14 +72,14 @@ pub fn p1_controls(context: &Context, _ball: &Ball, _ai_offset: f32, _bat: &Bat)
7472
pad.is_pressed(Button::DPadDown) || pad.value(Axis::LeftStickY) < -ANALOG_STICK_TOLERANCE
7573
});
7674

77-
let keys_pressed = keyboard::pressed_keys(context);
75+
let keys_pressed = context.keyboard.pressed_keys();
7876

7977
let move_down = pad_0_down_pressed
80-
|| keys_pressed.contains(&KeyCode::Z)
81-
|| keys_pressed.contains(&KeyCode::Down);
78+
|| keys_pressed.contains(&VirtualKeyCode::Z)
79+
|| keys_pressed.contains(&VirtualKeyCode::Down);
8280
let move_up = pad_0_up_pressed
83-
|| keys_pressed.contains(&KeyCode::A)
84-
|| keys_pressed.contains(&KeyCode::Up);
81+
|| keys_pressed.contains(&VirtualKeyCode::A)
82+
|| keys_pressed.contains(&VirtualKeyCode::Up);
8583

8684
if move_down {
8785
PLAYER_SPEED
@@ -100,10 +98,10 @@ pub fn p2_controls(context: &Context, _ball: &Ball, _ai_offset: f32, _bat: &Bat)
10098
pad.is_pressed(Button::DPadDown) || pad.value(Axis::LeftStickY) < -ANALOG_STICK_TOLERANCE
10199
});
102100

103-
let keys_pressed = keyboard::pressed_keys(context);
101+
let keys_pressed = context.keyboard.pressed_keys();
104102

105-
let move_down = pad_1_down_pressed || keys_pressed.contains(&KeyCode::M);
106-
let move_up = pad_1_up_pressed || keys_pressed.contains(&KeyCode::K);
103+
let move_down = pad_1_down_pressed || keys_pressed.contains(&VirtualKeyCode::M);
104+
let move_up = pad_1_up_pressed || keys_pressed.contains(&VirtualKeyCode::K);
107105

108106
if move_down {
109107
PLAYER_SPEED

boing-ggez/src/game.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use ggez::graphics::{DrawParam, Drawable, Image};
1+
use ggez::graphics::{Canvas, DrawParam, Drawable, Image};
22
use ggez::{audio, Context, GameResult};
33
use glam::Vec2;
44

@@ -39,19 +39,19 @@ impl Game {
3939
Option<fn(&Context, &Ball, f32, &Bat) -> f32>,
4040
),
4141
) -> Self {
42-
let table_image = Image::new(context, "/table.png").unwrap();
42+
let table_image = Image::from_path(context, "/table.png").unwrap();
4343
let effect_images = (0..2)
4444
.map(|image_i| {
4545
let image_name = format!("/effect{}.png", image_i);
46-
Image::new(context, image_name).unwrap()
46+
Image::from_path(context, image_name).unwrap()
4747
})
4848
.collect();
4949
let digit_images = (0..3)
5050
.map(|player| {
5151
(0..=9)
5252
.map(|image_i| {
5353
let image_name = format!("/digit{}{}.png", player, image_i);
54-
Image::new(context, image_name).unwrap()
54+
Image::from_path(context, image_name).unwrap()
5555
})
5656
.collect()
5757
})
@@ -126,14 +126,14 @@ impl Game {
126126
Ok(())
127127
}
128128

129-
pub fn draw(&mut self, context: &mut Context) -> GameResult {
129+
pub fn draw(&mut self, context: &mut Canvas) -> GameResult {
130130
// Draw background
131-
self.table_image.draw(context, DrawParam::new())?;
131+
self.table_image.draw(context, DrawParam::new());
132132

133133
// Draw 'just scored' effects, if required
134134
for (p, bat) in self.bats.iter().enumerate() {
135135
if bat.timer > 0 && self.ball.out() {
136-
self.effect_images[p].draw(context, DrawParam::new())?;
136+
self.effect_images[p].draw(context, DrawParam::new());
137137
}
138138
}
139139

@@ -142,13 +142,13 @@ impl Game {
142142
// the objects together and iterate them, but for this simplification only, it's not worth.
143143

144144
for bat in &mut self.bats {
145-
bat.draw(context)?;
145+
bat.draw(context);
146146
}
147147

148-
self.ball.draw(context)?;
148+
self.ball.draw(context);
149149

150150
for impact in &mut self.impacts {
151-
impact.draw(context)?;
151+
impact.draw(context);
152152
}
153153

154154
// Display scores - outer loop goes through each player
@@ -180,7 +180,7 @@ impl Game {
180180
self.digit_images[colour][score_char_val].draw(
181181
context,
182182
DrawParam::new().dest(Vec2::new((255 + (160 * p) + (i * 55)) as f32, 46.)),
183-
)?;
183+
);
184184
}
185185
}
186186

boing-ggez/src/global_state.rs

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use 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;
55
use ggez::{timer, Context, GameResult};
66

77
use 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

3336
impl 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

boing-ggez/src/graphic_entity.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use ggez::{
2-
graphics::{self, DrawParam, Image},
3-
Context, GameResult,
4-
};
1+
use ggez::graphics::{Canvas, DrawParam, Image};
52
use glam::Vec2;
63

74
/// Trait for implementing the drawing part of an Actor.
@@ -12,11 +9,11 @@ pub trait GraphicEntity {
129

1310
/// Draws an image, anchored to its center.
1411
/// This is due to ggez not supporting anchoring.
15-
fn draw(&mut self, context: &mut Context) -> GameResult {
12+
fn draw(&mut self, canvas: &mut Canvas) {
1613
let dest = Vec2::new(
1714
self.x() - (self.image().width() / 2) as f32,
1815
self.y() - (self.image().height() / 2) as f32,
1916
);
20-
graphics::draw(context, self.image(), DrawParam::new().dest(dest))
17+
canvas.draw(self.image(), DrawParam::new().dest(dest));
2118
}
2219
}

boing-ggez/src/impact.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl Impact {
3232
let images = (0..5)
3333
.map(|i| {
3434
let image_filename = format!("/impact{}.png", i / 2);
35-
Image::new(context, image_filename).unwrap()
35+
Image::from_path(context, image_filename).unwrap()
3636
})
3737
.collect();
3838

0 commit comments

Comments
 (0)