Skip to content

Instantly share code, notes, and snippets.

@rezich
Created March 3, 2017 20:30
Show Gist options
  • Save rezich/1d025023f4e2b93df5cad78481065742 to your computer and use it in GitHub Desktop.
Save rezich/1d025023f4e2b93df5cad78481065742 to your computer and use it in GitHub Desktop.
Game Maker DLL example with state preserved between calls
#include <stdlib.h>
#define GMEXPORT extern "C" __declspec(dllexport)
struct City {
int population;
};
City *city;
GMEXPORT double city_init() {
city = new City;
city->population = 0;
return 0;
}
GMEXPORT double city_set_population(double pop) {
city->population = pop;
return 0;
}
GMEXPORT double city_get_population() {
return city->population;
}
GMEXPORT double city_destroy() {
delete city;
return 0;
}
_city_init = external_define("citylib.dll", "city_init", dll_cdecl, ty_real, 0);
_city_set_population = external_define("citylib.dll", "city_set_population", dll_cdecl, ty_real, 1, ty_real);
_city_get_population = external_define("citylib.dll", "city_get_population", dll_cdecl, ty_real, 0);
_city_destroy = external_define("citylib.dll", "city_destroy", dll_cdecl, ty_real, 0);
show_message("Creating city...");
external_call(_city_init);
show_message("City created, setting population to 10000...");
external_call(_city_set_population, 10000);
show_message("Population set, getting population...");
var pop = external_call(_city_get_population);
show_message("...the population is " + string(pop) + ". Destroying city...");
external_call(_city_destroy);
show_message("City destroyed.");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment