如何在 GLPaint 示例代码中制作自己的记录路径
我最近下载了 GLPaint 示例代码并查看了其中非常有趣的部分。有一个recordedPaths NSMutableArray,其中有一些点,然后由GLPaint 读取和绘制。
它在这里声明:
NSMutableArray *recordedPaths;
recordedPaths = [NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Recording" ofType:@"data"]];
if([recordedPaths count])
[self performSelector:@selector(playback:) withObject:recordedPaths afterDelay:0.2];
这是用于播放的代码:
- (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];
}
从这里我了解到,recordedPaths 是一个可变数组,其中包含 CGPoint 的 struct c 数组,然后读取和渲染这些数组。 我想放入我自己的数组,但我一直遇到麻烦。
我尝试将 RecordedPaths 声明更改为:
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];
但这不起作用...... 有什么建议吗?
I've recently downloaded the GLPaint sample code and looked at a very interesting part in it. There is a recordedPaths NSMutableArray that has points in it that are then read and drawn by GLPaint.
It's declared 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 for playback:
- (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 recordedPaths is a mutable array that his in it struct c arrays of CGPoint that are then read and rendered.
I'd like to put in my own array and i've been having trouble with that.
I tried changing the recordedPaths declaration 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];
This didn't work though...
Any advice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您查看 Recording.data,您会发现每一行都是它自己的数组。要捕获墨水并回放它,您需要有一个数组数组。出于本演示的目的 - 声明一个可变数组 - writRay
捕获墨迹
播放墨迹。
为了获得最佳效果,请摇动屏幕以清除并从 AppController 中的 ChangeBrushColor 调用 playRay。
If you look at the Recording.data you will notice that each line is its own array. To capture the ink and play it back you need to have an array of arrays. For purposes of this demo - declare a mutable array - writRay
Capture the ink
Playback the ink.
For best effect shake the screen to clear and call playRay from changeBrushColor in the AppController.
代码错误。
(1) 您需要一个点数组。简单地声明
CGPoint*points;
不会创建一个点数组,只是一个未初始化的CGPoint指针。 如果您选择malloc
方式,则需要使用以下方式为数组分配空间,或者
记住
free
点。(2) 要将值复制到您需要使用的指针的内容
,但我建议您在循环中保持指针
points
不变,因为您将重用稍后再说。使用数组语法points[i]
。(3)
由于
*points
只是一个CGPoint,因此sizeof始终为8字节。您需要将结果乘以 100 才能得到正确的长度。(4)
points
已经是指向实际数据的指针。您无需再次获取它的地址。只需直接传递点
即可。所以最终的代码应该是这样的
Wrong code.
(1) You need an array of points. Simply declaring
CGPoint* points;
won't create an array of points, just an uninitialized pointer of CGPoint. You need to allocate space for the array either withor
Remember to
free
the points if you choose themalloc
way.(2) To copy value to the content of the pointer you need to use
But I suggest you keep the pointer
points
invariant in the loop, since you're going to reuse it later. Use the array syntaxpoints[i]
.(3)
Since
*points
is just one CGPoint, so the sizeof is always 8 bytes. You need to multiply the result by 100 to get the correct length.(4)
points
already is a pointer to the actual data. You don't need to take the address of it again. Just passpoints
directly.So the final code should look like
我刚刚读了这篇文章,因为我正在努力实现类似的目标。通过苹果对原始项目的修改,我能够通过相应地修改代码来创建一个新的“形状”。
请注意,它只绘制对角线......几次。但理论是用来创建你自己的图画的。
我从 KennyTM 的帖子中获取了代码并将其合并到“payback”函数中,这可以调整为在 initWithCoder 函数中创建数组,然后像原始代码一样发送它,但现在 - 这会给你一个结果。
我仍处于 openGL 编码的第一周,所以请原谅任何明显的错误/糟糕的方法,并感谢您的帮助!
希望这有帮助
I was just reading over this post as I am trying to achive a similar thing. With modifications to the original project by Apple, I was able to create a new 'shape' by modifying the code accordingly.
Note that it only draws a diagonal line... several times. But the theory is there to create your own drawing.
I've taken the code from KennyTM's post and incorporated it into the 'payback' function, this could be adapted to create the array in the initWithCoder function and then send it through like the original code but for now - this will get you a result.
I am still in my first weeks on openGL coding, so forgive any glaring mistakes / bad methods and thanks for the help!
Hope this helps