P/Invoke Bridge

Overview

bridge.cpp is the interface layer between C# (Mono) and C++. It contains:

  • A global static VulkanRenderer g_renderer; instance
  • extern "C" functions that delegate to g_renderer methods
  • Try/catch wrappers for functions that may throw

C# calls these functions via [DllImport("renderer")] in managed/rendering/NativeBridge.cs.

Bridge Functions by Category

Lifecycle

C BridgeC++ MethodNotes
renderer_init(w, h, title) → boolinit()try/catch
renderer_cleanup()cleanup()try/catch
renderer_should_close() → boolshouldClose()
renderer_poll_events()pollEvents()
renderer_render_frame()renderFrame()try/catch

Input

C BridgeC++ MethodNotes
renderer_is_key_pressed(key) → intisKeyPressed()returns 0/1
renderer_get_cursor_pos(x*, y*)getCursorPos()out params
renderer_set_cursor_locked(locked)setCursorLocked()int→bool
renderer_is_cursor_locked() → intisCursorLocked()bool→int
renderer_is_mouse_button_pressed(btn) → intisMouseButtonPressed()returns 0/1
renderer_get_scroll_offset(x*, y*)getScrollOffset()out params
renderer_reset_scroll_offset()resetScrollOffset()

Mesh

C BridgeC++ MethodNotes
renderer_load_mesh(path) → intloadMesh()try/catch, returns mesh ID
renderer_create_entity(mesh_id) → intcreateEntity()try/catch, returns entity ID
renderer_set_entity_transform(id, mat4)setEntityTransform()try/catch
renderer_remove_entity(id)removeEntity()try/catch
renderer_load_model(path) → boolloadModel()legacy, try/catch
renderer_set_rotation(rx, ry, rz)setRotation()legacy

Procedural Primitives

C BridgeC++ MethodNotes
renderer_create_box_mesh(w,h,l,r,g,b) → intcreateBoxMesh()try/catch
renderer_create_sphere_mesh(rad,seg,ring,r,g,b) → intcreateSphereMesh()try/catch
renderer_create_plane_mesh(w,h,r,g,b) → intcreatePlaneMesh()try/catch
renderer_create_cylinder_mesh(rad,h,seg,r,g,b) → intcreateCylinderMesh()try/catch
renderer_create_capsule_mesh(rad,h,seg,ring,r,g,b) → intcreateCapsuleMesh()try/catch

Camera

C BridgeC++ Method
renderer_set_camera(eyeXYZ, targetXYZ, upXYZ, fov)setCamera()

Lighting

C BridgeC++ Method
renderer_set_light(idx, type, pos, dir, color, intensity, radius, cones)setLight()
renderer_clear_light(idx)clearLight()
renderer_set_ambient(intensity)setAmbientIntensity()

Time

C BridgeC++ Method
renderer_update_time()updateTime()
renderer_get_delta_time() → floatgetDeltaTime()
renderer_get_total_time() → floatgetTotalTime()

Debug

C BridgeC++ MethodNotes
renderer_set_debug_overlay(enabled)setDebugOverlay()int→bool
renderer_get_entity_count() → intgetActiveEntityCount()

Debug Wireframe Entities

C BridgeC++ MethodNotes
renderer_create_debug_entity(mesh_id) → intcreateDebugEntity()try/catch, returns ID
renderer_set_debug_entity_transform(id, mat4)setDebugEntityTransform()try/catch
renderer_remove_debug_entity(id)removeDebugEntity()try/catch
renderer_clear_debug_entities()clearDebugEntities()try/catch

Type Mapping (C# → C++)

C# TypeC BridgeC++ Type
intintint
floatfloatfloat
bool (return)boolbool
stringconst char*const char*
float[]const float*const float*
out doubledouble*double&
out floatfloat*float&
int (bool param)intconverted to bool via != 0

NativeBridge.cs Convenience Wrappers

The C# side provides both raw [DllImport] declarations and convenience wrappers:

  • IsKeyPressed(key) → converts int return to bool
  • SetCursorLocked(bool) → converts bool to int parameter
  • Overloaded primitives with default color (0.7, 0.7, 0.7) and segment counts (32 segments, 16 rings)
  • GLFW key/mouse button constants defined as const int

:::tip Where to Edit Adding a new bridge function (3-file checklist):

  1. native/renderer.h — Declare the method on VulkanRenderer (public section)
  2. native/renderer/, native/scene/, or native/ui/ — Implement the method in the appropriate .cpp file
  3. native/bridge.cpp — Add extern "C" wrapper calling g_renderer.method(). Use try/catch if it can throw.
  4. managed/rendering/NativeBridge.cs — Add [DllImport(LIB)] declaration + optional convenience wrapper :::