Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confusion: SKSpriteNode & SKTexture difference.

I am confused with SKSpriteNode & SKTexture Node. I have seen in the tutorials that SKSpriteNode can be used to add image like [SKSpriteNode spritenodewithimagename:@"someimag"]; and same thing is happening in SKTexture as [SKTexture texturewithimge/file];

what is difference between a Texture and Image. If we are adding image by using SKSpriteNode then what is the reason to use SKTexture or if we use SKTexture and Texture Atlases then why we added image to be added in SKSpriteNode.

Confusion is there, what is difference between both of them.

like image 333
Programmer Avatar asked Sep 28 '13 05:09

Programmer


1 Answers

SKSpriteNode is a node that displays (renders) a SKTexture on screen at a given position, with optional scaling and stretching.

SKTexture is a storage class that contains an image file in a format that is suitable for rendering, plus additional information like the frame rectangle if the texture references only a smaller portion of the image / texture atlas.

One reason for splitting the two is that you usually want multiple sprites to draw with the same SKTexture or from the same SKTextureAtlas. This avoids having to keep copies of the same image in memory for each individual sprite, which would easily become prohibitive. For example a 4 MB texture used by 100 Sprites still uses 4 MB of memory, as opposed to 400 MB.

Update to answer comment:

The term 'texture' dates back to the 70s.

A texture is an in-memory representation of an image formatted specifically for use in rendering. Common image formats (PNG, JPG, GIF, etc.) don't lend themselves well for rendering by a graphics chip. Textures are an "image format" that graphics hardware and renderers such as OpenGL understand and have standardized.

If you load a PNG or JPG into a texture, the format of the image changes. It's color depth, alpha channel, orientation, memory layout, compression method may change. Additional data may be introduced such as mip-map levels, which is the original texture scaled down by a certain percentage in order to draw farther-away polygons with a lower resolution version of the same texture, which decreases aliasing and speeds up rendering.

That's only scratching the surface though. What's important to keep in mind is that no rendering engine works with images directly, they're always converted into textures. This has mainly to do with efficiency of the rendering process.

Whenever you specify an image directly in an API such as Sprite Kit, for example spriteWithImageNamed: then internally what happens is that the renderer first checks if there's an existing texture with the given image name, and if so, uses that. If there's no such image loaded yet, it will load the image, convert it to a texture, and store it with the image name as key for future reference (this is called texture caching).

like image 148
LearnCocos2D Avatar answered Sep 19 '22 11:09

LearnCocos2D