Graphics Layer Abstraction Module
use GLAM::OpenGL;
# use GLAM::SDL;
my $gl =new GLAM({height=>400,width=>800,dt=>1/60});
my $gameObject =new GameObject($gl->{canvas},$GameParametersAsHashref);
$gl->mainLoop(\&update);
sub update{
my ($app)=@_;
if($app->key("esc") || $app->key("q")){# keys to exit GameLoop
print("goodbye!\n");
exit(0);
}
$gameObject->update($app->{dt},<List_of_IO_etc>);
}
Using interactive graphics typically requires a Graphics Library, which provides an API to connect both user input to the display output. SDL3 and OpenGL are available to Perl, and have their own Modules on CPAN. GLAM attempts to deliver a unified API with common functions e.g. drawing primitives and user input identical whichever GL is used. I am not good at programming graphics nor do I code often, so a tool that eases graphics layer coding may be helpful. GLAM bundles the IO, graphics primitives and a simple 2d Vector toolkit in one Module, GLAM::OpenGL for using GLFW (OpenGL), and GLAM::SDL for SDL3. The code has been adapted from Reddit submissions by u/First_Ad8230 and u/s_throwaway_r
A GLAM game essentially sets up window, which is a drawable canvas for graphical elements which, in turn, are merely muliple triangles. these elements are created in a GL agostic way. The window updates every "tick". At every tick the keyboard and mouse status is collected and is accessible to the Game Logic in consistent way regardless if GL. The game logic has also access to a Vector Math toolkit which will simplify handling the positions of the elements.
A basic Ho-To is provided, along with an example skeleton to help get started
The rope demonstration that triggered this is the first program that uses GLAM.
A single line can be changed to use either OpenGL or SDL.
rope-GLAM.webm
GLAM depends on the underlying Graphics layer and the corresponding Perl Modules to be installed. The installation of these has not been trivial for me. I would be pleased if someone could provide details of reliable methods for different systems. In mine (Ubuntu 20.04) this worked.
Needs gl and glfw.
sudo apt install libopengl-perl
sudo apt install libglfw3
sudo apt install libglfw3-dev
sudo cpanm OpenGL OpenGL::GLFW
Needs Perl 5.40 minimum. I had to use Perlbrew (something I am not familiar with), and even then I had difficulties.
- Install as many of the pre-requisites as possible on the SDL3 wiki.
- Manually install libraries going to the logs of every attempt as cpanm SDL3 failing.
- I needed to install
libsslby installing debs found at libssl1.1 and its associatedlibssl-dev sudo apt install libpipewire-0.2-1 libjack0 libjack-dev libpipewire-0.2-dev libpulse-dev libsndio7.0 libsndio-devsudo apt install libxss1 and libxss-dev- After this
cpanm SDL3worked for me.
GLAM's operations are subject to change as it is new.
- When the GLAM object is created it generates a Canvas object which provides a window and graphics drawing primitives.
- This Canvas Object is passed to the Game Object, along with any initialisation parameters
- The Game object is initialised. It stores the Game state, provides the game logic, has an update function that responds to any keys pressed, mouse state, and the time elapsed etc. at each Game loop.
- When the GLAM's Game loop is started,
- does some house keeping
- the loop checks for any end conditions
- submits the mouse,keyboard data to the Game Object's logic
- the Game object updates its statee, prepares the screen update before returning to the game loop
- loop the refreshes the screen buffer etc and continues
- if exit condition met GLAM tidies up and exits
my $gl =new GLAM({height=>$WINDOW_HEIGHT,width=>$WINDOW_WIDTH,dt=>$DELTA_TIME_SEC});
Creates a window for the application and returns a GLAM Object which has
as a property a canvas object (e.g. $gl->{canvas} in above example ).
This canvas object can be provided for any object that needs to draw on
the canvas (the game object, UI elements etc). At each gameloop, the applications
checks mouseposition, the state of the left mouse botton and the "escape" and "q"
keys on the keyboard. Clearly all the keys could be checked, but for this early version#
the these two keys are used to exit the application.
The application may respond to inputs. These are polled at the beginning of the gameloop
and passed to the game Object for example thus:
$carrom->update($app->{dt},$app->mousePosition(),$app->button("left"));
my $qPressed=$app->key("q")
Checks the state of the "q" key. It would certainly be possible to check other keys...but for
this active iteration, while we work out the best way of handling key presses, just the "q" v
ab=nd "esxc" keys are checked at each gameLoop, the examples shown simply use these to abort the
application.
$app->mousePosition()
Returns the mouseposition as a Vector2 object.
$app->button("left")
Returns the state of the Left mouse button...true if pressed, false otherwise.
This starts a loop, and takes a subroutine ref that is run every loop cycle. This subroutine might typically
- test an exit status and exit is this is met
- pass the input states (mouse position, button, time delta to the Game Object for processing
- updating the game state, generating the next frame for rendering
$gl->mainLoop(\&update);
sub update{
my ($app)=@_;
if($app->key("esc") || $app->key("q")){
print("goodbye!\n");
exit(0);
}
$carrom->update($app->{dt},$app->mousePosition(),$app->button("left"));
}
The Canvas object is built with the GLAM object and really only uses a 1) triangle drawer and 2) colour changer from the backend and from these, derives everything else
Text Stared v0.05 Line Drawings v0.05 AOV 3D objects Streamline background and foreground rendering
0.05
- started making Logo, Text and Button Helpers
- Polylines and composite shapes
v0.04
- added named colours
- updated Carrom-GLAM.pl to be nearly complete game
v0.03
- allow multiple ways of getting a Vector2 at inception
- correct for differences in coordinate geometry between OpenGL and SDL
- create an interactive Carrom like game
- Ball.pm has additional components for surface friction
- Acknowledge developers of perl OpenGL and perl SDL.
carrom.mp4
v 0.02 Ball physics
- some additional Vector2 Methods
- (consider future overloading for Vector arithmetic)
- Added Ball.pm for initial ball physics in 2D
- Created Bounce-GLAM.pl to demo ball physics
bounce-GLAM.webm
v 0.01 Initial Commit
- GLAM::SDL and GAM::OpenGL
- allows a Rope-GLAM.pl to work in both in OpenGL and SDL3