home *** CD-ROM | disk | FTP | other *** search
/ PC Format (South-Africa) 2001 June / PCFJune.iso / Xenon / XenonSource.exe / gamesystem / source / gs_starfield.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-27  |  3.0 KB  |  158 lines

  1. //-------------------------------------------------------------
  2. //
  3. // Class:    gsCStarfield
  4. //
  5. // Author:    John M Phillips
  6. //
  7. // Started:    12/03/00
  8. //
  9. // Base:    gsCVisual
  10. //
  11. // Derived:    None
  12. //
  13. //-------------------------------------------------------------
  14.  
  15. #include "gamesystem.h"
  16.  
  17. //-------------------------------------------------------------
  18.  
  19. gsCStarfield::gsCStarfield()
  20. {
  21.     m_position = 0;
  22.     m_layers = 0;
  23.     m_width = 0;
  24.     m_height = 0;
  25.  
  26.     m_point = 0;
  27.     m_colour = 0;
  28.     m_layer = 0;
  29.     m_offset = 0;
  30. }
  31.  
  32. //-------------------------------------------------------------
  33.  
  34. gsCStarfield::~gsCStarfield()
  35. {
  36.     destroy();
  37. }
  38.  
  39. //-------------------------------------------------------------
  40.  
  41. void gsCStarfield::create()
  42. {
  43.     gsCScreen *screen = gsCApplication::getScreen();
  44.  
  45.     if (!screen) {
  46.         gsREPORT("gsCStarfield::create requires active screen");
  47.         return;
  48.         }
  49.  
  50.     m_width = screen->getSize().getX();
  51.     m_height = screen->getSize().getY();
  52.  
  53.     m_point = new gsCPoint[m_width];
  54.     m_colour = new gsCColour[m_width];
  55.  
  56.     m_layer = new int[m_width];
  57.     m_offset = new int[m_width];
  58.  
  59.     gsCRandom random;
  60.  
  61.     for (int x = 0; x < m_width; x++) {
  62.         
  63.         m_point[x].setX(x);
  64.  
  65.         int l = random.getInt(m_layers);
  66.  
  67.         m_layer[x] = l;
  68.         m_offset[x] = random.getInt(m_height) * (1 << l);
  69.  
  70.         int cupper = 255 - 16 * x;
  71.         int clower = 128 - 16 * x;
  72.  
  73.         m_colour[x] = gsCColour(random.getInt(clower,cupper),
  74.                                 random.getInt(clower,cupper),
  75.                                 random.getInt(clower,cupper));
  76.         }
  77.  
  78. }
  79.  
  80. //-------------------------------------------------------------
  81.  
  82. void gsCStarfield::destroy()
  83. {
  84.     if (m_point) {
  85.         delete [] m_point;
  86.         m_point = 0;
  87.         delete [] m_colour;
  88.         m_colour = 0;
  89.         delete [] m_layer;
  90.         m_layer = 0;
  91.         delete [] m_offset;
  92.         m_offset = 0;
  93.         }
  94.     
  95.     m_layers = 0;
  96. }
  97.  
  98. //-------------------------------------------------------------
  99.  
  100. void gsCStarfield::initialize(int layers)
  101. {
  102.     destroy();
  103.  
  104.     m_layers = layers;
  105.  
  106.     if (m_layers > 0)
  107.         create();
  108. }
  109.  
  110. //-------------------------------------------------------------
  111.  
  112. void gsCStarfield::setPosition(int y)
  113. {
  114.     m_position = y;
  115. }
  116.  
  117. //-------------------------------------------------------------
  118.  
  119. int gsCStarfield::getPosition()
  120. {
  121.     return m_position;
  122. }
  123.  
  124. //-------------------------------------------------------------
  125.  
  126. void gsCStarfield::move(int offset)
  127. {
  128.     m_position += offset;
  129. }
  130.  
  131. //-------------------------------------------------------------
  132.  
  133. void gsCStarfield::draw()
  134. {
  135.     if (m_layers == 0)
  136.         return;
  137.  
  138.     gsCScreen *screen = gsCApplication::getScreen();
  139.  
  140.     if (!screen)
  141.         return;
  142.  
  143.     int range = 1 << m_layers;
  144.  
  145.     while (m_position < 0)
  146.         m_position += range * m_height;
  147.     while (m_position >= range * m_height)
  148.         m_position -= range * m_height;
  149.  
  150.     for (int x = 0; x < m_width; x++)
  151.         m_point[x].setY(((m_offset[x] + m_position) >> m_layer[x]) % m_height);
  152.  
  153.     screen->drawPoints(m_width,m_point,m_colour,false);
  154. }
  155.  
  156. //-------------------------------------------------------------
  157.  
  158.