1
0
mirror of https://github.com/thes3m/XNI synced 2024-12-26 13:26:06 +01:00

All around update and fixes

git-svn-id: http://xni.googlecode.com/svn/XNI@104 ac433895-eea3-a490-d80a-17149a75e588
This commit is contained in:
Matej Jan 2012-05-14 08:40:49 +00:00
parent e56e806481
commit d04d7bf8f3
34 changed files with 365 additions and 98 deletions

BIN
Classes/.DS_Store vendored

Binary file not shown.

View File

@ -12,6 +12,8 @@
@interface SoundEffect (Internal)
@property (nonatomic, readonly) NSUInteger bufferID;
+ (void) update;
@end

View File

@ -7,6 +7,7 @@
//
#import "SoundEffect.h"
#import "SoundEffect+Internal.h"
#import "Retronator.Xni.Framework.Audio.h"
@ -68,6 +69,10 @@ static float masterVolume = 1;
@synthesize duration, name;
- (NSUInteger)bufferID {
return bufferID;
}
+ (float) speedOfSound {
return speedOfSound;
}
@ -125,7 +130,7 @@ static float masterVolume = 1;
}
- (SoundEffectInstance *) createInstance {
return [[[SoundEffectInstance alloc] initWithBufferID:bufferID] autorelease];
return [[[SoundEffectInstance alloc] initWithSoundEffect:self] autorelease];
}
- (BOOL) play {

View File

@ -11,6 +11,6 @@
@interface SoundEffectInstance (Internal)
- (id) initWithBufferID:(NSUInteger)bufferID;
- (id) initWithSoundEffect:(SoundEffect*)soundEffect;
@end

View File

@ -16,6 +16,7 @@
@private
BOOL isLooped;
float pan, pitch, volume;
SoundEffect *parent;
NSUInteger sourceID;
}

View File

@ -8,18 +8,21 @@
#import "SoundEffectInstance.h"
#import "SoundEffectInstance+Internal.h"
#import "SoundEffect+Internal.h"
@implementation SoundEffectInstance
- (id) initWithBufferID:(NSUInteger)bufferID
- (id) initWithSoundEffect:(SoundEffect *)soundEffect
{
self = [super init];
if (self != nil) {
parent = [soundEffect retain];
// grab a source ID from openAL
alGenSources(1, &sourceID);
// attach the buffer to the source
alSourcei(sourceID, AL_BUFFER, bufferID);
alSourcei(sourceID, AL_BUFFER, parent.bufferID);
// set some basic source prefs
alSourcef(sourceID, AL_PITCH, 1.0f);
@ -84,6 +87,7 @@
- (void) dealloc
{
alDeleteSources(1, &sourceID);
[parent release];
[super dealloc];
}

View File

@ -15,4 +15,12 @@
@synthesize sourceFilename;
@synthesize sourceTool;
- (void)dealloc
{
[fragmentIdentifier release];
[sourceFilename release];
[sourceTool release];
[super dealloc];
}
@end

View File

@ -29,4 +29,7 @@
- (void) loadContent;
- (void) unloadContent;
- (void) onVisibleChanged;
- (void) onDrawOrderChanged;
@end

View File

@ -30,7 +30,7 @@
- (void) setVisible:(BOOL)value {
if (visible != value) {
visible = value;
[visibleChanged raiseWithSender:self];
[self onVisibleChanged];
}
}
@ -38,7 +38,7 @@
- (void) setDrawOrder:(int)value {
if (drawOrder != value) {
drawOrder = value;
[drawOrderChanged raiseWithSender:self];
[self onDrawOrderChanged];
}
}
@ -60,6 +60,14 @@
- (void) drawWithGameTime:(GameTime*)gameTime {}
- (void) unloadContent {}
- (void)onVisibleChanged {
[visibleChanged raiseWithSender:self];
}
- (void)onDrawOrderChanged {
[drawOrderChanged raiseWithSender:self];
}
- (void) dealloc
{
if (contentLoaded) {

View File

@ -46,9 +46,7 @@
NSMutableArray *enabledComponents;
NSMutableArray *visibleComponents;
NSMutableArray *componentsList;
NSMutableSet *initializedComponents;
// Services
GameServiceContainer *services;

View File

@ -14,6 +14,8 @@
#import "Retronator.Xni.Framework.Content.h"
#import "TouchPanel+Internal.h"
#import "GameWindow+Internal.h"
#import "GameViewController.h"
#import "GameView.h"
#import "Guide+Internal.h"
#import "SoundEffect+Internal.h"
@ -57,9 +59,7 @@ static NSArray *drawOrderSort;
// First it is used for constructing a list of components, that need to be initialized.
// In run it is used to make a copy of enabled/visible components for enumerating over them.
componentsList = [[NSMutableArray alloc] init];
initializedComponents = [[NSMutableSet alloc] init];
[components.componentAdded subscribeDelegate:
[Delegate delegateWithTarget:self Method:@selector(componentAddedTo:eventArgs:)]];
@ -84,7 +84,7 @@ static NSArray *drawOrderSort;
[Guide initializeWithGame:self];
// Get the game host.
gameHost = (GameHost*)[UIApplication sharedApplication];
gameHost = (GameHost*)[UIApplication sharedApplication];
}
return self;
@ -130,15 +130,26 @@ static NSArray *drawOrderSort;
inRun = YES;
[self beginRun];
// First update with zero gameTime.
[self updateWithGameTime:gameTime];
lastFrameTime = [[NSDate alloc] init];
// First frame with zero gameTime.
[self updateWithGameTime:gameTime];
if ([self beginDraw]) {
[self drawWithGameTime:gameTime];
[self endDraw];
}
// Force redraw.
[self.window.gameViewController.gameView layoutSubviews];
// Run the game host with a delay event, so we don't block this method.
[gameHost performSelector:@selector(run) withObject:nil afterDelay:0];
}
- (void) tick {
if (!lastFrameTime) {
lastFrameTime = [[NSDate alloc] init];
}
// Sleep if inactive.
if (!isActive) {
CFRunLoopRunInMode(kCFRunLoopDefaultMode, inactiveSleepTime, NO);
@ -195,7 +206,8 @@ static NSArray *drawOrderSort;
- (void) applicationDidFinishLaunching:(UIApplication *)application {
NSLog(@"Application has started.");
[self performSelector:@selector(run) withObject:nil afterDelay:0];
[self run];
}
- (void) applicationWillResignActive:(UIApplication *)application
@ -235,7 +247,6 @@ static NSArray *drawOrderSort;
while ([componentsList count] > 0) {
id<IGameComponent> component = [componentsList objectAtIndex:0];
[component initialize];
[initializedComponents addObject:component];
[componentsList removeObjectAtIndex:0];
}
initializeDone = YES;
@ -270,7 +281,7 @@ static NSArray *drawOrderSort;
- (void) endDraw {
[graphicsDeviceManager endDraw];
}
- (void) unloadContent {}
- (void) endRun {}
@ -310,10 +321,7 @@ static NSArray *drawOrderSort;
- (void) componentAddedTo:(GameComponentCollection*)sender eventArgs:(GameComponentCollectionEventArgs*)e {
// Initialize component if it's being added after main initialize has been called.
if (initializeDone) {
if (![initializedComponents containsObject:e.gameComponent]) {
[e.gameComponent initialize];
[initializedComponents addObject:e.gameComponent];
}
[e.gameComponent initialize];
} else {
[componentsList addObject:e.gameComponent];
}
@ -411,7 +419,6 @@ static NSArray *drawOrderSort;
[self unloadContent];
[gameTime release];
[initializedComponents release];
[componentsList release];
[enabledComponents release];
[visibleComponents release];

View File

@ -26,4 +26,7 @@
@property (nonatomic, readonly) Game *game;
- (void) onEnabledChanged;
- (void) onUpdateOrderChanged;
@end

View File

@ -32,7 +32,7 @@
- (void) setEnabled:(BOOL)value {
if (enabled != value) {
enabled = value;
[enabledChanged raiseWithSender:self];
[self onEnabledChanged];
}
}
@ -40,7 +40,7 @@
- (void) setUpdateOrder:(int)value {
if (updateOrder != value) {
updateOrder = value;
[updateOrderChanged raiseWithSender:self];
[self onUpdateOrderChanged];
}
}
@ -49,6 +49,14 @@
- (void) initialize {}
- (void) onEnabledChanged {
[enabledChanged raiseWithSender:self];
}
- (void) onUpdateOrderChanged {
[updateOrderChanged raiseWithSender:self];
}
- (void) updateWithGameTime:(GameTime*)gameTime {}
- (void) dealloc

View File

@ -18,7 +18,7 @@
self = [super init];
if (self) {
[MediaPlayer load];
window = [[GameWindow alloc] init];
window = [[GameWindow alloc] init];
}
return self;
}

View File

@ -13,6 +13,7 @@
@interface BasicEffectPass : EffectPass {
BasicEffect *basicEffect;
ReachGraphicsDevice *reachGraphicsDevice;
}
- (id) initWithBasicEffect:(BasicEffect*)theBasicEffect graphicsDevice:(GraphicsDevice*)theGraphicsDevice;
@ -124,6 +125,7 @@
self = [super initWithName:@"BasicEffectPass" graphicsDevice:theGraphicsDevice];
if (self) {
basicEffect = theBasicEffect;
reachGraphicsDevice = (ReachGraphicsDevice*)theGraphicsDevice;
}
return self;
}
@ -153,7 +155,7 @@
// Set texturing.
if (basicEffect.textureEnabled) {
[graphicsDevice.textures setItem:basicEffect.texture atIndex:0];
glActiveTexture(GL_TEXTURE0);
//glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
} else {
glDisable(GL_TEXTURE_2D);
@ -188,10 +190,9 @@
}
- (void) activateLight:(DirectionalLight *)light name:(uint)lightName {
if (light.enabled) {
glEnable(lightName);
} else {
glDisable(lightName);
[reachGraphicsDevice setLight:lightName to:light.enabled];
if (!light.enabled) {
return;
}

View File

@ -41,6 +41,7 @@
SamplerStateCollection *samplerStates;
TextureCollection *textures;
Viewport *viewport;
int activeTextureIndex;
// Events
Event *deviceResetting;

View File

@ -86,10 +86,12 @@
self.blendFactor = [Color white];
self.blendState = [BlendState opaque];
self.depthStencilState = [DepthStencilState defaultDepth];
glDepthRangef(0, 1);
graphicsDeviceStatus = GraphicsDeviceStatusNormal;
self.indices = nil;
self.rasterizerState = [RasterizerState cullCounterClockwise];
self.referenceStencil = 0;
activeTextureIndex = -1;
[samplerStates setItem:[SamplerState linearClamp] atIndex:0];
// Create events.
@ -108,30 +110,54 @@
@synthesize blendState;
- (void) setBlendState:(BlendState*)value {
if (value != blendState) {
[value retain];
[blendState release];
blendState = value;
BlendState *old = blendState;
blendState = [value retain];
// Apply the blend state.
glBlendFunc(blendState.colorSourceBlend, blendState.colorDestinationBlend);
if (old.colorSourceBlend != blendState.colorSourceBlend ||
old.colorDestinationBlend != blendState.colorDestinationBlend ||
old.alphaSourceBlend != blendState.alphaSourceBlend ||
old.alphaDestinationBlend != blendState.alphaDestinationBlend) {
glBlendFuncSeparate(blendState.colorSourceBlend,
blendState.colorDestinationBlend,
blendState.alphaSourceBlend,
blendState.alphaDestinationBlend);
}
if (old.colorBlendFunction != blendState.colorBlendFunction ||
old.alphaBlendFunction != blendState.alphaBlendFunction) {
glBlendEquationSeparate(blendState.colorBlendFunction,
blendState.alphaBlendFunction);
}
[old release];
}
}
@synthesize depthStencilState;
- (void) setDepthStencilState:(DepthStencilState*)value {
if (value != depthStencilState) {
[value retain];
[depthStencilState release];
depthStencilState = value;
DepthStencilState *old = depthStencilState;
depthStencilState = [value retain];
// Apply depth state.
FLAG_BLOCK(depthStencilState.depthBufferEnable, GL_DEPTH_TEST)
glDepthFunc(depthStencilState.depthBufferFunction);
glDepthMask(depthStencilState.depthBufferWriteEnable);
glDepthRangef(0, 1);
if (old.depthBufferEnable != depthStencilState.depthBufferEnable) {
FLAG_BLOCK(depthStencilState.depthBufferEnable, GL_DEPTH_TEST)
}
if (old.depthBufferFunction != depthStencilState.depthBufferFunction) {
glDepthFunc(depthStencilState.depthBufferFunction);
}
if (old.depthBufferWriteEnable != depthStencilState.depthBufferWriteEnable) {
glDepthMask(depthStencilState.depthBufferWriteEnable);
}
// TODO: Apply stencil state.
[old release];
}
}
@ -337,7 +363,7 @@
[GraphicsDevice getFormat:&format AndType:&type ForSurfaceFormat:texture.format];
glBindTexture(GL_TEXTURE_2D, texture.textureId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
@ -348,7 +374,7 @@
} else {
glTexImage2D(GL_TEXTURE_2D, level, format, texture.width, texture.height, 0, format, type, data);
}
glBindTexture(GL_TEXTURE_2D, 0);
}
@ -452,7 +478,10 @@
}
- (void) applySamplerState:(id)sender eventArgs:(XniSamplerEventArgs*)e {
glActiveTexture(e.samplerIndex);
if (activeTextureIndex != e.samplerIndex) {
activeTextureIndex = e.samplerIndex;
glActiveTexture(GL_TEXTURE0 + e.samplerIndex);
}
Texture *texture = [textures itemAtIndex:e.samplerIndex];

View File

@ -18,11 +18,11 @@ typedef enum {
} Blend;
typedef enum {
BlendFunctionAdd,
BlendFunctionMax,
BlendFunctionMin,
BlendFunctionReverseSubstract,
BlendFunctionSubtract
BlendFunctionAdd = GL_FUNC_ADD,
//BlendFunctionMax = GL_MAX,
//BlendFunctionMin = GL_MIN,
BlendFunctionReverseSubstract = GL_FUNC_REVERSE_SUBTRACT,
BlendFunctionSubtract = GL_FUNC_SUBTRACT
} BlendFunction;
typedef enum {

View File

@ -12,6 +12,9 @@
@interface ReachGraphicsDevice : GraphicsDevice {
BOOL lightsActive[8];
}
- (void) setLight:(uint)lightname to:(BOOL)value;
@end

View File

@ -38,6 +38,18 @@
@implementation ReachGraphicsDevice
- (id)initWithGame:(Game *)theGame {
self = [super initWithGame:theGame];
if (self) {
// Disable lights.
for (int i=0; i<8; i++) {
lightsActive[i] = false;
glDisable(GL_LIGHT0 + i);
}
}
return self;
}
- (EAGLContext*) createContext {
return [[[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1] autorelease];
}
@ -59,7 +71,7 @@
minVertexIndex:(int)minVertexIndex
numVertices:(int)numVertices
startIndex:(int)startIndex
primitiveCount:(int)primitiveCount;
primitiveCount:(int)primitiveCount
{
[self enableVertexBuffers];
@ -176,6 +188,19 @@
}
- (void)setLight:(uint)lightname to:(BOOL)value {
int index = lightname - GL_LIGHT0;
if (value != lightsActive[index]) {
lightsActive[index] = value;
if (value) {
glEnable(lightname);
} else {
glDisable(lightname);
}
}
}
// Private methods
- (void) enableVertexBuffers {

View File

@ -12,6 +12,8 @@
#import "SpriteFont+Internal.h"
#import "XniAdaptiveArray.h"
typedef struct {
float x;
float y;
@ -60,6 +62,8 @@ static inline void SpriteSetSource(XniSprite *sprite, Rectangle *source, Texture
} else {
sprite->source.width = 1;
sprite->source.height = 1;
sprite->source.x = 0;
sprite->source.y = 0;
}
if (effects & SpriteEffectsFlipHorizontally) {
@ -76,8 +80,12 @@ static inline void SpriteSetSource(XniSprite *sprite, Rectangle *source, Texture
static inline void SpriteSetVertices(XniSprite *sprite, float positionX, float positionY, float originX, float originY, float scaleX, float scaleY, float rotation, float width, float height) {
float x = originX * scaleX;
float y = -originY * scaleY;
float c = cos(rotation);
float s = sin(rotation);
float c = 1;
float s = 0;
if (rotation) {
c = cosf(rotation);
s = sinf(rotation);
}
sprite->position.x = positionX - x * c - y * s;
sprite->position.y = positionY - x * s + y * c;
sprite->width.x = width * scaleX * c;
@ -90,7 +98,9 @@ static inline void SpriteSetDestinationFast(XniSprite *sprite, Rectangle *destin
sprite->position.x = destination.x;
sprite->position.y = destination.y;
sprite->width.x = destination.width;
sprite->width.y = 0;
sprite->height.y = destination.height;
sprite->height.x = 0;
}
static inline void SpriteSetDestination(XniSprite *sprite, Rectangle *destination, float originX, float originY, float rotation) {
@ -113,7 +123,7 @@ static inline void SpriteSetPosition(XniSprite *sprite, Vector2 *position, float
- (void) setProjection;
- (void) apply;
- (void) draw:(XniSprite*)sprite;
void draw(XniSprite *sprite, NSMutableArray *sprites, SpriteSortMode sortMode, SpriteBatch *it);
- (void) draw;
- (void) drawFrom:(int)startIndex to:(int)endIndex;
@ -121,6 +131,30 @@ static inline void SpriteSetPosition(XniSprite *sprite, Vector2 *position, float
@implementation SpriteBatch
// Sprite pool
static XniAdaptiveArray *spritePool;
static inline XniSprite *SpriteFromPool() {
if (spritePool.count > 0) {
XniSprite *sprite = *(XniSprite**)[spritePool removeLastItem];
return sprite;
} else {
XniSprite *sprite = [[XniSprite alloc] init];
return sprite;
}
}
static inline void ReturnSpriteToPool(XniSprite *sprite) {
[spritePool addItem:&sprite];
}
static inline void ReturnSpritesToPool(NSArray *sprites) {
for (XniSprite *sprite in sprites) {
[spritePool addItem:&sprite];
}
}
// Recyclable vertices
static VertexPositionColorTextureStruct vertices[4];
@ -151,6 +185,8 @@ static VertexPositionColorTextureStruct vertices[4];
textureSort = [[NSArray arrayWithObject:textureSortDescriptor] retain];
frontToBackSort = [[NSArray arrayWithObject:depthAscendingSortDescriptor] retain];
backToFrontSort = [[NSArray arrayWithObject:depthDescendingSortDescriptor] retain];
spritePool = [[XniAdaptiveArray alloc] initWithItemSize:sizeof(XniSprite*) initialCapacity:64];
}
- (void) setProjection {
@ -185,7 +221,14 @@ static VertexPositionColorTextureStruct vertices[4];
DepthStencilState:(DepthStencilState*)theDepthStencilState
RasterizerState:(RasterizerState*)theRasterizerState
Effect:(Effect*)theEffect {
[self beginWithSortMode:theSortMode BlendState:theBlendState SamplerState:theSamplerState DepthStencilState:theDepthStencilState RasterizerState:theRasterizerState Effect:theEffect TransformMatrix:nil];
// Make sure not to overwrite the world transform in basic effect.
Matrix *transform = nil;
if ([theEffect isKindOfClass:[BasicEffect class]]) {
transform = ((BasicEffect*)theEffect).world;
}
[self beginWithSortMode:theSortMode BlendState:theBlendState SamplerState:theSamplerState DepthStencilState:theDepthStencilState RasterizerState:theRasterizerState Effect:theEffect TransformMatrix:transform];
}
- (void) beginWithSortMode:(SpriteSortMode)theSortMode
@ -223,72 +266,72 @@ static VertexPositionColorTextureStruct vertices[4];
}
- (void) draw:(Texture2D*)texture toRectangle:(Rectangle*)destinationRectangle tintWithColor:(Color*)color {
XniSprite *sprite = [[[XniSprite alloc] init] autorelease];
XniSprite *sprite = SpriteFromPool();
sprite->texture = texture;
SpriteSetDestinationFast(sprite, destinationRectangle);
SpriteSetSource(sprite, nil, texture, SpriteEffectsNone);
sprite->color = color.packedValue;
[self draw:sprite];
draw(sprite, sprites, sortMode, self);
}
- (void) draw:(Texture2D*)texture toRectangle:(Rectangle*)destinationRectangle fromRectangle:(Rectangle*)sourceRectangle tintWithColor:(Color*)color {
XniSprite *sprite = [[[XniSprite alloc] init] autorelease];
XniSprite *sprite = SpriteFromPool();
sprite->texture = texture;
SpriteSetDestinationFast(sprite, destinationRectangle);
SpriteSetSource(sprite, sourceRectangle, texture, SpriteEffectsNone);
sprite->color = color.packedValue;
[self draw:sprite];
draw(sprite, sprites, sortMode, self);
}
- (void) draw:(Texture2D*)texture toRectangle:(Rectangle*)destinationRectangle fromRectangle:(Rectangle*)sourceRectangle tintWithColor:(Color*)color
rotation:(float)rotation origin:(Vector2*)origin effects:(SpriteEffects)effects layerDepth:(float)layerDepth {
XniSprite *sprite = [[[XniSprite alloc] init] autorelease];
XniSprite *sprite = SpriteFromPool();
sprite->texture = texture;
SpriteSetDestination(sprite, destinationRectangle, origin.x, origin.y, rotation);
SpriteSetSource(sprite, sourceRectangle, texture, effects);
sprite->color = color.packedValue;
sprite->layerDepth = layerDepth;
[self draw:sprite];
draw(sprite, sprites, sortMode, self);
}
- (void) draw:(Texture2D*)texture to:(Vector2*)position tintWithColor:(Color*)color {
XniSprite *sprite = [[[XniSprite alloc] init] autorelease];
XniSprite *sprite = SpriteFromPool();
sprite->texture = texture;
SpriteSetPositionFast(sprite, position, texture.width, texture.height);
SpriteSetSource(sprite, nil, texture, SpriteEffectsNone);
sprite->color = color.packedValue;
[self draw:sprite];
draw(sprite, sprites, sortMode, self);
}
- (void) draw:(Texture2D*)texture to:(Vector2*)position fromRectangle:(Rectangle*)sourceRectangle tintWithColor:(Color*)color {
XniSprite *sprite = [[[XniSprite alloc] init] autorelease];
XniSprite *sprite = SpriteFromPool();
sprite->texture = texture;
SpriteSetPositionFast(sprite, position, sourceRectangle ? sourceRectangle.width : texture.width, sourceRectangle ? sourceRectangle.height : texture.height);
SpriteSetSource(sprite, sourceRectangle, texture, SpriteEffectsNone);
sprite->color = color.packedValue;
[self draw:sprite];
draw(sprite, sprites, sortMode, self);
}
- (void) draw:(Texture2D*)texture to:(Vector2*)position fromRectangle:(Rectangle*)sourceRectangle tintWithColor:(Color*)color
rotation:(float)rotation origin:(Vector2*)origin scaleUniform:(float)scale effects:(SpriteEffects)effects layerDepth:(float)layerDepth {
XniSprite *sprite = [[[XniSprite alloc] init] autorelease];
XniSprite *sprite = SpriteFromPool();
sprite->texture = texture;
SpriteSetPosition(sprite, position, origin.x, origin.y, scale, scale, rotation, sourceRectangle ? sourceRectangle.width : texture.width, sourceRectangle ? sourceRectangle.height : texture.height);
SpriteSetSource(sprite, sourceRectangle, texture, effects);
sprite->color = color.packedValue;
sprite->layerDepth = layerDepth;
[self draw:sprite];
draw(sprite, sprites, sortMode, self);
}
- (void) draw:(Texture2D*)texture to:(Vector2*)position fromRectangle:(Rectangle*)sourceRectangle tintWithColor:(Color*)color
rotation:(float)rotation origin:(Vector2*)origin scale:(Vector2*)scale effects:(SpriteEffects)effects layerDepth:(float)layerDepth {
XniSprite *sprite = [[[XniSprite alloc] init] autorelease];
XniSprite *sprite = SpriteFromPool();
sprite->texture = texture;
SpriteSetPosition(sprite, position, origin.x, origin.y, scale.x, scale.y, rotation, sourceRectangle ? sourceRectangle.width : texture.width, sourceRectangle ? sourceRectangle.height : texture.height);
SpriteSetSource(sprite, sourceRectangle, texture, effects);
sprite->color = color.packedValue;
sprite->layerDepth = layerDepth;
[self draw:sprite];
draw(sprite, sprites, sortMode, self);
}
- (void) drawStringWithSpriteFont:(SpriteFont*)spriteFont text:(NSString*)text to:(Vector2*)position tintWithColor:(Color*)color {
@ -326,11 +369,12 @@ static VertexPositionColorTextureStruct vertices[4];
}
}
- (void) draw:(XniSprite *)sprite {
void draw(XniSprite *sprite, NSMutableArray *sprites, SpriteSortMode sortMode, SpriteBatch *it) {
[sprites addObject:sprite];
if (sortMode == SpriteSortModeImmediate) {
[self draw];
[it draw];
ReturnSpritesToPool(sprites);
[sprites removeAllObjects];
}
}
@ -365,6 +409,7 @@ static VertexPositionColorTextureStruct vertices[4];
[self draw];
// Clean up.
ReturnSpritesToPool(sprites);
[sprites removeAllObjects];
beginCalled = NO;
}
@ -430,7 +475,7 @@ static VertexPositionColorTextureStruct vertices[4];
vertices[3].position.x = vertices[0].position.x + sprite->height.x + sprite->width.x;
vertices[3].position.y = vertices[0].position.y + sprite->height.y + sprite->width.y;
vertices[3].position.z = sprite->layerDepth;
vertices[0].texture.x = sprite->source.x;
vertices[1].texture.x = sprite->source.x;
vertices[2].texture.x = sprite->source.x + sprite->source.width;

View File

@ -17,6 +17,7 @@
}
- (id) initWithItemSize:(int)itemSize initialCapacity:(int)initialCapacity;
- (id) initWithArray:(VertexArray*)source;
@property (nonatomic, readonly) void *array;
@property (nonatomic, readonly) int count;

View File

@ -19,6 +19,13 @@
return self;
}
- (id)initWithArray:(VertexArray *)source {
if (self = [super init]) {
array = [[XniAdaptiveArray alloc] initWithArray:source->array];
}
return self;
}
- (void *) array {
return array.array;
}

View File

@ -57,11 +57,21 @@
float projectionY = -((source.y - y) * 2 / height) + 1;
float projectionZ = (source.z - minDepth) / (maxDepth - minDepth);
Vector4 *objectSpace = [Vector4 vectorWithX:projectionX y:projectionY z:projectionZ w:1];
[objectSpace transformWith:[Matrix invert:projection]];
[objectSpace transformWith:[Matrix invert:view]];
[objectSpace transformWith:[Matrix invert:world]];
[objectSpace multiplyBy:1/objectSpace.w];
Vector4Struct objectSpace = Vector4Make(projectionX,projectionY,projectionZ,1);
MatrixStruct m = *projection.data;
MatrixInvert(&m);
Vector4Transform(&objectSpace, &m, &objectSpace);
m = *view.data;
MatrixInvert(&m);
Vector4Transform(&objectSpace, &m, &objectSpace);
m = *world.data;
MatrixInvert(&m);
Vector4Transform(&objectSpace, &m, &objectSpace);
Vector4Multiply(&objectSpace, 1/objectSpace.w, &objectSpace);
return [Vector3 vectorWithX:objectSpace.x y:objectSpace.y z:objectSpace.z];
}

View File

@ -27,6 +27,7 @@
NSMutableSet *releaseTouches;
NSMutableSet *lateReleaseTouches;
NSMutableDictionary *touchLocations;
NSMutableDictionary *uiTouchesForLocations;
}
@property (nonatomic) int displayWidth;

View File

@ -15,16 +15,16 @@
#import "GameView.h"
@interface XniTouchLocation : NSObject {
@public
int identifier;
Vector2 *position;
Vector2 *previousPosition;
TouchLocationState state;
int age;
}
- (id) initWithPosition:(Vector2*)thePosition;
@property (nonatomic) TouchLocationState state;
- (void) update;
- (void) moveToPosition:(Vector2*)newPosition;
@ -48,10 +48,9 @@ static int nextID = 0;
return self;
}
@synthesize state;
- (void) update {
[self moveToPosition:position];
age++;
}
- (void) moveToPosition:(Vector2*)newPosition {
@ -93,6 +92,7 @@ static TouchPanel *instance;
lateReleaseTouches = [[NSMutableSet alloc] init];
touchLocations = (NSMutableDictionary*)CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
uiTouchesForLocations = (NSMutableDictionary*)CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
}
return self;
}
@ -134,7 +134,11 @@ static TouchPanel *instance;
// After get state is done, all pressed touches should be moved.
[touch update];
}
if (touch->age > 200) {
UITouch *key = [uiTouchesForLocations objectForKey:touch];
[removeTouches addObject:key];
}
}
return collection;
}
@ -152,7 +156,8 @@ static TouchPanel *instance;
float scale = view.scale;
for (UITouch *touch in touches) {
XniTouchLocation *location = [touchLocations objectForKey:touch];
if (location) {
if (location) {
location->age = 0;
CGPoint position = [touch locationInView:view];
[location moveToPosition:[Vector2 vectorWithX:position.x * scale y:position.y * scale]];
}
@ -179,14 +184,21 @@ static TouchPanel *instance;
- (void) update {
// Remove all previously released touches.
for (UITouch *touch in removeTouches) {
XniTouchLocation *location = [[touchLocations objectForKey:touch] retain];
[touchLocations removeObjectForKey:touch];
if (location) {
[uiTouchesForLocations removeObjectForKey:location];
[location release];
}
}
[removeTouches removeAllObjects];
// Set released touches.
for (UITouch *touch in releaseTouches) {
XniTouchLocation *location = [touchLocations objectForKey:touch];
location.state = TouchLocationStateReleased;
if (location) {
location->state = TouchLocationStateReleased;
}
}
// Shift the pools.
@ -203,6 +215,7 @@ static TouchPanel *instance;
initWithPosition:[Vector2 vectorWithX:position.x * scale y:position.y * scale]] autorelease];
CFDictionaryAddValue((CFMutableDictionaryRef)touchLocations, touch, location);
CFDictionaryAddValue((CFMutableDictionaryRef)uiTouchesForLocations, location, touch);
}
[addTouches removeAllObjects];
@ -215,6 +228,7 @@ static TouchPanel *instance;
[releaseTouches release];
[lateReleaseTouches release];
[touchLocations release];
[uiTouchesForLocations release];
[super dealloc];
}

View File

@ -258,4 +258,24 @@ static inline void MatrixDivide(MatrixStruct *value1, MatrixStruct *value2, Matr
result->m42 = value1->m42 / value2->m42;
result->m43 = value1->m43 / value2->m43;
result->m44 = value1->m44 / value2->m44;
}
static inline void MatrixLerp(MatrixStruct *value1, MatrixStruct *value2, float amount, MatrixStruct *result) {
float first = 1 - amount;
result->m11 = value1->m11 * first + value2->m11 * amount;
result->m12 = value1->m12 * first + value2->m12 * amount;
result->m13 = value1->m13 * first + value2->m13 * amount;
result->m14 = value1->m14 * first + value2->m14 * amount;
result->m21 = value1->m21 * first + value2->m21 * amount;
result->m22 = value1->m22 * first + value2->m22 * amount;
result->m23 = value1->m23 * first + value2->m23 * amount;
result->m24 = value1->m24 * first + value2->m24 * amount;
result->m31 = value1->m31 * first + value2->m31 * amount;
result->m32 = value1->m32 * first + value2->m32 * amount;
result->m33 = value1->m33 * first + value2->m33 * amount;
result->m34 = value1->m34 * first + value2->m34 * amount;
result->m41 = value1->m41 * first + value2->m41 * amount;
result->m42 = value1->m42 * first + value2->m42 * amount;
result->m43 = value1->m43 * first + value2->m43 * amount;
result->m44 = value1->m44 * first + value2->m44 * amount;
}

View File

@ -91,7 +91,7 @@ static MediaPlayer *instance;
@synthesize volume;
- (void) setVolume:(float)value {
volume = value;
volume = MAX(0, MIN(1, value));
if (!isMuted) {
queue.activeSong.audioPlayer.volume = volume;
@ -160,8 +160,14 @@ static MediaPlayer *instance;
if (![self checkAvailability]) {
return;
}
if (queue.activeSong) {
[queue.activeSong.audioPlayer stop];
}
song.audioPlayer.currentTime = 0;
song.audioPlayer.delegate = self;
[queue setSong:song];
[self fillSongIndices];
[self moveNext];
@ -221,9 +227,10 @@ static MediaPlayer *instance;
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
if ([remainingSongIndices count] == 0 && !isRepeating) {
// The music stops, activate the ambient category again.
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error:nil];
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error:nil];
soloModeActivated = NO;
return;
[self setMediaState:MediaStateStopped];
return;
}
[self moveNext];

View File

@ -13,4 +13,12 @@ static inline PointStruct PointMake(int x, int y) {
static inline void PointSet(PointStruct *point, int x, int y) {
point->x = x;
point->y = y;
}
}
static inline void PointAdd(PointStruct *value1, PointStruct *value2, PointStruct *result) {
PointSet(result, value1->x + value2->x, value1->y + value2->y);
}
static inline void PointSubtract(PointStruct *value1, PointStruct *value2, PointStruct *result) {
PointSet(result, value1->x - value2->x, value1->y - value2->y);
}

View File

@ -28,8 +28,14 @@
@property (nonatomic) int y;
@property (nonatomic, readonly) PointStruct *data;
+ (XniPoint*) add:(XniPoint*)value1 to:(XniPoint*)value2;
+ (XniPoint*) subtract:(XniPoint*)value1 by:(XniPoint*)value2;
- (XniPoint*) set:(XniPoint*)value;
- (XniPoint*) add:(XniPoint*)value;
- (XniPoint*) subtract:(XniPoint*)value;
- (BOOL) equals:(XniPoint*)point;
+ (XniPoint*) zero;

View File

@ -59,11 +59,33 @@
// METHODS
+ (XniPoint *)add:(XniPoint *)value1 to:(XniPoint *)value2 {
PointStruct resultData;
PointAdd(value1.data, value2.data, &resultData);
return [XniPoint pointWithStruct:&resultData];
}
+ (XniPoint *)subtract:(XniPoint *)value1 by:(XniPoint *)value2 {
PointStruct resultData;
PointSubtract(value1.data, value2.data, &resultData);
return [XniPoint pointWithStruct:&resultData];
}
- (XniPoint*) set:(XniPoint*)value {
data = *value.data;
return self;
}
- (XniPoint *)add:(XniPoint *)value {
PointAdd(&data, value.data, &data);
return self;
}
- (XniPoint *)subtract:(XniPoint *)value {
PointSubtract(&data, value.data, &data);
return self;
}
- (id) copyWithZone:(NSZone *)zone {
return [[XniPoint allocWithZone:zone] initWithPointStruct:&data];
}

View File

@ -17,6 +17,7 @@
}
- (id) initWithItemSize:(int)theItemSize initialCapacity:(int)theCapacity;
- (id) initWithArray:(XniAdaptiveArray*)source;
@property (nonatomic, readonly) int itemSize;
@property (nonatomic, readonly) void *array;
@ -24,5 +25,6 @@
- (void) addItem:(void*)item;
- (void) clear;
- (void*) removeLastItem;
@end

View File

@ -19,6 +19,17 @@
return self;
}
- (id) initWithArray:(XniAdaptiveArray*)source {
if (self = [super init]) {
itemSize = source.itemSize;
capacity = source.count;
count = source.count;
array = malloc(capacity * itemSize);
memcpy(array, source.array, capacity * itemSize);
}
return self;
}
@synthesize itemSize;
@synthesize array;
@synthesize count;
@ -40,6 +51,11 @@
count = 0;
}
- (void*)removeLastItem {
count--;
return array + count * itemSize;
}
- (void) dealloc
{
free(array);

View File

@ -2664,7 +2664,6 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
ARCHS = "$(ARCHS_STANDARD_32_BIT)";
COPY_PHASE_STRIP = NO;
DSTROOT = /tmp/XNI.dst;
GCC_DYNAMIC_NO_PIC = NO;
@ -2684,7 +2683,6 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
ARCHS = "$(ARCHS_STANDARD_32_BIT)";
DSTROOT = /tmp/XNI.dst;
GCC_MODEL_TUNING = G5;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
@ -2700,7 +2698,10 @@
1DEB922308733DC00010E9CD /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ARCHS = "$(ARCHS_STANDARD_32_BIT)";
ARCHS = (
armv7,
armv6,
);
GCC_C_LANGUAGE_STANDARD = c99;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_PREPROCESSOR_DEFINITIONS = "";
@ -2716,7 +2717,10 @@
1DEB922408733DC00010E9CD /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ARCHS = "$(ARCHS_STANDARD_32_BIT)";
ARCHS = (
armv7,
armv6,
);
GCC_C_LANGUAGE_STANDARD = c99;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
@ -2731,7 +2735,6 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
ARCHS = "$(ARCHS_STANDARD_32_BIT)";
COPY_PHASE_STRIP = NO;
DSTROOT = /tmp/XNI.dst;
GCC_DYNAMIC_NO_PIC = NO;
@ -2751,7 +2754,6 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
ARCHS = "$(ARCHS_STANDARD_32_BIT)";
DSTROOT = /tmp/XNI.dst;
GCC_MODEL_TUNING = G5;
GCC_PRECOMPILE_PREFIX_HEADER = YES;