SPTexture Class Reference
| Inherits from | NSObject |
| Declared in | SPTexture.h |
Overview
A texture stores the information that represents an image. It cannot be displayed directly,
but has to be mapped onto a display object. In Sparrow, that display object is SPImage.
Image formats
Sparrow supports different file formats for textures. The most common formats are PNG, which
contains an alpha channel, and JPG (without an alpha channel). You can also load files in
the PVR format (compressed or uncompressed). That’s a special format of the graphics chip of
iOS devices that is very efficient.
HD textures
Furthermore, Sparrow supports development in multiple resolutions, i.e. creating a game
simultaneously for normal and retina displays. If HD texture support is activated (it is by default
if you start Sparrow with [SPViewController startWithRoot:]) and you load a texture like this:
[[SPTexture alloc] initWithContentsOfFile:@"image.png"];
Sparrow will check if it finds the file image@2x.png, and will load that instead (provided that
it is available and the application is running on a HD device). The texture object will then
return values for width and height that are the original number of pixels divided by 2
(by setting their scale-factor to 2.0). That way, you will always work with the same values
for width and height - regardless of the device type.
It is also possible to switch textures depending on the device the app is executed on. The
convention is to add a device modifier (~ipad or ~iphone) to the image name, directly before
the file extension (and after the scale modifier, if there is one).
Drawing API
Sparrow lets you create custom graphics directly at run-time by using the Core Graphics API.
You access the drawing API with a special init-method of SPTexture, which takes a block-parameter
you can fill with your drawing code.
SPTexture *customTexture = [[SPTexture alloc] initWithWidth:200 height:100
draw:^(CGContextRef context)
{
// draw a string
CGContextSetGrayFillColor(context, 1.0f, 1.0f);
NSString *string = @"Hello Core Graphics";
[string drawAtPoint:CGPointMake(20.0f, 20.0f)
withFont:[UIFont fontWithName:@"Arial" size:25]];
}];
Texture Frame
The frame property of a texture allows you to define the position where the texture will appear
within an SPImage. The rectangle is specified in the coordinate system of the texture:
SPTexture *baseTexture = [SPTexture textureWithContentsOfFile:@"10x10.png"];
SPRectangle *frame = [SPRectangle rectangleWithX:-10 y:-10 width:30 height:30];
SPTexture *texture = [SPTexture textureWithRegion:nil frame:frame ofTexture:baseTexture];
SPImage *image = [SPImage imageWithTexture:texture];
This code would create an image with a size of 30x30, with the texture placed at x=10, y=10 within that image (assuming that the base texture has a width and height of 10 pixels, it would appear in the middle of the image). This is especially useful when a texture has transparent areas at its sides. It is then possible to crop the texture (removing the transparent edges) and make up for that by specifying a frame.
The texture class itself does not make any use of the frame data. It’s up to classes that use
SPTexture to support that feature.
Tasks
Initialization
-
– initWithWidth:height:Initializes an empty texture with a certain size (in points).
-
– initWithWidth:height:draw:Initializes a texture with a certain size (in points), as well as a block containing Core Graphics commands. The texture will have the current scale factor of the stage; no mipmaps will be created.
-
– initWithWidth:height:generateMipmaps:draw:Initializes a texture with a certain size (in points), as well as a block containing Core Graphics commands. The texture will have the current scale factor of the stage.
-
– initWithWidth:height:generateMipmaps:scale:draw:Initializes a texture with a certain size (in points), as well as a block containing Core Graphics commands.
-
– initWithContentsOfFile:Initializes a texture with the contents of a file (supported formats: png, jpg, pvr); no mip maps will be created. Sparrow will automatically pick the optimal file for the current system, using standard iOS naming conventions (“@2x”, “~ipad” etc). If the file name ends with “.gz”, the file will be uncompressed automatically.
-
– initWithContentsOfFile:generateMipmaps:Initializes a texture with the contents of a file (supported formats: png, jpg, pvr). Sparrow will automatically pick the optimal file for the current system, using standard iOS naming conventions (“@2x”, “~ipad” etc). If the file name ends with “.gz”, the file will be uncompressed automatically.
-
– initWithContentsOfImage:Initializes a texture with the contents of a UIImage; no mip maps will be created. The texture will have the same scale factor as the image.
-
– initWithContentsOfImage:generateMipmaps:Initializes a texture with the contents of a UIImage. The texture will have the same scale factor as the image.
-
– initWithRegion:ofTexture:Initializes a texture with a region (in points) of another texture. The new texture will reference the base texture; no data is duplicated.
-
– initWithRegion:frame:ofTexture:Initializes a texture with a region (in points) of another texture, as well as a frame rectangle that makes up for trimmed parts (see class description). The new texture will reference the base texture; no data is duplicated.
-
+ textureWithContentsOfFile:Factory method.
-
+ textureWithContentsOfFile:generateMipmaps:Factory method.
-
+ textureWithRegion:ofTexture:Factory method.
-
+ textureWithWidth:height:draw:Factory method.
-
+ emptyTextureFactory method. Creates an empty (transparent) texture.
Methods
-
– adjustVertexData:atIndex:numVertices:Converts texture coordinates and vertex positions of raw vertex data into the format required for rendering.
-
– adjustTexCoords:numVertices:stride:Converts texture coordinates stored at the given memory region into the format required for rendering. While the texture coordinates of an image always use the range [0, 1], the actual coordinates could be different: you might be working with a SubTexture. This method adjusts the coordinates accordingly.
-
– adjustPositions:numVertices:stride:Moves the position coordinates stored at the given memory region into the format required for rendering. This happens for SubTextures that contain a ‘frame’.
Loading Textures asynchronously
-
+ loadFromFile:onComplete:Loads a texture asynchronously from a local file and executes a callback block when it’s finished. No mip maps will be created; premultiplied alpha state is guessed by file type.
-
+ loadFromFile:generateMipmaps:onComplete:Loads a texture asynchronously from a local file and executes a callback block when it’s finished.
-
+ loadFromURL:onComplete:Loads a texture asynchronously from an URL and executes a callback block when it’s finished. The url will be used exactly as it is passed; no mip maps are created. The scale factor will be parsed from the file name (default: 1).
-
+ loadFromURL:generateMipmaps:onComplete:Loads a texture asynchronously from an URL and executes a callback block when it’s finished. The url will be used exactly as it is passed; the scale factor will be parsed from the file name (default: 1).
-
+ loadFromURL:generateMipmaps:scale:onComplete:Loads a texture asynchronously from an URL and executes a callback block when it’s finished. The url will be used exactly as it is passed (i.e. no scale factor suffix will be added).
-
+ loadFromSuffixedURL:onComplete:Loads a texture asynchronously from an URL and executes a callback block when it’s finished. The method adds a suffix with the current scale factor to the url (e.g.
@2x). If that resource is not found, the method will fail. No mip maps are created. -
+ loadFromSuffixedURL:generateMipmaps:onComplete:Loads a texture asynchronously from an URL and executes a callback block when it’s finished. The method adds a suffix with the current scale factor to the url (e.g.
@2x). If that resource is not found, the method will fail.
Properties
-
widthThe width of the image in points.
property -
heightThe height of the image in points.
property -
nativeWidthThe width of the texture in pixels (without scale adjustment).
property -
nativeHeightThe height of the texture in pixels (without scale adjustment).
property -
rootThe SPGLTexture this texture is based on.
property -
nameThe OpenGL texture identifier.
property -
premultipliedAlphaIndicates if the alpha values are premultiplied into the RGB values.
property -
scaleThe scale factor, which influences
propertywidthandheightproperties. -
formatThe OpenGL texture format of this texture.
property -
mipmapsIndicates if the texture contains mipmaps.
property -
frameThe frame indicates how the texture should be displayed within an image. (Default:
propertynil) -
repeatIndicates if the texture should repeat like a wallpaper or stretch the outermost pixels. Note: this makes sense only in textures with sidelengths that are powers of two and that are not loaded from a texture atlas (i.e. no subtextures). (Default:
propertyNO) -
smoothingThe smoothing type influences how the texture appears when it is scaled up or down. (Default:
propertySPTextureSmoothingBilinear)
Properties
format
The OpenGL texture format of this texture.
@property (nonatomic, readonly) SPTextureFormat formatDiscussion
The OpenGL texture format of this texture.
Declared In
SPTexture.hframe
The frame indicates how the texture should be displayed within an image. (Default: nil)
@property (nonatomic, readonly) SPRectangle *frameDiscussion
The frame indicates how the texture should be displayed within an image. (Default: nil)
Declared In
SPTexture.hheight
The height of the image in points.
@property (nonatomic, readonly) float heightDiscussion
The height of the image in points.
Declared In
SPTexture.hmipmaps
Indicates if the texture contains mipmaps.
@property (nonatomic, readonly) BOOL mipmapsDiscussion
Indicates if the texture contains mipmaps.
Declared In
SPTexture.hname
The OpenGL texture identifier.
@property (nonatomic, readonly) uint nameDiscussion
The OpenGL texture identifier.
Declared In
SPTexture.hnativeHeight
The height of the texture in pixels (without scale adjustment).
@property (nonatomic, readonly) float nativeHeightDiscussion
The height of the texture in pixels (without scale adjustment).
Declared In
SPTexture.hnativeWidth
The width of the texture in pixels (without scale adjustment).
@property (nonatomic, readonly) float nativeWidthDiscussion
The width of the texture in pixels (without scale adjustment).
Declared In
SPTexture.hpremultipliedAlpha
Indicates if the alpha values are premultiplied into the RGB values.
@property (nonatomic, readonly) BOOL premultipliedAlphaDiscussion
Indicates if the alpha values are premultiplied into the RGB values.
Declared In
SPTexture.hrepeat
Indicates if the texture should repeat like a wallpaper or stretch the outermost pixels.
Note: this makes sense only in textures with sidelengths that are powers of two and that are
not loaded from a texture atlas (i.e. no subtextures). (Default: NO)
@property (nonatomic, assign) BOOL repeatDiscussion
Indicates if the texture should repeat like a wallpaper or stretch the outermost pixels.
Note: this makes sense only in textures with sidelengths that are powers of two and that are
not loaded from a texture atlas (i.e. no subtextures). (Default: NO)
Declared In
SPTexture.hroot
The SPGLTexture this texture is based on.
@property (nonatomic, readonly) SPGLTexture *rootDiscussion
The SPGLTexture this texture is based on.
Declared In
SPTexture.hscale
The scale factor, which influences width and height properties.
@property (nonatomic, readonly) float scaleDiscussion
The scale factor, which influences width and height properties.
Declared In
SPTexture.hsmoothing
The smoothing type influences how the texture appears when it is scaled up or down.
(Default: SPTextureSmoothingBilinear)
@property (nonatomic, assign) SPTextureSmoothing smoothingDiscussion
The smoothing type influences how the texture appears when it is scaled up or down.
(Default: SPTextureSmoothingBilinear)
Declared In
SPTexture.hClass Methods
emptyTexture
Factory method. Creates an empty (transparent) texture.
+ (instancetype)emptyTextureDiscussion
Factory method. Creates an empty (transparent) texture.
Declared In
SPTexture.hloadFromFile:generateMipmaps:onComplete:
Loads a texture asynchronously from a local file and executes a callback block when it’s finished.
+ (void)loadFromFile:(NSString *)path generateMipmaps:(BOOL)mipmaps onComplete:(SPTextureLoadingBlock)callbackDiscussion
Loads a texture asynchronously from a local file and executes a callback block when it’s finished.
Declared In
SPTexture.hloadFromFile:onComplete:
Loads a texture asynchronously from a local file and executes a callback block when it’s finished. No mip maps will be created; premultiplied alpha state is guessed by file type.
+ (void)loadFromFile:(NSString *)path onComplete:(SPTextureLoadingBlock)callbackDiscussion
Loads a texture asynchronously from a local file and executes a callback block when it’s finished. No mip maps will be created; premultiplied alpha state is guessed by file type.
Declared In
SPTexture.hloadFromSuffixedURL:generateMipmaps:onComplete:
Loads a texture asynchronously from an URL and executes a callback block when it’s finished.
The method adds a suffix with the current scale factor to the url (e.g. @2x). If that resource
is not found, the method will fail.
+ (void)loadFromSuffixedURL:(NSURL *)url generateMipmaps:(BOOL)mipmaps onComplete:(SPTextureLoadingBlock)callbackDiscussion
Loads a texture asynchronously from an URL and executes a callback block when it’s finished.
The method adds a suffix with the current scale factor to the url (e.g. @2x). If that resource
is not found, the method will fail.
Declared In
SPTexture.hloadFromSuffixedURL:onComplete:
Loads a texture asynchronously from an URL and executes a callback block when it’s finished.
The method adds a suffix with the current scale factor to the url (e.g. @2x). If that resource
is not found, the method will fail. No mip maps are created.
+ (void)loadFromSuffixedURL:(NSURL *)url onComplete:(SPTextureLoadingBlock)callbackDiscussion
Loads a texture asynchronously from an URL and executes a callback block when it’s finished.
The method adds a suffix with the current scale factor to the url (e.g. @2x). If that resource
is not found, the method will fail. No mip maps are created.
Declared In
SPTexture.hloadFromURL:generateMipmaps:onComplete:
Loads a texture asynchronously from an URL and executes a callback block when it’s finished. The url will be used exactly as it is passed; the scale factor will be parsed from the file name (default: 1).
+ (void)loadFromURL:(NSURL *)url generateMipmaps:(BOOL)mipmaps onComplete:(SPTextureLoadingBlock)callbackDiscussion
Loads a texture asynchronously from an URL and executes a callback block when it’s finished. The url will be used exactly as it is passed; the scale factor will be parsed from the file name (default: 1).
Declared In
SPTexture.hloadFromURL:generateMipmaps:scale:onComplete:
Loads a texture asynchronously from an URL and executes a callback block when it’s finished. The url will be used exactly as it is passed (i.e. no scale factor suffix will be added).
+ (void)loadFromURL:(NSURL *)url generateMipmaps:(BOOL)mipmaps scale:(float)scale onComplete:(SPTextureLoadingBlock)callbackDiscussion
Loads a texture asynchronously from an URL and executes a callback block when it’s finished. The url will be used exactly as it is passed (i.e. no scale factor suffix will be added).
Declared In
SPTexture.hloadFromURL:onComplete:
Loads a texture asynchronously from an URL and executes a callback block when it’s finished. The url will be used exactly as it is passed; no mip maps are created. The scale factor will be parsed from the file name (default: 1).
+ (void)loadFromURL:(NSURL *)url onComplete:(SPTextureLoadingBlock)callbackDiscussion
Loads a texture asynchronously from an URL and executes a callback block when it’s finished. The url will be used exactly as it is passed; no mip maps are created. The scale factor will be parsed from the file name (default: 1).
Declared In
SPTexture.htextureWithContentsOfFile:
Factory method.
+ (instancetype)textureWithContentsOfFile:(NSString *)pathDiscussion
Factory method.
Declared In
SPTexture.htextureWithContentsOfFile:generateMipmaps:
Factory method.
+ (instancetype)textureWithContentsOfFile:(NSString *)path generateMipmaps:(BOOL)mipmapsDiscussion
Factory method.
Declared In
SPTexture.hInstance Methods
adjustPositions:numVertices:stride:
Moves the position coordinates stored at the given memory region into the format required for rendering. This happens for SubTextures that contain a ‘frame’.
- (void)adjustPositions:(void *)data numVertices:(int)count stride:(int)strideParameters
- data
A pointer to the first coordinate pair (
xandygiven as floats).
- count
The number of coordinate pairs.
- stride
The byte offset between consecutive coordinate pairs. If
strideis 0, the coordinates are tightly packed.
Discussion
Moves the position coordinates stored at the given memory region into the format required for rendering. This happens for SubTextures that contain a ‘frame’.
Declared In
SPTexture.hadjustTexCoords:numVertices:stride:
Converts texture coordinates stored at the given memory region into the format required for rendering. While the texture coordinates of an image always use the range [0, 1], the actual coordinates could be different: you might be working with a SubTexture. This method adjusts the coordinates accordingly.
- (void)adjustTexCoords:(void *)data numVertices:(int)count stride:(int)strideParameters
- data
A pointer to the first coordinate pair (
uandvgiven as floats).
- count
The number of coordinate pairs.
- stride
The byte offset between consecutive coordinate pairs. If
strideis 0, the coordinates are tightly packed.
Discussion
Converts texture coordinates stored at the given memory region into the format required for rendering. While the texture coordinates of an image always use the range [0, 1], the actual coordinates could be different: you might be working with a SubTexture. This method adjusts the coordinates accordingly.
Declared In
SPTexture.hadjustVertexData:atIndex:numVertices:
Converts texture coordinates and vertex positions of raw vertex data into the format required for rendering.
- (void)adjustVertexData:(SPVertexData *)vertexData atIndex:(int)index numVertices:(int)countDiscussion
Converts texture coordinates and vertex positions of raw vertex data into the format required for rendering.
Declared In
SPTexture.hinitWithContentsOfFile:
Initializes a texture with the contents of a file (supported formats: png, jpg, pvr); no mip maps will be created. Sparrow will automatically pick the optimal file for the current system, using standard iOS naming conventions (“@2x”, “~ipad” etc). If the file name ends with “.gz”, the file will be uncompressed automatically.
- (instancetype)initWithContentsOfFile:(NSString *)pathDiscussion
Initializes a texture with the contents of a file (supported formats: png, jpg, pvr); no mip maps will be created. Sparrow will automatically pick the optimal file for the current system, using standard iOS naming conventions (“@2x”, “~ipad” etc). If the file name ends with “.gz”, the file will be uncompressed automatically.
Declared In
SPTexture.hinitWithContentsOfFile:generateMipmaps:
Initializes a texture with the contents of a file (supported formats: png, jpg, pvr). Sparrow will automatically pick the optimal file for the current system, using standard iOS naming conventions (“@2x”, “~ipad” etc). If the file name ends with “.gz”, the file will be uncompressed automatically.
- (instancetype)initWithContentsOfFile:(NSString *)path generateMipmaps:(BOOL)mipmapsDiscussion
Initializes a texture with the contents of a file (supported formats: png, jpg, pvr). Sparrow will automatically pick the optimal file for the current system, using standard iOS naming conventions (“@2x”, “~ipad” etc). If the file name ends with “.gz”, the file will be uncompressed automatically.
Declared In
SPTexture.hinitWithContentsOfImage:
Initializes a texture with the contents of a UIImage; no mip maps will be created. The texture will have the same scale factor as the image.
- (instancetype)initWithContentsOfImage:(UIImage *)imageDiscussion
Initializes a texture with the contents of a UIImage; no mip maps will be created. The texture will have the same scale factor as the image.
Declared In
SPTexture.hinitWithContentsOfImage:generateMipmaps:
Initializes a texture with the contents of a UIImage. The texture will have the same scale factor as the image.
- (instancetype)initWithContentsOfImage:(UIImage *)image generateMipmaps:(BOOL)mipmapsDiscussion
Initializes a texture with the contents of a UIImage. The texture will have the same scale factor as the image.
Declared In
SPTexture.hinitWithRegion:frame:ofTexture:
Initializes a texture with a region (in points) of another texture, as well as a frame rectangle that makes up for trimmed parts (see class description). The new texture will reference the base texture; no data is duplicated.
- (instancetype)initWithRegion:(SPRectangle *)region frame:(SPRectangle *)frame ofTexture:(SPTexture *)textureDiscussion
Initializes a texture with a region (in points) of another texture, as well as a frame rectangle that makes up for trimmed parts (see class description). The new texture will reference the base texture; no data is duplicated.
Declared In
SPTexture.hinitWithRegion:ofTexture:
Initializes a texture with a region (in points) of another texture. The new texture will reference the base texture; no data is duplicated.
- (instancetype)initWithRegion:(SPRectangle *)region ofTexture:(SPTexture *)textureDiscussion
Initializes a texture with a region (in points) of another texture. The new texture will reference the base texture; no data is duplicated.
Declared In
SPTexture.hinitWithWidth:height:
Initializes an empty texture with a certain size (in points).
- (instancetype)initWithWidth:(float)width height:(float)heightDiscussion
Initializes an empty texture with a certain size (in points).
Declared In
SPTexture.hinitWithWidth:height:draw:
Initializes a texture with a certain size (in points), as well as a block containing Core Graphics commands. The texture will have the current scale factor of the stage; no mipmaps will be created.
- (instancetype)initWithWidth:(float)width height:(float)height draw:(SPTextureDrawingBlock)drawingBlockDiscussion
Initializes a texture with a certain size (in points), as well as a block containing Core Graphics commands. The texture will have the current scale factor of the stage; no mipmaps will be created.
Declared In
SPTexture.hinitWithWidth:height:generateMipmaps:draw:
Initializes a texture with a certain size (in points), as well as a block containing Core Graphics commands. The texture will have the current scale factor of the stage.
- (instancetype)initWithWidth:(float)width height:(float)height generateMipmaps:(BOOL)mipmaps draw:(SPTextureDrawingBlock)drawingBlockDiscussion
Initializes a texture with a certain size (in points), as well as a block containing Core Graphics commands. The texture will have the current scale factor of the stage.
Declared In
SPTexture.hinitWithWidth:height:generateMipmaps:scale:draw:
Initializes a texture with a certain size (in points), as well as a block containing Core Graphics commands.
- (instancetype)initWithWidth:(float)width height:(float)height generateMipmaps:(BOOL)mipmaps scale:(float)scale draw:(SPTextureDrawingBlock)drawingBlockDiscussion
Initializes a texture with a certain size (in points), as well as a block containing Core Graphics commands.
Declared In
SPTexture.h