diff options
author | Paweł Redman <pawel.redman@gmail.com> | 2016-03-30 20:29:08 +0200 |
---|---|---|
committer | Paweł Redman <pawel.redman@gmail.com> | 2016-03-30 20:29:08 +0200 |
commit | 745d5a072d8669d1bc35e7c1f28b75b3484542e8 (patch) | |
tree | ecc9cbd03511764e79251ec256c81cb8796f7c36 /src/physics.c | |
parent | bc50836fdcd20fda174df392c264cc6f03030cdf (diff) |
Finish ITC; implement async sim control.
Diffstat (limited to 'src/physics.c')
-rw-r--r-- | src/physics.c | 82 |
1 files changed, 44 insertions, 38 deletions
diff --git a/src/physics.c b/src/physics.c index be6a426..c8a7cbd 100644 --- a/src/physics.c +++ b/src/physics.c @@ -4,6 +4,8 @@ void phy_sim_destroy(phy_sim *sim) { size_t i; SDL_DestroyMutex(sim->rotate_lock); + + itc_chan_destroy(&sim->ctl); for (i = 0; i < 3; i++) { free(sim->fields[i].E); @@ -48,6 +50,9 @@ int phy_sim_create(phy_sim *sim) { phy_sim_compute_const_fields(sim); + itc_chan_create(&sim->ctl); + sim->running = false; + sim->rotate_lock = SDL_CreateMutex(); assert(sim->rotate_lock); @@ -62,15 +67,11 @@ void phy_sim_compute_const_fields(phy_sim *sim) { size_t x, y, z; phy_field_info *fi = &sim->field_info; - size_t border_width; - - border_width = 3; for (z = 0; z < fi->depth; z++) for (y = 0; y < fi->height; y++) for (x = 0; x < fi->width; x++) { float *eps, *mu; - float border_fx, border_fy, border_fz, border_f; eps = sim->field_eps + z * fi->zstr1 + y * fi->ystr1 + x * fi->xstr1; @@ -79,36 +80,6 @@ void phy_sim_compute_const_fields(phy_sim *sim) *eps = 1.129409066837282e+11; *mu = -7.957747154594767e+5; - -/* - if (x < border_width) { - border_fx = (float)x / border_width; - } else if (x >= fi->width - border_width) { - border_fx = 1.0f - ((float)x - fi->width + border_width + 1) / border_width; - } else { - border_fx = 1.0f; - } - - if (y < border_width) { - border_fy = (float)y / border_width; - } else if (y >= fi->height - border_width) { - border_fy = 1.0f - ((float)y - fi->height + border_width + 1) / border_width; - } else { - border_fy = 1.0f; - } - - if (z < border_width) { - border_fz = (float)y / border_width; - } else if (z >= fi->depth - border_width) { - border_fz = 1.0f - ((float)z - fi->depth + border_width + 1) / border_width; - } else { - border_fz = 1.0f; - } - - border_f = min3(border_fx, border_fy, border_fz); - - *eps *= border_f; - *mu *= border_f;*/ } } @@ -228,17 +199,52 @@ void phy_sim_step(phy_sim *sim, float dt) { sim->time += dt; } -/* -void phy_control(phy_command cmd, void *data) +void phy_run_command(phy_sim *sim, int number, void *data) +{ + switch (number) { + case PHY_CMD_PAUSE: + sim->running = false; + break; + + case PHY_CMD_RESUME: + sim->running = true; + break; -} */ + case PHY_CMD_STEP: + phy_sim_step(sim, 0.1); + break; + } +} int phy_thread(phy_sim *sim) { + int rv, cmd_num; + void *cmd_data; + while (1) { - phy_sim_step(sim, 0.1); + if (sim->running) { + while (!itc_chan_pop(&sim->ctl, &cmd_num, &cmd_data)) { + if (cmd_num == PHY_CMD_QUIT) + goto out; + + phy_run_command(sim, cmd_num, cmd_data); + } + + phy_sim_step(sim, 0.1); + } else { + // do it only once per iteration + if (!itc_chan_pop_block(&sim->ctl, &cmd_num, + &cmd_data)) { + if (cmd_num == PHY_CMD_QUIT) + goto out; + + phy_run_command(sim, cmd_num, cmd_data); + } + } } +out: + printf("phy_thread: quitting\n"); } |