display.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include <grass/gis.h>
  6. #include <grass/raster.h>
  7. #include <grass/display.h>
  8. #include "cmd_line.h"
  9. #include "local_proto.h"
  10. #define SIZE 80
  11. static char buf[SIZE], old_time[SIZE], cur_time[SIZE];
  12. static double f2s_x, f2s_y; /*file-to-screen factors */
  13. static int old_value = 0;
  14. static int xoffset, yoffset; /*screen coors of u-l corner of file */
  15. static int x1, y1, x2, y2; /*diagonal pts of a box */
  16. static int x1_st, y1_st, x2_st, y2_st; /*pts of the elapsed Spread Time box */
  17. static int x1_ct, y1_ct, x2_ct, y2_ct; /*pts of the Current Time box */
  18. static struct Colors colors;
  19. static struct tm *t_time;
  20. static time_t c_time;
  21. void display_init(void)
  22. {
  23. extern struct Cell_head window;
  24. extern int nrows, ncols; /*numbers of rows and columns in file */
  25. double t, b, l, r; /*top, bottom, left and right of frame */
  26. int width, height;
  27. /*set time zone for tracing local time */
  28. tzset();
  29. if (R_open_driver() != 0) {
  30. G_fatal_error("couldn't open display");
  31. }
  32. D_setup(1);
  33. D_get_screen_window(&t, &b, &l, &r);
  34. /*printf("\nt,b,l,r: %d, %d, %d, %d", t, b, l, r);
  35. */
  36. /*setup some graphics boxes */
  37. R_standard_color(D_translate_color("grey"));
  38. R_move_abs(l, t + (b - t) / 20);
  39. R_cont_abs(r, t + (b - t) / 20);
  40. R_move_abs(r, t + (b - t) / 10);
  41. R_cont_abs(l, t + (b - t) / 10);
  42. R_move_abs(l + (r - l) / 2, t);
  43. R_cont_abs(l + (r - l) / 2, t + (b - t) / 10);
  44. /*logo box */
  45. x1 = l;
  46. y1 = t;
  47. x2 = l + 0.5 * (r - l);
  48. y2 = t + 0.05 * (b - t);
  49. R_standard_color(5);
  50. R_box_abs(x1, y1, x2 - 1, y2 - 1);
  51. R_text_size((int)(0.055 * (x2 - x1)), (int)(0.7 * (y2 - y1)));
  52. R_move_abs((int)(x1 + 0.03 * (x2 - x1)), (int)(y1 + 0.825 * (y2 - y1)));
  53. R_standard_color(8);
  54. R_text("Live SPREAD Simulation");
  55. /*elapsed spread time box */
  56. x1_st = l + 0.5 * (r - l);
  57. y1_st = t;
  58. x2_st = r;
  59. y2_st = t + 0.05 * (b - t);
  60. /*printf("\ny1-st y2-st x1st, x2st: %d, %d, %d, %d", y1_st,y2_st,x1_st,x2_st);
  61. */ R_standard_color(9);
  62. R_box_abs(x1_st + 1, y1_st, x2_st, y2_st - 1);
  63. R_text_size((int)(0.049 * (x2_st - x1_st)), (int)(0.5 * (y2_st - y1_st)));
  64. R_move_abs((int)(x1_st + 0.03 * (x2_st - x1_st)),
  65. (int)(y1_st + 0.75 * (y2_st - y1_st)));
  66. R_standard_color(1);
  67. R_text("Elapsed Spread Time 00:00");
  68. /*starting watch-time box */
  69. x1 = l;
  70. y1 = t + 0.05 * (b - t);
  71. x2 = l + 0.5 * (r - l);
  72. y2 = t + 0.1 * (b - t);
  73. R_standard_color(9);
  74. R_box_abs(x1, y1 + 1, x2 - 1, y2 - 1);
  75. R_text_size((int)(0.05 * (x2 - x1)), (int)(0.5 * (y2 - y1)));
  76. R_move_abs((int)(x1 + 0.03 * (x2 - x1)), (int)(y1 + 0.75 * (y2 - y1)));
  77. time(&c_time);
  78. t_time = localtime(&c_time);
  79. strftime(cur_time, SIZE, "%H:%M", t_time);
  80. sprintf(buf, " Started At %s", cur_time);
  81. R_standard_color(8);
  82. R_text(buf);
  83. /*current watch-time box */
  84. x1_ct = l + 0.5 * (r - l);
  85. y1_ct = t + 0.05 * (b - t);
  86. x2_ct = r;
  87. y2_ct = t + 0.1 * (b - t);
  88. R_standard_color(9);
  89. R_box_abs(x1_ct + 1, y1_ct + 1, x2_ct, y2_ct - 1);
  90. R_text_size((int)(0.05 * (x2_ct - x1_ct)), (int)(0.5 * (y2_ct - y1_ct)));
  91. R_move_abs((int)(x1_ct + 0.03 * (x2_ct - x1_ct)),
  92. (int)(y1_ct + 0.75 * (y2_ct - y1_ct)));
  93. R_standard_color(8);
  94. sprintf(buf, " Current Time %s", cur_time);
  95. R_text(buf);
  96. /*live image display box */
  97. D_reset_screen_window(t + (b - t) / 10 + 2, b, l, r);
  98. R_close_driver();
  99. /*Set a graster map as a background image */
  100. if (backdrop_layer) {
  101. sprintf(buf, "d.rast -o %s", backdrop_layer);
  102. system(buf);
  103. }
  104. /*figure scaling factors of a file cell to a screen unit */
  105. R_open_driver();
  106. Rast_make_grey_scale_colors(&colors, 0, 59);
  107. width = r - l;
  108. height = 0.9 * (b - t) - 1;
  109. if (width * nrows > height * ncols) {
  110. f2s_y = height / (float)nrows;
  111. f2s_x = (window.ew_res / window.ns_res) * f2s_y;
  112. yoffset = t + 0.1 * (b - t) + 3;
  113. xoffset = l + (width - f2s_x * ncols) / 2 + 1;
  114. }
  115. else {
  116. f2s_x = width / (float)ncols;
  117. f2s_y = (window.ns_res / window.ew_res) * f2s_x;
  118. xoffset = l + 1;
  119. yoffset = t + 0.1 * (b - t) + (height - f2s_y * nrows) / 2 + 3;
  120. }
  121. D_reset_screen_window(t, b, l, r);
  122. }
  123. void draw_a_cell(int row, int col, int cell_value)
  124. {
  125. x1 = xoffset + f2s_x * col;
  126. y1 = yoffset + f2s_y * row;
  127. x2 = x1 + f2s_x + 0.999; /*tradeoff:allowing overlaps to avoid gaps */
  128. y2 = y1 + f2s_y + 0.999; /*tradeoff:allowing overlaps to avoid gaps */
  129. D_color(cell_value % 60, &colors);
  130. R_box_abs(x1, y1, x2, y2);
  131. R_flush();
  132. /* if cell_value changes, update it in the elasped-spread-time box,
  133. * also update the current watch-time if it changes*/
  134. if (cell_value > old_value) {
  135. old_value = cell_value;
  136. R_standard_color(9);
  137. R_box_abs(x1_st + 1, y1_st, x2_st, y2_st - 1);
  138. R_text_size((int)(0.049 * (x2_st - x1_st)),
  139. (int)(0.5 * (y2_st - y1_st)));
  140. R_move_abs((int)(x1_st + 0.03 * (x2_st - x1_st)),
  141. (int)(y1_st + 0.75 * (y2_st - y1_st)));
  142. R_standard_color(1);
  143. sprintf(buf, "Elapsed Spread Time %d%d:%d%d", (cell_value / 600),
  144. (cell_value / 60 - cell_value / 600 * 10),
  145. ((cell_value - cell_value / 60 * 60) / 10),
  146. ((cell_value - cell_value / 60 * 60) -
  147. (cell_value - cell_value / 60 * 60) / 10 * 10));
  148. R_text(buf);
  149. time(&c_time);
  150. t_time = localtime(&c_time);
  151. strftime(cur_time, SIZE, "%H:%M", t_time);
  152. if (strcmp(cur_time, old_time) != 0) {
  153. strcpy(old_time, cur_time);
  154. R_standard_color(9);
  155. R_box_abs(x1_ct + 1, y1_ct + 1, x2_ct, y2_ct);
  156. R_text_size((int)(0.05 * (x2_ct - x1_ct)),
  157. (int)(0.5 * (y2_ct - y1_ct)));
  158. R_move_abs((int)(x1_ct + 0.03 * (x2_ct - x1_ct)),
  159. (int)(y1_ct + 0.75 * (y2_ct - y1_ct)));
  160. R_standard_color(8);
  161. sprintf(buf, " Current Time %s", cur_time);
  162. R_text(buf);
  163. }
  164. }
  165. }
  166. void draw_a_burning_cell(int row, int col)
  167. {
  168. x1 = xoffset + f2s_x * col;
  169. y1 = yoffset + f2s_y * row;
  170. x2 = x1 + f2s_x + 0.999; /*tradeoff:allowing overlaps to avoid gaps */
  171. y2 = y1 + f2s_y + 0.999; /*tradeoff:allowing overlaps to avoid gaps */
  172. R_standard_color(D_translate_color("red"));
  173. R_box_abs(x1, y1, x2, y2);
  174. R_flush();
  175. }
  176. void display_close(void)
  177. {
  178. R_close_driver();
  179. }