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

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

Minor changes

  • Property mode set to 100644
File size: 22.2 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 <map>
25
26#include <allegro5/allegro.h>
27#include <allegro5/allegro_font.h>
28#include <allegro5/allegro_ttf.h>
29#include <allegro5/allegro_primitives.h>
30
31#include "../../common/Message.h"
32#include "../../common/Common.h"
33#include "../../common/WorldMap.h"
34#include "../../common/Player.h"
35
36#include "Window.h"
37#include "Textbox.h"
38#include "Button.h"
39#include "chat.h"
40
41#ifdef WINDOWS
42 #pragma comment(lib, "ws2_32.lib")
43#endif
44
45using namespace std;
46
47void initWinSock();
48void shutdownWinSock();
49void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, WorldMap *gameMap, map<unsigned int, Player>& mapPlayers, unsigned int& curPlayerId, int &scoreBlue, int &scoreRed);
50void drawMap(WorldMap* gameMap);
51void drawPlayers(map<unsigned int, Player>& mapPlayers, ALLEGRO_FONT* font, unsigned int curPlayerId);
52POSITION screenToMap(POSITION pos);
53POSITION mapToScreen(POSITION pos);
54
55// callbacks
56void registerAccount();
57void login();
58void logout();
59void quit();
60void sendChatMessage();
61
62void error(const char *);
63
64const float FPS = 60;
65const int SCREEN_W = 640;
66const int SCREEN_H = 480;
67
68enum STATE {
69 STATE_START,
70 STATE_LOGIN // this means you're already logged in
71};
72
73int state;
74
75bool doexit;
76
77Window* wndLogin;
78Window* wndMain;
79Window* wndCurrent;
80
81Textbox* txtUsername;
82Textbox* txtPassword;
83Textbox* txtChat;
84
85int sock;
86struct sockaddr_in server, from;
87struct hostent *hp;
88NETWORK_MSG msgTo, msgFrom;
89string username;
90chat chatConsole;
91
92int main(int argc, char **argv)
93{
94 ALLEGRO_DISPLAY *display = NULL;
95 ALLEGRO_EVENT_QUEUE *event_queue = NULL;
96 ALLEGRO_TIMER *timer = NULL;
97 bool key[4] = { false, false, false, false };
98 bool redraw = true;
99 doexit = false;
100 map<unsigned int, Player> mapPlayers;
101 unsigned int curPlayerId = -1;
102 int scoreBlue, scoreRed;
103
104 scoreBlue = 0;
105 scoreRed = 0;
106
107 state = STATE_START;
108
109 if(!al_init()) {
110 fprintf(stderr, "failed to initialize allegro!\n");
111 return -1;
112 }
113
114 if (al_init_primitives_addon())
115 cout << "Primitives initialized" << endl;
116 else
117 cout << "Primitives not initialized" << endl;
118
119 al_init_font_addon();
120 al_init_ttf_addon();
121
122 #if defined WINDOWS
123 ALLEGRO_FONT *font = al_load_ttf_font("../pirulen.ttf", 12, 0);
124 #elif defined LINUX
125 ALLEGRO_FONT *font = al_load_ttf_font("pirulen.ttf", 12, 0);
126 #endif
127
128 if (!font) {
129 fprintf(stderr, "Could not load 'pirulen.ttf'.\n");
130 getchar();
131 return -1;
132 }
133
134 if(!al_install_keyboard()) {
135 fprintf(stderr, "failed to initialize the keyboard!\n");
136 return -1;
137 }
138
139 if(!al_install_mouse()) {
140 fprintf(stderr, "failed to initialize the mouse!\n");
141 return -1;
142 }
143
144 timer = al_create_timer(1.0 / FPS);
145 if(!timer) {
146 fprintf(stderr, "failed to create timer!\n");
147 return -1;
148 }
149
150 display = al_create_display(SCREEN_W, SCREEN_H);
151 if(!display) {
152 fprintf(stderr, "failed to create display!\n");
153 al_destroy_timer(timer);
154 return -1;
155 }
156
157 WorldMap* gameMap = WorldMap::loadMapFromFile("../../data/map.txt");
158
159 wndLogin = new Window(0, 0, SCREEN_W, SCREEN_H);
160 wndLogin->addComponent(new Textbox(104, 40, 100, 20, font));
161 wndLogin->addComponent(new Textbox(104, 70, 100, 20, font));
162 wndLogin->addComponent(new Button(22, 100, 90, 20, font, "Register", registerAccount));
163 wndLogin->addComponent(new Button(122, 100, 60, 20, font, "Login", login));
164 wndLogin->addComponent(new Button(540, 10, 80, 20, font, "Quit", quit));
165
166 txtUsername = (Textbox*)wndLogin->getComponent(0);
167 txtPassword = (Textbox*)wndLogin->getComponent(1);
168
169 wndMain = new Window(0, 0, SCREEN_W, SCREEN_H);
170 wndMain->addComponent(new Textbox(95, 40, 525, 20, font));
171 wndMain->addComponent(new Button(95, 70, 160, 20, font, "Send Message", sendChatMessage));
172 wndMain->addComponent(new Button(540, 10, 80, 20, font, "Logout", logout));
173
174 txtChat = (Textbox*)wndMain->getComponent(0);
175
176 wndCurrent = wndLogin;
177
178 event_queue = al_create_event_queue();
179 if(!event_queue) {
180 fprintf(stderr, "failed to create event_queue!\n");
181 al_destroy_display(display);
182 al_destroy_timer(timer);
183 return -1;
184 }
185
186 al_set_target_bitmap(al_get_backbuffer(display));
187
188 al_register_event_source(event_queue, al_get_display_event_source(display));
189 al_register_event_source(event_queue, al_get_timer_event_source(timer));
190 al_register_event_source(event_queue, al_get_keyboard_event_source());
191 al_register_event_source(event_queue, al_get_mouse_event_source());
192
193 al_clear_to_color(al_map_rgb(0,0,0));
194
195 al_flip_display();
196
197 if (argc != 3) {
198 cout << "Usage: server port" << endl;
199 exit(1);
200 }
201
202 initWinSock();
203
204 sock = socket(AF_INET, SOCK_DGRAM, 0);
205 if (sock < 0)
206 error("socket");
207
208 set_nonblock(sock);
209
210 server.sin_family = AF_INET;
211 hp = gethostbyname(argv[1]);
212 if (hp==0)
213 error("Unknown host");
214
215 memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length);
216 server.sin_port = htons(atoi(argv[2]));
217
218 al_start_timer(timer);
219
220 while(!doexit)
221 {
222 ALLEGRO_EVENT ev;
223
224 al_wait_for_event(event_queue, &ev);
225
226 if(wndCurrent->handleEvent(ev)) {
227 // do nothing
228 }
229 else if(ev.type == ALLEGRO_EVENT_TIMER) {
230 redraw = true; // seems like we should just call a draw function here instead
231 }
232 else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
233 doexit = true;
234 }
235 else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) {
236 }
237 else if(ev.type == ALLEGRO_EVENT_KEY_UP) {
238 switch(ev.keyboard.keycode) {
239 case ALLEGRO_KEY_ESCAPE:
240 doexit = true;
241 break;
242 case ALLEGRO_KEY_S: // pickup an item next to you
243 if (state == STATE_LOGIN) {
244 msgTo.type = MSG_TYPE_PICKUP_FLAG;
245 memcpy(msgTo.buffer, &curPlayerId, 4);
246 sendMessage(&msgTo, sock, &server);
247 }
248 break;
249 case ALLEGRO_KEY_D: // drop the current item
250 if (state == STATE_LOGIN) {
251 // find the current player in the player list
252 map<unsigned int, Player>::iterator it;
253 Player* p = NULL;
254 for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
255 {
256 if (it->second.id == curPlayerId)
257 p = &it->second;
258 }
259
260 if (p != NULL) {
261 int flagType = WorldMap::OBJECT_NONE;
262
263 if (p->hasBlueFlag)
264 flagType = WorldMap::OBJECT_BLUE_FLAG;
265 else if (p->hasRedFlag)
266 flagType = WorldMap::OBJECT_RED_FLAG;
267
268 if (flagType != WorldMap::OBJECT_NONE) {
269 msgTo.type = MSG_TYPE_DROP_FLAG;
270 memcpy(msgTo.buffer, &curPlayerId, 4);
271 sendMessage(&msgTo, sock, &server);
272 }
273 }
274 }
275 break;
276 }
277 }
278 else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
279 if(wndCurrent == wndMain) {
280 if (ev.mouse.button == 1) { // left click
281 msgTo.type = MSG_TYPE_PLAYER_MOVE;
282
283 POSITION pos;
284 pos.x = ev.mouse.x;
285 pos.y = ev.mouse.y;
286 pos = screenToMap(pos);
287
288 if (pos.x != -1)
289 {
290 memcpy(msgTo.buffer, &curPlayerId, 4);
291 memcpy(msgTo.buffer+4, &pos.x, 4);
292 memcpy(msgTo.buffer+8, &pos.y, 4);
293
294 sendMessage(&msgTo, sock, &server);
295 }
296 else
297 cout << "Invalid point: User did not click on the map" << endl;
298 }else if (ev.mouse.button == 2) { // right click
299 map<unsigned int, Player>::iterator it;
300
301 Player* curPlayer;
302 for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
303 {
304 if (it->second.id == curPlayerId)
305 curPlayer = &it->second;
306 }
307
308 Player* target;
309 for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
310 {
311 target = &it->second;
312 if (target->id != curPlayerId && target->team != curPlayer->team) {
313 msgTo.type = MSG_TYPE_START_ATTACK;
314 memcpy(msgTo.buffer, &curPlayerId, 4);
315 memcpy(msgTo.buffer+4, &target->id, 4);
316
317 sendMessage(&msgTo, sock, &server);
318 }
319 }
320 }
321 }
322 }
323
324 if (receiveMessage(&msgFrom, sock, &from) >= 0)
325 processMessage(msgFrom, state, chatConsole, gameMap, mapPlayers, curPlayerId, scoreBlue, scoreRed);
326
327 if (redraw)
328 {
329 redraw = false;
330
331 wndCurrent->draw(display);
332
333 chatConsole.draw(font, al_map_rgb(255,255,255));
334
335 if(wndCurrent == wndLogin) {
336 al_draw_text(font, al_map_rgb(0, 255, 0), 4, 43, ALLEGRO_ALIGN_LEFT, "Username:");
337 al_draw_text(font, al_map_rgb(0, 255, 0), 1, 73, ALLEGRO_ALIGN_LEFT, "Password:");
338 }
339 else if(wndCurrent == wndMain) {
340 al_draw_text(font, al_map_rgb(0, 255, 0), 4, 43, ALLEGRO_ALIGN_LEFT, "Message:");
341
342 ostringstream ossScoreBlue, ossScoreRed;
343
344 ossScoreBlue << "Blue: " << scoreBlue << endl;
345 ossScoreRed << "Red: " << scoreRed << endl;
346
347 al_draw_text(font, al_map_rgb(0, 255, 0), 330, 80, ALLEGRO_ALIGN_LEFT, ossScoreBlue.str().c_str());
348 al_draw_text(font, al_map_rgb(0, 255, 0), 515, 80, ALLEGRO_ALIGN_LEFT, ossScoreRed.str().c_str());
349
350 // update player positions
351 map<unsigned int, Player>::iterator it;
352 for (it = mapPlayers.begin(); it != mapPlayers.end(); it++)
353 {
354 it->second.move(gameMap); // ignore return value
355 }
356
357 drawMap(gameMap);
358 drawPlayers(mapPlayers, font, curPlayerId);
359 }
360
361 al_flip_display();
362 }
363 }
364
365 #if defined WINDOWS
366 closesocket(sock);
367 #elif defined LINUX
368 close(sock);
369 #endif
370
371 shutdownWinSock();
372
373 delete wndLogin;
374 delete wndMain;
375
376 delete gameMap;
377
378 al_destroy_event_queue(event_queue);
379 al_destroy_display(display);
380 al_destroy_timer(timer);
381
382 return 0;
383}
384
385// need to make a function like this that works on windows
386void error(const char *msg)
387{
388 perror(msg);
389 shutdownWinSock();
390 exit(1);
391}
392
393void initWinSock()
394{
395#if defined WINDOWS
396 WORD wVersionRequested;
397 WSADATA wsaData;
398 int wsaerr;
399
400 wVersionRequested = MAKEWORD(2, 2);
401 wsaerr = WSAStartup(wVersionRequested, &wsaData);
402
403 if (wsaerr != 0) {
404 cout << "The Winsock dll not found." << endl;
405 exit(1);
406 }else
407 cout << "The Winsock dll was found." << endl;
408#endif
409}
410
411void shutdownWinSock()
412{
413#if defined WINDOWS
414 WSACleanup();
415#endif
416}
417
418POSITION screenToMap(POSITION pos)
419{
420 pos.x = pos.x-300;
421 pos.y = pos.y-100;
422
423 if (pos.x < 0 || pos.y < 0)
424 {
425 pos.x = -1;
426 pos.y = -1;
427 }
428
429 return pos;
430}
431
432POSITION mapToScreen(POSITION pos)
433{
434 pos.x = pos.x+300;
435 pos.y = pos.y+100;
436
437 return pos;
438}
439
440POSITION mapToScreen(FLOAT_POSITION pos)
441{
442 POSITION p;
443 p.x = pos.x+300;
444 p.y = pos.y+100;
445
446 return p;
447}
448
449void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, WorldMap *gameMap, map<unsigned int, Player>& mapPlayers, unsigned int& curPlayerId, int &scoreBlue, int &scoreRed)
450{
451 string response = string(msg.buffer);
452
453 cout << "Processing message" << endl;
454
455 switch(state)
456 {
457 case STATE_START:
458 {
459 cout << "In STATE_START" << endl;
460
461 switch(msg.type)
462 {
463 case MSG_TYPE_REGISTER:
464 {
465 break;
466 }
467 case MSG_TYPE_LOGIN:
468 {
469 if (response.compare("Player has already logged in.") == 0)
470 {
471 username.clear();
472 cout << "User login failed" << endl;
473 }
474 else if (response.compare("Incorrect username or password") == 0)
475 {
476 username.clear();
477 cout << "User login failed" << endl;
478 }
479 else
480 {
481 state = STATE_LOGIN;
482 wndCurrent = wndMain;
483
484 Player p("", "");
485 p.deserialize(msg.buffer);
486 mapPlayers[p.id] = p;
487 curPlayerId = p.id;
488
489 cout << "Got a valid login response with the player" << endl;
490 cout << "Player id: " << curPlayerId << endl;
491 cout << "map size: " << mapPlayers.size() << endl;
492 }
493
494 break;
495 }
496 case MSG_TYPE_PLAYER: // kind of hacky to put this here
497 {
498 cout << "Got MSG_TYPE_PLAYER message in STATE_START" << endl;
499
500 Player p("", "");
501 p.deserialize(msg.buffer);
502 p.timeLastUpdated = getCurrentMillis();
503 mapPlayers[p.id] = p;
504
505 cout << "new player id: " << p.id << endl;
506 cout << "map size: " << mapPlayers.size() << endl;
507
508 break;
509 }
510 case MSG_TYPE_OBJECT:
511 {
512 cout << "Received OBJECT message in STATE_START." << endl;
513
514 WorldMap::Object o(0, WorldMap::OBJECT_NONE, 0, 0);
515 o.deserialize(msg.buffer);
516 cout << "object id: " << o.id << endl;
517 gameMap->updateObject(o.id, o.type, o.pos.x, o.pos.y);
518
519 break;
520 }
521 }
522
523 break;
524 }
525 case STATE_LOGIN:
526 {
527 switch(msg.type)
528 {
529 case MSG_TYPE_REGISTER:
530 {
531 break;
532 }
533 case MSG_TYPE_LOGIN:
534 {
535 cout << "Got a login message" << endl;
536
537 chatConsole.addLine(response);
538 cout << "Added new line" << endl;
539
540 break;
541 }
542 case MSG_TYPE_LOGOUT:
543 {
544 cout << "Got a logout message" << endl;
545
546 if (response.compare("You have successfully logged out.") == 0)
547 {
548 cout << "Logged out" << endl;
549 state = STATE_START;
550 wndCurrent = wndLogin;
551 }
552
553 break;
554 }
555 case MSG_TYPE_PLAYER:
556 {
557 cout << "Got MSG_TYPE_PLAYER message in STATE_LOGIN" << endl;
558
559 Player p("", "");
560 p.deserialize(msg.buffer);
561 p.timeLastUpdated = getCurrentMillis();
562 mapPlayers[p.id] = p;
563
564 break;
565 }
566 case MSG_TYPE_PLAYER_MOVE:
567 {
568 cout << "Got a player move message" << endl;
569
570 unsigned int id;
571 int x, y;
572
573 memcpy(&id, msg.buffer, 4);
574 memcpy(&x, msg.buffer+4, 4);
575 memcpy(&y, msg.buffer+8, 4);
576
577 mapPlayers[id].target.x = x;
578 mapPlayers[id].target.y = y;
579
580 break;
581 }
582 case MSG_TYPE_CHAT:
583 {
584 chatConsole.addLine(response);
585
586 break;
587 }
588 case MSG_TYPE_OBJECT:
589 {
590 cout << "Received object message in STATE_LOGIN." << endl;
591
592 WorldMap::Object o(0, WorldMap::OBJECT_NONE, 0, 0);
593 o.deserialize(msg.buffer);
594 gameMap->updateObject(o.id, o.type, o.pos.x, o.pos.y);
595
596 break;
597 }
598 case MSG_TYPE_REMOVE_OBJECT:
599 {
600 cout << "Received REMOVE_OBJECT message!" << endl;
601
602 int id;
603 memcpy(&id, msg.buffer, 4);
604
605 cout << "Removing object with id " << id << endl;
606
607 if (!gameMap->removeObject(id))
608 cout << "Did not remove the object" << endl;
609
610 break;
611 }
612 case MSG_TYPE_SCORE:
613 {
614 memcpy(&scoreBlue, msg.buffer, 4);
615 memcpy(&scoreRed, msg.buffer+4, 4);
616
617 break;
618 }
619 case MSG_TYPE_ATTACK:
620 {
621 break;
622 }
623 case MSG_TYPE_PROJECTILE:
624 {
625 break;
626 }
627 case MSG_TYPE_REMOVE_PROJECTILE:
628 {
629 break;
630 }
631 default:
632 {
633 cout << "Received an unexpected message type: " << msg.type << endl;
634 }
635 }
636
637 break;
638 }
639 default:
640 {
641 cout << "The state has an invalid value: " << state << endl;
642
643 break;
644 }
645 }
646}
647
648// this should probably be in the WorldMap class
649void drawMap(WorldMap* gameMap)
650{
651 POSITION mapPos;
652 mapPos.x = 0;
653 mapPos.y = 0;
654 mapPos = mapToScreen(mapPos);
655
656 for (int x=0; x<12; x++)
657 {
658 for (int y=0; y<12; y++)
659 {
660 WorldMap::TerrainType el = gameMap->getElement(x, y);
661 WorldMap::StructureType structure = gameMap->getStructure(x, y);
662
663 if (el == WorldMap::TERRAIN_GRASS)
664 al_draw_filled_rectangle(x*25+mapPos.x, y*25+mapPos.y, x*25+25+mapPos.x, y*25+25+mapPos.y, al_map_rgb(0, 255, 0));
665 else if (el == WorldMap::TERRAIN_OCEAN)
666 al_draw_filled_rectangle(x*25+mapPos.x, y*25+mapPos.y, x*25+25+mapPos.x, y*25+25+mapPos.y, al_map_rgb(0, 0, 255));
667 else if (el == WorldMap::TERRAIN_ROCK)
668 al_draw_filled_rectangle(x*25+mapPos.x, y*25+mapPos.y, x*25+25+mapPos.x, y*25+25+mapPos.y, al_map_rgb(100, 100, 0));
669
670 if (structure == WorldMap::STRUCTURE_BLUE_FLAG) {
671 al_draw_circle(x*25+12+mapPos.x, y*25+12+mapPos.y, 12, al_map_rgb(0, 0, 0), 3);
672 //al_draw_filled_rectangle(x*25+5+mapPos.x, y*25+5+mapPos.y, x*25+20+mapPos.x, y*25+20+mapPos.y, al_map_rgb(0, 0, 255));
673 }else if (structure == WorldMap::STRUCTURE_RED_FLAG) {
674 al_draw_circle(x*25+12+mapPos.x, y*25+12+mapPos.y, 12, al_map_rgb(0, 0, 0), 3);
675 //al_draw_filled_rectangle(x*25+5+mapPos.x, y*25+5+mapPos.y, x*25+20+mapPos.x, y*25+20+mapPos.y, al_map_rgb(255, 0, 0));
676 }
677 }
678 }
679
680 for (int x=0; x<12; x++)
681 {
682 for (int y=0; y<12; y++)
683 {
684 vector<WorldMap::Object> vctObjects = gameMap->getObjects(x, y);
685
686 vector<WorldMap::Object>::iterator it;
687 for(it = vctObjects.begin(); it != vctObjects.end(); it++) {
688 switch(it->type) {
689 case WorldMap::OBJECT_BLUE_FLAG:
690 al_draw_filled_rectangle(it->pos.x-8+mapPos.x, it->pos.y-8+mapPos.y, it->pos.x+8+mapPos.x, it->pos.y+8+mapPos.y, al_map_rgb(0, 0, 255));
691 break;
692 case WorldMap::OBJECT_RED_FLAG:
693 al_draw_filled_rectangle(it->pos.x-8+mapPos.x, it->pos.y-8+mapPos.y, it->pos.x+8+mapPos.x, it->pos.y+8+mapPos.y, al_map_rgb(255, 0, 0));
694 break;
695 }
696 }
697 }
698 }
699}
700
701void drawPlayers(map<unsigned int, Player>& mapPlayers, ALLEGRO_FONT* font, unsigned int curPlayerId)
702{
703 map<unsigned int, Player>::iterator it;
704
705 Player* p;
706 POSITION pos;
707 ALLEGRO_COLOR color;
708
709 for(it = mapPlayers.begin(); it != mapPlayers.end(); it++)
710 {
711 p = &it->second;
712 pos = mapToScreen(p->pos);
713
714 if (p->id == curPlayerId)
715 al_draw_filled_circle(pos.x, pos.y, 14, al_map_rgb(0, 0, 0));
716
717 if (p->team == 0)
718 color = al_map_rgb(0, 0, 255);
719 else if (p->team == 1)
720 color = al_map_rgb(255, 0, 0);
721
722 al_draw_filled_circle(pos.x, pos.y, 12, color);
723
724 // draw player class
725 int fontHeight = al_get_font_line_height(font);
726
727 string strClass;
728 switch (p->playerClass) {
729 case Player::CLASS_WARRIOR:
730 strClass = "W";
731 break;
732 case Player::CLASS_RANGER:
733 strClass = "R";
734 break;
735 case Player::CLASS_NONE:
736 strClass = "";
737 break;
738 default:
739 strClass = "";
740 break;
741 }
742 al_draw_text(font, al_map_rgb(0, 0, 0), pos.x, pos.y-fontHeight/2, ALLEGRO_ALIGN_CENTRE, strClass.c_str());
743
744 // draw player health
745 al_draw_filled_rectangle(pos.x-12, pos.y-24, pos.x+12, pos.y-16, al_map_rgb(0, 0, 0));
746 if (it->second.maxHealth != 0)
747 al_draw_filled_rectangle(pos.x-11, pos.y-23, pos.x-11+(22*it->second.health)/it->second.maxHealth, pos.y-17, al_map_rgb(255, 0, 0));
748
749 if (p->hasBlueFlag)
750 al_draw_filled_rectangle(pos.x+4, pos.y-18, pos.x+18, pos.y-4, al_map_rgb(0, 0, 255));
751 else if (p->hasRedFlag)
752 al_draw_filled_rectangle(pos.x+4, pos.y-18, pos.x+18, pos.y-4, al_map_rgb(255, 0, 0));
753 }
754}
755
756void registerAccount()
757{
758 string username = txtUsername->getStr();
759 string password = txtPassword->getStr();
760
761 txtUsername->clear();
762 txtPassword->clear();
763
764 msgTo.type = MSG_TYPE_REGISTER;
765
766 strcpy(msgTo.buffer, username.c_str());
767 strcpy(msgTo.buffer+username.size()+1, password.c_str());
768
769 sendMessage(&msgTo, sock, &server);
770}
771
772void login()
773{
774 string strUsername = txtUsername->getStr();
775 string strPassword = txtPassword->getStr();
776 username = strUsername;
777
778 txtUsername->clear();
779 txtPassword->clear();
780
781 msgTo.type = MSG_TYPE_LOGIN;
782
783 strcpy(msgTo.buffer, strUsername.c_str());
784 strcpy(msgTo.buffer+username.size()+1, strPassword.c_str());
785
786 sendMessage(&msgTo, sock, &server);
787}
788
789void logout()
790{
791 txtChat->clear();
792
793 msgTo.type = MSG_TYPE_LOGOUT;
794
795 strcpy(msgTo.buffer, username.c_str());
796
797 sendMessage(&msgTo, sock, &server);
798}
799
800void quit()
801{
802 doexit = true;
803}
804
805void sendChatMessage()
806{
807 string msg = txtChat->getStr();
808
809 txtChat->clear();
810
811 msgTo.type = MSG_TYPE_CHAT;
812
813 strcpy(msgTo.buffer, msg.c_str());
814
815 sendMessage(&msgTo, sock, &server);
816}
Note: See TracBrowser for help on using the repository browser.