Advertisement
Guest User

Untitled

a guest
Dec 12th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.00 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <arpa/inet.h>
  5.  
  6. #include <SDL2/SDL.h>
  7. #include <SDL2/SDL_ttf.h>
  8.  
  9. #define BUF_LEN 1024
  10. #define ACTIVE_TEXTS_LEN 10
  11.  
  12. struct text_t {
  13.     char *text;
  14.     SDL_Surface* text_surface;
  15.     SDL_Texture* texture;
  16.     SDL_Rect dstrect; /* destination rectangle */
  17. };
  18.  
  19. struct cell_t {
  20.     int x;
  21.     int y;
  22.     char *hover_text;
  23. };
  24.  
  25. struct data_t {
  26.     int w;
  27.     int h;
  28.         int cell_size;
  29.     int fullscreen;
  30.    
  31.     char *name;
  32.     char *server_ip;
  33.     int server_port;
  34.    
  35.     /* these variables are set by the server. */
  36.     int map_w;
  37.     int map_h;
  38.     int max_update_rate;
  39. };
  40.  
  41. void parse_cfg_file(FILE *cfg_file, struct data_t *cfg);
  42. int set_buffer(char *buf, char *str);
  43.  
  44. /* SDL stuff */
  45. int init(struct data_t *cfg);
  46. void quit();
  47. struct text_t init_text(int x, int y, int r, int g, int b, int a, char *text);
  48. void render_map(struct cell_t **map, struct data_t *cfg);
  49.  
  50. //The window we'll be rendering to
  51. SDL_Window* window = NULL;
  52.  
  53. //The window renderer
  54. SDL_Renderer* renderer = NULL;
  55.  
  56. //Globally used font
  57. TTF_Font *gFont = NULL;
  58.  
  59.  
  60. int main(int argc, char **argv)
  61. {
  62.     if (argc < 2) {
  63.         fprintf(stderr, "usage: 2drust <config file>\n");
  64.         return 1;
  65.     }
  66.    
  67.     struct data_t cfg;
  68.     cfg.w = 0;
  69.     cfg.h = 0;
  70.     cfg.cell_size = 0;
  71.     cfg.fullscreen = 0;
  72.     cfg.name = NULL;
  73.     cfg.server_ip = NULL;
  74.    
  75.     FILE *cfg_file = NULL;
  76.     cfg_file = fopen(argv[1], "r");
  77.     if (cfg_file == NULL) {
  78.         fprintf(stderr, "ERR: file read error on config file\n");
  79.         return 1;
  80.     }
  81.     parse_cfg_file(cfg_file, &cfg);
  82.     fclose(cfg_file);
  83.    
  84.     puts("CLIENT VARIABLES:");
  85.     printf("width = %d\n", cfg.w);
  86.     printf("height = %d\n", cfg.h);
  87.     printf("fullscreen = %d\n", cfg.fullscreen);
  88.     printf("name = %s\n", cfg.name);
  89.     printf("server_ip = %s\n", cfg.server_ip); 
  90.     printf("server_port = %d\n", cfg.server_port);
  91.     puts("");
  92.    
  93.     /* server connection stuff */
  94.     char buf[BUF_LEN];
  95.     int sock = socket(PF_INET, SOCK_DGRAM, 0);
  96.     struct sockaddr_in server;
  97.     server.sin_family = AF_INET;
  98.     server.sin_port = htons(cfg.server_port); /* htons converts between little and big endian */
  99.     server.sin_addr.s_addr = inet_addr(cfg.server_ip); /* retarded format. it's their spec, not mine. */
  100.     memset(server.sin_zero, '\0', sizeof server.sin_zero); /* this is also retarded */
  101.     int server_size = sizeof(server);
  102.     int len = 0;
  103.    
  104.     len = set_buffer(buf, "a"); /* "a" is for initial request, server returns its variables */
  105.  
  106.     /* for some reason they insist that the struct be const. this means we have to do
  107.     the autistic casting you see below if you want it to compile. */
  108.     sendto(sock, buf, len, 0,(struct sockaddr *) &server, server_size); /* retarded. */
  109.     memset(buf, '\0', BUF_LEN); /* clear the buffer */
  110.     len = recvfrom(sock, buf, BUF_LEN, 0, NULL, NULL);
  111.     //printf("SERVER SAYS: %s\n", buf);
  112.     puts("SERVER VARIABLES:");
  113.     sscanf(buf, "%dx%d:%d",&cfg.map_w, &cfg.map_h, &cfg.max_update_rate);
  114.     printf("map_w = %d\nmap_h = %d\nmax_update_rate = %d\n", cfg.map_w, cfg.map_h, cfg.max_update_rate);
  115.    
  116.     /* now take the data we got from the server and
  117.     allocate a 2d array from it */
  118.     struct cell_t **map = NULL;
  119.     map = malloc(cfg.map_w * sizeof(struct cell_t*));
  120.     if (map == NULL) {
  121.         fprintf(stderr, "ERR: malloc failed");
  122.         return 1;
  123.     }
  124.     int i;
  125.     int j;
  126.     for (i = 0; i < cfg.map_w; i++) {
  127.         map[i] = malloc(cfg.map_h * sizeof(struct cell_t));
  128.         if (map[i] == NULL) {
  129.             fprintf(stderr, "ERR: malloc failed");
  130.             return 1;
  131.         }
  132.         for (j = 0; j < cfg.map_h; j++) {
  133.             map[i][j].x = cfg.cell_size * i;
  134.             map[i][j].y = cfg.cell_size * j;
  135.             map[i][j].hover_text = NULL;
  136.         }
  137.         }
  138.        
  139.         /*
  140.         for (i = 0; i < cfg.map_w; i++) {
  141.             for (j = 0; j < cfg.map_h; j++) {
  142.                 printf("%d,%d\n", map[i][j].x, map[i][j].y);
  143.         }
  144.     } */
  145.  
  146.  
  147.    
  148.     /* SDL Stuff */
  149.     /*************/
  150.     init(&cfg); /* this inits all the sdl crap */
  151.    
  152.     struct text_t *active_text[ACTIVE_TEXTS_LEN] = {0};
  153.    
  154.     struct text_t t1 = init_text(50, 50, 255, 0, 255, 255, "Kill yourself.");
  155.     active_text[0] = &t1;
  156.    
  157.     struct text_t t2 = init_text(150, 150, 255, 0, 0, 255, "F for fullscreen toggle\n Y for chat.");
  158.     active_text[1] = &t2;
  159.    
  160.     int should_quit = 0;
  161.     //int picker = 0;
  162.     SDL_Event event;
  163.    
  164.     int fullscreen = 0;
  165.     int txt_input_enabled = 0;
  166.     char text_buf[256];
  167.    
  168.    
  169.     while (!should_quit) {
  170.     /*SDL_GetMouseState(&x, &y);
  171.     x /= 25;
  172.     y /= 25;
  173.     printf("hovering on box %d,%d\n", x, y); */
  174.         while( SDL_PollEvent( &event ) != 0 ) {
  175.             /* User requests quit */
  176.             switch(event.type) {
  177.             case SDL_QUIT:
  178.                 should_quit = 1;
  179.                 break;
  180.             case SDL_KEYDOWN:
  181.                 if (txt_input_enabled) {
  182.                     const char *key = SDL_GetKeyName(event.key.keysym.sym);
  183.                     if (event.key.keysym.sym == SDLK_SPACE) {
  184.                         key = " ";
  185.                     }
  186.                     if (strlen(key) == 1) {
  187.                         printf("key: %s\n", key);
  188.                         strncat(text_buf, key, 1);
  189.                     }
  190.                 }
  191.                 switch(event.key.keysym.sym) {
  192.                 case SDLK_UP:
  193.                     active_text[0]->dstrect.y--;
  194.                     break;
  195.                 case SDLK_DOWN:
  196.                     active_text[0]->dstrect.y++;
  197.                     break;
  198.                 case SDLK_LEFT:
  199.                     active_text[0]->dstrect.x--;
  200.                     break;
  201.                 case SDLK_RIGHT:
  202.                     active_text[0]->dstrect.x++;
  203.                     break;
  204.                 default: /* any other key */
  205.                     break;
  206.                 } /* terminate switch */
  207.                 break;
  208.             case SDL_KEYUP:
  209.                 switch(event.key.keysym.sym) {
  210.                 case SDLK_f:
  211.                     if (fullscreen) {
  212.                         SDL_SetWindowFullscreen(window, 0);
  213.                         fullscreen = 0;
  214.                     } else {
  215.                         SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
  216.                         fullscreen = 1;
  217.                     }
  218.                     break;
  219.                 case SDLK_y:
  220.                     if (!txt_input_enabled) {
  221.                         txt_input_enabled = 1;
  222.                     }
  223.                     break;
  224.                 case SDLK_RETURN:
  225.                     if (txt_input_enabled) {
  226.                         txt_input_enabled = 0;
  227.                         printf("%s\n", text_buf);
  228.                     }
  229.                 }
  230.                 break;
  231.            /* case SDL_TEXTINPUT:
  232.                  Add new text onto the end of our text *
  233.                 printf("KEY:%s\n",event.text.text);
  234.                 strcat(text, event.text.text);
  235.                 break; */
  236.             } /* terminate event switch */
  237.         }
  238.         /* Clear screen */
  239.         SDL_SetRenderDrawColor( renderer, 0, 0, 0, 255 );
  240.         SDL_RenderClear( renderer ); /*
  241.         if ( (SDL_GetTicks() % 50) == 0) {
  242.             int x = active_text[0]->dstrect.x, y = active_text[0]->dstrect.y;
  243.             SDL_DestroyTexture(active_text[0]->texture);
  244.             SDL_FreeSurface(active_text[0]->text_surface);
  245.             if (picker == 0) {
  246.                
  247.                 t1 = init_text(x, y, 100, 0, 255, 255, "None of that halo");
  248.                 picker = 1;
  249.             } else {
  250.                 t1 = init_text(x, y, 255, 0, 255, 255, "No games.");
  251.                 picker = 0;
  252.             }
  253.             active_text[0] = &t1;
  254.         }
  255.        
  256.  
  257.         for (i = 0; i < ACTIVE_TEXTS_LEN; i++) {
  258.             if (active_text[i] != 0) {
  259.                 SDL_RenderCopy(renderer, active_text[i]->texture, NULL, &active_text[i]->dstrect);
  260.             }
  261.         }*/
  262.         render_map(map, &cfg);
  263.         //Update screen
  264.         SDL_RenderPresent(renderer);
  265.        // should_quit = 1;
  266.     } /* terminate game loop */
  267.  
  268.     quit(); /* quits all the SDL stuff */
  269.     //int j;
  270.     for (i = 0; i < cfg.map_w; i++) {
  271.         free(map[i]);
  272.     }
  273.     free(map);
  274.     free(cfg.name);
  275.     free(cfg.server_ip);
  276.     return 0;
  277. }
  278.  
  279. /* returns len of info in buffer */
  280. int set_buffer(char *buf, char *str)
  281. {
  282.     int i;
  283.     for (i = 0; str[i]; i++) {
  284.         buf[i] = str[i];
  285.     }
  286.     return i;
  287. }
  288.  
  289. void parse_cfg_file(FILE *cfg_file, struct data_t *cfg)
  290. {
  291.     char buf[BUF_LEN];
  292.     char key[BUF_LEN];
  293.     char *tok = NULL;
  294.     int iskey = 1;
  295.    
  296.     while ( fgets(buf, BUF_LEN, cfg_file) ) {
  297.         if (buf[0] == '#') {
  298.             continue;
  299.         }else if ( (tok = strstr(buf, "#")) ) {
  300.             tok[0] = '\0';
  301.         }
  302.         tok = strtok(buf, ":\n ");
  303.         iskey = 1;
  304.         do {
  305.             if (iskey) {
  306.                 strncpy(key, tok, strlen(tok) + 1); /* +1 for that pesky NULL */
  307.                 iskey = 0;
  308.             } else {
  309.                 switch(key[0]) {
  310.                 case 'n':
  311.                     cfg->name = malloc( strlen(tok) + 1 ); /* +1 for that pesky NULL */
  312.                     if (cfg->name == NULL) {
  313.                         fprintf(stderr, "ERR: malloc failed on name\n");
  314.                         exit(EXIT_FAILURE);
  315.                     }
  316.                     strncpy(cfg->name, tok, strlen(tok) + 1); /* +1 for that pesky NULL */
  317.                     break;
  318.                 case 's':
  319.                     cfg->server_ip = malloc( strlen(tok) + 1 ); /* +1 for that pesky NULL */
  320.                     if (cfg->server_ip == NULL) {
  321.                         fprintf(stderr, "ERR: malloc failed on server ip\n");
  322.                         exit(EXIT_FAILURE);
  323.                     }
  324.                     strncpy(cfg->server_ip, tok, strlen(tok) + 1); /* +1 for that pesky NULL */
  325.                     break;
  326.                 case 'p':
  327.                     sscanf(tok, "%d", &cfg->server_port);
  328.                     break;
  329.                 case 'w':
  330.                     sscanf(tok, "%d", &cfg->w);
  331.                     break;
  332.                 case 'h':
  333.                     sscanf(tok, "%d", &cfg->h);
  334.                     break;
  335.                 case 'c':
  336.                     sscanf(tok, "%d", &cfg->cell_size);
  337.                     break;
  338.                 case 'f':
  339.                     sscanf(tok, "%d", &cfg->fullscreen);
  340.                     if (cfg->fullscreen > 0) {
  341.                         cfg->fullscreen = 1;
  342.                     } else {
  343.                         cfg->fullscreen = 0;
  344.                     }
  345.                     break;
  346.                 }
  347.             }
  348.         } while ( (tok = strtok(NULL, ":\n ")) );
  349.     }
  350. }
  351.  
  352. struct text_t init_text(int x, int y, int r, int g, int b, int a, char *text)
  353. {
  354.     struct text_t thetext;
  355.     SDL_Color color = {r, g, b, a};
  356.     thetext.text_surface = TTF_RenderText_Solid( gFont, text, color );
  357.     if (thetext.text_surface == NULL) {
  358.         printf( "Unable to create text surface! SDL_ttf Error: %s\n", TTF_GetError() );
  359.     }
  360.     thetext.texture = SDL_CreateTextureFromSurface( renderer, thetext.text_surface );
  361.  
  362.     int w,h;
  363.     SDL_QueryTexture(thetext.texture, NULL, NULL, &w, &h);
  364.     SDL_Rect dstrect = { x, y, w, h };
  365.     thetext.dstrect = dstrect;
  366.    
  367.     return thetext;
  368. }
  369.  
  370. void render_map(struct cell_t **map, struct data_t *cfg)
  371. {
  372.     SDL_Rect rect;
  373.     int i;
  374.     int j;
  375.     SDL_SetRenderDrawColor( renderer, 0, 255, 0, 255 );
  376.     for (i = 0; i < cfg->map_w; i++) {
  377.         for (j = 0; j < cfg->map_h; j++) {
  378.            
  379.             rect.x = map[i][j].x;
  380.             rect.y = map[i][j].y;
  381.             rect.w = cfg->cell_size;
  382.             rect.h = cfg->cell_size;
  383.                 SDL_RenderDrawRect(renderer, &rect);
  384.         }
  385.     }
  386. }
  387.  
  388.  
  389. int init(struct data_t *cfg)
  390. {
  391.     /* Initialize SDL */
  392.     if (SDL_Init( SDL_INIT_VIDEO ) < 0) {
  393.         printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
  394.         return 1;
  395.     }
  396.  
  397.     /* Create window */
  398.     window = SDL_CreateWindow( "IQ Simulator 2k17", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, cfg->w, cfg->h, SDL_WINDOW_SHOWN );
  399.     if( window == NULL ) {
  400.         printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
  401.         return 1;
  402.     }
  403.  
  404.     /* Create renderer for window */
  405.     renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED );
  406.     if( renderer == NULL ) {
  407.         printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError());
  408.         return 1;
  409.     }
  410.     if (TTF_Init() == -1) {
  411.         printf("SDL_ttf could not initialized! SDL_ttf Error: %s\n", TTF_GetError() );
  412.         return 1;
  413.     }
  414.     gFont = TTF_OpenFont("DejaVuSans.ttf", 18);
  415.     if (gFont == NULL) {
  416.         printf("Failed to load font! SDL_ttf Error: %s\n", TTF_GetError());
  417.         return 1;
  418.     }
  419.    
  420.     return 0;
  421. }
  422.  
  423. void quit()
  424. {
  425.     /* Destroy renderer and window */
  426.     SDL_DestroyRenderer( renderer );
  427.     SDL_DestroyWindow( window );
  428.     window = NULL;
  429.     renderer = NULL;
  430.  
  431.     /* Quit SDL subsystems */
  432.     TTF_Quit();
  433.     SDL_Quit();
  434. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement