SDL_Chomik tutorial

This is a tutorial for the SDL_Chomik programming language. The language is an extension of the Chomik programming language, i.e. all code working in Chomik can be used in SDL_Chomik (but not vice versa!). The interpreter sdl_chomik is based on the SDL2 library.


Hello World

Let us begin traditionally with a Hello World program:


let sdl loop body = value code
{
  <print "Hello world">;
};
<sdl loop>;

"sdl loop" is a predefined variable of type code. It can be therefore executed. It is actually implemented in C++, but searches for a code variable "sdl loop body". If found - it will be constantly called (on every iteration).


The simplest program

The simplest SDL_chomik program consists just of the "sdl loop" execution.


<sdl loop>;

It creates a window of a fixed size. There is no way in SDL_Chomik to resize it (as for now). Note that while we execute SDL_Chomik code we jump to C++ (to execute "sdl loop body") and then again jump into SDL_Chomik (to execute the "sdl loop body", if defined).


The key handlers

Before we run the "sdl loop" we are free to redefine the code variables "on key return", "on key backspace", "on key space", "on key escape". Similarly there are the code variables for the arrow keys ("on key up", "on key down", "on key left", "on key right").


let on key return = value code
{
  <print "return">;
};

let on key escape = value code
{
  <print "escape">;
};

<sdl loop>;

Please execute the above program with the sdl_chomik interpreter. Then press Return/Escape and observe how SDL_Chomik uses your handlers to handle the key events.


Show image

In order to use images we need to download them from a file. It is customary to use the "chomik.png" image (a public domain hamster image). After creating an image (with "create new image ..." predefined code family of variables) the integer variable "the created image index" is assigned with a new value identifying the image. It is usually stored in a user defined integer variable (in the example below "chomik image index" and "background image index"). In order to show the image we use (in the sdl loop body) the predefined code family of variables named "show image". It gets the following integer "parameters":

  • image index
  • x coordinate
  • y coordinate



<create new image "../image/chomik.png">;
variable chomik image index: integer;
let chomik image index = <the created image index>;

<create new image "../image/background.png">;
variable background image index: integer;
let background image index = <the created image index>;

let sdl loop body = value code
{
  <show image <background image index> 0 0 >;
  <show image <chomik image index> 0 0 >;
};

<sdl loop>;

Executing the above program looks as following:



Show text

In order to show text we need to create a new font using the "create new font" code family of variables. We assume that a ttf font file exists in the location provided. Then the integer variable "the created font index" will be assigned. We can copy it to a user defined variable for later use. In order to show a text (in the below example "hello world!" at the coordinates 200,100) we need to call "show text ...". It requires the following "parameters":

  • font index
  • x coordinate
  • y coordinate



<create new image "../image/chomik.png">;
variable chomik image index: integer;
let chomik image index = <the created image index>;

<create new image "../image/background.png">;
variable background image index: integer;
let background image index = <the created image index>;

<create new font "../font/PlayfairDisplay-Regular.ttf" 32>;
variable my font index:integer;
let my font index = <the created font index>;


let sdl loop body = value code
{
  <show image <background image index> 0 0 >;
  <show image <chomik image index> 0 0 >;
  <show text <my font index> "hello world!" 200 100>;
};

<sdl loop>;

Executing the above program looks as following: