From 7363f062baf99097a9a5e2e4366cced0afd526cb Mon Sep 17 00:00:00 2001 From: kartofen Date: Fri, 28 Oct 2022 22:23:02 +0300 Subject: wasm support --- src/display.c | 19 +++++----- src/display.h | 9 +++-- src/main.c | 55 +++++++++++++++++++++-------- src/shell.html | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 166 insertions(+), 26 deletions(-) create mode 100644 src/shell.html (limited to 'src') diff --git a/src/display.c b/src/display.c index 2a46493..9ecb6cb 100644 --- a/src/display.c +++ b/src/display.c @@ -1,4 +1,9 @@ -#include +#ifdef PLATFORM_WEB + #include RAYLIB_SRC +#else + #include +#endif + #include "display.h" static dspl_createinfo info; @@ -7,7 +12,7 @@ void dspl_create(dspl_createinfo createinfo) { info = createinfo; } -void dspl_start() +void dspl_start(void) { InitWindow(info.width, info.height, info.name); SetTargetFPS(info.fps); @@ -22,21 +27,17 @@ void dspl_start() } } -void dspl_destroy() +void dspl_destroy(void) { CloseWindow(); } void dspl_draw_circle(int cx, int cy, int r) { - int offsetx = (info.width/2); - int offsety = (info.height/4); - DrawCircle(offsetx + cx, offsety + cy, r, RED); + DrawCircle(OFFSETX + cx, OFFSETY + cy, r, RED); } void dspl_draw_line(int x1, int y1, int x2, int y2, int c) { - int offsetx = (info.width/2); - int offsety = (info.height/4); - DrawLine(offsetx + x1, offsety + y1, offsetx + x2, offsety + y2 , (c == 0) ? BLUE : RED); + DrawLine(OFFSETX + x1, OFFSETY + y1, OFFSETX + x2, OFFSETY + y2 , (c == 0) ? BLUE : RED); } diff --git a/src/display.h b/src/display.h index 088feb7..1dadb0f 100644 --- a/src/display.h +++ b/src/display.h @@ -6,12 +6,15 @@ typedef struct dspl_createinfo { int height; char *name; int fps; - void (*update_func)(); + void (*update_func)(void); } dspl_createinfo; +#define OFFSETX (info.width/2) +#define OFFSETY (info.height/8) + void dspl_create(dspl_createinfo info); -void dspl_start(); -void dspl_destroy(); +void dspl_start(void); +void dspl_destroy(void); void dspl_draw_line(int x1, int y1, int x2, int y2, int c); void dspl_draw_circle(int cx, int cy, int r); diff --git a/src/main.c b/src/main.c index 4742ab8..dd37021 100644 --- a/src/main.c +++ b/src/main.c @@ -1,27 +1,36 @@ #include +#include +#include +#include #include #include "display.h" -#define W 1600 -#define H 900 -#define FPS 1000 -#define SCALE 1000 -#define SWING_SCALE 15 +#define W 800 +#define H 600 + +#ifndef PLATFORM_WEB + #define FPS 120 +#else + #define FPS 60 +#endif + +#define SCALE 800 +#define CYCLES 5 #define RFPS 1/FPS #define PI 3.141592654 +#define g -9.81f * SCALE -const float s = (PI/180 * 25); // rad -const float l = 0.5 * SCALE; // m -const int m = 7; // kg -const float g = -9.81f * SCALE; // m/s^2 +float s = 25; // starting agnle +float l = 0.5; // lenght +float m = 0.01; // mass in kg float t; float v; float a; float secs = 0.0f; -int swings = 2 * SWING_SCALE; +int swings = 2 * CYCLES; int has_reached_starting_amp(float old_t) { @@ -34,17 +43,22 @@ int has_reached_starting_amp(float old_t) void update() { + if(secs == 0.0f) { + printf("\nStarting angle: %f", s/(PI/180)); + printf("\nLenght: %f", l/SCALE); + printf("\nMass: %f\n", m/SCALE); + } + float old_t = t; a = (g/l) * sinf(t); v += a * RFPS; t += v * RFPS; if(has_reached_starting_amp(old_t)) { - puts("new swing"); swings += 1; } - if(swings >= 2 * SWING_SCALE) { - printf("period: %f\n", secs/SWING_SCALE); + if(swings >= 2 * CYCLES) { + printf("period: %f\n", secs/CYCLES); secs = 0.0f; swings = 0; } @@ -57,8 +71,21 @@ void update() dspl_draw_circle(px, py, m); } -int main(void) +int main(int argc, char **argv) { + if(argc != 1 && argc != 4) { + puts("Please provide either 0 or 3 arguments:\n\t1st is starting angle in degrees\n\t2nd is lenght in meters\n\t3rd is mass in kilograms"); + return 1; + } + + float arg_s = strtof(argv[1], NULL); + float arg_l = strtof(argv[2], NULL); + float arg_m = strtof(argv[3], NULL); + + s = ((arg_s == 0.0f) ? s : arg_s) * PI/180; + l = ((arg_l == 0.0f) ? l : arg_l) * SCALE; + m = ((arg_m == 0.0f) ? m : arg_m) * SCALE; + t = s; v = 0; a = 0; diff --git a/src/shell.html b/src/shell.html new file mode 100644 index 0000000..f8e31a8 --- /dev/null +++ b/src/shell.html @@ -0,0 +1,109 @@ + + + + + + Pendulum + + + +
Downloading...
+ +
+ +
+ +
+
+
+
+
+
+
+ +
+ + + + + + {{{ SCRIPT }}} + + -- cgit v1.2.3