source: network-game/client/Client/main.cpp@ ad5d122

Last change on this file since ad5d122 was ad5d122, checked in by dportnoy <dmp1488@…>, 12 years ago

Fixed a bug with Player serialization

  • Property mode set to 100644
File size: 13.5 KB
Line 
1#include "../../common/Compiler.h"
2
3#if defined WINDOWS
4 #include <winsock2.h>
5 #include <WS2tcpip.h>
6#elif defined LINUX
7 #include <sys/types.h>
8 #include <unistd.h>
9 #include <sys/socket.h>
10 #include <netinet/in.h>
11 #include <netdb.h>
12 #include <cstring>
13#endif
14
15#include <sys/types.h>
16#include <cstdio>
17#include <cstdlib>
18#include <string>
19#include <iostream>
20#include <sstream>
21
22#include <map>
23
24#include <allegro5/allegro.h>
25#include <allegro5/allegro_font.h>
26#include <allegro5/allegro_ttf.h>
27#include <allegro5/allegro_primitives.h>
28
29#include "../../common/Message.h"
30#include "../../common/Common.h"
31#include "../../common/Player.h"_
32
33#include "Window.h"
34#include "Textbox.h"
35#include "Button.h"
36#include "chat.h"
37
38#ifdef WINDOWS
39 #pragma comment(lib, "ws2_32.lib")
40#endif
41
42using namespace std;
43
44void initWinSock();
45void shutdownWinSock();
46void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, map<unsigned int, Player>& mapPlayers, unsigned int& curPlayerId);
47void drawPlayers(map<unsigned int, Player>& mapPlayers, unsigned int curPlayerId);
48
49// callbacks
50void registerAccount();
51void login();
52void logout();
53void quit();
54void sendChatMessage();
55
56void error(const char *);
57
58const float FPS = 60;
59const int SCREEN_W = 640;
60const int SCREEN_H = 480;
61const int BOUNCER_SIZE = 32;
62enum MYKEYS {
63 KEY_UP,
64 KEY_DOWN,
65 KEY_LEFT,
66 KEY_RIGHT
67};
68
69enum STATE {
70 STATE_START,
71 STATE_LOGIN // this means you're already logged in
72};
73
74int state;
75
76bool doexit;
77
78Window* wndLogin;
79Window* wndMain;
80Window* wndCurrent;
81
82Textbox* txtUsername;
83Textbox* txtPassword;
84Textbox* txtChat;
85
86int sock;
87struct sockaddr_in server, from;
88struct hostent *hp;
89NETWORK_MSG msgTo, msgFrom;
90string username;
91chat chatConsole;
92
93int main(int argc, char **argv)
94{
95 ALLEGRO_DISPLAY *display = NULL;
96 ALLEGRO_EVENT_QUEUE *event_queue = NULL;
97 ALLEGRO_TIMER *timer = NULL;
98 ALLEGRO_BITMAP *bouncer = NULL;
99 bool key[4] = { false, false, false, false };
100 bool redraw = true;
101 doexit = false;
102 map<unsigned int, Player> mapPlayers;
103 unsigned int curPlayerId = -1;
104
105 float bouncer_x = SCREEN_W / 2.0 - BOUNCER_SIZE / 2.0;
106 float bouncer_y = SCREEN_H / 2.0 - BOUNCER_SIZE / 2.0;
107
108 state = STATE_START;
109
110 if(!al_init()) {
111 fprintf(stderr, "failed to initialize allegro!\n");
112 return -1;
113 }
114
115 if (al_init_primitives_addon())
116 cout << "Primitives initialized" << endl;
117 else
118 cout << "Primitives not initialized" << endl;
119
120 al_init_font_addon();
121 al_init_ttf_addon();
122
123 #if defined WINDOWS
124 ALLEGRO_FONT *font = al_load_ttf_font("../pirulen.ttf", 12, 0);
125 #elif defined LINUX
126 ALLEGRO_FONT *font = al_load_ttf_font("pirulen.ttf", 12, 0);
127 #endif
128
129 if (!font) {
130 fprintf(stderr, "Could not load 'pirulen.ttf'.\n");
131 getchar();
132 return -1;
133 }
134
135 if(!al_install_keyboard()) {
136 fprintf(stderr, "failed to initialize the keyboard!\n");
137 return -1;
138 }
139
140 if(!al_install_mouse()) {
141 fprintf(stderr, "failed to initialize the mouse!\n");
142 return -1;
143 }
144
145 timer = al_create_timer(1.0 / FPS);
146 if(!timer) {
147 fprintf(stderr, "failed to create timer!\n");
148 return -1;
149 }
150
151 display = al_create_display(SCREEN_W, SCREEN_H);
152 if(!display) {
153 fprintf(stderr, "failed to create display!\n");
154 al_destroy_timer(timer);
155 return -1;
156 }
157
158 wndLogin = new Window(0, 0, SCREEN_W, SCREEN_H);
159 wndLogin->addComponent(new Textbox(104, 40, 100, 20, font));
160 wndLogin->addComponent(new Textbox(104, 70, 100, 20, font));
161 wndLogin->addComponent(new Button(22, 100, 90, 20, font, "Register", registerAccount));
162 wndLogin->addComponent(new Button(122, 100, 60, 20, font, "Login", login));
163 wndLogin->addComponent(new Button(540, 10, 80, 20, font, "Quit", quit));
164
165 txtUsername = (Textbox*)wndLogin->getComponent(0);
166 txtPassword = (Textbox*)wndLogin->getComponent(1);
167
168 wndMain = new Window(0, 0, SCREEN_W, SCREEN_H);
169 wndMain->addComponent(new Textbox(95, 40, 525, 20, font));
170 wndMain->addComponent(new Button(95, 70, 160, 20, font, "Send Message", sendChatMessage));
171 wndMain->addComponent(new Button(540, 10, 80, 20, font, "Logout", logout));
172
173 txtChat = (Textbox*)wndMain->getComponent(0);
174
175 wndCurrent = wndLogin;
176
177 bouncer = al_create_bitmap(BOUNCER_SIZE, BOUNCER_SIZE);
178 if(!bouncer) {
179 fprintf(stderr, "failed to create bouncer bitmap!\n");
180 al_destroy_display(display);
181 al_destroy_timer(timer);
182 return -1;
183 }
184
185 event_queue = al_create_event_queue();
186 if(!event_queue) {
187 fprintf(stderr, "failed to create event_queue!\n");
188 al_destroy_bitmap(bouncer);
189 al_destroy_display(display);
190 al_destroy_timer(timer);
191 return -1;
192 }
193
194 al_set_target_bitmap(bouncer);
195
196 al_clear_to_color(al_map_rgb(255, 0, 255));
197
198 al_set_target_bitmap(al_get_backbuffer(display));
199
200 al_register_event_source(event_queue, al_get_display_event_source(display));
201 al_register_event_source(event_queue, al_get_timer_event_source(timer));
202 al_register_event_source(event_queue, al_get_keyboard_event_source());
203 al_register_event_source(event_queue, al_get_mouse_event_source());
204
205 al_clear_to_color(al_map_rgb(0,0,0));
206
207 al_flip_display();
208
209 if (argc != 3) {
210 cout << "Usage: server port" << endl;
211 exit(1);
212 }
213
214 initWinSock();
215
216 sock = socket(AF_INET, SOCK_DGRAM, 0);
217 if (sock < 0)
218 error("socket");
219
220 set_nonblock(sock);
221
222 server.sin_family = AF_INET;
223 hp = gethostbyname(argv[1]);
224 if (hp==0)
225 error("Unknown host");
226
227 memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length);
228 server.sin_port = htons(atoi(argv[2]));
229
230 al_start_timer(timer);
231
232 while(!doexit)
233 {
234 ALLEGRO_EVENT ev;
235 al_wait_for_event(event_queue, &ev);
236
237 if(wndCurrent->handleEvent(ev)) {
238 // do nothing
239 }
240 else if(ev.type == ALLEGRO_EVENT_TIMER) {
241 if(key[KEY_UP] && bouncer_y >= 4.0) {
242 bouncer_y -= 4.0;
243 }
244
245 if(key[KEY_DOWN] && bouncer_y <= SCREEN_H - BOUNCER_SIZE - 4.0) {
246 bouncer_y += 4.0;
247 }
248
249 if(key[KEY_LEFT] && bouncer_x >= 4.0) {
250 bouncer_x -= 4.0;
251 }
252
253 if(key[KEY_RIGHT] && bouncer_x <= SCREEN_W - BOUNCER_SIZE - 4.0) {
254 bouncer_x += 4.0;
255 }
256
257 redraw = true;
258 }
259 else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
260 doexit = true;
261 }
262 else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) {
263 switch(ev.keyboard.keycode) {
264 case ALLEGRO_KEY_UP:
265 key[KEY_UP] = true;
266 break;
267
268 case ALLEGRO_KEY_DOWN:
269 key[KEY_DOWN] = true;
270 break;
271
272 case ALLEGRO_KEY_LEFT:
273 key[KEY_LEFT] = true;
274 break;
275
276 case ALLEGRO_KEY_RIGHT:
277 key[KEY_RIGHT] = true;
278 break;
279 }
280 }
281 else if(ev.type == ALLEGRO_EVENT_KEY_UP) {
282 switch(ev.keyboard.keycode) {
283 case ALLEGRO_KEY_UP:
284 key[KEY_UP] = false;
285 break;
286
287 case ALLEGRO_KEY_DOWN:
288 key[KEY_DOWN] = false;
289 break;
290
291 case ALLEGRO_KEY_LEFT:
292 key[KEY_LEFT] = false;
293 break;
294
295 case ALLEGRO_KEY_RIGHT:
296 key[KEY_RIGHT] = false;
297 break;
298
299 case ALLEGRO_KEY_ESCAPE:
300 doexit = true;
301 break;
302 }
303 }
304 else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
305 if(wndCurrent == wndMain) {
306 msgTo.type = MSG_TYPE_PLAYER_MOVE;
307
308 memcpy(msgTo.buffer, &curPlayerId, 4);
309 memcpy(msgTo.buffer+4, &ev.mouse.x, 4);
310 memcpy(msgTo.buffer+8, &ev.mouse.y, 4);
311
312 sendMessage(&msgTo, sock, &server);
313 }
314 }
315
316 if (receiveMessage(&msgFrom, sock, &from) >= 0)
317 {
318 processMessage(msgFrom, state, chatConsole, mapPlayers, curPlayerId);
319 cout << "state: " << state << endl;
320 }
321
322 if (redraw && al_is_event_queue_empty(event_queue))
323 {
324 redraw = false;
325
326 wndCurrent->draw(display);
327
328 drawPlayers(mapPlayers, curPlayerId);
329
330 chatConsole.draw(font, al_map_rgb(255,255,255));
331
332 if(wndCurrent == wndLogin) {
333 al_draw_text(font, al_map_rgb(0, 255, 0), 4, 43, ALLEGRO_ALIGN_LEFT, "Username:");
334 al_draw_text(font, al_map_rgb(0, 255, 0), 1, 73, ALLEGRO_ALIGN_LEFT, "Password:");
335 }
336 else if(wndCurrent == wndMain) {
337 al_draw_text(font, al_map_rgb(0, 255, 0), 4, 43, ALLEGRO_ALIGN_LEFT, "Message:");
338 }
339
340 al_flip_display();
341 }
342 }
343
344 #if defined WINDOWS
345 closesocket(sock);
346 #elif defined LINUX
347 close(sock);
348 #endif
349
350 shutdownWinSock();
351
352 delete wndLogin;
353 delete wndMain;
354
355 al_destroy_event_queue(event_queue);
356 al_destroy_bitmap(bouncer);
357 al_destroy_display(display);
358 al_destroy_timer(timer);
359
360 return 0;
361}
362
363// need to make a function like this that works on windows
364void error(const char *msg)
365{
366 perror(msg);
367 shutdownWinSock();
368 exit(1);
369}
370
371void initWinSock()
372{
373#if defined WINDOWS
374 WORD wVersionRequested;
375 WSADATA wsaData;
376 int wsaerr;
377
378 wVersionRequested = MAKEWORD(2, 2);
379 wsaerr = WSAStartup(wVersionRequested, &wsaData);
380
381 if (wsaerr != 0) {
382 cout << "The Winsock dll not found." << endl;
383 exit(1);
384 }else
385 cout << "The Winsock dll was found." << endl;
386#endif
387}
388
389void shutdownWinSock()
390{
391#if defined WINDOWS
392 WSACleanup();
393#endif
394}
395
396void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, map<unsigned int, Player>& mapPlayers, unsigned int& curPlayerId)
397{
398 string response = string(msg.buffer);
399
400 cout << "Got message: " << msg.type << endl;
401
402 switch(state)
403 {
404 case STATE_START:
405 {
406 cout << "In STATE_START" << endl;
407
408 switch(msg.type)
409 {
410 case MSG_TYPE_REGISTER:
411 {
412 break;
413 }
414 case MSG_TYPE_LOGIN:
415 {
416 if (response.compare("Player has already logged in.") == 0)
417 {
418 username.clear();
419 cout << "User login failed" << endl;
420 }
421 else if (response.compare("Incorrect username or password") == 0)
422 {
423 username.clear();
424 cout << "User login failed" << endl;
425 }
426 else
427 {
428 state = STATE_LOGIN;
429 wndCurrent = wndMain;
430
431 Player p("", "");
432 p.deserialize(msg.buffer);
433 mapPlayers[p.id] = p;
434 curPlayerId = p.id;
435
436 cout << "Got a valid login response with the player" << endl;
437 cout << "Player id: " << curPlayerId << endl;
438 }
439
440 break;
441 }
442 }
443
444 break;
445 }
446 case STATE_LOGIN:
447 {
448 switch(msg.type)
449 {
450 case MSG_TYPE_REGISTER:
451 {
452 break;
453 }
454 case MSG_TYPE_LOGIN:
455 {
456 chatConsole.addLine(response);
457
458 if (response.compare("You have successfully logged out.") == 0)
459 {
460 cout << "Logged out" << endl;
461 state = STATE_START;
462 wndCurrent = wndLogin;
463 }
464 else
465 {
466 cout << "Added new line" << endl;
467 }
468
469 break;
470 }
471 case MSG_TYPE_PLAYER:
472 {
473 Player p("", "");
474 p.deserialize(msg.buffer);
475 mapPlayers[p.id] = p;
476
477 cout << "Received MSG_TYPE_PLAYER message" << endl;
478
479 break;
480 }
481 case MSG_TYPE_CHAT:
482 {
483 chatConsole.addLine(response);
484
485 break;
486 }
487 }
488
489 break;
490 }
491 default:
492 {
493 cout << "The state has an invalid value: " << state << endl;
494
495 break;
496 }
497 }
498}
499
500void drawPlayers(map<unsigned int, Player>& mapPlayers, unsigned int curPlayerId)
501{
502 map<unsigned int, Player>::iterator it;
503
504 for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
505 {
506 Player *p = &it->second;
507
508 if (p->id == curPlayerId)
509 al_draw_filled_circle(p->pos.x, p->pos.y, 15, al_map_rgb(0, 255, 0));
510 else
511 al_draw_filled_circle(p->pos.x, p->pos.y, 30, al_map_rgb(255, 0, 0));
512 }
513}
514
515void registerAccount()
516{
517 string username = txtUsername->getStr();
518 string password = txtPassword->getStr();
519
520 txtUsername->clear();
521 txtPassword->clear();
522
523 msgTo.type = MSG_TYPE_REGISTER;
524
525 strcpy(msgTo.buffer, username.c_str());
526 strcpy(msgTo.buffer+username.size()+1, password.c_str());
527
528 sendMessage(&msgTo, sock, &server);
529}
530
531void login()
532{
533 string strUsername = txtUsername->getStr();
534 string strPassword = txtPassword->getStr();
535 username = strUsername;
536
537 txtUsername->clear();
538 txtPassword->clear();
539
540 msgTo.type = MSG_TYPE_LOGIN;
541
542 strcpy(msgTo.buffer, strUsername.c_str());
543 strcpy(msgTo.buffer+username.size()+1, strPassword.c_str());
544
545 sendMessage(&msgTo, sock, &server);
546}
547
548void logout()
549{
550 txtChat->clear();
551
552 msgTo.type = MSG_TYPE_LOGOUT;
553
554 strcpy(msgTo.buffer, username.c_str());
555
556 sendMessage(&msgTo, sock, &server);
557}
558
559void quit()
560{
561 doexit = true;
562}
563
564void sendChatMessage()
565{
566 string msg = txtChat->getStr();
567
568 txtChat->clear();
569
570 msgTo.type = MSG_TYPE_CHAT;
571
572 strcpy(msgTo.buffer, msg.c_str());
573
574 sendMessage(&msgTo, sock, &server);
575}
Note: See TracBrowser for help on using the repository browser.