Skip to content

Commit

Permalink
Partially fix overlay flickering (#32)
Browse files Browse the repository at this point in the history
* Partially fix overlay flickering

* Update readme

* Update Cargo.toml

Co-authored-by: James Liu <contact@jamessliu.com>

* Update readme

---------

Co-authored-by: James Liu <contact@jamessliu.com>
  • Loading branch information
SDesya74 and james7132 authored Feb 23, 2024
1 parent 9d5d07e commit 6a5dd47
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 46 deletions.
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
8 changes: 5 additions & 3 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 All @@ -83,7 +85,7 @@ fn main() {

|Bevy Version |bevy\_steamworks|
|:------------|:---------------|
|0.13 |0.10 |
|0.13 |0.10, 0.11 |
|0.12 |0.9 |
|0.11 |0.8 |
|0.10 |0.7 |
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();

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

0 comments on commit 6a5dd47

Please sign in to comment.