Reputation: 21
I am interested in making a game that depend on pixels, drawing every pixel one at a time.
But I am having an extremely hard time finding barebones source code showing how to do this. Does everything have to go through OpenGL?
I know there is a sample in the devkit, but that does not plot 1 pixel, it plots a big circle, and I need 1 pixel mapping.
Is there a barebones sample that allows a simple register of finger input and drawing 1 pixel where you hit the screen?
Upvotes: 2
Views: 2231
Reputation: 2155
Quartz2D is slow. Extremely slow especially on devices like 3G and iPod touch 2G (I am talking about almost 1 - 5 fps performance)
OpenGL is faster than Quartz2D on the iPhone.
These are the steps to follow
Upvotes: 0
Reputation: 170309
There are two common ways to draw raw pixels to the screen: rendering using OpenGL ES or drawing using Quartz 2D. Both have their advantages and disadvantages.
OpenGL ES will give you the best drawing performance when doing a lot of display updates. You can look to Apple's GLPaint sample application for an example of how to do finger-driven painting using OpenGL ES. You could even modify the brush used in that example to only be a single pixel wide, if you wanted.
However, OpenGL ES can be complex to work with and will require a lot of code to do the drawing you want, even in 2-D. You could refer to the question Learning OpenGL ES 1.x for resources to help you understand these APIs.
Quartz 2D drawing is much slower for regularly updated content, but it can be more approachable than OpenGL ES. Quartz 2D is based around vector graphics, so it's not really intended to be a pixel-by-pixel rendering framework. I give some pointers to tutorials on this framework in my answer to this question.
A third alternative, which may not be as straightforward as these two, would be to create a UIImage and manually alter its pixels in response to drawing operations, redisplaying the image after every change. Again, you'll run into the same slow performance issues as if you were working with Quartz 2D. For more on this, refer to the answers posted in this question.
Upvotes: 1
Reputation: 2592
For that you need to use line draw function,as the line is drawn pixel by pixel,you can give the points for that,here is how i did it for you.
UIGraphicsBeginImageContext(self.view.frame.size);
[testImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 12.0, 0.0, 20.0, 14.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), 2, 5);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), 2, 5);
CGContextStrokePath(UIGraphicsGetCurrentContext());
testImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIGraphicsBeginImageContext(self.view.frame.size); //you can put here testimage to get the image only
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
This will also save the image in the phone's image library. Hope this will help you.
Upvotes: 0