heload, a mess
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
#define WINDOW_WIDTH 1280
|
||||
#define WINDOW_HEIGHT 720
|
||||
|
||||
namespace hiisi {
|
||||
/* This function runs once at startup. */
|
||||
int init(EngineData *state, int argc, char *argv[])
|
||||
{
|
||||
@@ -52,7 +53,7 @@ int iterate(EngineData *state)
|
||||
|
||||
/* we'll have the rectangles grow and shrink over a few seconds. */
|
||||
const float direction = ((now % 2000) >= 1000) ? 1.0f : -1.0f;
|
||||
const float scale = ((float) (((int) (now % 1000)) - 500) / 500.0f) * direction;
|
||||
const float scale = ((float) (((int) (now % 1000)) - 500) / 50.0f) * direction;
|
||||
|
||||
/* as you can see from this, rendering draws over whatever was drawn before it. */
|
||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE); /* black, full alpha */
|
||||
@@ -64,7 +65,7 @@ int iterate(EngineData *state)
|
||||
pretty standard in 2D graphics. */
|
||||
|
||||
/* Let's draw a single rectangle (square, really). */
|
||||
rects[0].x = rects[0].y = 100;
|
||||
rects[0].x = rects[0].y = 10;
|
||||
rects[0].w = rects[0].h = 100 + (100 * scale);
|
||||
SDL_SetRenderDrawColor(renderer, 250, 0, 255, SDL_ALPHA_OPAQUE); /* red, full alpha */
|
||||
SDL_RenderRect(renderer, &rects[0]);
|
||||
@@ -110,3 +111,4 @@ void quit(EngineData *state)
|
||||
{
|
||||
/* SDL will clean up the window/renderer for us. */
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,25 +1,17 @@
|
||||
#ifndef HIISI_H_
|
||||
#define HIISI_H_
|
||||
|
||||
extern "C" {
|
||||
namespace hiisi {
|
||||
|
||||
struct EngineData {
|
||||
void *window = 0;
|
||||
void *renderer = 0;
|
||||
};
|
||||
struct EngineData {
|
||||
void *window = 0;
|
||||
void *renderer = 0;
|
||||
};
|
||||
|
||||
struct Engine {
|
||||
void *module = 0;
|
||||
int (*init)(EngineData *, int, char**);
|
||||
int (*event)(EngineData *);
|
||||
int (*iterate)(EngineData *);
|
||||
void (*quit)(EngineData *);
|
||||
};
|
||||
int init(EngineData *state, int argc, char *argv[]);
|
||||
int event(EngineData *state);
|
||||
int iterate(EngineData *state);
|
||||
void quit(EngineData *state);
|
||||
|
||||
int init(EngineData *state, int argc, char *argv[]);
|
||||
int event(EngineData *state);
|
||||
int iterate(EngineData *state);
|
||||
void quit(EngineData *state);
|
||||
|
||||
}
|
||||
} // namespace hiisi
|
||||
#endif // HIISI_H_
|
||||
|
||||
@@ -15,101 +15,36 @@ void reload_signal_handler(int) {
|
||||
reload_requested = true;
|
||||
}
|
||||
|
||||
Engine load_hiisi() {
|
||||
const char *name = "build/hiisi/libhiisi-engine.so";
|
||||
void *mod = dlopen(name, RTLD_NOW);
|
||||
|
||||
Lmid_t list;
|
||||
dlinfo(mod, RTLD_DI_LMID, &list);
|
||||
printf("link-map list id: %ld\n", list);
|
||||
|
||||
link_map *lmap = 0;
|
||||
dlinfo(mod, RTLD_DI_LINKMAP, &lmap);
|
||||
|
||||
printf("Link map:\n");
|
||||
printf("addr: %lu\n", lmap->l_addr);
|
||||
printf("name: %s\n", lmap->l_name);
|
||||
printf("dynamic section: %p\n", lmap->l_ld);
|
||||
|
||||
char pathname[256];
|
||||
dlinfo(mod, RTLD_DI_ORIGIN, pathname);
|
||||
printf("path: %s\n", pathname);
|
||||
|
||||
/* Discover the size of the buffer that we must pass to
|
||||
RTLD_DI_SERINFO. */
|
||||
Dl_serinfo serinfo;
|
||||
Dl_serinfo *sip;
|
||||
|
||||
if (dlinfo(mod, RTLD_DI_SERINFOSIZE, &serinfo) == -1) {
|
||||
fprintf(stderr, "RTLD_DI_SERINFOSIZE failed: %s\n", dlerror());
|
||||
void reload_modules() {
|
||||
if (he_reload_module("libhiisi-engine.so") != 0) {
|
||||
fprintf(stderr, "Failed to reload libhiisi-engine.so. Exiting.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
/* Allocate the buffer for use with RTLD_DI_SERINFO. */
|
||||
|
||||
sip = (Dl_serinfo*)malloc(serinfo.dls_size);
|
||||
if (sip == NULL) {
|
||||
perror("malloc");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
/* Initialize the 'dls_size' and 'dls_cnt' fields in the newly
|
||||
allocated buffer. */
|
||||
|
||||
if (dlinfo(mod, RTLD_DI_SERINFOSIZE, sip) == -1) {
|
||||
fprintf(stderr, "RTLD_DI_SERINFOSIZE failed: %s\n", dlerror());
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
/* Fetch and print library search list. */
|
||||
|
||||
if (dlinfo(mod, RTLD_DI_SERINFO, sip) == -1) {
|
||||
fprintf(stderr, "RTLD_DI_SERINFO failed: %s\n", dlerror());
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
for (size_t j = 0; j < serinfo.dls_cnt; j++)
|
||||
printf("dls_serpath[%zu].dls_name = %s\n",
|
||||
j, sip->dls_serpath[j].dls_name);
|
||||
|
||||
Engine engine;
|
||||
engine.module = mod;
|
||||
*(void **)&engine.init = dlsym(mod, "init");
|
||||
*(void **)&engine.event = dlsym(mod, "event");
|
||||
*(void **)&engine.iterate = dlsym(mod, "iterate");
|
||||
*(void **)&engine.quit = dlsym(mod, "quit");
|
||||
|
||||
assert(engine.init);
|
||||
assert(engine.event);
|
||||
assert(engine.iterate);
|
||||
assert(engine.quit);
|
||||
|
||||
return engine;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
|
||||
signal(SIGUSR1, reload_signal_handler);
|
||||
|
||||
EngineData state;
|
||||
|
||||
Engine hiisi = load_hiisi();
|
||||
if (!hiisi.module) {
|
||||
printf("%s\n", dlerror());
|
||||
return 1;
|
||||
if (he_init() != 0) {
|
||||
fprintf(stderr, "Failed to initialize heload. Exiting.\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
hiisi.init(&state, argc, argv);
|
||||
while (hiisi.event(&state)) {
|
||||
hiisi.iterate(&state);
|
||||
he_print_module_infos();
|
||||
|
||||
hiisi::EngineData state;
|
||||
|
||||
hiisi::init(&state, argc, argv);
|
||||
while (hiisi::event(&state)) {
|
||||
hiisi::iterate(&state);
|
||||
if (reload_requested) {
|
||||
dlclose(hiisi.module);
|
||||
hiisi = load_hiisi();
|
||||
reload_modules();
|
||||
reload_requested = false;
|
||||
}
|
||||
}
|
||||
|
||||
dlclose(hiisi.module);
|
||||
he_deinit();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user