title: Add value to vertex array
body: <p>I want to make my vertex array dynamic. So that values would be added after every mouse event. When user drags his mouse I register mouse location coordinates. Coordinates is named 'loc'. And when user drags his mouse the 'loc' value is updated. So I want that when 'loc' is updated, coordinates would be <strong>added</strong> to vertex array. Still I can only do that when 'loc' is updated it rebuilds vertex array, so my vertex array always has only one coordinates (current 'loc' value).</p>
  
  <p>My vertex array values are stored in GLfloat:</p>
  
  <pre><code>GLfloat vertexes[] = { loc.x, loc.y };
  </code></pre>
  
  <p>'loc' is registered in <code>- (void) mouseDragged:(NSEvent *)event</code> by:</p>
  
  <pre><code>loc = [self convertPoint: [event locationInWindow] fromView:self];
  </code></pre>
  
  <p>Vertex array is drawn in <code>- (void) drawMyShape</code> by:</p>
  
  <pre><code>glEnableClientState(GL_VERTEX_ARRAY);
  glVertexPointer(2, GL_FLOAT, 0, vertexes);
  glPointSize(30);
  glDrawArrays(GL_POINTS, 0, vertexCount);
  glFlush();
  glDisableClientState(GL_VERTEX_ARRAY);
  </code></pre>
  
  <p><code>- (void) drawMyShape</code> is called after registering mouse events and adding them to GLfloat in <code>- (void) mouseDragged:(NSEvent *)event</code> by:</p>
  
  <pre><code>[self drawMyShape]
  </code></pre>
  
  <hr>
  
  <p>Current code: </p>
  
  <p>in .h file:</p>
  
  <pre><code>NSMutableArray *vertices;
  @property (nonatomic, retain) NSMutableArray *vertices;
  </code></pre>
  
  <p>in the beginning of .m file</p>
  
  <pre><code>@dynamic vertices;
  </code></pre>
  
  <p>in .m file in <code>- (id)initWithCoder:(NSCoder *)coder</code></p>
  
  <pre><code>vertices = [[NSMutableArray alloc] init];
  </code></pre>
  
  <p>in .m file in <code>- (void) mouseDragged:(NSEvent *)event</code></p>
  
  <pre><code>loc = [self convertPoint: [event locationInWindow] fromView:self];
  NSValue *locationValue = [NSValue valueWithPoint:loc];
  [vertices addObject:locationValue];`
  [self addValuesToArray];
  </code></pre>
  
  <p>in .m file in <code>- (void) addValuesToArray</code></p>
  
  <pre><code>int count = [vertices count] * 2; 
  NSLog(@"count: %d", count); 
  int currIndex = 0; 
  GLfloat GLVertices[] = {*(GLfloat *)malloc(count * sizeof(GLfloat))}; 
  for (NSValue *locationValue in vertices) { 
    NSValue *locationValue = [vertices objectAtIndex:currIndex++]; 
    CGPoint curLoc = locationValue.pointValue; 
    GLVertices[currIndex++] = curLoc.x; 
    GLVertices[currIndex++] = curLoc.y; 
  } 
  </code></pre>
  
  <p>And it crashes on </p>
  
  <pre><code>NSValue *locationValue = [vertices objectAtIndex:i];
  </code></pre>
  
  <p>After crash in Log i see (<em>removed some parts of log because they are not important I think</em>):</p>
  
  <pre><code>sum: 2
  *** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]
  sum: 3
  *** -[__NSArrayM objectAtIndex:]: index 3 beyond bounds [0 .. 2]
  sum: 4
  *** -[__NSArrayM objectAtIndex:]: index 4 beyond bounds [0 .. 3]
  sum: 5
  *** -[__NSArrayM objectAtIndex:]: index 5 beyond bounds [0 .. 4]
  sum: 6
  (lldb) 
  </code></pre>