Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possibility to set FPS #24

Open
ttulka opened this issue Aug 22, 2021 · 10 comments
Open

Possibility to set FPS #24

ttulka opened this issue Aug 22, 2021 · 10 comments
Labels
enhancement New feature or request

Comments

@ttulka
Copy link
Contributor

ttulka commented Aug 22, 2021

It would be handy to set FPS from the game code. This would save developers some boilerplate code.

export function update(): void {
    // next update will be called in 0.5 sec
    w4.setFPS(30);

    // ...

What do you think?

@aduros
Copy link
Owner

aduros commented Aug 23, 2021

What situations would changing the FPS be useful?

@ttulka
Copy link
Contributor Author

ttulka commented Aug 23, 2021

Each game has its own update speed, usually depending on the level. For instance, your Tetris speeds up the falling of the bricks. Instead of counting the delay in the game code, one could simply change the FPS like this:

w4.setFPS(GAME_FPS[currentLevel]);

This is probably not the only one approach, but similar mechanism would be handy for game developers.

@aduros
Copy link
Owner

aduros commented Aug 23, 2021

Hmm, there could maybe be a memory register for it, although for simplicity I feel like it should remain a constant 60 FPS and leave it up to the game to handle. It doesn't seem like that much boilerplate.

Note that tone() durations are measured in "frames" (1/60th of a second), so redefining the length of a frame might create some confusion there.

@aduros aduros added the enhancement New feature or request label Aug 23, 2021
@ttulka
Copy link
Contributor Author

ttulka commented Aug 23, 2021

Maybe is changing FPS not ideal, I agree. Still, I believe some out-of-the-box mechanism for controlling the game speed provided by the platform would be really handy.

All in all, it is a problem every game must deal with. Why not solve it once and use it everywhere?

As feedback from other users would be helpful. It'd make sense to postpone this.

@leroycep
Copy link

leroycep commented Sep 3, 2021

I think at very least the speed should be limited. Playing the examples on my main monitors is difficult because it runs at 144 Hz, which makes the games much faster.

@FaberVitale
Copy link
Contributor

FaberVitale commented Sep 5, 2021

Documentation is currently a bit misleading:
incorrectly assumes that FPS is always at 60 but the amount of times rAF runs the callback is tied to several factors:

  • is device running in power save mode?
  • What's screen refresh rate?
  • can the device keep a constant frame rate

For instance on my mac plugging and unplugging the power cord changes the frame rate from 60 to 30.
Screenshot 2021-09-05 at 19 14 24
Screenshot 2021-09-05 at 19 15 05

I think there are 2 possible approaches:

  1. Expose a function to cap the frame rate.
  2. Change update signature from fn update(); to fn update(timestamp: WASM i64); where timestamp is the argument that rAF passes to loop .

@ttulka
Copy link
Contributor Author

ttulka commented Sep 6, 2021

@FaberVitale Thanks for clearing things up!

When the platform cannot call update in a constant interval it should provide a way to get the current FPS to ensure consistent speed. The first option proposed by you makes sense to me. Alternatively, load<u8>(w4.FPS); would fit well to the platform's API style.

A workaround would be using a timer in the game's code and performing (or not performing) update based on the time (number of milliseconds) since the previous update.

@DenialAdams
Copy link
Contributor

DenialAdams commented Nov 1, 2021

Documentation is currently a bit misleading: incorrectly assumes that FPS is always at 60 but the amount of times rAF runs the callback is tied to several factors:

* is device running in power save mode?

* What's screen refresh rate?

* can the device keep a constant frame rate

For instance on my mac plugging and unplugging the power cord changes the frame rate from 60 to 30. Screenshot 2021-09-05 at 19 14 24 Screenshot 2021-09-05 at 19 15 05

I think there are 2 possible approaches:

1. Expose a function to cap the frame rate.

2. Change update signature from `fn update();` to  `fn update(timestamp:  WASM i64);` where timestamp is the argument that rAF passes to [loop](https://github.com/aduros/wasm4/blob/main/runtimes/web/src/index.js#L343) .

Sorry to trouble you @FaberVitale , but could you share how you were able to overlay the frame rate? It may be helpful for an issue I'm running into. Thanks :)

@FaberVitale
Copy link
Contributor

@DenialAdams

  • 1 Open "rendering" section

Screenshot 2021-11-01 at 07 58 00

  • 2 Toggle on "frame rendering stats"

Screenshot 2021-11-01 at 08 00 08

@rajsite
Copy link

rajsite commented Dec 22, 2022

Another use case (for either setting a lower bound FPS or reading the current time between frames, etc) could be for running on low-power devices. I'm using wasm4-aot to make a GameBoy Advance ROM out of a wasm-4 game. Using that approach is difficult to hit 60fps but it's still amazing it works at all!

Can see the difference between running the wasm-4 build in the browser and running in an online gba emulator (just click "Run Anyway" if it complains about the ROM format). The GBA rom isn't hitting 60 fps in the emulator so the game feels slow.

I ended up scaling all tick oriented constants (by eye-balling the movement speed) and the time scaled ROM in a gba emulator at least moves a little better even if the FPS is lower.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

6 participants