Skip to content

Commit f42ef0e

Browse files
SiegeLordExSiegeLord
authored andcommitted
Tweak the enet examples.
- Allow using arrow keys for control. - Make them compile without C99. - Fix warnings and style issues.
1 parent 9b45404 commit f42ef0e

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

examples/ex_enet_client.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ static ENetPeer* connect_client(ENetHost *client, int port)
6565
enet_address_set_host(&address, "localhost");
6666
address.port = port;
6767
/* Initiate the connection, allocating the two channels 0 and 1. */
68-
server = enet_host_connect(client, & address, 2, 0);
68+
server = enet_host_connect(client, &address, 2, 0);
6969
if (server == NULL)
7070
abort_example("Client: No available peers for initiating an ENet connection.\n");
7171

7272
/* Wait up to 5 seconds for the connection attempt to succeed. */
73-
if (enet_host_service(client, & event, 5000) > 0 &&
73+
if (enet_host_service(client, &event, 5000) > 0 &&
7474
event.type == ENET_EVENT_TYPE_CONNECT)
7575
{
7676
printf("Client: Connected to %x:%u.\n",
@@ -138,6 +138,7 @@ int main(int argc, char **argv)
138138
bool done = false; // when true, client exits
139139
int dx = 0, dy = 0; // movement direction
140140
int port = DEFAULT_PORT;
141+
int i;
141142

142143
if (argc == 2) {
143144
port = atoi(argv[1]);
@@ -186,17 +187,25 @@ int main(int argc, char **argv)
186187
break;
187188
case ALLEGRO_EVENT_KEY_DOWN:
188189
switch (event.keyboard.keycode) {
190+
case ALLEGRO_KEY_UP:
189191
case ALLEGRO_KEY_W: dy -= 1; direction_changed = true; break;
192+
case ALLEGRO_KEY_DOWN:
190193
case ALLEGRO_KEY_S: dy += 1; direction_changed = true; break;
194+
case ALLEGRO_KEY_LEFT:
191195
case ALLEGRO_KEY_A: dx -= 1; direction_changed = true; break;
196+
case ALLEGRO_KEY_RIGHT:
192197
case ALLEGRO_KEY_D: dx += 1; direction_changed = true; break;
193198
}
194199
break;
195200
case ALLEGRO_EVENT_KEY_UP:
196201
switch (event.keyboard.keycode) {
202+
case ALLEGRO_KEY_UP:
197203
case ALLEGRO_KEY_W: dy += 1; direction_changed = true; break;
204+
case ALLEGRO_KEY_DOWN:
198205
case ALLEGRO_KEY_S: dy -= 1; direction_changed = true; break;
206+
case ALLEGRO_KEY_LEFT:
199207
case ALLEGRO_KEY_A: dx += 1; direction_changed = true; break;
208+
case ALLEGRO_KEY_RIGHT:
200209
case ALLEGRO_KEY_D: dx -= 1; direction_changed = true; break;
201210
}
202211
break;
@@ -229,7 +238,7 @@ int main(int argc, char **argv)
229238

230239
// draw each player
231240
al_clear_to_color(al_map_rgb_f(0, 0, 0));
232-
for (int i = 0; i < MAX_PLAYER_COUNT; i++) {
241+
for (i = 0; i < MAX_PLAYER_COUNT; i++) {
233242
if (!players[i].active) continue;
234243

235244
int x = players[i].x;
@@ -244,6 +253,7 @@ int main(int argc, char **argv)
244253
disconnect_client(client, server);
245254
enet_host_destroy(client);
246255
enet_deinitialize();
256+
return 0;
247257
}
248258

249259
/* vim: set sts=3 sw=3 et: */

examples/ex_enet_server.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ static float rand01(void) { return rand() / (float)RAND_MAX; }
2424

2525
static int init_player(void)
2626
{
27+
int i;
2728
// find the first open player slot
28-
for (int i = 0 ; i < MAX_PLAYER_COUNT ; ++i) {
29+
for (i = 0 ; i < MAX_PLAYER_COUNT ; ++i) {
2930
if (!players[i].active) {
3031
// assign a random color and position to this new player
3132
players[i].active = true;
@@ -58,6 +59,7 @@ static void send_receive(ENetHost *server)
5859
ServerMessage msg_out;
5960
ENetPacket *packet;
6061
ENetEvent event;
62+
int i;
6163

6264
// Process all messages in queue, exit as soon as it is empty
6365
while (enet_host_service(server, &event, 0) > 0) {
@@ -91,7 +93,7 @@ static void send_receive(ENetHost *server)
9193
enet_host_broadcast(server, 0, packet);
9294

9395
// notify new client of all other existing players
94-
for (int i = 0 ; i < MAX_PLAYER_COUNT ; ++i) {
96+
for (i = 0 ; i < MAX_PLAYER_COUNT ; ++i) {
9597
if (!players[i].active || i == player_id) continue;
9698

9799
msg_out = create_join_message(i);
@@ -136,7 +138,8 @@ static void send_receive(ENetHost *server)
136138

137139
static void update_players(ENetHost *server, float time)
138140
{
139-
for (int i = 0 ; i < MAX_PLAYER_COUNT ; ++i) {
141+
int i;
142+
for (i = 0 ; i < MAX_PLAYER_COUNT ; ++i) {
140143
if (!players[i].active) continue;
141144

142145
players[i].x += players[i].dx * PLAYER_SPEED * time;
@@ -163,11 +166,11 @@ static ENetHost* create_server(int port)
163166
ENetHost *server;
164167
/* Bind the server to the default localhost. */
165168
/* A specific host address can be specified by */
166-
/* enet_address_set_host (& address, "x.x.x.x"); */
169+
/* enet_address_set_host (&address, "x.x.x.x"); */
167170
address.host = ENET_HOST_ANY;
168171
/* Bind the server to port 1234. */
169172
address.port = port;
170-
server = enet_host_create(& address /* the address to bind the server host to */,
173+
server = enet_host_create(&address /* the address to bind the server host to */,
171174
32 /* allow up to 32 clients and/or outgoing connections */,
172175
2 /* allow up to 2 channels to be used, 0 and 1 */,
173176
0 /* assume any amount of incoming bandwidth */,
@@ -231,6 +234,7 @@ int main(int argc, char **argv)
231234

232235
enet_host_destroy(server);
233236
enet_deinitialize();
237+
return 0;
234238
}
235239

236240
/* vim: set sts=3 sw=3 et: */

0 commit comments

Comments
 (0)