Easiest way to create layer filled with image pattern in cocos2d v3 - ios

I am using cocos2d v3 which has easy to access functionality to create CCSprites (images), CCDrawNode (primitives) and other types of layer like objects.
But I haven't found examples how to create simple layer (i.e. 100x100px) which filled by smaller image (i.e. 4x4px).
What is the easiest way to solve this problem?
Thanks in advice.

I don't know if you would consider this the easiest way, but I think it is the fastest from a performance point of view:
Subclass CCNode and implement -(void)draw. Inside draw you can make custom openGL calls, bind a texture with GL_REPEAT as parameter and draw it.
You can find a good Intro and examples to openGL-es here: http://iphonedevelopment.blogspot.de/2009/05/opengl-es-from-ground-up-part-6_25.html
The easiest way I can think of btw would be to have some for loops creating millions of CCSprites filling the screen. But I wouldn't recommend that ;)

CCTexture* texture = [[CCTextureCache sharedTextureCache] addImage:#"bg.png"];
CCSprite *bgNode = [CCSprite spriteWithTexture:texture
rect:CGRectMake(0, 0, _rootNode.contentSize.width, _appCtrl.winSize.height)];
bgNode.position = ccp(bgNode.contentSize.width / 2.0f, bgNode.contentSize.height / 2.0f);
ccTexParams params = {GL_LINEAR,GL_LINEAR,GL_REPEAT,GL_REPEAT};
[bgNode.texture setTexParameters:&params];
In Cocos2d v3 Unit Tests sources I have found another way to solve this problem.
Note: you must do
#import "CCTexture_Private.h"

Related

Dynamic Bezier drawing in a spritekit game - Best (performance) approach?

I am not an iOS developer, I just started to implement my game, that I have done in flash, hoping that it will be super fast using the native environment.
The project is a 2d game that has a lot of dynamic bezier drawing based on the user's interaction. Basically I draw dynamic blobs (amoeba type of shapes).
First I tried Swift, which is very similar to actionscript, but it turned out, Apple won't be able to accept apps built with Swift before the final release of Xcode6 and as I want release my game before September I went to objective C route.
I wanted to use sprite kit because of the integrated physics engine, sprite hierarchy, etc.
I tried to use SKSphapeNode for drawing, but I realised it very quickly, it is not suitable for my needs. (cannot draw a stroke if it is thicker than 2 pixels, it has memory leaks etc.)
So I used UIBezierPath that I put into an UIImage, but I am not happy with the performance as I have to create a new UIImage with the new dynamically generated bezier shape.
These are the options I found so far:
SkShapeNode - not suitable for my game
UIBezierPath - UIImage > I have to create a new UIImage every frame, so it is slow
OpenGL - I haven't tried it yet, I am not sure it is possible to use it with Sprite kit
CALayer - I am trying to integrate it with spritekit at the moment, but I have a feeling I will have the same problem that I had with the UIImage approach.
Does anybody have any idea, tip what approach would be the best performance-wise?
Thank you for your help in advance!
I tried to create a lightning effect with using SpriteKit, so check out my article about that: https://andreygordeev.com/2014/11/01/lightning-with-srite-kit.html
Some of the approaches use UIBezierPath, so may be you find that useful.

What is the difference between CGContextAddLineToPoint and addLineToPoint function in UIBezierPath?

I have rendered some basic shapes using quartz-2d. I came across two methods to draw a line. First is to get a context using UIGraphicsGetCurrentContext() and then draw the line using CGContextAddLineToPoint.
And other way is to define a UIBezierPath object and draw using its function addLineToPoint
[bezierPath addLineToPoint:CGPointMake(10, 10)];
And then I have to add bezierPath to the context using CGContextAddPath.
So I wanted to know the difference between these two approaches as both are used to draw only a line. Is there a performance issue between these two? Also which method is better under what circumstances.
The UIBezierPath is an object from the UIKit, and allow you to use a set of function mixing particular curve with control point but also with a normal line.
Being an object from the UIKit you are not able to use CGContextAddLineToPoint, but anyway at the end you need to add the path in the context with CGContextAddPath.
With CGContexAddLineToPoint instead you draw directly on the context.
So my suggest is to use this last way if you have not particular reasons (like amazing curve with different control point). Otherwise, use UIBezierPath.
The CGContextAdd.. functions are lower-level C Quartz2D API that bridge between CGContext and CGPathRef instances. You use a CGContext when you want to draw something, while CGPathRef is the structure that manages geometric shapes.
On the other hand, UIBezierPath is an Objective-C UIKit class that wraps around CGPath, and provides also some bridge to CGContext functions, for example with setFill, setStroke, fill or stroke methods.
Is totally safe to mix the two approaches, but it's sure that CGContext gives you more tools. Performance and memory are better with direct CGContext calls, but performance difference are absolutely negligible in 99% of the cases.

How to Efficiently Draw Shadows on Numerous CALayers in iOS?

I've got some card games which use CALayers to draw individual cards. There can easily be 40 or 50 of them on the screen, which usually works fine.
I recently tried to turn on their shadows using the simple properties for CALayers:
theCardLayer.shadowOffset = CGSizeMake(3,2);
theCardLayer.shadowOpacity = 0.7f;
At that point, the program started getting really laggy. Fair enough; some of the docs said that the shadows could be CPU-intensive.
Any ideas for how to efficiently draw shadows on everything? They're all on the same CALayer in the same UIView, so I'm wondering if there might be a way to pull the mask of the layer or its UIView and shadow that, or something ...
Any functionality up to iOS5 is fair game.
At the very least, try setting your layer's shadowPath property. It can make shadow rendering significantly faster.
Kurt offered up the correct solution. Here's an example of how to use a shadowPath:
UIBezierPath *thisCLPath = [UIBezierPath
bezierPathWithRoundedRect:theCardLayer.bounds
cornerRadius:10.0f];
theCardLayer.shadowPath = thisCLPath.CGPath;
Clearly, I'm using rounded corners here. For a straight-edged layer, you can just use bezierPathWithRect:. There are a few other helpful methods in UIBezierPath as well.
The result is just the right side of laggy on older iOS devices (like an iPhone4 or a mid-generation iPod Touch) and blazing on an iPad3.

Making a Drawable layer in cocos2d

I want to make sort of a chalk board for part of my app, and I was wondering how to accomplish this?
I was thinking I could create a sprite and have it's image set to something very small (maybe a small point), and then add a new instance of that sprite everywhere the user touches to simulate a draw event. Something like [self addChild:someSprite]; for each touch location.
But it seems like that would be extremely memory inefficient. There has to be a better way than that, Maybe drawing actual lines? I'm probably overlooking some method.
Thanks for any help.
You need to use CCRenderTexture for chalk board paintings. Check this article & project for a drawing example.
Your variant isn't such "memory inefficient" as you think. No matter how much sprites will you create with the same texture, your texture will be placed to the memory only once. And all the sprites will use pointer to it. Just one thing to prevent many unnessesary calls is to use CCBatchNode. It will draw all it's children with single draw call. Without using it, draw will be called on every children.

iOS. Has Anyone Found a Way to KeyFrame UIView Attributes?

One of the things sorely lacking from iOS is the ability to set keys on an attribute/attributes and interpolate between them using a spline. Has anyone thought about this and/or found an approach to get around this? Is everyone using Cocos2d for this sort of thing? Cocos2d is way more capability then I need?
Thanks,
Doug
Check out the CAKeyframeAnimation class. UIViews use Core Animation layers, so a Core Animation...er...animation that you apply to a view’s layer gets applied to its contents as well. Most appearance-related properties on CALayer are animatable—obvious ones like frame and opacity, for instance, but also things like backgroundColor and cornerRadius.

Resources