Weird issue with drawing code

Started by
6 comments, last by WitchLord 11 years ago

Hello,

My angelscript programs have the ability to draw things on the screen using openGL. This works just fine usually, as the angelscript merely calls regular functions that wrap the openGL themselves.

For example, both the C++ and the angelscript have the ability to call void drawQuad(pos, size, color). However, in some builds it fails to work correctly. the exact same function when called from C++ always works and the result is on the screen, but if called from angelscript it fails to draw anything even though the script is executed.

Tested platforms:

Windows -> Generic calls -> works

Windows -> Native calls -> works

Android -> Generic calls -> works

Android -> Native calls -> fails

Linux -> Native calls -> fails

this is all the data I have so far... It is very weird, but i double-tested it and it is a reality. I can even call it three times: c++ then angelscript then c++, and only the first and last are effectively drawn.

If you have any idea of what this may be, i appreciate it.

EDIT: It seems to be another weird inexplicable bug with the registration of my Color type. I checked and when it comes from scripts, it comes with undefined behavior (always trash values). Must be some wicked issue with the copy constructor or something, i ll try to resolve it tomorrow and post about it then)

Advertisement

It is most likely something related to how you've registered your value types. When using native calling conventions it is very important to register the types correctly so that AngelScript will make pass the arguments to the functions exactly the way that the C++ function expects them.

I need to know the signature of the drawQuad function, the C++ declaration of each type involved and how you've registered them in order to help you further. Specifically the flags passed to the RegisterObjectType() have a significant chance of being the cause of the problem.

If you get the flags wrong, then AngelScript may attempt to push the value on the stack, where the C++ function expects to receive a reference, or vice versa. Specifically on Android and Linux there is also a high chance that C++ expects the value to be passed in the CPU registers, and if the flags are wrong AngelScript won't do that.

The following template function that I'm including in the next release may help in getting the correct flags:

#include <type_traits>
 
 
template<typename T>
asUINT GetTypeTraits()
{
  bool hasConstructor =  std::is_default_constructible<T>::value && !std::has_trivial_default_constructor<T>::value;
  bool hasDestructor = std::is_destructible<T>::value && !std::has_trivial_destructor<T>::value;
  bool hasAssignmentOperator = std::is_copy_assignable<T>::value && !std::has_trivial_copy_assign<T>::value;
  bool hasCopyConstructor = std::is_copy_constructible<T>::value && !std::has_trivial_copy_constructor<T>::value;
  bool isFloat = std::is_floating_point<T>::value;
  bool isPrimitive = std::is_integral<T>::value || std::is_pointer<T>::value || std::is_enum<T>::value;
 
  if( isFloat )
    return asOBJ_APP_FLOAT;
  if( isPrimitive )
    return asOBJ_APP_PRIMITIVE;
 
  asDWORD flags = asOBJ_APP_CLASS;
  if( hasConstructor )
    flags |= asOBJ_APP_CLASS_CONSTRUCTOR;
  if( hasDestructor )
    flags |= asOBJ_APP_CLASS_DESTRUCTOR;
  if( hasAssignmentOperator )
    flags |= asOBJ_APP_CLASS_ASSIGNMENT;
  if( hasCopyConstructor )
    flags |= asOBJ_APP_CLASS_COPY_CONSTRUCTOR;
  return flags;
}

It will only work with compilers that support the C++11 standard, but even if your compiler doesn't support it I believe it will help you understand how the flags should be determined for the types.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Okay, then here is all the steps involved:

The script function:


void onRender(Renderer@ renderer)
{
     Color c;
     c.r = 255;
     c.g = 200;
     c.b  = 100;
     //c.alpha is by default 255, so we should be able to see a shape nevertheless, but c.alpha is a trashed low value inside     drawDebugQuad(), which causes us not to see anything
     renderer.drawDebugQuad(200,200,0,200,200, c);
}
 

The relevant function drawDebugQuad:


r = asEngine->RegisterObjectMethod("Renderer", "void drawDebugQuad(float,float,float,float,float,Color)", asMETHOD(Renderer, drawDebugQuad), asCALL_THISCALL);

Signature: void Renderer::drawDebugQuad(float x, float y, float angle, float width, float height, Color color)
 

The relevant Color type:


 asEngine->RegisterObjectType("Color", sizeof(Color), asOBJ_VALUE | asOBJ_APP_CLASS_CDAK);
asEngine->RegisterObjectProperty("Color", "uint8 r", asOFFSET(Color, r));
asEngine->RegisterObjectProperty("Color", "uint8 g", asOFFSET(Color, g));
asEngine->RegisterObjectProperty("Color", "uint8 b", asOFFSET(Color, b));
asEngine->RegisterObjectProperty("Color", "uint8 a", asOFFSET(Color, a));
asEngine->RegisterObjectBehaviour("Color", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(ColorCTOR), asCALL_CDECL_OBJLAST);
asEngine->RegisterObjectBehaviour("Color", asBEHAVE_CONSTRUCT, "void f(int, int, int)", asFUNCTION(ColorCTOR2), asCALL_CDECL_OBJLAST);
asEngine->RegisterObjectBehaviour("Color", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(ColorDTOR), asCALL_CDECL_OBJLAST);
asEngine->RegisterObjectBehaviour("Color", asBEHAVE_CONSTRUCT, "void f(const Color& in)", asFUNCTION(ObjectCopyConstructor<Color>), asCALL_CDECL_OBJLAST);
 


template<typename T>
void ObjectCopyConstructor(const T &in, void* address){
new(address) T(in);
}
 

Color class interface is simple:


Color();
Color(int byteRed, int byteGreen, int byteBlue, int byteAlpha = 255);
Uint8 r,g,b,a;

 

Also already tried using POD types to the same result. Im not sure whats wrong.

Note: If i register the Color type using generics it works as-is.

From what you're showing the color type should be registered with the flags asOBJ_APP_CLASS_C | asOBJ_APP_CLASS_ALLINTS

This definitely what is causing the error on andriod a linux.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Thanks again Andreas! I was short on knowledge on this one..

So I only specify the asOBJ_APP_CLASS_* flags for members that I EXPLICITLY define? Or is it something else? Also, how important is that ALLINTS flag exactly? when do we not use it?

Yes, that's the basic rule.

The asOBJ_APP_CLASS_ALLINTS is necessary on those platform where the value is passed in different cpu registers depending on the content of the class. Linux and Android are examples of such platforms.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

So, I will never need platform specific registration code as long as I use the right flags, right? Because it would simply be too much #ifdef linux , this way #ifdef windows another way #else generic calls..

But nevertheless, so far, so good! Thanks! This library must be one of the most incredible pieces of software I've seen! :)

No you will not need any #ifdef in your code. The same flags can and should preferably be used on all platforms. The library will take care of the differences.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement