I am programming a fighting game in C with a kind of API for allowing the add of plugins (shared library). Each plugin is a kind of artificial intelligence which can play the game. A decision can be taken each end frame during the game. My plugins has this kind of structure :
#include "acquisition.h"
void decision_frame(Decision_frame* d){
void* me = tell_me_who_i_am("my_plugin.so");
if (some_information_about(me) == foo){
d->turn_left = 12;
}
/* bla bla bla */
}
So, I realize I am trying to implement some objects in non oriented object language and, due to that, the "void* me" is a variable which identify the caller. The header acquisition look like :
#ifndef _GUARD_
#define _GUARD_
typedef struct{
int move;
double angle_move;
double angle_gun;
double angle_radar;
int shot;
}Decision_frame;
void* tell_me_how_i_am(char* plug_name);
Bar some_information_about(void* me);
Foo other_information_about(void* me);
#endif
My acquisition.c (kind of API part...) file currently contains that :
Space* get_space(void){
static Space S;
printf("Space adress (by getter) : %p\n", &S);
return &S;
}
Finally, my main do that :
1 -> Space initialization (use of getter) to set a local variable in main pointing to the Space. (POINT ONE) 2 -> Do some allocation memory inside field of my struct Space. 3 -> recall of the getter after a bunch of dlopen over the plugins bar.so (POINT TWO)
I thus have by some trashly verbose :
Space address (by getter) : 0x6071c0 (POINT ONE)
Space address in main : 0x6071c0
Auto-loading of plugins...
2 potential plugins founds :
- alea_all.so... loading OK
- full_dummy.so... loading OK
Space address (by getter) : 0x7ff4e6c650a0 (POINT TWO)
Space address in main : 0x6071c0
I imagine mixing position independent code and non position independent code is bad!!!!!!! I handle the compilation as follow :
bin/acquisition.o: src/acquisition.c includes/acquisition.h
$(GCC) -fpic -c src/acquisition.c -o bin/acquisition.o $(CFLAGS)
plugins/full_dummy.so: plugins/full_dummy.c bin/acquisition.o
$(GCC) -c -fPIC plugins/full_dummy.c -o plugins/full_dummy.o $(CFLAGS)
$(GCC) -shared -o plugins/full_dummy.so plugins/full_dummy.o bin/acquisition.o $(CFLAGS)
I do not know : how much a -fpic code could depend from non -fpic code (because a lot of non fpic modules uses the static variables from acquisition.c) ? How much static variables could be used in this kind of pattern ?
And is there a trivial, safe and nice way to design this kind of problem in Pure C ? I mean, plugin some AI on a same program...
Sorry for my English and for being long...