OSNOW1.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Seven Kingdoms: Ancient Adversaries
  3. *
  4. * Copyright 1997,1998 Enlight Software Ltd.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. // Filename : OSNOW1.CPP
  21. // Description: class SnowLayer
  22. // Onwership : Gilbert
  23. #include <OSNOW.h>
  24. #include <OVGABUF.h>
  25. #include <COLOR.h>
  26. #include <ALL.h>
  27. //---------- Define constant -------------//
  28. // #### begin Gilbert 8/5 ######//
  29. const char SNOW_COLOUR1 = char(VGA_GRAY+8);
  30. const char SNOW_COLOUR2 = char(VGA_GRAY+11);
  31. const char SNOW_COLOUR3 = char(VGA_GRAY+14);
  32. // #### end Gilbert 8/5 ######//
  33. //---------- Begin of function SnowLayer::set_bound -----//
  34. //
  35. void SnowLayer::set_bound(int x1, int y1, int x2, int y2)
  36. {
  37. bound_x1 = x1;
  38. bound_y1 = y1;
  39. bound_x2 = x2;
  40. bound_y2 = y2;
  41. }
  42. //---------- End of function SnowLayer::set_bound -----//
  43. //---------- Begin of function SnowLayer::init -----//
  44. //
  45. void SnowLayer::init(short h, short v, short speed, short amp, short r,
  46. double s, char animSpeed, short initPeriod)
  47. {
  48. h_sep = h;
  49. v_sep = v;
  50. seed = r + h*2 + v*5 + speed*7;
  51. fall_speed = speed;
  52. amplitude = amp;
  53. radius = r;
  54. snow_x = bound_x1 + random(h_sep) + r;
  55. snow_y = bound_y1 + r -1;
  56. slide_speed = (short)(s * fall_speed);
  57. blind_site = (char)(h + v) % 3; // random number?
  58. anim_speed = animSpeed;
  59. anim_phase = 0;
  60. period = initPeriod;
  61. }
  62. //---------- End of function SnowLayer::init -----//
  63. //---------- Begin of function SnowLayer::fall -----//
  64. //
  65. void SnowLayer::fall()
  66. {
  67. for( anim_phase += anim_speed; anim_phase >= 10; anim_phase -= 10)
  68. {
  69. snow_y = snow_y + fall_speed;
  70. while( snow_y - radius - bound_y1 >= v_sep)
  71. {
  72. snow_y -= v_sep;
  73. blind_site = (blind_site + 1) % 3;
  74. if(++period > 1000)
  75. period = 1000;
  76. }
  77. snow_x += random(amplitude * 2 + 1) - amplitude + slide_speed;
  78. if( snow_x - radius < bound_x1 )
  79. {
  80. snow_x += h_sep;
  81. blind_site = (blind_site + 2) % 3;
  82. }
  83. else if( snow_x - radius - bound_x1 >= h_sep)
  84. {
  85. snow_x -= h_sep;
  86. blind_site = (blind_site + 1) % 3;
  87. }
  88. }
  89. }
  90. //---------- End of function SnowLayer::fall -----//
  91. //---------- Begin of function SnowLayer::draw_step -----//
  92. // note : blind_site is ranging from 0 to 2.
  93. // if the total (cx+cy) % 3 is equal to blind_site, the snow
  94. // is not displayed.
  95. // Therefore one third of the snow is not displayed.
  96. // The snow layer doesn't look too regular.
  97. //
  98. // blind_site is adjusted in SnowLayer::fall() such that
  99. // snow doesn't disappear suddenly.
  100. //
  101. void SnowLayer::draw_step(VgaBuf *vgabuf)
  102. {
  103. int sx, sy, cx, cy;
  104. int vPitch = vgabuf->buf_pitch();
  105. char *dotPt;
  106. fall();
  107. switch(radius)
  108. {
  109. case 1:
  110. for(sy = snow_y, cy=0; sy < bound_y2 && cy < period; sy+= v_sep, ++cy)
  111. {
  112. for( sx = snow_x, cx=0; sx < bound_x2; sx += h_sep, ++cx)
  113. {
  114. if((cx+cy) % 3 != blind_site)
  115. {
  116. dotPt = vgabuf->buf_ptr() + vPitch*(sy-1) + sx-1;
  117. *dotPt++ = SNOW_COLOUR1;
  118. *dotPt++ = SNOW_COLOUR2;
  119. *dotPt = SNOW_COLOUR1;
  120. dotPt += vPitch - 2;
  121. *dotPt++ = SNOW_COLOUR2;
  122. *dotPt++ = SNOW_COLOUR3;
  123. *dotPt = SNOW_COLOUR2;
  124. dotPt += vPitch - 2;
  125. *dotPt++ = SNOW_COLOUR1;
  126. *dotPt++ = SNOW_COLOUR2;
  127. *dotPt = SNOW_COLOUR1;
  128. }
  129. }
  130. }
  131. case 0:
  132. for(sy = snow_y,cy =0; sy <= bound_y2 && cy < period; sy+= v_sep, ++cy)
  133. {
  134. for( sx = snow_x, cx=0; sx <= bound_x2; sx += h_sep, ++cx)
  135. {
  136. if( (cx+cy) % 3 != blind_site)
  137. {
  138. vgabuf->draw_pixel(sx, sy, SNOW_COLOUR3);
  139. }
  140. }
  141. }
  142. break;
  143. default:
  144. err_now("undefined snow radius");
  145. }
  146. }
  147. //---------- End of function SnowLayer::draw_step -----//
  148. //---------- Begin of function SnowLayer::random -----//
  149. unsigned SnowLayer::random(unsigned bound)
  150. {
  151. #define MULTIPLIER 0x015a4e35L
  152. #define INCREMENT 1
  153. seed = MULTIPLIER * seed + INCREMENT;
  154. return seed % bound;
  155. }
  156. //---------- End of function SnowLayer::random -----//