News for hackers. Stuff that matters.
Re:Flecs – A fast entity component system for C and C++ by korijn on Tuesday April 04, 2023 @01:58AM (#35435669)
Any idea what a game loop would look like for an ECS engine that only renders a new frame when some animation is running or when the user is manipulating the camera, but not otherwise?

Let's not argue "why". Most obvious would be saving battery.

Re:Flecs – A fast entity component system for C and C++ by crabmusket on Tuesday April 04, 2023 @02:16AM (#35435762)
I've implemented exactly this at $WORK. We literally just set a global variable, "should render next frame", from input handlers and other important events. It's not pretty but it gets the job done. There are occasional bugs where someone forgets to set the render flag after some significant event. But the bug goes away as soon as the mouse is moved, so it's usually not a big deal. If we wanted we could also schedule a "render once per second" to bound the amount of time that visual issues would persist for.

I'm not sure how this translates to ECS architecture - probably depends on your library, but if you're in charge of the main loop it shouldn't be a problem.

Re:Flecs – A fast entity component system for C and C++ by nyanpasu64 on Tuesday April 04, 2023 @02:42AM (#35435943)
One trick I found is to gate mutable access to state behind a "transaction object" which sets a flag for each object mutated, then once the transaction object's destructor is called, recomputes all derived state based on which flags were set. Using a destructor might not be the best idea since I ended up not marking it noexcept, it may be better to add an explicit "recompute" function called at the end, but this breaks the convenience of `state().cursor_mut() = new_cursor;` automatically recomputing derived state once the state() object is destroyed.
Re:Flecs – A fast entity component system for C and C++ by pavlov on Tuesday April 04, 2023 @03:00AM (#35436066)
> “the bug goes away as soon as the mouse is moved, so it's usually not a big deal”

This reminds me of:

https://thedailywtf.com/articles/Hidden-Tax-Moves

"Wait, let me try," Bruce said, grabbing the mouse.

"Whoa, what just happened?" a teammate exclaimed. "What did you do? A ton of traffic just came through!"

Re:Flecs – A fast entity component system for C and C++ by crabmusket on Tuesday April 04, 2023 @04:05AM (#35436521)
Incredible. It's like the opposite of JS logic, where you are encouraged to do as little as possible in event handlers.
Re:Flecs – A fast entity component system for C and C++ by fileeditview on Tuesday April 04, 2023 @04:07AM (#35436538)
> After several meetings, it was decided that it wasn't the best idea to put critical code in an app's MouseMove event.

Prove that the story is real ;)

Re:Flecs – A fast entity component system for C and C++ by Ygg2 on Tuesday April 04, 2023 @04:50AM (#35436877)
Experienced worse.

At one company we had a deployment script written in English. That support from India would execute. Except they would occasionally inject their undocumented codes to fix their own issues.

That and the component oriented distributed monolith that ran on single machine. It was monolith architecture but it was made to run on multiple machines. However it was never distributed and starting the components was an arcane science.

Re:Flecs – A fast entity component system for C and C++ by fileeditview on Tuesday April 04, 2023 @07:39AM (#35438039)
Oh i guess we all have these horror stories.. I was more hinting at the fact that it took several meetings to decide something obvious.
Re:Flecs – A fast entity component system for C and C++ by illiarian on Tuesday April 04, 2023 @06:43AM (#35437571)
> We literally just set a global variable

I feel like game programming in general is one big "we set a global variable" :)

Re:Flecs – A fast entity component system for C and C++ by jiggawatts on Tuesday April 04, 2023 @02:03AM (#35435701)
I wrote a game engine that did that, because it was for a very simple, largely static scene.

You would implement it almost the same as a standard Windows GUI -- using an event handler loop that marks the screen as "dirty" and needing a refresh if it has changed.

Where it would immediately break down is ongoing animations. If you stopped moving your mouse, then animations would stop. Of course, you can set up the event loop to continue triggering if there are animations going on, but then you basically have a standard game engine loop running most of the time.

Personally, I'd be happy if games simply paused rendering when minimised. Some games do this, some don't.

Re:Flecs – A fast entity component system for C and C++ by korijn on Tuesday April 04, 2023 @02:05AM (#35435706)
Well, if those animations are things such as camera smoothing, it won't have such an impact. But I see your point.

Thanks for entertaining the idea!

Re:Flecs – A fast entity component system for C and C++ by cnity on Tuesday April 04, 2023 @04:31AM (#35436730)
You just have to include all the parameters of the animation as part of your "dirtiness" calculation. I reckon in practice this means centralising pretty much _all game state_ in some way that you force the dirtiness state to be set correctly.
Re:Flecs – A fast entity component system for C and C++ by Cthulhu_ on Tuesday April 04, 2023 @03:44AM (#35436378)
Some games (I'm thinking FFXIV) will run on a lower framerate (15fps) while minimized or, in that game's case, if the player is idle for an X amount of time, that's a pretty decent compromise.

But yeah, games don't need to render any frames while minimized. I do feel like this should be the operating system's responsibility as well, but then, a lot of games still have the graphics rendering and other parts of the game linked tightly together.

Re:Flecs – A fast entity component system for C and C++ by exDM69 on Tuesday April 04, 2023 @09:19AM (#35439145)
> I'd be happy if games simply paused rendering when minimised. Some games do this, some don't.

Yes, please.

It drives me nuts to have GPU fans spinning when game is minimized, paused, in the loading screen, turn based and no inputs, etc.

Re:Flecs – A fast entity component system for C and C++ by tbillington on Tuesday April 04, 2023 @02:48AM (#35435991)
The bevy engine (ECS based) has an `UpdateMode` that specifies "Continuous" vs "Reactive" https://docs.rs/bevy/0.10.1/bevy/winit/enum.UpdateMode.html.

Here's an example using it, it's well suited for "desktop applications", the shorthand to use it is `desktop_app()` https://github.com/bevyengine/bevy/blob/1c5c94715cb17cda5ae2....

https://github.com/bevyengine/bevy/

Re:Flecs – A fast entity component system for C and C++ by charcircuit on Tuesday April 04, 2023 @03:26AM (#35436265)
How often your app takes in events != How often your app outputs frames

The OP only mentioned that he wanted to stop outputting frames. Animations, physics, input, etc should still happen normally.

Re:Flecs – A fast entity component system for C and C++ by pengaru on Tuesday April 04, 2023 @03:17AM (#35436204)
ECS isn't really relevant to the question

game loops usually call into simulate and render entrypoints.

There's state potentially mutated by simulate.. render looks at that state and potentially produces a new output to present on-screen.

Typically the render entrypoint returns a status indicating whether a new output was produced or not to put on-screen. If no output was produced you don't flip pages. So in a quiescent situation with nothing changing, render returns "don't flip" and you just leave the existing page displayed til the next iteration.

How you track the internal game state / dirty vs. clean state is implementation detail behind those simulate and render entrypoints and depends greatly on the type of game.

Re:Flecs – A fast entity component system for C and C++ by Cthulhu_ on Tuesday April 04, 2023 @03:42AM (#35436358)
I'd say this is something that can be done on a driver / GPU level; if the inputs to render the next frame are the same, it can just reuse the previous one.

That's probably oversimplifying it, I don't know anything about graphics programming.

Re:Flecs – A fast entity component system for C and C++ by cnity on Tuesday April 04, 2023 @04:25AM (#35436677)
This doesn't really work. By the time the driver or GPU could "know" that the frame is the same, it's already done a lot of the heavy lifting. As in, it has basically still rendered the next frame by this point. Actually writing the result to the framebuffer is not expensive.
Re:Flecs – A fast entity component system for C and C++ by ww520 on Tuesday April 04, 2023 @05:35AM (#35437134)
For energy efficient game loop, it still runs in a fixed frequency, e.g. in 60fps or 30fps. For each frame, the time is spent on event processing, state update, rendering, and sleep. Some of the steps can be skipped. The goal is to lower the times spent in the non-sleeping steps, and sleeps more for each frame.

Event queues are used to track things in long duration across multiple frames, like user input, animation sequences, sound sequences, pending state update, pending physics update, pending AI update, etc.

In each frame, check the event queues. If they are empty, sleep. Otherwise, process the events in the queues. For user input, do the state update correspond to the input, like update the camera position.

For ongoing animation, advance the steps in the sequences. When an animation sequence completes, remove it from the queue. Same for sound.

For pending state update, run the update. E.g. the ship is moving, do the state update on velocity and position. For ECS, you might end up calling or not calling the systems for the item type for batch update. E.g. when the bullet queue is not empty, you might just run the bullet related systems over all bullets for simplicity. When all the bullets end and the queue is empty, the bullet related systems are not run.

The physics queue would have items being simulated. Run those. Physics could run at a slower rate like 5fps, so the queued items can be skipped most of the times. Same with the AI items in the AI queue.

Whenever there's a state update, mark the screen as outdated. At the rendering step, run rendering if the screen is outdated. It's simpler to just run the whole rendering for any change. Ideally, when there's no events in the queue or the state updates are not triggered, there's no need for rendering.

This applies to using ECS or not. ECS is for tracking states of entities and do batch update with systems.

Re:Flecs – A fast entity component system for C and C++ by ajmmertens on Tuesday April 04, 2023 @10:52AM (#35440585)
Definitely possible. I know of a project that used an event-based architecture to decide when to run ECS systems.