PDA

View Full Version : Need help for c++ plugin



avogad
03-13-2007, 04:06 AM
Here is the begining of the plugin i'm writing. However, as soon as i'm trying to call getRotation, the compiler screams !!! Is there something i'm missing or a way plugins have to be built. I've read some C examples but I still have problems to understand them.


#include <lwserver.h>
#include <lwmotion.h>
#include <stdio.h>
#include <lwhost.h>
#include <cmath>

typedef struct {
float x,y,z,w;
} Quaternion;

int execute( long version, GlobalFunc *global, LWItemMotionHandler *local, void *serverData )
{
if ( version != LWITEMMOTION_VERSION )
return AFUNC_BADVERSION;

LWMessageFuncs *msg;
msg = (LWMessageFuncs*)global( LWMESSAGEFUNCS_GLOBAL, GFUSE_TRANSIENT );
if ( !msg )
return AFUNC_BADGLOBAL;

msg->info("Here is my motion plugin","... writen in c++");

return AFUNC_OK;
}

void getRotation( Quaternion *quat, LWItemMotionAccess *access )
{
LWDVector rot;

access->getParam(LWIP_ROTATION, access->time, rot);
}

extern "C" {
ServerRecord ServerDesc[] = {
{ (const char*)"ItemMotionHandler", (const char*)"Quaternion", (ActivateFunc*)execute },
{ NULL }
};
}

Lightwolf
03-13-2007, 04:25 AM
Erm...
a) You are not filling out the *local struct with callbacks to your functions - so LW will just initialize the plugin and exit it again
b) GetRotation() never gets called.

I think you need to go back and have a good look at some of the sample code again.

Cheers,
Mike

avogad
03-13-2007, 04:37 AM
#include <lwserver.h>
#include <lwmotion.h>
#include <stdio.h>
#include <lwhost.h>
#include <cmath>
#include "Quaternion.h"




/*
================================================== ====================
Create()
Allocate and initialize instance data.
================================================== ==================== */

LWInstance Create( void *priv, LWItemID item, LWError *err )
{
Quaternion *inst;

if ( inst = NULL)
{
inst->qx()=1;
inst->qy()=1;
inst->qz()=1;
inst->qw()=1;
}
else
*err = "Couldn't allocate instance data.";

return inst;
}

/*
================================================== ====================
Destroy()
Free resources allocated by Create().
================================================== ==================== */
void Destroy( Quaternion *inst )
{
delete( inst );
}

/*
================================================== ====================
Copy()
Copy instance data.
================================================== ==================== */
LWError Copy( Quaternion *to, Quaternion *from )
{
*to = *from;
return NULL;
}

/*
================================================== ====================
Load()
Read instance data.
================================================== ==================== */
LWError Load( Quaternion *inst, const LWLoadState *ls )
{
float xyzw[ 4 ];

LWLOAD_FP( ls, xyzw, 4 );
inst->qx() = xyzw[0];
inst->qy() = xyzw[1];
inst->qz() = xyzw[2];
inst->qw() = xyzw[3];
return NULL;
}

/*
================================================== ====================
Save()
Write instance data.
================================================== ==================== */

LWError Save( Quaternion *inst, const LWSaveState *ss )
{
float xyzw[ 4 ];

xyzw[0]=inst->qx();
xyzw[1]=inst->qy();
xyzw[2]=inst->qz();
xyzw[3]=inst->qw();

LWSAVE_FP( ss, xyzw, 4 );
return NULL;
}

/*
================================================== ====================
Describe()
Write a short, human-readable string describing
the instance data.
================================================== ==================== */

const char * Describe( Quaternion *inst )
{
static char desc[ 20 ];
sprintf( desc, "Quaternion" );
return desc;
}

/*
================================================== ====================
Flags()
================================================== ==================== */

int Flags( Quaternion *inst )
{
return 0;
}

/*
================================================== ====================
Evaluate()
This is where we can modify the item's motion.
================================================== ==================== */

XCALL_( static void )
Evaluate( Quaternion *inst, const LWItemMotionAccess *access )
{
LWDVector rot = { 0.0 };

access->getParam( LWIP_ROTATION, access->time, rot );
}

/*
================================================== ====================
Handler()

Quaternion activation function. Check the version and fill in the
callback fields of the handler structure.
================================================== ==================== */

int Quaternion( long version, GlobalFunc *global, LWItemMotionHandler *local, void *serverData)
{
if ( version != LWITEMMOTION_VERSION ) return AFUNC_BADVERSION;

LWMessageFuncs *msg;
msg = (LWMessageFuncs*)global( LWMESSAGEFUNCS_GLOBAL, GFUSE_TRANSIENT );
if ( !msg )
return AFUNC_BADGLOBAL;

msg->info("Here is my motion plugin","... writen in c++");

local->inst->create = Create;
local->inst->destroy = Destroy;
local->inst->load = Load;
local->inst->save = Save;
local->inst->copy = Copy;
local->inst->descln = Describe;
local->evaluate = Evaluate;
local->flags = Flags;

return AFUNC_OK;
}

extern "C" {
ServerRecord ServerDesc[] = {
{ (const char*)"ItemMotionHandler", (const char*)"Quaternion", (ActivateFunc*)Activate },
{ NULL }
};
}


Does this look better ?!

Lightwolf
03-13-2007, 04:41 AM
Almost:


...
Quaternion *inst = new Quaternion();

if ( inst == NULL)
{
...

Remember, = assigns, == compares.

Cheers,
Mike

avogad
03-13-2007, 04:46 AM
Thx anyway, i still have compilations errors:


1>.\LW_Quaternion.cpp(152) : error C2440: '=' : impossible de convertir de 'const char *(__cdecl *)(Quaternion *)' en 'const char *(__cdecl *)(LWInstance)'

Is there a cast to do?! It might come from the fact that i'm using C++...

Lightwolf
03-13-2007, 04:50 AM
Thx anyway, i still have compilations errors:

C++ is more strict than C.
C allows this:


void Destroy( Quaternion *inst )
{
;
}

in C++ it should be


void Destroy( void *inst )
{
Quaternion *my_quat = static_cast<Quaternion *> inst;
}