Advertisement
Guest User

Untitled

a guest
May 7th, 2015
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.91 KB | None | 0 0
  1. #include <allegro5/allegro.h>
  2. #include <allegro5/allegro_primitives.h>
  3. #include <arpa/inet.h>
  4. #include <netinet/in.h>
  5. #include <stdio.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <unistd.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <pthread.h>
  12. #include <iostream>
  13. #define BUFLEN 512
  14. #define PORT 9930
  15.  
  16. char buf[BUFLEN];
  17.  
  18. struct posxy
  19.     {
  20.     int x;
  21.     int y;
  22.     };
  23.  
  24. struct posxy player_1;
  25. struct posxy player_2;
  26.  
  27.  
  28. void err(const char *s)
  29. {
  30.     perror(s);
  31.     exit(1);
  32. }
  33.  
  34. void *game(void *data)
  35. {
  36.     std::cout<<(int*)data<<std::endl;
  37.     ALLEGRO_DISPLAY *display;
  38.     ALLEGRO_EVENT_QUEUE *event_queue;
  39.     ALLEGRO_EVENT events;
  40.     ALLEGRO_KEYBOARD_STATE keyState;
  41.  
  42.    
  43.  
  44.     int x = 0, y = 0;
  45.     int x1 = -10 , y1 = -20;
  46.  
  47.    
  48.  
  49.     al_init();
  50.     al_set_new_display_flags(ALLEGRO_WINDOWED);
  51.  
  52.     display = al_create_display(800, 600);
  53.  
  54.     event_queue = al_create_event_queue();
  55.  
  56.     al_set_window_title(display, "GameClient");
  57.  
  58.     al_set_window_position(display, 200, 100);
  59.  
  60.     al_init_primitives_addon();
  61.  
  62.     al_install_keyboard();
  63.    
  64.  
  65.     ALLEGRO_TIMER *timer = al_create_timer(1.0 / 60);
  66.  
  67.     al_register_event_source(event_queue, al_get_timer_event_source(timer));
  68.     al_register_event_source(event_queue, al_get_display_event_source(display));
  69.     al_register_event_source(event_queue, al_get_keyboard_event_source());
  70.    
  71.  
  72.     al_start_timer(timer);
  73.     while(1)
  74.     {              
  75.        
  76.         al_wait_for_event(event_queue, &events);
  77.         if (events.type == ALLEGRO_EVENT_TIMER)
  78.         {    
  79.            
  80.             al_get_keyboard_state(&keyState);
  81.             if (al_key_down(&keyState, ALLEGRO_KEY_DOWN))
  82.             {                
  83.                 y += 5;
  84.                                
  85.             }
  86.             else if (al_key_down(&keyState, ALLEGRO_KEY_UP))
  87.             {                  
  88.                 y -= 5;          
  89.                
  90.             }
  91.             else if (al_key_down(&keyState, ALLEGRO_KEY_RIGHT))
  92.             {                
  93.                  x += 5;        
  94.                            
  95.             }
  96.             else if (al_key_down(&keyState, ALLEGRO_KEY_LEFT))
  97.             {                  
  98.                  x -= 5;                        
  99.             }
  100.             player_1.x = x;
  101.             player_1.y = y;              
  102.            
  103.     }
  104.     al_clear_to_color(al_map_rgb(0, 0, 0));
  105.     al_draw_rectangle(player_2.x, player_2.y,player_2.x + 32, player_2.y + 32, al_map_rgb(255,0,255), 1);
  106.     al_draw_rectangle(x, y, x + 32, y + 32, al_map_rgb(255,0,255), 1);
  107.     al_flip_display();
  108.    
  109. }
  110. }
  111.  
  112. void *multiplayer(void *data)
  113. {
  114.     struct sockaddr_in serv_addr;
  115.     int sockfd, i;
  116.     socklen_t slen = sizeof(serv_addr);
  117.    
  118.  
  119.    
  120.  
  121.     if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
  122.         err("socket");
  123.  
  124.     bzero(&serv_addr, sizeof(serv_addr));
  125.     serv_addr.sin_family = AF_INET;
  126.     serv_addr.sin_port = htons(PORT);
  127.     if (inet_aton("127.0.0.1", &serv_addr.sin_addr)==0)
  128.     {
  129.         fprintf(stderr, "inet_aton() failed\n");
  130.         exit(1);
  131.     }
  132.  
  133.     while(1)
  134.     {                            
  135.  
  136.         if (sendto(sockfd, &player_1, sizeof(struct posxy), 0, (struct sockaddr*)&serv_addr, slen)==-1)
  137.             err("sendto()");
  138.         if (recvfrom(sockfd, &player_2, sizeof(struct posxy), 0, (struct sockaddr*)&serv_addr, &slen) == -1)
  139.             err("recvfrom()");                
  140.         /*printf("Received packet from %s:%d\nData: %s\n\n",
  141.                inet_ntoa(serv_addr.sin_addr), ntohs(serv_addr.sin_port), buf);*/
  142.     }
  143.  
  144.     close(sockfd);
  145. }
  146.  
  147. int main(int argc, char** argv)
  148. {
  149.     buf[0] = 0;
  150. pthread_t t1, t2;
  151.  
  152.    int x = 0;
  153.  
  154.    pthread_create(&t1, NULL, multiplayer, (void *)(&x));
  155.    pthread_create(&t2, NULL, game, (void *)(&x));
  156.  
  157.    pthread_join(t1, NULL);
  158.    pthread_join(t2, NULL);
  159.    
  160.     return 0;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement