2

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?

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
edepot
  • 21
  • 2

3 Answers3

1

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.

Community
  • 1
  • 1
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
  • Ok, I actually started on the GLPaint example. But for the life of me, I am unable to make it plot 1 pixel, even after changing the pixel width. Either it is invisible or it ends up not plotting certain areas of the screen. Also, I looked at the frameworks used by this example, and it uses a LOT of higher level frameworks. Isn't it true the OpenGL is the lowest layer possible? If yes, adding those frameworks will end up slowing it down. Is there a barebones example to plot 1 pixel at the lowest layer with a tap? Even with GlPaint, what needs to be changed? I tried and it wouldn't work – edepot Feb 26 '11 at 07:05
0

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.

Sabby
  • 2,586
  • 2
  • 27
  • 41
0

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

  1. Set your projection to ortho to match screen resolution. glOrtho with 480,320 as width and height. (This is not absolutely necessary)
  2. Create a array of all pixels you need to draw.
  3. Make sure glPointSize is set to 1.0f
  4. Call glDrawArrays with GL_POINTS parameter.
  5. You can also use color buffer to give different colors to each pixel.
  • Thanks, but you are ahead of me. I just started, and I am looking at the GLPaint example. I am unable to change that code to make it plot 1 pixel. Sometimes nothing shows up, and when I tweak it to the lowest that can be shown, some areas are never drawn on. Is the glOrtho setup incorrectly in that example? – edepot Feb 26 '11 at 07:01
  • GLPaint sample uses a texture to draw, and it doesnt call glClear every frame. What you can try is replace that texture file with another one of size 2x2 and solid white color with no transparent areas and try – Rajavanya Subramaniyan Feb 27 '11 at 10:53