Advertisement
anilgupta

Untitled

Feb 27th, 2012
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import "Terrain.h"
  2. //#import "HelloWorldLayer.h"
  3. #import "GameScene.h"
  4.  
  5. @implementation Terrain
  6. @synthesize stripes = _stripes;
  7.  
  8. - (void) generateHills {
  9.    
  10.     CGSize winSize = [CCDirector sharedDirector].winSize;
  11.    
  12.     float minDX = 10;
  13.     float minDY = 20;
  14.     int rangeDX = 40;
  15.     int rangeDY = 30;
  16.    
  17.     float x = -minDX;
  18.     float y = winSize.height/4-minDY;
  19.    
  20.     float dy, ny;
  21.     float sign = 1; // +1 - going up, -1 - going  down
  22.     float paddingTop = 8;
  23.     float paddingBottom = 9;
  24.    
  25.     for (int i=0; i<kMaxHillKeyPoints; i++) {
  26.         _hillKeyPoints[i] = CGPointMake(x, y);
  27.         if (i == 0) {
  28.             x = 0;
  29.             y = winSize.height/4;
  30.         } else {
  31.             x +=80;
  32.             while(true) {
  33.                 dy = 30;
  34.                 ny = y + dy*sign;
  35.                 if(ny < winSize.height-paddingTop && ny > paddingBottom) {
  36.                     break;  
  37.                 }
  38.             }
  39.             y = ny;
  40.         }
  41.         sign *= -1;
  42.     }
  43. }
  44.  
  45. - (void)resetHillVertices {
  46.    
  47.     CGSize winSize = [CCDirector sharedDirector].winSize;
  48.    
  49.     static int prevFromKeyPointI = -1;
  50.     static int prevToKeyPointI = -1;
  51.    
  52.     // key points interval for drawing
  53.     while (_hillKeyPoints[_fromKeyPointI+1].x < _offsetX-winSize.width/8/self.scale) {
  54.         _fromKeyPointI++;
  55.     }
  56.     while (_hillKeyPoints[_toKeyPointI].x < _offsetX+winSize.width*9/8/self.scale) {
  57.         _toKeyPointI++;
  58.     }
  59.    
  60.     if (prevFromKeyPointI != _fromKeyPointI || prevToKeyPointI != _toKeyPointI) {
  61.        
  62.         // vertices for visible area
  63.         _nHillVertices = 0;
  64.         _nBorderVertices = 0;
  65.         CGPoint p0, p1, pt0, pt1;
  66.         p0 = _hillKeyPoints[_fromKeyPointI];
  67.         for (int i=_fromKeyPointI+1; i<_toKeyPointI+1; i++) {
  68.             p1 = _hillKeyPoints[i];
  69.            
  70.             // triangle strip between p0 and p1
  71.             int hSegments = floorf((p1.x-p0.x)/kHillSegmentWidth);
  72.             float dx = (p1.x - p0.x) / hSegments;
  73.             float da = M_PI / hSegments;
  74.             float ymid = (p0.y + p1.y) / 2;
  75.             float ampl = (p0.y - p1.y) / 2;
  76.             pt0 = p0;
  77.             _borderVertices[_nBorderVertices++] = pt0;
  78.             for (int j=1; j<hSegments+1; j++) {
  79.                 pt1.x = p0.x + j*dx;
  80.                 pt1.y = ymid + ampl * cosf(da*j);
  81.                 _borderVertices[_nBorderVertices++] = pt1;
  82.                
  83.                 _hillVertices[_nHillVertices] = CGPointMake(pt0.x, 0);
  84.                 _hillTexCoords[_nHillVertices++] = CGPointMake(pt0.x/512, 1.0f);
  85.                 _hillVertices[_nHillVertices] = CGPointMake(pt1.x, 0);
  86.                 _hillTexCoords[_nHillVertices++] = CGPointMake(pt1.x/512, 1.0f);
  87.                
  88.                 _hillVertices[_nHillVertices] = CGPointMake(pt0.x, pt0.y);
  89.                 _hillTexCoords[_nHillVertices++] = CGPointMake(pt0.x/512, 0);
  90.                 _hillVertices[_nHillVertices] = CGPointMake(pt1.x, pt1.y);
  91.                 _hillTexCoords[_nHillVertices++] = CGPointMake(pt1.x/512, 0);
  92.                
  93.                 pt0 = pt1;
  94.             }
  95.            
  96.             p0 = p1;
  97.         }
  98.        
  99.         prevFromKeyPointI = _fromKeyPointI;
  100.         prevToKeyPointI = _toKeyPointI;        
  101.     }
  102.    
  103. }
  104.  
  105. - (id)init {
  106.     if ((self = [super init])) {
  107.         [self generateHills];
  108.         [self resetHillVertices];
  109.     }
  110.     return self;
  111. }
  112.  
  113. - (void) draw {
  114.    
  115.     glBindTexture(GL_TEXTURE_2D, _stripes.texture.name);
  116.     glDisableClientState(GL_COLOR_ARRAY);
  117.    
  118.     glColor4f(1, 4, 2, 1);
  119.     glVertexPointer(2, GL_FLOAT, 0, _hillVertices);
  120.     glTexCoordPointer(2, GL_FLOAT, 0, _hillTexCoords);
  121.     glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)_nHillVertices);
  122.    
  123. /*  for(int i = MAX(_fromKeyPointI, 1); i <= _toKeyPointI; ++i) {
  124.         glColor4f(1.0, 0, 0, 1.0);
  125.         ccDrawLine(_hillKeyPoints[i-1], _hillKeyPoints[i]);    
  126.        
  127.         glColor4f(1.0, 1.0, 1.0, 1.0);
  128.        
  129.         CGPoint p0 = _hillKeyPoints[i-1];
  130.         CGPoint p1 = _hillKeyPoints[i];
  131.         int hSegments = floorf((p1.x-p0.x)/kHillSegmentWidth);
  132.         float dx = (p1.x - p0.x) / hSegments;
  133.         float da = M_PI / hSegments;
  134.         float ymid = (p0.y + p1.y) / 3;
  135.         float ampl = (p0.y - p1.y) / 5;
  136.        
  137.         CGPoint pt0, pt1;
  138.         pt0 = p0;
  139.         for (int j = 0; j < hSegments+1; ++j) {
  140.            
  141.             pt1.x = p0.x + j*dx;
  142.             pt1.y = ymid + ampl * cosf(da*j);
  143.            
  144.             ccDrawLine(pt0, pt1);
  145.            
  146.             pt0 = pt1;
  147.            
  148.         }
  149.     }*/
  150.    
  151. }
  152.  
  153. - (void) setOffsetX:(float)newOffsetX {
  154.     _offsetX = newOffsetX;
  155.     self.position = CGPointMake(-_offsetX*self.scale, 0);
  156.     [self resetHillVertices];
  157. }
  158.  
  159. - (void)dealloc {
  160.     [_stripes release];
  161.     _stripes = NULL;
  162.     [super dealloc];
  163. }
  164.  
  165. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement