- Timestamp:
- Dec 3, 2012, 12:24:23 AM (12 years ago)
- Branches:
- master
- Children:
- 41ad8ed
- Parents:
- 439f7bc
- Location:
- client
- Files:
-
- 8 added
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
client/.gitignore
r439f7bc r87b3ee2 4 4 Client.sdf 5 5 Client.opensdf 6 client6 gameClient 7 7 allegro.log -
client/Client/Client.vcxproj
r439f7bc r87b3ee2 66 66 <ClCompile Include="..\..\common\message.cpp" /> 67 67 <ClCompile Include="chat.cpp" /> 68 <ClCompile Include="Button.cpp" /> 69 <ClCompile Include="GuiComponent.cpp" /> 68 70 <ClCompile Include="main.cpp" /> 71 <ClCompile Include="Textbox.cpp" /> 72 <ClCompile Include="Window.cpp" /> 69 73 </ItemGroup> 70 74 <ItemGroup> … … 72 76 <ClInclude Include="..\..\common\message.h" /> 73 77 <ClInclude Include="chat.h" /> 78 <ClInclude Include="Button.h" /> 79 <ClInclude Include="GuiComponent.h" /> 80 <ClInclude Include="Textbox.h" /> 81 <ClInclude Include="Window.h" /> 74 82 </ItemGroup> 75 83 <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> -
client/Client/Client.vcxproj.filters
r439f7bc r87b3ee2 20 20 <UniqueIdentifier>{6c9ad2f2-bc8d-4e25-9c5c-c4440c60991c}</UniqueIdentifier> 21 21 </Filter> 22 <Filter Include="Header Files\gui"> 23 <UniqueIdentifier>{c6c812ef-0533-42bc-9d71-aa40428b5ca8}</UniqueIdentifier> 24 </Filter> 25 <Filter Include="Source Files\gui"> 26 <UniqueIdentifier>{10ae8361-2a07-4085-b595-761b47868bad}</UniqueIdentifier> 27 </Filter> 22 28 </ItemGroup> 23 29 <ItemGroup> … … 29 35 </ClCompile> 30 36 <ClCompile Include="chat.cpp"> 37 <Filter>Source Files</Filter> 38 </ClCompile> 39 <ClCompile Include="GuiComponent.cpp"> 40 <Filter>Source Files\gui</Filter> 41 </ClCompile> 42 <ClCompile Include="Textbox.cpp"> 43 <Filter>Source Files\gui</Filter> 44 </ClCompile> 45 <ClCompile Include="Button.cpp"> 46 <Filter>Source Files\gui</Filter> 47 </ClCompile> 48 <ClCompile Include="Window.cpp"> 31 49 <Filter>Source Files</Filter> 32 50 </ClCompile> … … 42 60 <Filter>Header Files\common</Filter> 43 61 </ClInclude> 62 <ClInclude Include="GuiComponent.h"> 63 <Filter>Header Files\gui</Filter> 64 </ClInclude> 65 <ClInclude Include="Textbox.h"> 66 <Filter>Header Files\gui</Filter> 67 </ClInclude> 68 <ClInclude Include="Button.h"> 69 <Filter>Header Files\gui</Filter> 70 </ClInclude> 71 <ClInclude Include="Window.h"> 72 <Filter>Header Files</Filter> 73 </ClInclude> 44 74 </ItemGroup> 45 75 </Project> -
client/Client/chat.cpp
r439f7bc r87b3ee2 19 19 { 20 20 for(int x=0; x<vctChat.size(); x++) 21 al_draw_text(font, color, 10, 1 0+x*15, ALLEGRO_ALIGN_LEFT, vctChat[x].c_str());21 al_draw_text(font, color, 10, 140+x*15, ALLEGRO_ALIGN_LEFT, vctChat[x].c_str()); 22 22 23 23 al_draw_text(font, color, 10, 460, ALLEGRO_ALIGN_LEFT, strPrompt.c_str()); … … 30 30 31 31 // returns true if the event was consumed, false if it should be passed on 32 bool chat:: processEvent(ALLEGRO_EVENT ev)32 bool chat::handleEvent(ALLEGRO_EVENT e) 33 33 { 34 34 ALLEGRO_KEYBOARD_STATE keys; 35 35 al_get_keyboard_state(&keys); 36 36 37 if (e v.type == ALLEGRO_EVENT_KEY_DOWN) {37 if (e.type == ALLEGRO_EVENT_KEY_DOWN) { 38 38 char newChar = 0; 39 39 40 if (ALLEGRO_KEY_A <= e v.keyboard.keycode && ev.keyboard.keycode <= ALLEGRO_KEY_Z) {41 newChar = 'a'+e v.keyboard.keycode-ALLEGRO_KEY_A;40 if (ALLEGRO_KEY_A <= e.keyboard.keycode && e.keyboard.keycode <= ALLEGRO_KEY_Z) { 41 newChar = 'a'+e.keyboard.keycode-ALLEGRO_KEY_A; 42 42 if (al_key_down(&keys, ALLEGRO_KEY_LSHIFT) || al_key_down(&keys, ALLEGRO_KEY_RSHIFT)) 43 43 newChar -= 32; 44 44 } 45 if (ALLEGRO_KEY_0 <= e v.keyboard.keycode && ev.keyboard.keycode <= ALLEGRO_KEY_9)46 newChar = '0'+e v.keyboard.keycode-ALLEGRO_KEY_0;45 if (ALLEGRO_KEY_0 <= e.keyboard.keycode && e.keyboard.keycode <= ALLEGRO_KEY_9) 46 newChar = '0'+e.keyboard.keycode-ALLEGRO_KEY_0; 47 47 48 48 if (newChar != 0) { … … 51 51 } 52 52 53 if (e v.keyboard.keycode == ALLEGRO_KEY_ENTER) {53 if (e.keyboard.keycode == ALLEGRO_KEY_ENTER) { 54 54 strEnteredInput = strPrompt; 55 55 strPrompt.clear(); -
client/Client/chat.h
r439f7bc r87b3ee2 24 24 void addLine(string s); 25 25 26 bool processEvent(ALLEGRO_EVENT ev);26 bool handleEvent(ALLEGRO_EVENT e); 27 27 }; 28 28 -
client/Client/main.cpp
r439f7bc r87b3ee2 18 18 #include <string> 19 19 #include <iostream> 20 #include <vector>21 20 22 21 #include <allegro5/allegro.h> … … 26 25 #include "../../common/message.h" 27 26 27 #include "Window.h" 28 #include "Textbox.h" 29 #include "Button.h" 28 30 #include "chat.h" 29 31 … … 36 38 void initWinSock(); 37 39 void shutdownWinSock(); 38 void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole, string &username); 40 void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole); 41 42 // callbacks 43 void registerAccount(); 44 void login(); 45 void logout(); 46 void quit(); 47 void sendChatMessage(); 39 48 40 49 void error(const char *); … … 53 62 enum STATE { 54 63 STATE_START, 55 STATE_LOGIN, 56 STATE_LOGOUT 64 STATE_LOGIN // this means you're already logged in 57 65 }; 66 67 int state; 68 69 bool doexit; 70 71 Window* wndLogin; 72 Window* wndMain; 73 Window* wndCurrent; 74 75 Textbox* txtUsername; 76 Textbox* txtPassword; 77 Textbox* txtChat; 78 79 int sock; 80 struct sockaddr_in server, from; 81 struct hostent *hp; 82 NETWORK_MSG msgTo, msgFrom; 83 string username; 84 chat chatConsole; 58 85 59 86 int main(int argc, char **argv) … … 63 90 ALLEGRO_TIMER *timer = NULL; 64 91 ALLEGRO_BITMAP *bouncer = NULL; 92 bool key[4] = { false, false, false, false }; 93 bool redraw = true; 94 doexit = false; 95 65 96 float bouncer_x = SCREEN_W / 2.0 - BOUNCER_SIZE / 2.0; 66 97 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; 70 71 int state = STATE_START; 72 73 chat chatConsole; 98 99 state = STATE_START; 74 100 75 101 if(!al_init()) { … … 78 104 } 79 105 106 al_init_primitives_addon(); 80 107 al_init_font_addon(); 81 108 al_init_ttf_addon(); … … 93 120 return -1; 94 121 } 122 123 if(!al_install_mouse()) { 124 fprintf(stderr, "failed to initialize the mouse!\n"); 125 return -1; 126 } 95 127 96 128 timer = al_create_timer(1.0 / FPS); … … 106 138 return -1; 107 139 } 108 140 141 wndLogin = new Window(0, 0, SCREEN_W, SCREEN_H); 142 wndLogin->addComponent(new Textbox(104, 40, 100, 20, font)); 143 wndLogin->addComponent(new Textbox(104, 70, 100, 20, font)); 144 wndLogin->addComponent(new Button(22, 100, 90, 20, font, "Register", registerAccount)); 145 wndLogin->addComponent(new Button(122, 100, 60, 20, font, "Login", login)); 146 wndLogin->addComponent(new Button(540, 10, 80, 20, font, "Quit", quit)); 147 148 txtUsername = (Textbox*)wndLogin->getComponent(0); 149 txtPassword = (Textbox*)wndLogin->getComponent(1); 150 151 wndMain = new Window(0, 0, SCREEN_W, SCREEN_H); 152 wndMain->addComponent(new Textbox(95, 40, 525, 20, font)); 153 wndMain->addComponent(new Button(95, 70, 160, 20, font, "Send Message", sendChatMessage)); 154 wndMain->addComponent(new Button(540, 10, 80, 20, font, "Logout", logout)); 155 156 txtChat = (Textbox*)wndMain->getComponent(0); 157 158 wndCurrent = wndLogin; 159 109 160 bouncer = al_create_bitmap(BOUNCER_SIZE, BOUNCER_SIZE); 110 161 if(!bouncer) { … … 131 182 132 183 al_register_event_source(event_queue, al_get_display_event_source(display)); 133 134 184 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 135 136 185 al_register_event_source(event_queue, al_get_keyboard_event_source()); 186 al_register_event_source(event_queue, al_get_mouse_event_source()); 137 187 138 188 al_clear_to_color(al_map_rgb(0,0,0)); 139 189 140 190 al_flip_display(); 141 142 int sock;143 struct sockaddr_in server, from;144 struct hostent *hp;145 NETWORK_MSG msgTo, msgFrom;146 string username;147 191 148 192 if (argc != 3) { … … 171 215 ALLEGRO_EVENT ev; 172 216 al_wait_for_event(event_queue, &ev); 173 174 if(ev.type == ALLEGRO_EVENT_TIMER) { 217 218 if(wndCurrent->handleEvent(ev)) { 219 // do nothing 220 } 221 else if(ev.type == ALLEGRO_EVENT_TIMER) { 175 222 if(key[KEY_UP] && bouncer_y >= 4.0) { 176 223 bouncer_y -= 4.0; … … 195 242 } 196 243 else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) { 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()); 204 205 switch(state) 206 { 207 case STATE_START: 208 { 209 username = input; 210 strcpy(msgTo.buffer+input.size()+1, "MyPassword"); 211 msgTo.type = MSG_TYPE_REGISTER; 212 //msgTo.type = MSG_TYPE_LOGIN; 213 214 sendMessage(&msgTo, sock, &server); 215 receiveMessage(&msgFrom, sock, &from); 216 processMessage(msgFrom, state, chatConsole, username); 217 cout << "state: " << state << endl; 218 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; 232 233 sendMessage(&msgTo, sock, &server); 234 receiveMessage(&msgFrom, sock, &from); 235 processMessage(msgFrom, state, chatConsole, username); 236 cout << "state: " << state << endl; 237 238 break; 239 } 240 case STATE_LOGOUT: 241 { 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; 245 246 break; 247 } 248 default: 249 { 250 cout << "The state has an invalid value: " << state << endl; 251 252 break; 253 } 254 } 255 } 256 }else { 257 switch(ev.keyboard.keycode) { 258 case ALLEGRO_KEY_UP: 259 key[KEY_UP] = true; 260 break; 261 262 case ALLEGRO_KEY_DOWN: 263 key[KEY_DOWN] = true; 264 break; 265 266 case ALLEGRO_KEY_LEFT: 267 key[KEY_LEFT] = true; 268 break; 269 270 case ALLEGRO_KEY_RIGHT: 271 key[KEY_RIGHT] = true; 272 break; 273 } 244 switch(ev.keyboard.keycode) { 245 case ALLEGRO_KEY_UP: 246 key[KEY_UP] = true; 247 break; 248 249 case ALLEGRO_KEY_DOWN: 250 key[KEY_DOWN] = true; 251 break; 252 253 case ALLEGRO_KEY_LEFT: 254 key[KEY_LEFT] = true; 255 break; 256 257 case ALLEGRO_KEY_RIGHT: 258 key[KEY_RIGHT] = true; 259 break; 274 260 } 275 261 } … … 301 287 redraw = false; 302 288 303 al_clear_to_color(al_map_rgb(0,0,0));289 wndCurrent->draw(display); 304 290 305 291 al_draw_bitmap(bouncer, bouncer_x, bouncer_y, 0); 306 292 307 293 chatConsole.draw(font, al_map_rgb(255,255,255)); 294 295 if(wndCurrent == wndLogin) { 296 al_draw_text(font, al_map_rgb(0, 255, 0), 4, 43, ALLEGRO_ALIGN_LEFT, "Username:"); 297 al_draw_text(font, al_map_rgb(0, 255, 0), 1, 73, ALLEGRO_ALIGN_LEFT, "Password:"); 298 } 299 else if(wndCurrent == wndMain) { 300 al_draw_text(font, al_map_rgb(0, 255, 0), 4, 43, ALLEGRO_ALIGN_LEFT, "Message:"); 301 } 308 302 309 303 al_flip_display(); … … 319 313 shutdownWinSock(); 320 314 315 delete wndLogin; 316 delete wndMain; 317 321 318 al_destroy_event_queue(event_queue); 322 319 al_destroy_bitmap(bouncer); … … 360 357 } 361 358 362 void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole , string &username)359 void processMessage(NETWORK_MSG &msg, int &state, chat &chatConsole) 363 360 { 364 361 string response = string(msg.buffer); … … 372 369 chatConsole.addLine(response); 373 370 374 /* 375 if (response.compare("Player has already logged in.") == 0) 371 switch(msg.type) 376 372 { 377 cout << "User login failed" << endl; 378 username.clear(); 379 } 380 else 381 { 382 cout << "User login successful" << endl; 383 state = STATE_LOGIN; 384 } 385 */ 373 case MSG_TYPE_REGISTER: 374 { 375 break; 376 } 377 case MSG_TYPE_LOGIN: 378 { 379 if (response.compare("Player has already logged in.") == 0) 380 { 381 username.clear(); 382 cout << "User login failed" << endl; 383 } 384 else if (response.compare("Incorrect username or password") == 0) 385 { 386 username.clear(); 387 cout << "User login failed" << endl; 388 } 389 else 390 { 391 state = STATE_LOGIN; 392 wndCurrent = wndMain; 393 cout << "User login successful" << endl; 394 } 395 break; 396 } 397 } 386 398 387 399 break; … … 391 403 chatConsole.addLine(response); 392 404 393 if (response.compare("You have successfully logged out. You may quit the game.") == 0)405 switch(msg.type) 394 406 { 395 cout << "Logged out" << endl; 396 state = STATE_LOGOUT; 397 } 398 else 399 { 400 cout << "Added new line" << endl; 407 case MSG_TYPE_REGISTER: 408 { 409 break; 410 } 411 case MSG_TYPE_LOGIN: 412 { 413 if (response.compare("You have successfully logged out.") == 0) 414 { 415 cout << "Logged out" << endl; 416 state = STATE_START; 417 wndCurrent = wndLogin; 418 } 419 else 420 { 421 cout << "Added new line" << endl; 422 } 423 424 break; 425 } 401 426 } 402 427 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 428 break; 410 429 } … … 417 436 } 418 437 } 438 439 void registerAccount() 440 { 441 string username = txtUsername->getStr(); 442 string password = txtPassword->getStr(); 443 444 txtUsername->clear(); 445 txtPassword->clear(); 446 447 msgTo.type = MSG_TYPE_REGISTER; 448 449 strcpy(msgTo.buffer, username.c_str()); 450 strcpy(msgTo.buffer+username.size()+1, password.c_str()); 451 452 sendMessage(&msgTo, sock, &server); 453 receiveMessage(&msgFrom, sock, &from); 454 processMessage(msgFrom, state, chatConsole); 455 cout << "state: " << state << endl; 456 } 457 458 void login() 459 { 460 string strUsername = txtUsername->getStr(); 461 string strPassword = txtPassword->getStr(); 462 username = strUsername; 463 464 txtUsername->clear(); 465 txtPassword->clear(); 466 467 msgTo.type = MSG_TYPE_LOGIN; 468 469 strcpy(msgTo.buffer, strUsername.c_str()); 470 strcpy(msgTo.buffer+username.size()+1, strPassword.c_str()); 471 472 sendMessage(&msgTo, sock, &server); 473 receiveMessage(&msgFrom, sock, &from); 474 processMessage(msgFrom, state, chatConsole); 475 cout << "state: " << state << endl; 476 } 477 478 void logout() 479 { 480 txtChat->clear(); 481 482 msgTo.type = MSG_TYPE_LOGOUT; 483 484 strcpy(msgTo.buffer, username.c_str()); 485 486 sendMessage(&msgTo, sock, &server); 487 receiveMessage(&msgFrom, sock, &from); 488 processMessage(msgFrom, state, chatConsole); 489 } 490 491 void quit() 492 { 493 doexit = true; 494 } 495 496 void sendChatMessage() 497 { 498 string msg = txtChat->getStr(); 499 500 txtChat->clear(); 501 502 msgTo.type = MSG_TYPE_CHAT; 503 504 strcpy(msgTo.buffer, msg.c_str()); 505 506 sendMessage(&msgTo, sock, &server); 507 receiveMessage(&msgFrom, sock, &from); 508 processMessage(msgFrom, state, chatConsole); 509 } -
client/makefile
r439f7bc r87b3ee2 1 client : ../common/message.cpp Client/main.cpp1 gameClient : ../common/message.cpp Client/main.cpp 2 2 g++ -o $@ $?
Note:
See TracChangeset
for help on using the changeset viewer.