Skip to content

Commit

Permalink
translated MachTimer class to C++
Browse files Browse the repository at this point in the history
  • Loading branch information
jongwook committed Aug 13, 2010
1 parent fe14f1c commit 5ae5276
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 36 deletions.
8 changes: 4 additions & 4 deletions Classes/EAGLView.mm
Expand Up @@ -316,8 +316,8 @@ - (void)awakeFromNib
//Create and advertise networking and discover others
[self setup];

mytimer = [MachTimer alloc];
[mytimer start];
mytimer = new MachTimer();
mytimer->start();

#ifdef LATE_LAUNCH
NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
Expand Down Expand Up @@ -843,8 +843,8 @@ - (void)drawView {

urs_PullVis(); // update vis data before we call events, this way we have a rate based pulling that is available in all events.
// Clock ourselves.
float elapsedtime = [mytimer elapsedSec];
[mytimer start];
float elapsedtime = mytimer->elapsedSec();
mytimer->start();
callAllOnUpdate(elapsedtime); // Call lua APIs OnUpdates when we render a new region. We do this first so that stuff can still be drawn for this region.

CGRect bounds = [self bounds];
Expand Down
25 changes: 7 additions & 18 deletions Classes/MachTimer.mm → Classes/MachTimer.cpp
Expand Up @@ -3,35 +3,24 @@
// MachTimer.m
//

#import "MachTimer.h"
#include "MachTimer.h"

static mach_timebase_info_data_t timebase;

@implementation MachTimer
+ (void)initialize
{
MachTimer::MachTimer() {
(void) mach_timebase_info(&timebase);
t0 = mach_absolute_time();
}


- init
{
if(self = [super init]) {
t0 = mach_absolute_time();
}
return self;
}

- (void)start
{
void MachTimer::start() {
t0 = mach_absolute_time();
}

- (uint64_t)elapsed {
uint64_t MachTimer::elapsed() {
return mach_absolute_time() - t0;
}

- (float)elapsedSec {
float MachTimer::elapsedSec() {
return ((float)(mach_absolute_time() - t0)) * ((float)timebase.numer) / ((float)timebase.denom) / 1000000000.0f;
}
@end

18 changes: 11 additions & 7 deletions Classes/MachTimer.h
Expand Up @@ -2,17 +2,21 @@
// MachTimer.h
//

#ifndef __MACHTIMER_H__
#define __MACHTIMER_H__

#include <assert.h>
#include <mach/mach.h>
#include <mach/mach_time.h>
#include <unistd.h>

@interface MachTimer : NSObject {
class MachTimer {
uint64_t t0;
}

- (void)start;
- (uint64_t)elapsed;
- (float)elapsedSec;
public:
MachTimer();
void start();
uint64_t elapsed();
float elapsedSec();
};

@end
#endif
6 changes: 3 additions & 3 deletions Classes/urAPI.cpp
Expand Up @@ -2068,7 +2068,7 @@ int l_InputPosition(lua_State* lua)

int l_Time(lua_State* lua)
{
lua_pushnumber(lua, [systimer elapsedSec]);
lua_pushnumber(lua, systimer->elapsedSec());
return 1;
}

Expand Down Expand Up @@ -4053,6 +4053,6 @@ void l_setupAPI(lua_State *lua)
lua_rawseti(lua, -2, 2); // Can be extended to any number of channels here
lua_setglobal(lua,"urSoundData");
#endif
systimer = [MachTimer alloc];
[systimer start];
systimer = new MachTimer();
systimer->start();
}
8 changes: 4 additions & 4 deletions urMus.xcodeproj/project.pbxproj
Expand Up @@ -65,7 +65,7 @@
7778B2090FED04AF00F491D3 /* lzio.c in Sources */ = {isa = PBXBuildFile; fileRef = 7778B1E80FED04AF00F491D3 /* lzio.c */; };
7778B20A0FED04AF00F491D3 /* print.c in Sources */ = {isa = PBXBuildFile; fileRef = 7778B1EA0FED04AF00F491D3 /* print.c */; };
7778B2F90FED34FB00F491D3 /* urAPI.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7778B2F80FED34FB00F491D3 /* urAPI.cpp */; };
777E0FC20FF19903005882D0 /* MachTimer.mm in Sources */ = {isa = PBXBuildFile; fileRef = 777E0FC10FF19903005882D0 /* MachTimer.mm */; };
777E0FC20FF19903005882D0 /* MachTimer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 777E0FC10FF19903005882D0 /* MachTimer.cpp */; };
777E5AC91057F5EB007FD908 /* RIOAudioUnitLayer.mm in Sources */ = {isa = PBXBuildFile; fileRef = 777E5AC81057F5EB007FD908 /* RIOAudioUnitLayer.mm */; };
778FB5220FEEE8F900F109A7 /* Texture2d.mm in Sources */ = {isa = PBXBuildFile; fileRef = 778FB5210FEEE8F900F109A7 /* Texture2d.mm */; };
778FB5350FEEEB7800F109A7 /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 778FB5340FEEEB7800F109A7 /* QuartzCore.framework */; };
Expand Down Expand Up @@ -317,7 +317,7 @@
7778B2F70FED34FB00F491D3 /* urAPI.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = urAPI.h; sourceTree = "<group>"; };
7778B2F80FED34FB00F491D3 /* urAPI.cpp */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.objcpp; fileEncoding = 4; path = urAPI.cpp; sourceTree = "<group>"; };
777E0FC00FF19903005882D0 /* MachTimer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MachTimer.h; sourceTree = "<group>"; };
777E0FC10FF19903005882D0 /* MachTimer.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MachTimer.mm; sourceTree = "<group>"; };
777E0FC10FF19903005882D0 /* MachTimer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = MachTimer.cpp; sourceTree = "<group>"; };
777E5AC71057F5EB007FD908 /* RIOAudioUnitLayer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = RIOAudioUnitLayer.h; path = RIOAudioUnitLayer/RIOAudioUnitLayer.h; sourceTree = "<group>"; };
777E5AC81057F5EB007FD908 /* RIOAudioUnitLayer.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = RIOAudioUnitLayer.mm; path = RIOAudioUnitLayer/RIOAudioUnitLayer.mm; sourceTree = "<group>"; };
778FB5200FEEE8F900F109A7 /* Texture2d.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Texture2d.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -609,7 +609,7 @@
778FB5200FEEE8F900F109A7 /* Texture2d.h */,
778FB5210FEEE8F900F109A7 /* Texture2d.mm */,
777E0FC00FF19903005882D0 /* MachTimer.h */,
777E0FC10FF19903005882D0 /* MachTimer.mm */,
777E0FC10FF19903005882D0 /* MachTimer.cpp */,
77C492F8105CAA1300B2BA88 /* urSound.h */,
77C492F9105CAA1300B2BA88 /* urSound.cpp */,
77B56EE4108954C4008931AC /* urSTK.h */,
Expand Down Expand Up @@ -1242,7 +1242,7 @@
7778B20A0FED04AF00F491D3 /* print.c in Sources */,
7778B2F90FED34FB00F491D3 /* urAPI.cpp in Sources */,
778FB5220FEEE8F900F109A7 /* Texture2d.mm in Sources */,
777E0FC20FF19903005882D0 /* MachTimer.mm in Sources */,
777E0FC20FF19903005882D0 /* MachTimer.cpp in Sources */,
777E5AC91057F5EB007FD908 /* RIOAudioUnitLayer.mm in Sources */,
77C492FA105CAA1300B2BA88 /* urSound.cpp in Sources */,
77B56E3B10894FE7008931AC /* ADSR.cpp in Sources */,
Expand Down

0 comments on commit 5ae5276

Please sign in to comment.