Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/current' into merge_with_current
Browse files Browse the repository at this point in the history
  • Loading branch information
aleokdev committed Jun 14, 2024
2 parents 8b71d9e + c8ac751 commit a8de0a8
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 65 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,26 @@ jobs:

- name: Run cargo clippy --all-targets --package tiled
run: cargo clippy --all-targets --package tiled

docs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Update package manager
run: sudo apt-get update

- name: Install dependencies
run: sudo apt-get install -y libsfml-dev libcsfml-dev libasound2-dev libudev-dev

- name: Get stable Rust toolchain with doc
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
components: rust-docs

- name: Run cargo doc -p tiled --no-deps
run: cargo doc -p tiled --no-deps
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Panic on unexpected XML in `ObjectData` content. (#291)
- Divide by zero when parsing a tileset with height/width dimension of 0. (#292)

## [Unreleased]
## [0.11.3]
## Changed
- Replace `libflate` with `flate2`. (#281)

## [0.11.2]
## Changed
- Updated `Image` docs. (#270)
- Update `libflate` dependency to `2.0.0`. (#279)
- Fix some doc links. (#273)
- Update ggez example to 0.9.3.

## [0.11.1]
### Added
Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tiled"
version = "0.11.1"
version = "0.11.3"
description = "A rust crate for loading maps created by the Tiled editor"
categories = ["game-development"]
keywords = ["gamedev", "tiled", "tmx", "map"]
Expand Down Expand Up @@ -34,12 +34,12 @@ path = "examples/ggez/main.rs"
[dependencies]
base64 = "0.22.1"
xml-rs = "0.8.4"
libflate = "2.1.0"
zstd = { version = "0.13.1", optional = true, default-features = false }
flate2 = "1.0.28"

[dev-dependencies.sfml]
version = "0.20.0"
features = ["graphics"]

[dev-dependencies.ggez]
version = "0.7"
version = "0.9.3"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# rs-tiled
```toml
tiled = "0.11.1"
tiled = "0.11.2"
```

[![Rust](https://github.com/mapeditor/rs-tiled/actions/workflows/rust.yml/badge.svg)](https://github.com/mapeditor/rs-tiled/actions/workflows/rust.yml)
Expand Down
60 changes: 35 additions & 25 deletions examples/ggez/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ mod res_reader;
use ggez::{
event::{self, MouseButton},
graphics::{self, DrawParam},
input,
mint::Point2,
Context, GameResult,
Context, GameResult, *,
};
use map::MapHandler;
use res_reader::GgezResourceReader;
Expand Down Expand Up @@ -45,10 +44,8 @@ struct Game {

impl Game {
fn new(ctx: &mut ggez::Context) -> GameResult<Self> {
graphics::set_default_filter(ctx, graphics::FilterMode::Nearest);

// Load the map, using a loader with `GgezResourceReader` for reading from the ggez filesystem
let mut loader = tiled::Loader::with_reader(GgezResourceReader(ctx));
let mut loader = tiled::Loader::with_reader(GgezResourceReader(&mut ctx.fs));
let map = loader.load_tmx_map("/tiled_base64_external.tmx").unwrap();

let map_handler = MapHandler::new(map, ctx).unwrap();
Expand All @@ -72,13 +69,14 @@ impl event::EventHandler<ggez::GameError> for Game {
.map
.background_color()
.unwrap_or([0.1, 0.2, 0.3, 1.0].into());
graphics::clear(ctx, bg_color);
let mut canvas = graphics::Canvas::from_frame(ctx, bg_color);
canvas.set_sampler(graphics::Sampler::nearest_clamp());

self.draw_map(ctx)?;
self.draw_map(ctx, &mut canvas)?;

self.draw_fps(ctx)?;
self.draw_fps(ctx, &mut canvas)?;

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

Ok(())
}
Expand All @@ -89,28 +87,39 @@ impl event::EventHandler<ggez::GameError> for Game {
button: event::MouseButton,
_x: f32,
_y: f32,
) {
) -> Result<(), GameError> {
// Right click toggles the demo animation effect
if button == MouseButton::Right {
self.map.example_animate = !self.map.example_animate;
self.map.invalidate_batch_cache();
}

Ok(())
}

fn mouse_motion_event(&mut self, ctx: &mut Context, _x: f32, _y: f32, dx: f32, dy: f32) {
fn mouse_motion_event(
&mut self,
ctx: &mut Context,
_x: f32,
_y: f32,
dx: f32,
dy: f32,
) -> Result<(), GameError> {
// Left or middle click + drag pans the map around
if input::mouse::button_pressed(ctx, event::MouseButton::Left)
|| input::mouse::button_pressed(ctx, event::MouseButton::Middle)
if ctx.mouse.button_pressed(event::MouseButton::Left)
|| ctx.mouse.button_pressed(event::MouseButton::Middle)
{
self.pan.0 += dx;
self.pan.1 += dy;

// Need to invalidate for parallax to work
self.map.invalidate_batch_cache();
}

Ok(())
}

fn mouse_wheel_event(&mut self, ctx: &mut Context, _x: f32, y: f32) {
fn mouse_wheel_event(&mut self, ctx: &mut Context, _x: f32, y: f32) -> Result<(), GameError> {
// Scroll wheel zooms

let old_scale = self.scale;
Expand All @@ -120,24 +129,26 @@ impl event::EventHandler<ggez::GameError> for Game {
let Point2 {
x: mouse_x,
y: mouse_y,
} = input::mouse::position(ctx);
} = ctx.mouse.position();
self.pan.0 = (self.pan.0 - mouse_x) / old_scale * self.scale + mouse_x;
self.pan.1 = (self.pan.1 - mouse_y) / old_scale * self.scale + mouse_y;

// Need to invalidate for parallax to work
self.map.invalidate_batch_cache();

Ok(())
}
}

impl Game {
fn draw_map(&mut self, ctx: &mut Context) -> GameResult {
fn draw_map(&mut self, ctx: &mut Context, canvas: &mut graphics::Canvas) -> GameResult {
// Draw tiles + objects

let draw_param = DrawParam::default()
.dest([self.pan.0, self.pan.1])
.scale([self.scale, self.scale]);

self.map.draw(ctx, draw_param, self.pan)?;
self.map.draw(ctx, canvas, draw_param, self.pan)?;

// Draw bounds

Expand All @@ -148,25 +159,24 @@ impl Game {
rect,
graphics::Color::from_rgb_u32(0x888888),
)?;
graphics::draw(ctx, &r1, draw_param)?;
canvas.draw(&r1, draw_param);

Ok(())
}

fn draw_fps(&self, ctx: &mut Context) -> GameResult {
let fps = ggez::timer::fps(ctx);
fn draw_fps(&self, ctx: &mut Context, canvas: &mut graphics::Canvas) -> GameResult {
let fps = ctx.time.fps();
let text = graphics::Text::new(format!("{:.0} fps", fps));

let (window_width, _window_height) = graphics::size(ctx);
let (window_width, _window_height) = ctx.gfx.drawable_size();

graphics::draw(
ctx,
canvas.draw(
&text,
DrawParam::default()
.dest([window_width - text.width(ctx) - 40.0, 10.0])
.dest([window_width - text.measure(ctx)?.x - 40.0, 10.0])
.scale([1.25, 1.25])
.color(graphics::Color::WHITE),
)?;
);

Ok(())
}
Expand Down
Loading

0 comments on commit a8de0a8

Please sign in to comment.