hot.c (3678B)
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <dlfcn.h> 4 #include <signal.h> 5 #include <unistd.h> 6 7 #ifdef ENABLE_GUI 8 #include <GLFW/glfw3.h> 9 #define NK_INCLUDE_FIXED_TYPES 10 #define NK_INCLUDE_STANDARD_IO 11 #define NK_INCLUDE_STANDARD_VARARGS 12 #define NK_INCLUDE_DEFAULT_ALLOCATOR 13 #define NK_INCLUDE_VERTEX_BUFFER_OUTPUT 14 #define NK_INCLUDE_FONT_BAKING 15 #define NK_INCLUDE_DEFAULT_FONT 16 #define NK_IMPLEMENTATION 17 #define NK_GLFW_GL2_IMPLEMENTATION 18 #define NK_KEYSTATE_BASED_INPUT 19 #include <nuklear.h> 20 #include <nuklear_glfw_gl2.h> 21 #endif 22 23 #include "common.h" 24 25 #define MAX_MEMORY 4064 26 #define WINDOW_WIDTH 512 27 #define WINDOW_HEIGHT 512 28 #define BUFFER_SIZE 256 29 30 void* state = NULL; 31 int should_exit = 0; 32 // init gui state 33 int width,height; 34 35 #ifdef ENABLE_GUI 36 struct nk_context* ctx; 37 GLFWwindow* win; 38 #endif 39 40 void sig_handle() { 41 printf("Reloaded\n"); 42 system("date"); 43 should_exit = 1; 44 } 45 46 typedef void* (*module_main_func)(void*, int); 47 48 static void error_callback(int e, const char *d) 49 {printf("Error %d: %s\n", e, d);} 50 51 int 52 main(int argc, char* argv[]) { 53 signal(SIGQUIT, sig_handle); 54 #ifdef ENABLE_GUI 55 glfwSetErrorCallback(error_callback); 56 if (!glfwInit()) { 57 fprintf(stdout, "[GFLW] failed to init!\n"); 58 exit(1); 59 } 60 /***********************/ 61 win = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Balance", NULL, NULL); 62 glfwMakeContextCurrent(win); 63 glfwGetWindowSize(win, &width, &height); 64 65 /* GUI */ 66 ctx = nk_glfw3_init(win, NK_GLFW3_INSTALL_CALLBACKS); 67 /***********************/ 68 //nk_init_fixed(ctx, calloc(1, MAX_MEMORY), MAX_MEMORY, &font); 69 { 70 struct nk_font_atlas *atlas; 71 nk_glfw3_font_stash_begin(&atlas); 72 struct nk_font *droid = nk_font_atlas_add_from_file(atlas, "./Ubuntu-Medium.ttf", 14, 0); 73 nk_glfw3_font_stash_end(); 74 nk_style_set_font(ctx, &droid->handle); 75 } 76 while(1) { 77 void* module = dlopen("./libbook.so", RTLD_NOW); 78 while(module == NULL) { 79 fprintf(stderr, "Failed to load module. (%s)\n", dlerror()); 80 fprintf(stderr, "Press return to try again.\n"); 81 getchar(); 82 module = dlopen("./libbook.so", RTLD_NOW); 83 } 84 module_main_func module_main = dlsym(module, "module_main"); 85 while (!glfwWindowShouldClose(win) && !should_exit) { 86 glfwPollEvents(); 87 nk_glfw3_new_frame(); 88 state = module_main((void*)ctx, width, height); 89 glfwGetWindowSize(win, &width, &height); 90 glViewport(0, 0, width, height); 91 glClearColor(0.10f, 0.18f, 1.0f, 1.0f); 92 glClear(GL_COLOR_BUFFER_BIT); 93 /* IMPORTANT: `nk_glfw_render` modifies some global OpenGL state 94 * with blending, scissor, face culling and depth test and defaults everything 95 * back into a default state. Make sure to either save and restore or 96 * reset your own state after drawing rendering the UI. */ 97 nk_glfw3_render(NK_ANTI_ALIASING_ON); 98 glfwSwapBuffers(win); 99 } 100 dlclose(module); 101 should_exit = 0; 102 } 103 #else 104 //while(1) { 105 void* module = dlopen("./libbook.so", RTLD_NOW); 106 while(module == NULL){ 107 fprintf(stderr, "Failed to load module. (%s)\n", dlerror()); 108 fprintf(stderr, "Press return to try again.\n"); 109 getchar(); 110 module = dlopen("./libbook.so", RTLD_NOW); 111 } 112 module_main_func module_main = dlsym(module, "module_main"); 113 FILE* in = fopen("october-2023.txt", "r"); 114 char* data = (char*)malloc(2048 * sizeof(char)); 115 size_t data_size = 0; 116 size_t c_read = 0; 117 while((c_read = fread(data + data_size + 0, 1, BUFFER_SIZE, in)) != 0) { 118 data_size += c_read; 119 } 120 if (ferror(in)) fprintf(stderr, "Error reading file\n"); 121 fprintf(stdout, "Startig loop\n"); 122 module_main(data, data_size); 123 124 while(should_exit == 0) { 125 sleep(1); 126 } 127 should_exit = 0; 128 dlclose(module); 129 fprintf(stderr, "Continue?\n"); 130 //} 131 #endif 132 return 0; 133 } 134