init.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* $Id: init.c,v 1.26 2001/04/29 22:24:37 kilobug Exp $ */
  2. #include "client.h"
  3. #define DEFAULT_PORT 4242
  4. #define DEFAULT_HOST "localhost"
  5. conf_t *gl_config = NULL;
  6. static void clean()
  7. {
  8. send_goodbye(gl_config);
  9. }
  10. static void clean_exit()
  11. {
  12. clean();
  13. exit(1);
  14. }
  15. static void help(const char *name)
  16. {
  17. printf("syntaxe: %s [-d] [-h machine] [-p port]\n", name);
  18. exit(1);
  19. }
  20. static const char *get_arg(int argc, char **argv, int *i)
  21. {
  22. if (*i == argc - 1)
  23. {
  24. printf("%s requiert un argument.\n", argv[*i]);
  25. exit(1);
  26. }
  27. return (argv[++(*i)]);
  28. }
  29. static void sigusr2()
  30. {
  31. gl_config->terminated = TRUE;
  32. if (gl_config->ready)
  33. allow_new_turn(gl_config);
  34. }
  35. static guchar *gen_convtb()
  36. {
  37. guchar *res;
  38. int org, add;
  39. double f;
  40. res = g_malloc(256 * 256 * sizeof(*res));
  41. for (org = 0; org < 256; org++)
  42. for (add = 0; add < 256; add++)
  43. {
  44. /* f = ((double) org / 256);
  45. f = (0.2 + 0.8 * SQR(f)) * add / 1.3;*/
  46. f = add * 0.3;
  47. res[org + add * 256] = MIN(org + f, 255);
  48. }
  49. return res;
  50. }
  51. conf_t *init(int argc, char **argv)
  52. {
  53. conf_t *res;
  54. int i;
  55. gtk_set_locale();
  56. gtk_init(&argc, &argv);
  57. gdk_set_use_xshm(TRUE);
  58. gdk_rgb_init();
  59. res = g_malloc(sizeof(*res));
  60. res->screen = NULL;
  61. res->debug = FALSE;
  62. res->socket = -1;
  63. res->host = DEFAULT_HOST;
  64. res->port = DEFAULT_PORT;
  65. res->gdk_io_id = -1;
  66. res->map = NULL;
  67. res->convtb = gen_convtb();
  68. res->turn.mode = tt_pause;
  69. res->turn.nbt = 10;
  70. res->turn.spd = 50;
  71. res->connected = res->ready = FALSE;
  72. res->window = res->status = res->mini = res->main = res->conf = NULL;
  73. res->stat.r4d2 = res->stat.akx = res->stat.pl = res->stat.stat = NULL;
  74. res->stat.menu = NULL;
  75. res->stat.on = FALSE;
  76. for (i = 1; i < argc; i++)
  77. {
  78. if (argv[i][0] != '-')
  79. help(argv[0]);
  80. switch (argv[i][1])
  81. {
  82. case 'd':
  83. res->debug = TRUE;
  84. break;
  85. case 'h':
  86. res->host = get_arg(argc, argv, &i);
  87. break;
  88. case 'p':
  89. res->port = atoi(get_arg(argc, argv, &i));
  90. break;
  91. default:
  92. help(argv[0]);
  93. }
  94. }
  95. res->host = g_strdup(res->host);
  96. res->background = load_picture("sand.gif", NULL);
  97. res->akx = load_picture("akx.xpm", "akx_mask.xpm");
  98. res->r4d2 = load_picture("r4d2.xpm", "r4d2_mask.xpm");
  99. res->big_ofs_x = res->big_ofs_y = 0;
  100. res->mini_center_x = res->mini_center_y = 0;
  101. res->big_zoom = 50.0;
  102. res->log_big_zoom = 4;
  103. res->big_radar = FALSE;
  104. res->big_pulse = TRUE;
  105. net_set_error_handler(network_error, res);
  106. gl_config = res;
  107. atexit(clean);
  108. signal(SIGINT, clean_exit);
  109. signal(SIGUSR2, sigusr2);
  110. return res;
  111. }
  112. gint idle_connect(void *conf)
  113. {
  114. do_connect(conf);
  115. return FALSE;
  116. }
  117. void callback_connect(GtkWidget *widget, void *conf)
  118. {
  119. do_connect(conf);
  120. }