I don't think angelscript supports unions, but who amongst us hasn't posted in a forum not realizing what we're doing :). Anyways, I have some preliminary benchmark numbers and one more issue.
Old Code Using Typedef Method
void PostProcess() { Color col; int avg; for (int off=canvas.Width()*canvas.Height()-1; off>=0; off--) { col = canvas.GetPixelOffset(off); avg = REDVAL(col) + GREENVAL(col) + BLUEVAL(col); avg /= 3; canvas.SetPixelOffset(off, RGBA(avg, avg, avg, ALPHAVAL(col))); }}
New Code Using Class Method
void PostProcess() { Color4 col; int avg; for (int off=canvas.Width()*canvas.Height()-1; off>=0; off--) { col = canvas.GetPixelOffset(off); avg = col.r + col.g + col.b; col.r = avg; col.g = avg; col.b = avg; canvas.SetPixelOffset(off, col.ToInt()); }}
Using the old method, best time over 10+ runs was 109ms.
Using the new method, best time over 10+ runs was 98ms.
So I figure throw in one more optimization since i'm using a class for the color now :
void PostProcess() { Color4 col; int avg; for (int off=canvas.Width()*canvas.Height()-1; off>=0; off--) { col = canvas.GetPixelOffset(off); col.MakeGray(); canvas.SetPixelOffset(off, col.ToInt()); }}
Using this method, best time over 10+ runs was 73ms.
Conclusion - New method is definitely faster. It looks like function call overhead was larger than the class usage overhead after all. Now there is just one issue I can't seem to get my head around.
Notice in the examples I had to use "col.ToInt()". I registered an asBEHAVE_VALUE_CAST in my color test class as such :
r = engine->RegisterObjectType("Color4", sizeof(UINT), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_PRIMITIVE); r = engine->RegisterObjectProperty("Color4", "uint8 a", 3); r = engine->RegisterObjectProperty("Color4", "uint8 r", 2); r = engine->RegisterObjectProperty("Color4", "uint8 g", 1); r = engine->RegisterObjectProperty("Color4", "uint8 b", 0); r = engine->RegisterObjectBehaviour("Color4", asBEHAVE_CONSTRUCT, "void f(uint8 r, uint8 g, uint8 b, uint8 a)", asFUNCTION(color_construct4), asCALL_CDECL_OBJLAST); assert( r >= 0 ); //r = engine->RegisterObjectBehaviour("Color4", asBEHAVE_CONSTRUCT, "void f(uint col)", asFUNCTION(color_construct1), asCALL_CDECL_OBJLAST); assert( r >= 0 ); r = engine->RegisterObjectBehaviour("Color4", asBEHAVE_ASSIGNMENT, "Color4 &f(uint col)", asFUNCTION(uint_to_color), asCALL_CDECL_OBJLAST); assert( r >= 0 ); r = engine->RegisterObjectBehaviour("Color4", asBEHAVE_VALUE_CAST, "uint f() const", asFUNCTION(color_to_uint), asCALL_CDECL_OBJLAST); assert( r >= 0 ); r = engine->RegisterObjectMethod("Color4", "uint ToInt() const", asFUNCTION(color_to_uint), asCALL_CDECL_OBJLAST); assert( r >= 0 ); r = engine->RegisterObjectMethod("Color4", "void MakeGray() const", asFUNCTION(color_makegray), asCALL_CDECL_OBJLAST); assert( r >= 0 );
However the following code throws an error : Can't implicitly convert from 'Color4&' to 'uint'.
Color4 col = 1; uint ctmp = col;
Any ideas?