Skip to content

Commit

Permalink
Cheerp pong tutorial: bound platform movement (#39)
Browse files Browse the repository at this point in the history
Co-authored-by: Alex Bates <[email protected]>
  • Loading branch information
cattabiani and bates64 authored Nov 29, 2023
1 parent 2183244 commit e49d346
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/content/docs/cheerp/01-tutorials/03-pong.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,11 @@ Let's add two new methods to `Platform`:
```cpp title="pong.cpp"
void moveLeft()
{
x -= 5;
x = fmax(x-10, 0);
}
void moveRight()
void moveRight(int Xmax)
{
x += 5;
x += fmin(x+10, Xmax-width);
}

```
Expand All @@ -235,9 +235,10 @@ void Graphics::keyDownHandler(client::KeyboardEvent* e)
if(e->get_keyCode() == 37)
platform.moveLeft();
else if(e->get_keyCode() == 39)
platform.moveRight();
platform.moveRight(400);
}
```
Notice that we are using `fmin` and `fmax` instead of the usual `std::min`/`std::max`. In general, standard library (STL) functions are compiled to Wasm. We can use STL functions from the JS code but there are restrictions. One of these dictates that Wasm functions that have pointers or references to basic types are not available in the JS-annotated part of the code. Indeed, `std::min`/`std::max` get references to basic types while `std::fmin`/`std::fmax` get their inputs by copy. Another solution would have been to wrap `std::min`/`std::max` in non-JS-annotated functions that take `int`s by copy. This limitation will hopefully disappear at some point in the future.

Let's also register an `EventListener` in `Graphics::initializeCanvas`.

Expand Down

0 comments on commit e49d346

Please sign in to comment.