Calling a Objc method directly from C

I would like to avoid the middle man and call Objective C directly from C

Currently I do this

This is called from a dispatch table in a pure C file

    _ctx->mt_render_funcs.mtlEnd(_ctx);

Which calls this routine in a obj c file .m

void mtlEnd(MTRenderContext mt_ctx) { // Call the Objective-C method using Objective-C syntax [(__bridge id) mt_ctx->mt_render_funcs.mtlObj mtlEnd]; }

Which ends up here... in ObjC

#pragma mark mtlEnd

  • (void)mtlEnd

{ // vertex buffer size_t size;

size = sizeof(Vertex4ColorNormalTex) * _ctx->vert_eng.current_vertex;

[_currentRenderEncoder setVertexBytes:_ctx->vert_eng.vertices length: size atIndex: VertexInputIndexVertices];

[_currentRenderEncoder drawPrimitives:(MTLPrimitiveType)_ctx->vert_eng.prim_type
                          vertexStart:0
                          vertexCount:_ctx->vert_eng.current_vertex];

}

It would simplify this to get rid of one call and call ObjC directly.

The other idea is I want to use GCD and put a lock / unlock on the call from C to ensure thread safety so I can use GCD to dispatch a thread to do the ObjC routines.

I want to stick with C as the foundation so it can be used directly from C or a FFI interface from other languages. But Metal works well in ObjC and I would prefer to use that.

Thanks ahead of time.

Calling a Objc method directly from C
 
 
Q