OpenGL 放大器(Cocos2d magnifier)

Per @Gob00st request, I will add a very simple magnifier class to show the basic idea behind it. It currently takes a rect that will be used as the magnified area, but it doesn't take into account the scale, so this needs to be adjusted. But the basic idea remains the same.

Magnifier.h

#import <Foundation/Foundation.h>
#import "cocos2d.h"

@interface Magnifier : CCNode {

	BOOL active;	// boolean for determining if magnifier is active (visible) or not
	CGRect rect;	// rect on the screen to magnify
	CGFloat magnifyScale;	// the scale we want to magnify

	CCNode *renderNode;		// the node to be drawn on texture
	CCRenderTexture *renderTexture;		// a CCRenderTexture object

}

- (id)initWithNodeToMagnify:(CCNode *)n rect:(CGRect)rectToMagnify scale:(CGFloat)scale;
- (void)enable;
- (void)disable;

@end

Magnifier.m

#import "Magnifier.h"

@implementation Magnifier

- (id)initWithNodeToMagnify:(CCNode *)n rect:(CGRect)rectToMagnify scale:(CGFloat)scale
{
	if (self = [super init]) {

		self.visible = active = NO;
		renderNode = n;
		rect = rectToMagnify;
		magnifyScale = scale;

		renderTexture = [[CCRenderTexture renderTextureWithWidth:rect.size.width height:rect.size.height] retain];
		[self addChild:renderTexture];

	}
	return self;
}

- (void)enable
{
	self.visible = active = YES;
	[self scheduleUpdate];
}

- (void)disable
{
	self.visible = active = NO;
	[self unscheduleUpdate];
}

- (void)drawAreaToTexture
{
	[renderTexture beginWithClear:0.0 g:0.0 b:0.0 a:1.0];

	// shift the renderNode's position to capture exactly the rect we need
	CGPoint originalPosition = renderNode.position;
	renderNode.position = ccpSub(originalPosition, rect.origin);

	// scale the node as we want
	CGFloat originalScale = renderNode.scale;
	renderNode.scale = magnifyScale;

	[renderNode visit];

	// shift renderNode's position back
	renderNode.position = originalPosition;

	// scale back
	renderNode.scale = originalScale;

	[renderTexture end];
}

- (void)update:(ccTime)dt
{
	[self drawAreaToTexture];
}

- (void)dealloc
{
	[renderTexture release];
	[super dealloc];
}

@end

Example on how to use it. This uses the right bottom area of the screen and displays it in the left bottom:

// init magnifier
CGRect magnifierRect = CGRectMake([CCDirector sharedDirector].winSize.width-100.0f, 0.0f, 100.0f, 100.0f);

Magnifier *magnifier = [[Magnifier alloc] initWithNodeToMagnify:self rect:magnifierRect scale:1.0f];
magnifier.position = ccp(magnifierRect.size.width*0.5f, magnifierRect.size.height*0.5f);
[self addChild:magnifier];

// enable magnifier
[magnifier enable];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值