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

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

Added a chat class to encapsulate the server messages and text input

  • Property mode set to 100644
File size: 7.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 <stdio.h>
17#include <stdlib.h>
18#include <string>
19#include <iostream>
20#include <vector>
21
22#include <allegro5/allegro.h>
23#include <allegro5/allegro_font.h>
24#include <allegro5/allegro_ttf.h>
25
26#include "../../common/message.h"
27
28#include "chat.h"
29
30#ifdef WINDOWS
31 #pragma comment(lib, "ws2_32.lib")
32#endif
33
34using namespace std;
35
36void initWinSock();
37void shutdownWinSock();
38void error(const char *);
39
40const float FPS = 60;
41const int SCREEN_W = 640;
42const int SCREEN_H = 480;
43const int BOUNCER_SIZE = 32;
44enum MYKEYS {
45 KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT
46};
47
48int main(int argc, char **argv)
49{
50 ALLEGRO_DISPLAY *display = NULL;
51 ALLEGRO_EVENT_QUEUE *event_queue = NULL;
52 ALLEGRO_TIMER *timer = NULL;
53 ALLEGRO_BITMAP *bouncer = NULL;
54 float bouncer_x = SCREEN_W / 2.0 - BOUNCER_SIZE / 2.0;
55 float bouncer_y = SCREEN_H / 2.0 - BOUNCER_SIZE / 2.0;
56 bool key[4] = { false, false, false, false };
57 bool redraw = true;
58 bool doexit = false;
59
60 chat chatConsole;
61
62 if(!al_init()) {
63 fprintf(stderr, "failed to initialize allegro!\n");
64 return -1;
65 }
66
67 al_init_font_addon();
68 al_init_ttf_addon();
69
70 ALLEGRO_FONT *font = al_load_ttf_font("../pirulen.ttf", 12, 0);
71
72 if (!font) {
73 fprintf(stderr, "Could not load 'pirulen.ttf'.\n");
74 getchar();
75 return -1;
76 }
77
78 if(!al_install_keyboard()) {
79 fprintf(stderr, "failed to initialize the keyboard!\n");
80 return -1;
81 }
82
83 timer = al_create_timer(1.0 / FPS);
84 if(!timer) {
85 fprintf(stderr, "failed to create timer!\n");
86 return -1;
87 }
88
89 display = al_create_display(SCREEN_W, SCREEN_H);
90 if(!display) {
91 fprintf(stderr, "failed to create display!\n");
92 al_destroy_timer(timer);
93 return -1;
94 }
95
96 bouncer = al_create_bitmap(BOUNCER_SIZE, BOUNCER_SIZE);
97 if(!bouncer) {
98 fprintf(stderr, "failed to create bouncer bitmap!\n");
99 al_destroy_display(display);
100 al_destroy_timer(timer);
101 return -1;
102 }
103
104 event_queue = al_create_event_queue();
105 if(!event_queue) {
106 fprintf(stderr, "failed to create event_queue!\n");
107 al_destroy_bitmap(bouncer);
108 al_destroy_display(display);
109 al_destroy_timer(timer);
110 return -1;
111 }
112
113 al_set_target_bitmap(bouncer);
114
115 al_clear_to_color(al_map_rgb(255, 0, 255));
116
117 al_set_target_bitmap(al_get_backbuffer(display));
118
119 al_register_event_source(event_queue, al_get_display_event_source(display));
120
121 al_register_event_source(event_queue, al_get_timer_event_source(timer));
122
123 al_register_event_source(event_queue, al_get_keyboard_event_source());
124
125 al_clear_to_color(al_map_rgb(0,0,0));
126
127 al_flip_display();
128
129 int sock, n;
130 struct sockaddr_in server, from;
131 struct hostent *hp;
132 char buffer[256];
133 NETWORK_MSG msgTo, msgFrom;
134
135 if (argc != 3) {
136 cout << "Usage: server port" << endl;
137 exit(1);
138 }
139
140 initWinSock();
141
142 sock = socket(AF_INET, SOCK_DGRAM, 0);
143 if (sock < 0)
144 error("socket");
145
146 server.sin_family = AF_INET;
147 hp = gethostbyname(argv[1]);
148 if (hp==0)
149 error("Unknown host");
150
151 memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length);
152 server.sin_port = htons(atoi(argv[2]));
153
154 strcpy(msgTo.buffer, "Hello");
155 n=sendMessage(&msgTo, sock, &server);
156 if (n < 0)
157 error("sendMessage");
158
159 n = receiveMessage(&msgFrom, sock, &from);
160 if (n < 0)
161 error("receiveMessage");
162
163 chatConsole.addLine(msgFrom.buffer);
164
165 al_start_timer(timer);
166
167 while(!doexit)
168 {
169 ALLEGRO_EVENT ev;
170 al_wait_for_event(event_queue, &ev);
171
172 if(ev.type == ALLEGRO_EVENT_TIMER) {
173 if(key[KEY_UP] && bouncer_y >= 4.0) {
174 bouncer_y -= 4.0;
175 }
176
177 if(key[KEY_DOWN] && bouncer_y <= SCREEN_H - BOUNCER_SIZE - 4.0) {
178 bouncer_y += 4.0;
179 }
180
181 if(key[KEY_LEFT] && bouncer_x >= 4.0) {
182 bouncer_x -= 4.0;
183 }
184
185 if(key[KEY_RIGHT] && bouncer_x <= SCREEN_W - BOUNCER_SIZE - 4.0) {
186 bouncer_x += 4.0;
187 }
188
189 redraw = true;
190 }
191 else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
192 doexit = true;
193 }
194 else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) {
195 bool eventConsumed = chatConsole.processEvent(ev);
196
197 if (eventConsumed) {
198 string input = chatConsole.getInput();
199 if (!input.empty()) {
200 cout << "input: " << input << endl;
201 strcpy(msgTo.buffer, input.c_str());
202
203 n=sendMessage(&msgTo, sock, &server);
204 if (n < 0)
205 error("sendMessage");
206
207 n = receiveMessage(&msgFrom, sock, &from);
208 if (n < 0)
209 error("receiveMessage");
210
211 chatConsole.addLine(string(msgFrom.buffer));
212 cout << "Added new line" << endl;
213 }
214 }else {
215 switch(ev.keyboard.keycode) {
216 case ALLEGRO_KEY_UP:
217 key[KEY_UP] = true;
218 break;
219
220 case ALLEGRO_KEY_DOWN:
221 key[KEY_DOWN] = true;
222 break;
223
224 case ALLEGRO_KEY_LEFT:
225 key[KEY_LEFT] = true;
226 break;
227
228 case ALLEGRO_KEY_RIGHT:
229 key[KEY_RIGHT] = true;
230 break;
231 }
232 }
233 }
234 else if(ev.type == ALLEGRO_EVENT_KEY_UP) {
235 switch(ev.keyboard.keycode) {
236 case ALLEGRO_KEY_UP:
237 key[KEY_UP] = false;
238 break;
239
240 case ALLEGRO_KEY_DOWN:
241 key[KEY_DOWN] = false;
242 break;
243
244 case ALLEGRO_KEY_LEFT:
245 key[KEY_LEFT] = false;
246 break;
247
248 case ALLEGRO_KEY_RIGHT:
249 key[KEY_RIGHT] = false;
250 break;
251
252 case ALLEGRO_KEY_ESCAPE:
253 doexit = true;
254 break;
255 }
256 }
257
258 if(redraw && al_is_event_queue_empty(event_queue)) {
259 redraw = false;
260
261 al_clear_to_color(al_map_rgb(0,0,0));
262
263 al_draw_bitmap(bouncer, bouncer_x, bouncer_y, 0);
264
265 chatConsole.draw(font, al_map_rgb(255,255,255));
266
267 al_flip_display();
268 }
269 }
270
271 #if defined WINDOWS
272 closesocket(sock);
273 #elif defined LINUX
274 close(sock);
275 #endif
276
277 shutdownWinSock();
278
279 al_destroy_event_queue(event_queue);
280 al_destroy_bitmap(bouncer);
281 al_destroy_display(display);
282 al_destroy_timer(timer);
283
284 return 0;
285}
286
287void initWinSock()
288{
289#if defined WINDOWS
290 WORD wVersionRequested;
291 WSADATA wsaData;
292 int wsaerr;
293
294 wVersionRequested = MAKEWORD(2, 2);
295 wsaerr = WSAStartup(wVersionRequested, &wsaData);
296
297 if (wsaerr != 0) {
298 cout << "The Winsock dll not found." << endl;
299 exit(1);
300 }else
301 cout << "The Winsock dll was found." << endl;
302#endif
303}
304
305void shutdownWinSock()
306{
307#if defined WINDOWS
308 WSACleanup();
309#endif
310}
311
312// need to make a function like this that works on windows
313void error(const char *msg)
314{
315 perror(msg);
316 shutdownWinSock();
317 exit(1);
318}
Note: See TracBrowser for help on using the repository browser.