Return a vec2 from a Codea addon?

Hi, how do you return a vec2 value from a codea addon? I tried using Pnew from the old runtime, but it doesn’t seem to work. Do you have any example on how to do it?

@tnlogy I’ll have to create some custom headers to bundle with exported projects for pushing (and reading) Codea data types. For now you can declare the following function prototypes in a header and try them out — let me know if it works:

Let’s call this CodeaLuaTypes.h for now

    typedef struct color_type_t
        lua_Number r,g,b,a;
    } color_type;

    //Read vec types from stack (check actually verifies type)
    lua_Number *getvec2(lua_State *L, int i);
    lua_Number *checkvec2(lua_State *L, int i);
    lua_Number *getvec3(lua_State *L, int i);
    lua_Number *checkvec3(lua_State *L, int i);
    lua_Number *getvec4(lua_State *L, int i);
    lua_Number *checkvec4(lua_State *L, int i);

    //Push vec types on the stack
    void pushvec2(lua_State *L, lua_Number x, lua_Number y);
    void pushvec3(lua_State *L, lua_Number x, lua_Number y, lua_Number z);
    void pushvec4(lua_State *L, lua_Number x, lua_Number y, lua_Number z, lua_Number w);

    //Check and fetch the image on the stack at index i
    image_type *checkimage(lua_State *L, int i);

    //Push image data or UIImage on the stack
    image_type* pushimage(lua_State *L, unsigned char* data, size_t width, size_t height, boolean_t premultipliedAlpha, float scale);
    image_type* pushUIImage(lua_State* L, UIImage* image);

    //Color type
    color_type *getcolor(lua_State *L, int i);
    color_type *checkcolor(lua_State *L, int i);

    //Creates the userdata and puts it on the stack, and returns the same userdata
    color_type* pushcolor(lua_State *L, lua_Number r, lua_Number g, lua_Number b, lua_Number a);


`image_type` can probably be defined as (untested, may have issues):

typedef unsigned char image_color_element;
typedef struct image_type_data_t {
    image_color_element r,g,b,a;
} image_type_data;

typedef struct image_type_t
    lua_Integer scaledWidth, scaledHeight; //Scaled by 1/contentScaleFactor (user facing)
    lua_Integer rawWidth, rawHeight; //The raw size (renderer facing)
    NSUInteger scaleFactor;
    image_type_data* data;
    BOOL dataChanged;
    BOOL needsFlush;
    void* texture;
    boolean_t premultiplied;
    boolean_t hasFramebuffer;
    GLuint framebufferHandle;
    GLuint depthRenderBuffer;
} image_type;


Thanks, that’s great! I’ve successfully used createUIImageFromImage, but it looks nice to get the image_type_data, if I figure out the format, to save some conversion steps and maybe speed things up.

My camera feature detection code using OpenCV and Codea is really slow though. :slight_smile:

Thanks a lot!