summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorJune McEnroe <programble@gmail.com>2017-07-31 22:14:56 -0400
committerJune McEnroe <programble@gmail.com>2017-07-31 22:14:56 -0400
commit4204d65ff0dd3eaf1e300658d58254ceb27c2c1c (patch)
treebf78feb251fff82d13ab5fc761ca420353307fc7
parentShow other clients' cursors (diff)
downloadtorus-4204d65ff0dd3eaf1e300658d58254ceb27c2c1c.tar.gz
torus-4204d65ff0dd3eaf1e300658d58254ceb27c2c1c.zip
Use designated initializers for messages
I did not realize this syntax was so powerful.
Diffstat (limited to '')
-rwxr-xr-xclient.c14
-rwxr-xr-xhelp.c14
-rwxr-xr-xserver.c77
3 files changed, 62 insertions, 43 deletions
diff --git a/client.c b/client.c
index d77aeb7..74e3a6f 100755
--- a/client.c
+++ b/client.c
@@ -38,16 +38,18 @@ static void clientMessage(const struct ClientMessage *msg) {
 }
 
 static void clientMove(int8_t dx, int8_t dy) {
-    struct ClientMessage msg = { .type = CLIENT_MOVE };
-    msg.data.m.dx = dx;
-    msg.data.m.dy = dy;
+    struct ClientMessage msg = {
+        .type = CLIENT_MOVE,
+        .data.m = { .dx = dx, .dy = dy },
+    };
     clientMessage(&msg);
 }
 
 static void clientPut(uint8_t color, char cell) {
-    struct ClientMessage msg = { .type = CLIENT_PUT };
-    msg.data.p.color = color;
-    msg.data.p.cell = cell;
+    struct ClientMessage msg = {
+        .type = CLIENT_PUT,
+        .data.p = { .color = color, .cell = cell },
+    };
     clientMessage(&msg);
 }
 
diff --git a/help.c b/help.c
index ec4a861..7fc5f42 100755
--- a/help.c
+++ b/help.c
@@ -29,16 +29,18 @@ static void clientMessage(const struct ClientMessage *msg) {
 }
 
 static void clientMove(int8_t dx, int8_t dy) {
-    struct ClientMessage msg = { .type = CLIENT_MOVE };
-    msg.data.m.dx = dx;
-    msg.data.m.dy = dy;
+    struct ClientMessage msg = {
+        .type = CLIENT_MOVE,
+        .data.m = { .dx = dx, .dy = dy },
+    };
     clientMessage(&msg);
 }
 
 static void clientPut(char cell) {
-    struct ClientMessage msg = { .type = CLIENT_PUT };
-    msg.data.p.color = color;
-    msg.data.p.cell = cell;
+    struct ClientMessage msg = {
+        .type = CLIENT_PUT,
+        .data.p = { .color = color, .cell = cell },
+    };
     clientMessage(&msg);
 }
 
diff --git a/server.c b/server.c
index b88c386..a32ada5 100755
--- a/server.c
+++ b/server.c
@@ -116,11 +116,13 @@ static void clientRemove(struct Client *client) {
     if (client->next) client->next->prev = client->prev;
     if (clientHead == client) clientHead = client->next;
 
-    struct ServerMessage msg = { .type = SERVER_CURSOR };
-    msg.data.c.oldCellX = client->cellX;
-    msg.data.c.oldCellY = client->cellY;
-    msg.data.c.newCellX = CURSOR_NONE;
-    msg.data.c.newCellX = CURSOR_NONE;
+    struct ServerMessage msg = {
+        .type = SERVER_CURSOR,
+        .data.c = {
+            .oldCellX = client->cellX, .oldCellY = client->cellY,
+            .newCellX = CURSOR_NONE,   .newCellY = CURSOR_NONE,
+        },
+    };
     clientCast(client, &msg);
 
     close(client->fd);
@@ -128,9 +130,10 @@ static void clientRemove(struct Client *client) {
 }
 
 static bool clientCursors(const struct Client *client) {
-    struct ServerMessage msg = { .type = SERVER_CURSOR };
-    msg.data.c.oldCellX = CURSOR_NONE;
-    msg.data.c.oldCellY = CURSOR_NONE;
+    struct ServerMessage msg = {
+        .type = SERVER_CURSOR,
+        .data.c = { .oldCellX = CURSOR_NONE, .oldCellY = CURSOR_NONE },
+    };
 
     for (struct Client *friend = clientHead; friend; friend = friend->next) {
         if (friend == client) continue;
@@ -165,35 +168,44 @@ static bool clientMove(struct Client *client, int8_t dx, int8_t dy) {
     if (client->tileY == TILE_ROWS)  client->tileY = 0;
     if (client->tileY == UINT32_MAX) client->tileY = TILE_ROWS - 1;
 
-    struct ServerMessage msg = { .type = SERVER_MOVE };
-    msg.data.m.cellX = client->cellX;
-    msg.data.m.cellY = client->cellY;
+    struct ServerMessage msg = {
+        .type = SERVER_MOVE,
+        .data.m = { .cellX = client->cellX, .cellY = client->cellY },
+    };
     if (!clientSend(client, &msg)) return false;
 
     if (client->tileX != old.tileX || client->tileY != old.tileY) {
         msg.type = SERVER_TILE;
         if (!clientSend(client, &msg)) return false;
+
         if (!clientCursors(client)) return false;
 
-        msg.type = SERVER_CURSOR;
-        msg.data.c.oldCellX = old.cellX;
-        msg.data.c.oldCellY = old.cellY;
-        msg.data.c.newCellX = CURSOR_NONE;
-        msg.data.c.newCellX = CURSOR_NONE;
+        msg = (struct ServerMessage) {
+            .type = SERVER_CURSOR,
+            .data.c = {
+                .oldCellX = old.cellX,   .oldCellY = old.cellY,
+                .newCellX = CURSOR_NONE, .newCellY = CURSOR_NONE,
+            },
+        };
         clientCast(&old, &msg);
 
-        msg.data.c.oldCellX = CURSOR_NONE;
-        msg.data.c.oldCellY = CURSOR_NONE;
-        msg.data.c.newCellX = client->cellX;
-        msg.data.c.newCellY = client->cellY;
+        msg = (struct ServerMessage) {
+            .type = SERVER_CURSOR,
+            .data.c = {
+                .oldCellX = CURSOR_NONE,   .oldCellY = CURSOR_NONE,
+                .newCellX = client->cellX, .newCellY = client->cellY,
+            },
+        };
         clientCast(client, &msg);
 
     } else {
-        msg.type = SERVER_CURSOR;
-        msg.data.c.oldCellX = old.cellX;
-        msg.data.c.oldCellY = old.cellY;
-        msg.data.c.newCellX = client->cellX;
-        msg.data.c.newCellY = client->cellY;
+        msg = (struct ServerMessage) {
+            .type = SERVER_CURSOR,
+            .data.c = {
+                .oldCellX = old.cellX,     .oldCellY = old.cellY,
+                .newCellX = client->cellX, .newCellY = client->cellY,
+            },
+        };
         clientCast(client, &msg);
     }
 
@@ -205,12 +217,15 @@ static bool clientPut(const struct Client *client, uint8_t color, char cell) {
     tile->colors[client->cellY][client->cellX] = color;
     tile->cells[client->cellY][client->cellX] = cell;
 
-    struct ServerMessage msg = { .type = SERVER_PUT };
-    msg.data.p.cellX = client->cellX;
-    msg.data.p.cellY = client->cellY;
-    msg.data.p.color = color;
-    msg.data.p.cell = cell;
-
+    struct ServerMessage msg = {
+        .type = SERVER_PUT,
+        .data.p = {
+            .cellX = client->cellX,
+            .cellY = client->cellY,
+            .color = color,
+            .cell = cell,
+        },
+    };
     bool success = clientSend(client, &msg);
     clientCast(client, &msg);
     return success;