tags:

views:

151

answers:

1

The basic idea
is very easy. Simplified you could say... a snake like line realized by a let's say 3px line is expanding across the screen collecting and interacting with different stuff, can be steered through user input. Like a continous line you would draw with a pen.

I've already started reading apple's documentation on Quartz/CG. As I understand now, I need to put my rendering code into a UIView's drawRect. Then I would need to set a timer (found some answers/posts here and there, nothing concrete though) that fires x times per second and calls setNeedsDisplay on the UIView.

Question:
How to realize the following: Have whole snake on UIView 1 / (Layer ?), draw new part on UIView 2, merge them, so that the new part gets appended to UIView 1 (or CALayer instead of views ?). I ask explicitly about this cause I read, that one shouldn't redraw the same content over and over again, but just the new/moving part. I hope you can provide me some sample code or some details which classes I should use and the strategy on how to use them / which calls to make.


Edit
OK, I see that my idea or what I've read before to realize this with Quartz and different views is not so wise... ( Daniel Bleisteiner )
So I switched to OpenGL now, well I'm looking into it, reading examples, Jeff LaMarche's OpenGL blog entries, etc.. I guess I would be able to draw my line. I think I would create classes for curves, straight lines / direction changes, etc. and then on user input I would create the related objects (depending on the steer input) store them in an array and then recreate and redraw the whole line by reading the object properties from the objects stored in the array on each frame. A simple line I would draw like this (code from Beginning iPhone Development)

glDisable(GL_TEXTURE_2D);
GLfloat vertices[4];    
// Convert coordinates
vertices[0] = start.x;
vertices[1] = start.y;
vertices[2] = end.x;
vertices[3] = end.y;
glLineWidth(3.0);
glVertexPointer (2, GL_FLOAT , 0, vertices);
glDrawArrays (GL_LINES, 0, 2);

Maybe I will even find a way to antialias it but

  1. now I'm more curious if my idea is good enough or if there are some better established strategies for this
  2. and maybe someone could tell me how to seperate code for hud's, the line drawing itself and some menus that I will have to display f.e. at the beginning ... so that it's easy to transition from one "view" to another (like with a fade)? Some hints ?

Maybe you have also read a book, that will explain how to solve this kind of problems? Or you can point me to another good answer / example code as I have huge problems in finding "animated drawing" examples.

this got me a little further, but it is still a little vague for me. I don't know how to realize "update path that you draw (only with needed points + one last point that moves)"

A: 

Don't try to merge different views... setNeedsDisplay has a rect parameter that tells the core graphics part that only a certain part of the screen needs to be rendered again. Respect this parameter in your drawRect method and it should be enough for standard 2D games and tools.

If you intent to use intense graphics there is no other option than to use OpenGL. The performance is multiple times better and you won't have to care about optimizations... OpenGL does much in the background.

Daniel Bleisteiner
Indeed the graphics could get more intense over the time, so maybe I should really choose OpenGL.With OpenGL I would simply draw the same line (including the continuos growing part) over and over again and wouldn't have to worry about performance issues, or would I? And so I would only store the path in an array and redraw using the stored data in the array on each frame, right ? Is this what you suggested ?
Allisone
With OpenGL you won't draw yourself... you maintain a scene with objects, lightsources and cameras and OpenGL does the drawing all by itself. There exist some helpful classes that reduce the complexity of OpenGL to use it for easy 2D scenes without real lightning and such stuff. I don't have a link at hand but came over such things myself earlier. I think there was a class called OpenGLES2DView somewhere - maybe provided by Apple itself...
Daniel Bleisteiner