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
Related
I am trying to do dynamic drawing that draws a particle everywhere the finger is moving along the screen, exactly like in the game "Draw Something".
My intention is not to make a game, but I need this behavior for another app I am making.
Basically the behavior I am looking for is what the Apple sample code GLPaint does, simple drawing with the finger.
Now the question, I know that this can be done with OpenGL (GLPaint sample app does this), however I am new to OpenGL and I would like to use Core Graphics instead since it will only be 2D drawing.
Would it be possible to do this using Core Graphics only?, If so would performance be affected?. Some sample code for something like this using Core Graphics would be appreciated.
Is the best way to do this in Core Graphics to keep drawing a particle over and over again?.
Thank you,
Oscar
Have you considered using something like Cocos2D to achieve the OpenGL drawing? It's quite intuitive to pick up.
I'm sorry, I have no idea how easy/hard/performance hitting using CoreGraphics would be.
I am relatively new to doing graphics work on the iPhone and was hoping to get some guidance on which technology I should employ. The issue I am facing is that I need to create an iPhone app that will have a spinning wheel in it, think the Price Is Right wheel.
I am not looking for anyone to provide a detailed solution, that would spoil my fun! I would simply like to know would it be better to use straight Cocoa Animation and draw and move each cell by hand as the wheel spins, or should I attempt to use Core Animation Layers? I have also thought of using OpenGL ES and building a true cylinder in 3D and then rotating that, this seems the most logical to me but again I am new to all of this.
For situations like this, I usually recommend looking first to Core Animation and only going to OpenGL ES if you can't get the kind of performance you need. Redrawing a wheel frame-by-frame using only Quartz will result in abysmal performance, so that's not really an option here.
Using Core Animation, you can perform complex animations, such as your rotation, without requiring much code. You also can leverage UIViews and other UIKit components to provide touch interaction easily. You don't even necessarily need to step down to using layers, as every UIView is layer-backed and they tend to be pretty lightweight.
OpenGL ES will give you the best possible performance, but I wouldn't think you'd run into a limitation when simply spinning a wheel using Core Animation, even on older devices. OpenGL ES will require a lot more code to achieve the same effect, and will be more difficult to integrate with any touch interaction you may wish to do.
Based on the feedback I have dug deeper into Core Animation and have been able to accomplish what I need using it. Thanks for all the advice.
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.
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.
I've seen a lot of bandying about what's better, Quartz or OpenGL ES for 2D gaming. Neverminding libraries like Cocos2D, I'm curious if anyone can point to resources that teach using OpenGL ES as a 2D platform. I mean, are we really stating that learning 3D programming is worth a slight speed increase...or can it be learned from a 2D perspective?
GL is likely to give you better performance, with less CPU usage, battery drain, and so on. 2D drawing with GL is just like 3D drawing with GL, you just don't change the Z coordinate.
That being said, it's easier to write 2D drawing code with Quartz, so you have to decide the trade-off.
Cribbed from a similar answer I provided here:
You probably mean Core Animation when you say Quartz. Quartz handles static 2-D drawing within views or layers. On the iPhone, all Quartz drawing for display is done to a Core Animation layer, either directly or through a layer-backed view. Each time this drawing is performed, the layer is sent to the GPU to be cached. This re-caching is an expensive operation, so attempting to animate something by redrawing it each frame using Quartz results in terrible performance.
However, if you can split your graphics into sprites whose content doesn't change frequently, you can achieve very good performance using Core Animation. Each one of those sprites would be hosted in a Core Animation CALayer or UIKit UIView, and then animated about the screen. Because the layers are cached on the GPU, basically as textures, they can be moved around very smoothly. I've been able to move 50 translucent layers simultaneously at 60 FPS (100 at 30 FPS) on the original iPhone (not 3G S).
You can even do some rudimentary 3-D layout and animation using Core Animation, as I show in this sample application. However, you are limited to working with flat, rectangular structures (the layers).
If you need to do true 3-D work, or want to squeeze the last bit of performance out of the device, you'll want to look at OpenGL ES. However, OpenGL ES is nowhere near as easy to work with as Core Animation, so my recommendation has been to try Core Animation first and switch to OpenGL ES only if you can't do what you want. I've used both in my applications, and I greatly prefer working with Core Animation.