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

Partially fix overlay flickering #32

Merged
merged 4 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bevy-steamworks"
version = "0.10.0"
version = "0.11.0"
authors = ["james7132 <contact@jamessliu.com>"]
edition = "2021"
description = "A Bevy plugin for integrating with the Steamworks SDK."
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ use bevy_steamworks::*;
fn main() {
// Use the demo Steam AppId for SpaceWar
App::new()
// it is important to add the plugin before `RenderPlugin` that comes with `DefaultPlugins`
.add_plugins(SteamworksPlugin::init_app(480).unwrap())
.add_plugins(DefaultPlugins)
.add_plugins(SteamworksPlugin::new(AppId(480)))
.run()
}
```
Expand Down Expand Up @@ -72,8 +73,9 @@ fn steam_system(steam_client: Res<Client>) {
fn main() {
// Use the demo Steam AppId for SpaceWar
App::new()
// it is important to add the plugin before `RenderPlugin` that comes with `DefaultPlugins`
.add_plugins(SteamworksPlugin::init_app(480).unwrap())
.add_plugins(DefaultPlugins)
.add_plugins(SteamworksPlugin::new(AppId(480)))
.add_systems(Startup, steam_system)
.run()
}
Expand Down
23 changes: 23 additions & 0 deletions examples/basic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use bevy::prelude::*;
use bevy_steamworks::*;

fn steam_system(steam_client: Res<Client>) {
for friend in steam_client.friends().get_friends(FriendFlags::IMMEDIATE) {
println!(
"Friend: {:?} - {}({:?})",
friend.id(),
friend.name(),
friend.state()
);
}
}

fn main() {
// Use the demo Steam AppId for SpaceWar
App::new()
// it is important to add the plugin before `RenderPlugin` that comes with `DefaultPlugins`
.add_plugins(SteamworksPlugin::init_app(480).unwrap())
.add_plugins(DefaultPlugins)
.add_systems(Startup, steam_system)
.run()
}
96 changes: 54 additions & 42 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
//! fn main() {
//! // Use the demo Steam AppId for SpaceWar
//! App::new()
//! // it is important to add the plugin before `RenderPlugin` that comes with `DefaultPlugins`
//! .add_plugins(SteamworksPlugin::init_app(480).unwrap())
//! .add_plugins(DefaultPlugins)
//! .add_plugins(SteamworksPlugin::new(AppId(480)))
//! .run()
//! }
//! ```
Expand Down Expand Up @@ -53,14 +54,18 @@
//! fn main() {
//! // Use the demo Steam AppId for SpaceWar
//! App::new()
//! // it is important to add the plugin before `RenderPlugin` that comes with `DefaultPlugins`
//! .add_plugins(SteamworksPlugin::init_app(480).unwrap())
//! .add_plugins(DefaultPlugins)
//! .add_plugins(SteamworksPlugin::new(AppId(480)))
//! .add_systems(Startup, steam_system)
//! .run()
//! }
//! ```

use std::{ops::Deref, sync::Arc};
use std::{
ops::Deref,
sync::{Arc, Mutex},
};

use bevy_app::{App, First, Plugin};
use bevy_ecs::{
Expand Down Expand Up @@ -163,54 +168,61 @@ impl Deref for Client {
}

/// A Bevy [`Plugin`] for adding support for the Steam SDK.
pub struct SteamworksPlugin(AppId);
pub struct SteamworksPlugin {
steam: Mutex<Option<(steamworks::Client, SingleClient)>>,
}

impl SteamworksPlugin {
/// Creates a new `SteamworksPlugin`. The provided `app_id` should correspond
/// to the Steam app ID provided by Valve.
pub fn new(app_id: impl Into<AppId>) -> Self {
Self(app_id.into())
pub fn init_app(app_id: impl Into<AppId>) -> Result<Self, SteamAPIInitError> {
Ok(Self {
steam: Mutex::new(Some(steamworks::Client::init_app(app_id.into())?)),
})
}

/// Creates a new `SteamworksPlugin` using the automatically determined app ID.
/// If the game isn't being run through steam this can be provided by placing a steam_appid.txt
/// with the ID inside in the current working directory.
/// Alternatively, you can use `SteamworksPlugin::init_app(<app_id>)` to force a specific app ID.
pub fn init() -> Result<Self, SteamAPIInitError> {
Ok(Self {
steam: Mutex::new(Some(steamworks::Client::init()?)),
})
}
}

impl Plugin for SteamworksPlugin {
fn build(&self, app: &mut App) {
if app.world.contains_resource::<Client>() {
bevy_log::warn!("Attempted to add the Steamworks plugin multiple times!");
return;
}
match steamworks::Client::init_app(self.0) {
Err(err) => bevy_log::error!("Failed to initialize Steamworks client: {}", err),
Ok((client, single)) => {
app.insert_resource(Client(client.clone()))
.insert_resource(register_event_callbacks!(
client,
AuthSessionTicketResponse,
DownloadItemResult,
GameLobbyJoinRequested,
LobbyChatUpdate,
P2PSessionConnectFail,
P2PSessionRequest,
PersonaStateChange,
SteamServerConnectFailure,
SteamServersConnected,
SteamServersDisconnected,
UserAchievementStored,
UserStatsReceived,
UserStatsStored,
ValidateAuthTicketResponse
))
.insert_non_send_resource(single)
.add_event::<SteamworksEvent>()
.configure_sets(First, SteamworksSystem::RunCallbacks)
.add_systems(
First,
run_steam_callbacks
.in_set(SteamworksSystem::RunCallbacks)
.before(bevy_ecs::event::EventUpdates),
);
}
}
let (client, single) = self.steam.lock().unwrap().take().unwrap();
james7132 marked this conversation as resolved.
Show resolved Hide resolved

app.insert_resource(Client(client.clone()))
.insert_resource(register_event_callbacks!(
client,
AuthSessionTicketResponse,
DownloadItemResult,
GameLobbyJoinRequested,
LobbyChatUpdate,
P2PSessionConnectFail,
P2PSessionRequest,
PersonaStateChange,
SteamServerConnectFailure,
SteamServersConnected,
SteamServersDisconnected,
UserAchievementStored,
UserStatsReceived,
UserStatsStored,
ValidateAuthTicketResponse
))
.insert_non_send_resource(single)
.add_event::<SteamworksEvent>()
.configure_sets(First, SteamworksSystem::RunCallbacks)
.add_systems(
First,
run_steam_callbacks
.in_set(SteamworksSystem::RunCallbacks)
.before(bevy_ecs::event::EventUpdates),
);
}
}

Expand Down