Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Quick and dirty frames per second meter for sparrow
SXFPSMeter *meter = [[SXFPSMeter alloc] initWithText:@""];
[self addChild:meter];
#import <Foundation/Foundation.h>
#import "Sparrow.h"
@interface SXFPSMeter : SPTextField{
- (void)update:(SPEnterFrameEvent*)event;
#import "SXFPSMeter.h"
@implementation SXFPSMeter
- (id)initWithText:(NSString *)text{
self = [super initWithText:text];
self.hAlign = SPHAlignLeft;
self.vAlign = SPVAlignTop;
self.fontSize = 16;
self.color = 0xFF0000;
[self addEventListener:@selector(update:) atObject:self forType:SP_EVENT_TYPE_ENTER_FRAME];
self.touchable = NO;
return self;
- (void)update:(SPEnterFrameEvent*)event{
static int frameCount = 0;
static double totalTime = 0;
totalTime += event.passedTime;
if (++frameCount % 60 == 0){
self.text = [NSString stringWithFormat:@"FPS: %f", frameCount/totalTime];
frameCount = totalTime = 0;
- (void)dealloc{
[self removeEventListener:@selector(update:) atObject:self forType:SP_EVENT_TYPE_ENTER_FRAME];
[super dealloc];

This comment has been minimized.

Copy link

@kaala kaala commented May 4, 2012

missing a


This comment has been minimized.

Copy link
Owner Author

@pixelrevision pixelrevision commented May 4, 2012

@kaala seems like a reasonable enough thing to add so I popped it in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment