hot.c (2733B)
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <dlfcn.h> 4 #include <signal.h> 5 6 #include <GLFW/glfw3.h> 7 #define NK_INCLUDE_FIXED_TYPES 8 #define NK_INCLUDE_STANDARD_IO 9 #define NK_INCLUDE_STANDARD_VARARGS 10 #define NK_INCLUDE_DEFAULT_ALLOCATOR 11 #define NK_INCLUDE_VERTEX_BUFFER_OUTPUT 12 #define NK_INCLUDE_FONT_BAKING 13 #define NK_INCLUDE_DEFAULT_FONT 14 #define NK_IMPLEMENTATION 15 #define NK_GLFW_GL2_IMPLEMENTATION 16 #define NK_KEYSTATE_BASED_INPUT 17 #include <nuklear.h> 18 #include <nuklear_glfw_gl2.h> 19 #include "common.h" 20 21 #define MAX_MEMORY 4064 22 #define WINDOW_WIDTH 512 23 #define WINDOW_HEIGHT 512 24 25 void* state = NULL; 26 int should_exit = 0; 27 // init gui state 28 int width,height; 29 struct nk_context* ctx; 30 GLFWwindow* win; 31 32 void sig_handle() { 33 printf("Reloaded\n"); 34 system("date"); 35 should_exit = 1; 36 } 37 38 typedef void* (*module_main_func)(void*, int, int); 39 40 static void error_callback(int e, const char *d) 41 {printf("Error %d: %s\n", e, d);} 42 43 int main(int argc, char* argv[]) { 44 signal(SIGQUIT, sig_handle); 45 glfwSetErrorCallback(error_callback); 46 if (!glfwInit()) { 47 fprintf(stdout, "[GFLW] failed to init!\n"); 48 exit(1); 49 } 50 /***********************/ 51 win = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Balance", NULL, NULL); 52 glfwMakeContextCurrent(win); 53 glfwGetWindowSize(win, &width, &height); 54 55 /* GUI */ 56 ctx = nk_glfw3_init(win, NK_GLFW3_INSTALL_CALLBACKS); 57 /***********************/ 58 //nk_init_fixed(ctx, calloc(1, MAX_MEMORY), MAX_MEMORY, &font); 59 { 60 struct nk_font_atlas *atlas; 61 nk_glfw3_font_stash_begin(&atlas); 62 struct nk_font *droid = nk_font_atlas_add_from_file(atlas, "./Ubuntu-Medium.ttf", 14, 0); 63 nk_glfw3_font_stash_end(); 64 nk_style_set_font(ctx, &droid->handle); 65 } 66 while(1) { 67 void* module = dlopen("./libbalance.so", RTLD_NOW); 68 while(module == NULL) { 69 fprintf(stderr, "Failed to load module. (%s)\n", dlerror()); 70 fprintf(stderr, "Press return to try again.\n"); 71 getchar(); 72 module = dlopen("./libbalance.so", RTLD_NOW); 73 } 74 module_main_func module_main = dlsym(module, "module_main"); 75 while (!glfwWindowShouldClose(win) && !should_exit) { 76 glfwPollEvents(); 77 nk_glfw3_new_frame(); 78 state = module_main((void*)ctx, width, height); 79 glfwGetWindowSize(win, &width, &height); 80 glViewport(0, 0, width, height); 81 glClearColor(0.10f, 0.18f, 1.0f, 1.0f); 82 glClear(GL_COLOR_BUFFER_BIT); 83 /* IMPORTANT: `nk_glfw_render` modifies some global OpenGL state 84 * with blending, scissor, face culling and depth test and defaults everything 85 * back into a default state. Make sure to either save and restore or 86 * reset your own state after drawing rendering the UI. */ 87 nk_glfw3_render(NK_ANTI_ALIASING_ON); 88 glfwSwapBuffers(win); 89 } 90 dlclose(module); 91 should_exit = 0; 92 } 93 94 return 0; 95 }