1
- use eframe:: { egui:: * , * } ;
1
+ use std:: collections:: HashMap ;
2
+
3
+ use macroquad:: prelude:: * ;
2
4
use steamworks:: {
3
5
networking_types:: { NetworkingIdentity , SendFlags } ,
4
6
FriendFlags ,
5
7
} ;
6
8
7
- fn main ( ) -> eframe:: Result {
9
+ #[ macroquad:: main( "steamworks-rs" ) ]
10
+ async fn main ( ) {
8
11
// 480 is Spacewar!, the Steamworks SDK example app.
9
12
let client =
10
13
steamworks:: Client :: init_app ( 480 ) . expect ( "Steam is not running or has not been detected" ) ;
@@ -25,84 +28,55 @@ fn main() -> eframe::Result {
25
28
eprintln ! ( "Session failed: {info:#?}" ) ;
26
29
} ) ;
27
30
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 ( ) ;
31
33
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
34
36
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
+ }
108
82
}
0 commit comments