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

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

Replaced the client with a simple allegro drawing/font demo

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