Skip to content

Commit e9d0332

Browse files
committed
Simplify NetworkingMessages example
1 parent 9b99d7e commit e9d0332

File tree

3 files changed

+58
-84
lines changed

3 files changed

+58
-84
lines changed

examples/networking-messages/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ edition = "2021"
55

66
[dependencies]
77
steamworks = { path = "../.." }
8-
eframe = "0.28.1"
8+
macroquad = "0.4"
+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# NetworkingMessages Example
22

3-
This example demonstrates how to use the NetworkingMessages API to send and receive messages between friends playing the
4-
same game. You can use any SteamID, doesn't have to be a friend. For example, networking messages can be with the steam
5-
matchmaking service.
3+
This example demonstrates how to use the NetworkingMessages API to send and receive messages over the network. It sends
4+
the mouse position to all friends playing the same game. Green circle is your local player and red circles are your
5+
friends.
66

77
## Note
88
To use this example, you need to have two instances on two machines with two steam accounts running at the same time.
+54-80
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
use eframe::{egui::*, *};
1+
use std::collections::HashMap;
2+
3+
use macroquad::prelude::*;
24
use steamworks::{
35
networking_types::{NetworkingIdentity, SendFlags},
46
FriendFlags,
57
};
68

7-
fn main() -> eframe::Result {
9+
#[macroquad::main("steamworks-rs")]
10+
async fn main() {
811
// 480 is Spacewar!, the Steamworks SDK example app.
912
let client =
1013
steamworks::Client::init_app(480).expect("Steam is not running or has not been detected");
@@ -25,84 +28,55 @@ fn main() -> eframe::Result {
2528
eprintln!("Session failed: {info:#?}");
2629
});
2730

28-
// UI state
29-
let mut text_field = "Hello, world!".to_string();
30-
let mut message_history = vec![];
31+
// Keep track of all players
32+
let mut peers = HashMap::new();
3133

32-
run_simple_native("steamworks-rs", Default::default(), move |ctx, _| {
33-
// Run callback periodically, this is usually your main game loop or networking thread
34+
loop {
35+
// Poll the internal callbacks
3436
client.run_callbacks();
35-
ctx.request_repaint();
36-
37-
CentralPanel::default().show(ctx, |ui| {
38-
let text_height = ui.text_style_height(&TextStyle::Body);
39-
40-
// Get a list of friends who are playing Spacewar!
41-
let mut friend_list = friends.get_friends(FriendFlags::IMMEDIATE);
42-
friend_list.retain(|f| f.game_played().map_or(false, |g| g.game.app_id().0 == 480));
43-
44-
// Show the friend list
45-
SidePanel::left("friends").show_inside(ui, |ui| {
46-
ui.heading(format!("Logged in: {}", friends.name()));
47-
ui.label(format!("Online friends: {}", friend_list.len()));
48-
49-
// Show the list of friends
50-
ScrollArea::both().show_rows(ui, text_height, friend_list.len(), |ui, range| {
51-
for friend in &friend_list[range] {
52-
ui.monospace(friend.name());
53-
}
54-
});
55-
});
56-
57-
// Receive any pending messages
58-
let new_messages = messages.receive_messages_on_channel(0, 10);
59-
for msg in new_messages {
60-
println!("Received message #{:?}", msg.message_number());
61-
62-
let peer = msg.identity_peer();
63-
let data = std::str::from_utf8(msg.data()).expect("Peer sent invalid UTF-8");
64-
65-
message_history.push(format!("{peer:?}: {data}"));
66-
}
67-
68-
// Show message history
69-
ui.heading(format!("Chat history ({} messages)", message_history.len()));
70-
ScrollArea::both().auto_shrink([false, true]).show_rows(
71-
ui,
72-
text_height,
73-
message_history.len(),
74-
|ui, range| {
75-
for msg in &message_history[range] {
76-
ui.label(msg);
77-
}
78-
},
79-
);
80-
81-
// Text box for inputting a message and a button to send it
82-
TopBottomPanel::bottom("textbox").show_inside(ui, |ui| {
83-
ui.horizontal(|ui| {
84-
ui.text_edit_singleline(&mut text_field).request_focus();
85-
86-
// Send message to all friends
87-
if ui.button("Send message").clicked() {
88-
for friend in &friend_list {
89-
println!("Sending to {:?}", friend.id());
90-
91-
if let Err(err) = messages.send_message_to_user(
92-
NetworkingIdentity::new_steam_id(friend.id()),
93-
SendFlags::RELIABLE,
94-
text_field.as_bytes(),
95-
0,
96-
) {
97-
eprintln!("Send error: {err:?}");
98-
}
99-
}
100-
101-
// We can't send message to ourselves, so add it to chat history manually
102-
message_history.push(format!("Me: {text_field}"));
103-
}
104-
});
105-
});
106-
});
107-
})
37+
38+
clear_background(BLACK);
39+
40+
set_camera(&Camera2D::from_display_rect(Rect::new(
41+
-1.0, 1.0, 2.0, -2.0,
42+
)));
43+
44+
// Draw us at our mouse position
45+
let me = mouse_position_local();
46+
draw_circle(me.x, me.y, 0.1, GREEN);
47+
48+
// Send our mouse position to all friends
49+
for friend in friends.get_friends(FriendFlags::IMMEDIATE) {
50+
let identity = NetworkingIdentity::new_steam_id(friend.id());
51+
52+
// Convert our position to bytes
53+
let mut data = [0; 8];
54+
data[0..4].copy_from_slice(&me.x.to_le_bytes());
55+
data[4..8].copy_from_slice(&me.y.to_le_bytes());
56+
57+
let _ =
58+
messages.send_message_to_user(identity, SendFlags::UNRELIABLE_NO_DELAY, &data, 0);
59+
}
60+
61+
// Receive messages from the network
62+
for message in messages.receive_messages_on_channel(0, 100) {
63+
let peer = message.identity_peer();
64+
let data = message.data();
65+
66+
// Convert peer position from bytes
67+
let peer_x =
68+
f32::from_le_bytes(data[0..4].try_into().expect("Someone sent bad message"));
69+
let peer_y =
70+
f32::from_le_bytes(data[4..8].try_into().expect("Someone sent bad message"));
71+
72+
peers.insert(peer.debug_string(), (peer_x, peer_y));
73+
}
74+
75+
// Draw all peers
76+
for peer in peers.values() {
77+
draw_circle(peer.0, peer.1, 0.1, RED);
78+
}
79+
80+
next_frame().await;
81+
}
10882
}

0 commit comments

Comments
 (0)