Skip to content

Commit

Permalink
moved client code
Browse files Browse the repository at this point in the history
  • Loading branch information
hannorein committed Nov 5, 2023
1 parent 5508232 commit a597ddc
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 45 deletions.
2 changes: 1 addition & 1 deletion examples/shearing_sheet/Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
64 changes: 52 additions & 12 deletions examples/web_client/problem.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,58 @@
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <emscripten/fetch.h>
#include "rebound.h"
#include "fmemopen.h"
#include "display.h"
#include "input.h"
#include <emscripten/fetch.h>

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");
Expand All @@ -23,34 +65,32 @@ 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");

}

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);
//}
Expand Down
29 changes: 1 addition & 28 deletions src/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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){
Expand All @@ -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;
Expand Down
12 changes: 8 additions & 4 deletions src/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <GLFW/glfw3.h>
void reb_display_keyboard(GLFWwindow* window, int key, int scancode, int action, int mods);
#endif // __EMSCRIPTEN__
#endif // OPENGL
#endif

0 comments on commit a597ddc

Please sign in to comment.