diff --git a/examples/shearing_sheet/Makefile b/examples/shearing_sheet/Makefile index f4b086dc4..e73959125 100644 --- a/examples/shearing_sheet/Makefile +++ b/examples/shearing_sheet/Makefile @@ -1,5 +1,5 @@ export OPENGL=1 -#export OPT=-DSERVER +export OPT=-DSERVER include ../../src/Makefile.defs # CCPROBLEM is defined in Makefile.defs to allow for diff --git a/examples/web_client/problem.c b/examples/web_client/problem.c index 2b0f00579..c689f0808 100644 --- a/examples/web_client/problem.c +++ b/examples/web_client/problem.c @@ -2,16 +2,58 @@ #include #include #include +#include #include "rebound.h" #include "fmemopen.h" #include "display.h" #include "input.h" -#include + int first = 1; -void newCallback(struct reb_simulation* r); +static void request_key_succeeded(emscripten_fetch_t *fetch) { + emscripten_fetch_close(fetch); // Free data associated with the fetch. +} -void downloadSucceeded(emscripten_fetch_t *fetch) { +void request_failed(emscripten_fetch_t *fetch) { + printf("Requesting %s failed (status code: %d). Server might have shut down.\n", fetch->url, fetch->status); + emscripten_fetch_close(fetch); // Also free data on failure. +} + +void send_key(int key){ + emscripten_fetch_attr_t attr; + emscripten_fetch_attr_init(&attr); + strcpy(attr.requestMethod, "GET"); + attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY; + attr.onsuccess = request_key_succeeded; + attr.onerror = request_failed; + char buffer[1024]; + sprintf(buffer, "http://localhost:1234/keyboard/%d", key); + emscripten_fetch(&attr, buffer); +} + +void reb_display_keyboard_passthrough(GLFWwindow* window, int key, int scancode, int action, int mods){ + struct reb_display_data* data = glfwGetWindowUserPointer(window); + if (!data){ + printf("Error accessing data in reb_display_keyboard\n"); + return; + } + if (action==GLFW_PRESS){ + switch(key){ + case 'Q': + case ' ': + send_key(key); + break; + default: + break; + } + } + reb_display_keyboard(window, key, scancode, action, mods); +} + + +void request_frame_from_server(struct reb_simulation* r); + +void request_frame_succeeded(emscripten_fetch_t *fetch) { struct reb_simulation* r = fetch->userData; FILE* fin = reb_fmemopen((void*)fetch->data, fetch->numBytes, "r"); @@ -23,26 +65,24 @@ void downloadSucceeded(emscripten_fetch_t *fetch) { r->display_data = calloc(1, sizeof(struct reb_display_data)); r->display_data->r = r; reb_display_init(r); // Will return. Display routines running in animation_loop. + glfwSetKeyCallback(r->display_data->window, reb_display_keyboard_passthrough); } first = 0; emscripten_fetch_close(fetch); // Free data associated with the fetch. emscripten_sleep(1000./120.); - newCallback(r); + request_frame_from_server(r); } -void downloadFailed(emscripten_fetch_t *fetch) { - printf("Requesting %s failed (status code: %d). Server might have shut down.\n", fetch->url, fetch->status); - emscripten_fetch_close(fetch); // Also free data on failure. -} -void newCallback(struct reb_simulation* r){ + +void request_frame_from_server(struct reb_simulation* r){ emscripten_fetch_attr_t attr; emscripten_fetch_attr_init(&attr); attr.userData = r; strcpy(attr.requestMethod, "GET"); attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY; - attr.onsuccess = downloadSucceeded; - attr.onerror = downloadFailed; + attr.onsuccess = request_frame_succeeded; + attr.onerror = request_failed; emscripten_fetch(&attr, "http://localhost:1234/simulation"); } @@ -50,7 +90,7 @@ void newCallback(struct reb_simulation* r){ int main(int argc, char* argv[]) { struct reb_simulation* r = reb_simulation_create(); r->t = 123.; - newCallback(r); + request_frame_from_server(r); //while(1){ //sleep(1); //} diff --git a/src/display.c b/src/display.c index ee6f7d54f..73e3fb067 100644 --- a/src/display.c +++ b/src/display.c @@ -330,29 +330,8 @@ static void reb_display_cursor(GLFWwindow* window, double x, double y){ } } } -#ifdef __EMSCRIPTEN__ -static void downloadSucceeded(emscripten_fetch_t *fetch) { - emscripten_fetch_close(fetch); // Free data associated with the fetch. -} -static void downloadFailed(emscripten_fetch_t *fetch) { - printf("Downloading %s failed, HTTP failure status code: %d.\n", fetch->url, fetch->status); - emscripten_fetch_close(fetch); // Also free data on failure. -} -void send_key(int key){ - emscripten_fetch_attr_t attr; - emscripten_fetch_attr_init(&attr); - strcpy(attr.requestMethod, "GET"); - attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY; - attr.onsuccess = downloadSucceeded; - attr.onerror = downloadFailed; - char buffer[1024]; - sprintf(buffer, "http://localhost:1234/keyboard/%d", key); - emscripten_fetch(&attr, buffer); -} -#endif // __EMSCRIPTEN__ - -static void reb_display_keyboard(GLFWwindow* window, int key, int scancode, int action, int mods){ +void reb_display_keyboard(GLFWwindow* window, int key, int scancode, int action, int mods){ struct reb_display_data* data = glfwGetWindowUserPointer(window); if (!data){ printf("Error accessing data in reb_display_keyboard\n"); @@ -366,9 +345,6 @@ static void reb_display_keyboard(GLFWwindow* window, int key, int scancode, int break; case 'Q': data->r->status = REB_STATUS_USER; -#ifdef __EMSCRIPTEN__ - send_key(key); -#endif //__EMSCRIPTEN__ break; case ' ': if (data->r->status == REB_STATUS_PAUSED){ @@ -378,9 +354,6 @@ static void reb_display_keyboard(GLFWwindow* window, int key, int scancode, int printf("Pause.\n"); data->r->status = REB_STATUS_PAUSED; } -#ifdef __EMSCRIPTEN__ - send_key(key); -#endif //__EMSCRIPTEN__ break; case 'S': data->spheres = (data->spheres+1)%3; diff --git a/src/display.h b/src/display.h index 0b53761ea..a4f08d75c 100644 --- a/src/display.h +++ b/src/display.h @@ -27,12 +27,16 @@ struct reb_simulation; -/** - * @brief This function initializes OpenGL and starts the run loop. - * @param data A struct containing all the data needed by the visualization. - */ void reb_display_init(struct reb_simulation* const r); void reb_display_init_data(struct reb_simulation* const r); +#ifdef OPENGL +#ifdef __EMSCRIPTEN__ +// Only needed for emscripten client +#define GLFW_INCLUDE_NONE +#include +void reb_display_keyboard(GLFWwindow* window, int key, int scancode, int action, int mods); +#endif // __EMSCRIPTEN__ +#endif // OPENGL #endif