Example C Callback

Here's an example of how we could take the ClusterIO sample application and replace the Lua functionality with C. The action that is triggered by the cluster_update event should be changed from a gra.lua to a gra.ccallback action, it should call the same function as before, CBUpdateEvent. This library has a dependency on libgre.

ClusterIO_ccallbacks.h

#include <stddef.h>
#include <gre/gre.h>

void cbClusterUpdate(gr_action_context_t *action_context);

sb_ccallback_t clist[] = {
	{ "CBUpdateEvent", &cbClusterUpdate },
	{ NULL, NULL },
};
                

ClusterIO_ccallbacks.c

   
#include <stdio.h>
#include <string.h>
#include "ClusterIO_ccallbacks.h"

#include <ClusterIO_events.h>

#if defined(GRE_FEATURE_VFS_RESOURCES)
#include <gre/sdk/sbresource_vfs.h>
#endif

void
cbClusterUpdate(gr_action_context_t *action_context) {
	gr_application_t *app; 
	cluster_update_event_t *cluster_data;
	gr_data_union_t data;
	void *event_data;
	int nbytes;

	app = gr_context_get_application(action_context); 
	if(app == NULL) {
	    return;
	}

	event_data = gr_context_get_event_data(action_context, &nbytes);
	if (nbytes != sizeof(cluster_update_event_t)) {
		return;
	}

	cluster_data = (cluster_update_event_t *)event_data;
	
	memset(&data, 0, sizeof(data));
	data.i16 = cluster_data->speed;
	gr_application_set_data(
                                app, 
                                "speedometer_content.speed.text", 
                                "2s1", 
                                &data
                              );

	data.f32 = ((float)cluster_data->speed * (214.0f / 200.0f)) - 107;
	gr_application_set_data(
                                app, 
                                "speedometer.pointer_speedometer.rot", 
                                "4f1", 
                                &data);

	data.f32 = ((float)cluster_data->rpm / 10000) * 49;
	gr_application_set_data(
                                app, 
                                "tach_exterior.pointer_tach_exterior.rot", 
                                "4f1", 
                                &data);
}


DLLExport sb_ccallback_t *
sb_get_ccallback_list() {
	return clist;
}