CobraPy: baby steps
Some context: I am doing a sort of modern retro-programming environment-thingie.
So ... I caved. I caved and started typing.
It's still just exploratory, fooling around, but I wanted to see whether the vague ideas I had about how this could, maybe work were right, and hey, they are!
I wanted a text mode. Something with N rows and M columns where you could just say "put this text there". Done. In the most basic possible way that works.
pic.twitter.com/CgBOahWCJq— Roberto H. Alsina (@ralsina) October 6, 2020
print_at function that takes row, column and text, and does what it says in the label.
Immediate Graphics Mode
In these old computers you could just draw in the graphics memory. In 2020 ... not so much. So, I implemented a sort of offscreen buffer. You draw there, and it gets blitted a lot to screen. Ends up working like a flicker-free double-buffered screen! Nice!
ellipse which ... draws an ellipse. I know, surprising.
October 6, 2020
Interpreter / Prompt
It would not be 80s-like if you could not do those things interactively. So, yes, you can.
Parece que si. pic.twitter.com/wQu7mWkxGh— Roberto H. Alsina (@ralsina) October 6, 2020
Now that was interesting. you see, nowadays you are supposed to have an event loop. The computer is there, idling and when something happens, then it does something. So, you clicked? It reacts. 10 milliseconds passed? It does its thing.
In the 80s it was not like that. Your program started and it owned the machine. It went through your code, running it as fast as it could with its little two-megaherz-heart until it ran out of code and then it was done.
So, how to make it act like that?
- Start the event loop.
- Start a second thread with a prompt.
- Have the user interact with the prompt.
- When the user presses
Enter... send the command to the event loop via a queue.
- When the event arrives, do the thing.
And yes, it works! It's probably horrible for performance, but it does work just fine, and it's much, much easier to make a working program fast than it is to make a fast program work.
So, what's next?
I may try to integrate the prompt and the window where the text and such is displayed (right now it's in a terminal) ... maybe sprites.
- Make the API nice and useful (a few weeks of work)
- Make the thing run well (a few days of work)
- Do the janitorial work to make it usable by someone else (a couple days of work)
- Support it, document it, etc (8 to 30 years of work)
The Nerdy Details
- To do the graphics I am using pyglet
- To do offscreen drawing I am using Pillow
- Tooling includes the usual suspects: Python 3.8, poetry, pylint, flake8
- Code (all 50 lines or so!) in GitHub
Yes, this means this "80s" thing is actually multithreaded hardware-accelerated OpenGL graphics. I know, I know.