Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update boing-ggez to the latest ggez (0.9.3) #149

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ The completed ports are:

<!-- Add new releases at the bottom; this makes more likely to found outdated ports at the top -->

| 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 |
| [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 |
| [Rusty Roguelike][Rusty Roguelike] | [Hands-on Rust: Effective Learning...][Hands-on Rust: Effective Learning...] | Rust | [bracket-lib][bracket-lib], [Legion][Legion] | [bracket-lib][bracket-lib], [Bevy][Bevy] (ECS) 0.7 | Linux |
| [Rusty Roguelike][Rusty Roguelike] | [Hands-on Rust: Effective Learning...][Hands-on Rust: Effective Learning...] | Rust | [bracket-lib][bracket-lib], [Legion][Legion] | [Macroquad][Macroquad] 0.3, [Legion][Legion] 0.3 | Linux |
| [Bunner][Bunner] | [Code the Classics Vol. 1][Code the Classics Vol. 1] | Python | [PyGame Zero][PyGame Zero] | [Macroquad][Macroquad] 0.3 | Linux |
| 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.9 | Linux/Windows |
| [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 |
| [Rusty Roguelike][Rusty Roguelike] | [Hands-on Rust: Effective Learning...][Hands-on Rust: Effective Learning...] | Rust | [bracket-lib][bracket-lib], [Legion][Legion] | [bracket-lib][bracket-lib], [Bevy][Bevy] (ECS) 0.7 | Linux |
| [Rusty Roguelike][Rusty Roguelike] | [Hands-on Rust: Effective Learning...][Hands-on Rust: Effective Learning...] | Rust | [bracket-lib][bracket-lib], [Legion][Legion] | [Macroquad][Macroquad] 0.3, [Legion][Legion] 0.3 | Linux |
| [Bunner][Bunner] | [Code the Classics Vol. 1][Code the Classics Vol. 1] | Python | [PyGame Zero][PyGame Zero] | [Macroquad][Macroquad] 0.3 | Linux |

<!-- Keep the entries of each group sorted by name -->

Expand Down Expand Up @@ -147,9 +147,9 @@ Catacomb II:

A very straightforward port 🙂

This port suffers from one (Winit) bug:
This port suffers from one bug (it's currently unclear if it's a port or library problem):

- corruption when running on fullscreen (reported [here](https://github.com/ggez/ggez/issues/1066)).
- game running too fast (issue [here](https://github.com/rust-gamedev/rust-game-ports/issues/150)).

### Cavern/Macroquad

Expand Down
4 changes: 2 additions & 2 deletions boing-ggez/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[package]
authors = ["Saverio Miroddi <[email protected]>"]
edition = "2018"
edition = "2021"
name = "boing-ggez"
version = "0.1.0"

[dependencies]
fastrand = "1.7.0"
ggez = "0.7.0"
ggez = "0.9.3"
glam = {version = "0.20.5", features = ["mint"]}
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