How to make your own recorded file in GLPaint sample

I recently downloaded a sample GLPaint code and looked at a very interesting part. There is a registered NSMutableArray file in which there are dots that are then read and drawn by GLPaint.

It is indicated here:

NSMutableArray *recordedPaths; recordedPaths = [NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Recording" ofType:@"data"]]; if([recordedPaths count]) [self performSelector:@selector(playback:) withObject:recordedPaths afterDelay:0.2]; 

This is the code to play:

  - (void) playback:(NSMutableArray*)recordedPaths { NSData* data = [recordedPaths objectAtIndex:0]; CGPoint* point = (CGPoint*)[data bytes]; NSUInteger count = [data length] / sizeof(CGPoint), i; //Render the current path for(i = 0; i < count - 1; ++i, ++point) [self renderLineFromPoint:*point toPoint:*(point + 1)]; //Render the next path after a short delay [recordedPaths removeObjectAtIndex:0]; if([recordedPaths count]) [self performSelector:@selector(playback:) withObject:recordedPaths afterDelay:0.01]; } 

From this I understand that recordsPaths is a mutable array in which CGPoint arrays are built in it, which are then read and rendered. I would like to add my own array, and I had problems with this.

I tried changing the declaration of the recorded folders to this:

  NSMutableArray *myArray = [[NSMutableArray alloc] init]; CGPoint* points; CGPoint a = CGPointMake(50,50); int i; for (i=0; i<100; i++,points++) { a = CGPointMake(i,i); points = &a; } NSData *data = [NSData dataWithBytes:&points length:sizeof(*points)]; [myArray addObject:data]; 

That didn't work though ... Any tips?

+4
source share
3 answers

If you look at Recording.data, you will notice that each line is its own array. To capture ink and reproduce them, you will need an array of arrays. For the purposes of this demo, declare a mutable array - writRay

  @synthesize writRay; //init in code writRay = [[NSMutableArray alloc]init]; 

Ink capture

 // Handles the continuation of a touch. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { CGRect bounds = [self bounds]; UITouch* touch = [[event touchesForView:self] anyObject]; // Convert touch point from UIView referential to OpenGL one (upside-down flip) if (firstTouch) { firstTouch = NO; previousLocation = [touch previousLocationInView:self]; previousLocation.y = bounds.size.height - previousLocation.y; /******************* create a new array for this stroke points **************/ [writRay addObject:[[NSMutableArray alloc]init]]; /***** add 1st point *********/ [[writRay objectAtIndex:[writRay count] -1]addObject:[NSValue valueWithCGPoint:previousLocation]]; } else { location = [touch locationInView:self]; location.y = bounds.size.height - location.y; previousLocation = [touch previousLocationInView:self]; previousLocation.y = bounds.size.height - previousLocation.y; /********* add additional points *********/ [[writRay objectAtIndex:[writRay count] -1]addObject:[NSValue valueWithCGPoint:previousLocation]]; } // Render the stroke [self renderLineFromPoint:previousLocation toPoint:location]; } 

Reproduction of ink.

 - (void)playRay{ if(writRay != NULL){ for(int l = 0; l < [writRay count]; l++){ //replays my writRay -1 because of location point for(int p = 0; p < [[writRay objectAtIndex:l]count] -1; p ++){ [self renderLineFromPoint:[[[writRay objectAtIndex:l]objectAtIndex:p]CGPointValue] toPoint:[[[writRay objectAtIndex:l]objectAtIndex:p + 1]CGPointValue]]; } } } } 

For a better effect, shake the screen to clear and call playRay from changeBrushColor in the AppController.

+6
source
  CGPoint* points; CGPoint a = CGPointMake(50,50); int i; for (i=0; i<100; i++,points++) { a = CGPointMake(i,i); points = &a; } NSData *data = [NSData dataWithBytes:&points length:sizeof(*points)]; 

Incorrect code.

(1) You need an array of points. Just a CGPoint* points; declaration CGPoint* points; won't create an array of points, just an uninitialized CGPoint pointer. You must allocate space for the array using

 CGPoint points[100]; 

or

 CGPoint* points = malloc(sizeof(CGPoint)*100); 

Remember the free points if you chose the malloc method.

(2) To copy a value to the contents of a pointer, you need to use

 *points = a; 

But I suggest you keep the points pointer invariant in a loop, since you are going to reuse it later. Use the syntax of the points[i] array.

(3)

 sizeof(*points) 

Since *points is just one CGPoint, therefore sizeof is always 8 bytes. You need to multiply the result by 100 to get the correct length.

(4)

  [NSData dataWithBytes:&points ... 

points already a pointer to the actual data. You no longer need to take the address. Just pass points directly.


So the final code should look like

  CGPoint* points = malloc(sizeof(CGPoint)*100); // make a cast if the compiler warns. CGPoint a; int i; for (i=0; i<100; i++) { a = CGPointMake(i,i); points[i] = a; } NSData *data = [NSData dataWithBytes:points length:sizeof(*points)*100]; free(points); 
+1
source

I just read this post as I am trying to achieve a similar thing. With changes to the original Apple project, I was able to create a new "form" by changing the code accordingly.

Note that he only draws a diagonal line ... several times. But the theory is to create your own drawing.

I took the code from the KennyTM message and included it in the payback function, this could be adapted to create an array in the initWithCoder function, and then send it as the original code, but for now - this will be the result.

  CGPoint* points = malloc(sizeof(CGPoint)*100); CGPoint a; int iter; for (iter=0; iter<200; iter++) { a = CGPointMake(iter,iter); points[iter] = a; } NSData *data = [NSData dataWithBytes:points length:sizeof(*points)*100]; free(points); CGPoint* point = (CGPoint*)[data bytes]; NSUInteger count = [data length] / sizeof(CGPoint), i; for(i = 0; i < count - 1; ++i, ++point) [self renderLineFromPoint:*point toPoint:*(point + 1)]; [recordedPaths removeObjectAtIndex:0]; if([recordedPaths count]) [self performSelector:@selector(playback:) withObject:recordedPaths afterDelay:0.01]; 

I'm still in my early weeks on openGL coding, so goodbye to any glaring errors / bad methods and thanks for the help!

Hope this helps

+1
source

Source: https://habr.com/ru/post/1302643/


All Articles