Advertisement
Guest User

Untitled

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