Implemented JS clock global object, unbroke makefile.

git-svn-id: http://svn.berlios.de/svnroot/repos/oolite-linux/trunk@1175 127b21dd-08f5-0310-b4b7-95ae10353056
This commit is contained in:
Jens Ayton 2007-09-15 15:09:19 +00:00
parent 75259572d1
commit 7e4b1b0880
8 changed files with 232 additions and 2 deletions

View File

@ -26,7 +26,7 @@ endif
OBJC_PROGRAM_NAME = oolite
oolite_C_FILES = legacy_random.c strlcpy.c
oolite_OBJC_FILES = Comparison.m AI.m DustEntity.m Entity.m GameController.m GuiDisplayGen.m HeadUpDisplay.m main.m MyOpenGLView.m OpenGLSprite.m ParticleEntity.m PlanetEntity.m PlayerEntityLegacyScriptEngine.m PlayerEntityContracts.m PlayerEntityControls.m PlayerEntityLoadSave.m PlayerEntitySound.m PlayerEntity.m ResourceManager.m RingEntity.m ShipEntityAI.m ShipEntity.m SkyEntity.m StationEntity.m Universe.m OOSound.m SDLMusic.m NSFileManagerOOExtensions.m JoystickHandler.m PlayerEntityStickMapper.m OOBasicSoundReferencePoint.m OOBasicSoundSource.m OOCharacter.m OOTrumble.m WormholeEntity.m NSScannerOOExtensions.m OOXMLExtensions.m NSMutableDictionaryOOExtensions.m Geometry.m Octree.m CollisionRegion.m OOColor.m OOLogging.m OOCacheManager.m OOCache.m OOStringParsing.m OOCollectionExtractors.m OOVector.m OOMatrix.m OOQuaternion.m OOVoxel.m OOTriangle.m OOPListParsing.m OOFastArithmetic.m OOTextureScaling.m OOConstToString.m OOScript.m OOJSScript.m OOJavaScriptEngine.m OOPListScript.m NSStringOOExtensions.m PlayerEntityScriptMethods.m OOWeakReference.m OOJSEntity.m EntityOOJavaScriptExtensions.m OOJSQuaternion.m OOMaterial.m OOShaderMaterial.m OOShaderProgram.m OOShaderUniform.m OOTexture.m OOTextureLoader.m OOPNGTextureLoader.m OOOpenGLExtensionManager.m OOBasicMaterial.m OOSingleTextureMaterial.m OOCPUInfo.m OOSelfDrawingEntity.m OOEntityWithDrawable.m OODrawable.m OOJSVector.m OOMesh.m OOOpenGL.m OOGraphicsResetManager.m OOProbabilisticTextureManager.m OODebugGLDrawing.m OOShaderUniformMethodType.m OOAsyncQueue.m TextureStore.m OOOXPVerifier.m OOOXPVerifierStage.m OOFileScannerVerifierStage.m OOCheckRequiresPListVerifierStage.m OOCheckDemoShipsPListVerifierStage.m OOCheckEquipmentPListVerifierStage.m OOTextureVerifierStage.m OOModelVerifierStage.m OOCheckShipDataPListVerifierStage.m OOPListSchemaVerifier.m OOJSShip.m OOJSPlayer.m OOJSCall.m OOJSStation.m OOJSSystem.m OOLegacyEventHandlerScript.m OOJSOolite.m OORoleSet.m OOJSGlobal.m OOJSMissionVariables.m OOJSMission.m OOPriorityQueue OOScriptTimer OOJSTimer
oolite_OBJC_FILES = Comparison.m AI.m DustEntity.m Entity.m GameController.m GuiDisplayGen.m HeadUpDisplay.m main.m MyOpenGLView.m OpenGLSprite.m ParticleEntity.m PlanetEntity.m PlayerEntityLegacyScriptEngine.m PlayerEntityContracts.m PlayerEntityControls.m PlayerEntityLoadSave.m PlayerEntitySound.m PlayerEntity.m ResourceManager.m RingEntity.m ShipEntityAI.m ShipEntity.m SkyEntity.m StationEntity.m Universe.m OOSound.m SDLMusic.m NSFileManagerOOExtensions.m JoystickHandler.m PlayerEntityStickMapper.m OOBasicSoundReferencePoint.m OOBasicSoundSource.m OOCharacter.m OOTrumble.m WormholeEntity.m NSScannerOOExtensions.m OOXMLExtensions.m NSMutableDictionaryOOExtensions.m Geometry.m Octree.m CollisionRegion.m OOColor.m OOLogging.m OOCacheManager.m OOCache.m OOStringParsing.m OOCollectionExtractors.m OOVector.m OOMatrix.m OOQuaternion.m OOVoxel.m OOTriangle.m OOPListParsing.m OOFastArithmetic.m OOTextureScaling.m OOConstToString.m OOScript.m OOJSScript.m OOJavaScriptEngine.m OOPListScript.m NSStringOOExtensions.m PlayerEntityScriptMethods.m OOWeakReference.m OOJSEntity.m EntityOOJavaScriptExtensions.m OOJSQuaternion.m OOMaterial.m OOShaderMaterial.m OOShaderProgram.m OOShaderUniform.m OOTexture.m OOTextureLoader.m OOPNGTextureLoader.m OOOpenGLExtensionManager.m OOBasicMaterial.m OOSingleTextureMaterial.m OOCPUInfo.m OOSelfDrawingEntity.m OOEntityWithDrawable.m OODrawable.m OOJSVector.m OOMesh.m OOOpenGL.m OOGraphicsResetManager.m OOProbabilisticTextureManager.m OODebugGLDrawing.m OOShaderUniformMethodType.m OOAsyncQueue.m TextureStore.m OOOXPVerifier.m OOOXPVerifierStage.m OOFileScannerVerifierStage.m OOCheckRequiresPListVerifierStage.m OOCheckDemoShipsPListVerifierStage.m OOCheckEquipmentPListVerifierStage.m OOTextureVerifierStage.m OOModelVerifierStage.m OOCheckShipDataPListVerifierStage.m OOPListSchemaVerifier.m OOJSShip.m OOJSPlayer.m OOJSCall.m OOJSStation.m OOJSSystem.m OOLegacyEventHandlerScript.m OOJSOolite.m OORoleSet.m OOJSGlobal.m OOJSMissionVariables.m OOJSMission.m OOPriorityQueue.m OOScriptTimer.m OOJSTimer.m OOJSClock.m
include $(GNUSTEP_MAKEFILES)/objc.make
include GNUmakefile.postamble

View File

@ -366,6 +366,8 @@
1A6B1F370C9AAA60000717CF /* OOPriorityQueue.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A6B1F350C9AAA60000717CF /* OOPriorityQueue.h */; };
1A6B228D0C9B40D4000717CF /* OOJSTimer.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A6B228B0C9B40D4000717CF /* OOJSTimer.h */; };
1A6B228E0C9B40D4000717CF /* OOJSTimer.m in Sources */ = {isa = PBXBuildFile; fileRef = 1A6B228C0C9B40D4000717CF /* OOJSTimer.m */; };
1A6B25EE0C9C2746000717CF /* OOJSClock.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A6B25EC0C9C2745000717CF /* OOJSClock.h */; };
1A6B25EF0C9C2746000717CF /* OOJSClock.m in Sources */ = {isa = PBXBuildFile; fileRef = 1A6B25ED0C9C2746000717CF /* OOJSClock.m */; };
1A6B50430C8B42AC0035DFCC /* libOgg.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 25F3E8E9099502BB002F25FD /* libOgg.a */; };
1A6DD1230C57B5BC00A892F4 /* OOPListSchemaVerifier.h in Headers */ = {isa = PBXBuildFile; fileRef = 1A6DD1210C57B5BC00A892F4 /* OOPListSchemaVerifier.h */; };
1A6DD1240C57B5BC00A892F4 /* OOPListSchemaVerifier.m in Sources */ = {isa = PBXBuildFile; fileRef = 1A6DD1220C57B5BC00A892F4 /* OOPListSchemaVerifier.m */; };
@ -1275,6 +1277,8 @@
1A6B1F350C9AAA60000717CF /* OOPriorityQueue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OOPriorityQueue.h; sourceTree = "<group>"; };
1A6B228B0C9B40D4000717CF /* OOJSTimer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OOJSTimer.h; sourceTree = "<group>"; };
1A6B228C0C9B40D4000717CF /* OOJSTimer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OOJSTimer.m; sourceTree = "<group>"; };
1A6B25EC0C9C2745000717CF /* OOJSClock.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OOJSClock.h; sourceTree = "<group>"; };
1A6B25ED0C9C2746000717CF /* OOJSClock.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OOJSClock.m; sourceTree = "<group>"; };
1A6B50370C8B42480035DFCC /* libjs.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = libjs.xcodeproj; path = xcode/libjs.xcodeproj; sourceTree = "<group>"; };
1A6DD1210C57B5BC00A892F4 /* OOPListSchemaVerifier.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OOPListSchemaVerifier.h; sourceTree = "<group>"; };
1A6DD1220C57B5BC00A892F4 /* OOPListSchemaVerifier.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OOPListSchemaVerifier.m; sourceTree = "<group>"; };
@ -1969,6 +1973,8 @@
1ACEA7A90C91E32800C7CE97 /* OOJSMission.m */,
1A6B228B0C9B40D4000717CF /* OOJSTimer.h */,
1A6B228C0C9B40D4000717CF /* OOJSTimer.m */,
1A6B25EC0C9C2745000717CF /* OOJSClock.h */,
1A6B25ED0C9C2746000717CF /* OOJSClock.m */,
);
name = JavaScript;
sourceTree = "<group>";
@ -2563,6 +2569,7 @@
1A6B1EF00C9AA5C6000717CF /* OOScriptTimer.h in Headers */,
1A6B1F370C9AAA60000717CF /* OOPriorityQueue.h in Headers */,
1A6B228D0C9B40D4000717CF /* OOJSTimer.h in Headers */,
1A6B25EE0C9C2746000717CF /* OOJSClock.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@ -2901,6 +2908,7 @@
1A6B1EF10C9AA5C6000717CF /* OOScriptTimer.m in Sources */,
1A6B1F360C9AAA60000717CF /* OOPriorityQueue.m in Sources */,
1A6B228E0C9B40D4000717CF /* OOJSTimer.m in Sources */,
1A6B25EF0C9C2746000717CF /* OOJSClock.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View File

@ -547,6 +547,9 @@ typedef enum
- (int) dialFuelScoopStatus;
- (double) clockTime; // Note that this is not an OOTimeAbsolute
- (BOOL) clockAdjusting;
- (NSString *) dial_clock;
- (NSString *) dial_clock_adjusted;
- (NSString *) dial_fpsinfo;

View File

@ -2315,6 +2315,18 @@ double scoopSoundPlayTime = 0.0;
}
- (double) clockTime
{
return ship_clock;
}
- (BOOL) clockAdjusting
{
return ship_clock_adjust != 0;
}
- (NSString*) dial_clock
{
return ClockToString(ship_clock, ship_clock_adjust != 0);

View File

@ -0,0 +1,31 @@
/*
OOJSClock.h
JavaScript clock global object.
Oolite
Copyright (C) 2004-2007 Giles C Williams and contributors
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
*/
#import <jsapi.h>
void InitOOJSClock(JSContext *context, JSObject *global);

View File

@ -0,0 +1,175 @@
/*
OOJSClock.m
Oolite
Copyright (C) 2004-2007 Giles C Williams and contributors
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
*/
#import "OOJSClock.h"
#import "OOJavaScriptEngine.h"
#import "Universe.h"
#import "OOJSPlayer.h"
#import "PlayerEntity.h"
// Minimum allowable interval for repeating timers.
#define kMinInterval 0.25
static JSBool ClockGetProperty(JSContext *context, JSObject *this, jsval name, jsval *outValue);
// Methods
static JSBool ClockToString(JSContext *context, JSObject *this, uintN argc, jsval *argv, jsval *outResult);
static JSClass sClockClass =
{
"Clock",
JSCLASS_HAS_PRIVATE,
JS_PropertyStub, // addProperty
JS_PropertyStub, // delProperty
ClockGetProperty, // getProperty
JS_PropertyStub, // setProperty
JS_EnumerateStub, // enumerate
JS_ResolveStub, // resolve
JS_ConvertStub, // convert
JS_FinalizeStub, // finalize
JSCLASS_NO_OPTIONAL_MEMBERS
};
enum
{
// Property IDs
kClock_absoluteSeconds, // game real time clock, double, read-only
kClock_seconds, // game clock time, double, read-only
kClock_minutes, // game clock time minutes (rounded down), integer double, read-only
kClock_hours, // game clock time hours (rounded down), integer double, read-only
kClock_days, // game clock time days (rounded down), int, read-only
kClock_secondsComponent, // second component of game clock time, double, read-only
kClock_minutesComponent, // minute component of game clock time (rounded down), int, read-only
kClock_hoursComponent, // hour component of game clock time (rounded down), int, read-only
kClock_daysComponent, // day component of game clock time (rounded down), int, read-only
kClock_clockString, // game clock time as display string, string, read-only
kClock_isAdjusting // clock is adjusting, boolean, read-only
};
static JSPropertySpec sClockProperties[] =
{
// JS name ID flags
{ "absoluteSeconds", kClock_absoluteSeconds, JSPROP_PERMANENT | JSPROP_ENUMERATE | JSPROP_READONLY },
{ "seconds", kClock_seconds, JSPROP_PERMANENT | JSPROP_ENUMERATE | JSPROP_READONLY },
{ "minutes", kClock_minutes, JSPROP_PERMANENT | JSPROP_ENUMERATE | JSPROP_READONLY },
{ "hours", kClock_hours, JSPROP_PERMANENT | JSPROP_ENUMERATE | JSPROP_READONLY },
{ "days", kClock_days, JSPROP_PERMANENT | JSPROP_ENUMERATE | JSPROP_READONLY },
{ "secondsComponent", kClock_secondsComponent, JSPROP_PERMANENT | JSPROP_ENUMERATE | JSPROP_READONLY },
{ "minutesComponent", kClock_minutesComponent, JSPROP_PERMANENT | JSPROP_ENUMERATE | JSPROP_READONLY },
{ "hoursComponent", kClock_hoursComponent, JSPROP_PERMANENT | JSPROP_ENUMERATE | JSPROP_READONLY },
{ "daysComponent", kClock_daysComponent, JSPROP_PERMANENT | JSPROP_ENUMERATE | JSPROP_READONLY },
{ "clockString", kClock_clockString, JSPROP_PERMANENT | JSPROP_ENUMERATE | JSPROP_READONLY },
{ "isAdjusting", kClock_isAdjusting, JSPROP_PERMANENT | JSPROP_ENUMERATE | JSPROP_READONLY },
{ 0 }
};
static JSFunctionSpec sClockMethods[] =
{
// JS name Function min args
{ "toString", ClockToString, 0 },
{ 0 }
};
void InitOOJSClock(JSContext *context, JSObject *global)
{
JSObject *clockPrototype = JS_InitClass(context, global, NULL, &sClockClass, NULL, 0, sClockProperties, sClockMethods, NULL, NULL);
JS_DefineObject(context, global, "clock", &sClockClass, clockPrototype, JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT);
}
static JSBool ClockGetProperty(JSContext *context, JSObject *this, jsval name, jsval *outValue)
{
PlayerEntity *player = nil;
double clockTime;
player = OOPlayerForScripting();
clockTime = [player clockTime];
if (!JSVAL_IS_INT(name)) return YES;
switch (JSVAL_TO_INT(name))
{
case kClock_absoluteSeconds:
JS_NewDoubleValue(context, [UNIVERSE getTime], outValue);
break;
case kClock_seconds:
JS_NewDoubleValue(context, clockTime, outValue);
break;
case kClock_minutes:
JS_NewDoubleValue(context, floor(clockTime / 60.0), outValue);
break;
case kClock_hours:
JS_NewDoubleValue(context, floor(clockTime / 3600.0), outValue);
break;
case kClock_secondsComponent:
*outValue = INT_TO_JSVAL(fmod(clockTime, 60.0));
break;
case kClock_minutesComponent:
*outValue = INT_TO_JSVAL(fmod(floor(clockTime / 60.0), 60.0));
break;
case kClock_hoursComponent:
*outValue = INT_TO_JSVAL(fmod(floor(clockTime / 3600.0), 24.0));
break;
case kClock_days:
case kClock_daysComponent:
*outValue = INT_TO_JSVAL(floor(clockTime / 86400.0));
break;
case kClock_clockString:
*outValue = [[player dial_clock] javaScriptValueInContext:context];
break;
case kClock_isAdjusting:
*outValue = BOOLEAN_TO_JSVAL([player clockAdjusting]);
break;
default:
OOReportJavaScriptBadPropertySelector(context, @"Clock", JSVAL_TO_INT(name));
return NO;
}
return YES;
}
static JSBool ClockToString(JSContext *context, JSObject *this, uintN argc, jsval *argv, jsval *outResult)
{
*outResult = [[OOPlayerForScripting() dial_clock] javaScriptValueInContext:context];
return YES;
}

View File

@ -26,7 +26,6 @@ MA 02110-1301, USA.
#import "OOJSTimer.h"
#import "OOJavaScriptEngine.h"
#import "Universe.h"
#import "OOLogging.h"
// Minimum allowable interval for repeating timers.

View File

@ -37,6 +37,7 @@ MA 02110-1301, USA.
#import "OOJSSystem.h"
#import "OOJSOolite.h"
#import "OOJSTimer.h"
#import "OOJSClock.h"
#import "OOCollectionExtractors.h"
#import "Universe.h"
@ -196,6 +197,7 @@ static void ReportJSError(JSContext *context, const char *message, JSErrorReport
InitOOJSPlayer(context, globalObject);
InitOOJSScript(context, globalObject);
InitOOJSTimer(context, globalObject);
InitOOJSClock(context, globalObject);
OOLog(@"script.javaScript.init.success", @"Set up JavaScript context.");