Schmavery / reprocessing

ReasonML graphics library inspired by Processing
https://schmavery.github.io/reprocessing/
MIT License
679 stars 24 forks source link

Could a separate tick function make sense? #104

Closed MazeChaZer closed 5 years ago

MazeChaZer commented 5 years ago

I created my first reprocesing project today, worked really well :). A little thing I stumbled upon was how the draw function works. I was surprised that it renders the state but at the same time produces a new state. I was working with Env.deltaTime and asked myself: Should I first render the current state or should I first produce my new state and render that? After looking at the 2048 example I decided to first render the current state and then produce the new state afterwards. But thinking about it now it makes more sense to produce the new state first, so that the time delta is reflected in the rendering. Coming from Elm previously, I was used to the separation of update (which produces the new state) and view (which renders the state). It seems to me, that this separation could simplify the run function, too. Instead of

~?draw: option(('a, glEnvT) => 'a)

there would be

~?tick: option(('a, glEnvT) => 'a)
~?draw: option(('a, glEnvT) => ())

Before each call of the draw function, the tick function would be called, if it was defined. There stuff like time deltas can be handled. Then the draw function is executed and just renders the state. To me this is more intuitive, I'm really interested what others think about this.

Schmavery commented 5 years ago

Thanks for the feedback! This is like this because there's not really any difference in when these functions need to be called and it matches the Processing api.

If you really want this distinction, is there a downside to doing something like this? You could even call them update and view! 😛

let draw = (state, env) => {
 ...do draw things
};

let tick = (state, env) => {
 ...do tick things
 newState
};

Reprocessing.run(
  ~setup,
  ~draw=((s, e) => draw(s, e); tick(s, e)));
MazeChaZer commented 5 years ago

Great idea, I didn't even think about doing that :sweat_smile: This is what I'm doing now in my project. I just switched around the execution order of render and tick, so that the render function gets the updated state from tick:

let draw = (state, env) => {
  let newState = tick(state, env);
  render(newState, env);
  newState;
};

Thanks for your quick response! You can close the issue if there are no further points :)

Schmavery commented 5 years ago

Awesome, glad to hear it helped. Let us know if you run into any other issues :)