views:

659

answers:

1

Hi,

I have a game which is an iPhone adaptation of this game. Here my ship makes paths and then the enclosed path gets filled if there is no enemy present in that enclosed area.

I have taken the playField as a 2D array (int playField[300][300]).

When the color of the enclosed path needs to be changed to show that it is filled, the path of the ship is flagged 2 and the area enclosed is flagged 1. So i have fill the area where

playFieldCountX=300, playFieldCountY=300;
for (int i=0; i<playFieldCountX; i++) {
 for (int j=0; j<playFieldCountY; j++) {
  if(playField[i][j]==0){
                        "What comes here??"    }

I am currently filling the path by CGContextMoveToPoint, AddLineToPoint and then StrokePath of Quartz which is taking too much time.

Is there anything more simple? Like predefining a 1x1 image and rendering it at every pixel? Or just getting and setting the color of each pixel?

PS: I dont know OpenGL. If what you explain relates to OpenGL, please be more detailed about it. Thank you.

+3  A: 

You should create a CGBitmapContext, which takes a data buffer to store the image data in. When you create the CGBitmapContext, you will specify what format to store the pixel data in.

Then to change the picture, you can simply modify the buffer, and then recreate the UIImage from it to draw it to the screen.

NilObject
I am creating a CGBitmapContext in the init method of the view: CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();_myContext = CGBitmapContextCreate(NULL, self.frame.size.width, self.frame.size.height, 8, 4 * self.frame.size.width, colorSpace, kCGImageAlphaPremultipliedFirst);And then doing this in the drawRect method: CGContextRef currentContext = UIGraphicsGetCurrentContext(); CGImageRef image = CGBitmapContextCreateImage(_myContext); CGContextDrawImage(currentContext, [self bounds], image); CGContextDrawLayerInRect(currentContext, [self bounds], _myLayer);
But i cant figure out what you are telling me to do. Are you sure i can change the color of each and every pixel individually by this?Sorry for the congested comment above.
Don't pass NULL for the first parameter. malloc up a chunk of memory (width * height * 4). This data pointer is how you can draw and modify the buffer directly. For example: data[x + y * width] = 0xFF0000 would set a red pixel at x,y (assuming data is a int *).
NilObject
You may learn full sample here:http://www.markj.net/iphone-uiimage-pixel-color/
MikZ