st_lib.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /* Emacs style mode select -*- C++ -*-
  2. *-----------------------------------------------------------------------------
  3. *
  4. *
  5. * PrBoom: a Doom port merged with LxDoom and LSDLDoom
  6. * based on BOOM, a modified and improved DOOM engine
  7. * Copyright (C) 1999 by
  8. * id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman
  9. * Copyright (C) 1999-2000 by
  10. * Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze
  11. * Copyright 2005, 2006 by
  12. * Florian Schulze, Colin Phipps, Neil Stevens, Andrey Budko
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version 2
  17. * of the License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  27. * 02111-1307, USA.
  28. *
  29. * DESCRIPTION:
  30. * The status bar widget code.
  31. *
  32. *-----------------------------------------------------------------------------*/
  33. #include "doomdef.h"
  34. #include "doomstat.h"
  35. #include "v_video.h"
  36. #include "w_wad.h"
  37. #include "st_stuff.h"
  38. #include "st_lib.h"
  39. #include "r_main.h"
  40. #include "lprintf.h"
  41. int sts_always_red; //jff 2/18/98 control to disable status color changes
  42. int sts_pct_always_gray; // killough 2/21/98: always gray %'s? bug or feature?
  43. //
  44. // STlib_init()
  45. //
  46. void STlib_init(void)
  47. {
  48. // cph - no longer hold STMINUS pointer
  49. }
  50. //
  51. // STlib_initNum()
  52. //
  53. // Initializes an st_number_t widget
  54. //
  55. // Passed the widget, its position, the patches for the digits, a pointer
  56. // to the value displayed, a pointer to the on/off control, and the width
  57. // Returns nothing
  58. //
  59. void STlib_initNum
  60. ( st_number_t* n,
  61. int x,
  62. int y,
  63. const patchnum_t* pl,
  64. int* num,
  65. boolean* on,
  66. int width )
  67. {
  68. n->x = x;
  69. n->y = y;
  70. n->oldnum = 0;
  71. n->width = width;
  72. n->num = num;
  73. n->on = on;
  74. n->p = pl;
  75. }
  76. /*
  77. * STlib_drawNum()
  78. *
  79. * A fairly efficient way to draw a number based on differences from the
  80. * old number.
  81. *
  82. * Passed a st_number_t widget, a color range for output, and a flag
  83. * indicating whether refresh is needed.
  84. * Returns nothing
  85. *
  86. * jff 2/16/98 add color translation to digit output
  87. * cphipps 10/99 - const pointer to colour trans table, made function static
  88. */
  89. static void STlib_drawNum
  90. ( st_number_t* n,
  91. int cm,
  92. boolean refresh )
  93. {
  94. int numdigits = n->width;
  95. int num = *n->num;
  96. int w = n->p[0].width;
  97. int h = n->p[0].height;
  98. int x = n->x;
  99. int neg;
  100. // leban 1/20/99:
  101. // strange that somebody went through all the work to draw only the
  102. // differences, and then went and constantly redrew all the numbers.
  103. // return without drawing if the number didn't change and the bar
  104. // isn't refreshing.
  105. if(n->oldnum == num && !refresh)
  106. return;
  107. // CPhipps - compact some code, use num instead of *n->num
  108. if ((neg = (n->oldnum = num) < 0))
  109. {
  110. if (numdigits == 2 && num < -9)
  111. num = -9;
  112. else if (numdigits == 3 && num < -99)
  113. num = -99;
  114. num = -num;
  115. }
  116. // clear the area
  117. x = n->x - numdigits*w;
  118. #ifdef RANGECHECK
  119. if (n->y - ST_Y < 0)
  120. I_Error("STlib_drawNum: n->y - ST_Y < 0");
  121. #endif
  122. V_CopyRect(x, n->y - ST_Y, BG, w*numdigits, h, x, n->y, FG, VPT_STRETCH);
  123. // if non-number, do not draw it
  124. if (num == 1994)
  125. return;
  126. x = n->x;
  127. //jff 2/16/98 add color translation to digit output
  128. // in the special case of 0, you draw 0
  129. if (!num)
  130. // CPhipps - patch drawing updated, reformatted
  131. V_DrawNumPatch(x - w, n->y, FG, n->p[0].lumpnum, cm,
  132. (((cm!=CR_DEFAULT) && !sts_always_red) ? VPT_TRANS : VPT_NONE) | VPT_STRETCH);
  133. // draw the new number
  134. //jff 2/16/98 add color translation to digit output
  135. while (num && numdigits--) {
  136. // CPhipps - patch drawing updated, reformatted
  137. x -= w;
  138. V_DrawNumPatch(x, n->y, FG, n->p[num % 10].lumpnum, cm,
  139. (((cm!=CR_DEFAULT) && !sts_always_red) ? VPT_TRANS : VPT_NONE) | VPT_STRETCH);
  140. num /= 10;
  141. }
  142. // draw a minus sign if necessary
  143. //jff 2/16/98 add color translation to digit output
  144. // cph - patch drawing updated, load by name instead of acquiring pointer earlier
  145. if (neg)
  146. V_DrawNamePatch(x - w, n->y, FG, "STTMINUS", cm,
  147. (((cm!=CR_DEFAULT) && !sts_always_red) ? VPT_TRANS : VPT_NONE) | VPT_STRETCH);
  148. }
  149. /*
  150. * STlib_updateNum()
  151. *
  152. * Draws a number conditionally based on the widget's enable
  153. *
  154. * Passed a number widget, the output color range, and a refresh flag
  155. * Returns nothing
  156. *
  157. * jff 2/16/98 add color translation to digit output
  158. * cphipps 10/99 - make that pointer const
  159. */
  160. void STlib_updateNum
  161. ( st_number_t* n,
  162. int cm,
  163. boolean refresh )
  164. {
  165. if (*n->on) STlib_drawNum(n, cm, refresh);
  166. }
  167. //
  168. // STlib_initPercent()
  169. //
  170. // Initialize a st_percent_t number with percent sign widget
  171. //
  172. // Passed a st_percent_t widget, the position, the digit patches, a pointer
  173. // to the number to display, a pointer to the enable flag, and patch
  174. // for the percent sign.
  175. // Returns nothing.
  176. //
  177. void STlib_initPercent
  178. ( st_percent_t* p,
  179. int x,
  180. int y,
  181. const patchnum_t* pl,
  182. int* num,
  183. boolean* on,
  184. const patchnum_t* percent )
  185. {
  186. STlib_initNum(&p->n, x, y, pl, num, on, 3);
  187. p->p = percent;
  188. }
  189. /*
  190. * STlib_updatePercent()
  191. *
  192. * Draws a number/percent conditionally based on the widget's enable
  193. *
  194. * Passed a precent widget, the output color range, and a refresh flag
  195. * Returns nothing
  196. *
  197. * jff 2/16/98 add color translation to digit output
  198. * cphipps - const for pointer to the colour translation table
  199. */
  200. void STlib_updatePercent
  201. ( st_percent_t* per,
  202. int cm,
  203. int refresh )
  204. {
  205. if (*per->n.on && (refresh || (per->n.oldnum != *per->n.num))) {
  206. // killough 2/21/98: fix percents not updated;
  207. /* CPhipps - make %'s only be updated if number changed */
  208. // CPhipps - patch drawing updated
  209. V_DrawNumPatch(per->n.x, per->n.y, FG, per->p->lumpnum,
  210. sts_pct_always_gray ? CR_GRAY : cm,
  211. (sts_always_red ? VPT_NONE : VPT_TRANS) | VPT_STRETCH);
  212. }
  213. STlib_updateNum(&per->n, cm, refresh);
  214. }
  215. //
  216. // STlib_initMultIcon()
  217. //
  218. // Initialize a st_multicon_t widget, used for a multigraphic display
  219. // like the status bar's keys.
  220. //
  221. // Passed a st_multicon_t widget, the position, the graphic patches, a pointer
  222. // to the numbers representing what to display, and pointer to the enable flag
  223. // Returns nothing.
  224. //
  225. void STlib_initMultIcon
  226. ( st_multicon_t* i,
  227. int x,
  228. int y,
  229. const patchnum_t* il,
  230. int* inum,
  231. boolean* on )
  232. {
  233. i->x = x;
  234. i->y = y;
  235. i->oldinum = -1;
  236. i->inum = inum;
  237. i->on = on;
  238. i->p = il;
  239. }
  240. //
  241. // STlib_updateMultIcon()
  242. //
  243. // Draw a st_multicon_t widget, used for a multigraphic display
  244. // like the status bar's keys. Displays each when the control
  245. // numbers change or refresh is true
  246. //
  247. // Passed a st_multicon_t widget, and a refresh flag
  248. // Returns nothing.
  249. //
  250. void STlib_updateMultIcon
  251. ( st_multicon_t* mi,
  252. boolean refresh )
  253. {
  254. int w;
  255. int h;
  256. int x;
  257. int y;
  258. if (*mi->on && (mi->oldinum != *mi->inum || refresh))
  259. {
  260. if (mi->oldinum != -1)
  261. {
  262. x = mi->x - mi->p[mi->oldinum].leftoffset;
  263. y = mi->y - mi->p[mi->oldinum].topoffset;
  264. w = mi->p[mi->oldinum].width;
  265. h = mi->p[mi->oldinum].height;
  266. #ifdef RANGECHECK
  267. if (y - ST_Y < 0)
  268. I_Error("STlib_updateMultIcon: y - ST_Y < 0");
  269. #endif
  270. V_CopyRect(x, y-ST_Y, BG, w, h, x, y, FG, VPT_STRETCH);
  271. }
  272. if (*mi->inum != -1) // killough 2/16/98: redraw only if != -1
  273. V_DrawNumPatch(mi->x, mi->y, FG, mi->p[*mi->inum].lumpnum, CR_DEFAULT, VPT_STRETCH);
  274. mi->oldinum = *mi->inum;
  275. }
  276. }
  277. //
  278. // STlib_initBinIcon()
  279. //
  280. // Initialize a st_binicon_t widget, used for a multinumber display
  281. // like the status bar's weapons, that are present or not.
  282. //
  283. // Passed a st_binicon_t widget, the position, the digit patches, a pointer
  284. // to the flags representing what is displayed, and pointer to the enable flag
  285. // Returns nothing.
  286. //
  287. void STlib_initBinIcon
  288. ( st_binicon_t* b,
  289. int x,
  290. int y,
  291. const patchnum_t* i,
  292. boolean* val,
  293. boolean* on )
  294. {
  295. b->x = x;
  296. b->y = y;
  297. b->oldval = 0;
  298. b->val = val;
  299. b->on = on;
  300. b->p = i;
  301. }
  302. //
  303. // STlib_updateBinIcon()
  304. //
  305. // DInitialize a st_binicon_t widget, used for a multinumber display
  306. // like the status bar's weapons, that are present or not.
  307. //
  308. // Draw a st_binicon_t widget, used for a multinumber display
  309. // like the status bar's weapons that are present or not. Displays each
  310. // when the control flag changes or refresh is true
  311. //
  312. // Passed a st_binicon_t widget, and a refresh flag
  313. // Returns nothing.
  314. //
  315. void STlib_updateBinIcon
  316. ( st_binicon_t* bi,
  317. boolean refresh )
  318. {
  319. int x;
  320. int y;
  321. int w;
  322. int h;
  323. if (*bi->on && (bi->oldval != *bi->val || refresh))
  324. {
  325. x = bi->x - bi->p->leftoffset;
  326. y = bi->y - bi->p->topoffset;
  327. w = bi->p->width;
  328. h = bi->p->height;
  329. #ifdef RANGECHECK
  330. if (y - ST_Y < 0)
  331. I_Error("STlib_updateBinIcon: y - ST_Y < 0");
  332. #endif
  333. if (*bi->val)
  334. V_DrawNumPatch(bi->x, bi->y, FG, bi->p->lumpnum, CR_DEFAULT, VPT_STRETCH);
  335. else
  336. V_CopyRect(x, y-ST_Y, BG, w, h, x, y, FG, VPT_STRETCH);
  337. bi->oldval = *bi->val;
  338. }
  339. }