Skip to content
saiftynetPublic

About

Graphics Layer Abstraction Module

Resources

Stars

5 stars

Watchers

0 watching

Forks

Latest commit

 

History

64 Commits

Folders and files

Repository files navigation

GLAM

Graphics Layer Abstraction Module GLAM

SYNOPSIS

    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>);
   }

Description

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

How it works

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 first program

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

Installation

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.

OpenGL

Needs gl and glfw.

    sudo apt install libopengl-perl
    sudo apt install libglfw3
    sudo apt install libglfw3-dev
    sudo cpanm OpenGL OpenGL::GLFW

SDL3

Needs Perl 5.40 minimum. I had to use Perlbrew (something I am not familiar with), and even then I had difficulties.

  1. Install as many of the pre-requisites as possible on the SDL3 wiki.
  2. Manually install libraries going to the logs of every attempt as cpanm SDL3 failing.
  3. I needed to install libssl by installing debs found at libssl1.1 and its associated libssl-dev
  4. sudo apt install libpipewire-0.2-1 libjack0 libjack-dev libpipewire-0.2-dev libpulse-dev libsndio7.0 libsndio-dev
  5. sudo apt install libxss1 and libxss-dev
  6. After this cpanm SDL3 worked for me.

The Program Flow

GLAM's operations are subject to change as it is new.

  1. When the GLAM object is created it generates a Canvas object which provides a window and graphics drawing primitives.
  2. This Canvas Object is passed to the Game Object, along with any initialisation parameters
  3. 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.
  4. When the GLAM's Game loop is started,
    1. does some house keeping
    2. the loop checks for any end conditions
    3. submits the mouse,keyboard data to the Game Object's logic
    4. the Game object updates its statee, prepares the screen update before returning to the game loop
    5. loop the refreshes the screen buffer etc and continues
    6. if exit condition met GLAM tidies up and exits
GLAM

OBJECTS and Methods

GLAM::OpenGL and GLAM::SDL (maybe GLAM::Any)

new

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.

Inputs

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"));

key

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.

mousePosition

$app->mousePosition() Returns the mouseposition as a Vector2 object.

button

$app->button("left")

Returns the state of the Left mouse button...true if pressed, false otherwise.

mainloop

This starts a loop, and takes a subroutine ref that is run every loop cycle. This subroutine might typically

  1. test an exit status and exit is this is met
  2. pass the input states (mouse position, button, time delta to the Game Object for processing
  3. 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"));
}

SDLCanvas and GLCanvas

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

triangle

thickLine

circle

Vector2

add

diff

mul

div

dot

length

unitNormal

Ball Helper

the Writer Helper

The UI Helper

The Logo Helper

TO DO

Text Stared v0.05 Line Drawings v0.05 AOV 3D objects Streamline background and foreground rendering

CHANGES

0.05

  • started making Logo, Text and Button Helpers
  • Polylines and composite shapes
GLAM Text

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

Acknowledgements

About

Graphics Layer Abstraction Module

Resources

Stars

5 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages