Skip to content

Commit

Permalink
fixed a bug in ObjectManager that was causing the engine to freeze if…
Browse files Browse the repository at this point in the history
… an object was removed, then added again. Also got it so Entity::update now calls Entity.onUpdate in the javascript vm. Lastly, tried to get some properties binded, but the compiler is giving me some problems.
  • Loading branch information
0chroma committed Jan 21, 2011
1 parent 88f5d7d commit 964eb0d
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 11 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ $(JUICE_DIRS): FORCE

finish:
@echo ""
@mv -vf ./src/blackpyre .
@mv -vf ./support/v8-juice/src/lib/juice/libv8-juice.so ./lib
@mv -vf ./support/v8/libv8.so ./lib
@cp -vf ./src/blackpyre .
@cp -vf ./support/v8-juice/src/lib/juice/libv8-juice.so ./lib
@cp -vf ./support/v8/libv8.so ./lib
@chmod +x ./blackpyre
@echo ""
-ls --color ./blackpyre ./lib
Expand Down
19 changes: 18 additions & 1 deletion data/game/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
for(var i=0; i<360; i+=45){
print("Making entity at angle "+i);
var f = new Entity(300, 200, 100, 100, i, "bullet.png");
if(i==0) setTimeout(function(){ f.destroy(); }, 4000);
f.onUpdate = function(){
var tss = 2000; //f.timeSinceSpawn();
var time = tss*2;
while(time > 6000){
time -= 6000;
}
//f.angle = (tss/5)+f.initialAngle;
//f.posX = f.initialPosX + (time*Math.sin((f.initialAngle+(time/10))*(3.14/180))/17);
//f.posY = f.initialPosY + (time*Math.cos((f.initialAngle+(time/10))*(3.14/180))/17);
//f.posX++;
//f.posY++;
f.posX = 300;
f.posY = 200;
};
if(i==0){
setTimeout(function(){ f.hide(); }, 2000);
setTimeout(function(){ f.show(); }, 4000);
}
}
6 changes: 4 additions & 2 deletions src/Entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "GlUtil.h"
#include "ResourceManager.h"
#include "ObjectManager.h"
#include "Scripting.h"

#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
Expand Down Expand Up @@ -71,7 +72,8 @@ void Entity::render(){
}

void Entity::update(){
//uint32_t time = fabs(sin((timeSinceSpawn()/22.22)*(3.14/180)))*4000;
Scripting::callUpdateFunction(this);
/*//uint32_t time = fabs(sin((timeSinceSpawn()/22.22)*(3.14/180)))*4000;
uint32_t time = timeSinceSpawn()*2;
while(time > 6000){
time -= 6000;
Expand All @@ -80,7 +82,7 @@ void Entity::update(){
// posX = initialPosX+(time*sin(initialAngle*(3.14/180))/15);
// posY = initialPosY+(time*cos(initialAngle*(3.14/180))/15);
posX = initialPosX + (time*sin((initialAngle+(time/10))*(3.14/180))/17);
posY = initialPosY + (time*cos((initialAngle+(time/10))*(3.14/180))/17);
posY = initialPosY + (time*cos((initialAngle+(time/10))*(3.14/180))/17);*/
}

void Entity::show(){
Expand Down
1 change: 0 additions & 1 deletion src/Entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class Entity : public GameObject{

void show();
void hide();

private:
int objectmanagerId;
};
Expand Down
5 changes: 4 additions & 1 deletion src/ObjectManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include "ObjectManager.h"
#include "GameObject.h"

#include <stdio.h>

GameObject *ObjectManager::rootObject = 0;
ObjectManager *ObjectManager::instance = 0;
int ObjectManager::idCounter = 0;
Expand Down Expand Up @@ -40,9 +42,10 @@ int ObjectManager::addObject(GameObject *object){
}
listObj->next = object;
object->prev = listObj;
object->next = 0; //set a default incase it was added already
}
object->id = ++idCounter;
object->setSpawnTime();
object->setSpawnTime(); //TODO: maybe move this somewhere else since it would screw up some animations if the object was removed then added again
return (int) object->id;
}

Expand Down
43 changes: 40 additions & 3 deletions src/Scripting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,16 @@
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string>

// Some magic so we can get JS objects from our native objects
#define CLASSWRAP_BOUND_TYPE Entity
#define CLASSWRAP_BOUND_TYPE_NAME "Entity"
// OPTIONAL: #define CLASSWRAP_BOUND_TYPE_INHERITS BoundBaseClass
// ^^^^^ required if MyType sublcasses another bound native!
#include <v8/juice/ClassWrap_TwoWay.h>

Scripting *Scripting::instance = 0;
v8::Handle<v8::Context> Scripting::context;

Expand Down Expand Up @@ -98,6 +106,20 @@ v8::Handle<v8::ObjectTemplate> Scripting::getObjectTemplate(){
return global;
}

void Scripting::callUpdateFunction(Entity* obj){
v8::Locker tlock;
v8::HandleScope handle_scope;
namespace cv = ::v8::juice::convert;

v8::Handle<v8::Object> jsObj = v8::Handle<v8::Object>::Cast(cv::CastToJS(obj));

v8::Local<v8::Value> func = jsObj->Get(v8::String::New("onUpdate"));
if (func->IsFunction()){ //&& !func->isEmpty()){
v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast(func);
f->Call(jsObj, 0, 0);
}
}

// ======== Setup v8 juice's bindable functions ========

void Scripting::setupTimeFunctions(v8::Handle<v8::Object> dest){
Expand Down Expand Up @@ -134,11 +156,25 @@ v8::Handle<v8::Object> Scripting::setupEntityClass(v8::Handle<v8::Object> dest){

typedef cw::ClassWrap<Entity> CW;
CW & cw( CW::Instance() );


cw.BindMemVar<float, &Entity::posX>( "posX" );
cw.BindMemVar<float, &Entity::posY>( "posY" );
cw.BindMemVar<float, &Entity::angle>( "angle" );
cw.BindMemVar<float, &Entity::initialPosX>( "initialPosX" );
cw.BindMemVar<float, &Entity::initialPosY>( "initialPosY" );
cw.BindMemVar<float, &Entity::initialAngle>( "initialAngle" );


typedef convert::MemFuncInvocationCallbackCreator<Entity>
ICM; // typing-saver
cw.Set( "show", ICM::M0::Invocable<void,&Entity::show> );
cw.Set( "hide", ICM::M0::Invocable<void,&Entity::hide> );
cw.Set( "timeSinceSpawn", ICM::M0::Invocable<uint32_t,&Entity::timeSinceSpawn> );

cw.Set( "destroy", CW::DestroyObject );

cw.Seal(); // ends the binding process
cw.AddClassTo( dest ); // installs BoundNative class in dest
cw.AddClassTo( dest );
return cw.CtorTemplate()->GetFunction();
}

Expand Down Expand Up @@ -169,6 +205,7 @@ namespace v8 { namespace juice { namespace convert {
// Optional:
static const NativeToJS<float> FloatToJS = NativeToJS<float>();
static const JSToNative<float> JSToFloat = JSToNative<float>();

}}}

namespace v8 { namespace juice { namespace cw {
Expand All @@ -184,7 +221,7 @@ namespace v8 { namespace juice { namespace cw {
}}}

//define JS-side name for classes
JUICE_CLASSWRAP_CLASSNAME(Entity,"Entity")
//JUICE_CLASSWRAP_CLASSNAME(Entity,"Entity")


// ======== Runtime ========
Expand Down
3 changes: 3 additions & 0 deletions src/Scripting.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#define Scripting_h

class Entity;
class GameObject;

class Scripting {
public:
Expand All @@ -25,6 +26,8 @@ class Scripting {

static v8::Handle<v8::Value> func_quit(const v8::Arguments& args);
static v8::Handle<v8::Value> func_print(const v8::Arguments& args);

static void callUpdateFunction(Entity* obj);

private:
static Scripting *instance;
Expand Down

0 comments on commit 964eb0d

Please sign in to comment.