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

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

Client sends a test registration message when the user enters a username

  • Property mode set to 100644
File size: 10.0 KB
RevLine 
[a845faf]1#include "../../common/compiler.h"
2
[e08572c]3#if defined WINDOWS
[0dde5da]4 #include <winsock2.h>
5 #include <WS2tcpip.h>
[e08572c]6#elif defined LINUX
[0dde5da]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>
[a845faf]13#endif
[1912323]14
[d352805]15#include <sys/types.h>
[1912323]16#include <stdio.h>
17#include <stdlib.h>
[a845faf]18#include <string>
[1912323]19#include <iostream>
[9a3e6b1]20#include <vector>
[1912323]21
[d352805]22#include <allegro5/allegro.h>
23#include <allegro5/allegro_font.h>
24#include <allegro5/allegro_ttf.h>
[7d7df47]25
26#include "../../common/message.h"
27
[6475138]28#include "chat.h"
29
[a845faf]30#ifdef WINDOWS
[6475138]31 #pragma comment(lib, "ws2_32.lib")
[a845faf]32#endif
[1912323]33
34using namespace std;
35
[0dde5da]36void initWinSock();
37void shutdownWinSock();
[4da5aa3]38void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, string &username);
39
[1912323]40void error(const char *);
[7d7df47]41
[d352805]42const float FPS = 60;
43const int SCREEN_W = 640;
44const int SCREEN_H = 480;
45const int BOUNCER_SIZE = 32;
46enum MYKEYS {
[0cc431d]47 KEY_UP,
48 KEY_DOWN,
49 KEY_LEFT,
50 KEY_RIGHT
51};
52
53enum STATE {
54 STATE_START,
[ec48e7d]55 STATE_LOGIN,
56 STATE_LOGOUT
[d352805]57};
58
59int main(int argc, char **argv)
60{
61 ALLEGRO_DISPLAY *display = NULL;
62 ALLEGRO_EVENT_QUEUE *event_queue = NULL;
63 ALLEGRO_TIMER *timer = NULL;
64 ALLEGRO_BITMAP *bouncer = NULL;
65 float bouncer_x = SCREEN_W / 2.0 - BOUNCER_SIZE / 2.0;
66 float bouncer_y = SCREEN_H / 2.0 - BOUNCER_SIZE / 2.0;
67 bool key[4] = { false, false, false, false };
68 bool redraw = true;
69 bool doexit = false;
[9a3e6b1]70
[0cc431d]71 int state = STATE_START;
72
[6475138]73 chat chatConsole;
[9a3e6b1]74
[d352805]75 if(!al_init()) {
76 fprintf(stderr, "failed to initialize allegro!\n");
77 return -1;
78 }
79
80 al_init_font_addon();
81 al_init_ttf_addon();
82
83 ALLEGRO_FONT *font = al_load_ttf_font("../pirulen.ttf", 12, 0);
84
85 if (!font) {
86 fprintf(stderr, "Could not load 'pirulen.ttf'.\n");
87 getchar();
88 return -1;
89 }
90
91 if(!al_install_keyboard()) {
92 fprintf(stderr, "failed to initialize the keyboard!\n");
93 return -1;
94 }
95
96 timer = al_create_timer(1.0 / FPS);
97 if(!timer) {
98 fprintf(stderr, "failed to create timer!\n");
99 return -1;
100 }
101
102 display = al_create_display(SCREEN_W, SCREEN_H);
103 if(!display) {
104 fprintf(stderr, "failed to create display!\n");
105 al_destroy_timer(timer);
106 return -1;
107 }
108
109 bouncer = al_create_bitmap(BOUNCER_SIZE, BOUNCER_SIZE);
110 if(!bouncer) {
111 fprintf(stderr, "failed to create bouncer bitmap!\n");
112 al_destroy_display(display);
113 al_destroy_timer(timer);
114 return -1;
115 }
116
117 event_queue = al_create_event_queue();
118 if(!event_queue) {
119 fprintf(stderr, "failed to create event_queue!\n");
120 al_destroy_bitmap(bouncer);
121 al_destroy_display(display);
122 al_destroy_timer(timer);
123 return -1;
124 }
125
126 al_set_target_bitmap(bouncer);
127
128 al_clear_to_color(al_map_rgb(255, 0, 255));
129
130 al_set_target_bitmap(al_get_backbuffer(display));
131
132 al_register_event_source(event_queue, al_get_display_event_source(display));
133
134 al_register_event_source(event_queue, al_get_timer_event_source(timer));
135
136 al_register_event_source(event_queue, al_get_keyboard_event_source());
137
138 al_clear_to_color(al_map_rgb(0,0,0));
139
140 al_flip_display();
[9a3e6b1]141
[a4db787]142 int sock;
[9a3e6b1]143 struct sockaddr_in server, from;
144 struct hostent *hp;
145 NETWORK_MSG msgTo, msgFrom;
[171c4fe]146 string username;
[9a3e6b1]147
148 if (argc != 3) {
149 cout << "Usage: server port" << endl;
150 exit(1);
151 }
152
153 initWinSock();
154
155 sock = socket(AF_INET, SOCK_DGRAM, 0);
156 if (sock < 0)
157 error("socket");
158
159 server.sin_family = AF_INET;
160 hp = gethostbyname(argv[1]);
161 if (hp==0)
162 error("Unknown host");
163
164 memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length);
165 server.sin_port = htons(atoi(argv[2]));
166
[d352805]167 al_start_timer(timer);
168
169 while(!doexit)
170 {
171 ALLEGRO_EVENT ev;
172 al_wait_for_event(event_queue, &ev);
173
174 if(ev.type == ALLEGRO_EVENT_TIMER) {
175 if(key[KEY_UP] && bouncer_y >= 4.0) {
176 bouncer_y -= 4.0;
177 }
178
179 if(key[KEY_DOWN] && bouncer_y <= SCREEN_H - BOUNCER_SIZE - 4.0) {
180 bouncer_y += 4.0;
181 }
182
183 if(key[KEY_LEFT] && bouncer_x >= 4.0) {
184 bouncer_x -= 4.0;
185 }
186
187 if(key[KEY_RIGHT] && bouncer_x <= SCREEN_W - BOUNCER_SIZE - 4.0) {
188 bouncer_x += 4.0;
189 }
190
191 redraw = true;
192 }
193 else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
[9a3e6b1]194 doexit = true;
[d352805]195 }
196 else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) {
[6475138]197 bool eventConsumed = chatConsole.processEvent(ev);
198
199 if (eventConsumed) {
200 string input = chatConsole.getInput();
201 if (!input.empty()) {
202 cout << "input: " << input << endl;
203 strcpy(msgTo.buffer, input.c_str());
[0cc431d]204
205 switch(state)
206 {
[ec48e7d]207 case STATE_START:
[171c4fe]208 {
[ec48e7d]209 username = input;
[439f7bc]210 strcpy(msgTo.buffer+input.size()+1, "MyPassword");
211 msgTo.type = MSG_TYPE_REGISTER;
212 //msgTo.type = MSG_TYPE_LOGIN;
[a4db787]213
[4da5aa3]214 sendMessage(&msgTo, sock, &server);
215 receiveMessage(&msgFrom, sock, &from);
216 processMessage(msgFrom, state, chatConsole, username);
217 cout << "state: " << state << endl;
218
[ec48e7d]219 break;
220 }
221 case STATE_LOGIN:
222 {
223 if (input.compare("quit") == 0 ||
224 input.compare("exit") == 0 ||
225 input.compare("logout") == 0)
226 {
227 strcpy(msgTo.buffer, username.c_str());
228 msgTo.type = MSG_TYPE_LOGOUT;
229 }
230 else
231 msgTo.type = MSG_TYPE_CHAT;
[a4db787]232
[4da5aa3]233 sendMessage(&msgTo, sock, &server);
234 receiveMessage(&msgFrom, sock, &from);
235 processMessage(msgFrom, state, chatConsole, username);
236 cout << "state: " << state << endl;
[a4db787]237
[171c4fe]238 break;
239 }
[ec48e7d]240 case STATE_LOGOUT:
241 {
[4da5aa3]242 chatConsole.addLine("You're logged out, so you can't send any messages to the server.");
243
244 cout << "You're logged out, so you can't send any messages to the server." << endl;
[ec48e7d]245
[171c4fe]246 break;
247 }
248 default:
249 {
250 cout << "The state has an invalid value: " << state << endl;
[4da5aa3]251
[171c4fe]252 break;
253 }
[0cc431d]254 }
[6475138]255 }
256 }else {
257 switch(ev.keyboard.keycode) {
258 case ALLEGRO_KEY_UP:
259 key[KEY_UP] = true;
260 break;
[d352805]261
[6475138]262 case ALLEGRO_KEY_DOWN:
263 key[KEY_DOWN] = true;
264 break;
[d352805]265
[6475138]266 case ALLEGRO_KEY_LEFT:
267 key[KEY_LEFT] = true;
268 break;
[d352805]269
[6475138]270 case ALLEGRO_KEY_RIGHT:
271 key[KEY_RIGHT] = true;
272 break;
273 }
[d352805]274 }
275 }
276 else if(ev.type == ALLEGRO_EVENT_KEY_UP) {
277 switch(ev.keyboard.keycode) {
278 case ALLEGRO_KEY_UP:
279 key[KEY_UP] = false;
280 break;
281
282 case ALLEGRO_KEY_DOWN:
283 key[KEY_DOWN] = false;
284 break;
285
286 case ALLEGRO_KEY_LEFT:
287 key[KEY_LEFT] = false;
288 break;
289
290 case ALLEGRO_KEY_RIGHT:
291 key[KEY_RIGHT] = false;
292 break;
293
294 case ALLEGRO_KEY_ESCAPE:
295 doexit = true;
296 break;
297 }
298 }
299
300 if(redraw && al_is_event_queue_empty(event_queue)) {
301 redraw = false;
302
303 al_clear_to_color(al_map_rgb(0,0,0));
304
305 al_draw_bitmap(bouncer, bouncer_x, bouncer_y, 0);
[9a3e6b1]306
[6475138]307 chatConsole.draw(font, al_map_rgb(255,255,255));
[9a3e6b1]308
[d352805]309 al_flip_display();
310 }
311 }
[9a3e6b1]312
313 #if defined WINDOWS
314 closesocket(sock);
315 #elif defined LINUX
316 close(sock);
317 #endif
318
319 shutdownWinSock();
[d352805]320
321 al_destroy_event_queue(event_queue);
322 al_destroy_bitmap(bouncer);
323 al_destroy_display(display);
324 al_destroy_timer(timer);
325
326 return 0;
327}
328
[4da5aa3]329// need to make a function like this that works on windows
330void error(const char *msg)
331{
332 perror(msg);
333 shutdownWinSock();
334 exit(1);
335}
336
[0dde5da]337void initWinSock()
338{
339#if defined WINDOWS
340 WORD wVersionRequested;
341 WSADATA wsaData;
342 int wsaerr;
343
344 wVersionRequested = MAKEWORD(2, 2);
345 wsaerr = WSAStartup(wVersionRequested, &wsaData);
346
347 if (wsaerr != 0) {
348 cout << "The Winsock dll not found." << endl;
349 exit(1);
350 }else
351 cout << "The Winsock dll was found." << endl;
352#endif
353}
354
355void shutdownWinSock()
356{
357#if defined WINDOWS
358 WSACleanup();
359#endif
[1912323]360}
361
[4da5aa3]362void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, string &username)
[1912323]363{
[4da5aa3]364 string response = string(msg.buffer);
365
366 // this whole select block should go in a new function.
367 // Figure out how to pass and return params properly
368 switch(state)
369 {
370 case STATE_START:
371 {
372 chatConsole.addLine(response);
373
[439f7bc]374 /*
[4da5aa3]375 if (response.compare("Player has already logged in.") == 0)
376 {
377 cout << "User login failed" << endl;
378 username.clear();
379 }
380 else
381 {
382 cout << "User login successful" << endl;
383 state = STATE_LOGIN;
384 }
[439f7bc]385 */
[4da5aa3]386
387 break;
388 }
389 case STATE_LOGIN:
390 {
391 chatConsole.addLine(response);
392
393 if (response.compare("You have successfully logged out. You may quit the game.") == 0)
394 {
395 cout << "Logged out" << endl;
396 state = STATE_LOGOUT;
397 }
398 else
399 {
400 cout << "Added new line" << endl;
401 }
402
403 break;
404 }
405 case STATE_LOGOUT:
406 {
407 cout << "Bug: You're logged out, so you shouldn't be receiving any messages." << endl;
408
409 break;
410 }
411 default:
412 {
413 cout << "The state has an invalid value: " << state << endl;
414
415 break;
416 }
417 }
[0dde5da]418}
Note: See TracBrowser for help on using the repository browser.