game_rendering.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. #ifndef __GAME_RENDERING_H
  2. #define __GAME_RENDERING_H
  3. void Game::make_screen(mainloop::screen_params_t& sp, GameState& state) {
  4. sp.w = GRID_W;
  5. sp.h = GRID_H;
  6. sp.w2 = sp.w;
  7. sp.h2 = sp.h;
  8. auto& themes = state.render.ui_symbol_themes;
  9. for (const auto& i : constants().ui_symbols) {
  10. themes.push_back(i[n_skin]);
  11. }
  12. state.render.set_ui_symbol();
  13. }
  14. void Game::set_skin(GameState& state, unsigned int x, unsigned int y) {
  15. bool walkable = state.grid.is_walk(x, y);
  16. bool water = state.grid.is_water(x, y);
  17. state.render.set_is_viewblock(x, y, 0, !walkable);
  18. state.render.set_is_walkblock(x, y, 0, !walkable);
  19. grender::Grid::skin s;
  20. double z = state.grid._get(x, y);
  21. const Levelskin& lev = levelskins().get(p.worldz);
  22. if (walkable) {
  23. if (water) {
  24. if (z <= -5) {
  25. s = lev.deep_water[n_skin];
  26. } else {
  27. s = lev.shallow_water[n_skin];
  28. }
  29. } else {
  30. if (z <= -8) {
  31. s = lev.floor1[n_skin];
  32. } else if (z <= -7) {
  33. s = lev.floor2[n_skin];
  34. } else if (z <= -6) {
  35. s = lev.floor3[n_skin];
  36. } else if (z <= -5) {
  37. s = lev.floor4[n_skin];
  38. } else if (z <= -4) {
  39. s = lev.floor5[n_skin];
  40. } else if (z <= -3) {
  41. s = lev.floor6[n_skin];
  42. } else if (z <= -2) {
  43. s = lev.floor7[n_skin];
  44. } else {
  45. s = lev.floor8[n_skin];
  46. }
  47. }
  48. } else {
  49. if (water) {
  50. s = lev.water_wall[n_skin];
  51. } else {
  52. s = lev.wall[n_skin];
  53. }
  54. }
  55. state.render.set_skin(x, y, 0, s);
  56. // //
  57. features::Feature feat;
  58. if (state.features.get(x, y, feat)) {
  59. const Terrain& t = terrain().get(feat.tag);
  60. state.render.set_skin(x, y, 1, t.skin[n_skin]);
  61. state.render.set_is_viewblock(x, y, 1, t.viewblock);
  62. state.render.set_is_walkblock(x, y, 1, t.walkblock);
  63. state.render.set_is_lit(x, y, 1, t.is_lit);
  64. state.render.set_is_lightsource(x, y, 1, t.is_lightsource);
  65. } else {
  66. state.render.unset_skin(x, y, 1);
  67. state.render.set_is_viewblock(x, y, 1, false);
  68. state.render.set_is_walkblock(x, y, 1, false);
  69. state.render.set_is_lit(x, y, 1, false);
  70. state.render.set_is_lightsource(x, y, 1, false);
  71. }
  72. // //
  73. items::Item item;
  74. if (state.items.get(x, y, 0, item)) {
  75. const Design& d = designs().get(item.tag);
  76. state.render.set_skin(x, y, 4, d.skin[n_skin]);
  77. state.render.set_is_lit(x, y, 4, d.is_lit);
  78. } else {
  79. state.render.unset_skin(x, y, 4);
  80. state.render.set_is_lit(x, y, 4, false);
  81. }
  82. // //
  83. if (x == p.px && y == p.py) {
  84. s = p.get_species().skin[n_skin];
  85. state.render.set_skin(x, y, 5, s);
  86. } else {
  87. monsters::Monster& mon = state.monsters.get(x, y);
  88. if (!mon.null() && !mon.hidden) {
  89. const Species& s = species().get(mon.tag);
  90. auto skin = s.skin[n_skin];
  91. if (!mon.ally.null()) {
  92. skin.underline = true;
  93. }
  94. state.render.set_skin(x, y, 5, skin);
  95. state.render.set_is_walkblock(x, y, 5, true);
  96. } else {
  97. state.render.unset_skin(x, y, 5);
  98. state.render.set_is_walkblock(x, y, 5, false);
  99. }
  100. }
  101. state.render.validate(x, y);
  102. }
  103. void drawing_context_center_at(mainloop::drawing_context_t& ctx, unsigned int x, unsigned int y) {
  104. unsigned int grid_x = ctx.view_w / 4;
  105. unsigned int grid_y = ctx.view_h / 4;
  106. if (!ctx.do_center_view && grid_x > 1) {
  107. ctx.voff_off_x = -(x % grid_x) + (grid_x / 2);
  108. } else {
  109. ctx.voff_off_x = 0;
  110. }
  111. if (!ctx.do_center_view && grid_y > 1) {
  112. ctx.voff_off_y = -(y % grid_y) + (grid_y / 2);
  113. } else {
  114. ctx.voff_off_y = 0;
  115. }
  116. ctx.centerx = x;
  117. ctx.centery = y;
  118. }
  119. unsigned int get_lightradius(const Player& p, const GameState& state) {
  120. int r = -1;
  121. features::Feature feat;
  122. if (state.features.get(p.px, p.py, feat)) {
  123. r = terrain().get(feat.tag).view_radius;
  124. }
  125. if (!p.polymorph.species.null()) {
  126. r = p.get_species().range;
  127. }
  128. if (r < 0) {
  129. const Levelskin& ls = levelskins().get(p.worldz);
  130. r = std::min(ls.lightradius_max, ls.lightradius + p.get_lightradius());
  131. }
  132. if (p.blind > 0) {
  133. r = std::max(1, (int)(r * (1.0 - p.blind)));
  134. }
  135. return r;
  136. }
  137. void Game::drawing_context(mainloop::drawing_context_t& ctx, const GameState& state) {
  138. ctx.px = p.px;
  139. ctx.py = p.py;
  140. ctx.lightradius = get_lightradius(p, state);
  141. if (p.state & Player::LOOKING) {
  142. if (p.look.rangemin >= 0) {
  143. ctx.rangemin = p.look.rangemin;
  144. }
  145. if (p.look.rangemax >= 0) {
  146. ctx.rangemax = p.look.rangemax;
  147. }
  148. ctx.hlx = p.look.x;
  149. ctx.hly = p.look.y;
  150. drawing_context_center_at(ctx, p.look.x, p.look.y);
  151. } else {
  152. drawing_context_center_at(ctx, p.px, p.py);
  153. }
  154. }
  155. void draw_one_stat(Player& p, GameState& state, tag_t s, size_t n_skin) {
  156. const Stat& st = stats().get(s);
  157. double val = p.stats.gets(s);
  158. if (st.hidden && val == 0)
  159. return;
  160. double v;
  161. double min;
  162. double max;
  163. if (st.reversed) {
  164. max = st.min;
  165. min = st.max;
  166. } else {
  167. max = st.max;
  168. min = st.min;
  169. }
  170. if (st.progressbar) {
  171. v = 6.0 * (val - min) / (max - min);
  172. } else {
  173. v = 3.0 * (2 * val - max - min) / (max - min);
  174. }
  175. int vp = v;
  176. if (v < 0) {
  177. --vp;
  178. }
  179. const auto& zs = st.label;
  180. if (st.progressbar) {
  181. state.render.push_hud_line(vp, zs.label, zs.lskin[n_skin], zs.pskin[n_skin]);
  182. } else {
  183. state.render.push_hud_line(vp, zs.label, zs.lskin[n_skin], zs.pskin[n_skin], zs.nskin[n_skin]);
  184. }
  185. }
  186. void draw_one_count(Player& p, GameState& state, tag_t s, size_t n_skin) {
  187. const Count& ct = counts().get(s);
  188. double val = p.stats.getc(s);
  189. if (ct.hidden && val == 0)
  190. return;
  191. unsigned int v = (6u * val) / ct.cmax;
  192. const auto& zs = ct.label;
  193. state.render.push_hud_line(v, zs.label, zs.lskin[n_skin], (state.ticks & 1 ? zs.pskin[n_skin] : zs.nskin[n_skin]));
  194. }
  195. void Game::draw_hud(GameState& state) {
  196. const auto& hso = constants().hud_stats_order;
  197. const auto& hco = constants().hud_counts_order;
  198. for (const tag_t& i : hso) {
  199. draw_one_stat(p, state, i, n_skin);
  200. }
  201. for (const tag_t& i : hco) {
  202. draw_one_count(p, state, i, n_skin);
  203. }
  204. }
  205. #endif