I’m sure you are all waiting for this “missing part”. The code that is needed to make the engine work on PiTrex. Here it comes!
How the PiTrex library works
The team behind the PiTrex (see also Newsletter #0) has done an excellent job simplifying the access for 3rd party developers. All the needed functions are defined in vectrexInterface.h
and described in more detail on the Project Wiki.
But in simple words, it happens by doing the following:
Initialize interface
vectrexinit()
- Initialize the PiTrexv_setName()
- Set a game’s namev_init()
- Initialize the Vectrex interface
For each “frame”
v_waitRecal()
- This function must be called once per "round" to ensure a stable image and to supply a timing frame of referencev_readButtons()
- Read Joystick buttonsv_readJoystick1Analog()
- Read Joystick directionsv_setBrightness()
- (Re-)set brightness
Draw lines and text -
v_directDraw32()
to draw lines andv_printStringRaster()
to print a text
It’s a straightforward API and it looks quite similar to the game loop we use in Vexxon (Init(), Update(), and Cleanup()). To “draw” on the screen we use (for now) the two functions listed above.
Integration in Engine
Integrating this API in any game engine is therefore not very complex. It “just” needs a game engine that does all rendering by drawing a line. This is more complex to do for a modern game engine but quite simple for any retro-style game engine like the one we use here.
To make the engine itself platform independent it calls functions that begin with platform_ and must be implemented for each platform it runs on. I have already done that for Windows, macOS, and Linux and now we do the same for the PiTrex. For that the engine expects the following functions to be provided by each platform:
platform_set_pixel()
- Not used on the PiTrexplatform_draw_line()
- Calls v_directDraw32 via v_line in rb_pitrex_windows.cplatform_on_frame()
- Callsv_WaitRecal()
,v_readButtons()
andv_readJoystick1Analog()
viapitrex_frame()
inrb_pitrex_main.cpp
platform_draw_string()
- Callsv_printStringRaster()
viapitrex_draw_vtext()
inrb_pitrex_platform.c
Of course, it would be possible to keep all functionality in one file, but I have decided to split the PiTrex integration into three files:
rb_pitrex_main.cpp
- Bridge file that is also written in C++ and contains the platform callbacks described above. Also, it contains the main() function for the PiTrex and starts/interacts with the game engine.rb_pitrex_platform.c
- Abstraction layer that calls the Pitrex and Vectrex interface.rb_pitrex_main.cpp
never calls directly any function from the API. If the API ever changes, this is the only file in the project that must be updated.rb_pitrex_window.c
- This is a helper class that is more or less a direct copy ofwindows.h
from the PiTrex library and allows the usage of a user coordinate space
That’s it 😁👍 and all it needs to make the code work on the PiTrex!
Try it out and as always all the details on how to build the project on all platforms are described in the code repository on GitHub.
In the upcoming newsletters, the main focus is now on Vexxon - The Game but of course, I will also optimize the engine slightly over time and will add some helpful tools to simplify the programming. Stay tuned and thanks for subscribing!