-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
343 lines (300 loc) · 8.86 KB
/
Copy pathclient.cpp
File metadata and controls
343 lines (300 loc) · 8.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include <SDL2/SDL.h>
#include <cstdio>
#include <ctime>
#include <cstdint>
#include <cassert>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <netdb.h>
#include <vector>
#include "shared.hpp"
bool isRunning = true;
// default
uint32_t MAP_WIDTH = 200;
uint32_t MAP_HEIGHT = 200;
// default
uint32_t SCREEN_WIDTH = 800;
uint32_t SCREEN_HEIGHT = 800;
// pixels per 1 cell
struct {int x = 4; int y = 4;} SCALE;
// server socket
int sock;
// mouse brush
Type brushState = EMPTY;
bool isMousePressed = false;
struct {int x = 0; int y = 0;} mousePos;
// list of updated cells to be sent to server
std::vector<stateId> updatedCells;
// map state received from server
Type inState[MAX_SIZE] = {EMPTY};
// close the program when a pipe has been broken
bool sendRequest(Packet &packet) {
if (send(sock, &packet, sizeof(Packet), MSG_NOSIGNAL) < 0 &&
errno == EPIPE) {
isRunning = false;
return false;
}
else return true;
}
inline stateId getId(int &x, int &y) {
return x + y * MAP_WIDTH;
}
void readPacket(Packet &packet) {
if (packet.opcode == DISPLAY) {
memcpy(inState, packet.payload.map, MAX_SIZE);
} else if (packet.opcode == TERMINATE) {
printf("Server closed connection...\n");
isRunning = false;
} else if (packet.opcode == CONFIGURE) {
if (packet.payload.list.data[0] != 0 &&
packet.payload.list.data[1] != 0) {
MAP_WIDTH = packet.payload.list.data[0];
MAP_HEIGHT = packet.payload.list.data[1];
SCALE.x = SCREEN_WIDTH / MAP_WIDTH;
SCALE.y = SCREEN_HEIGHT / MAP_HEIGHT;
}
}
}
Packet preparePacket(Opcode opcode) {
Packet packet;
packet.opcode = opcode;
if (opcode == UPDATE) {
packet.payload.list.brushType = brushState;
uint32_t size = updatedCells.size() * sizeof(uint32_t);
if (size > MAX_SIZE - 1) {
// saving 4 bytes for a brush
memcpy(packet.payload.list.data, updatedCells.data(), MAX_SIZE - sizeof(stateId));
packet.size = MAX_SIZE;
} else {
memcpy(packet.payload.list.data, updatedCells.data(), size);
packet.size = size;
}
}
return packet;
}
void statePutCell(int &x, int &y) {
if (x >= 0 && y >= 0 &&
x < MAP_WIDTH && y < MAP_HEIGHT) {
updatedCells.push_back(getId(x, y));
}
}
void tryDrawing() {
if (isMousePressed) {
for (int x = mousePos.x - 1; x <= mousePos.x; x++) {
for (int y = mousePos.y - 1; y <= mousePos.y; y++) {
statePutCell(x, y);
}
}
}
}
void handleEvents(SDL_Event *e) {
while (SDL_PollEvent(e) > 0) {
switch(e->type) {
case SDL_QUIT:
isRunning = false;
break;
case SDL_MOUSEBUTTONDOWN:
isMousePressed = true;
case SDL_MOUSEMOTION:
mousePos.x = e->motion.x / SCALE.x;
mousePos.y = e->motion.y / SCALE.y;
tryDrawing();
//printf("Mouse moved to (%d, %d)\n", mousePos.x, mousePos.y);
break;
case SDL_MOUSEBUTTONUP:
isMousePressed = false;
break;
case SDL_KEYDOWN:
//printf("Scancode: 0x%02X\n", e->key.keysym.scancode);
// W KEY
if (e->key.keysym.scancode == 0x1A) {
brushState = WALL;
// S KEY
} else if (e->key.keysym.scancode == 0x16) {
brushState = SAND;
// G KEY
} else if (e->key.keysym.scancode == 0x0A) {
brushState = GAS;
// P KEY - pause
} else if (e->key.keysym.scancode == 0x13) {
Packet packet = preparePacket(PAUSE);
// try to send updates if possible
sendRequest(packet);
// SPACEBAR KEY
} else if (e->key.keysym.scancode == 0x2C) {
Packet packet = preparePacket(CLEAR);
// try to send updates if possible
sendRequest(packet);
} else {
brushState = EMPTY;
}
break;
}
}
}
inline Type *stateGet(int &x, int &y) {
if (x >= MAP_WIDTH || y >= MAP_HEIGHT ||
x < 0 || y < 0) {
return nullptr;
}
return &inState[x + y * (MAP_WIDTH)];
}
void renderMap(SDL_Window *window, SDL_Renderer *renderer) {
SDL_RenderClear(renderer);
for (int y = 0; y < MAP_HEIGHT; y++) {
for (int x = 0; x < MAP_WIDTH; x++) {
// drawing the cell
SDL_Rect rect = {0, 0, 0, 0};
SDL_Colour col;
rect = {x * SCALE.x, y * SCALE.y, SCALE.x, SCALE.y};
Type *cell = stateGet(x, y);
switch (*cell) {
case EMPTY:
col = SDL_Colour{0, 0, 0, 255};
break;
case WALL:
col = SDL_Colour{100, 100, 100, 255};
break;
case SAND:
col = SDL_Colour{255, 255, 50, 255};
break;
case GAS:
col = SDL_Colour{50, 20, 100, 255};
break;
default:
// custom gas colour
col = SDL_Colour{50, 20, 100, 255};
if (*cell % 2) {
col.b += 25 * ((*cell - 3) % 6);
} else {
col.r += 25 * ((*cell - 3) % 8);
}
break;
}
SDL_SetRenderDrawColor(renderer, col.r, col.g, col.b, col.a);
SDL_RenderFillRect(renderer, &rect);
}
}
SDL_RenderPresent(renderer);
}
// RAII
template<typename T, auto Destructor>
struct Scope_Handle {
T* ptr;
Scope_Handle() {
ptr = nullptr;
}
Scope_Handle(Scope_Handle&& rhs) {
this->ptr = nullptr;
*this = move(rhs);
}
~Scope_Handle() {
reset();
}
Scope_Handle& operator=(T* rhs) {
assert(ptr == nullptr);
ptr = rhs;
return *this;
}
Scope_Handle& operator=(Scope_Handle&& rhs) {
assert(ptr == nullptr);
ptr = rhs.ptr;
rhs.ptr = nullptr;
return *this;
}
void reset() {
if (ptr != nullptr) {
Destructor(ptr);
ptr = nullptr;
}
}
operator T*() const { return ptr; }
T* operator->() const { return ptr; }
};
int main(int argc, char* argv[]) {
// checking for size flag
if (argc == 6 && strcmp(sizeFlag, argv[3]) == 0) {
unsigned width = atoi(argv[4]);
unsigned height = atoi(argv[5]);
if (!(width <= 0 || height <= 0) &&
(width >= 400 && height >= 400)) {
SCREEN_WIDTH = width;
SCREEN_HEIGHT = height;
} else {
printf("Both display dimensions must be higher than 400.\n");
printf("Setting to default dimensions 800 x 800\n");
}
} else if (argc != 3) {
printf("Incorrect usage, please input address and port:\n");
printf("%s <address> <port>\n\t", argv[0]);
printf("or: %s <address> <port> %s <width> <height>\n", argv[0], sizeFlag);
exit(1);
}
addrinfo hints {};
hints.ai_family = AF_INET;
hints.ai_protocol = IPPROTO_TCP;
addrinfo *resolved;
if (int err = getaddrinfo(argv[1], argv[2], &hints, &resolved)) {
fprintf(stderr, "Resolving address failed\n");
exit(1);
}
sock = socket(resolved->ai_family, resolved->ai_socktype, resolved->ai_protocol);
if (connect(sock, resolved->ai_addr, resolved->ai_addrlen)) {
perror("Failed to connect");
exit(1);
}
fcntl(sock, F_SETFL, fcntl(sock, F_GETFL) | O_NONBLOCK);
freeaddrinfo(resolved);
Scope_Handle<SDL_Window, SDL_DestroyWindow> window;
Scope_Handle<SDL_Renderer, SDL_DestroyRenderer> renderer;
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "Failed to initialize the SDL2 library\n");
exit(1);
}
window = SDL_CreateWindow(
"gasand",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
SCREEN_WIDTH, SCREEN_HEIGHT,
0
);
if (!window) {
fprintf(stderr, "Failed to create window\n");
exit(1);
}
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (!renderer) {
fprintf(stderr, "Failed to create renderer\n");
exit(1);
}
SDL_Event event;
// packet struct used for communication
Packet packet;
// game loop
while (isRunning) {
// handle events
handleEvents(&event);
tryDrawing();
// try to send updates
if (updatedCells.size() > 0) {
packet = preparePacket(UPDATE);
if (!sendRequest(packet)) {
printf("Server connection lost, the client will now exit...\n");
}
updatedCells.clear();
}
// read a new state
while (read(sock, &packet, sizeof(Packet)) > 0) {
readPacket(packet);
}
renderMap(window, renderer);
}
// close socket
packet = preparePacket(TERMINATE);
sendRequest(packet);
close(sock);
// clean up
SDL_Quit();
return 0;
}