Something about the development of iPhone games (graphics)

Source: Internet
Author: User

For the graphics section, I first read the document
Quartz 2D programming guide
Http://developer.apple.com/iphone/library/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/Introduction/chapter_1_section_1.html
Another example is quartzdemo.
We recommend that you first check
First, in-(void) drawrect :( cgrect) rect, get context through cgcontextref context = uigraphicsgetcurrentcontext ();
For more information, see cgcontext reference.
Http://developer.apple.com/iphone/library/documentation/GraphicsImaging/Reference/CGContext/Reference/reference.html

Here I have encapsulated my own rendering class seimage, which is mainly used for easy plotting,
First, load the image
Drag the image to resources.
Uiimage * timg = [uiimage imagenamed: @ "wood.png"];
Cgimageref image = cgimageretain (timg. cgimage );

Then you can draw
+ (Void) drawimage :( cgcontextref) Context
Image :( cgimageref) aimage
Atpoint :( cgpoint) apoint
{
Cgrect imagerect;
Imagerect. Origin = apoint;
Imagerect. size =
Cgsizemake (cgimagegetwidth (aimage ),
Cgimagegetheight (aimage ));
Cgcontextdrawimage (context, imagerect, aimage );
}
This is to draw an image at the specified position.

Various Image Rendering methods are required in the game, such as symmetry, scaling, rotation,

Here. We need to save the current context status cgcontextsavegstate (context) before performing the transformation operation, and restore cgcontextrestoregstate (context) after the transformation );

Zoom: I understand it like this. He zoomed in your canvas. The image is also enlarged. So the coordinate system at which you draw the position is also enlarged. Therefore, when processing coordinates, we need to consider the transformation of coordinates,

+ (Void) drawimage :( cgcontextref) Context
Image :( cgimageref) aimage
Atpoint :( cgpoint) apoint
Zoom :( float) azoom
{
Cgcontextsavegstate (context );

Cgaffinetransform at =
Cgaffinetransformscale (at, azoom, azoom );
Cgcontextconcatctm (context, );

[Seimage drawimage: Context
Image: aimage atpoint: cgpointmake (apoint. x * azoom, apoint. y * azoom)];
Cgcontextrestoregstate (context );
}

However, you can consider using your drawing point as the baseline position to zoom in, so you don't need to worry about the changes in coordinates for any scaling.

+ (Void) drawimage :( cgcontextref) Context
Image :( cgimageref) aimage
Atpoint :( cgpoint) apoint
Zoom :( float) azoom
{
Cgcontextsavegstate (context );

Cgaffinetransform at =
Cgaffinetransformmaketranslation (apoint. X, apoint. Y); // move and zoom in to draw as the reference

At = cgaffinetransformscale (at, azoom, azoom );
Cgcontextconcatctm (context, );

[Seimage drawimage: Context
Image: aimage atpoint: cgpointmake (0.0, 0.0)]; // draw on the benchmark
Cgcontextrestoregstate (context );
}

I used 2nd methods and unified this approach in my drawing functions.

Below is the rotation,
Move the rotating point to the rotating center
Rotate,
Then draw an image at the drawing point. Note: The image is drawn based on the drawing point instead of the rotation center.

+ (Void) drawimage :( cgcontextref) Context
Image :( cgimageref) aimage
Atpoint :( cgpoint) apoint // specifies the position of the image.
Rolateatpoint :( cgpoint) arolatepoint // coordinates of the rotation center relative to the drawing point
Rolate :( float) arolate // Angle
{
Cgcontextsavegstate (context );

Cgaffinetransform at =
Cgaffinetransformmaketranslation (apoint. x + arolatepoint. X, apoint. Y + arolatepoint. y );
At = cgaffinetransformrotate (at, arolate * m_pi/180 );
Cgcontextconcatctm (context, );

[Seimage drawimage: Context
Image: aimage atpoint: cgpointmake (-arolatepoint. X,-arolatepoint. y)];

Cgcontextrestoregstate (context );
}

About symmetric transformations. You can set X to-1.0 for processing. At this time, it should be noted that my painting is based on the coordinate of the watermark, so after the conversion, We need to subtract the image width. I scaled and flipped at the same time, used to deal with the flip problem of any enlarged image

+ (Void) drawimage :( cgcontextref) Context
Image :( cgimageref) aimage
Atpoint :( cgpoint) apoint
Flipx :( bool) aflipx
{
If (aflipx)
{
[Seimage drawimage: Context
Image: aimage atpoint: apoint
Zoom: 1.0
Flipx: aflipx];
} Else {
[Seimage drawimage: Context
Image: aimage atpoint: apoint];
}
}

+ (Void) drawimage :( cgcontextref) Context
Image :( cgimageref) aimage
Atpoint :( cgpoint) apoint
Zoom :( float) azoom
Flipx :( bool) aflipx // whether to perform X flip
{
Cgcontextsavegstate (context );

Cgaffinetransform at =
Cgaffinetransformmaketranslation (apoint. X, apoint. y );

If (aflipx)
{
At = cgaffinetransformscale (at,-azoom, azoom );
Cgcontextconcatctm (context, );

[Seimage drawimage: Context
Image: aimage atpoint: cgpointmake (0.0-cgimagegetwidth (aimage), 0.0)];
}
Else {
At = cgaffinetransformscale (at, azoom, azoom );
Cgcontextconcatctm (context, );

[Seimage drawimage: Context
Image: aimage atpoint: cgpointmake (0.0, 0.0)];

}

Cgcontextrestoregstate (context );

}

The following functions process various transformation requirements + (void) drawimage :( cgcontextref) Context // draw Device
Image :( cgimageref) aimage // Image
Atpoint :( cgpoint) apoint // draw position (upper left corner)
Rolateatpoint :( cgpoint) arolatepoint // rotation center (coordinates relative to the drawn position)
Rolate :( float) arolate // Angle
Zoom :( float) azoom // magnification (year-on-year amplification)
Flipx :( bool) aflipx // process the flip
{
Cgcontextsavegstate (context );

Cgaffinetransform at =
Cgaffinetransformmaketranslation (apoint. x + arolatepoint. X, apoint. Y + arolatepoint. y );
At = cgaffinetransformrotate (at, arolate * m_pi/180 );
If (aflipx)
{
At = cgaffinetransformscale (at,-azoom, azoom );
}
Else {
At = cgaffinetransformscale (at, azoom, azoom );
}
Cgcontextconcatctm (context, );

[Seimage drawimage: Context
Image: aimage atpoint: cgpointmake (-arolatepoint. X,-arolatepoint. y)];

Cgcontextrestoregstate (context );
}

The attachment contains the seimage class,
+ (Void) drawgrid :( cgcontextref) Context
Drawrect :( cgrect) arect
Gridsize :( cgsize) asize;
It is used to draw asize-sized grids within a fixed range.

Please kindly advise

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.