390a04e4be7719e9c206a4da5ce63ca40e87d293
2 #include <SDL2/SDL_net.h>
11 static IPaddress addr
;
12 static UDPsocket sock
;
13 static char nickname
[32] = "Anonymous";
15 static Uint32 lastTime
;
16 static Uint32 currTime
;
19 static bool does_updated
;
21 static void cl_kill_client(ProtocolMessage m
) {
22 SDL_Log("Connection refused by server: %.*s\n", (int) sizeof(m
.sv
.kill
.message
), m
.sv
.kill
.message
);
26 static void cl_update_svinfo(ProtocolMessage m
) {
27 SDL_Log("Connected to server %.*s\n", (int) sizeof(m
.sv
.info
.name
), m
.sv
.info
.name
);
28 cl_playerid
= m
.sv
.info
.clientid
;
29 assert(cl_playerid
< MAX_PLAYERS
);
30 SDL_Log("Player id%i\n", cl_playerid
);
33 static void cl_update_svplayer(ProtocolMessage m
) {
47 static void cl_update_svbullet(ProtocolMessage m
) {
60 static void cl_recv() {
63 if(!RecvMessage(sock
, &address
, &m
))
67 case SV_INFO
: cl_update_svinfo(m
); break;
68 case SV_KILL
: cl_kill_client(m
); break;
69 case SV_SPLR
: cl_update_svplayer(m
); break;
70 case SV_SBUL
: cl_update_svbullet(m
); break;
71 default: SDL_Log("invalid message %i", m
.type
);
75 static void cl_send() {
77 SendMessage(sock
, addr
, cl_does(does
));
84 void cl_move(DoesBits code
) {
86 does
.bits
|= code
.bits
;
89 void cl_connect(const char * host
, uint16_t port
) {
90 if(SDLNet_ResolveHost(&addr
, host
, (port
) ? (port
) : (PROTOCOL_PORT
)) != 0) {
91 SDL_Log("Unable to resolve host: %s\n", SDLNet_GetError());
96 SendMessage(sock
, addr
, cl_info(nickname
));
99 bool received
= WaitMessage(sock
, NULL
, &m
, 10000);
101 SDL_Log("Connection timeout");
105 if(m
.type
== SV_KILL
) {
107 } else if(m
.type
== SV_INFO
) {
110 SDL_Log("Invalid first message %i", m
.type
);
118 void cl_disconnect(bool force
) {
123 SendMessage(sock
, addr
, cl_kill());
133 if(currTime
- lastTime
>= TICK_DELAY
) {
135 g_player_does(cl_playerid
, does
);
141 lastTime
= SDL_GetTicks();
143 currTime
= SDL_GetTicks();