Skip to content

Commit

Permalink
boing-ggez: Port ggez APIs to 0.8
Browse files Browse the repository at this point in the history
  • Loading branch information
64kramsystem committed Jan 4, 2023
1 parent 8d41b6e commit 3d36c99
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 85 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ The completed ports are:

| Game | Part of | Source Language | Source Libraries | Port Libraries | Tested on |
| :------------------------------------: | :--------------------------------------------------------------------------: | :-------------: | :------------------------------------------: | :------------------------------------------------: | :-------: |
| [Boing][Boing] | [Code the Classics Vol. 1][Code the Classics Vol. 1] | Python | [PyGame Zero][PyGame Zero] | [ggez][ggez] 0.7 | Linux |
| [Boing][Boing] | [Code the Classics Vol. 1][Code the Classics Vol. 1] | Python | [PyGame Zero][PyGame Zero] | [ggez][ggez] 0.8 | Linux |
| [Catacomb II (SDL)][Catacomb II (SDL)] | - | C | [SDL 2][SDL 2] | [Rust-SDL2][Rust-SDL2] 0.35 | Linux |
| [Cavern][Cavern] | [Code the Classics Vol. 1][Code the Classics Vol. 1] | Python | [PyGame Zero][PyGame Zero] | [Macroquad][Macroquad] 0.3 | Linux |
| [Soccer][Soccer] | [Code the Classics Vol. 1][Code the Classics Vol. 1] | Python | [PyGame Zero][PyGame Zero] | [Fyrox][Fyrox] 0.26 | Linux |
Expand Down
2 changes: 1 addition & 1 deletion boing-ggez/src/ball.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl GraphicEntity for Ball {

impl Ball {
pub fn new(context: &mut Context, dx: f32) -> Self {
let image = Image::new(context, "/ball.png").unwrap();
let image = Image::from_path(context, "/ball.png").unwrap();
let hit_sounds = (0..5)
.map(|i| {
let sound_name = format!("/hit{}.ogg", i);
Expand Down
2 changes: 1 addition & 1 deletion boing-ggez/src/bat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl Bat {
(0..3)
.map(|image_i| {
let image_name = format!("/bat{}{}.png", player, image_i);
Image::new(context, image_name).unwrap()
Image::from_path(context, image_name).unwrap()
})
.collect()
})
Expand Down
26 changes: 12 additions & 14 deletions boing-ggez/src/controls.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use ggez::{
event::{Axis, Button, KeyCode},
input::{
gamepad::{self, Gamepad},
keyboard,
},
event::{Axis, Button},
input::gamepad::Gamepad,
winit::event::VirtualKeyCode,
Context,
};

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

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

let keys_pressed = keyboard::pressed_keys(context);
let keys_pressed = context.keyboard.pressed_keys();

let move_down = pad_0_down_pressed
|| keys_pressed.contains(&KeyCode::Z)
|| keys_pressed.contains(&KeyCode::Down);
|| keys_pressed.contains(&VirtualKeyCode::Z)
|| keys_pressed.contains(&VirtualKeyCode::Down);
let move_up = pad_0_up_pressed
|| keys_pressed.contains(&KeyCode::A)
|| keys_pressed.contains(&KeyCode::Up);
|| keys_pressed.contains(&VirtualKeyCode::A)
|| keys_pressed.contains(&VirtualKeyCode::Up);

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

let keys_pressed = keyboard::pressed_keys(context);
let keys_pressed = context.keyboard.pressed_keys();

let move_down = pad_1_down_pressed || keys_pressed.contains(&KeyCode::M);
let move_up = pad_1_up_pressed || keys_pressed.contains(&KeyCode::K);
let move_down = pad_1_down_pressed || keys_pressed.contains(&VirtualKeyCode::M);
let move_up = pad_1_up_pressed || keys_pressed.contains(&VirtualKeyCode::K);

if move_down {
PLAYER_SPEED
Expand Down
22 changes: 11 additions & 11 deletions boing-ggez/src/game.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ggez::graphics::{DrawParam, Drawable, Image};
use ggez::graphics::{Canvas, DrawParam, Drawable, Image};
use ggez::{audio, Context, GameResult};
use glam::Vec2;

Expand Down Expand Up @@ -39,19 +39,19 @@ impl Game {
Option<fn(&Context, &Ball, f32, &Bat) -> f32>,
),
) -> Self {
let table_image = Image::new(context, "/table.png").unwrap();
let table_image = Image::from_path(context, "/table.png").unwrap();
let effect_images = (0..2)
.map(|image_i| {
let image_name = format!("/effect{}.png", image_i);
Image::new(context, image_name).unwrap()
Image::from_path(context, image_name).unwrap()
})
.collect();
let digit_images = (0..3)
.map(|player| {
(0..=9)
.map(|image_i| {
let image_name = format!("/digit{}{}.png", player, image_i);
Image::new(context, image_name).unwrap()
Image::from_path(context, image_name).unwrap()
})
.collect()
})
Expand Down Expand Up @@ -126,14 +126,14 @@ impl Game {
Ok(())
}

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

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

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

for bat in &mut self.bats {
bat.draw(context)?;
bat.draw(context);
}

self.ball.draw(context)?;
self.ball.draw(context);

for impact in &mut self.impacts {
impact.draw(context)?;
impact.draw(context);
}

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

Expand Down
43 changes: 26 additions & 17 deletions boing-ggez/src/global_state.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ggez::audio::{self, SoundSource};
use ggez::event::{EventHandler, KeyCode};
use ggez::graphics::{self, Image};
use ggez::input::keyboard::is_key_pressed;
use ggez::event::EventHandler;
use ggez::graphics::{self, Image, Rect};
use ggez::winit::event::VirtualKeyCode;
use ggez::{timer, Context, GameResult};

use crate::ball::Ball;
Expand All @@ -21,6 +21,9 @@ pub struct GlobalState {
space_down: bool,
fire_down: bool,

viewport_rect: Rect,
scissors_rect: Rect,

menu_images: Vec<Image>,
game_over_image: Image,

Expand All @@ -31,15 +34,15 @@ pub struct GlobalState {
}

impl GlobalState {
pub fn new(context: &mut Context) -> Self {
pub fn new(context: &mut Context, viewport_rect: Rect, scissors_rect: Rect) -> Self {
let menu_images = (0..2)
.map(|i| {
let menu_image_filename = format!("/menu{}.png", i);
Image::new(context, menu_image_filename).unwrap()
Image::from_path(context, menu_image_filename).unwrap()
})
.collect();

let game_over_image = Image::new(context, "/over.png").unwrap();
let game_over_image = Image::from_path(context, "/over.png").unwrap();

// For simplicity, we always assume that it's possible to play the music.
let music = audio::Source::new(context, "/theme.ogg").unwrap();
Expand All @@ -55,6 +58,8 @@ impl GlobalState {
num_players: 1,
space_down: false,
fire_down: false,
viewport_rect,
scissors_rect,
menu_images,
game_over_image,
music,
Expand All @@ -76,15 +81,16 @@ impl EventHandler for GlobalState {

// Work out whether the space key has just been pressed - i.e. in the previous frame it wasn't
// down, and in this frame it is.
let space_pressed = is_key_pressed(context, KeyCode::Space) && !self.space_down;
self.space_down = is_key_pressed(context, KeyCode::Space);
let space_pressed =
context.keyboard.is_key_pressed(VirtualKeyCode::Space) && !self.space_down;
self.space_down = context.keyboard.is_key_pressed(VirtualKeyCode::Space);

// We mimick the source project structure for the pad.
let fire_pressed = is_fire_button_pressed(context, PadNum::Zero) && !self.fire_down;
self.fire_down = is_fire_button_pressed(context, PadNum::Zero);

if is_quit_button_pressed(context, PadNum::Zero) {
ggez::event::quit(context);
context.request_quit();
}

match self.state {
Expand All @@ -106,9 +112,9 @@ impl EventHandler for GlobalState {

self.game = Game::new(context, controls);
} else {
let input_up = is_key_pressed(context, KeyCode::Up)
let input_up = context.keyboard.is_key_pressed(VirtualKeyCode::Up)
|| is_pad_up_pressed(context, PadNum::Zero);
let input_down = is_key_pressed(context, KeyCode::Down)
let input_down = context.keyboard.is_key_pressed(VirtualKeyCode::Down)
|| is_pad_down_pressed(context, PadNum::Zero);

if self.num_players == 2 && input_up {
Expand Down Expand Up @@ -147,23 +153,26 @@ impl EventHandler for GlobalState {
}

fn draw(&mut self, context: &mut Context) -> GameResult {
self.game.draw(context)?;
let mut canvas = graphics::Canvas::from_frame(context, graphics::Color::BLACK);
canvas.set_screen_coordinates(self.viewport_rect);
canvas.set_scissor_rect(self.scissors_rect)?;

self.game.draw(&mut canvas)?;

match self.state {
State::Menu => {
graphics::draw(
context,
canvas.draw(
&self.menu_images[self.num_players - 1],
graphics::DrawParam::new(),
)?;
);
}
State::GameOver => {
graphics::draw(context, &self.game_over_image, graphics::DrawParam::new())?;
canvas.draw(&self.game_over_image, graphics::DrawParam::new());
}
State::Play => {}
}

graphics::present(context)?;
canvas.finish(context)?;

timer::yield_now();

Expand Down
9 changes: 3 additions & 6 deletions boing-ggez/src/graphic_entity.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use ggez::{
graphics::{self, DrawParam, Image},
Context, GameResult,
};
use ggez::graphics::{Canvas, DrawParam, Image};
use glam::Vec2;

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

/// Draws an image, anchored to its center.
/// This is due to ggez not supporting anchoring.
fn draw(&mut self, context: &mut Context) -> GameResult {
fn draw(&mut self, canvas: &mut Canvas) {
let dest = Vec2::new(
self.x() - (self.image().width() / 2) as f32,
self.y() - (self.image().height() / 2) as f32,
);
graphics::draw(context, self.image(), DrawParam::new().dest(dest))
canvas.draw(self.image(), DrawParam::new().dest(dest));
}
}
2 changes: 1 addition & 1 deletion boing-ggez/src/impact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl Impact {
let images = (0..5)
.map(|i| {
let image_filename = format!("/impact{}.png", i / 2);
Image::new(context, image_filename).unwrap()
Image::from_path(context, image_filename).unwrap()
})
.collect();

Expand Down
Loading

0 comments on commit 3d36c99

Please sign in to comment.