by RLS
Graphics Programing Language

This is a language specifically designed for generating graphics files ( such as jpeg and png ). The overall system looks a bit like basic but has more advanced data types. The aim is to separate the geometry of a shape and the rendering details such as colour and pen width.

It should be useful for data presentation and simple technical diagrams. It main strength is where there a similar repeated elements that need to be placed precisely.

[= 	Tutorial Sample - Tic Tac Toe 3A

Command:
	grarls -png 320x200 -qty 3 tictactoe_3A.grs

Purpose:
=]


[= Main Configuration Parameters =]
Const Number PageWidth = 320, PageHeight = 200;
Const Number VBorder = 20;
Const Number Cells = 3;

[= Derived Constants =]
Const Number CellSize = ( PageHeight - ( 2 * VBorder ) ) // Cells;
Const Number TokenSize = CellSize * 0.8;
Const Number HBorder = ( PageWidth - Cells * CellSize ) // 2;

[= enumerated type for the players =]
Type TPlayer = ( Player_Nought = "O", Player_Cross = "X" );

[= a structure with all the information to specify a move =]
Type MoveRecord = { TPlayer piece, Number row, Number col };

[= symbol type - a Shape type =]
Type TPlayerSymbol = Shape(Number size);

[= type for the dispatch table =]
Type TDispatchTable = TPlayerSymbol[TPlayer];

[= the actual dispatch table =]
Const TDispatchTable Dispatcher = [Nought, Cross];

[= an open array to hold an entire game =]
Type MoveArray = MoveRecord[];


[=		PROGRAM
		-------
	    This will read in the data file ande draw the game.
	In a real program the file name would be passed as a program parameter
	and there would also be a routine for checking the data.
=]
Program()

	MoveArray movelist;

    Begin

	[= read in the game file =]
	movelist := ReadData("TUTSAMPLES:/tictactoe.txt");

	[= draw the game - board position is set here =]
	Game(movelist) => { HBorder, VBorder };

    End;


[=		GAME
		----
	    Draw the grid then all the pieces.
	Origin: top left corner of board.

=]
Shape Game(Const Ref MoveArray movelist)

    Begin

	[= draw the main grid =]
	Grid();

	[= draw all the moves in the array =]
	Over movelist As move Do

		[= use the dispatch array to draw the piece =]
		Dispatcher[move.piece](TokenSize) => CellCentre(move.row, move.col);

	EndOver;

    End;

[=		GRID
		----
	    Draw the grid.
	Origin: Top left of board.
=]
Shape Grid()
	Number i;
    Begin

	For i From 1 To Cells - 1 Do

		LineTo( { 0, Cells * CellSize } ) => {  i * CellSize, 0 };	
		LineTo( { Cells * CellSize, 0 } ) => { 0, i * CellSize };

	EndFor;	

    End;


[=		NOUGHT
		------
	    Just a simple circle.
	Origin: Centre of the symbol.
=]
Shape Nought(Number size)
    Begin
	Circle(size / 2 );
    End;


[=		CROSS
		-----
	    Two diagonal line.
	Origin: Centre of the symbol.
=]
Shape Cross(Number size)
	Number len = size / 2;
    Begin
	Line( {-len, -len}, {len, len} );
	Line( {len, -len}, {-len, len} );
    End;


[=		CELL CENTRE
		-----------
	    Calculate pixel coordinates of cell centre relative
	to the top left corner of the board.
=]
Function Point CellCentre(Number row, Number col)
    Begin

	[= return a calculated Point object =]
	Return { ( col - 1/2 ) * CellSize,  ( row - 1/2 ) * CellSize  };

    End;
Information updated 06/03/19
Note: This entry has not yet been moderated.
View Comments