Where can I find examples of Quartz 2D drawing on the iPhone? - iphone

I am going to develope the 2D game in Iphone using Quartz.
what is the main Difference between Quartz and QuartzCore?
I have searched a lot over the internet, but only able to find out the MAC OS with Quartz Examples.
If any body has any Link/URL for Examples of Quartz(2D) using Iphone Developement,which would be run in the Real Iphone Device?
Also if possible than give the Link for Bunch of examples/repository for Quartz Iphone.
Thanks,
Mishal Shah

I think you might be confusing a few things here. Quartz lets you do static 2-D vector drawing, but this is different from the QuartzCore framework, which you import into a project if you wish to use Core Animation. Core Animation is what you're looking for when it comes to 2-D animation on the Mac or iPhone (if you aren't going to go the way of OpenGL ES). You really do not want to use Quartz to redraw a moving scene, because you will get terrible display performance.
This does get confusing, because you can use Quartz to draw 2-D vector or raster art into UIViews or CALayers, and then animate those around using Core Animation. For a good introduction to Quartz, I recommend the Quartz 2D Programming Guide. For Core Animation, there is the Core Animation Programming Guide, as well as the Core Animation book by Bill Dudney.
You're right, there is a lot of sample code out there for Quartz on the Mac, as well as for Core Animation, but the nice thing is that Quartz and Core Animation use almost identical APIs on the Mac and iPhone. There are a few small differences, but for the most part code written for one runs on the other. As an example, we designed the Core Plot framework
around Core Animation, using Quartz drawing, because we can have an almost identical codebase between our Mac and iPhone versions of the framework. That's at least one large piece of sample code that you can look at.
Finally, Opacity is a really neat utility that lets you draw vector artwork and generate the Quartz drawing code to reproduce that artwork within your application. It's a great way to try things out.

There are a number of very nice Quartz2D and CoreAnimation animation examples in this project, from the 360iDev conference session on Core Animation:
http://github.com/neror/CA360

You should read the Quartz 2D Programming Guide. After reading the guide, take a look at QuartzDemo.

Related

Advantages of using Core Graphics

I would like to know what kind of advantages I get from using Core Graphics instead of Open GL ES. My main question is based on this:
Creating simple View animations.
Creating some visual appealing objects (Graphics like Core Plot for instance, Animated Objects, etc).
Time consuming (both learning and implementing)
Simple 2D Games
Complex 2D Games
3D Games
Code maintenance ad also cleaner code.
Easier integration with other UI elements.
Thanks.
First, I want to clear up a little terminology here. When people talk about Core Graphics, they generally are referring to Quartz 2D drawing, which is a 2-D vector-based drawing API. It is used to draw out vector elements either to the screen or to offscreen contexts like PDFs. Core Animation is responsible for animation, layout, and some limited 3-D effects involving rectangular layers and UI elements. OpenGL ES is a lower-level API for talking with the graphics hardware on iOS devices for both 2-D and 3-D drawing.
You're asking a lot in your question, and the judgment on what's best in each scenario is subjective and completely up to the developer and their particular needs. I can, however, provide a few general tips.
In general, a recommendation you'll see in Apple's documentation and in presentations by engineers is that you're best off using the highest level of abstraction that solves your particular problem.
If you need to just draw a 2-D user interface, the first thing you should try is to implement this using Apple's provided UIKit elements. If they don't have the capability you need, make custom UIViews. If you are designing Mac-iOS cross-platform code (like in the Core Plot framework), you might drop down to using custom Core Animation CALayers. Each step down in this process requires you to write more code to handle things that the level above did for you.
You can do a surprising amount of stuff with Core Animation, with pretty good performance. This isn't just limited to 2-D animations, but can extend into some simple 3-D work as well.
OpenGL ES is underneath the drawing of everything you see on the screen for an iOS device, although this is not exposed to you. As such, it provides the least abstraction for onscreen rendering, and requires you to write the most code to get something done. However, it can be necessary in situations where you want to extract the most performance from 2-D display (say, in an action game) or to render true 3-D objects and environments.
Again, I tend to recommend that people start at the highest level of abstraction when writing an application, and only drop down when they find that they cannot do something or the performance is not within the specification they are trying to hit. Fewer lines of code makes applications easier to write, debug, and maintain.
That said, there are some nice frameworks that have developed around abstracting away OpenGL ES, such as cocos2D and Unity 3D, which might make working with OpenGL ES easier in many situations. For each case, you'll need to evaluate what makes sense for the particular needs of your application.
Basically, use OpenGL if you are making a game. Otherwise, use CoreGraphics. CoreGraphics lets you do simple things embedded in your normal UI code.
Creating simple View animations.
-> CG
Creating some visual appealing objects (Graphics like Core Plot for instance, Animated Objects, etc).
-> CG
Time consuming (both learning and implementing)
-> OpenGL and CG are both kind of tough at first.
Simple 2D Games
-> OpenGL
Complex 2D Games
-> OpenGL
3D Games
-> OpenGL
Code maintenance ad also cleaner code.
-> Irrelevant

Using vector graphics in iPhone games

I'm Flash/AS3 developer and I'm wondering how some iPhone developers use vector assets in their games.
For example, "Lil' Pirates": this games looks like vector-based, it's zooming and unzooming easily, but I can't get any information about using vector assets at iOS.
Quartz 2D is a pretty lightweight framework for vector based graphics. It's very well documented...
Quartz Documentation
In particular I'd pay particular notice to layering and performance...
Quartz Layering and Performance
If performance is a worry I'd also have a read through the core animation documentation. Core animation uses CALayers to cache vectors drawn with Quartz to in-memory bitmaps. These CALayers can then be transformed and translated through the animation APIs. If you intend to perform a lot of drawing this is the route I would recommend.

Learning Graphics Framework for iPhone Development

I have some development experience of CocoaTouch Layer, Core Service Layer iPhone Applications.
Now, I have to migrate for some pure 2d and 3d graphical iPhone Applications.
For that as per my knowledge, I need following frameworks to learn :
1) Core Graphics
2) OpenGL ES
3) Quartz Core
I want to know that what should be the approach to learn this framework quickly.
Or else which framework to learn and which is not, and using which source I should learn this frameworks.
Thanks in Advance
Core Graphics
For this, I'd first recommend reading the Quartz 2D Programming Guide. It's the best set of documentation you'll find out there on the subject. Next, I'd read through some of the questions here tagged core-graphics, quartz-2d, or quartz-graphics. There are some great source code examples in the answers to those questions.
OpenGL ES
There are many "getting started with OpenGL ES" questions here, with links to many resources. These include:
Books on OpenGL ES targeted towards the iPhone
Learning OpenGL ES 1.x
What do I have to learn to get done with a 3D racing game for the iPhone? What Tools do I need?
Want to display a 3D model on the iPhone: how to get started?
I personally recommend the book "Mobile 3D Graphics: with OpenGL ES and M3G", as well as the "OpenGL ES Programming Guide for iPhone OS". My application Molecules is open source and it uses OpenGL ES. Finally, Jeff LaMarche has a great tutorial series on OpenGL ES.
Core Animation
By Quartz Core, you probably mean Core Animation. Again, I'd start with Apple's Core Animation Programming Guide, then take a look at the code examples you can find here under the various core-animation tagged questions. Beyond that, Bill Dudney's Core Animation for Mac OS X and the iPhone book is a very good introduction, and Marcus Zarra and Matt Long's Core Animation book looks to go into more detail on the subject.
I'm not sure there is a quick way really. The docs are pretty vast. And you need a pretty solid knowledge of fundamental graphics principles. I had trouble getting my head round the concepts and I have used a lot of graphics toolkits.
Couple of points that might be useful:
when trying to draw a rotated image, you need to translate the coordinate transformation back to the QUARTZ origin (bottom left) not some custom origin you set up eg top-left
SO you're best off (unless you know you and no one else maintaining your code will ever want to daw rotated images) accepting the possibly new to you ideas of using bottom left coordinates (where appropriate) rather than trying to override them by changing the context CTMs
don't assume that Core Graphics can do everything you want. You may need to manipulate pixels manually yourself for some tasks
design with the memory limitations and numbers and sizes of buffers to do a processing sequence firmly in mind
design with the texture size limitation of 1024 pixels in mind
make sure you use algorithms and approaches that are within the capabilities of the CPU and/or GPU; test with a mock-up first to sound out the performance issues
if doing 3D animations in OpenGL ES make sure that you have a way to reconcile and synchronize real time with the physics model of whatever you are animating
consider colour management, shading and highlighting, pay particular attention to the device's capabilities there
where possible use Core Animation instead of Core Graphics

Is OpenGL required for my iPhone game?

On an iPhone:
If I am writing a game that has multiple levels, with multiple animations (image sequences), jpg and png (transparent), some full screen and some not, some looped and some played once only. What is the best way of doing it? Each level might have up to 10MB of images. Add on to this music, and video (cut scenes). All 2D graphics, no 3D models.
Is OpenGL required? Or can this be achieved with Quartz or Core Animation?
I do similar using UIViews and a bit of Core Graphics (Quartz 2D) and it works fine. I've found the custom drawing in Core Graphics pushes it a bit further, tho - UIViews work best when given images rather than having to draw themselves. Also watch out for lots of transparencies. You'll probably find that large or long (many frame) animations will be the killer, though. There are some techniques for minimising the impact of the animations which involves allowing it to purge images from memory if not being immediately displayed (I forget the setting). This may result in your animations not being as smooth as you they would otherwise be (not sure if Open GL ES would help here, though).
You should probably prototype using UIViews, and decide then if it's worth doing the extra work for OpenGL ES. Also, if you're not already familiar with OpenGL/ Open GL ES it's a steep learning curve.
I've used both Quartz and OpenGL to do graphics on the iPhone, and while OpenGL has a much higher learning curve, it gives much better performance than Quartz. Let's say you have a scene that involves drawing 6 large, semi-transparent images on top of each other. Quartz will do it, but you'll probably get 15fps at best. OpenGL takes advantage of the iPhone's PowerVR chip and the drawing is hardware accelerated - so you can load those images into OpenGL textures and render at 25-30fps no problem.
I would agree with Phil though - try doing it using Quartz and see if it meets your needs. OpenGL is extremely powerful but it's API lacks some of the convenience features of Quartz (such as saving/restoring graphics state).
One another note entirely, you might want to take a look at Unity's iPhone development tools (http://unity3d.com/#iphone). They leverage OpenGL but provide you with an IDE to create your game. It abstracts away all of the graphics-level code, so you can focus on the high-level gameplay. My brother uses it to write iPhone games, and it's extremely cool.
I recommend having a look at Cocos2D iPhone.
cocos2d for iPhone is a framework for building 2D games, demos, and other graphical/interactive applications. It is based on the cocos2d design: it uses the same API, but instead of using python it uses objective-c.
Most likely OpenGl.
One advantage of using OpenGL ES would be that the investment of time for learning the technology could be applied to other platforms/contexts and your game is potentially more port-friendly. These may not be important to you.
I would suggest using Quartz. OpenGL ES is really best for 3d stuff. However both work fairly well, so if you already know OpenGL ES, it's fine to use that.
You should consider using a lot of less resources in your game, Apple recommends not to use more than 10 mb in texture for openGL apps.
Try texture atlas, reuse graphics, tile based graphics...but avoid to use to much graphic assets.

Core Animation or OpenGL ES?

I want to do the following:
Tap the screen and draw 3 cricles around the the tapped point.
Is it better to do this with Core Animation or OpenGL ES?
Where do I start?
My experience is this: the more complex my app became, the more I realized I should have had used OpenGL ES for what I was trying to do.
So, for your situation, If what you described is all there is, sure, Core Graphics does the trick. But, I'm guessing there's more to it than three circles.
With no experience with OpenGL at all, the learning curve for ES was about 20 days.
Thus, my advice is: OpenGL ES for pretty much every frame-to-frame graphics based app.
As mentioned, the Core Graphics framework is probably what you want. A good way to go about it would be to subclass UIView, then override the two methods drawRect: and touchesEnded:withEvent:.
When a touch event ends on the UIView, you can get the point of the last touch from the event passed to touchesEnded:withEvent:, and store it somehow in the instance of your subclassed UIView.
Then, in your implementation of drawRect:, you'll get the stored last touch point, and draw three circles around it using three calls to CGContextAddEllipseInRect, as discussed here: Quartz 2D Programming Guide: Paths (registration as Apple Developer required).
The advantage of learning OpenGL ES is that the time you put in to learn it will serve you well in the future on iPhone Apps and on other devices.
In OpenGL ES, there's no built-in way to draw a circle, so use sine and cosine to build your circles out of line segments.
Core Graphics is definitely simpler, and better for 2D. OpenGL ES is made for 3D, but can also be used for 2D. Both can be used, so if you already know one, use that. It shouldn't really matter that much.
I already knew OpenGL, so I tend to use OpenGL ES even for 2D, but if you haven't used either before, go with Core Graphics.
This could best be done with Quartz 2D (also known as Core Graphics)
See Apple's Quartz programming guide