Tamás Bálint Misius 820f44a716
Do rendering on another thread when possible
"Possible" here means "when there are no particles with Lua graphics functions that haven't reported that they don't want to be called again and there are no beforesimdraw/aftersimdraw event handlers". I put my trust in Simulation::elementCount here; I sure hope it's always correct. If there is any of these, the renderer thread is paused and rendering happens on the main thread. To be clear, these are the situations in which Lua code is called with LuaScriptInterface::eventTraits having the eventTraitSimGraphics bit set.

If the detection of this situation is somehow flawed, there will still be no crashes because when running on the renderer thread, Renderer is told to not call Lua functions, and GameController::BeforeSimDraw/AfterSimDraw is not called by GameView::RendererThread. The worst that can happen is that some particles are not rendered properly.

Renderer settings (color and display modes, deco state, finding-element state, etc.) are managed by GameModel in the form of a RendererSettings and are passed to Renderer before each frame. The RenderableSimulation that Renderer works off of is also configured before each frame, either to point to a copy dedicated to the renderer thread, or directly to GameModel's Simulation, if the renderer thread is paused. Similarly, the result of the rendering is managed by GameView in the form of a RendererFrame, to enable e.g. sampling with deco tools without having to pause the renderer thread.

Each time GameView::OnDraw is called, it checks whether rendering is allowed to happen on a different thread (see above for conditions).

If it is, but the renderer thread is absent, GameView starts it and dispatches it (provides with settings and simulation data) right away. At this point, the renderer thread is definitely rendering a frame. GameView waits for this to finish and exchanges data with the renderer once it's done: it takes the result of the rendering and also dispatches the renderer thread again. This introduces a one-frame delay in rendering, which we can live with.

If rendering is not allowed to happen on a different thread, GameView waits for the renderer thread to finish what it's currently doing and pauses it, then proceeds to render the frame by itself.

Affecting this condition (whether rendering is allowed to happen on a different) by installing or removing event handlers, or clearing graphics cache (which, note, requires acquiring a unique lock on it), while the renderer thread is working, is not an issue because the renderer thread doesn't bother with event handlers, and because it acquires a shared lock on the graphics cache for rendering.

The GameModel's Renderer is thus still primarily managed by GameView, potentially in two different threads, in a strictly non-overlapping manner. The exception to this is when RenderView butts in and starts doing renders of its own; in such cases, the renderer thread must be paused explicitly, as RenderView does by calling GameView::PauseRendererThread.
2024-08-21 23:48:13 +02:00
2024-07-03 20:51:05 +02:00
2024-08-07 10:56:56 +02:00
2021-12-08 14:33:28 +01:00
2022-12-25 11:13:46 +01:00
2012-05-24 19:01:00 +01:00
2024-08-13 20:43:39 +02:00
2024-04-01 19:54:39 -04:00

The Powder Toy - April 2024

Get the latest version from the Powder Toy website.

To use online features such as saving, you need to register an account. You can also visit the official TPT forum.

Have you ever wanted to blow something up? Or maybe you always dreamt of operating an atomic power plant? Do you have a will to develop your own CPU? The Powder Toy lets you to do all of these, and even more!

The Powder Toy is a free physics sandbox game, which simulates air pressure and velocity, heat, gravity and a countless number of interactions between different substances! The game provides you with various building materials, liquids, gases and electronic components which can be used to construct complex machines, guns, bombs, realistic terrains and almost anything else. You can then mine them and watch cool explosions, add intricate wirings, play with little stickmen or operate your machine. You can browse and play thousands of different saves made by the community or upload your own we welcome your creations!

There is a Lua API you can automate your work or even make plugins for the game. The Powder Toy is free and the source code is distributed under the GNU General Public License, so you can modify the game yourself or help with development.

Build instructions

See the Powder Toy Development Help section on the main page of the wiki.

Thanks

  • Stanislaw K Skowronek - Designed the original
  • Simon Robertshaw
  • Skresanov Savely
  • cracker64
  • Catelite
  • Victoria Hoyle
  • Nathan Cousins
  • jacksonmj
  • Felix Wallin
  • Lieuwe Mosch
  • Anthony Boot
  • Me4502
  • MaksProg
  • jacob1
  • mniip
  • LBPHacker

Libraries and other assets used

Instructions

Click on the elements with the mouse and draw in the field, like in MS Paint. The rest of the game is learning what happens next.

Controls

Key Action
TAB Switch between circle/square/triangle brush
Space Pause
Q / Esc Quit
Z Zoom
S Save stamp (use with Ctrl when STK2 is out)
L Load last saved stamp
K Stamp library
0-9 Set view mode
P / F2 Save screenshot as .png
E Bring up element search
F Pause and step to next frame
G Increase grid size
Shift + G Decrease grid size
H Show/Hide HUD
Ctrl + H / F1 Show intro text
D / F3 Debug mode (use with Ctrl when STK2 is out)
I Invert Pressure and Velocity map
W Cycle gravity modes (use with Ctrl when STK2 is out)
Y Cycle air modes
Ctrl + E Cycle edge modes
B Enter decoration editor menu
Ctrl + B Toggle decorations on/off
N Toggle Newtonian Gravity on/off
U Toggle ambient heat on/off
Ctrl + I Install powder toy, for loading saves/stamps by double clicking
Backtick Toggle console
= Reset pressure and velocity map
Ctrl + = Reset Electricity
[ Decrease brush size
] Increase brush size
Alt + [ Decrease brush size by 1
Alt + ] Increase brush size by 1
Ctrl + C/V/X Copy/Paste/Cut
Ctrl + Z Undo
Ctrl + Y Redo
Ctrl + Cursor drag Rectangle
Shift + Cursor drag Line
Middle click Sample element
Alt + Left click Sample element
Mouse scroll Change brush size
Ctrl + Mouse scroll Change vertical brush size
Shift + Mouse scroll Change horizontal brush size
Shift + R Horizontal mirror for selected area when pasting stamps
Ctrl + Shift + R Vertical mirror for selected area when pasting stamps
R Rotate selected area counterclockwise when pasting stamps
F11 Toggle fullscreen

Command Line

Command Description Example
scale:SIZE Change window scale factor scale:2
kiosk Fullscreen mode
proxy:SERVER[:PORT] Proxy server to use proxy:wwwcache.lancs.ac.uk:8080
open FILE Opens the file as a stamp or game save
ddir DIRECTORY Directory used for saving stamps and preferences
ptsave:SAVEID Open online save, used by ptsave: URLs ptsave:2198
disable-network Disables internet connections
disable-bluescreen Disable bluescreen handler
redirect Redirects output to stdout.txt / stderr.txt
cafile:CAFILE Set certificate bundle path cafile:/etc/ssl/certs/ca-certificates.crt
capath:CAPATH Set certificate directory path capath:/etc/ssl/certs
Languages
C++ 94.5%
Meson 2.2%
C 1.1%
Python 0.9%
Lua 0.8%
Other 0.3%