During my code spike to explore Tk
as a potential candidate for my Gameboy emulator screen I created this example. It uses threads to send events to the Tk main loop and consequently update the screen. Threads were important because I will need to trigger the screen update from other classes (specifically the cpu).
I chose to use the PPM format to store the image since it is easy to create and allows pixel manipulation. The downside is performance. The data to create the image is large (one byte per pixel). For the real thing I should be able to avoid memory allocation for every update by encapsulating the data in a class with helper methods for per pixel manipulation.
The update events must be sent from a different process. This will require forking and putting all of this code (including the require
statements) inside of the child process and pulling update events from a pipe.