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

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

Changed the frontend to a gui implemented using Allegro

  • Property mode set to 100644
File size: 7.7 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
[a845faf]28#ifdef WINDOWS
29 #pragma comment(lib, "ws2_32.lib")
30#endif
[1912323]31
32using namespace std;
33
[0dde5da]34void initWinSock();
35void shutdownWinSock();
[1912323]36void error(const char *);
[7d7df47]37
[d352805]38const float FPS = 60;
39const int SCREEN_W = 640;
40const int SCREEN_H = 480;
41const int BOUNCER_SIZE = 32;
42enum MYKEYS {
43 KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT
44};
45
46int main(int argc, char **argv)
47{
48 ALLEGRO_DISPLAY *display = NULL;
49 ALLEGRO_EVENT_QUEUE *event_queue = NULL;
50 ALLEGRO_TIMER *timer = NULL;
51 ALLEGRO_BITMAP *bouncer = NULL;
52 float bouncer_x = SCREEN_W / 2.0 - BOUNCER_SIZE / 2.0;
53 float bouncer_y = SCREEN_H / 2.0 - BOUNCER_SIZE / 2.0;
54 bool key[4] = { false, false, false, false };
55 bool redraw = true;
56 bool doexit = false;
[9a3e6b1]57
58 vector<string> vctChat;
59 string strPrompt;
60 string strEnteredInput;
61
[d352805]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();
[9a3e6b1]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 vctChat.push_back(string(msgFrom.buffer));
164
[d352805]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) {
[9a3e6b1]192 doexit = true;
[d352805]193 }
194 else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) {
195 switch(ev.keyboard.keycode) {
196 case ALLEGRO_KEY_UP:
197 key[KEY_UP] = true;
198 break;
199
200 case ALLEGRO_KEY_DOWN:
201 key[KEY_DOWN] = true;
202 break;
203
204 case ALLEGRO_KEY_LEFT:
205 key[KEY_LEFT] = true;
206 break;
207
208 case ALLEGRO_KEY_RIGHT:
209 key[KEY_RIGHT] = true;
210 break;
[9a3e6b1]211
212 case ALLEGRO_KEY_ENTER:
213 strEnteredInput = strPrompt;
214 strPrompt.clear();
215
216 if (strEnteredInput.compare("q") == 0) {
217 doexit = true;
218 }
219 else
220 {
221 strcpy(msgTo.buffer, strEnteredInput.c_str());
222 n=sendMessage(&msgTo, sock, &server);
223 if (n < 0)
224 error("sendMessage");
225
226 n = receiveMessage(&msgFrom, sock, &from);
227 if (n < 0)
228 error("receiveMessage");
229
230 vctChat.push_back(string(msgFrom.buffer));
231 }
232
233 break;
[d352805]234 }
[9a3e6b1]235
236 if(ALLEGRO_KEY_A <= ev.keyboard.keycode && ev.keyboard.keycode <= ALLEGRO_KEY_Z)
237 {
238 char newChar = 'a'+ev.keyboard.keycode-ALLEGRO_KEY_A;
239 strPrompt.append(1, newChar);
240 }
241
242 if(ALLEGRO_KEY_0 <= ev.keyboard.keycode && ev.keyboard.keycode <= ALLEGRO_KEY_9)
243 {
244 char newChar = '0'+ev.keyboard.keycode-ALLEGRO_KEY_0;
245 strPrompt.append(1, newChar);
246 }
[d352805]247 }
248 else if(ev.type == ALLEGRO_EVENT_KEY_UP) {
249 switch(ev.keyboard.keycode) {
250 case ALLEGRO_KEY_UP:
251 key[KEY_UP] = false;
252 break;
253
254 case ALLEGRO_KEY_DOWN:
255 key[KEY_DOWN] = false;
256 break;
257
258 case ALLEGRO_KEY_LEFT:
259 key[KEY_LEFT] = false;
260 break;
261
262 case ALLEGRO_KEY_RIGHT:
263 key[KEY_RIGHT] = false;
264 break;
265
266 case ALLEGRO_KEY_ESCAPE:
267 doexit = true;
268 break;
269 }
270 }
271
272 if(redraw && al_is_event_queue_empty(event_queue)) {
273 redraw = false;
274
275 al_clear_to_color(al_map_rgb(0,0,0));
276
277 al_draw_bitmap(bouncer, bouncer_x, bouncer_y, 0);
[9a3e6b1]278
279 for(int x=0; x<vctChat.size(); x++)
280 al_draw_text(font, al_map_rgb(255,255,255), 10, 10+x*15, ALLEGRO_ALIGN_LEFT, vctChat[x].c_str());
281
282 al_draw_text(font, al_map_rgb(255,255,255), 10, 460, ALLEGRO_ALIGN_LEFT, strPrompt.c_str());
[d352805]283
284 al_flip_display();
285 }
286 }
[9a3e6b1]287
288 #if defined WINDOWS
289 closesocket(sock);
290 #elif defined LINUX
291 close(sock);
292 #endif
293
294 shutdownWinSock();
[d352805]295
296 al_destroy_event_queue(event_queue);
297 al_destroy_bitmap(bouncer);
298 al_destroy_display(display);
299 al_destroy_timer(timer);
300
301 return 0;
302}
303
[0dde5da]304void initWinSock()
305{
306#if defined WINDOWS
307 WORD wVersionRequested;
308 WSADATA wsaData;
309 int wsaerr;
310
311 wVersionRequested = MAKEWORD(2, 2);
312 wsaerr = WSAStartup(wVersionRequested, &wsaData);
313
314 if (wsaerr != 0) {
315 cout << "The Winsock dll not found." << endl;
316 exit(1);
317 }else
318 cout << "The Winsock dll was found." << endl;
319#endif
320}
321
322void shutdownWinSock()
323{
324#if defined WINDOWS
325 WSACleanup();
326#endif
[1912323]327}
328
[a845faf]329// need to make a function like this that works on windows
[1912323]330void error(const char *msg)
331{
[0dde5da]332 perror(msg);
333 shutdownWinSock();
334 exit(1);
335}
Note: See TracBrowser for help on using the repository browser.