Passing floats/doubles to C

new topic     » topic index » view thread      » older message » newer message

To Robert Craig or anyone that can help me, please...
Mark Brown take note, this effects morfit functions.

I'm been coding Direct3D into Exotica lately and been working
with Mark Brown with morfit that needs floats and doubles as
variables inside of a C structure.
With my Direct3D/Exotica C code, I noticed I couldn't print out
the float/double values given within Euphoria's poked "C-Like"
structure. I found in the euphoria mailing archives the
post 'Passing Floats by Value' by David Guy which who basically
had the same problem as I. Robert Craig replied to that post to do this:

>- convert each argument, a, as:
>         a = bytes_to_int(atom_to_float32(a))

because he said "Euphoria only supports passing floating-point atoms
to C routines in the form of C doubles (8 bytes)."
Okay, that kludged method worked for a parameter.

My current problem is that I need to pass floats and/or doubles to a C
structure. When I poke in bytes_to_int(atom_to_float32(a)) for a float
or poke in atom_to_float64(a)), where a is a float or double, I still
only
get a 0.0 value.

In my C code, I have this as a structure:

typedef struct tagMATERIAL
{
        double diffuse_r;
        double diffuse_g;
        double diffuse_b;
        double diffuse_a;
        double ambient_r;
        double ambient_g;
        double ambient_b;
        double ambient_a;
        double specular_r;
        double specular_g;
        double specular_b;
        double specular_a;
        double emissive_r;
        double emissive_g;
        double emissive_b;
        double emissive_a;
        double power;
} MATERIAL;

and this c code to print the values within the C struct after
Euphoria updates it through the dll function:

DllExport int SET_MATERIALS(MATERIAL material)
{
    char txt[255];

        sprintf(txt,"%f",material.diffuse_r);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material.diffuse_g);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material.diffuse_b);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material.diffuse_a);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material.ambient_r);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material.ambient_g);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material.ambient_b);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material.ambient_a);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material.specular_r);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material.specular_g);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material.specular_b);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material.specular_a);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material.emissive_r);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material.emissive_g);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material.emissive_b);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material.emissive_a);
        FILE_REPORT(txt);
        sprintf(txt,"%f",material.power);
        FILE_REPORT(txt);

        return 0;
}

The above C code all displays 0.0000 for me, not correct.
This is my Euhporia code that updates the C structure:

atom material
material=allocate(146)
.
.
.
poke4(material,    atom_to_float64(1.0))
poke4(material+8,  atom_to_float64(1.0))
poke4(material+16, atom_to_float64(1.0))
poke4(material+24, atom_to_float64(1.0))
poke4(material+32, atom_to_float64(1.0))
poke4(material+40, atom_to_float64(1.0))
poke4(material+48, atom_to_float64(1.0))
poke4(material+56, atom_to_float64(1.0))
poke4(material+64, atom_to_float64(1.0))
poke4(material+72, atom_to_float64(1.0))
poke4(material+80, atom_to_float64(1.0))
poke4(material+88, atom_to_float64(1.0))
poke4(material+96, atom_to_float64(0.0))
poke4(material+114, atom_to_float64(0.0))
poke4(material+122, atom_to_float64(0.0))
poke4(material+130, atom_to_float64(0.0))
poke4(material+138, atom_to_float64(0.0))

funcval=c_func(SET_MATERIALS,{material})
--------

Am I doing something wrong here???
I can take out the atom_to_float64() routine, but still
get 0.0 for each member value.

I had my C MATERIAL structure with all floats. They need to be
floats. But I can leave them doubles and let C do a type
conversion on them. I have to pass these values on to
the Direct3D's structure which each of it's structure members
are floats. Floats are important! and need to be able to pass
floats, not doubles.

My only alternative is to make my C function with 17 C_DOUBLE
parameters and all my other functions too that needs FLOATS inside
a structure. OR mabie passing them as an parameter array[17]. Arg!

Many, many, many thanks in advance if someone can give me a
workable solution here....

-- Todd Riggins

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu